sql-maker 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +23 -0
- data/Rakefile +14 -0
- data/lib/sql-maker.rb +5 -0
- data/lib/sql/maker.rb +676 -0
- data/lib/sql/maker/condition.rb +378 -0
- data/lib/sql/maker/error.rb +3 -0
- data/lib/sql/maker/helper.rb +22 -0
- data/lib/sql/maker/quoting.rb +138 -0
- data/lib/sql/maker/select.rb +544 -0
- data/lib/sql/maker/select/oracle.rb +30 -0
- data/lib/sql/maker/select_set.rb +194 -0
- data/lib/sql/maker/util.rb +54 -0
- data/lib/sql/query_maker.rb +429 -0
- data/scripts/perl2ruby.rb +34 -0
- data/spec/maker/bind_param_spec.rb +62 -0
- data/spec/maker/condition/add_raw_spec.rb +29 -0
- data/spec/maker/condition/compose_empty_spec.rb +72 -0
- data/spec/maker/condition/empty_values_spec.rb +25 -0
- data/spec/maker/condition/make_term_spec.rb +38 -0
- data/spec/maker/condition/where_spec.rb +35 -0
- data/spec/maker/delete_spec.rb +116 -0
- data/spec/maker/insert_empty_spec.rb +23 -0
- data/spec/maker/insert_spec.rb +61 -0
- data/spec/maker/new_line_spec.rb +9 -0
- data/spec/maker/select/oracle/oracle_spec.rb +16 -0
- data/spec/maker/select/pod_select_spec.rb +34 -0
- data/spec/maker/select/statement_spec.rb +805 -0
- data/spec/maker/select_set_spec.rb +294 -0
- data/spec/maker/select_spec.rb +470 -0
- data/spec/maker/simple_spec.rb +54 -0
- data/spec/maker/strict_spec.rb +45 -0
- data/spec/maker/subquery_spec.rb +77 -0
- data/spec/maker/update_spec.rb +138 -0
- data/spec/maker/util_spec.rb +6 -0
- data/spec/maker/where_spec.rb +28 -0
- data/spec/query_maker/and_using_hash_spec.rb +13 -0
- data/spec/query_maker/cheatsheet_spec.rb +32 -0
- data/spec/query_maker/column_bind_spec.rb +55 -0
- data/spec/query_maker/refs_in_bind_spec.rb +19 -0
- data/spec/spec_helper.rb +15 -0
- data/sql-maker.gemspec +25 -0
- metadata +185 -0
@@ -0,0 +1,54 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require 'sql/maker'
|
3
|
+
|
4
|
+
describe 'select_query' do
|
5
|
+
include SQL::Maker::Helper
|
6
|
+
|
7
|
+
context 'driver: sqlite' do
|
8
|
+
builder = SQL::Maker.new(:driver => 'sqlite')
|
9
|
+
|
10
|
+
it do
|
11
|
+
stmt = builder.select_query('foo', ['foo', 'bar'], {:bar => 'baz', :john => 'man'}, {:order_by => sql_raw('yo')})
|
12
|
+
expect(stmt.as_sql).to be == %Q{SELECT "foo", "bar"\nFROM "foo"\nWHERE ("bar" = ?) AND ("john" = ?)\nORDER BY yo}
|
13
|
+
expect(stmt.bind.join(',')).to be == 'baz,man'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'driver: mysql, quote_char: "", new_line: " "' do
|
18
|
+
builder = SQL::Maker.new(:driver => 'mysql', :quote_char => '', :new_line => ' ')
|
19
|
+
|
20
|
+
it do
|
21
|
+
stmt = builder.select_query('foo', ['foo', 'bar'], {:bar => 'baz', :john => 'man'}, {:order_by => 'yo'})
|
22
|
+
expect(stmt.as_sql).to be == %Q{SELECT foo, bar FROM foo WHERE (bar = ?) AND (john = ?) ORDER BY yo}
|
23
|
+
expect(stmt.bind.join(',')).to be == 'baz,man'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'new_condition' do
|
28
|
+
builder = SQL::Maker.new(:driver => 'sqlite', :quote_char => %q{`}, :name_sep => %q{.})
|
29
|
+
cond = builder.new_condition
|
30
|
+
expect(cond.is_a?(SQL::Maker::Condition)).to be_truthy
|
31
|
+
expect(cond.quote_char).to be == %q{`}
|
32
|
+
expect(cond.name_sep).to be == %q{.}
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'new_select' do
|
36
|
+
it 'driver: sqlite, quote_char: "`", name_sep: "."' do
|
37
|
+
builder = SQL::Maker.new(:driver => 'sqlite', :quote_char => %q{`}, :name_sep => %q{.})
|
38
|
+
select = builder.new_select()
|
39
|
+
expect(select.is_a?(SQL::Maker::Select)).to be_truthy
|
40
|
+
expect(select.quote_char).to be == %q{`}
|
41
|
+
expect(select.name_sep).to be == %q{.}
|
42
|
+
expect(select.new_line).to be == %Q{\n}
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'driver: mysql, quote_char: "", new_line: " "' do
|
46
|
+
builder = SQL::Maker.new(:driver => 'sqlite', :quote_char => %q{}, :name_sep => %q{.}, :new_line => %q{ })
|
47
|
+
select = builder.new_select()
|
48
|
+
expect(select.is_a?(SQL::Maker::Select)).to be_truthy
|
49
|
+
expect(select.quote_char).to be == %q{}
|
50
|
+
expect(select.name_sep).to be == %q{.}
|
51
|
+
expect(select.new_line).to be == %q{ }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require 'sql/maker'
|
3
|
+
|
4
|
+
describe 'SQL::Maker' do
|
5
|
+
let(:maker) do
|
6
|
+
SQL::Maker.new(
|
7
|
+
:driver => 'SQLite',
|
8
|
+
:strict => true,
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
it { expect(maker.strict).to be == true }
|
13
|
+
|
14
|
+
it "maker.new_condition" do
|
15
|
+
expect { maker.new_condition.add(:foo => [1]) }.to raise_error(SQL::Maker::Error)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "select.new_condition" do
|
19
|
+
select = maker.new_select
|
20
|
+
expect(select.strict).to be == true
|
21
|
+
expect { select.new_condition.add(:foo => [1]) }.to raise_error(SQL::Maker::Error)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "maker.select" do
|
25
|
+
expect { maker.select("user", ['*'], { :name => ["John", "Tom" ]}) }.to raise_error(SQL::Maker::Error)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "maker.insert" do
|
29
|
+
expect { maker.insert(
|
30
|
+
:user, { :name => "John", :created_on => "datetime(now)" }
|
31
|
+
) }.to raise_error(SQL::Maker::Error)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "maker.delete" do
|
35
|
+
expect { maker.delete(:user, { :name => ["John", "Tom"]}) }.to raise_error(SQL::Maker::Error)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "maker.update where" do
|
39
|
+
expect { maker.update(:user, {:name => "John"}, { :user_id => [1, 2] }) }.to raise_error(SQL::Maker::Error)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "maker.update set" do
|
43
|
+
expect { maker.update(:user, {:name => "select *"}) }.to raise_error(SQL::Maker::Error)
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require 'sql/maker'
|
3
|
+
|
4
|
+
describe 'SQL::Maker' do
|
5
|
+
context 'select_subquery' do
|
6
|
+
context 'driver: sqlite' do
|
7
|
+
builder = SQL::Maker.new(:driver => 'sqlite')
|
8
|
+
|
9
|
+
it do
|
10
|
+
stmt1 = builder.select_query('sakura', ['hoge', 'fuga'], {:fuga => 'piyo', :zun => 'doko'})
|
11
|
+
expect(stmt1.as_sql).to be == %Q{SELECT "hoge", "fuga"\nFROM "sakura"\nWHERE ("fuga" = ?) AND ("zun" = ?)}
|
12
|
+
expect(stmt1.bind.join(',')).to be == 'piyo,doko'
|
13
|
+
|
14
|
+
stmt2 = builder.select_query([stmt1,'stmt1'], ['foo', 'bar'], {:bar => 'baz', :john => 'man'})
|
15
|
+
expect(stmt2.as_sql).to be == %Q{SELECT "foo", "bar"\nFROM (SELECT "hoge", "fuga"\nFROM "sakura"\nWHERE ("fuga" = ?) AND ("zun" = ?)), "stmt1"\nWHERE ("bar" = ?) AND ("john" = ?)}
|
16
|
+
expect(stmt2.bind.join(',')).to be == 'piyo,doko,baz,man'
|
17
|
+
|
18
|
+
stmt3 = builder.select_query([stmt2,'stmt2'], ['baz'], {'baz'=>'bar'}, {:order_by => 'yo'})
|
19
|
+
expect(stmt3.as_sql).to be == %Q{SELECT "baz"\nFROM (SELECT "foo", "bar"\nFROM (SELECT "hoge", "fuga"\nFROM "sakura"\nWHERE ("fuga" = ?) AND ("zun" = ?)), "stmt1"\nWHERE ("bar" = ?) AND ("john" = ?)), "stmt2"\nWHERE ("baz" = ?)\nORDER BY "yo"}
|
20
|
+
expect(stmt3.bind.join(',')).to be == 'piyo,doko,baz,man,bar'
|
21
|
+
end
|
22
|
+
|
23
|
+
#it 'no infinite loop' do
|
24
|
+
# stmt = builder.new_select
|
25
|
+
# stmt.add_select( 'id' )
|
26
|
+
# stmt.add_where( 'foo'=>'bar' )
|
27
|
+
# stmt.add_from( stmt, 'itself' )
|
28
|
+
|
29
|
+
# expect(stmt.as_sql).to be == %Q{SELECT "id"\nFROM (SELECT "id"\nFROM \nWHERE ("foo" = ?)), "itself"\nWHERE ("foo" = ?)}
|
30
|
+
# expect(stmt.bind.join(',')).to be == 'bar,bar'
|
31
|
+
#end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'subquery_and_join' do
|
36
|
+
subquery = SQL::Maker::Select.new( :quote_char => %q{}, :name_sep => %q{.}, :new_line => %q{ } )
|
37
|
+
subquery.add_select('*')
|
38
|
+
subquery.add_from( 'foo' )
|
39
|
+
subquery.add_where( 'hoge' => 'fuga' )
|
40
|
+
|
41
|
+
stmt = SQL::Maker::Select.new( :quote_char => %q{}, :name_sep => %q{.}, :new_line => %q{ } )
|
42
|
+
stmt.add_join(
|
43
|
+
[ subquery, 'bar' ] => {
|
44
|
+
:type => 'inner',
|
45
|
+
:table => 'baz',
|
46
|
+
:alias => 'b1',
|
47
|
+
:condition => 'bar.baz_id = b1.baz_id'
|
48
|
+
},
|
49
|
+
)
|
50
|
+
expect(stmt.as_sql).to be == "FROM (SELECT * FROM foo WHERE (hoge = ?)) bar INNER JOIN baz b1 ON bar.baz_id = b1.baz_id"
|
51
|
+
expect(stmt.bind.join(',')).to be == 'fuga'
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'complex' do
|
55
|
+
s1 = SQL::Maker::Select.new( :quote_char => %q{}, :name_sep => %q{.}, :new_line => %q{ } )
|
56
|
+
s1.add_select('*')
|
57
|
+
s1.add_from( 'foo' )
|
58
|
+
s1.add_where( 'hoge' => 'fuga' )
|
59
|
+
|
60
|
+
s2 = SQL::Maker::Select.new( :quote_char => %q{}, :name_sep => %q{.}, :new_line => %q{ } )
|
61
|
+
s2.add_select('*')
|
62
|
+
s2.add_from( s1, 'f' )
|
63
|
+
s2.add_where( 'piyo' => 'puyo' )
|
64
|
+
|
65
|
+
stmt = SQL::Maker::Select.new( :quote_char => %q{}, :name_sep => %q{.}, :new_line => %q{ } )
|
66
|
+
stmt.add_join(
|
67
|
+
[ s2, 'bar' ] => {
|
68
|
+
:type => 'inner',
|
69
|
+
:table => 'baz',
|
70
|
+
:alias => 'b1',
|
71
|
+
:condition => 'bar.baz_id = b1.baz_id'
|
72
|
+
},
|
73
|
+
)
|
74
|
+
expect(stmt.as_sql).to be == "FROM (SELECT * FROM (SELECT * FROM foo WHERE (hoge = ?)) f WHERE (piyo = ?)) bar INNER JOIN baz b1 ON bar.baz_id = b1.baz_id"
|
75
|
+
expect(stmt.bind.join(',')).to be == 'fuga,puyo'
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require 'sql/maker'
|
3
|
+
|
4
|
+
describe 'SQL::Maker#update' do
|
5
|
+
include SQL::Maker::Helper
|
6
|
+
|
7
|
+
context 'driver: sqlite' do
|
8
|
+
builder = SQL::Maker.new(:driver => 'sqlite')
|
9
|
+
# it 'arrayref, where cause(hashref)' do
|
10
|
+
|
11
|
+
# it 'arrayref, where cause(arrayref)' do
|
12
|
+
|
13
|
+
# it 'arrayref, where cause(condition)' do
|
14
|
+
|
15
|
+
it 'ordered hashref, where cause(hashref)' do
|
16
|
+
sql, bind = builder.update('foo', {:bar => 'baz', :john => 'man'}, {:yo => 'king'})
|
17
|
+
expect(sql).to be == %Q{UPDATE "foo" SET "bar" = ?, "john" = ? WHERE ("yo" = ?)}
|
18
|
+
expect(bind.join(',')).to be == 'baz,man,king'
|
19
|
+
end
|
20
|
+
|
21
|
+
#it 'ordered hashref, where cause(arrayref)' do
|
22
|
+
|
23
|
+
it 'ordered hashref, where cause(condition)' do
|
24
|
+
cond = builder.new_condition
|
25
|
+
cond.add(:yo => 'king')
|
26
|
+
sql, bind = builder.update('foo', {:bar => 'baz', :john => 'man'}, cond)
|
27
|
+
expect(sql).to be == %Q{UPDATE "foo" SET "bar" = ?, "john" = ? WHERE ("yo" = ?)}
|
28
|
+
expect(bind.join(',')).to be == 'baz,man,king'
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'ordered hashref' do
|
32
|
+
# no where
|
33
|
+
sql, bind = builder.update('foo', {:bar => 'baz', :john => 'man'})
|
34
|
+
expect(sql).to be == %Q{UPDATE "foo" SET "bar" = ?, "john" = ?}
|
35
|
+
expect(bind.join(',')).to be == 'baz,man'
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'literal, sub query' do
|
39
|
+
sql, bind = builder.update('foo', { :user_id => 100, :updated_on => ['datetime(?)', 'now'], :counter => ['counter + 1' ] })
|
40
|
+
expect(sql).to be == %Q{UPDATE "foo" SET "user_id" = ?, "updated_on" = datetime(?), "counter" = counter + 1}
|
41
|
+
expect(bind.join(',')).to be == '100,now'
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'literal, sub query using term' do
|
45
|
+
sql, bind = builder.update('foo', { :user_id => 100, :updated_on => sql_raw('datetime(?)', 'now'), :counter => sql_raw('counter + 1') } )
|
46
|
+
expect(sql).to be == %Q{UPDATE "foo" SET "user_id" = ?, "updated_on" = datetime(?), "counter" = counter + 1}
|
47
|
+
expect(bind.join(',')).to be == '100,now'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'driver: mysql' do
|
52
|
+
builder = SQL::Maker.new(:driver => 'mysql')
|
53
|
+
# it 'array ref, where cause(hashref)' do
|
54
|
+
|
55
|
+
# it 'array ref, where cause(arrayref)' do
|
56
|
+
|
57
|
+
# it 'array ref, where cause(condition)' do
|
58
|
+
|
59
|
+
it 'ordered hashref, where cause(hashref)' do
|
60
|
+
sql, bind = builder.update('foo', {:bar => 'baz', :john => 'man'}, {:yo => 'king'})
|
61
|
+
expect(sql).to be == %Q{UPDATE `foo` SET `bar` = ?, `john` = ? WHERE (`yo` = ?)}
|
62
|
+
expect(bind.join(',')).to be == 'baz,man,king'
|
63
|
+
end
|
64
|
+
|
65
|
+
# it 'ordered hashref, where cause(arrayref)' do
|
66
|
+
|
67
|
+
it 'ordered hashref, where cause(condition)' do
|
68
|
+
cond = builder.new_condition
|
69
|
+
cond.add(:yo => 'king')
|
70
|
+
sql, bind = builder.update('foo', {:bar => 'baz', :john => 'man'}, cond)
|
71
|
+
expect(sql).to be == %Q{UPDATE `foo` SET `bar` = ?, `john` = ? WHERE (`yo` = ?)}
|
72
|
+
expect(bind.join(',')).to be == 'baz,man,king'
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'ordered hashref' do
|
76
|
+
# no where
|
77
|
+
sql, bind = builder.update('foo', {:bar => 'baz', :john => 'man'})
|
78
|
+
expect(sql).to be == %Q{UPDATE `foo` SET `bar` = ?, `john` = ?}
|
79
|
+
expect(bind.join(',')).to be == 'baz,man'
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'literal, sub query' do
|
83
|
+
sql, bind = builder.update('foo', { :user_id => 100, :updated_on => ['FROM_UNIXTIME(?)', 1302241686], :counter => ['counter + 1' ] })
|
84
|
+
expect(sql).to be == %Q{UPDATE `foo` SET `user_id` = ?, `updated_on` = FROM_UNIXTIME(?), `counter` = counter + 1}
|
85
|
+
expect(bind.join(',')).to be == '100,1302241686'
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'literal, sub query using term' do
|
89
|
+
sql, bind = builder.update('foo', { :user_id => 100, :updated_on => sql_raw('FROM_UNIXTIME(?)', 1302241686), :counter => sql_raw('counter + 1') } )
|
90
|
+
expect(sql).to be == %Q{UPDATE `foo` SET `user_id` = ?, `updated_on` = FROM_UNIXTIME(?), `counter` = counter + 1}
|
91
|
+
expect(bind.join(',')).to be == '100,1302241686'
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context 'driver: mysql, quote_char: "", new_line: " "' do
|
96
|
+
builder = SQL::Maker.new(:driver => 'mysql', :quote_char => '', :new_line => ' ')
|
97
|
+
# it 'array ref, where cause(hashref)' do
|
98
|
+
|
99
|
+
# it 'array ref, where cause(arrayref)' do
|
100
|
+
|
101
|
+
# it 'array ref, where cause(condition)' do
|
102
|
+
|
103
|
+
it 'ordered hashref, where cause(hashref)' do
|
104
|
+
sql, bind = builder.update('foo', {:bar => 'baz', :john => 'man'}, {:yo => 'king'})
|
105
|
+
expect(sql).to be == %Q{UPDATE foo SET bar = ?, john = ? WHERE (yo = ?)}
|
106
|
+
expect(bind.join(',')).to be == 'baz,man,king'
|
107
|
+
end
|
108
|
+
|
109
|
+
# it 'ordered hashref, where cause(arrayref)' do
|
110
|
+
|
111
|
+
it 'ordered hashref, where cause(condition)' do
|
112
|
+
cond = builder.new_condition
|
113
|
+
cond.add(:yo => 'king')
|
114
|
+
sql, bind = builder.update('foo', {:bar => 'baz', :john => 'man'}, cond)
|
115
|
+
expect(sql).to be == %Q{UPDATE foo SET bar = ?, john = ? WHERE (yo = ?)}
|
116
|
+
expect(bind.join(',')).to be == 'baz,man,king'
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'ordered hashref' do
|
120
|
+
# no where
|
121
|
+
sql, bind = builder.update('foo', {:bar => 'baz', :john => 'man'})
|
122
|
+
expect(sql).to be == %Q{UPDATE foo SET bar = ?, john = ?}
|
123
|
+
expect(bind.join(',')).to be == 'baz,man'
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'literal, sub query' do
|
127
|
+
sql, bind = builder.update('foo', { :user_id => 100, :updated_on => ['FROM_UNIXTIME(?)', 1302241686], :counter => ['counter + 1' ]} )
|
128
|
+
expect(sql).to be == %Q{UPDATE foo SET user_id = ?, updated_on = FROM_UNIXTIME(?), counter = counter + 1}
|
129
|
+
expect(bind.join(',')).to be == '100,1302241686'
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'literal, sub query using term' do
|
133
|
+
sql, bind = builder.update('foo', { :user_id => 100, :updated_on => sql_raw('FROM_UNIXTIME(?)', 1302241686), :counter => sql_raw('counter + 1') } )
|
134
|
+
expect(sql).to be == %Q{UPDATE foo SET user_id = ?, updated_on = FROM_UNIXTIME(?), counter = counter + 1}
|
135
|
+
expect(bind.join(',')).to be == '100,1302241686'
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require 'sql/maker'
|
3
|
+
|
4
|
+
describe 'SQL::Maker' do
|
5
|
+
context 'driver: sqlite'do
|
6
|
+
builder = SQL::Maker.new(:driver => 'sqlite')
|
7
|
+
|
8
|
+
it 'none' do
|
9
|
+
sql, bind = builder.where( {} )
|
10
|
+
expect(sql).to be == %Q{}
|
11
|
+
expect(bind.join(',')).to be == ''
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'simple' do
|
15
|
+
sql, bind = builder.where( {:x => 3} )
|
16
|
+
expect(sql).to be == %Q{("x" = ?)}
|
17
|
+
expect(bind.join(',')).to be == '3'
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'array' do
|
21
|
+
# i probably don't need to support this
|
22
|
+
sql, bind = builder.where( [:x, 3] )
|
23
|
+
expect(sql).to be == %Q{("x" = ?)}
|
24
|
+
expect(bind.join(',')).to be == '3'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require 'sql/query_maker'
|
3
|
+
require 'sql/maker/helper'
|
4
|
+
|
5
|
+
describe 'SQL::QueryMaker' do
|
6
|
+
include SQL::Maker::Helper
|
7
|
+
|
8
|
+
it 'orderd_hash' do
|
9
|
+
q = sql_and(foo: 1, bar: sql_eq(2), baz: sql_lt(3))
|
10
|
+
expect(q.as_sql).to be == '(`foo` = ?) AND (`bar` = ?) AND (`baz` < ?)'
|
11
|
+
expect(q.bind).to be == [1,2,3]
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require 'sql/query_maker'
|
3
|
+
require 'sql/maker/helper'
|
4
|
+
|
5
|
+
def test(src, expected_term, expected_bind)
|
6
|
+
describe 'SQL::QueryMaker' do
|
7
|
+
include SQL::Maker::Helper
|
8
|
+
|
9
|
+
it src do
|
10
|
+
term = instance_eval src
|
11
|
+
sql = term.as_sql
|
12
|
+
bind = term.bind
|
13
|
+
expect(sql).to be == expected_term
|
14
|
+
expect(bind).to be == expected_bind
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
begin
|
20
|
+
file = File.open("#{ROOT}/lib/sql/query_maker.rb")
|
21
|
+
while line = file.gets
|
22
|
+
break if line =~ /=head1 CHEAT SHEET/
|
23
|
+
end
|
24
|
+
while line = file.gets
|
25
|
+
src = $1 if line =~ /IN:\s*(.+)\s*$/
|
26
|
+
query = eval($1, binding) if line =~ /OUT QUERY:(.+)/
|
27
|
+
if line =~ /OUT BIND:(.+)/
|
28
|
+
bind = eval($1, binding)
|
29
|
+
test(src, query, bind)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require 'sql/query_maker'
|
3
|
+
require 'sql/maker/helper'
|
4
|
+
|
5
|
+
describe 'SQL::QueryMaker' do
|
6
|
+
include SQL::Maker::Helper
|
7
|
+
|
8
|
+
it "sql_in" do
|
9
|
+
expect(sql_in(['bar','baz']).as_sql('foo')).to be == '`foo` IN (?,?)'
|
10
|
+
end
|
11
|
+
|
12
|
+
it "sql_lt" do
|
13
|
+
expect(sql_lt(3).as_sql('foo')).to be == '`foo` < ?'
|
14
|
+
end
|
15
|
+
|
16
|
+
it "sql_not_in" do
|
17
|
+
expect(sql_not_in(['bar','baz']).as_sql('foo')).to be == '`foo` NOT IN (?,?)'
|
18
|
+
end
|
19
|
+
|
20
|
+
it "sql_ne" do
|
21
|
+
expect(sql_ne('bar').as_sql('foo')).to be == '`foo` != ?'
|
22
|
+
end
|
23
|
+
|
24
|
+
it "sql_is_not_null" do
|
25
|
+
expect(sql_is_not_null().as_sql('foo')).to be == '`foo` IS NOT NULL'
|
26
|
+
end
|
27
|
+
|
28
|
+
it "sql_between" do
|
29
|
+
expect(sql_between('1', '2').as_sql('foo')).to be == '`foo` BETWEEN ? AND ?'
|
30
|
+
end
|
31
|
+
|
32
|
+
it "sql_like" do
|
33
|
+
expect(sql_like('xaic').as_sql('foo')).to be == '`foo` LIKE ?'
|
34
|
+
end
|
35
|
+
|
36
|
+
it "sql_or" do
|
37
|
+
expect(sql_or([sql_gt('bar'), sql_lt('baz')]).as_sql('foo')).to be == '(`foo` > ?) OR (`foo` < ?)'
|
38
|
+
end
|
39
|
+
|
40
|
+
it "sql_and" do
|
41
|
+
expect(sql_and([sql_gt('bar'), sql_lt('baz')]).as_sql('foo')).to be == '(`foo` > ?) AND (`foo` < ?)'
|
42
|
+
end
|
43
|
+
|
44
|
+
it "sql_op" do
|
45
|
+
expect(sql_op('IN (SELECT foo_id FROM bar WHERE t=?)',[44]).as_sql('foo_id')).to be == '`foo_id` IN (SELECT foo_id FROM bar WHERE t=?)'
|
46
|
+
end
|
47
|
+
|
48
|
+
it "sql_in([])" do
|
49
|
+
expect(sql_in([]).as_sql('foo')).to be == '0=1'
|
50
|
+
end
|
51
|
+
|
52
|
+
it "sql_not_in([])" do
|
53
|
+
expect(sql_not_in([]).as_sql('foo')).to be == '1=1'
|
54
|
+
end
|
55
|
+
end
|