baza 0.0.19 → 0.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop_todo.yml +157 -0
  3. data/Gemfile +11 -9
  4. data/Gemfile.lock +41 -9
  5. data/README.md +1 -1
  6. data/Rakefile +19 -16
  7. data/VERSION +1 -1
  8. data/baza.gemspec +31 -7
  9. data/config/best_project_practice_rubocop.yml +2 -0
  10. data/config/best_project_practice_rubocop_todo.yml +157 -0
  11. data/lib/baza/base_sql_driver.rb +79 -20
  12. data/lib/baza/cloner.rb +2 -2
  13. data/lib/baza/column.rb +6 -0
  14. data/lib/baza/database.rb +33 -0
  15. data/lib/baza/database_model.rb +26 -0
  16. data/lib/baza/database_model_functionality.rb +10 -0
  17. data/lib/baza/database_model_name.rb +16 -0
  18. data/lib/baza/db.rb +210 -413
  19. data/lib/baza/dbtime.rb +20 -20
  20. data/lib/baza/driver.rb +3 -3
  21. data/lib/baza/drivers/active_record/columns.rb +1 -1
  22. data/lib/baza/drivers/active_record/indexes.rb +1 -1
  23. data/lib/baza/drivers/active_record/result.rb +3 -5
  24. data/lib/baza/drivers/active_record/tables.rb +2 -2
  25. data/lib/baza/drivers/active_record.rb +30 -16
  26. data/lib/baza/drivers/mysql/column.rb +51 -48
  27. data/lib/baza/drivers/mysql/columns.rb +8 -8
  28. data/lib/baza/drivers/mysql/database.rb +28 -0
  29. data/lib/baza/drivers/mysql/databases.rb +35 -0
  30. data/lib/baza/drivers/mysql/index.rb +32 -22
  31. data/lib/baza/drivers/mysql/result.rb +24 -24
  32. data/lib/baza/drivers/mysql/sqlspecs.rb +1 -1
  33. data/lib/baza/drivers/mysql/table.rb +80 -91
  34. data/lib/baza/drivers/mysql/tables.rb +30 -32
  35. data/lib/baza/drivers/mysql/unbuffered_result.rb +5 -5
  36. data/lib/baza/drivers/mysql.rb +127 -220
  37. data/lib/baza/drivers/mysql2/database.rb +2 -0
  38. data/lib/baza/drivers/mysql2/databases.rb +2 -0
  39. data/lib/baza/drivers/mysql2/result.rb +3 -5
  40. data/lib/baza/drivers/mysql2.rb +21 -106
  41. data/lib/baza/drivers/mysql_java/database.rb +2 -0
  42. data/lib/baza/drivers/mysql_java/databases.rb +2 -0
  43. data/lib/baza/drivers/mysql_java.rb +16 -12
  44. data/lib/baza/drivers/sqlite3/column.rb +33 -33
  45. data/lib/baza/drivers/sqlite3/columns.rb +6 -6
  46. data/lib/baza/drivers/sqlite3/database.rb +2 -0
  47. data/lib/baza/drivers/sqlite3/databases.rb +25 -0
  48. data/lib/baza/drivers/sqlite3/index.rb +20 -6
  49. data/lib/baza/drivers/sqlite3/result.rb +16 -17
  50. data/lib/baza/drivers/sqlite3/sqlspecs.rb +1 -1
  51. data/lib/baza/drivers/sqlite3/table.rb +99 -100
  52. data/lib/baza/drivers/sqlite3/tables.rb +30 -23
  53. data/lib/baza/drivers/sqlite3/unbuffered_result.rb +2 -2
  54. data/lib/baza/drivers/sqlite3.rb +27 -14
  55. data/lib/baza/drivers/sqlite3_java/database.rb +2 -0
  56. data/lib/baza/drivers/sqlite3_java/unbuffered_result.rb +2 -2
  57. data/lib/baza/drivers/sqlite3_java.rb +12 -10
  58. data/lib/baza/drivers/sqlite3_rhodes.rb +9 -7
  59. data/lib/baza/dump.rb +55 -55
  60. data/lib/baza/errors.rb +5 -0
  61. data/lib/baza/idquery.rb +25 -25
  62. data/lib/baza/index.rb +6 -0
  63. data/lib/baza/jdbc_driver.rb +6 -8
  64. data/lib/baza/jdbc_result.rb +1 -1
  65. data/lib/baza/model.rb +299 -309
  66. data/lib/baza/model_custom.rb +74 -72
  67. data/lib/baza/model_handler.rb +244 -255
  68. data/lib/baza/model_handler_sqlhelper.rb +122 -132
  69. data/lib/baza/mysql_base_driver.rb +112 -0
  70. data/lib/baza/query_buffer.rb +23 -23
  71. data/lib/baza/result_base.rb +2 -2
  72. data/lib/baza/revision.rb +67 -67
  73. data/lib/baza/row.rb +82 -84
  74. data/lib/baza/sqlspecs.rb +1 -1
  75. data/lib/baza/table.rb +31 -0
  76. data/lib/baza.rb +43 -10
  77. data/shippable.yml +1 -0
  78. data/spec/cloner_spec.rb +4 -4
  79. data/spec/drivers/active_record_mysql2_spec.rb +3 -3
  80. data/spec/drivers/active_record_mysql_spec.rb +2 -2
  81. data/spec/drivers/active_record_sqlite3_spec.rb +2 -2
  82. data/spec/drivers/mysql2_spec.rb +1 -0
  83. data/spec/drivers/mysql_spec.rb +1 -0
  84. data/spec/drivers/sqlite3_spec.rb +4 -5
  85. data/spec/info_active_record_example.rb +4 -6
  86. data/spec/info_active_record_mysql.rb +6 -7
  87. data/spec/info_active_record_mysql2.rb +2 -4
  88. data/spec/info_active_record_mysql2_shippable.rb +2 -4
  89. data/spec/info_active_record_mysql_shippable.rb +2 -4
  90. data/spec/info_active_record_sqlite3.rb +5 -6
  91. data/spec/info_mysql2_example.rb +1 -1
  92. data/spec/info_mysql2_shippable.rb +1 -1
  93. data/spec/info_mysql_example.rb +1 -1
  94. data/spec/info_mysql_shippable.rb +1 -1
  95. data/spec/info_sqlite3.rb +4 -3
  96. data/spec/model_handler_spec.rb +102 -115
  97. data/spec/spec_helper.rb +5 -5
  98. data/spec/support/driver_collection.rb +98 -95
  99. data/spec/support/driver_columns_collection.rb +18 -22
  100. data/spec/support/driver_databases_collection.rb +29 -0
  101. data/spec/support/driver_indexes_collection.rb +7 -6
  102. data/spec/support/driver_tables_collection.rb +38 -26
  103. metadata +49 -3
