-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfields.rb
More file actions
371 lines (310 loc) · 9.96 KB
/
fields.rb
File metadata and controls
371 lines (310 loc) · 9.96 KB
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
require 'set'
require 'json'
require_relative 'validators.rb'
require_relative 'constraints.rb'
module DBValue
class DBValue
def is_required?
@constraints.include?(Constraints::NotNull.new)
end
end
class DBInt < DBValue
attr_accessor :field_type, :constraints, :name
def initialize(field_type: "", constraints: nil)
@name = "DBInt"
if field_type!="" and not Fields::IntTypes::Types.include?(field_type)
raise StandardError.new(field_type + " is not a valid integer type")
end
if constraints == nil
constraints = Set.new
end
@constraints = constraints
@field_type = field_type
end
def ==(other)
if not other.is_a?(DBInt)
return false
end
if @field_type != other.field_type or other.constraints != @constraints
return false
end
return true
end
def validator
return Validator::All.new(@constraints.to_a.map{|x| x.validator}.concat([Validator::IsInt.new]))
end
def to_s
return @field_type + "INT"
end
alias name to_s
end
class DBChar < DBValue
attr_accessor :max_length, :constraints, :name
def initialize(max_length: 255, constraints: nil)
@name = "DBChar"
if constraints == nil
constraints = Set.new
end
@constraints = constraints
@max_length = max_length
end
def ==(other)
if not other.is_a?(DBChar)
return false
end
if @max_length != other.max_length or other.constraints != @constraints
return false
end
return true
end
def validator
return Validator::All.new(@constraints.to_a.map{|x| x.validator}.concat([Validator::IsString.new, Validator::MaxLength.new(@max_length)]))
end
def to_s
return "VARCHAR(" + @max_length.to_s + ")"
end
alias name to_s
end
class DBText < DBValue
attr_accessor :max_length, :constraints
def initialize(max_length: 255, constraints: nil)
if constraints == nil
constraints = Set.new
end
@constraints = constraints
@max_length = max_length
end
def ==(other)
if not other.is_a?(DBText)
return false
end
if @max_length != other.max_length or other.constraints != @constraints
return false
end
return true
end
def validator
arr = [Validator::IsString.new]
if @max_length != nil
arr.append(Validator::MaxLength.new(@max_length))
end
return Validator::All.new(@constraints.to_a.map{|x| x.validator}.concat(arr))
end
def to_s
if @max_length == nil
return "TEXT"
end
return "TEXT(" + @max_length.to_s + ")"
end
alias name to_s
end
end
module Fields
attr_accessor :fieldMap
module IntTypes
Tiny = "TINY"
Small = "SMALL"
Big = "BIG"
Types = Set.new([Tiny, Small, Big])
end
class IntField
attr_accessor :field_type, :_constraints
def initialize(field_type: "", constraints: nil)
if field_type!="" and not Fields::IntTypes::Types.include?(field_type)
raise StandardError.new(field_type + " is not a valid integer type")
end
if constraints == nil
constraints = Set.new
end
@_constraints = constraints
@field_type = field_type
end
def ==(other)
if not other.is_a?(IntField)
return false
end
if @field_type != other.field_type
return false
end
return true
end
def to_json(*a)
{ 'json_class' => self.class.name, 'data' => {"field_type" => @field_type} }.to_json(*a)
end
def self.json_create(o)
data = o['data']
new(field_type: data["field_type"])
end
def to_sql(ctx, tb_name, field, platform)
if platform == Platforms::SQLITE or platform == Platforms::POSTGRES
return self.to_s
else
unsupported_platform(platform)
end
end
def defaults
Set.new [Constraints::NotNull.new]
end
def get_value(constraints)
return DBValue::DBInt.new(field_type: @field_type, constraints: constraints)
end
def to_s
return @field_type + "INT"
end
end
class CharField
attr_accessor :max_length, :_constraints
def initialize(max_length: 255, constraints: nil)
if constraints == nil
constraints = Set.new
end
@_constraints = constraints
@max_length = max_length
end
def ==(other)
if not other.is_a?(CharField)
return false
end
if @max_length != other.max_length
return false
end
return true
end
def to_json(*a)
{ 'json_class' => self.class.name, 'data' => {"max_length" => @max_length} }.to_json(*a)
end
def self.json_create(o)
data = o['data']
new(max_length: data["max_length"])
end
def to_sql(ctx, tb_name, field, platform)
if platform == Platforms::SQLITE or platform == Platforms::POSTGRES
return self.to_s
else
unsupported_platform(platform)
end
end
def get_value(constraints)
return DBValue::DBChar.new(max_length: @max_length, constraints: constraints)
end
def defaults
Set.new [Constraints::NotNull.new]
end
def to_s
return "VARCHAR(" + @max_length.to_s + ")"
end
end
class ForeignKeyField
attr_accessor :reference, :back_ref_internal, :_constraints
def initialize(reference: nil, back_ref: nil, constraints: nil)
if constraints == nil
constraints = Set.new
end
if reference == nil
raise ArgumentError.new("Reference is required")
end
self.back_ref = back_ref
@_constraints = constraints
@reference = reference
end
def ==(other)
if not other.is_a?(ForeignKeyField)
return false
end
if @reference != other.reference
return false
end
return true
end
def eql?(other)
return self == other
end
def hash
return "ForeignKeyField".hash & @reference.hash
end
def back_ref
return @back_ref_internal
end
def back_ref=(table_name)
if table_name == nil
return
end
if @back_ref_internal != nil
raise ArgumentError.new "Back reference is already initialized"
end
@back_ref_internal = table_name.downcase + "__id"
end
def to_json(*a)
{ 'json_class' => self.class.name, 'data' => {"reference" => @reference} }.to_json(*a)
end
def self.json_create(o)
data = o["data"]
new(reference: data["reference"])
end
def to_sql(ctx, tb_name, field, platform)
if platform == Platforms::SQLITE or platform == Platforms::POSTGRES
ctx.add_end("ALTER TABLE #{tb_name}_ ADD CONSTRAINT \"#{tb_name}__#{@reference}_#{field}__fk\" FOREIGN KEY (#{field}) REFERENCES #{@reference}_(id) ON DELETE CASCADE")
return "BIGINT"
else
unsupported_platform(platform)
end
end
def defaults
Set.new [Constraints::Nullable.new]
end
def to_s
return "Reference(" + @reference.to_s + ", cascade)"
end
end
class TextField
attr_accessor :max_length, :_constraints
def initialize(max_length: nil, constraints: nil)
if constraints == nil
constraints = Set.new
end
@_constraints = constraints
@max_length = max_length
end
def ==(other)
if not other.is_a?(TextField)
return false
end
if @max_length != other.max_length
return false
end
return true
end
def get_value(constraints)
return DBValue::DBText.new(max_length: @max_length, constraints: constraints)
end
def to_s
if @max_length == nil
return "TEXT"
end
return "TEXT(" + @max_length.to_s + ")"
end
def self.json_create(o)
data = o['data']
new(max_length: data["max_length"])
end
def defaults
Set.new [Constraints::NotNull.new]
end
def to_sql(ctx, tb_name, field, platform)
if platform == Platforms::SQLITE or platform == Platforms::POSTGRES
return self.to_s
else
unsupported_platform(platform)
end
end
def to_json(*a)
{ 'json_class' => self.class.name, 'data' => {"max_length" => @max_length} }.to_json(*a)
end
end
@fieldMap = {
"Fields::IntField" => IntField,
"Fields::CharField" => CharField,
"Fields::TextField" => TextField,
"Fields::ForeignKeyField" => ForeignKeyField,
}
end