baza 0.0.19 → 0.0.20

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -2,40 +2,43 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
2
2
  attr_reader :name, :type
3
3
 
4
4
  def initialize(args)
5
- @db = args[:db]
6
- @data = args[:data]
7
- @name = @data[:name].to_sym
8
- @type = @data[:type].to_sym
9
- @tables = args[:tables]
5
+ @db = args.fetch(:db)
6
+ @data = args.fetch(:data)
7
+ @name = @data.fetch(:name)
8
+ @type = @data.fetch(:type).to_sym
9
+ @tables = args.fetch(:tables)
10
10
 
11
11
  @list = Wref::Map.new
12
12
  @indexes_list = Wref::Map.new
13
13
  end
14
14
 
15
15
  def maxlength
16
- return @data[:maxlength]
16
+ @data.fetch(:maxlength)
17
17
  end
18
18
 
19
19
  def reload
20
- @data = @db.select("sqlite_master", {type: "table", name: name}, {orderby: "name"}).fetch
20
+ data = @db.select("sqlite_master", {type: "table", name: name}, orderby: "name").fetch
21
+ raise Baza::Errors::TableNotFound unless data
22
+ @data = data
23
+ self
21
24
  end
22
25
 
23
26
  def rows_count
24
- data = @db.q("SELECT COUNT(*) AS count FROM `#{name}`").fetch
25
- return data[:count].to_i
27
+ data = @db.query("SELECT COUNT(*) AS count FROM `#{name}`").fetch
28
+ data.fetch(:count).to_i
26
29
  end
27
30
 
28
- #Drops the table from the database.
31
+ # Drops the table from the database.
29
32
  def drop
30
33
  raise "Cant drop native table: '#{name}'." if native?
31
34
  @db.query("DROP TABLE `#{name}`")
32
35
  @tables.remove_from_list(self) if @tables.exists_in_list?(self)
33
36
  end
34
37
 
35
- #Returns true if the table is safe to drop.
38
+ # Returns true if the table is safe to drop.
36
39
  def native?
37
40
  return true if name.to_s == "sqlite_sequence"
38
- return false
41
+ false
39
42
  end
40
43
 
41
44
  def optimize
@@ -43,7 +46,7 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
43
46
  end
44
47
 
45
48
  def rename(newname)
46
- newname = newname.to_sym
49
+ newname = newname.to_s
47
50
 
48
51
  @tables.remove_from_list(self)
49
52
  newtable = clone(newname)
@@ -53,37 +56,42 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
53
56
  @name = newname
54
57
  @tables.add_to_list(self)
55
58
 
56
- #Rename table on all columns and indexes.
57
- @list.each do |name, column|
59
+ # Rename table on all columns and indexes.
60
+ @list.each do |_name, column|
58
61
  column.args[:table_name] = newname
59
62
  end
60
63
 
61
- @indexes_list.each do |name, index|
64
+ @indexes_list.each do |_name, index|
62
65
  index.args[:table_name] = newname
63
66
  end
64
67
  end
65
68
 
69
+ # Drops the table and creates it again
66
70
  def truncate
67
- @db.query("DELETE FROM `#{name}` WHERE 1=1")
68
- return nil
71
+ table_data = data
72
+ drop
73
+ @db.tables.create(table_data.delete(:name), table_data)
74
+ self
69
75
  end
70
76
 
71
77
  def table
72
- return @db.tables[@table_name]
78
+ @db.tables[@table_name]
73
79
  end
74
80
 
75
81
  def column(name)
76
- list = self.columns
77
- return list[name] if list[name]
78
- raise Errno::ENOENT, "Column not found: #{name}."
82
+ columns do |column|
83
+ return column if column.name == name.to_s
84
+ end
85
+
86
+ raise Baza::Errors::ColumnNotFound, "Column not found: #{name}"
79
87
  end
80
88
 
81
89
  def columns
82
90
  @db.cols
83
- ret = {}
91
+ ret = []
84
92
 
85
- @db.q("PRAGMA table_info(`#{@db.esc_table(name)}`)") do |d_cols|
86
- column_name = d_cols[:name].to_sym
93
+ @db.query("PRAGMA table_info(`#{@db.escape_table(name)}`)") do |d_cols|
94
+ column_name = d_cols.fetch(:name)
87
95
  obj = @list.get(column_name)
88
96
 
89
97
  unless obj
@@ -98,7 +106,7 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
98
106
  if block_given?