@@ -1,7 +1,7 @@
1
- require 'ostruct'
1
+ require "ostruct"
2
2
 
3
3
  class Baza::ModelHandler
4
- #This method helps build SQL from Objects-instances list-method. It should not be called directly but only through Objects.list.
4
+ # This method helps build SQL from Objects-instances list-method. It should not be called directly but only through Objects.list.
5
5
  def sqlhelper(list_args, args_def)
6
6
  args = args_def
7
7
 
@@ -12,7 +12,7 @@ class Baza::ModelHandler
12
12
  end
13
13
 
14
14
  if args[:table]
15
- table_def = "`#{db.esc_table(args[:table])}`."
15
+ table_def = "`#{db.escape_table(args[:table])}`."
16
16
  else
17
17
  table_def = ""
18
18
  end
@@ -51,7 +51,7 @@ class Baza::ModelHandler
51
51
  list_args.delete("ordermode")
52
52
  end
53
53
 
54
- sql_order << "#{table_def}`#{db.esc_col(list_args["orderby"])}`#{ordermode}"
54
+ sql_order << "#{table_def}`#{db.escape_column(list_args["orderby"])}`#{ordermode}"
55
55
  list_args.delete("orderby")
56
56
  end
57
57
  elsif list_args["orderby"].is_a?(Array)
@@ -71,26 +71,26 @@ class Baza::ModelHandler
71
71
 
72
72
  if val[0].is_a?(Array)
73
73
  if args[:joined_tables]
74
- args[:joined_tables].each do |table_name, table_data|
74
+ args[:joined_tables].each do |table_name, _table_data|
75
75
  next if table_name.to_s != val[0][0].to_s
76
76
  do_joins[table_name] = true
77
- orders << "`#{db.esc_table(table_name)}`.`#{db.esc_col(val[0][1])}`#{ordermode}"
77
+ orders << "`#{db.escape_table(table_name)}`.`#{db.escape_column(val[0][1])}`#{ordermode}"
78
78
  found = true
79
79
  break
80
80
  end
81
81
  end
82
82
 
83
- raise "Could not find joined table for ordering: '#{val[0][0]}'." if !found
83
+ raise "Could not find joined table for ordering: '#{val[0][0]}'." unless found
84
84
  else
85
85
  orderstr = val[0]
86
86
  end
87
87
  elsif val.is_a?(String)
88
88
  orderstr = val
89
89
  ordermode = " ASC"
90
- elsif val.is_a?(Hash) and val[:type] == :sql
90
+ elsif val.is_a?(Hash) && val[:type] == :sql
91
91
  orders << val[:sql]
92
92
  found = true
93
- elsif val.is_a?(Hash) and val[:type] == :case
93
+ elsif val.is_a?(Hash) && val[:type] == :case
94
94
  caseorder = " CASE"
95
95
 
96
96
  val[:case].each do |key, caseval|
@@ -99,38 +99,35 @@ class Baza::ModelHandler
99
99
  col_str = nil
100
100
 
101
101
  if col.is_a?(Array)
102
- raise "No joined tables for '#{args[:table]}'." if !args[:joined_tables]
102
+ raise "No joined tables for '#{args[:table]}'." unless args[:joined_tables]
103
103
 
