baza 0.0.14 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +58 -13
  3. data/VERSION +1 -1
  4. data/baza.gemspec +15 -3
  5. data/include/db.rb +871 -865
  6. data/include/drivers/mysql/mysql.rb +104 -297
  7. data/include/drivers/mysql/mysql_column.rb +133 -0
  8. data/include/drivers/mysql/mysql_columns.rb +4 -127
  9. data/include/drivers/mysql/mysql_index.rb +76 -0
  10. data/include/drivers/mysql/mysql_indexes.rb +0 -73
  11. data/include/drivers/mysql/mysql_result.rb +42 -0
  12. data/include/drivers/mysql/mysql_result_java.rb +61 -0
  13. data/include/drivers/mysql/mysql_result_mysql2.rb +26 -0
  14. data/include/drivers/mysql/mysql_result_unbuffered.rb +72 -0
  15. data/include/drivers/mysql/mysql_sqlspecs.rb +1 -1
  16. data/include/drivers/mysql/mysql_table.rb +361 -0
  17. data/include/drivers/mysql/mysql_tables.rb +23 -381
  18. data/include/drivers/sqlite3/libknjdb_java_sqlite3.rb +17 -22
  19. data/include/drivers/sqlite3/libknjdb_sqlite3_ironruby.rb +13 -13
  20. data/include/drivers/sqlite3/sqlite3.rb +39 -105
  21. data/include/drivers/sqlite3/sqlite3_column.rb +146 -0
  22. data/include/drivers/sqlite3/sqlite3_columns.rb +17 -149
  23. data/include/drivers/sqlite3/sqlite3_index.rb +55 -0
  24. data/include/drivers/sqlite3/sqlite3_indexes.rb +0 -52
  25. data/include/drivers/sqlite3/sqlite3_result.rb +35 -0
  26. data/include/drivers/sqlite3/sqlite3_result_java.rb +39 -0
  27. data/include/drivers/sqlite3/sqlite3_table.rb +399 -0
  28. data/include/drivers/sqlite3/sqlite3_tables.rb +7 -403
  29. data/include/idquery.rb +19 -19
  30. data/include/model.rb +139 -139
  31. data/include/model_handler_sqlhelper.rb +74 -74
  32. data/spec/support/driver_columns_collection.rb +17 -0
  33. metadata +14 -2
@@ -2,39 +2,39 @@ class Baza::ModelHandler
2
2
  #This method helps build SQL from Objects-instances list-method. It should not be called directly but only through Objects.list.
3
3
  def sqlhelper(list_args, args_def)
4
4
  args = args_def
5
-
5
+
6
6
  if args[:db]
7
7
  db = args[:db]
8
8
  else
9
9
  db = @args[:db]
10
10
  end
11
-
11
+
12
12
  if args[:table]
13
13
  table_def = "`#{db.esc_table(args[:table])}`."
14
14
  else
15
15
  table_def = ""
16
16
  end
17
-
17
+
18
18
  sql_joins = ""
19
19
  sql_where = ""
20
20
  sql_order = ""
21
21
  sql_limit = ""
22
22
  sql_groupby = ""
23
-
23
+
24
24
  do_joins = {}
25
-
25
+
26
26
  limit_from = nil
27
27
  limit_to = nil
28
-
28
+
29
29
  if list_args.key?("orderby")
30
30
  orders = []
31
31
  orderstr = list_args["orderby"]
32
32
  list_args["orderby"] = [list_args["orderby"]] if list_args["orderby"].is_a?(Hash)
33
-
33
+
34
34
  if list_args["orderby"].is_a?(String)
35
35
  found = false
36
36
  found = true if args[:cols].key?(orderstr)
37
-
37
+
38
38
  if found
39
39
  sql_order << " ORDER BY "
40
40
  ordermode = " ASC"
@@ -45,28 +45,28 @@ class Baza::ModelHandler
45
45
  ordermode = " ASC"
46
46
  raise "Unknown ordermode: #{list_args["ordermode"]}"
47
47
  end
48
-
48
+
49
49
  list_args.delete("ordermode")
50
50
  end
51
-
51
+
52
52
  sql_order << "#{table_def}`#{db.esc_col(list_args["orderby"])}`#{ordermode}"
53
53
  list_args.delete("orderby")
54
54
  end
55
55
  elsif list_args["orderby"].is_a?(Array)
56
56
  sql_order << " ORDER BY "
57
-
57
+
58
58
  list_args["orderby"].each do |val|
59
59
  ordermode = nil
60
60
  orderstr = nil