99
107
  yield obj
100
108
  else
101
- ret[column_name] = obj
109
+ ret << obj
102
110
  end
103
111
  end
104
112
 
@@ -111,28 +119,28 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
111
119
 
112
120
  def create_columns(col_arr)
113
121
  col_arr.each do |col_data|
114
- #if col_data.key?("after")
122
+ # if col_data.key?("after")
115
123
  # self.create_column_programmatic(col_data)
116
- #else
117
- @db.query("ALTER TABLE `#{self.name}` ADD COLUMN #{@db.cols.data_sql(col_data)};")
118
- #end
124
+ # else
125
+ @db.query("ALTER TABLE `#{name}` ADD COLUMN #{@db.cols.data_sql(col_data)};")
126
+ # end
119
127
  end
120
128
  end
121
129
 
122
130
  def create_column_programmatic(col_data)
123
131
  temp_name = "temptable_#{Time.now.to_f.to_s.hash}"
124
- cloned_tabled = self.clone(temp_name)
125
- cols_cur = self.columns
126
- @db.query("DROP TABLE `#{self.name}`")
132
+ cloned_tabled = clone(temp_name)
133
+ cols_cur = columns
134
+ @db.query("DROP TABLE `#{name}`")
127
135
 
128
- sql = "CREATE TABLE `#{self.name}` ("
136
+ sql = "CREATE TABLE `#{name}` ("
129
137
  first = true
130
138
  cols_cur.each do |name, col|
131
- sql << ", " if !first
139
+ sql << ", " unless first
132
140
  first = false if first
133
141
  sql << @db.cols.data_sql(col.data)
134
142
 
135
- if col_data[:after] and col_data[:after] == name
143
+ if col_data[:after] && col_data[:after] == name
136
144
  sql << ", #{@db.cols.data_sql(col_data)}"
137
145
  end
138
146
  end
@@ -141,15 +149,13 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
141
149
 
142
150
  sql = "INSERT INTO `#{self.name}` SELECT "
143
151
  first = true
144
- cols_cur.each do |name, col|
145
- sql << ", " if !first
152
+ cols_cur.each do |name, _col|
153
+ sql << ", " unless first
146
154
  first = false if first
147
155
 
148
156
  sql << "`#{name}`"
149
157
 
150
- if col_data[:after] and col_data[:after] == name
151
- sql << ", ''"
152
- end
158
+ sql << ", ''" if col_data[:after] && col_data[:after] == name
153
159
  end
154
160
  sql << " FROM `#{temp_name}`"
155
161
  @db.query(sql)
@@ -161,23 +167,26 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
161
167
 
162
168
  sql = "CREATE TABLE `#{newname}` ("
163
169
  first = true
164
- columns.each do |name, col|
170
+ columns.each do |col|
165
171
  sql << ", " unless first
166
172
  first = false if first
167
173
  sql << @db.cols.data_sql(col.data)
168
174
  end
169
-
170
175
  sql << ");"
176
+
171
177
  @db.query(sql)
172
178
  @db.query("INSERT INTO `#{newname}` SELECT * FROM `#{name}`")
173
179
 
174
180
  indexes_to_create = []
175
181
  new_table = @db.tables[newname.to_sym]
