sqldsl 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README CHANGED
@@ -89,3 +89,6 @@ See the tests for more examples
89
89
  assert_equal expected, statement.to_sql
90
90
  end
91
91
  end
92
+
93
+ === Contributors
94
+ Matt Deiters
@@ -0,0 +1,7 @@
1
+ class DistinctSelect < Select #:nodoc:
2
+ class << self
3
+ def [](*columns)
4
+ self.new("select distinct #{create_columns_list(columns)}")
5
+ end
6
+ end
7
+ end
data/lib/hash.rb ADDED
@@ -0,0 +1,16 @@
1
+ class Hash
2
+
3
+ # call-seq: hash.to_sql -> a_string
4
+ #
5
+ # Returns a string with single quotes escaped.
6
+ #
7
+ # {:column1 => :foo, :column2 => :bar}.to_sql #=> "column1 as foo, column2 as bar"
8
+ def to_sql
9
+ result = []
10
+ each_pair do |key, value|
11
+ result << "#{key} as #{value}".to_sym
12
+ end
13
+ result.sort{ |x,y| x.to_s <=> y.to_s }.to_sql
14
+ end
15
+
16
+ end
data/lib/numeric.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  class Numeric
2
+ include WhereValue
2
3
  # call-seq: numeric.to_sql -> a_numeric
3
4
  #
4
5
  # Returns self
data/lib/select.rb CHANGED
@@ -6,8 +6,22 @@ class Select < SqlStatement
6
6
  #
7
7
  # Select[1, :column1, 'book'].to_sql #=> "select 1, column1, 'book'"
8
8
  def [](*columns)
9
- self.new("select #{columns.collect{ |column| column.to_sql }.join(', ')}")
9
+ self.new("select #{create_columns_list(columns)}")
10
10
  end
11
+
12
+ def create_columns_list(columns) #:nodoc:
13
+ columns.collect{ |column| column.to_sql }.join(', ')
14
+ end
15
+
16
+ # call-seq: Select.distinct -> a_select
17
+ #
18
+ # Returns a Select class that appends 'distinct' to the select clause
19
+ #
20
+ # Select.distinct[1, :column1, 'book'].to_sql #=> "select distinct 1, column1, 'book'"
21
+ def distinct
22
+ DistinctSelect
23
+ end
24
+
11
25
  end
12
26
 
13
27
  # call-seq: select.from -> a_select
@@ -26,7 +40,17 @@ class Select < SqlStatement
26
40
  #
27
41
  # Select[1, :column1, 'book'].from[:table1, :table2].to_sql #=> "select 1, column1, 'book' from table1, table2"
28
42
  def [](*table_names)
29
- @to_sql += table_names.collect{ |table| table.to_s }.sort.join(', ')
43
+ @to_sql += table_names.sort{ |x,y| x.to_s <=> y.to_s }.collect{ |table| table.to_s }.sort.join(', ')
44
+ self
45
+ end
46
+
47
+ # call-seq: select.order_by -> a_select
48
+ #
49
+ # Returns a Select instance with the order arguments, joined by ', ' appended to the SQL statement.
50
+ #
51
+ # Select[1].from[:table1].order_by(:column1, :column2).to_sql #=> "select 1 from table1 order by column1, column2"
52
+ def order_by(*column)
53
+ @to_sql << " order by #{column.to_sql}"
30
54
  self
31
55
  end
32
56
 
data/lib/sqldsl.rb CHANGED
@@ -1,12 +1,15 @@
1
+ require File.dirname(__FILE__) + '/where_value.rb'
1
2
  require File.dirname(__FILE__) + '/object.rb'
2
3
  require File.dirname(__FILE__) + '/symbol.rb'
3
4
  require File.dirname(__FILE__) + '/array.rb'
4
5
  require File.dirname(__FILE__) + '/string.rb'
6
+ require File.dirname(__FILE__) + '/hash.rb'
5
7
  require File.dirname(__FILE__) + '/numeric.rb'
6
8
  require File.dirname(__FILE__) + '/time.rb'
7
9
  require File.dirname(__FILE__) + '/sql_statement.rb'
