sqldsl 1.3.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -21,12 +21,13 @@ See the tests for more examples
21
21
  === Test Helper
22
22
 
23
23
  require 'test/unit'
24
+ require File.dirname(__FILE__) + '/../lib/sqldsl'
24
25
  unless File.directory? File.dirname(__FILE__) + '/../vendor/mocha-0.4.0/'
25
26
  raise "mocha 4.0 is required to run the test suite. create the 'vendor' directory as a sibling of test and 'gem unpack mocha' in 'vendor'"
26
27
  end
27
28
  $:.unshift File.dirname(__FILE__) + '/../vendor/mocha-0.4.0/lib/'
28
29
  require File.dirname(__FILE__) + '/../vendor/mocha-0.4.0/lib/mocha'
29
- require File.dirname(__FILE__) + '/../lib/sqldsl'
30
+
30
31
 
31
32
  === Insert Example
32
33
 
@@ -101,7 +102,7 @@ See the tests for more examples
101
102
 
102
103
  def test_select_with_receive_any_objects_and_operators
103
104
  statement = Select[:column1, 'book', 10].from[:table1, :table2].where do
104
- table1.column1 = 99
105
+ column1 == 99
105
106
  column1 <=> 100
106
107
  column2 < 'foo'
107
108
  column3 <= column4
@@ -112,7 +113,7 @@ See the tests for more examples
112
113
  column2 << [3, 4]
113
114
  end
114
115
  expected = "select column1, 'book', 10 from table1, table2
115
- where table1.column1 = 99 and column1 <> 100 and column2 < 'foo'
116
+ where column1 = 99 and column1 <> 100 and column2 < 'foo'
116
117
  and column3 <= column4 and column1 > 0 and column2 >= 'bar'
117
118
  and column1 is not null and column1 in (1, 2) and column2 not in (3, 4)"
118
119
  assert_equal expected.delete("\n").squeeze(" "), statement.to_sql
@@ -147,10 +148,23 @@ See the tests for more examples
147
148
  assert_equal expected, statement.to_sql
148
149
  end
149
150
 
151
+ def test_select_with_inner_join
152
+ expected = "select * from t1 a inner join t2 b on a.id = b.id inner join t3 c on b.id2 = c.id where c.attr1 = 'foo' and b.attr1 = 'foo2'"
153
+ statement = Select.all.from[:t1.as(:a)].inner_join[:t2.as(:b)].on do
154
+ a.id == b.id
155
+ end.inner_join[:t3.as(:c)].on do
156
+ b.id2 == c.id
157
+ end.where do
158
+ c.attr1 == 'foo'
159
+ b.attr1 == 'foo2'
160
+ end
161
+ assert_equal expected, statement.to_sql
162
+ end
163
+
150
164
  def test_columns_in_inner_where_are_validated_against_outer_tables
151
165
  statement = Select.all.from[:table].where do
152
166
  exists(Select.all.from[:inner_table.as(:aliased)].where do
153
- table.column1 = aliased.column1
167
+ table.column1 == aliased.column1
154
168
  end)
155
169
  end
156
170
  assert_equal 'select * from table where exists (select * from inner_table aliased where table.column1 = aliased.column1)', statement.to_sql
@@ -0,0 +1,3 @@
1
+ class BlankSlate
2
+ instance_methods.each { |m| undef_method m unless m =~ /^__/ || m =~ /class/ }
3
+ end
@@ -0,0 +1,9 @@
1
+ class InnerJoinBuilder
2
+ def initialize(select_builder)
3
+ @select_builder = select_builder
4
+ end
5
+
6
+ def [](table_name)
7
+ @select_builder.inner_join_table(table_name)
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ class OnWhereBuilder < WhereBuilder
2
+
3
+ # call-seq: on_where.to_sql -> a_string
4
+ #
5
+ # Returns a string by collecting all the conditions and joins them with ' and '.
6
+ #
7
+ # OnWhereBuilder.new [] do
8
+ # equal :column1, 10
9
+ # equal :column2, 'book'
10
+ # end.to_sql #=> " on column1 = 10 and column2 = 'book'"
11
+ def to_sql
12
+ " on #{sql_parts.join(' and ')}"
13
+ end
14
+
15
+ end
data/lib/receive_any.rb CHANGED
@@ -1,4 +1,4 @@
1
- class ReceiveAny #:nodoc:
1
+ class ReceiveAny < BlankSlate #:nodoc:
2
2
  attr_reader :to_sql, :builder
3
3
 