176
- indexes.each do |name, index|
177
- index_name = name.to_s
182
+ indexes.each do |index|
183
+ index_name = index.name.gsub(/\A#{Regexp.escape(name)}_/, "")
178
184
 
179
185
  if @db.opts[:index_append_table_name] && match = index_name.match(/\A(.+?)__(.+)\Z/)
180
186
  index_name = match[2]
187
+ else
188
+ # Two indexes with the same name can't exist, and we are cloning, so we need to change the name
189
+ index_name = "#{newname}_#{index_name}"
181
190
  end
182
191
 
183
192
  create_data = index.data
@@ -197,29 +206,28 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
197
206
 
198
207
  def copy(args = {})
199
208
  temp_name = "temptable_#{Time.now.to_f.to_s.hash}"
200
- cloned_tabled = self.clone(temp_name)
201
- cols_cur = self.columns
202
- @db.query("DROP TABLE `#{self.name}`")
209
+ cloned_tabled = clone(temp_name)
210
+ cols_cur = columns
211
+ @db.query("DROP TABLE `#{name}`")
203
212
 
204
- sql = "CREATE TABLE `#{self.name}` ("
213
+ sql = "CREATE TABLE `#{name}` ("
205
214
  first = true
206
- cols_cur.each do |name, col|
207
- next if args[:drops] && args[:drops].to_s.include?(name.to_s)
215
+ cols_cur.each do |col|
216
+ next if args[:drops] && args[:drops].to_s.include?(col.name)
208
217
 
209
- sql << ", " if !first
218
+ sql << ", " unless first
210
219
  first = false if first
211
220
 
212
- if args.key?(:alter_columns) && args[:alter_columns][name.to_sym]
213
- sql << @db.cols.data_sql(args[:alter_columns][name.to_sym])
221
+ if args.key?(:alter_columns) && args[:alter_columns][col.name]
222
+ sql << @db.cols.data_sql(args[:alter_columns][col.name])
214
223
  else
215
224
  sql << @db.cols.data_sql(col.data)
216
225
  end
217
226
 
218
- if args[:new]
219
- args[:new].each do |col_data|
220
- if col_data[:after] && col_data[:after] == name
221
- sql << ", #{@db.cols.data_sql(col_data)}"
222
- end
227
+ next unless args[:new]
228
+ args[:new].each do |col_data|
229
+ if col_data[:after] && col_data[:after] == col.name
230
+ sql << ", #{@db.cols.data_sql(col_data)}"
223
231
  end
224
232
  end
225
233
  end
@@ -227,22 +235,19 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
227
235
  sql << ");"
228
236
  @db.query(sql)
229
237
 
230
- sql = "INSERT INTO `#{self.name}` SELECT "
238
+ sql = "INSERT INTO `#{name}` SELECT "
231
239
  first = true
232
- cols_cur.each do |name, col|
233
- next if args[:drops] && args[:drops].to_s.include?(name.to_s)
240
+ cols_cur.each do |col|
241
+ next if args[:drops] && args[:drops].to_s.include?(col.name)
234
242
 
235
- sql << ", " if !first
243
+ sql << ", " unless first
236
244
  first = false if first
237
245
 
238
- sql << "`#{name}`"
246
+ sql << "`#{col.name}`"
239
247
 
240
- if args[:news]
241
- args[:news].each do |col_data|
242
- if col_data[:after] && col_data[:after] == name.to_s
243
- sql << ", ''"
244
- end
245
- end
248
+ next unless args[:news]
249
+ args[:news].each do |col_data|
250
+ sql << ", ''" if col_data[:after] && col_data[:after] == col.name
246
251
  end
247
252
  end
248
253
 
@@ -252,36 +257,33 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
252
257
  end
253
258
 
254
259
  def index(index_name)
255
- index_name = index_name.to_sym
260
+ index_name = index_name.to_s
256
261
 
257
- if index = @indexes_list[index_name]
262
+ if (index = @indexes_list[index_name])
258
263
  return index
259
264
  end
260
265
 
261
266
  if @db.opts[:index_append_table_name]
262
267
  tryname = "#{name}__#{index_name}"
263
268
 
264
- if index = @indexes_list[tryname]
269
+ if (index = @indexes_list[tryname])
265
270
  return index
266
271
  end
267
272
  end
268
273
 
269
- indexes do |index|
270
- if index.name.to_s == "#{name}__#{index_name}"
271
- return index
272
- end
273
-
274
- return index if index.name.to_s == index_name.to_s
274
+ indexes do |index_i|
275
+ return index_i if index_i.name == "#{name}__#{index_name}"
276
+ return index_i if index_i.name == index_name
275
277
  end
276
278
 
277
- raise Errno::ENOENT, "Index not found: #{index_name}."
279
+ raise Baza::Errors::IndexNotFound, "Index not found: #{index_name}."
278
280
  end
279
281
 
280
282
  def indexes
281
283
  @db.indexes
282
- ret = {} unless block_given?
284
+ ret = [] unless block_given?
283
285
 
284
- @db.q("PRAGMA index_list(`#{@db.esc_table(name)}`)") do |d_indexes|
286
+ @db.query("PRAGMA index_list(`#{@db.escape_table(name)}`)") do |d_indexes|
285
287
  next if d_indexes[:Key_name] == "PRIMARY"
286
288
  obj = @indexes_list.get(d_indexes[:name])
287
289
 
@@ -304,7 +306,7 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
304
306
  if block_given?
305
307
  yield(obj)
306
308
  else
307
- ret[d_indexes[:name].to_sym] = obj
309
+ ret << obj
308
310
  end
309
311
  end
310
312
 
@@ -316,31 +318,28 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
316
318
  end
317
319
 
318
320
  def create_indexes(index_arr, args = nil)
319
- if args && args[:return_sql]
320
- ret = []
321
- end
321
+ ret = [] if args && args[:return_sql]
322
322
 
323
323
  index_arr.each do |index_data|
324
- if index_data.is_a?(String) or index_data.is_a?(Symbol)
324
+ if index_data.is_a?(String) || index_data.is_a?(Symbol)
325
325
  index_data = {name: index_data, columns: [index_data]}
326
326
  end
327
327
 
328
328
  raise "No name was given in data: '#{index_data}'." if !index_data.key?(:name) || index_data[:name].to_s.strip.empty?
329
329
  raise "No columns was given on index #{index_data[:name]}." if !index_data[:columns] || index_data[:columns].empty?
330
330
 
331
- name = index_data[:name]
332
- name = "#{self.name}__#{name}" if @db.opts[:index_append_table_name]
331
+ index_name = index_data.fetch(:name).to_s
332
+ index_name = "#{name}__#{index_name}" if @db.opts[:index_append_table_name] && !index_name.start_with?("#{name}__")
333
333
 
334
334
  sql = "CREATE"
335
335
  sql << " UNIQUE" if index_data[:unique]
336
- sql << " INDEX '#{@db.esc_col(name)}' ON `#{@db.esc_table(self.name)}` ("
336
+ sql << " INDEX '#{@db.escape_column(index_name)}' ON `#{@db.escape_table(name)}` ("
337
337
 
338
338
  first = true
339
- index_data[:columns].each do |col_name|
340
- sql << ", " if !first
339
+ index_data.fetch(:columns).each do |col_name|
340
+ sql << ", " unless first
341
341
  first = false if first
342
-
343
- sql << "`#{@db.esc_col(col_name)}`"
342
+ sql << "`#{@db.escape_column(col_name)}`"
344
343
  end
345
344
 
346
345
  sql << ")"
@@ -366,15 +365,15 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
366
365
  indexes: []
367
366
  }