104
104
  found = false
105
- args[:joined_tables].each do |table_name, table_data|
106
- if table_name == col.first
107
- do_joins[table_name] = true
108
- col_str = "`#{db.esc_table(table_name)}`.`#{db.esc_col(col.last)}`"
109
- found = true
110
- break
111
- end
105
+ args[:joined_tables].each do |table_name, _table_data|
106
+ next unless table_name == col.first
107
+ do_joins[table_name] = true
108
+ col_str = "`#{db.escape_table(table_name)}`.`#{db.escape_column(col.last)}`"
109
+ found = true
110
+ break
112
111
  end
113
112
 
114
- 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
115
- elsif col.is_a?(String) or col.is_a?(Symbol)
113
+ 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)}" unless found
114
+ elsif col.is_a?(String) || col.is_a?(Symbol)
116
115
  col_str = "#{table_def}`#{col}`"
117
116
  found = true
118
117
  else
119
118
  raise "Unknown type for case-ordering: '#{col.class.name}'."
120
119
  end
121
120
 
122
- raise "'colstr' was not set." if !col_str
121
+ raise "'colstr' was not set." unless col_str
123
122
  caseorder << " WHEN #{col_str} = '#{db.esc(isval)}' THEN '#{db.esc(caseval)}'"
124
123
  end
125
124
 
126
- if val[:else]
127
- caseorder << " ELSE '#{db.esc(val[:else])}'"
128
- end
125
+ caseorder << " ELSE '#{db.esc(val[:else])}'" if val[:else]
129
126
 
130
127
  caseorder << " END"
131
128
  orders << caseorder
132
129
  elsif val.is_a?(Hash)
133
- raise "No joined tables." if !args.key?(:joined_tables)
130
+ raise "No joined tables." unless args.key?(:joined_tables)
134
131
 
135
132
  if val[:mode] == "asc"
136
133
  ordermode = " ASC"
@@ -143,25 +140,24 @@ class Baza::ModelHandler
143
140
  if table_data[:parent_table]
144
141
  table_name_real = table_name
145
142
  elsif table_data[:datarow]
146
- table_name_real = self.datarow_from_datarow_argument(table_data[:datarow]).classname
143
+ table_name_real = datarow_from_datarow_argument(table_data[:datarow]).classname
147
144
  else
148
145
  table_name_real = @args[:module].const_get(table_name).classname
149
146
  end
150
147
 
151
- if table_name.to_s == val[:table].to_s
152
- do_joins[table_name] = true
153
-
154
- if val[:sql]
155
- orders << val[:sql]
156
- elsif val[:col]
157
- orders << "`#{db.esc_table(table_name_real)}`.`#{db.esc_col(val[:col])}`#{ordermode}"
158
- else
159
- raise "Couldnt figure out how to order based on keys: '#{val.keys.sort}'."
160
- end
148
+ next unless table_name.to_s == val[:table].to_s
149
+ do_joins[table_name] = true
161
150
 
162
- found = true
163
- break
151
+ if val[:sql]
152
+ orders << val[:sql]
153
+ elsif val[:col]
154
+ orders << "`#{db.escape_table(table_name_real)}`.`#{db.escape_column(val[:col])}`#{ordermode}"
155
+ else
156
+ raise "Couldnt figure out how to order based on keys: '#{val.keys.sort}'."
164
157
  end
158
+
159
+ found = true
160
+ break
165
161
  end
166
162
  end
167
163
  else
@@ -170,11 +166,9 @@ class Baza::ModelHandler
170
166
 
171
167
  found = true if args[:cols].key?(orderstr)
172
168
 
173
- if !found
174
- raise "Column not found for ordering: #{orderstr}."
175
- end
169
+ raise "Column not found for ordering: #{orderstr}." unless found
176
170
 
177
- orders << "#{table_def}`#{db.esc_col(orderstr)}`#{ordermode}" if orderstr
171
+ orders << "#{table_def}`#{db.escape_column(orderstr)}`#{ordermode}" if orderstr
178
172
  end
179
173
 
180
174
  sql_order << orders.join(", ")
@@ -189,9 +183,9 @@ class Baza::ModelHandler
189
183
 
190
184
  if realkey.is_a?(Array)
191
185
  if !args[:joins_skip]
192
- datarow_obj = self.datarow_obj_from_args(args_def, list_args, realkey[0])
186
+ datarow_obj = datarow_obj_from_args(args_def, list_args, realkey[0])
193
187
  args = datarow_obj.columns_sqlhelper_args
194
- raise "Couldnt get arguments from SQLHelper." if !args
188
+ raise "Couldnt get arguments from SQLHelper." unless args
195
189
  else
196
190
  datarow_obj = @args[:module].const_get(realkey[0])
197
191
  args = args_def
@@ -200,7 +194,7 @@ class Baza::ModelHandler
200
194
  table_sym = realkey[0].to_sym
201
195
  do_joins[table_sym] = true
202
196
  list_table_name_real = table_sym
