twowaysql 0.2.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/History.txt +3 -0
- data/License.txt +13 -0
- data/Manifest.txt +38 -0
- data/README.txt +382 -0
- data/Rakefile +4 -0
- data/config/hoe.rb +73 -0
- data/config/requirements.rb +15 -0
- data/issues/issue-25efcfc383f3b0f6c0e2730ae7c2975bb2b3de26.yaml +18 -0
- data/issues/issue-39023ea09e17e2d64bcef03aa59cdfe38b78ad5b.yaml +26 -0
- data/issues/issue-4bc308d55ae91f266e656162a4147d356de1166c.yaml +24 -0
- data/issues/issue-897995fa10377eabdf597e8e7692f17087c76923.yaml +26 -0
- data/issues/issue-bd38c1cdc965d73dd629a81db2de1bcdcf4b10b8.yaml +26 -0
- data/issues/issue-f2b773020b54f839c03d899b38b5113c8fd991df.yaml +18 -0
- data/issues/issue-f39b907d01d7fa93df8c7a9de2e1b5e27727ee0a.yaml +18 -0
- data/issues/issue-f64d73ed4f9854f1ded77e6496dbf59cfb3770a7.yaml +18 -0
- data/issues/project.yaml +16 -0
- data/lib/twowaysql/node.rb +239 -0
- data/lib/twowaysql/parser.rb +489 -0
- data/lib/twowaysql/parser.y +226 -0
- data/lib/twowaysql/template.rb +75 -0
- data/lib/twowaysql/version.rb +9 -0
- data/lib/twowaysql.rb +6 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/script/txt2html +82 -0
- data/setup.rb +1585 -0
- data/spec/large_sql_spec.rb +142 -0
- data/spec/learning_regex_spec.rb +234 -0
- data/spec/spec.opts +0 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/twowaysql_spec.rb +736 -0
- data/tasks/deployment.rake +53 -0
- data/tasks/ditz.rake +8 -0
- data/tasks/environment.rake +7 -0
- data/tasks/racc.rake +23 -0
- data/tasks/rspec.rake +21 -0
- data/tasks/website.rake +17 -0
- metadata +104 -0
@@ -0,0 +1,142 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
|
4
|
+
describe TwoWaySQL::Template do
|
5
|
+
|
6
|
+
describe "multi-line SQL and preserve space example" do
|
7
|
+
|
8
|
+
before do
|
9
|
+
@sql = <<-EOS
|
10
|
+
SELECT DISTINCT
|
11
|
+
i.id AS item_id
|
12
|
+
,d.display_name AS display_name
|
13
|
+
,h.status AS status_id
|
14
|
+
,i.unique_name AS unique_name
|
15
|
+
,i.created_on
|
16
|
+
FROM
|
17
|
+
some_schema.item i
|
18
|
+
INNER JOIN some_schema.item_detail d
|
19
|
+
ON i.id = d.item_id
|
20
|
+
INNER JOIN some_schema.item_history h
|
21
|
+
ON i.id = h.item_id
|
22
|
+
|
23
|
+
/*BEGIN*/WHERE
|
24
|
+
/*IF ctx[:name] */AND i.name ILIKE /*ctx[:name]*/'hoge%'/*END*/
|
25
|
+
/*IF ctx[:display_name] */AND d.display_name ILIKE /*ctx[:display_name]*/'hoge%'/*END*/
|
26
|
+
/*IF ctx[:status] */AND h.status IN /*ctx[:status]*/(3, 4, 9)/*END*/
|
27
|
+
/*IF ctx[:ignore_status] */AND h.status NOT IN /*ctx[:ignore_status]*/(4, 9)/*END*/
|
28
|
+
/*END*/
|
29
|
+
|
30
|
+
/*IF ctx[:order_by] */ ORDER BY /*$ctx[:order_by]*/i.id /*$ctx[:order]*/ASC /*END*/
|
31
|
+
/*IF ctx[:limit] */ LIMIT /*ctx[:limit]*/10/*END*/
|
32
|
+
/*IF ctx[:offset] */ OFFSET /*ctx[:offset]*/0/*END*/
|
33
|
+
EOS
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
describe do
|
38
|
+
before do
|
39
|
+
template = TwoWaySQL::Template.parse(@sql)
|
40
|
+
@result = template.merge(:name => "HOGE", :status => [3, 4])
|
41
|
+
end
|
42
|
+
|
43
|
+
it "sql" do
|
44
|
+
expected = <<-EOS
|
45
|
+
SELECT DISTINCT
|
46
|
+
i.id AS item_id
|
47
|
+
,d.display_name AS display_name
|
48
|
+
,h.status AS status_id
|
49
|
+
,i.unique_name AS unique_name
|
50
|
+
,i.created_on
|
51
|
+
FROM
|
52
|
+
some_schema.item i
|
53
|
+
INNER JOIN some_schema.item_detail d
|
54
|
+
ON i.id = d.item_id
|
55
|
+
INNER JOIN some_schema.item_history h
|
56
|
+
ON i.id = h.item_id
|
57
|
+
|
58
|
+
WHERE
|
59
|
+
i.name ILIKE ?
|
60
|
+
|
61
|
+
AND h.status IN (?, ?)
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
EOS
|
69
|
+
@result.sql.should == expected
|
70
|
+
end
|
71
|
+
|
72
|
+
it "bound_variables" do
|
73
|
+
@result.bound_variables.should == ["HOGE", 3, 4]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
describe "a little complex UPDATE statement example" do
|
82
|
+
|
83
|
+
before do
|
84
|
+
@sql = <<-EOS
|
85
|
+
UPDATE
|
86
|
+
some_schema.item
|
87
|
+
|
88
|
+
SET
|
89
|
+
display_order =
|
90
|
+
CASE display_order
|
91
|
+
WHEN NULL THEN 1 + /*ctx[:target_id_list].size*/1
|
92
|
+
ELSE display_order + /*ctx[:target_id_list].size*/1
|
93
|
+
END
|
94
|
+
,updated_on = CURRENT_TIMESTAMP
|
95
|
+
,updated_by = /*ctx[:account_id]*/999
|
96
|
+
|
97
|
+
WHERE
|
98
|
+
item_id IN /*ctx[:item_id_list]*/(25,26,27)
|
99
|
+
/*IF ctx[:status_id] */AND status_id = /*ctx[:status_id]*/100/*END*/
|
100
|
+
EOS
|
101
|
+
end
|
102
|
+
|
103
|
+
describe do
|
104
|
+
before do
|
105
|
+
template = TwoWaySQL::Template.parse(@sql)
|
106
|
+
data = {
|
107
|
+
:target_id_list => [11,12,13],
|
108
|
+
:item_id_list => [31,32,33,34],
|
109
|
+
:account_id => 50,
|
110
|
+
:status_id => 2
|
111
|
+
}
|
112
|
+
@result = template.merge(data)
|
113
|
+
end
|
114
|
+
|
115
|
+
it "sql" do
|
116
|
+
expected = <<-EOS
|
117
|
+
UPDATE
|
118
|
+
some_schema.item
|
119
|
+
|
120
|
+
SET
|
121
|
+
display_order =
|
122
|
+
CASE display_order
|
123
|
+
WHEN NULL THEN 1 + ?
|
124
|
+
ELSE display_order + ?
|
125
|
+
END
|
126
|
+
,updated_on = CURRENT_TIMESTAMP
|
127
|
+
,updated_by = ?
|
128
|
+
|
129
|
+
WHERE
|
130
|
+
item_id IN (?, ?, ?, ?)
|
131
|
+
AND status_id = ?
|
132
|
+
EOS
|
133
|
+
@result.sql.should == expected
|
134
|
+
end
|
135
|
+
|
136
|
+
it "bound_variables" do
|
137
|
+
@result.bound_variables.should == [3, 3, 50, 31, 32, 33, 34, 2]
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
@@ -0,0 +1,234 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
|
4
|
+
describe "Ruby Regex" do
|
5
|
+
|
6
|
+
describe "comment node match" do
|
7
|
+
before do
|
8
|
+
@re = /\/\*.*?\*\//m
|
9
|
+
end
|
10
|
+
|
11
|
+
it "comment node" do
|
12
|
+
str = "int a = 1; /* this is a comment.*/"
|
13
|
+
str.scan(@re).should == ["/* this is a comment.*/"]
|
14
|
+
end
|
15
|
+
|
16
|
+
it "multi-line commnet" do
|
17
|
+
str = <<-EOS
|
18
|
+
Hey,
|
19
|
+
/*
|
20
|
+
this is commented
|
21
|
+
*/
|
22
|
+
non-comment
|
23
|
+
EOS
|
24
|
+
str.scan(@re).should == ["/*\nthis is commented\n*/"]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
describe "string expression" do
|
30
|
+
before do
|
31
|
+
@str_re = /"([^"\\]*(?:\\.[^"\\]*)*)"/
|
32
|
+
end
|
33
|
+
it "normal string" do
|
34
|
+
(@str_re =~ 'he said "foo bar"').should_not be_nil
|
35
|
+
$1.should == 'foo bar'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
describe "non-whitespace delimiters" do
|
42
|
+
before do
|
43
|
+
@str_re = /\A(\S+?)(?=\s*(?:(?:\/|\#)\*|-{2,}|\(|\)|\,))/
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "-- comment start" do
|
47
|
+
it do
|
48
|
+
(@str_re =~ 'foo-bar--ELSE').should_not be_nil
|
49
|
+
$1.should == 'foo-bar'
|
50
|
+
$'.should == '--ELSE'
|
51
|
+
end
|
52
|
+
it 'foo-bar--ELSE' do
|
53
|
+
(@str_re =~ 'foo-bar--ELSE').should_not be_nil
|
54
|
+
$1.should == 'foo-bar'
|
55
|
+
$'.should == '--ELSE'
|
56
|
+
end
|
57
|
+
it 'foo-bar --ELSE' do
|
58
|
+
(@str_re =~ 'foo-bar --ELSE').should_not be_nil
|
59
|
+
$1.should == 'foo-bar'
|
60
|
+
$'.should == ' --ELSE'
|
61
|
+
end
|
62
|
+
it 'foobar--ELSE' do
|
63
|
+
(@str_re =~ 'foobar--ELSE').should_not be_nil
|
64
|
+
$1.should == 'foobar'
|
65
|
+
$'.should == '--ELSE'
|
66
|
+
end
|
67
|
+
it 'foo-bar---ELSE' do
|
68
|
+
(@str_re =~ 'foo-bar---ELSE').should_not be_nil
|
69
|
+
$1.should == 'foo-bar'
|
70
|
+
$'.should == '---ELSE'
|
71
|
+
end
|
72
|
+
it "it's OK to not to match 'foobar'" do
|
73
|
+
(@str_re =~ 'foobar').should be_nil
|
74
|
+
$1.should be_nil
|
75
|
+
$&.should be_nil
|
76
|
+
$'.should be_nil
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "/* comment start" do
|
81
|
+
it "match before spaces" do
|
82
|
+
(@str_re =~ 'foo/bar /* hoge */').should_not be_nil
|
83
|
+
$1.should == 'foo/bar'
|
84
|
+
$'.should == ' /* hoge */'
|
85
|
+
end
|
86
|
+
it do
|
87
|
+
(@str_re =~ 'foo/bar/* hoge */').should_not be_nil
|
88
|
+
$1.should == 'foo/bar'
|
89
|
+
$'.should == '/* hoge */'
|
90
|
+
end
|
91
|
+
it do
|
92
|
+
(@str_re =~ 'foo/bar/* hoge */').should_not be_nil
|
93
|
+
$1.should == 'foo/bar'
|
94
|
+
$'.should == '/* hoge */'
|
95
|
+
end
|
96
|
+
it do
|
97
|
+
(@str_re =~ 'foobar/* hoge */').should_not be_nil
|
98
|
+
$1.should == 'foobar'
|
99
|
+
$'.should == '/* hoge */'
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe "#* comment start" do
|
104
|
+
it "match before spaces" do
|
105
|
+
(@str_re =~ 'foo#bar #* hoge *#').should_not be_nil
|
106
|
+
$1.should == 'foo#bar'
|
107
|
+
$'.should == ' #* hoge *#'
|
108
|
+
end
|
109
|
+
it do
|
110
|
+
(@str_re =~ 'foo#bar#* hoge *#').should_not be_nil
|
111
|
+
$1.should == 'foo#bar'
|
112
|
+
$'.should == '#* hoge *#'
|
113
|
+
end
|
114
|
+
it do
|
115
|
+
(@str_re =~ 'foo#bar#* hoge *#').should_not be_nil
|
116
|
+
$1.should == 'foo#bar'
|
117
|
+
$'.should == '#* hoge *#'
|
118
|
+
end
|
119
|
+
it do
|
120
|
+
(@str_re =~ 'foobar#* hoge *#').should_not be_nil
|
121
|
+
$1.should == 'foobar'
|
122
|
+
$'.should == '#* hoge *#'
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
describe "comma" do
|
127
|
+
it do
|
128
|
+
(@str_re =~ 'foo,bar').should_not be_nil
|
129
|
+
$1.should == 'foo'
|
130
|
+
$'.should == ',bar'
|
131
|
+
end
|
132
|
+
it do
|
133
|
+
(@str_re =~ 'foo , bar').should_not be_nil
|
134
|
+
$1.should == 'foo'
|
135
|
+
$'.should == ' , bar'
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
describe "left paren" do
|
140
|
+
it do
|
141
|
+
(@str_re =~ 'foo(bar').should_not be_nil
|
142
|
+
$1.should == 'foo'
|
143
|
+
$'.should == '(bar'
|
144
|
+
end
|
145
|
+
it do
|
146
|
+
(@str_re =~ 'foo ( bar').should_not be_nil
|
147
|
+
$1.should == 'foo'
|
148
|
+
$'.should == ' ( bar'
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
describe "right paren" do
|
153
|
+
it do
|
154
|
+
(@str_re =~ 'foo)bar').should_not be_nil
|
155
|
+
$1.should == 'foo'
|
156
|
+
$'.should == ')bar'
|
157
|
+
end
|
158
|
+
it do
|
159
|
+
(@str_re =~ 'foo ) bar').should_not be_nil
|
160
|
+
$1.should == 'foo'
|
161
|
+
$'.should == ' ) bar'
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
end
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
describe "double-single-quote escape" do
|
170
|
+
before do
|
171
|
+
@str_re = /(\'(?:[^\']+|\'\')*\')/
|
172
|
+
end
|
173
|
+
it do
|
174
|
+
(@str_re =~ "he said 'foo bar' then 'baz'").should_not be_nil
|
175
|
+
$1.should == "'foo bar'"
|
176
|
+
end
|
177
|
+
it do
|
178
|
+
(@str_re =~ "he said 'Let''s go' then went out").should_not be_nil
|
179
|
+
$1.should == "'Let''s go'"
|
180
|
+
end
|
181
|
+
it do
|
182
|
+
(@str_re =~ "he said 'foo bar'then went out").should_not be_nil
|
183
|
+
$1.should == "'foo bar'"
|
184
|
+
end
|
185
|
+
it do
|
186
|
+
sql = "SELECT * FROM emp/*BEGIN*/ WHERE /*IF ctx[:job]*/job = /*ctx[:job]*/'CLERK'/*END*//*IF ctx['deptno']*/ AND deptno = /*ctx[:deptno]*/20/*END*//*END*/"
|
187
|
+
(@str_re =~ sql).should_not be_nil
|
188
|
+
$1.should == "'CLERK'"
|
189
|
+
end
|
190
|
+
|
191
|
+
end
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
describe "comment start-end pair" do
|
196
|
+
before do
|
197
|
+
@str_re = /\A(\/|\#)\*([^\*]+)\*\1/
|
198
|
+
end
|
199
|
+
|
200
|
+
describe "matched pair" do
|
201
|
+
it do
|
202
|
+
(@str_re =~ '/*ctx[:job]*/').should_not be_nil
|
203
|
+
$1.should == '/'
|
204
|
+
$2.should == 'ctx[:job]'
|
205
|
+
$'.should == ''
|
206
|
+
end
|
207
|
+
|
208
|
+
it do
|
209
|
+
(@str_re =~ '#*ctx[:job]*#').should_not be_nil
|
210
|
+
$1.should == '#'
|
211
|
+
$2.should == 'ctx[:job]'
|
212
|
+
$'.should == ''
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
describe "unmatched pair" do
|
217
|
+
it do
|
218
|
+
(@str_re =~ '/*ctx[:job]*#').should be_nil
|
219
|
+
$1.should be_nil
|
220
|
+
$2.should be_nil
|
221
|
+
$'.should be_nil
|
222
|
+
end
|
223
|
+
|
224
|
+
it do
|
225
|
+
(@str_re =~ '/*ctx[:job]*#').should be_nil
|
226
|
+
$1.should be_nil
|
227
|
+
$2.should be_nil
|
228
|
+
$'.should be_nil
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
end
|
233
|
+
|
234
|
+
end
|
data/spec/spec.opts
ADDED
File without changes
|