-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.cr
302 lines (261 loc) · 7.4 KB
/
adapter.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
module Rome
abstract struct Adapter
private getter builder : Query::Builder
def initialize(@builder)
end
# abstract def self.quote(name : Symbol | String, io : IO)
def quote(name : Symbol | String, io : IO)
self.class.quote(name, io)
end
def insert(attributes : Hash) : Nil
sql, args = insert_sql(attributes)
rs = Rome.connection &.exec(sql, args: args)
yield rs.last_insert_id
end
def select_one
return if @builder.none?
sql, args = select_sql
Rome.connection &.query_one?(sql, args: args) { |rs| yield rs }
end
def select_all(&block : DB::ResultSet -> U) : Array(U) forall U
if @builder.none?
Array(U).new(0)
else
sql, args = select_sql
Rome.connection &.query_all(sql, args: args) { |rs| yield rs }
end
end
def select_each : Nil
return if @builder.none?
sql, args = select_sql
Rome.connection &.query_each(sql, args: args) { |rs| yield rs }
end
def scalar
sql, args = select_sql
Rome.connection &.scalar(sql, args: args)
end
def update(attributes : Hash | NamedTuple) : Nil
return if @builder.none?
sql, args = update_sql(attributes)
Rome.connection &.exec(sql, args: args)
end
def delete : Nil
return if @builder.none?
sql, args = delete_sql
Rome.connection &.exec(sql, args: args)
end
def to_sql : String
sql, _ = select_sql
sql
end
protected def insert_sql(attributes) : {String, Array(Value)}
args = [] of Value
sql = String.build do |str|
build_insert(attributes, str, args)
end
{sql, args}
end
protected def select_sql
args = [] of Value
sql = String.build do |str|
build_select(str)
build_where(str, args)
build_order_by(str)
build_limit(str)
end
{sql, args}
end
protected def update_sql(attributes) : {String, Array(Value)}
args = [] of Value
sql = String.build do |str|
build_update(attributes, str, args)
build_where(str, args)
end
{sql, args}
end
protected def delete_sql : {String, Array(Value)}
args = [] of Value
sql = String.build do |str|
build_delete(str)
build_where(str, args)
end
{sql, args}
end
protected def build_select(io) : Nil
io << "SELECT "
io << "DISTINCT " if builder.distinct?
if selects = builder.selects?
selects.each_with_index do |column_name, index|
io << ", " unless index == 0
case column_name
when Symbol
quote(column_name, io)
when String
io << column_name
end
end
else
io << '*'
end
io << " FROM "
quote(builder.table_name, io)
end
private def build_insert(attributes : Hash, io, args)
io << "INSERT INTO "
quote(builder.table_name, io)
if attributes.empty?
build_insert_default_values(io)
else
io << " ("
attributes.each_with_index do |(column_name, _), index|
io << ", " unless index == 0
quote(column_name, io)
end
io << ") VALUES ("
attributes.each_with_index do |(_, value), index|
args << value
io << ", " unless index == 0
io << '?'
end
io << ')'
end
end
private def build_insert(attributes : NamedTuple, io, args)
io << "INSERT INTO "
quote(builder.table_name, io)
if attributes.empty?
build_insert_default_values(io)
else
io << " ("
attributes.each_with_index do |column_name, _, index|
io << ", " unless index == 0
quote(column_name, io)
end
io << ") VALUES ("
attributes.each_with_index do |_, value, index|
args << value
io << ", " unless index == 0
io << '?'
end
io << ')'
end
end
private def build_insert_default_values(io)
io << " DEFAULT VALUES"
end
private def build_update(attributes : Hash, io, args)
io << "UPDATE "
quote(builder.table_name, io)
io << " SET "
attributes.each_with_index do |(column_name, value), index|
args << value
io << ", " unless index == 0
quote(column_name, io)
io << " = ?"
end
end
private def build_update(attributes : NamedTuple, io, args)
io << "UPDATE "
quote(builder.table_name, io)
io << " SET "
attributes.each_with_index do |column_name, value, index|
args << value
io << ", " unless index == 0
quote(column_name, io)
io << " = ?"
end
end
private def build_delete(io : IO) : Nil
io << "DELETE FROM "
quote(builder.table_name, io)
end
protected def build_where(io, args) : Nil
return unless conditions = builder.conditions?
io << " WHERE "
conditions.each_with_index do |condition, index|
io << " AND " unless index == 0
case condition
when Query::Builder::Condition
case value = condition.value
when Array(Value)
quote(condition.column_name, io)
if condition.not
io << " NOT IN ("
else
io << " IN ("
end
value.size.times do |index|
io << ", " unless index == 0
io << '?'
end
io << ')'
args.concat(value)
when nil
quote(condition.column_name, io)
if condition.not
io << " IS NOT NULL"
else
io << " IS NULL"
end
when Regex
build_where_regex(condition, io, args)
else
quote(condition.column_name, io)
args << value
if condition.not
io << " <> ?"
else
io << " = ?"
end
end
when Query::Builder::RawCondition
io << "NOT " if condition.not
io << '(' << condition.raw << ')'
if values = condition.values
args.concat(values)
end
end
end
end
def build_where_regex(condition, io, args)
args << condition.value.as(Regex).source
io << "NOT (" if condition.not
quote(condition.column_name, io)
io << " REGEXP ?"
io << ')' if condition.not
end
protected def build_order_by(io) : Nil
return unless orders = builder.orders?
io << " ORDER BY "
orders.each_with_index do |order, index|
io << ", " unless index == 0
case order
when {Symbol, Symbol}
column_name, direction = order.as({Symbol, Symbol})
quote(column_name, io)
case direction
when :asc then io << " ASC"
when :desc then io << " DESC"
end
when String
io << order
end
end
end
protected def build_limit(io) : Nil
if limit = builder.limit?
io << " LIMIT " << limit
end
if offset = builder.offset?
io << " OFFSET " << offset
end
end
end
@@adapters = {} of String => Adapter.class
def self.adapters : Hash(String, Adapter.class)
@@adapters
end
def self.register_adapter(name : String, adapter_class : Adapter.class) : Nil
@@adapters[name] = adapter_class
end
end