368
367
 
369
- columns.each do |name, column|
368
+ columns do |column|
370
369
  ret[:columns] << column.data
371
370
  end
372
371
 
373
- indexes.each do |name, index|
374
- ret[:indexes] << index.data if name != "PRIMARY"
372
+ indexes do |index|
373
+ ret[:indexes] << index.data unless index.name == "PRIMARY"
375
374
  end
376
375
 
377
- return ret
376
+ ret
378
377
  end
379
378
 
380
379
  def insert(data)
@@ -391,8 +390,8 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
391
390
 
392
391
  private
393
392
 
394
- def parse_columns_from_sql sql
393
+ def parse_columns_from_sql(sql)
395
394
  columns_sql = sql.match(/\((.+?)\)\Z/)[1]
396
- return columns_sql.split(",").map{ |column| column[1, column.length - 2] }
395
+ columns_sql.split(",").map { |column| column.match(/`(.+)`/)[1] }
397
396
  end
398
397
  end
@@ -1,3 +1,5 @@
1
+ require "monitor"
2
+
1
3
  class Baza::Driver::Sqlite3::Tables
2
4
  attr_reader :db, :driver
3
5
 
@@ -5,33 +7,36 @@ class Baza::Driver::Sqlite3::Tables
5
7
  @args = args
6
8
  @db = @args[:db]
7
9
 
8
- @list_mutex = Mutex.new
10
+ @list_mutex = Monitor.new
9
11
  @list = Wref::Map.new
10
12
  end
11
13
 
12
14
  def [](table_name)
13
- table_name = table_name.to_sym
15
+ table_name = table_name.to_s
14
16
 
15
17
  if ret = @list.get(table_name)
16
18
  return ret
17
19
  end
18
20
 
19
- self.list do |table_obj|
20
- return table_obj if table_obj.name == table_name
21
+ list(name: table_name) do |table|
22
+ return table if table.name == table_name
21
23
  end
22
24
 
23
- raise Errno::ENOENT, "Table was not found: #{table_name}."
25
+ raise Baza::Errors::TableNotFound, "Table was not found: #{table_name}."
24
26
  end
25
27
 
26
- def list
27
- ret = {} unless block_given?
28
+ def list(args = {})
29
+ ret = [] unless block_given?
28
30
 
29
31
  @list_mutex.synchronize do
