-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.rb
396 lines (362 loc) · 11.5 KB
/
model.rb
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
require_relative 'inferModels.rb'
require_relative 'fields.rb'
require_relative 'utils.rb'
require_relative 'query.rb'
Absolute = Struct.new("Absolute", :value) do
def is_absolute?
return true
end
def to_sql(table_name)
self.value.to_s
end
end
Relative = Struct.new("Relative", :value) do
def is_absolute?
return false
end
def to_sql(table_name)
return "(select currval('#{table_name}__id_seq')) - #{self.value.to_s}"
end
end
class Collection
attr_accessor :collection_internal, :model, :query
def initialize(collection, model)
self.collection = collection
@model = model
end
def banish_collection
@collection_internal = nil
end
def collection=(val)
@collection_internal = val
end
def collection
if @collection_internal == nil and @query != nil
conn = DBConn.getConnection
@collection_internal = conn.exec @query.to_sql
end
return @collection_internal
end
def where(obj)
self.banish_collection
@query.where(obj)
return self
end
def limit(n)
self.banish_collection
@query.limit(n)
return self
end
def order_by(key, desc=true)
self.banish_collection
@query.order_by(key, desc)
return self
end
def self.from_query(query, model)
this = new(nil, model)
this.query = query
return this
end
def instantiate(obj)
instance = @model.instantiate
instance.dangerously_set_field(:id, obj["id"])
for k, v in obj
model_key = k.to_s[0...-4]
model_key_sym = model_key.to_sym
if k.to_s.end_with?("__id") and @model.schema.relations[@model.name].has_key?(model_key_sym)
instance.dangerously_set_field(model_key_sym, v)
else
instance.dangerously_set_field(k, v)
end
end
instance.saved = true
return instance
end
def each
for obj in self.collection
yield self.instantiate(obj)
end
end
def first
for obj in self
return obj
end
end
end
class Record
attr_accessor :name, :keys, :obj, :model, :readonly_keys, :autoincrement_keys, :singulars, :saved
def initialize(name, model, keys, readonly_keys, autoincrement_keys, singulars)
@name = name
@singulars = singulars
@autoincrement_keys = autoincrement_keys
@readonly_keys = readonly_keys
@model = model
@keys = Set.new
@obj = {}
@saved = false
for k in keys
@keys.add(k)
end
end
def [](index)
if @saved and ((not @obj.has_key?(index)) or @obj[index].is_a?(Integer)) and (not @autoincrement_keys.include?(index))
if not @model.model.has_key?(index)
raise ArgumentError.new "No key named '#{index}' is present in #{@model.name}"
end
modelObj = @model.model[index]
tableName = modelObj.name
new_model = Model.new(tableName, @model.schema)
if @singulars.include?(index)
id = @obj[index.to_sym]
query = Query.new(new_model).where({id: id}).limit(1)
record = Collection.from_query(query, new_model).first
@obj[index] = record
return record
else
back_ref = new_model.back_refs[index.to_s[0...-1] + "__id"]
id = @obj[:id]
dict = {}
dict[back_ref.to_sym] = id
query = Query.new(new_model).where(dict)
collection = Collection.from_query(query, new_model)
@obj[index] = collection
return collection
end
end
if not @obj.has_key?(index)
raise ArgumentError.new "No field named '#{index}' yet exists"
end
return @obj[index]
end
def []=(index, val)
if @readonly_keys.include?(index)
raise ArgumentError.new "'#{index}' is a read-only field record"
end
if not @keys.include?(index)
raise ArgumentError.new "'#{index}' not in record"
end
@obj[index] = val
end
def validate
validated = {}.compare_by_identity
self.validate_single(validated)
end
def validate_single(validated)
for k in @keys
if (not @obj.has_key?(k)) and @model.model[k].is_a? DBValue::DBValue and @model.model[k].is_required? and (not readonly_keys.include?(k))
raise ArgumentError.new "'#{k}' is a required value"
end
end
for k, v in @obj
dbObj = @model.model[k]
if dbObj.is_a? DBValue::DBValue then
if not dbObj.validator.validate(v)
v_str = v == nil ? "nil" : v.class.name
raise ArgumentError.new "#{v_str} is not a valid #{dbObj.name}"
end
else
if v == nil
next
end
if v.is_a? Integer
next
end
if v.is_a?(Record) and v.name == dbObj.name
validated[v] = v
if validated.include?(v)
next
end
v.validate_single(validated)
next
end
raise ArgumentError.new "#{v} is not a valid object of #{dbObj.class.name}"
end
end
end
def to_s
strs = []
for k, v in @obj
if v == nil
strs.append "#{k}: nil"
elsif v.is_a? Record
strs.append "#{k}: #{v.name}"
else
strs.append "#{k}: #{v}"
end
end
return @name + "{" + strs.join(", ") + "}"
end
def mark_saved
if @saved
return
end
@saved = true
for k, field in @obj
if field.is_a? Record
field.mark_saved
end
end
end
def dangerously_set_field(key, val)
@obj[key.to_sym] = val
end
def save
self.validate
inserts, updates = self.to_sql
conn = DBConn.getConnection
conn.exec "BEGIN;"
for tup in inserts
obj, returns, insert = tup
res = conn.exec(insert + ";")
for row in res
break
end
for return_ in returns
value = row[return_.to_s.downcase]
obj.dangerously_set_field(return_, value)
end
end
for update in updates
conn.exec(update + ";")
end
conn.exec "COMMIT;"
self.mark_saved
end
def to_sql
generated = {}.compare_by_identity
statements = []
deps = {}
offsets = {}
updates = []
inserts = self.to_sql_singleton({}, updates, offsets, deps, statements, generated)
complete_insertion_updates = self.to_sql_update(offsets, deps, generated)
updates += complete_insertion_updates
return statements, updates
end
def to_sql_update(offsets, deps, generated)
updates = []
for table, dep in deps
sets = Set.new
for k, model in dep
sets.add(k.to_s + "__id = #{offsets[model].to_sql(model.name)}")
end
offset = offsets[table]
if sets.length > 0
updates.append "UPDATE #{table.name}_ SET #{sets.to_a.join(", ")} WHERE id = #{offset.to_sql(table.name)}"
end
end
return updates
end
def to_sql_singleton(inserts, updates, offsets, deps, statements, generated)
if generated.has_key?(self)
return
end
generated[self] = self
fields = {}
for k, v in @obj
if v.is_a? Record
fields[k] = nil
else
fields[k] = v
end
end
names_arr = []
vals_arr = []
returns_arr = @autoincrement_keys.to_a
to_generates = {}
for key, field in @obj
if @singulars.include?(key)
names_arr.append((key.to_s + "__id").to_sym)
if field.is_a? Record
to_generates[key] = field
vals_arr.append("NULL")
else
vals_arr.append("#{field}")
end
else
names_arr.append(key.to_s)
if field.is_a? String
val = "'#{fields[key].to_s}'"
else
val = fields[key].to_s
end
vals_arr.append(val)
end
end
if @saved
assignments = names_arr.zip(vals_arr).map{|xs| xs[0].to_s + " = " + xs[1].to_s}.join(", ")
id = @obj[:id]
updates.append("UPDATE #{@name}_ SET #{assignments} WHERE id = #{id}")
offsets[self] = Absolute.new id
else
names = names_arr.join(", ")
vals = vals_arr.join(", ")
returns = returns_arr.join(", ")
statements.append([self, returns_arr, "INSERT INTO #{@name}_ (#{names}) VALUES (#{vals}) RETURNING #{returns}"])
if inserts.has_key? self.name
inserts[self.name] += 1
else
inserts[self.name] = 0
end
offsets[self] = Relative.new inserts[self.name]
end
for k, to_generate in to_generates
to_generate.to_sql_singleton(inserts, updates, offsets, deps, statements, generated)
end
deps[self] = to_generates
return inserts
end
end
class Model
attr_accessor :model, :name, :obj, :readonly_fields, :singulars, :foreign_keys, :schema, :back_refs
def initialize(name, schema)
@name = name
@readonly_fields = Set.new [:id]
@auto_increment_fields = Set.new [:id]
@foreign_keys = Set.new
@model = {}
@schema = schema
schema_dict = schema.to_dict
graph = schema.graph
relations = schema.relations
vertices = relations[name]
@singulars = Set.new
@back_refs = {}
for k, v in schema.graph[name][1]
@back_refs[v.back_ref] = k
end
for k, v in vertices
@foreign_keys.add(k)
if v.is_singular
@singulars.add(k)
@model[k] = schema_dict[v.reference]
else
@model[(k.to_s + "s").to_sym] = schema_dict[v.reference]
end
end
for k, feild in schema_dict[self.name].table.obj
if not feild.is_a? Fields::ForeignKeyField
constraints = schema_dict[self.name].table.constraints
@model[k.to_sym] = feild.get_value(constraints[k])
if constraints[k].include?(Constraints::AutoIncrement.new)
@readonly_fields.add(k.to_sym)
@auto_increment_fields.add(k.to_sym)
end
end
end
end
def instantiate
set = Set.new
for k, v in @model
set.add(k)
end
return Record.new(self.name, self, set, @readonly_fields, @auto_increment_fields, @singulars)
end
def to_s
strs = []
for k, v in @model
strs.append "#{k}: #{v.name}"
end
return @name + "{" + strs.join(", ") + "}"
end
end