203
- table = "`#{db.esc_table(list_table_name_real)}`."
197
+ table = "`#{db.escape_table(list_table_name_real)}`."
204
198
  key = realkey[1]
205
199
  else
206
200
  table = table_def
@@ -208,38 +202,38 @@ class Baza::ModelHandler
208
202
  key = realkey
209
203
  end
210
204
 
211
- if args.key?(:cols_bools) and args[:cols_bools].index(key) != nil
205
+ if args.key?(:cols_bools) && !args[:cols_bools].index(key).nil?
212
206
  val_s = val.to_s
213
207
 
214
- if val_s == "1" or val_s == "true"
208
+ if val_s == "1" || val_s == "true"
215
209
  realval = "1"
216
- elsif val_s == "0" or val_s == "false"
210
+ elsif val_s == "0" || val_s == "false"
217
211
  realval = "0"
218
212
  else
219
213
  raise "Could not make real value out of class: #{val.class.name} => #{val}."
220
214
  end
221
215
 
222
- sql_where << " AND #{table}`#{db.esc_col(key)}` = '#{db.esc(realval)}'"
216
+ sql_where << " AND #{table}`#{db.escape_column(key)}` = '#{db.esc(realval)}'"
223
217
  found = true
224
218
  elsif args[:cols].key?(key.to_s)
225
219
  if val.is_a?(Array)
226
- if val.empty? and db.opts[:type].to_s == "mysql"
220
+ if val.empty? && db.opts[:type].to_s == "mysql"
227
221
  sql_where << " AND false"
228
222
  else
229
- escape_sql = val.map { |v| "'#{db.escape(v)}'" }.join(',')
230
- sql_where << " AND #{table}`#{db.esc_col(key)}` IN (#{escape_sql})"
223
+ escape_sql = val.map { |v| "'#{db.escape(v)}'" }.join(",")
224
+ sql_where << " AND #{table}`#{db.escape_column(key)}` IN (#{escape_sql})"
231
225
  end
232
- elsif val.is_a?(Hash) and val[:type].to_sym == :col
233
- raise "No table was given for join: '#{val}', key: '#{key}' on table #{table}." if !val.key?(:table)
226
+ elsif val.is_a?(Hash) && val[:type].to_sym == :col
227
+ raise "No table was given for join: '#{val}', key: '#{key}' on table #{table}." unless val.key?(:table)
234
228
  do_joins[val[:table].to_sym] = true
235
- sql_where << " AND #{table}`#{db.esc_col(key)}` = `#{db.esc_table(val[:table])}`.`#{db.esc_col(val[:name])}`"
236
- elsif val.is_a?(Hash) and val[:type] == :sqlval and val[:val] == :null
237
- sql_where << " AND #{table}`#{db.esc_col(key)}` IS NULL"
229
+ sql_where << " AND #{table}`#{db.escape_column(key)}` = `#{db.escape_table(val[:table])}`.`#{db.escape_column(val[:name])}`"
230
+ elsif val.is_a?(Hash) && val[:type] == :sqlval && val[:val] == :null
231
+ sql_where << " AND #{table}`#{db.escape_column(key)}` IS NULL"
238
232
  elsif val.is_a?(Proc)
239
- call_args = OpenStruct.new(:ob => self, :db => db)
240
- sql_where << " AND #{table}`#{db.esc_col(key)}` = '#{db.esc(val.call(call_args))}'"
233
+ call_args = OpenStruct.new(ob: self, db: db)
234
+ sql_where << " AND #{table}`#{db.escape_column(key)}` = '#{db.esc(val.call(call_args))}'"
241
235
  else
242
- sql_where << " AND #{table}`#{db.esc_col(key)}` = '#{db.esc(val)}'"
236
+ sql_where << " AND #{table}`#{db.escape_column(key)}` = '#{db.esc(val)}'"
243
237
  end
244
238
 
245
239
  found = true
@@ -253,61 +247,61 @@ class Baza::ModelHandler
253
247
  limit_from = 0
254
248
  limit_to = val.to_i
255
249
  found = true
256
- elsif args.key?(:cols_dbrows) and args[:cols_dbrows].index("#{key.to_s}_id") != nil
250
+ elsif args.key?(:cols_dbrows) && !args[:cols_dbrows].index("#{key}_id").nil?
257
251
  if val == false
258
- sql_where << " AND #{table}`#{db.esc_col(key.to_s + "_id")}` = '0'"
252
+ sql_where << " AND #{table}`#{db.escape_column(key.to_s + "_id")}` = '0'"
259
253
  elsif val.is_a?(Array)
260
254
  if val.empty?
261
255
  sql_where << " AND false"
262
256
  else
263
- sql_where << " AND #{table}`#{db.esc_col("#{key}_id")}` IN (#{Knj::ArrayExt.join(:arr => val, :sep => ",", :surr => "'", :callback => proc{|obj| obj.id.sql})})"
257
+ sql_where << " AND #{table}`#{db.escape_column("#{key}_id")}` IN (#{Knj::ArrayExt.join(arr: val, sep: ",", surr: "'", callback: proc { |obj| obj.id.sql })})"
264
258
  end
