sql-parser-vlad 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 75f041d77f950fb458f43d3e7489eb2b3098f9ac780cb09c70a1bde601327fed
4
+ data.tar.gz: d4dbd3abd54f43cb992f5d726e3b390b1fda1e14feed4a7f4905bd9018da37a1
5
+ SHA512:
6
+ metadata.gz: 9f1198e58fcde7c0b509e69c6605cafbfc963aaf396f9d9582f78f2a41c4fdeb70ab4185b13c7931356c120ada5400789c467df8f8cf6c90804180560b59e66d
7
+ data.tar.gz: 40f615ac67cc626027677628dc67685ff3ae4638e7c138a48374dfb0aa4ca6afc2b8b2a21e01eee93213ae55319cf0c0cbcf5694731681855830eb29a5bad7d5
@@ -0,0 +1,338 @@
1
+ class SQLParser::Parser
2
+
3
+ rule
4
+ # dynamic sql
5
+
6
+ direct_sql_statement
7
+ : direct_sql_data_statement
8
+
9
+ direct_sql_data_statement
10
+ : direct_select_statement_multiple_rows
11
+ | insert_specification
12
+
13
+ direct_select_statement_multiple_rows
14
+ : query_expression order_by_clause { result = SQLParser::Statement::DirectSelect.new(val[0], val[1]) }
15
+
16
+ # module contents
17
+ order_by_clause
18
+ : # no action
19
+ | ORDER BY sort_specification_list { result = SQLParser::Statement::OrderBy.new(val[2]) }
20
+
21
+ sort_specification_list
22
+ : sort_specification_list comma sort_specification { result = Array(val[0]) + Array(val[2]) }
23
+ | sort_specification
24
+
25
+ sort_specification
26
+ : sort_key ordering_specification { result = val[1].new(val[0]) }
27
+
28
+ sort_key
29
+ : column_name
30
+ | unsigned_integer { result = SQLParser::Statement::Integer.new(val[0]) }
31
+
32
+ ordering_specification
33
+ : { result = SQLParser::Statement::Ascending } # default
34
+ | ASC { result = SQLParser::Statement::Ascending }
35
+ | DESC { result = SQLParser::Statement::Descending }
36
+
37
+ # queries
38
+ subquery
39
+ # FIXME: shortcut
40
+ : left_paren query_expression right_paren { result = SQLParser::Statement::Subquery.new(val[1]) }
41
+
42
+ query_expression
43
+ : query_specification
44
+
45
+ insert_specification
46
+ : INSERT INTO table_reference value_list { result = SQLParser::Statement::Insert.new(val[2], nil, val[3]) }
47
+ | INSERT INTO table_reference column_list value_list { result = SQLParser::Statement::Insert.new(val[2], val[3], val[4]) }
48
+
49
+ column_list
50
+ : left_paren in_column_list right_paren { result = SQLParser::Statement::InColumnList.new(val[1]) }
51
+
52
+ in_column_list
53
+ : value_expression comma in_column_list { result = Array(val[0]) + Array(val[2]) }
54
+ | value_expression
55
+
56
+ value_list
57
+ : VALUES left_paren in_value_list right_paren { result = SQLParser::Statement::InValueList.new(val[2]) }
58
+
59
+ query_specification
60
+ : SELECT DISTINCTROW select_list table_expression { result = SQLParser::Statement::Select.new(val[2], val[3], "DISTINCTROW") }
61
+ | SELECT DISTINCTROW select_list { result = SQLParser::Statement::Select.new(val[2], nil, "DISTINCTROW") }
62
+ | SELECT DISTINCT select_list table_expression { result = SQLParser::Statement::Select.new(val[2], val[3], "DISTINCT") }
63
+ | SELECT DISTINCT select_list { result = SQLParser::Statement::Select.new(val[2], nil, "DISTINCT") }
64
+ | SELECT ALL select_list table_expression { result = SQLParser::Statement::Select.new(val[2], val[3], "ALL") }
65
+ | SELECT ALL select_list { result = SQLParser::Statement::Select.new(val[2], nil, "ALL") }
66
+ | SELECT select_list table_expression { result = SQLParser::Statement::Select.new(val[1], val[2]) }
67
+ | SELECT select_list { result = SQLParser::Statement::Select.new(val[1]) }
68
+
69
+ select_list
70
+ : asterisk { result = SQLParser::Statement::All.new }
71
+ | select_sublist { result = SQLParser::Statement::SelectList.new(val[0]) }
72
+
73
+ select_sublist
74
+ : derived_column comma select_sublist { result = Array(val[0]) + Array(val[2]) }
75
+ | derived_column
76
+
77
+ derived_column
78
+ : value_expression AS column_name { result = SQLParser::Statement::As.new(val[0], val[2]) }
79
+ | value_expression column_name { result = SQLParser::Statement::As.new(val[0], val[1]) }
80
+ | value_expression
81
+
82
+ table_expression
83
+ : from_clause where_clause group_by_clause having_clause { result = SQLParser::Statement::TableExpression.new(val[0], val[1], val[2], val[3]) }
84
+
85
+ from_clause
86
+ : FROM table_reference { result = SQLParser::Statement::FromClause.new(val[1]) }
87
+
88
+ table_reference
89
+ : table_name AS column_name { result = SQLParser::Statement::As.new(val[0], val[2]) }
90
+ | table_name column_name { result = SQLParser::Statement::As.new(val[0], val[1]) }
91
+ | table_name
92
+ | joined_table
93
+
94
+ table_subquery
95
+ : subquery
96
+
97
+ joined_table
98
+ : cross_join
99
+ | qualified_join
100
+
101
+ cross_join
102
+ : table_reference comma table_name { result = SQLParser::Statement::CrossJoin.new(val[0], val[2]) }
103
+ | table_reference CROSS JOIN table_name { result = SQLParser::Statement::CrossJoin.new(val[0], val[3]) }
104
+
105
+ qualified_join
106
+ : table_reference join_type JOIN table_reference join_specification { result = val[1].new(val[0], val[3], val[4]) }
107
+
108
+ join_type
109
+ : INNER { result = SQLParser::Statement::InnerJoin }
110
+ | LEFT OUTER { result = SQLParser::Statement::LeftOuterJoin }
111
+ | LEFT { result = SQLParser::Statement::LeftJoin }
112
+ | RIGHT OUTER { result = SQLParser::Statement::RightOuterJoin }
113
+ | RIGHT { result = SQLParser::Statement::RightJoin }
114
+ | FULL { result = SQLParser::Statement::FullJoin }
115
+ | FULL OUTER { result = SQLParser::Statement::FullOuterJoin }
116
+
117
+ join_specification
118
+ : join_condition
119
+ | named_columns_join
120
+
121
+ join_condition
122
+ : ON search_condition { result = SQLParser::Statement::On.new(val[1]) }
123
+
124
+ named_columns_join
125
+ : USING left_paren join_column_list right_paren { result = SQLParser::Statement::Using.new(val[2]) }
126
+
127
+ join_column_list
128
+ : column_name_list
129
+
130
+ where_clause
131
+ : # no action
132
+ | WHERE search_condition { result = SQLParser::Statement::WhereClause.new(val[1]) }
133
+
134
+ group_by_clause
135
+ : # no action
136
+ | GROUP BY grouping_column_reference_list { result = SQLParser::Statement::GroupByClause.new(val[2]) }
137
+
138
+ grouping_column_reference_list
139
+ : grouping_column_reference_list comma grouping_column_reference { result = Array(val[0]) + Array(val[2]) }
140
+ | grouping_column_reference
141
+
142
+ grouping_column_reference
143
+ : column_reference
144
+
145
+ having_clause
146
+ : # no action
147
+ | HAVING search_condition { result = SQLParser::Statement::HavingClause.new(val[1]) }
148
+
149
+ # query expression components
150
+ row_subquery
151
+ : subquery
152
+
153
+ between_predicate
154
+ : row_value_constructor NOT BETWEEN row_value_constructor AND row_value_constructor { result = SQLParser::Statement::Not.new(SQLParser::Statement::Between.new(val[0], val[3], val[5])) }
155
+ | row_value_constructor BETWEEN row_value_constructor AND row_value_constructor { result = SQLParser::Statement::Between.new(val[0], val[2], val[4]) }
156
+
157
+ in_predicate
158
+ : row_value_constructor NOT IN in_predicate_value { result = SQLParser::Statement::Not.new(SQLParser::Statement::In.new(val[0], val[3])) }
159
+ | row_value_constructor IN in_predicate_value { result = SQLParser::Statement::In.new(val[0], val[2]) }
160
+
161
+ in_predicate_value
162
+ : table_subquery
163
+ | left_paren in_value_list right_paren { result = SQLParser::Statement::InValueList.new(val[1]) }
164
+
165
+ in_value_list
166
+ : value_expression comma in_value_list { result = Array(val[0]) + Array(val[2]) }
167
+ | value_expression
168
+
169
+ like_predicate
170
+ # FIXME: the SQL-92 grammar indicates these should be
171
+ # character_value_expression nodes, but changing them causes reduce/reduce
172
+ # conflicts.
173
+ : row_value_constructor NOT LIKE row_value_constructor { result = SQLParser::Statement::Not.new(SQLParser::Statement::Like.new(val[0], val[3])) }
174
+ | row_value_constructor LIKE row_value_constructor { result = SQLParser::Statement::Like.new(val[0], val[2]) }
175
+
176
+ null_predicate
177
+ : row_value_constructor IS NOT NULL { result = SQLParser::Statement::Not.new(SQLParser::Statement::Is.new(val[0], SQLParser::Statement::Null.new)) }
178
+ | row_value_constructor IS NULL { result = SQLParser::Statement::Is.new(val[0], SQLParser::Statement::Null.new) }
179
+
180
+ exists_predicate
181
+ : EXISTS table_subquery { result = SQLParser::Statement::Exists.new(val[1]) }
182
+
183
+ # constraints
184
+ table_name
185
+ : identifier { result = SQLParser::Statement::Table.new(val[0]) }
186
+
187
+ column_name_list
188
+ : column_name_list comma column_name { result = Array(val[0]) + Array(val[2]) }
189
+ | column_name
190
+
191
+ # search condition
192
+ search_condition
193
+ : boolean_term
194
+ | search_condition OR boolean_term { result = SQLParser::Statement::Or.new(val[0], val[2]) }
195
+
196
+ boolean_term
197
+ : boolean_factor
198
+ | boolean_term AND boolean_factor { result = SQLParser::Statement::And.new(val[0], val[2]) }
199
+
200
+ boolean_factor:
201
+ : NOT boolean_test { result = SQLParser::Statement::Not.new(val[1]) }
202
+ | boolean_test
203
+
204
+ boolean_test
205
+ : boolean_primary
206
+
207
+ boolean_primary
208
+ : predicate
209
+ | left_paren search_condition right_paren { result = val[1] }
210
+
211
+ predicate
212
+ : comparison_predicate
213
+ | between_predicate
214
+ | in_predicate
215
+ | like_predicate
216
+ | null_predicate
217
+ | exists_predicate
218
+
219
+ comparison_predicate
220
+ : row_value_constructor equals_operator row_value_constructor { result = SQLParser::Statement::Equals.new(val[0], val[2]) }
221
+ | row_value_constructor not_equals_operator row_value_constructor { result = SQLParser::Statement::Not.new(SQLParser::Statement::Equals.new(val[0], val[2])) }
222
+ | row_value_constructor less_than_operator row_value_constructor { result = SQLParser::Statement::Less.new(val[0], val[2]) }
223
+ | row_value_constructor greater_than_operator row_value_constructor { result = SQLParser::Statement::Greater.new(val[0], val[2]) }
224
+ | row_value_constructor less_than_or_equals_operator row_value_constructor { result = SQLParser::Statement::LessOrEquals.new(val[0], val[2]) }
225
+ | row_value_constructor greater_than_or_equals_operator row_value_constructor { result = SQLParser::Statement::GreaterOrEquals.new(val[0], val[2]) }
226
+
227
+ row_value_constructor
228
+ : row_value_constructor_element
229
+ | row_subquery
230
+
231
+ row_value_constructor_element
232
+ : value_expression
233
+
234
+ value_expression
235
+ : numeric_value_expression
236
+ | general_literal
237
+
238
+ numeric_value_expression
239
+ : term plus_sign numeric_value_expression { result = SQLParser::Statement::Add.new(val[0], val[2]) }
240
+ | term minus_sign numeric_value_expression { result = SQLParser::Statement::Subtract.new(val[0], val[2]) }
241
+ | term
242
+
243
+ term
244
+ : factor asterisk term { result = SQLParser::Statement::Multiply.new(val[0], val[2]) }
245
+ | factor solidus term { result = SQLParser::Statement::Divide.new(val[0], val[2]) }
246
+ | factor
247
+
248
+ factor
249
+ : sign value_expression_primary { result = val[0].new(val[1]) }
250
+ | value_expression_primary
251
+
252
+ value_expression_primary
253
+ : unsigned_value_specification
254
+ | column_reference
255
+ | set_function_specification
256
+ | left_paren value_expression right_paren { result = val[1] }
257
+
258
+ unsigned_value_specification
259
+ : unsigned_literal
260
+ | general_value_specification
261
+
262
+ unsigned_literal
263
+ : unsigned_numeric_literal
264
+ | general_literal
265
+
266
+ general_value_specification
267
+ : CURRENT_USER { result = SQLParser::Statement::CurrentUser.new }
268
+
269
+ column_reference
270
+ : qualifier period column_name { result = SQLParser::Statement::QualifiedColumn.new(val[0], val[2]) }
271
+ | column_name
272
+
273
+ qualifier
274
+ : table_name
275
+
276
+ set_function_specification
277
+ : COUNT left_paren asterisk right_paren { result = SQLParser::Statement::Count.new(SQLParser::Statement::All.new) }
278
+ | general_set_function
279
+
280
+ general_set_function
281
+ : COUNT left_paren value_expression right_paren { result = SQLParser::Statement::Count.new(val[2]) }
282
+ | AVG left_paren value_expression right_paren { result = SQLParser::Statement::Average.new(val[2]) }
283
+ | MAX left_paren value_expression right_paren { result = SQLParser::Statement::Maximum.new(val[2]) }
284
+ | MIN left_paren value_expression right_paren { result = SQLParser::Statement::Minimum.new(val[2]) }
285
+ | SUM left_paren value_expression right_paren { result = SQLParser::Statement::Sum.new(val[2]) }
286
+
287
+ # literal numbers, strings, dates and times
288
+ unsigned_numeric_literal
289
+ : exact_numeric_literal
290
+ | approximate_numeric_literal
291
+
292
+ exact_numeric_literal
293
+ : unsigned_integer period unsigned_integer { result = SQLParser::Statement::Float.new("#{val[0]}.#{val[2]}".to_f) }
294
+ | unsigned_integer period { result = SQLParser::Statement::Float.new(val[0]) }
295
+ | period unsigned_integer { result = SQLParser::Statement::Float.new("0.#{val[1]}".to_f) }
296
+ | unsigned_integer { result = SQLParser::Statement::Integer.new(val[0]) }
297
+
298
+ approximate_numeric_literal
299
+ : mantissa E exponent { result = SQLParser::Statement::ApproximateFloat.new(val[0], val[2]) }
300
+
301
+ mantissa
302
+ : exact_numeric_literal
303
+
304
+ exponent
305
+ : signed_integer
306
+
307
+ signed_integer
308
+ : sign unsigned_integer { result = val[0].new(SQLParser::Statement::Integer.new(val[1])) }
309
+ | unsigned_integer { result = SQLParser::Statement::Integer.new(val[0]) }
310
+
311
+ sign
312
+ : plus_sign { result = SQLParser::Statement::UnaryPlus }
313
+ | minus_sign { result = SQLParser::Statement::UnaryMinus }
314
+
315
+ # sql module
316
+ column_name
317
+ : identifier { result = SQLParser::Statement::Column.new(val[0]) }
318
+
319
+ # literals
320
+ general_literal
321
+ : quote character_string_literal quote { result = SQLParser::Statement::String.new(val[1]) }
322
+ | quote quote { result = SQLParser::Statement::String.new('') }
323
+ | datetime_literal
324
+
325
+ datetime_literal
326
+ : date_literal
327
+
328
+ date_literal
329
+ : DATE date_string { result = SQLParser::Statement::Date.new(val[1]) }
330
+
331
+ ---- header ----
332
+ require File.dirname(__FILE__) + '/parser.rex.rb'
333
+
334
+ ---- inner ----
335
+
336
+ def self.parse(sql)
337
+ new.scan_str(sql)
338
+ end