30
- q_tables = @db.select("sqlite_master", {"type" => "table"}, {orderby: "name"}) do |d_tables|
31
- next if d_tables[:name] == "sqlite_sequence"
32
+ tables_args = {type: "table"}
33
+ tables_args[:name] = args.fetch(:name) if args[:name]
34
+
35
+ q_tables = @db.select("sqlite_master", tables_args, orderby: "name") do |d_tables|
36
+ table_name = d_tables.fetch(:name)
37
+ next if table_name == "sqlite_sequence"
32
38
 
33
- tname = d_tables[:name].to_sym
34
- obj = @list.get(tname)
39
+ obj = @list.get(table_name)
35
40
 
36
41
  unless obj
37
42
  obj = Baza::Driver::Sqlite3::Table.new(
@@ -39,25 +44,25 @@ class Baza::Driver::Sqlite3::Tables
39
44
  data: d_tables,
40
45
  tables: self
41
46
  )
42
- @list[tname] = obj
47
+ @list[table_name] = obj
43
48
  end
44
49
 
45
50
  if block_given?
46
- yield(obj)
51
+ yield obj
47
52
  else
48
- ret[tname] = obj
53
+ ret << obj
49
54
  end
50
55
  end
51
56
  end
52
57
 
53
58
  if block_given?
54
- return nil
59
+ nil
55
60
  else
56
- return ret
61
+ ret
57
62
  end
58
63
  end
59
64
 
60
- def exists_in_list? table
65
+ def exists_in_list?(table)
61
66
  @list.key?(table.name)
62
67
  end
63
68
 
@@ -73,15 +78,17 @@ class Baza::Driver::Sqlite3::Tables
73
78
 
74
79
  CREATE_ALLOWED_KEYS = [:indexes, :columns]
75
80
  def create(name, data, args = nil)
76
- data.each do |key, val|
81
+ data.each_key do |key|
77
82
  raise "Invalid key: '#{key}' (#{key.class.name})." unless CREATE_ALLOWED_KEYS.include?(key)
78
83
  end
79
84
 
85
+ raise "No columns given" if data.fetch(:columns).empty?
86
+
80
87
  sql = "CREATE TABLE `#{name}` ("
81
88
 
82
89
  first = true
83
- data[:columns].each do |col_data|
84
- sql << ", " if !first
90
+ data.fetch(:columns).each do |col_data|
91
+ sql << ", " unless first
85
92
  first = false if first
86
93
  sql << @db.cols.data_sql(col_data)
87
94
  end
@@ -94,13 +101,13 @@ class Baza::Driver::Sqlite3::Tables
94
101
  @db.query(sql)
95
102
  end
96
103
 
97
- if data.key?(:indexes) && data[:indexes]
104
+ if data[:indexes]
98
105
  table_obj = self[name]
99
106
 
100
107
  if args && args[:return_sql]
101
- ret += table_obj.create_indexes(data[:indexes], return_sql: true)
108
+ ret += table_obj.create_indexes(data.fetch(:indexes), return_sql: true)
102
109
  else
103
- table_obj.create_indexes(data[:indexes])
110
+ table_obj.create_indexes(data.fetch(:indexes))
104
111
  end
105
112
  end
106
113
 
@@ -1,8 +1,8 @@
1
1
  class Baza::Driver::Sqlite3::UnbufferedResult
2
- def initialize(driver, statement)
2
+ def initialize(_driver, statement)
3
3
  @statement = statement
4
4
  @statement.execute
5
- @columns = statement.columns.map { |column| column.to_sym }
5
+ @columns = statement.columns.map(&:to_sym)
6
6
  end
7
7
 
8
8
  def fetch
@@ -1,7 +1,9 @@
1
- #This class handels SQLite3-specific behaviour.
1
+ # This class handels SQLite3-specific behaviour.
2
2
  class Baza::Driver::Sqlite3 < Baza::BaseSqlDriver
3
3
  path = "#{File.dirname(__FILE__)}/sqlite3"
4
4
 
5
+ autoload :Database, "#{path}/database"
6
+ autoload :Databases, "#{path}/databases"
5
7
  autoload :Table, "#{path}/table"
6
8
  autoload :Tables, "#{path}/tables"
7
9
  autoload :Column, "#{path}/column"
@@ -14,7 +16,14 @@ class Baza::Driver::Sqlite3 < Baza::BaseSqlDriver
14
16
 