265
259
  else
266
- sql_where << " AND #{table}`#{db.esc_col(key.to_s + "_id")}` = '#{db.esc(val.id)}'"
260
+ sql_where << " AND #{table}`#{db.escape_column(key.to_s + "_id")}` = '#{db.esc(val.id)}'"
267
261
  end
268
262
 
269
263
  found = true
270
- elsif match = key.match(/^([A-z_\d]+)_(search|has)$/) and args[:cols].key?(match[1]) != nil
264
+ elsif match = key.match(/^([A-z_\d]+)_(search|has)$/) && !args[:cols].key?(match[1]).nil?
271
265
  if match[2] == "search"
272
266
  Knj::Strings.searchstring(val).each do |str|
273
- sql_where << " AND #{table}`#{db.esc_col(match[1])}` LIKE '%#{db.esc(str)}%'"
267
+ sql_where << " AND #{table}`#{db.escape_column(match[1])}` LIKE '%#{db.esc(str)}%'"
274
268
  end
275
269
  elsif match[2] == "has"
276
270
  if val
277
- sql_where << " AND #{table}`#{db.esc_col(match[1])}` != ''"
271
+ sql_where << " AND #{table}`#{db.escape_column(match[1])}` != ''"
278
272
  else
279
- sql_where << " AND #{table}`#{db.esc_col(match[1])}` = ''"
273
+ sql_where << " AND #{table}`#{db.escape_column(match[1])}` = ''"
280
274
  end
281
275
  end
282
276
 
283
277
  found = true
284
- elsif match = key.match(/^([A-z_\d]+)_(not|lower)$/) and args[:cols].key?(match[1])
278
+ elsif match = key.match(/^([A-z_\d]+)_(not|lower)$/) && args[:cols].key?(match[1])
285
279
  if match[2] == "not"
286
280
  if val.is_a?(Array)
287
281
  if val.empty?
288
- #ignore.
282
+ # ignore.
289
283
  else
290
284
  escape_sql = Knj::ArrayExt.join(
291
- :arr => val,
292
- :callback => proc{|value|
285
+ arr: val,
286
+ callback: proc do|value|
293
287
  db.escape(value)
294
- },
295
- :sep => ",",
296
- :surr => "'"
288
+ end,
289
+ sep: ",",
290
+ surr: "'"
297
291
  )
298
- sql_where << " AND #{table}`#{db.esc_col(match[1])}` NOT IN (#{escape_sql})"
292
+ sql_where << " AND #{table}`#{db.escape_column(match[1])}` NOT IN (#{escape_sql})"
299
293
  end
300
294
  else
301
- sql_where << " AND #{table}`#{db.esc_col(match[1])}` != '#{db.esc(val)}'"
295
+ sql_where << " AND #{table}`#{db.escape_column(match[1])}` != '#{db.esc(val)}'"
302
296
  end
303
297
  elsif match[2] == "lower"
304
- sql_where << " AND LOWER(#{table}`#{db.esc_col(match[1])}`) = LOWER('#{db.esc(val)}')"
298
+ sql_where << " AND LOWER(#{table}`#{db.escape_column(match[1])}`) = LOWER('#{db.esc(val)}')"
305
299
  else
306
300
  raise "Unknown mode: '#{match[2]}'."
307
301
  end
308
302
 
309
303
  found = true
310
- 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
304
+ elsif args.key?(:cols_date) && (match = key.match(/^(.+)_(day|week|month|year|from|to|below|above)(|_(not))$/)) && !args[:cols_date].index(match[1]).nil?
311
305
  not_v = match[4]
312
306
  val = Datet.in(val) if val.is_a?(Time)
313
307
 
@@ -316,65 +310,65 @@ class Baza::ModelHandler
316
310
  sql_where << " AND ("
317
311
  first = true
318
312
 
319
- val.each do |realval|
313
+ val.each do |dayval|
320
314
  if first
321
315
  first = false
322
316
  else
323
317
  sql_where << " OR "
324
318
  end
325
319
 
326
- 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)}'")}"
320
+ sql_where << "#{db.sqlspecs.strftime("%d %m %Y", "#{table}`#{db.escape_column(match[1])}`")} #{self.not(not_v, "!")}= #{db.sqlspecs.strftime("%d %m %Y", "'#{db.esc(dayval.dbstr)}'")}"
327
321
  end
328
322
 
329
323
  sql_where << ")"
330
324
  else
331
- 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)}'")}"
325
+ sql_where << " AND #{db.sqlspecs.strftime("%d %m %Y", "#{table}`#{db.escape_column(match[1])}`")} #{self.not(not_v, "!")}= #{db.sqlspecs.strftime("%d %m %Y", "'#{db.esc(val.dbstr)}'")}"
332
326
  end
333
327
  elsif match[2] == "week"