61
61
  found = false
62
-
62
+
63
63
  if val.is_a?(Array)
64
64
  if val[1] == "asc"
65
65
  ordermode = " ASC"
66
66
  elsif val[1] == "desc"
67
67
  ordermode = " DESC"
68
68
  end
69
-
69
+
70
70
  if val[0].is_a?(Array)
71
71
  if args[:joined_tables]
72
72
  args[:joined_tables].each do |table_name, table_data|
@@ -77,7 +77,7 @@ class Baza::ModelHandler
77
77
  break
78
78
  end
79
79
  end
80
-
80
+
81
81
  raise "Could not find joined table for ordering: '#{val[0][0]}'." if !found
82
82
  else
83
83
  orderstr = val[0]
@@ -90,15 +90,15 @@ class Baza::ModelHandler
90
90
  found = true
91
91
  elsif val.is_a?(Hash) and val[:type] == :case
92
92
  caseorder = " CASE"
93
-
93
+
94
94
  val[:case].each do |key, caseval|
95
95
  col = key.first
96
96
  isval = key.last
97
97
  col_str = nil
98
-
98
+
99
99
  if col.is_a?(Array)
100
100
  raise "No joined tables for '#{args[:table]}'." if !args[:joined_tables]
101
-
101
+
102
102
  found = false
103
103
  args[:joined_tables].each do |table_name, table_data|
104
104
  if table_name == col.first
@@ -108,7 +108,7 @@ class Baza::ModelHandler
108
108
  break
109
109
  end
110
110
  end
111
-
111
+
112
112
  raise "No such joined table on '#{args[:table]}': '#{col.first}' (#{col.first.class.name}) with the following joined table:\n#{Php4r.print_r(args[:joined_tables], true)}" if !found
113
113
  elsif col.is_a?(String) or col.is_a?(Symbol)
114
114
  col_str = "#{table_def}`#{col}`"
@@ -116,26 +116,26 @@ class Baza::ModelHandler
116
116
  else
117
117
  raise "Unknown type for case-ordering: '#{col.class.name}'."
118
118
  end
119
-
119
+
120
120
  raise "'colstr' was not set." if !col_str
121
121
  caseorder << " WHEN #{col_str} = '#{db.esc(isval)}' THEN '#{db.esc(caseval)}'"
122
122
  end
123
-
123
+
124
124
  if val[:else]
125
125
  caseorder << " ELSE '#{db.esc(val[:else])}'"
126
126
  end
127
-
127
+
128
128
  caseorder << " END"
129
129
  orders << caseorder
130
130
  elsif val.is_a?(Hash)
131
131
  raise "No joined tables." if !args.key?(:joined_tables)
132
-
132
+
133
133
  if val[:mode] == "asc"
134
134
  ordermode = " ASC"
135
135
  elsif val[:mode] == "desc"
136
136
  ordermode = " DESC"
137
137
  end
138
-
138
+
139
139
  if args[:joined_tables]
140
140
  args[:joined_tables].each do |table_name, table_data|
141
141
  if table_data[:parent_table]
@@ -145,10 +145,10 @@ class Baza::ModelHandler
145
145
  else
146
146
  table_name_real = @args[:module].const_get(table_name).classname
147
147
  end
148
-
148
+
149
149
  if table_name.to_s == val[:table].to_s
150
150
  do_joins[table_name] = true
151
-
151
+
152
152
  if val[:sql]
153
153
  orders << val[:sql]
154
154
  elsif val[:col]
@@ -156,7 +156,7 @@ class Baza::ModelHandler
156
156
  else
157
157
  raise "Couldnt figure out how to order based on keys: '#{val.keys.sort}'."
158
158
  end
159
-
159
+
160
160
  found = true
161
161
  break
162
162
  end
@@ -165,26 +165,26 @@ class Baza::ModelHandler
165
165
  else
166
166
  raise "Unknown object: #{val.class.name}"
167
167
  end
168
-
168
+
169
169
  found = true if args[:cols].key?(orderstr)
170
-
170
+
171
171
  if !found
172
172
  raise "Column not found for ordering: #{orderstr}."
173
173
  end
174
-
174
+
175
175
  orders << "#{table_def}`#{db.esc_col(orderstr)}`#{ordermode}" if orderstr
176
176
  end
177
-
177
+
178
178
  sql_order << orders.join(", ")
179
179
  list_args.delete("orderby")
180
180
  else
181
181
  raise "Unknown orderby object: #{list_args["orderby"].class.name}."
182
182
  end
183
183
  end