15
17
  attr_reader :mutex_statement_reader
16
18
 
17
- #Helper to enable automatic registering of database using Baza::Db.from_object
19
+ def self.args
20
+ [{
21
+ label: "Path",
22
+ name: "path"
23
+ }]
24
+ end
25
+
26
+ # Helper to enable automatic registering of database using Baza::Db.from_object
18
27
  def self.from_object(args)
19
28
  if args[:object].class.name == "SQLite3::Database"
20
29
  return {
@@ -27,7 +36,7 @@ class Baza::Driver::Sqlite3 < Baza::BaseSqlDriver
27
36
  end
28
37
  end
29
38
 
30
- #Constructor. This should not be called manually.
39
+ # Constructor. This should not be called manually.
31
40
  def initialize(baza_db)
32
41
  super
33
42
 
@@ -38,14 +47,14 @@ class Baza::Driver::Sqlite3 < Baza::BaseSqlDriver
38
47
  @conn = @baza.opts[:conn]
39
48
  else
40
49
  raise "No path was given." unless @path
41
- require 'sqlite3' unless ::Object.const_defined?(:SQLite3)
50
+ require "sqlite3" unless ::Object.const_defined?(:SQLite3)
42
51
 
43
52
  @conn = ::SQLite3::Database.open(@path)
44
53
  @conn.type_translation = false # Type translation is always done in the C ext for SQLite3
45
54
  end
46
55
  end
47
56
 
48
- #Executes a query against the driver.
57
+ # Executes a query against the driver.
49
58
  def query(sql)
50
59
  @mutex_statement_reader.synchronize do
51
60
  return Baza::Driver::Sqlite3::Result.new(self, @conn.prepare(sql))
@@ -53,29 +62,33 @@ class Baza::Driver::Sqlite3 < Baza::BaseSqlDriver
53
62
  end
54
63
 
55
64
  def query_ubuf(sql)
56
- return Baza::Driver::Sqlite3::UnbufferedResult.new(self, @conn.prepare(sql))
65
+ Baza::Driver::Sqlite3::UnbufferedResult.new(self, @conn.prepare(sql))
57
66
  end
58
67
 
59
- #Escapes a string to be safe to used in a query.
68
+ # Escapes a string to be safe to used in a query.
60
69
  def escape(string)
61
- #This code is taken directly from the documentation so we dont have to rely on the SQLite3::Database class. This way it can also be used with JRuby and IronRuby...
62
- #http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html
63
- return string.to_s.gsub(/'/, "''")
70
+ # This code is taken directly from the documentation so we dont have to rely on the SQLite3::Database class. This way it can also be used with JRuby and IronRuby...
71
+ # http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html
72
+ string.to_s.gsub(/'/, "''")
64
73
  end
65
74
 
66
- #Returns the last inserted ID.
75
+ # Returns the last inserted ID.
67
76
  def last_id
68
77
  return @conn.last_insert_row_id if @conn.respond_to?(:last_insert_row_id)
69
- return query("SELECT last_insert_rowid() AS id").fetch[:id].to_i
78
+ query("SELECT last_insert_rowid() AS id").fetch[:id].to_i
70
79
  end
71
80
 
72
- #Closes the connection to the database.
81
+ # Closes the connection to the database.
73
82
  def close
74
83
  @mutex_statement_reader.synchronize { @conn.close }
75
84
  end
76
85
 
77
- #Starts a transaction, yields the database and commits.
86
+ # Starts a transaction, yields the database and commits.
78
87
  def transaction
79
88
  @conn.transaction { yield @baza }
80
89
  end
90
+
91
+ def supports_multiple_databases?
92
+ false
93
+ end
81
94
  end
@@ -0,0 +1,2 @@
1
+ class Baza::Driver::Sqlite3java::Database < Baza::Database
2
+ end
@@ -1,5 +1,5 @@
1
1
  class Baza::Driver::Sqlite3Java::UnbufferedResult < Baza::ResultBase
2
- def initialize(driver, result_set)
2
+ def initialize(_driver, result_set)
3
3
  @result_set = result_set
4
4
 
5
5
  if @result_set
@@ -22,7 +22,7 @@ class Baza::Driver::Sqlite3Java::UnbufferedResult < Baza::ResultBase
22
22
  hash[@columns[count]] = @result_set.string(count + 1)
23
23
  end
24
24
 
25
- return hash
25
+ hash
26
26
  end
27
27
 
28
28
  def each