334
- sql_where << " AND #{db.sqlspecs.strftime("%W %Y", "#{table}`#{db.esc_col(match[1])}`")} #{self.not(not_v, "!")}= #{db.sqlspecs.strftime("%W %Y", "'#{db.esc(val.dbstr)}'")}"
328
+ sql_where << " AND #{db.sqlspecs.strftime("%W %Y", "#{table}`#{db.escape_column(match[1])}`")} #{self.not(not_v, "!")}= #{db.sqlspecs.strftime("%W %Y", "'#{db.esc(val.dbstr)}'")}"
335
329
  elsif match[2] == "month"
336
- sql_where << " AND #{db.sqlspecs.strftime("%m %Y", "#{table}`#{db.esc_col(match[1])}`")} #{self.not(not_v, "!")}= #{db.sqlspecs.strftime("%m %Y", "'#{db.esc(val.dbstr)}'")}"
330
+ sql_where << " AND #{db.sqlspecs.strftime("%m %Y", "#{table}`#{db.escape_column(match[1])}`")} #{self.not(not_v, "!")}= #{db.sqlspecs.strftime("%m %Y", "'#{db.esc(val.dbstr)}'")}"
337
331
  elsif match[2] == "year"
338
- sql_where << " AND #{db.sqlspecs.strftime("%Y", "#{table}`#{db.esc_col(match[1])}`")} #{self.not(not_v, "!")}= #{db.sqlspecs.strftime("%Y", "'#{db.esc(val.dbstr)}'")}"
339
- elsif match[2] == "from" or match[2] == "above"
340
- sql_where << " AND #{table}`#{db.esc_col(match[1])}` >= '#{db.esc(val.dbstr)}'"
341
- elsif match[2] == "to" or match[2] == "below"
342
- sql_where << " AND #{table}`#{db.esc_col(match[1])}` <= '#{db.esc(val.dbstr)}'"
332
+ sql_where << " AND #{db.sqlspecs.strftime("%Y", "#{table}`#{db.escape_column(match[1])}`")} #{self.not(not_v, "!")}= #{db.sqlspecs.strftime("%Y", "'#{db.esc(val.dbstr)}'")}"
333
+ elsif match[2] == "from" || match[2] == "above"
334
+ sql_where << " AND #{table}`#{db.escape_column(match[1])}` >= '#{db.esc(val.dbstr)}'"
335
+ elsif match[2] == "to" || match[2] == "below"
336
+ sql_where << " AND #{table}`#{db.escape_column(match[1])}` <= '#{db.esc(val.dbstr)}'"
343
337
  else
344
338
  raise "Unknown date-key: #{match[2]}."
345
339
  end
346
340
 
347
341
  found = true
348
- elsif args.key?(:cols_num) and match = key.match(/^(.+)_(from|to|above|below|numeric)$/) and args[:cols_num].index(match[1]) != nil
342
+ elsif args.key?(:cols_num) && match = key.match(/^(.+)_(from|to|above|below|numeric)$/) && !args[:cols_num].index(match[1]).nil?
349
343
  if match[2] == "from"
350
- sql_where << " AND #{table}`#{db.esc_col(match[1])}` >= '#{db.esc(val)}'"
344
+ sql_where << " AND #{table}`#{db.escape_column(match[1])}` >= '#{db.esc(val)}'"
351
345
  elsif match[2] == "to"
352
- sql_where << " AND #{table}`#{db.esc_col(match[1])}` <= '#{db.esc(val)}'"
346
+ sql_where << " AND #{table}`#{db.escape_column(match[1])}` <= '#{db.esc(val)}'"
353
347
  elsif match[2] == "above"
354
- sql_where << " AND #{table}`#{db.esc_col(match[1])}` > '#{db.esc(val)}'"
348
+ sql_where << " AND #{table}`#{db.escape_column(match[1])}` > '#{db.esc(val)}'"
355
349
  elsif match[2] == "below"
356
- sql_where << " AND #{table}`#{db.esc_col(match[1])}` < '#{db.esc(val)}'"
350
+ sql_where << " AND #{table}`#{db.escape_column(match[1])}` < '#{db.esc(val)}'"
357
351
  else
358
352
  raise "Unknown method of treating cols-num-argument: #{match[2]}."
359
353
  end
360
354
 
361
355
  found = true
362
- elsif match = key.match(/^(.+)_lookup$/) and args[:cols].key?("#{match[1]}_id") and args[:cols].key?("#{match[1]}_class")
363
- sql_where << " AND #{table}`#{db.esc_col("#{match[1]}_class")}` = '#{db.esc(val.table)}'"
364
- sql_where << " AND #{table}`#{db.esc_col("#{match[1]}_id")}` = '#{db.esc(val.id)}'"
356
+ elsif match = key.match(/^(.+)_lookup$/) && args[:cols].key?("#{match[1]}_id") && args[:cols].key?("#{match[1]}_class")
357
+ sql_where << " AND #{table}`#{db.escape_column("#{match[1]}_class")}` = '#{db.esc(val.table)}'"
358
+ sql_where << " AND #{table}`#{db.escape_column("#{match[1]}_id")}` = '#{db.esc(val.id)}'"
365
359
  found = true