184
-
184
+
185
185
  list_args.each do |realkey, val|
186
186
  found = false
187
-
187
+
188
188
  if realkey.is_a?(Array)
189
189
  if !args[:joins_skip]
190
190
  datarow_obj = self.datarow_obj_from_args(args_def, list_args, realkey[0])
@@ -194,7 +194,7 @@ class Baza::ModelHandler
194
194
  datarow_obj = @args[:module].const_get(realkey[0])
195
195
  args = args_def
196
196
  end
197
-
197
+
198
198
  table_sym = realkey[0].to_sym
199
199
  do_joins[table_sym] = true
200
200
  list_table_name_real = table_sym
@@ -205,10 +205,10 @@ class Baza::ModelHandler
205
205
  args = args_def
206
206
  key = realkey
207
207
  end
208
-
208
+
209
209
  if args.key?(:cols_bools) and args[:cols_bools].index(key) != nil
210
210
  val_s = val.to_s
211
-
211
+
212
212
  if val_s == "1" or val_s == "true"
213
213
  realval = "1"
214
214
  elsif val_s == "0" or val_s == "false"
@@ -216,7 +216,7 @@ class Baza::ModelHandler
216
216
  else
217
217
  raise "Could not make real value out of class: #{val.class.name} => #{val}."
218
218
  end
219
-
219
+
220
220
  sql_where << " AND #{table}`#{db.esc_col(key)}` = '#{db.esc(realval)}'"
221
221
  found = true
222
222
  elsif args[:cols].key?(key.to_s)
@@ -246,7 +246,7 @@ class Baza::ModelHandler
246
246
  else
247
247
  sql_where << " AND #{table}`#{db.esc_col(key)}` = '#{db.esc(val)}'"
248
248
  end
249
-
249
+
250
250
  found = true
251
251
  elsif key.to_s == "limit_from"
252
252
  limit_from = val.to_i
@@ -270,7 +270,7 @@ class Baza::ModelHandler
270
270
  else
271
271
  sql_where << " AND #{table}`#{db.esc_col(key.to_s + "_id")}` = '#{db.esc(val.id)}'"
272
272
  end
273
-
273
+
274
274
  found = true
275
275
  elsif match = key.match(/^([A-z_\d]+)_(search|has)$/) and args[:cols].key?(match[1]) != nil
276
276
  if match[2] == "search"
@@ -284,7 +284,7 @@ class Baza::ModelHandler
284
284
  sql_where << " AND #{table}`#{db.esc_col(match[1])}` = ''"
285
285
  end
286
286
  end
287
-
287
+
288
288
  found = true
289
289
  elsif match = key.match(/^([A-z_\d]+)_(not|lower)$/) and args[:cols].key?(match[1])
290
290
  if match[2] == "not"
@@ -310,27 +310,27 @@ class Baza::ModelHandler
310
310
  else
311
311
  raise "Unknown mode: '#{match[2]}'."
312
312
  end
313
-
313
+
314
314
  found = true
315
315
  elsif args.key?(:cols_date) and match = key.match(/^(.+)_(day|week|month|year|from|to|below|above)(|_(not))$/) and args[:cols_date].index(match[1]) != nil
316
316
  not_v = match[4]
317
317
  val = Datet.in(val) if val.is_a?(Time)
318
-
318
+
319
319
  if match[2] == "day"
320
320
  if val.is_a?(Array)
321
321
  sql_where << " AND ("
322
322
  first = true
323
-
323
+
324
324
  val.each do |realval|
325
325
  if first
326
326
  first = false
327
327
  else
328
328
  sql_where << " OR "
329
329
  end
330
-
330
+
331
331
  sql_where << "#{db.sqlspecs.strftime("%d %m %Y", "#{table}`#{db.esc_col(match[1])}`")} #{self.not(not_v, "!")}= #{db.sqlspecs.strftime("%d %m %Y", "'#{db.esc(realval.dbstr)}'")}"
332
332
  end
333
-
333
+
334
334
  sql_where << ")"
335
335
  else
336
336
  sql_where << " AND #{db.sqlspecs.strftime("%d %m %Y", "#{table}`#{db.esc_col(match[1])}`")} #{self.not(not_v, "!")}= #{db.sqlspecs.strftime("%d %m %Y", "'#{db.esc(val.dbstr)}'")}"
@@ -348,7 +348,7 @@ class Baza::ModelHandler
348
348
  else
349
349
  raise "Unknown date-key: #{match[2]}."
350
350
  end
351
-
351
+
352
352
  found = true
