sqliterate 0.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.
@@ -0,0 +1,40 @@
1
+ module SQLiterate
2
+ grammar Literals
3
+ include Space
4
+ include Identifier
5
+ include Keywords
6
+ include String
7
+
8
+ rule literal_value
9
+ multi_string / numeric_literal / boolean_literal
10
+ end
11
+
12
+ rule positional_param
13
+ '$' digits <Node::PositionalParam>
14
+ end
15
+
16
+ rule numeric_literal
17
+ decimal_literal exponent_literal <Node::NumericLiteral>
18
+ /
19
+ decimal_literal
20
+ end
21
+
22
+ rule decimal_literal
23
+ ( digits ( '.' digits? )? / '.' digits ) <Node::DecimalLiteral>
24
+ end
25
+
26
+ rule exponent_literal
27
+ 'e' s:( '+' / '-' )? digits <Node::ExponentLiteral>
28
+ end
29
+
30
+ rule digits
31
+ [0-9]+ <Node::Digits>
32
+ end
33
+
34
+ rule boolean_literal
35
+ true_kw <Node::Boolean::True>
36
+ /
37
+ false_kw <Node::Boolean::False>
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,65 @@
1
+ module SQLiterate
2
+ grammar Operator
3
+ include Space
4
+ include Comment
5
+ include Keywords
6
+
7
+ rule gen_operator
8
+ symbol_operator / keyword_operator
9
+ end
10
+
11
+ rule keyword_operator
12
+ ( and_kw / or_kw / not_kw ) <Node::KeywordOperator>
13
+ end
14
+
15
+ rule symbol_operator
16
+ sign_ending_operator
17
+ /
18
+ nonsign_ending_operator
19
+ /
20
+ ambiguous_operator
21
+ /
22
+ single_char_operator
23
+ end
24
+
25
+ rule single_char_operator
26
+ c:op_char <Node::SingleCharOperator>
27
+ end
28
+
29
+ rule sign_ending_operator
30
+ b:( r:( basic_op_char !special_op_char )* basic_op_char )? special_op_char
31
+ o:( r:( op_char !(sign_op_char !op_char) )* op_char )? sign_op_char !op_char
32
+ <Node::SignEndingOperator>
33
+ end
34
+
35
+ rule nonsign_ending_operator
36
+ r:( op_char !(nonsign_op_char !op_char) )* op_char nonsign_op_char !op_char
37
+ <Node::NonsignEndingOperator>
38
+ end
39
+
40
+ rule ambiguous_operator
41
+ r:( op_char !(sign_op_char !op_char) )* op_char
42
+ <Node::AmbiguousOperator>
43
+ end
44
+
45
+ rule sign_op_char
46
+ c:[+\-] comment* <Node::OperatorChar>
47
+ end
48
+
49
+ rule basic_op_char
50
+ c:[+\-*/<>=] comment* <Node::OperatorChar>
51
+ end
52
+
53
+ rule special_op_char
54
+ c:[~!@#%^&|`?] comment* <Node::OperatorChar>
55
+ end
56
+
57
+ rule nonsign_op_char
58
+ c:[*/<>=~!@#%^&|`?] comment* <Node::OperatorChar>
59
+ end
60
+
61
+ rule op_char
62
+ c:[+\-*/<>=~!@#%^&|`?] comment* <Node::OperatorChar>
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,25 @@
1
+ module SQLiterate
2
+ grammar Query
3
+ include Space
4
+ include Keywords
5
+ include Expression
6
+
7
+ rule query
8
+ space with_section space query_expression space <Node::Query>
9
+ end
10
+
11
+ rule with_section
12
+ (with_kw required_space with_queries / space) <Node::WithSection>
13
+ end
14
+
15
+ rule with_queries
16
+ with_query r:( space ',' space with_query )* <Node::WithQueries>
17
+ end
18
+
19
+ rule with_query
20
+ identifier required_space as_kw required_space '('
21
+ space query_expression space
22
+ ')' <Node::WithQuery>
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ module SQLiterate
2
+ grammar Space
3
+ include Comment
4
+
5
+ rule required_space
6
+ spacing+
7
+ end
8
+
9
+ rule space
10
+ spacing*
11
+ end
12
+
13
+ rule spacing
14
+ [ \n] / comment
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,47 @@
1
+ module SQLiterate
2
+ grammar String
3
+ include Identifier
4
+
5
+ rule multi_string
6
+ single_string t:( ' '* "\n" ' '* single_string )* <Node::MultiString>
7
+ end
8
+
9
+ rule single_string
10
+ std_string_constant / ext_string_constant
11
+ end
12
+
13
+ rule std_string_constant
14
+ "'" c:( std_character )* "'" <Node::StringConstant>
15
+ end
16
+
17
+ rule std_character
18
+ "''" <Node::Character::Quote>
19
+ /
20
+ [^'] <Node::Character::Text>
21
+ end
22
+
23
+ rule ext_string_constant
24
+ [eE] "'" c:( ext_character )* "'" <Node::StringConstant>
25
+ end
26
+
27
+ rule ext_character
28
+ "''" <Node::Character::Quote>
29
+ /
30
+ "\\" c:( ext_string_escape ) <Node::Character::ExtEscape>
31
+ /
32
+ [^\\'] <Node::Character::Text>
33
+ end
34
+
35
+ rule ext_string_escape
36
+ [bfnrt] <Node::Character::NotImplemented>
37
+ /
38
+ [0-7] 1..3 <Node::Character::NotImplemented>
39
+ /
40
+ 'x' [0-9A-F] 1..2 <Node::Character::NotImplemented>
41
+ /
42
+ [uU] ([0-9A-F] 8..8 / [0-9A-F] 4..4) <Node::Character::NotImplemented>
43
+ /
44
+ [^xuU] <Node::Character::Text>
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ # coding: utf-8
2
+ require 'sqliterate/node/control/expression'
3
+ require 'sqliterate/node/control/query'
@@ -0,0 +1,192 @@
1
+ module SQLiterate
2
+ module Node
3
+ module GenExpression
4
+ def tables
5
+ gen_value.tables + r.elements.flat_map { |e| e.gen_value.tables }
6
+ end
7
+ end
8
+
9
+ module GenValue
10
+ module Literal
11
+ def tables
12
+ []
13
+ end
14
+ end
15
+ module Subscript
16
+ def tables
17
+ field_selection.tables + r.elements.flat_map do |e|
18
+ e.range_expression.tables
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ module FunctionCall
25
+ def tables
26
+ function_params.tables
27
+ end
28
+ end
29
+
30
+ module TypeCast
31
+ def tables
32
+ scalar_expression.tables
33
+ end
34
+ end
35
+
36
+ module VoidExpression
37
+ def tables
38
+ []
39
+ end
40
+ end
41
+
42
+ module AggregateExpression
43
+ def tables
44
+ expressions_list.tables + order_by_clause.tables
45
+ end
46
+ end
47
+
48
+ module OrderByClause
49
+ def tables
50
+ respond_to?(:expressions_list) ? expressions_list.tables : []
51
+ end
52
+ end
53
+
54
+ module ExpressionsList
55
+ def tables
56
+ gen_expression.tables + r.elements.flat_map do |e|
57
+ e.gen_expression.tables
58
+ end
59
+ end
60
+ end
61
+
62
+ module NamedExpressionsList
63
+ def tables
64
+ named_expression.tables + r.elements.flat_map do |e|
65
+ e.named_expression.tables
66
+ end
67
+ end
68
+ end
69
+
70
+ module NamedExpression
71
+ def tables
72
+ gen_expression.tables
73
+ end
74
+ end
75
+
76
+ module AllColumns
77
+ def tables
78
+ []
79
+ end
80
+ end
81
+
82
+ module RangeExpression
83
+ def tables
84
+ b.tables + e.tables
85
+ end
86
+ end
87
+
88
+ module FieldSelection
89
+ def tables
90
+ row_value.tables
91
+ end
92
+ end
93
+
94
+ module RowValue
95
+ module Query
96
+ def tables
97
+ query_expression.tables
98
+ end
99
+ end
100
+ module Expression
101
+ def tables
102
+ gen_expression.tables
103
+ end
104
+ end
105
+ module Identifier
106
+ def tables
107
+ []
108
+ end
109
+ end
110
+ end
111
+
112
+ module QueryExpression
113
+ def tables
114
+ query_value.tables + r.elements.flat_map { |e| e.query_value.tables }
115
+ end
116
+ end
117
+
118
+ module QueryValue
119
+ module Expression
120
+ def tables
121
+ query_expression.tables
122
+ end
123
+ end
124
+ module Select
125
+ end
126
+ end
127
+
128
+ module SelectQuery
129
+ def tables
130
+ select_list.tables + table_expression.tables
131
+ end
132
+ end
133
+
134
+ module TableExpression
135
+ def tables
136
+ from_clause.tables
137
+ end
138
+ end
139
+
140
+ module FromClause
141
+ def tables
142
+ respond_to?(:table_references) ? table_references.tables : []
143
+ end
144
+ end
145
+
146
+ module TableReferences
147
+ def tables
148
+ table_joins.tables + r.elements.flat_map { |e| e.table_joins.tables }
149
+ end
150
+ end
151
+
152
+ module TableJoins
153
+ def tables
154
+ [table_reference.table] + r.elements.map { |e| e.joined_table.table }
155
+ end
156
+ end
157
+
158
+ module TableReference
159
+ def table
160
+ if respond_to?(:table_spec)
161
+ [ table_name.name, table_spec.tables ]
162
+ else
163
+ [ name, [name] ]
164
+ end
165
+ end
166
+ end
167
+
168
+ module TableSpec
169
+ module Query
170
+ def tables
171
+ query_expression.tables
172
+ end
173
+ end
174
+ module Joins
175
+ def tables
176
+ table_joins.tables
177
+ end
178
+ end
179
+ module Table
180
+ def tables
181
+ [name]
182
+ end
183
+ end
184
+ end
185
+
186
+ module Join
187
+ def table
188
+ table_reference.table
189
+ end
190
+ end
191
+ end
192
+ end
@@ -0,0 +1,39 @@
1
+ module SQLiterate
2
+ module Node
3
+ module Query
4
+ def common_tables
5
+ Hash[ with_section.tables.map { |t| [t.first, expand_table(t)] } ]
6
+ end
7
+
8
+ def tables
9
+ cte = common_tables
10
+ query_expression.tables.flat_map { |t| expand_table(t) }
11
+ .flat_map { |t| cte[t] || [t] }
12
+ .sort.uniq
13
+ end
14
+
15
+ def expand_table(t)
16
+ a, b = t
17
+ b ? b.flat_map { |x| expand_table(x) } : [a]
18
+ end
19
+ end
20
+
21
+ module WithSection
22
+ def tables
23
+ respond_to?(:with_queries) ? with_queries.tables : []
24
+ end
25
+ end
26
+
27
+ module WithQueries
28
+ def tables
29
+ [with_query.table] + r.elements.map { |e| e.with_query.table }
30
+ end
31
+ end
32
+
33
+ module WithQuery
34
+ def table
35
+ [ identifier.name, query_expression.tables ]
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ # coding: utf-8
2
+ require 'sqliterate/node/evaluate/literals'
3
+ require 'sqliterate/node/evaluate/string'
4
+ require 'sqliterate/node/evaluate/operator'
5
+ require 'sqliterate/node/evaluate/expression'