366
360
  elsif realkey == "groupby"
367
361
  found = true
368
362
 
369
363
  if val.is_a?(Array)
370
364
  val.each do |col_name|
371
- raise "Column '#{val}' not found on table '#{table}'." if !args[:cols].key?(col_name)
365
+ raise "Column '#{val}' not found on table '#{table}'." unless args[:cols].key?(col_name)
372
366
  sql_groupby << ", " if sql_groupby.length > 0
373
- sql_groupby << "#{table}`#{db.esc_col(col_name)}`"
367
+ sql_groupby << "#{table}`#{db.escape_column(col_name)}`"
374
368
  end
375
369
  elsif val.is_a?(String)
376
370
  sql_groupby << ", " if sql_groupby.length > 0
377
- sql_groupby << "#{table}`#{db.esc_col(val)}`"
371
+ sql_groupby << "#{table}`#{db.escape_column(val)}`"
378
372
  else
379
373
  raise "Unknown class given for 'groupby': '#{val.class.name}'."
380
374
  end
@@ -385,11 +379,11 @@ class Baza::ModelHandler
385
379
 
386
380
  args = args_def
387
381
 
388
- if !args[:joins_skip]
389
- raise "No joins defined on '#{args[:table]}' for: '#{args[:table]}'." if !do_joins.empty? and !args[:joined_tables]
382
+ unless args[:joins_skip]
383
+ raise "No joins defined on '#{args[:table]}' for: '#{args[:table]}'." if !do_joins.empty? && !args[:joined_tables]
390
384
 
391
- do_joins.each do |table_name, temp_val|
392
- raise "No join defined on table '#{args[:table]}' for table '#{table_name}'." if !args[:joined_tables].key?(table_name)
385
+ do_joins.each do |table_name, _temp_val|
386
+ raise "No join defined on table '#{args[:table]}' for table '#{table_name}'." unless args[:joined_tables].key?(table_name)
393
387
  table_data = args[:joined_tables][table_name]
394
388
 
395
389
  if table_data.key?(:parent_table)
@@ -410,62 +404,60 @@ class Baza::ModelHandler
410
404
  class_name = args[:table].to_sym
411
405
 
412
406
  if table_data[:datarow]
413
- datarow = self.datarow_from_datarow_argument(table_data[:datarow])
407
+ datarow = datarow_from_datarow_argument(table_data[:datarow])
414
408
  else
415
- self.requireclass(class_name) if @objects.key?(class_name)
409
+ requireclass(class_name) if @objects.key?(class_name)
416
410
  datarow = @args[:module].const_get(class_name)
417
411
  end
418
412
 
419
- if !datarow.columns_sqlhelper_args
413
+ unless datarow.columns_sqlhelper_args
420
414
  ob.requireclass(datarow.table.to_sym)
421
- raise "No SQL-helper-args on class '#{datarow.table}' ???" if !datarow.columns_sqlhelper_args
415
+ raise "No SQL-helper-args on class '#{datarow.table}' ???" unless datarow.columns_sqlhelper_args
422
416
  end
423
417
 
424
418
  newargs = datarow.columns_sqlhelper_args.clone
425
419
  newargs[:table] = join_table_name_real
426
420
  newargs[:joins_skip] = true
427
421
 
428
- #Clone the where-arguments and run them against another sqlhelper to sub-join.
422
+ # Clone the where-arguments and run them against another sqlhelper to sub-join.
429
423
  join_args = table_data[:where].clone
430
- ret = self.sqlhelper(join_args, newargs)
424
+ ret = sqlhelper(join_args, newargs)
431
425
  sql_joins << ret[:sql_where]
432
426
 
433
- #If any of the join-arguments are left, then we should throw an error.
434
- join_args.each do |key, val|
427
+ # If any of the join-arguments are left, then we should throw an error.
428
+ join_args.each_key do |key|
435
429
  raise "Invalid key '#{key}' when trying to join table '#{table_name}' on table '#{args_def[:table]}'."
436
430
  end
437
431
  end
438
432
  end
439
433
 
440
- #If limit arguments has been given then add them.
441
- if limit_from and limit_to
442
- sql_limit = " LIMIT #{limit_from}, #{limit_to}"
443
- end
434
+ # If limit arguments has been given then add them.
435
+ sql_limit = " LIMIT #{limit_from}, #{limit_to}" if limit_from && limit_to
444
436
 
445
437
  sql_groupby = nil if sql_groupby.empty?
446
438
 