353
353
  elsif args.key?(:cols_num) and match = key.match(/^(.+)_(from|to|above|below|numeric)$/) and args[:cols_num].index(match[1]) != nil
354
354
  if match[2] == "from"
@@ -362,7 +362,7 @@ class Baza::ModelHandler
362
362
  else
363
363
  raise "Unknown method of treating cols-num-argument: #{match[2]}."
364
364
  end
365
-
365
+
366
366
  found = true
367
367
  elsif match = key.match(/^(.+)_lookup$/) and args[:cols].key?("#{match[1]}_id") and args[:cols].key?("#{match[1]}_class")
368
368
  sql_where << " AND #{table}`#{db.esc_col("#{match[1]}_class")}` = '#{db.esc(val.table)}'"
@@ -370,7 +370,7 @@ class Baza::ModelHandler
370
370
  found = true
371
371
  elsif realkey == "groupby"
372
372
  found = true
373
-
373
+
374
374
  if val.is_a?(Array)
375
375
  val.each do |col_name|
376
376
  raise "Column '#{val}' not found on table '#{table}'." if !args[:cols].key?(col_name)
@@ -384,19 +384,19 @@ class Baza::ModelHandler
384
384
  raise "Unknown class given for 'groupby': '#{val.class.name}'."
385
385
  end
386
386
  end
387
-
387
+
388
388
  list_args.delete(realkey) if found
389
389
  end
390
-
390
+
391
391
  args = args_def
392
-
392
+
393
393
  if !args[:joins_skip]
394
394
  raise "No joins defined on '#{args[:table]}' for: '#{args[:table]}'." if !do_joins.empty? and !args[:joined_tables]
395
-
395
+
396
396
  do_joins.each do |table_name, temp_val|
397
397
  raise "No join defined on table '#{args[:table]}' for table '#{table_name}'." if !args[:joined_tables].key?(table_name)
398
398
  table_data = args[:joined_tables][table_name]
399
-
399
+
400
400
  if table_data.key?(:parent_table)
401
401
  join_table_name_real = table_name
402
402
  sql_joins << " LEFT JOIN `#{table_data[:parent_table]}` AS `#{table_name}` ON 1=1"
@@ -405,50 +405,50 @@ class Baza::ModelHandler
405
405
  join_table_name_real = const.classname
406
406
  sql_joins << " LEFT JOIN `#{const.table}` AS `#{const.classname}` ON 1=1"
407
407
  end
408
-
408
+
409
409
  if table_data[:ob]
410
410
  ob = table_data[:ob]
411
411
  else
412
412
  ob = self
413
413
  end
414
-
414
+
415
415
  class_name = args[:table].to_sym
416
-
416
+
417
417
  if table_data[:datarow]
418
418
  datarow = self.datarow_from_datarow_argument(table_data[:datarow])
419
419
  else
420
420
  self.requireclass(class_name) if @objects.key?(class_name)
421
421
  datarow = @args[:module].const_get(class_name)
422
422
  end
423
-
423
+
424
424
  if !datarow.columns_sqlhelper_args
425
425
  ob.requireclass(datarow.table.to_sym)
426
426
  raise "No SQL-helper-args on class '#{datarow.table}' ???" if !datarow.columns_sqlhelper_args
427
427
  end
428
-
428
+
429
429
  newargs = datarow.columns_sqlhelper_args.clone
430
430
  newargs[:table] = join_table_name_real
431
431
  newargs[:joins_skip] = true
432
-
432
+
433
433
  #Clone the where-arguments and run them against another sqlhelper to sub-join.
434
434
  join_args = table_data[:where].clone
435
435
  ret = self.sqlhelper(join_args, newargs)
436
436
  sql_joins << ret[:sql_where]
437
-
437
+
438
438
  #If any of the join-arguments are left, then we should throw an error.
439
439
  join_args.each do |key, val|
440
440
  raise "Invalid key '#{key}' when trying to join table '#{table_name}' on table '#{args_def[:table]}'."
441
441
  end
442
442
  end
443
443
  end
444
-
444
+
445
445
  #If limit arguments has been given then add them.
446
446
  if limit_from and limit_to
447
447
  sql_limit = " LIMIT #{limit_from}, #{limit_to}"
448
448
  end
449
-
450
- sql_groupby = nil if sql_groupby.length <= 0
451
-
449
+
450
+ sql_groupby = nil if sql_groupby.empty?
451
+
452
452
  return {
453
453
  :sql_joins => sql_joins,
454
454
  :sql_where => sql_where,
@@ -457,43 +457,43 @@ class Baza::ModelHandler
457
457
  :sql_groupby => sql_groupby
458
458
  }