4
4
  def initialize(identifier, builder)
@@ -9,6 +9,7 @@ class ReceiveAny #:nodoc:
9
9
  def equal(arg)
10
10
  builder.equal self, arg
11
11
  end
12
+ alias == equal
12
13
 
13
14
  def not_equal(arg)
14
15
  builder.not_equal self, arg
@@ -52,8 +53,7 @@ class ReceiveAny #:nodoc:
52
53
 
53
54
  def method_missing(sym, *args)
54
55
  raise ArgumentError.new("#{self.to_sql} is not specified as a table in your from statement") unless @builder.tables.include?(self.to_sql.to_sym)
55
- @to_sql << ".#{sym.to_s}".chomp("=")
56
- self.equal args.first if sym.to_s =~ /=$/
56
+ @to_sql << ".#{sym.to_s}"
57
57
  self
58
58
  end
59
59
  end
data/lib/select.rb CHANGED
@@ -68,4 +68,24 @@ class Select < SqlStatement
68
68
  self
69
69
  end
70
70
 
71
+ def inner_join
72
+ InnerJoinBuilder.new(self)
73
+ end
74
+
75
+ def inner_join_table(table_name)
76
+ @to_sql << " inner join "
77
+ if table_name.to_s =~ / as /
78
+ @tables << table_name.to_s.split(/ as /).last.to_sym
79
+ @to_sql << table_name.to_s.gsub(/ as /, " ")
80
+ else
81
+ @tables << table_name
82
+ @to_sql << table_name.to_s
83
+ end
84
+ self
85
+ end
86
+
87
+ def on(&block)
88
+ @to_sql += OnWhereBuilder.new(self.tables, &block).to_sql
89
+ self
90
+ end
71
91
  end
data/lib/sqldsl.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require File.dirname(__FILE__) + '/blank_slate.rb'
1
2
  require File.dirname(__FILE__) + '/receive_any.rb'
2
3
  require File.dirname(__FILE__) + '/symbol.rb'
3
4
  require File.dirname(__FILE__) + '/array.rb'
@@ -5,8 +6,10 @@ require File.dirname(__FILE__) + '/string.rb'
5
6
  require File.dirname(__FILE__) + '/numeric.rb'
6
7
  require File.dirname(__FILE__) + '/time.rb'
7
8
  require File.dirname(__FILE__) + '/sql_statement.rb'
9
+ require File.dirname(__FILE__) + '/inner_join_builder.rb'
8
10
  require File.dirname(__FILE__) + '/where_builder.rb'
9
11
  require File.dirname(__FILE__) + '/or_where_builder.rb'
12
+ require File.dirname(__FILE__) + '/on_where_builder.rb'
10
13
  require File.dirname(__FILE__) + '/select.rb'
11
14
  require File.dirname(__FILE__) + '/distinct_select.rb'
12
15
  require File.dirname(__FILE__) + '/insert.rb'
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.3.2"
30
+ s.version = "1.4.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'
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class InnerJoinBuilderTest < Test::Unit::TestCase
4
+ def test_add_tables
5
+ ij = InnerJoinBuilder.new(s_builder = mock)
6
+ s_builder.expects(:inner_join_table).with(:"table1 as foo")
7
+ ij[:table1.as(:foo)]
8
+ end
9
+ end
@@ -27,8 +27,7 @@ class ReceiveAnyTest < Test::Unit::TestCase
27
27
  builder.expects(:equal).with do |lval, rval|
28
28
  lval.class == ReceiveAny && rval == 1
29
29
  end
30
- builder.expects(:tables).returns [:foo]
31
- ReceiveAny.new(:foo, builder).bar = 1
30
+ ReceiveAny.new(:foo, builder) == 1
32
31
  end
33
32
 
34
33
  def test_not_equal
@@ -25,7 +25,7 @@ class SelectAcceptanceTest < Test::Unit::TestCase
25
25
 
26
26
  def test_select_with_receive_any_objects_and_operators
27
27
  statement = Select[:column1, 'book', 10].from[:table1, :table2].where do
28
- table1.column1 = 99
28
+ column1 == 99
29
29
  column1 <=> 100
30
30
  column2 < 'foo'
31
31
  column3 <= column4
@@ -36,7 +36,7 @@ class SelectAcceptanceTest < Test::Unit::TestCase
36
36
  column2 << [3, 4]
37
37
  end
38
38
  expected = "select column1, 'book', 10 from table1, table2
