@@ -20,22 +20,31 @@ defmodule SQL.Adapters.ANSI do
2020 def token_to_string ( { tag , _ , [ left ] } , mod ) when tag in ~w[ asc desc isnull notnull] a do
2121 "#{ mod . token_to_string ( left ) } #{ mod . token_to_string ( tag ) } "
2222 end
23- def token_to_string ( { :fun , _ , [ left , right ] } , mod ) do
23+ def token_to_string ( { :not = tag , _ , [ { t , _ , _ } = left ] } , mod ) when t == :ident do
24+ "#{ mod . token_to_string ( left ) } #{ mod . token_to_string ( tag ) } "
25+ end
26+ def token_to_string ( { :fn , _ , [ left , right ] } , mod ) do
2427 "#{ mod . token_to_string ( left ) } #{ mod . token_to_string ( right ) } "
2528 end
29+ def token_to_string ( { :dot , _ , [ left , right ] } , mod ) do
30+ "#{ mod . token_to_string ( left ) } .#{ mod . token_to_string ( right ) } "
31+ end
32+ def token_to_string ( { tag , [ { :type , :operator } | _ ] , [ { :not = t , _ , left } , right ] } , mod ) do
33+ "#{ mod . token_to_string ( left ) } #{ mod . token_to_string ( t ) } #{ mod . token_to_string ( tag ) } #{ mod . token_to_string ( right ) } "
34+ end
2635 def token_to_string ( { tag , [ { :type , :operator } | _ ] , [ left , right ] } , mod ) do
2736 "#{ mod . token_to_string ( left ) } #{ mod . token_to_string ( tag ) } #{ mod . token_to_string ( right ) } "
2837 end
29- def token_to_string ( { :ident , [ { :type , :non_reserved } , { : tag, tag } | _ ] , [ { :paren , _ , _ } = value ] } , mod ) do
38+ def token_to_string ( { :ident , [ { :tag , tag } | _ ] , [ { :paren , _ , _ } = value ] } , mod ) do
3039 "#{ mod . token_to_string ( tag ) } #{ mod . token_to_string ( value ) } "
3140 end
32- def token_to_string ( { :ident , [ { :type , :non_reserved } , { : tag, tag } | _ ] , [ { :numeric , _ , _ } = value ] } , mod ) do
41+ def token_to_string ( { :ident , [ { :tag , tag } | _ ] , [ { :numeric , _ , _ } = value ] } , mod ) do
3342 "#{ mod . token_to_string ( tag ) } #{ mod . token_to_string ( value ) } "
3443 end
35- def token_to_string ( { :ident , [ { :type , :non_reserved } , { : tag, tag } | _ ] , _ } , mod ) do
44+ def token_to_string ( { :ident , [ { :tag , tag } | _ ] , _ } , mod ) do
3645 mod . token_to_string ( tag )
3746 end
38- def token_to_string ( { tag , [ { :type , :reserved } | _ ] , [ { :paren , _ , _ } = value ] } , mod ) when tag not in ~w[ on as in select] a do
47+ def token_to_string ( { tag , [ { :type , :reserved } | _ ] , [ { :paren , _ , _ } = value ] } , mod ) when tag not in ~w[ on in select] a do
3948 "#{ mod . token_to_string ( tag ) } #{ mod . token_to_string ( value ) } "
4049 end
4150 def token_to_string ( { tag , [ { :type , :reserved } | _ ] , [ ] } , mod ) do
@@ -59,12 +68,24 @@ defmodule SQL.Adapters.ANSI do
5968 def token_to_string ( { :comments , _ , value } , _mod ) do
6069 "\\ *#{ value } *\\ "
6170 end
71+ def token_to_string ( { :double_quote , [ { :prefix , prefix } | _ ] , value } , _mod ) do
72+ "#{ prefix } \" #{ value } \" "
73+ end
6274 def token_to_string ( { :double_quote , _ , value } , _mod ) do
6375 "\" #{ value } \" "
6476 end
77+ def token_to_string ( { :quote , [ { :prefix , prefix } | _ ] , value } , _mod ) do
78+ "#{ prefix } '#{ value } '"
79+ end
6580 def token_to_string ( { :quote , _ , value } , _mod ) do
6681 "'#{ value } '"
6782 end
83+ def token_to_string ( { :backtick , [ { :prefix , prefix } | _ ] , value } , _mod ) do
84+ "#{ prefix } `#{ value } `"
85+ end
86+ def token_to_string ( { :backtick , _ , value } , _mod ) do
87+ "`#{ value } `"
88+ end
6889 def token_to_string ( { :paren , _ , value } , mod ) do
6990 "(#{ mod . token_to_string ( value ) } )"
7091 end
@@ -77,9 +98,6 @@ defmodule SQL.Adapters.ANSI do
7798 def token_to_string ( { :comma , _ , value } , mod ) do
7899 "#{ mod . token_to_string ( value ) } ,"
79100 end
80- def token_to_string ( { :dot , _ , [ left , right ] } , mod ) do
81- "#{ mod . token_to_string ( left ) } .#{ mod . token_to_string ( right ) } "
82- end
83101 def token_to_string ( { tag , _ , value } , _mod ) when tag in ~w[ ident numeric] a do
84102 "#{ value } "
85103 end
@@ -92,17 +110,17 @@ defmodule SQL.Adapters.ANSI do
92110 def token_to_string ( value , _mod ) when is_integer ( value ) do
93111 [ value ]
94112 end
95- def token_to_string ( { tag , _ , [ left , right ] } , mod ) when tag in ~w[ like ilike as union except intersect between and or is not in cursor for to] a do
113+ def token_to_string ( { tag , _ , [ left , right ] } , mod ) when tag in ~w[ like ilike union except intersect between and or is not in cursor for to] a do
96114 "#{ mod . token_to_string ( left ) } #{ mod . token_to_string ( tag ) } #{ mod . token_to_string ( right ) } "
97115 end
98- def token_to_string ( { tag , [ { :type , :reserved } | _ ] , values } , mod ) do
99- " #{ mod . token_to_string ( tag ) } #{ mod . token_to_string ( values ) } "
116+ def token_to_string ( { tag , _ , [ ] } , mod ) do
117+ mod . token_to_string ( tag )
100118 end
101- def token_to_string ( { tag , [ { :type , :non_reserved } | _ ] , values } , mod ) when tag != :ident do
119+ def token_to_string ( { tag , [ { :type , :reserved } | _ ] , values = [ _ | _ ] } , mod ) do
102120 "#{ mod . token_to_string ( tag ) } #{ mod . token_to_string ( values ) } "
103121 end
104- def token_to_string ( { tag , _ , [ ] } , mod ) do
105- mod . token_to_string ( tag )
122+ def token_to_string ( { tag , [ { :type , :non_reserved } | _ ] , values = [ _ | _ ] } , mod ) when tag != :ident do
123+ " #{ mod . token_to_string ( tag ) } #{ mod . token_to_string ( values ) } "
106124 end
107125 def token_to_string ( values , mod ) do
108126 values
@@ -136,22 +154,31 @@ defmodule SQL.Adapters.ANSI do
136154 def to_iodata ( { tag , _ , [ left ] } , context , indent ) when tag in ~w[ asc desc isnull notnull] a do
137155 [ context . module . to_iodata ( left , context , indent ) , ?\s | context . module . to_iodata ( tag , context , indent ) ]
138156 end
139- def to_iodata ( { :fun , _ , [ left , right ] } , context , indent ) do
157+ def to_iodata ( { :fn , _ , [ left , right ] } , context , indent ) do
140158 [ context . module . to_iodata ( left , context , indent ) | context . module . to_iodata ( right , context , indent ) ]
141159 end
160+ def to_iodata ( { :dot , _ , [ left , right ] } , context , indent ) do
161+ [ context . module . to_iodata ( left , context , indent ) , ?\. | context . module . to_iodata ( right , context , indent ) ]
162+ end
163+ def to_iodata ( { tag , [ { :type , :operator } | _ ] , [ { :not = t , _ , left } , right ] } , context , indent ) do
164+ [ context . module . to_iodata ( left , context , indent ) , ?\s , context . module . to_iodata ( t , context , indent ) , ?\s , context . module . to_iodata ( tag , context , indent ) , ?\s | context . module . to_iodata ( right , context , indent ) ]
165+ end
142166 def to_iodata ( { tag , [ { :type , :operator } | _ ] , [ left , { :paren , _ , _ } = right ] } , context , indent ) do
143167 [ context . module . to_iodata ( left , context , indent ) , ?\s , context . module . to_iodata ( tag , context , indent ) , ?\s | context . module . to_iodata ( right , context , indent ) ]
144168 end
145169 def to_iodata ( { tag , [ { :type , :operator } | _ ] , [ left , right ] } , context , indent ) do
146170 [ context . module . to_iodata ( left , context , indent ) , ?\s , context . module . to_iodata ( tag , context , indent ) , ?\s | context . module . to_iodata ( right , context , indent ) ]
147171 end
148- def to_iodata ( { :ident , [ { :type , :non_reserved } , { :tag , tag } | _ ] , [ { :paren , _ , _ } = value ] } , context , indent ) do
172+ def to_iodata ( { tag , [ { :type , :operator } | _ ] , [ ] } , context , indent ) do
173+ [ context . module . to_iodata ( tag , context , indent ) ]
174+ end
175+ def to_iodata ( { :ident , [ { :tag , tag } | _ ] , [ { :paren , _ , _ } = value ] } , context , indent ) do
149176 [ context . module . to_iodata ( tag , context , indent ) | context . module . to_iodata ( value , context , indent ) ]
150177 end
151- def to_iodata ( { :ident , [ { :type , :non_reserved } , { : tag, tag } | _ ] , [ { :numeric , _ , _ } = value ] } , context , indent ) do
178+ def to_iodata ( { :ident , [ { :tag , tag } | _ ] , [ { :numeric , _ , _ } = value ] } , context , indent ) do
152179 [ context . module . to_iodata ( tag , context , indent ) , ?\s | context . module . to_iodata ( value , context , indent ) ]
153180 end
154- def to_iodata ( { :ident , [ { :type , :non_reserved } , { : tag, tag } | _ ] , _ } , context , indent ) do
181+ def to_iodata ( { :ident , [ { :tag , tag } | _ ] , _ } , context , indent ) do
155182 context . module . to_iodata ( tag , context , indent )
156183 end
157184 def to_iodata ( { tag , [ { :type , :reserved } | _ ] , [ { :paren , _ , _ } = value ] } , context , indent ) when tag not in ~w[ on in select] a do
@@ -178,15 +205,30 @@ defmodule SQL.Adapters.ANSI do
178205 def to_iodata ( { :comments , _ , value } , _context , _indent ) do
179206 [ ?\\ , ?* , value | [ ?* , ?\\ ] ]
180207 end
208+ def to_iodata ( { :double_quote , [ { :prefix , prefix } | _ ] , value } = node , context , _indent ) do
209+ case node in context . errors do
210+ true -> [ [ prefix , ?" , :red , value | [ :reset , ?" ] ] ]
211+ false -> [ prefix , ?" , value , ?" ]
212+ end
213+ end
181214 def to_iodata ( { :double_quote , _ , value } = node , context , _indent ) do
182215 case node in context . errors do
183216 true -> [ [ ?" , :red , value | [ :reset , ?" ] ] ]
184- false -> value
217+ false -> [ ?" , value , ?" ]
185218 end
186219 end
220+ def to_iodata ( { :quote , [ { :prefix , prefix } | _ ] , value } , _context , _indent ) do
221+ [ prefix , ?' , value | [ ?' ] ]
222+ end
187223 def to_iodata ( { :quote , _ , value } , _context , _indent ) do
188224 [ ?' , value | [ ?' ] ]
189225 end
226+ def to_iodata ( { :backtick , [ { :prefix , prefix } | _ ] , value } , _context , _indent ) do
227+ [ prefix , ?` , value | [ ?` ] ]
228+ end
229+ def to_iodata ( { :backtick , _ , value } , _context , _indent ) do
230+ [ ?` , value | [ ?` ] ]
231+ end
190232 def to_iodata ( { :paren , _ , [ { _ , [ { :type , :reserved } | _ ] , _ } | _ ] = value } , context , indent ) do
191233 [ ?( , ?\n , context . module . to_iodata ( value , context , indent + 1 ) | ?) ]
192234 end
@@ -202,9 +244,6 @@ defmodule SQL.Adapters.ANSI do
202244 def to_iodata ( { :comma , _ , value } , context , indent ) do
203245 [ context . module . to_iodata ( value , context , indent ) , ?, , ?\s ]
204246 end
205- def to_iodata ( { :dot , _ , [ left , right ] } , context , indent ) do
206- [ context . module . to_iodata ( left , context , indent ) , ?\. | context . module . to_iodata ( right , context , indent ) ]
207- end
208247 def to_iodata ( { tag , _ , value } = node , context , _indent ) when tag in ~w[ ident numeric] a do
209248 case node in context . errors do
210249 true -> [ :red , value , :reset ]
@@ -226,10 +265,10 @@ defmodule SQL.Adapters.ANSI do
226265 def to_iodata ( { tag , _ , [ left , right ] } , context , indent ) when tag in ~w[ like ilike union except intersect between and or is not in cursor for to] a do
227266 [ context . module . to_iodata ( left , context , indent ) , ?\s , context . module . to_iodata ( tag , context , indent ) , ?\s | context . module . to_iodata ( right , context , indent ) ]
228267 end
229- def to_iodata ( { tag , [ { :type , :reserved } | _ ] , values } , context , indent ) do
268+ def to_iodata ( { tag , [ { :type , :reserved } | _ ] , values = [ _ | _ ] } , context , indent ) do
230269 [ context . module . to_iodata ( tag , context , indent ) , ?\s | context . module . to_iodata ( values , context , indent ) ]
231270 end
232- def to_iodata ( { tag , [ { :type , :non_reserved } | _ ] , values } , context , indent ) when tag != :ident do
271+ def to_iodata ( { tag , [ { :type , :non_reserved } | _ ] , values = [ _ | _ ] } , context , indent ) when tag != :ident do
233272 [ context . module . to_iodata ( tag , context , indent ) , ?\s | context . module . to_iodata ( values , context , indent ) ]
234273 end
235274 def to_iodata ( { tag , _ , [ ] } , context , indent ) do
0 commit comments