459
459
  end
460
-
460
+
461
461
  #Used by sqlhelper-method to look up datarow-classes and automatically load them if they arent loaded already.
462
462
  def datarow_obj_from_args(args, list_args, class_name)
463
463
  class_name = class_name.to_sym
464
-
464
+
465
465
  if !args.key?(:joined_tables)
466
466
  raise "No joined tables on '#{args[:table]}' to find datarow for: '#{class_name}'."
467
467
  end
468
-
468
+
469
469
  args[:joined_tables].each do |table_name, table_data|
470
470
  next if table_name.to_sym != class_name
471
471
  return self.datarow_from_datarow_argument(table_data[:datarow]) if table_data[:datarow]
472
-
472
+
473
473
  self.requireclass(class_name) if @objects.key?(class_name)
474
474
  return @args[:module].const_get(class_name)
475
475
  end
476
-
476
+
477
477
  raise "Could not figure out datarow for: '#{class_name}'."
478
478
  end
479
-
479
+
480
480
  def datarow_from_datarow_argument(datarow_argument)
481
481
  if datarow_argument.is_a?(String)
482
482
  const = Knj::Strings.const_get_full(datarow_argument)
483
483
  else
484
484
  const = datarow_argument
485
485
  end
486
-
486
+
487
487
  self.load_class(datarow_argument.to_s.split("::").last) if !const.initialized? #Make sure the class is initialized.
488
-
488
+
489
489
  return const
490
490
  end
491
-
491
+
492
492
  def not(not_v, val)
493
493
  if not_v == "not" or not_v == "not_"
494
494
  return val
495
495
  end
496
-
496
+
497
497
  return ""
498
498
  end
499
- end
499
+ end
@@ -46,4 +46,21 @@ shared_examples_for "a baza columns driver" do
46
46
  col_text = test_table.column(:text)
47
47
  col_text.type.should eq :varchar
48
48
  end
49
+
50
+ it "should be able to change columns" do
51
+ col_text = test_table.column(:text)
52
+ col_text.change(name: "text2", type: :int, default: 5)
53
+
54
+ col_text.type.should eq :int
55
+ col_text.default.should eq "5"
56
+ col_text.name.should eq :text2
57
+ end
58
+
59
+ it "should be able to drop a column" do
60
+ test_table.column(:text).drop
61
+
62
+ expect {
63
+ test_table.column(:text)
64
+ }.to raise_error(Errno::ENOENT)
65
+ end
49
66
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baza
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Johansen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-15 00:00:00.000000000 Z
11
+ date: 2015-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: datet
@@ -200,16 +200,28 @@ files:
200
200
  - include/driver.rb
201
201
  - include/drivers/active_record/active_record.rb
202
202
  - include/drivers/mysql/mysql.rb
203
+ - include/drivers/mysql/mysql_column.rb
203
204
  - include/drivers/mysql/mysql_columns.rb
205
+ - include/drivers/mysql/mysql_index.rb
204
206
  - include/drivers/mysql/mysql_indexes.rb
207
+ - include/drivers/mysql/mysql_result.rb
208
+ - include/drivers/mysql/mysql_result_java.rb
209
+ - include/drivers/mysql/mysql_result_mysql2.rb
210
+ - include/drivers/mysql/mysql_result_unbuffered.rb
205
211
  - include/drivers/mysql/mysql_sqlspecs.rb
212
+ - include/drivers/mysql/mysql_table.rb
206
213
  - include/drivers/mysql/mysql_tables.rb
207
214
  - include/drivers/sqlite3/libknjdb_java_sqlite3.rb
208
215
  - include/drivers/sqlite3/libknjdb_sqlite3_ironruby.rb
209
216
  - include/drivers/sqlite3/sqlite3.rb
217
+ - include/drivers/sqlite3/sqlite3_column.rb
210
218
  - include/drivers/sqlite3/sqlite3_columns.rb
219
+ - include/drivers/sqlite3/sqlite3_index.rb
211
220
  - include/drivers/sqlite3/sqlite3_indexes.rb
221
+ - include/drivers/sqlite3/sqlite3_result.rb
222
+ - include/drivers/sqlite3/sqlite3_result_java.rb
212
223
  - include/drivers/sqlite3/sqlite3_sqlspecs.rb
224
+ - include/drivers/sqlite3/sqlite3_table.rb
213
225
  - include/drivers/sqlite3/sqlite3_tables.rb
214
226
  - include/dump.rb
215
227
  - include/idquery.rb