8
10
  require File.dirname(__FILE__) + '/where_builder.rb'
9
11
  require File.dirname(__FILE__) + '/select.rb'
12
+ require File.dirname(__FILE__) + '/distinct_select.rb'
10
13
  require File.dirname(__FILE__) + '/insert.rb'
11
14
  require File.dirname(__FILE__) + '/update.rb'
12
15
  require File.dirname(__FILE__) + '/delete.rb'
data/lib/string.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  class String
2
+ include WhereValue
2
3
 
3
4
  # call-seq: string.to_sql -> a_string
4
5
  #
data/lib/symbol.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  class Symbol
2
-
2
+ include WhereValue
3
3
  # call-seq: symbol.to_sql -> a_string
4
4
  #
5
5
  # Returns a string with single quotes escaped.
data/lib/where_builder.rb CHANGED
@@ -18,6 +18,15 @@ class WhereBuilder
18
18
  def equal(lval, rval)
19
19
  sql_parts << rval.to_sql_equal(lval)
20
20
  end
21
+
22
+ # call-seq: where.not_null(arg1)
23
+ #
24
+ # Appends a not null condition to the where SQL clause.
25
+ #
26
+ # WhereBuilder.new { not_null :column1 }.to_sql #=> " where column1 is not null"
27
+ def not_null(column)
28
+ sql_parts << "#{column} is not null"
29
+ end
21
30
 
22
31
  # call-seq: where.exists(clause)
23
32
  #
@@ -49,8 +58,6 @@ class WhereBuilder
49
58
  " where #{sql_parts.join(' and ')}"
50
59
  end
51
60
 
52
- protected
53
-
54
61
  def sql_parts #:nodoc:
55
62
  @sql_parts ||= []
56
63
  end
@@ -0,0 +1,8 @@
1
+ module WhereValue
2
+
3
+ def is_not_in &block
4
+ builder = eval "self", block.binding
5
+ builder.sql_parts << "#{self.to_sql} not in (#{block.call.to_sql})"
6
+ end
7
+
8
+ end
data/rakefile.rb CHANGED
@@ -27,7 +27,7 @@ Gem::manage_gems
27
27
  specification = Gem::Specification.new do |s|
28
28
  s.name = "sqldsl"
29
29
  s.summary = "A DSL for creating SQL Statements"
30
- s.version = "1.0.0"
30
+ s.version = "1.1.0"
31
31
  s.author = 'Jay Fields'
32
32
  s.description = "A DSL for creating SQL Statements"
33
33
  s.email = 'sqldsl-developer@rubyforge.org'
data/test/hash_test.rb ADDED
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class HashTest < Test::Unit::TestCase
4
+
5
+ def test_to_sql_name_value_as_aliasing
6
+ assert_equal "a as b, c as d", {:a => :b, :c => :d}.to_sql
7
+ end
8
+
9
+ end
@@ -0,0 +1,12 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class NumbericTest < Test::Unit::TestCase
4
+ def test_to_sql_gives_self
5
+ assert_equal 123, 123.to_sql
6
+ end
7
+
8
+ def test_is_a_where_value_duck
9
+ assert_equal true, 123.respond_to?(:is_not_in)
10
+ end
11
+
12
+ end
data/test/select_test.rb CHANGED
@@ -26,4 +26,21 @@ class SelectTest < Test::Unit::TestCase
26
26
  assert_equal 'select column from bar, foo',
27
27
  Select[:column].from[:foo, :bar].to_sql
28
28
  end
29
+
30
+ def test_order_by
31
+ assert_equal 'select foo order by bar', Select[:foo].order_by(:bar).to_sql
32
+ end
33
+
34
+ def test_order_by_with_multiple_arguments
35
+ assert_equal 'select foo order by bar, baz', Select[:foo].order_by(:bar, :baz).to_sql
36
+ end
37
+
38
+ def test_distinct_select
39
+ assert_equal 'select distinct foo', Select.distinct[:foo].to_sql
40
+ end
41
+
42
+ def test_column_aliasing
43
+ assert_equal 'select column1 as foo, column2 as bar', Select[:column1 => :foo, :column2 => :bar].to_sql
44
+ end
45
+
29
46
  end