447
- return {
448
- :sql_joins => sql_joins,
449
- :sql_where => sql_where,
450
- :sql_limit => sql_limit,
451
- :sql_order => sql_order,
452
- :sql_groupby => sql_groupby
439
+ {
440
+ sql_joins: sql_joins,
441
+ sql_where: sql_where,
442
+ sql_limit: sql_limit,
443
+ sql_order: sql_order,
444
+ sql_groupby: sql_groupby
453
445
  }
454
446
  end
455
447
 
456
- #Used by sqlhelper-method to look up datarow-classes and automatically load them if they arent loaded already.
457
- def datarow_obj_from_args(args, list_args, class_name)
448
+ # Used by sqlhelper-method to look up datarow-classes and automatically load them if they arent loaded already.
449
+ def datarow_obj_from_args(args, _list_args, class_name)
458
450
  class_name = class_name.to_sym
459
451
 
460
- if !args.key?(:joined_tables)
452
+ unless args.key?(:joined_tables)
461
453
  raise "No joined tables on '#{args[:table]}' to find datarow for: '#{class_name}'."
462
454
  end
463
455
 
464
456
  args[:joined_tables].each do |table_name, table_data|
465
457
  next if table_name.to_sym != class_name
466
- return self.datarow_from_datarow_argument(table_data[:datarow]) if table_data[:datarow]
458
+ return datarow_from_datarow_argument(table_data[:datarow]) if table_data[:datarow]
467
459
 
468
- self.requireclass(class_name) if @objects.key?(class_name)
460
+ requireclass(class_name) if @objects.key?(class_name)
469
461
  return @args[:module].const_get(class_name)
470
462
  end
471
463
 
@@ -479,16 +471,14 @@ class Baza::ModelHandler
479
471
  const = datarow_argument
480
472
  end
481
473
 
482
- self.load_class(datarow_argument.to_s.split("::").last) if !const.initialized? #Make sure the class is initialized.
474
+ load_class(datarow_argument.to_s.split("::").last) unless const.initialized? # Make sure the class is initialized.
483
475
 
484
- return const
476
+ const
485
477
  end
486
478
 
487
479
  def not(not_v, val)
488
- if not_v == "not" or not_v == "not_"
489
- return val
490
- end
480
+ return val if not_v == "not" || not_v == "not_"
491
481
 
492
- return ""
482
+ ""
493
483
  end
494
484
  end
@@ -0,0 +1,112 @@
1
+ class Baza::MysqlBaseDriver < Baza::BaseSqlDriver
2
+ def self.args
3
+ [{
4
+ label: "Host",
5
+ name: "host"
6
+ }, {
7
+ label: "Port",
8
+ name: "port"
9
+ }, {
10
+ label: "Username",
11
+ name: "user"
12
+ }, {
13
+ label: "Password",
14
+ name: "pass"
15
+ }, {
16
+ label: "Database",
17
+ name: "db"
18
+ }, {
19
+ label: "Encoding",
20
+ name: "encoding"
21
+ }]
22
+ end
23
+
24
+ # Inserts multiple rows in a table. Can return the inserted IDs if asked to in arguments.
25
+ def insert_multi(tablename, arr_hashes, args = nil)
26
+ sql = "INSERT INTO `#{tablename}` ("
27
+
28
+ first = true
29
+ if args && args[:keys]
30
+ keys = args[:keys]
31
+ elsif arr_hashes.first.is_a?(Hash)
32
+ keys = arr_hashes.first.keys
33
+ else
34
+ raise "Could not figure out keys."
35
+ end
36
+
37
+ keys.each do |col_name|
38
+ sql << "," unless first
39
+ first = false if first
40
+ sql << "`#{escape_column(col_name)}`"
41
+ end
42
+
43
+ sql << ") VALUES ("
44
+
45
+ first = true
46
+ arr_hashes.each do |hash|
47
+ if first
48
+ first = false
49
+ else
50
+ sql << "),("
51
+ end
52
+
53
+ first_key = true
54
+ if hash.is_a?(Array)
55
+ hash.each do |val|
56
+ if first_key
57
+ first_key = false
58
+ else
59
+ sql << ","
60
+ end
61
+
62
+ sql << @baza.sqlval(val)
63
+ end
64
+ else
65
+ hash.each do |_key, val|
66
+ if first_key
67
+ first_key = false
68
+ else
69
+ sql << ","
70
+ end
71
+
72
+ sql << @baza.sqlval(val)
73
+ end
74
+ end
75
+ end
76
+
77
+ sql << ")"
78
+
79
+ return sql if args && args[:return_sql]
80
+
81
+ query(sql)
82
+
83
+ if args && args[:return_id]
84
+ first_id = last_id
85
+ raise "Invalid ID: #{first_id}" if first_id.to_i <= 0
86
+ ids = [first_id]
87
+ 1.upto(arr_hashes.length - 1) do |count|
88
+ ids << first_id + count
89
+ end
90
+
91
+ ids_length = ids.length
92
+ arr_hashes_length = arr_hashes.length
93
+ raise "Invalid length (#{ids_length}, #{arr_hashes_length})." unless ids_length == arr_hashes_length
94
+
95
+ return ids
96
+ else
97
+ return nil
98
+ end
99
+ end
100
+
101
+ def transaction
102
+ @baza.q("START TRANSACTION")
103
+
104
+ begin
105
+ yield @baza
106
+ @baza.q("COMMIT")
107
+ rescue
108
+ @baza.q("ROLLBACK")
109
+ raise
110
+ end
111
+ end
112
+ end