39
- where table1.column1 = 99 and column1 <> 100 and column2 < 'foo'
39
+ where column1 = 99 and column1 <> 100 and column2 < 'foo'
40
40
  and column3 <= column4 and column1 > 0 and column2 >= 'bar'
41
41
  and column1 is not null and column1 in (1, 2) and column2 not in (3, 4)"
42
42
  assert_equal expected.delete("\n").squeeze(" "), statement.to_sql
@@ -71,10 +71,23 @@ class SelectAcceptanceTest < Test::Unit::TestCase
71
71
  assert_equal expected, statement.to_sql
72
72
  end
73
73
 
74
+ def test_select_with_inner_join
75
+ expected = "select * from t1 a inner join t2 b on a.id = b.id inner join t3 c on b.id2 = c.id where c.attr1 = 'foo' and b.attr1 = 'foo2'"
76
+ statement = Select.all.from[:t1.as(:a)].inner_join[:t2.as(:b)].on do
77
+ a.id == b.id
78
+ end.inner_join[:t3.as(:c)].on do
79
+ b.id2 == c.id
80
+ end.where do
81
+ c.attr1 == 'foo'
82
+ b.attr1 == 'foo2'
83
+ end
84
+ assert_equal expected, statement.to_sql
85
+ end
86
+
74
87
  def test_columns_in_inner_where_are_validated_against_outer_tables
75
88
  statement = Select.all.from[:table].where do
76
89
  exists(Select.all.from[:inner_table.as(:aliased)].where do
77
- table.column1 = aliased.column1
90
+ table.column1 == aliased.column1
78
91
  end)
79
92
  end
80
93
  assert_equal 'select * from table where exists (select * from inner_table aliased where table.column1 = aliased.column1)', statement.to_sql
data/test/select_test.rb CHANGED
@@ -57,5 +57,20 @@ class SelectTest < Test::Unit::TestCase
57
57
  def test_table_array
58
58
  assert_equal [:table1, :table2], Select.all.from[:table1, :table2].instance_variable_get("@tables")
59
59
  end
60
+
61
+ def test_inner_join
62
+ assert_equal InnerJoinBuilder, Select.all.inner_join.class
63
+ end
64
+
65
+ def test_inner_join_with_table
66
+ assert_equal "select * from table1 inner join table2", Select.all.from[:table1].inner_join[:table2].to_sql
67
+ end
68
+
69
+ def test_inner_join_with_table_with_on
70
+ statement = Select.all.from[:table1].inner_join[:table2].on do
71
+ table2.column == 1
72
+ end
73
+ assert_equal "select * from table1 inner join table2 on table2.column = 1", statement.to_sql
74
+ end
60
75
 
61
76
  end
data/test/test_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/sqldsl'
2
3
  unless File.directory? File.dirname(__FILE__) + '/../vendor/mocha-0.4.0/'
3
4
  raise "mocha 4.0 is required to run the test suite. create the 'vendor' directory as a sibling of test and 'gem unpack mocha' in 'vendor'"
4
5
  end
5
6
  $:.unshift File.dirname(__FILE__) + '/../vendor/mocha-0.4.0/lib/'
6
7
  require File.dirname(__FILE__) + '/../vendor/mocha-0.4.0/lib/mocha'
7
- require File.dirname(__FILE__) + '/../lib/sqldsl'
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: sqldsl
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.3.2
7
- date: 2007-03-16 00:00:00 -04:00
6
+ version: 1.4.0
7
+ date: 2007-03-17 00:00:00 -04:00
8
8
  summary: A DSL for creating SQL Statements
9
9
  require_paths:
10
10
  - lib
@@ -29,10 +29,13 @@ authors:
29
29
  - Jay Fields
30
30
  files:
31
31
  - lib/array.rb
32
+ - lib/blank_slate.rb
32
33
  - lib/delete.rb
33
34
  - lib/distinct_select.rb
35
+ - lib/inner_join_builder.rb
34
36
  - lib/insert.rb
35
37
  - lib/numeric.rb
38
+ - lib/on_where_builder.rb
36
39
  - lib/or_where_builder.rb
37
40
  - lib/receive_any.rb
38
41
  - lib/select.rb
@@ -47,6 +50,7 @@ files:
47
50
  - test/array_test.rb
48
51
  - test/delete_acceptance_test.rb
49
52
  - test/delete_test.rb
53
+ - test/inner_join_builder_test.rb
50
54
  - test/insert_acceptance_test.rb
51
55
  - test/insert_test.rb
52
56
  - test/numeric_test.rb