data/test/string_test.rb CHANGED
@@ -8,4 +8,9 @@ class StringTest < Test::Unit::TestCase
8
8
  def test_to_sql_gives_quoted_and_escapes_single_quotes
9
9
  assert_equal "'it''s'", "it's".to_sql
10
10
  end
11
+
12
+ def test_is_a_where_value_duck
13
+ assert_equal true, "foo".respond_to?(:is_not_in)
14
+ end
15
+
11
16
  end
data/test/symbol_test.rb CHANGED
@@ -4,4 +4,9 @@ class SymbolTest < Test::Unit::TestCase
4
4
  def test_to_sql_returns_to_s
5
5
  assert_equal 'asdf', :asdf.to_sql
6
6
  end
7
+
8
+ def test_is_a_where_value_duck
9
+ assert_equal true, :sym.respond_to?(:is_not_in)
10
+ end
11
+
7
12
  end
@@ -47,4 +47,21 @@ class WhereBuilderTest < Test::Unit::TestCase
47
47
  end
48
48
  assert_equal ' where not exists (select foo)', statement.to_sql
49
49
  end
50
+
51
+ def test_is_not_null_where_criteria
52
+ statement = WhereBuilder.new do
53
+ not_null :something
54
+ end
55
+ assert_equal ' where something is not null', statement.to_sql
56
+ end
57
+
58
+ def test_is_not_in_where_criteria
59
+ statement = WhereBuilder.new do
60
+ :column1.is_not_in do
61
+ Select[:shipment_option_id]
62
+ end
63
+ end
64
+ assert_equal ' where column1 not in (select shipment_option_id)', statement.to_sql
65
+ end
66
+
50
67
  end
@@ -0,0 +1,22 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class WhereValueTest < Test::Unit::TestCase
4
+
5
+ def test_not_in_sql_statement_with_value
6
+ klass = Class.new() do
7
+ include WhereValue
8
+
9
+ def to_sql
10
+ "klass"
11
+ end
12
+ end
13
+
14
+ statement = Select[:foo].where do
15
+ klass.new.is_not_in do
16
+ "anything"
17
+ end
18
+ end
19
+ assert_equal "select foo where klass not in ('anything')", statement.to_sql
20
+ end
21
+
22
+ end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.11
2
+ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: sqldsl
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.0.0
7
- date: 2006-09-30 00:00:00 -04:00
6
+ version: 1.1.0
7
+ date: 2007-03-01 00:00:00 -05:00
8
8
  summary: A DSL for creating SQL Statements
9
9
  require_paths:
10
10
  - lib
@@ -25,11 +25,14 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
25
25
  platform: ruby
26
26
  signing_key:
27
27
  cert_chain:
28
+ post_install_message:
28
29
  authors:
29
30
  - Jay Fields
30
31
  files:
31
32
  - lib/array.rb
32
33
  - lib/delete.rb
34
+ - lib/distinct_select.rb
35
+ - lib/hash.rb
33
36
  - lib/insert.rb
34
37
  - lib/numeric.rb
35
38
  - lib/object.rb
@@ -41,12 +44,15 @@ files:
41
44
  - lib/time.rb
42
45
  - lib/update.rb
43
46
  - lib/where_builder.rb
47
+ - lib/where_value.rb
44
48
  - test/all_tests.rb
45
49
  - test/array_test.rb
46
50
  - test/delete_acceptance_test.rb
47
51
  - test/delete_test.rb
52
+ - test/hash_test.rb
48
53
  - test/insert_acceptance_test.rb
49
54
  - test/insert_test.rb
55
+ - test/numeric_test.rb
50
56
  - test/object_test.rb
51
57
  - test/select_acceptance_test.rb
52
58
  - test/select_test.rb
@@ -57,6 +63,7 @@ files:
57
63
  - test/update_acceptance_test.rb
58
64
  - test/update_test.rb
59
65
  - test/where_builder_test.rb
66
+ - test/where_value_test.rb
60
67
  - rakefile.rb
61
68
  - README
62
69
  test_files: