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
@@ -1,4 +1,4 @@
1
- #This class controls the results for the normal MySQL-driver.
1
+ # This class controls the results for the normal MySQL-driver.
2
2
  class Baza::Driver::Mysql::Result < Baza::ResultBase
3
3
  INT_TYPES = {
4
4
  ::Mysql::Field::TYPE_DECIMAL => true,
@@ -18,25 +18,25 @@ class Baza::Driver::Mysql::Result < Baza::ResultBase
18
18
  ::Mysql::Field::TYPE_DATE => true
19
19
  }
20
20
 
21
- #Constructor. This should not be called manually.
21
+ # Constructor. This should not be called manually.
22
22
  def initialize(driver, result)
23
23
  @driver = driver
24
24
  @result = result
25
25
  @mutex = Mutex.new
26
26
  @type_translation = driver.baza.opts[:type_translation]
27
27
 
28
- if @result
29
- @keys = []
30
- @types = [] if @type_translation
28
+ return unless @result
31
29
 
32
- @result.fetch_fields.each do |key|
33
- @keys << key.name.to_sym
34
- @types << key.type if @type_translation
35
- end
30
+ @keys = []
31
+ @types = [] if @type_translation
32
+
33
+ @result.fetch_fields.each do |key|
34
+ @keys << key.name.to_sym
35
+ @types << key.type if @type_translation
36
36
  end
37
37
  end
38
38
 
39
- #Returns a single result as a hash with symbols as keys.
39
+ # Returns a single result as a hash with symbols as keys.
40
40
  def fetch
41
41
  fetched = nil
42
42
  @mutex.synchronize do
@@ -51,10 +51,10 @@ class Baza::Driver::Mysql::Result < Baza::ResultBase
51
51
  end
52
52
  end
53
53
 
54
- return Hash[*@keys.zip(fetched).flatten]
54
+ Hash[*@keys.zip(fetched).flatten]
55
55
  end
56
56
 
57
- #Loops over every result yielding it.
57
+ # Loops over every result yielding it.
58
58
  def each
59
59
  while data = fetch
60
60
  yield data
@@ -64,18 +64,18 @@ class Baza::Driver::Mysql::Result < Baza::ResultBase
64
64
  private
65
65
 
66
66
  def translate_value_to_type(value, type_no)
67
- unless value === nil
68
- if INT_TYPES[type_no]
69
- return value.to_i
70
- elsif FLOAT_TYPES[type_no]
71
- return value.to_f
72
- elsif TIME_TYPES[type_no]
73
- return Time.parse(value)
74
- elsif DATE_TYPES[type_no]
75
- return Date.parse(value)
76
- else
77
- return value.to_s
78
- end
67
+ return if value == nil
68
+
69
+ if INT_TYPES[type_no]
70
+ return value.to_i
71
+ elsif FLOAT_TYPES[type_no]
72
+ return value.to_f
73
+ elsif TIME_TYPES[type_no]
74
+ return Time.parse(value)
75
+ elsif DATE_TYPES[type_no]
76
+ return Date.parse(value)
77
+ else
78
+ return value.to_s
79
79
  end
80
80
  end
81
81
  end
@@ -1,5 +1,5 @@
1
1
  class Baza::Driver::Mysql::Sqlspecs < Baza::Sqlspecs
2
2
  def strftime(val, colstr)
3
- return "DATE_FORMAT(#{colstr}, '#{val}')"
3
+ "DATE_FORMAT(#{colstr}, '#{val}')"
4
4
  end
5
5
  end
@@ -2,71 +2,71 @@ class Baza::Driver::Mysql::Table < Baza::Table
2
2
  attr_reader :list, :name
3
3
 
4
4
  def initialize(args)
5
- @args = args
6
- @db = args[:db]
7
- @data = args[:data]
5
+ @db = args.fetch(:db)
6
+ @data = args.fetch(:data)
8
7
  @list = Wref::Map.new
9
8
  @indexes_list = Wref::Map.new
10
- @name = @data[:Name].to_sym
11
- @tables = args[:tables]
12
-
13
- raise "Could not figure out name from: '#{@data}'." if @data[:Name].to_s.strip.empty?
9
+ @name = @data.fetch(:name)
10
+ @tables = args.fetch(:tables)
14
11
  end
15
12
 
16
13
  def reload
17
- @data = @db.q("SHOW TABLE STATUS WHERE `Name` = '#{@db.esc(self.name)}'").fetch
14
+ data = @db.q("SHOW TABLE STATUS WHERE `Name` = '#{@db.esc(name)}'").fetch
15
+ raise Baza::Errors::TableNotFound unless data
16
+ @data = data
17
+ self
18
18
  end
19
19
 
20
- #Used to validate in Wref::Map.
20
+ # Used to validate in Wref::Map.
21
21
  def __object_unique_id__
22
- return @data[:Name]
22
+ name
23
23
  end
24
24
 
25
25
  def drop
26
- raise "Cant drop native table: '#{self.name}'." if self.native?
27
- @db.query("DROP TABLE `#{@db.esc_table(self.name)}`")
26
+ raise "Cant drop native table: '#{name}'." if self.native?
27
+ @db.query("DROP TABLE `#{@db.escape_table(name)}`")
28
28
  @tables.__send__(:remove_from_list, self)
29
- return nil
29
+ nil
30
30
  end
31
31
 
32
- #Returns true if the table is safe to drop.
32
+ # Returns true if the table is safe to drop.
33
33
  def native?
34
34
  data = @db.q("SELECT DATABASE() AS db").fetch
35
- return true if data[:db] == "mysql"
36
- return false
35
+ return true if data.fetch(:db) == "mysql"
36
+ false
37
37
  end
38
38
 
39
39
  def optimize
40
- @db.query("OPTIMIZE TABLE `#{@db.esc_table(self.name)}`")
41
- return self
40
+ @db.query("OPTIMIZE TABLE `#{@db.escape_table(name)}`")
41
+ self
42
42
  end
43
43
 
44
44
  def rows_count
45
- return @db.q("SELECT COUNT(*) AS count FROM `#{@db.esc_table(self.name)}`").fetch[:count].to_i
45
+ @db.query("SELECT COUNT(*) AS count FROM `#{@db.escape_table(name)}`").fetch.fetch(:count).to_i
46
46
  end
47
47
 
48
48
  def column(name)
49
- name = name.to_sym
49
+ name = name.to_s
50
50
 
51
51
  if col = @list.get(name)
52
52
  return @list[name]
53
53
  end
54
54
 
55
- self.columns(:name => name) do |col|
56
- return col if col.name == name
55
+ columns(name: name) do |col_i|
56
+ return col_i if col_i.name == name
57
57
  end
58
58
 
59
- raise Errno::ENOENT, "Column not found: '#{name}'."
59
+ raise Baza::Errors::ColumnNotFound, "Column not found: '#{name}'"
60
60
  end
61
61
 
62
62
  def columns(args = nil)
63
63
  @db.cols
64
- ret = {}
65
- sql = "SHOW FULL COLUMNS FROM `#{@db.esc_table(name)}`"
66
- sql << " WHERE `Field` = '#{@db.esc(args[:name])}'" if args && args.key?(:name)
64
+ ret = []
65
+ sql = "SHOW FULL COLUMNS FROM `#{@db.escape_table(name)}`"
66
+ sql << " WHERE `Field` = '#{@db.esc(args.fetch(:name))}'" if args && args.key?(:name)
67
67
 
68
68
  @db.q(sql) do |d_cols|
69
- column_name = d_cols[:Field].to_sym
69
+ column_name = d_cols.fetch(:Field)
70
70
  obj = @list.get(name)
71
71
 
72
72
  unless obj
@@ -79,9 +79,9 @@ class Baza::Driver::Mysql::Table < Baza::Table
79
79
  end
80
80
 
81
81
  if block_given?
82
- yield(obj)
82
+ yield obj
83
83
  else
84
- ret[column_name] = obj
84
+ ret << obj
85
85
  end
86
86
  end
87
87
 
@@ -94,14 +94,15 @@ class Baza::Driver::Mysql::Table < Baza::Table
94
94
 
95
95
  def indexes(args = nil)
96
96
  @db.indexes
97
- ret = {}
97
+ ret = []
98
98
 
99
- sql = "SHOW INDEX FROM `#{@db.esc_table(name)}`"
100
- sql << " WHERE `Key_name` = '#{@db.esc(args[:name])}'" if args && args.key?(:name)
99
+ sql = "SHOW INDEX FROM `#{@db.escape_table(name)}`"
100
+ sql << " WHERE `Key_name` = '#{@db.esc(args.fetch(:name))}'" if args && args.key?(:name)
101
101
 
102
- @db.q(sql) do |d_indexes|
102
+ @db.query(sql) do |d_indexes|
103
103
  next if d_indexes[:Key_name] == "PRIMARY"
104
- obj = @indexes_list.get(d_indexes[:Key_name].to_s)
104
+ index_name = d_indexes.fetch(:Key_name)
105
+ obj = @indexes_list.get(index_name)
105
106
 
106
107
  unless obj
107
108
  obj = Baza::Driver::Mysql::Index.new(
@@ -109,14 +110,14 @@ class Baza::Driver::Mysql::Table < Baza::Table
109
110
  db: @db,
110
111
  data: d_indexes
111
112
  )
112
- obj.columns << d_indexes[:Column_name]
113
- @indexes_list[d_indexes[:Key_name].to_s] = obj
113
+ obj.columns << d_indexes.fetch(:Column_name)
114
+ @indexes_list[index_name] = obj
114
115
  end
115
116
 
116
117
  if block_given?
117
118
  yield obj
118
119
  else
119
- ret[d_indexes[:Key_name].to_s] = obj
120
+ ret << obj
120
121
  end
121
122
  end
122
123
 
@@ -134,24 +135,24 @@ class Baza::Driver::Mysql::Table < Baza::Table
134
135
  return index
135
136
  end
136
137
 
137
- indexes(name: name) do |index|
138
- return index if index.name.to_s == name
138
+ indexes(name: name) do |index_i|
139
+ return index_i if index_i.name == name
139
140
  end
140
141
 
141
- raise Errno::ENOENT, "Index not found: #{name}."
142
+ raise Baza::Errors::IndexNotFound, "Index not found: #{name}."
142
143
  end
143
144
 
144
145
  def create_columns(col_arr)
145
146
  @db.transaction do
146
147
  col_arr.each do |col_data|
147
- sql = "ALTER TABLE `#{self.name}` ADD COLUMN #{@db.cols.data_sql(col_data)};"
148
+ sql = "ALTER TABLE `#{name}` ADD COLUMN #{@db.cols.data_sql(col_data)};"
148
149
  @db.query(sql)
149
150
  end
150
151
  end
151
152
  end
152
153
 
153
154
  def create_indexes(index_arr, args = {})
154
- return Baza::Driver::Mysql::Table.create_indexes(index_arr, args.merge(:table_name => self.name, :db => @db))
155
+ Baza::Driver::Mysql::Table.create_indexes(index_arr, args.merge(table_name: name, db: @db))
155
156
  end
156
157
 
157
158
  def self.create_indexes(index_arr, args = {})
@@ -163,20 +164,16 @@ class Baza::Driver::Mysql::Table < Baza::Table
163
164
  end
164
165
 
165
166
  index_arr.each do |index_data|
166
- if !args[:return_sql]
167
- sql = ""
168
- end
167
+ sql = "" unless args[:return_sql]
169
168
 
170
- if args[:create] || !args.key?(:create)
171
- sql << "CREATE"
172
- end
169
+ sql << "CREATE" if args[:create] || !args.key?(:create)
173
170
 
174
171
  if index_data.is_a?(String) || index_data.is_a?(Symbol)
175
172
  index_data = {name: index_data, columns: [index_data]}
176
173
  end
177
174
 
178
175
  raise "No name was given: '#{index_data}'." if !index_data.key?(:name) || index_data[:name].to_s.strip.empty?
179
- raise "No columns was given on index: '#{index_data[:name]}'." if !index_data[:columns] || index_data[:columns].empty?
176
+ raise "No columns was given on index: '#{index_data.fetch(:name)}'." if !index_data[:columns] || index_data[:columns].empty?
180
177
 
181
178
  if args[:return_sql]
182
179
  if first
@@ -187,87 +184,81 @@ class Baza::Driver::Mysql::Table < Baza::Table
187
184
  end
188
185
 
189
186
  sql << " UNIQUE" if index_data[:unique]
190
- sql << " INDEX `#{db.esc_col(index_data[:name])}`"
187
+ sql << " INDEX `#{db.escape_column(index_data.fetch(:name))}`"
191
188
 
192
189
  if args[:on_table] || !args.key?(:on_table)
193
- sql << " ON `#{db.esc_table(args[:table_name])}`"
190
+ sql << " ON `#{db.escape_table(args.fetch(:table_name))}`"
194
191
  end
195
192
 
196
193
  sql << " ("
197
194
 
198
195
  first = true
199
196
  index_data[:columns].each do |col_name|
200
- sql << ", " if !first
197
+ sql << ", " unless first
201
198
  first = false if first
202
199
 
203
- sql << "`#{db.esc_col(col_name)}`"
200
+ sql << "`#{db.escape_column(col_name)}`"
204
201
  end
205
202
 
206
203
  sql << ")"
207
204
 
208
- if !args[:return_sql]
209
- db.query(sql)
210
- end
205
+ db.query(sql) unless args[:return_sql]
211
206
  end
212
207
 
213
- if args[:return_sql]
214
- return sql
215
- else
216
- return nil
217
- end
208
+ sql if args[:return_sql]
218
209
  end
219
210
 
220
211
  def rename(newname)
221
- newname = newname.to_sym
212
+ newname = newname.to_s
222
213
  oldname = name
223
214
 
224
215
  @tables.__send__(:remove_from_list, self)
225
- @db.query("ALTER TABLE `#{@db.esc_table(oldname)}` RENAME TO `#{@db.esc_table(newname)}`")
216
+ @db.query("ALTER TABLE `#{@db.escape_table(oldname)}` RENAME TO `#{@db.escape_table(newname)}`")
226
217
 
227
- @data[:Name] = newname
218
+ @data[:name] = newname
228
219
  @name = newname
229
220
  @tables.__send__(:add_to_list, self)
230
221
 
231
- @list.each do |name, column|
222
+ @list.each do |_name, column|
232
223
  column.args[:table_name] = newname
233
224
  end
234
225
 
235
- @indexes_list.each do |name, index|
236
- index.args[:table_name] = newname
226
+ @indexes_list.each do |_name, index|
227
+ index.table_name = newname
237
228
  end
238
229
  end
239
230
 
240
231
  def truncate
241
- @db.query("TRUNCATE `#{@db.esc_table(self.name)}`")
242
- return self
232
+ @db.query("TRUNCATE `#{@db.escape_table(name)}`")
233
+ self
243
234
  end
244
235
 
245
236
  def data
246
237
  ret = {
247
- name: self.name,
238
+ name: name,
248
239
  columns: [],
249
240
  indexes: []
250
241
  }
251
242
 
252
- columns.each do |name, column|
243
+ columns do |column|
253
244
  ret[:columns] << column.data
254
245
  end
255
246
 
256
- indexes.each do |name, index|
257
- ret[:indexes] << index.data if name != "PRIMARY"
247
+ indexes do |index|
248
+ ret[:indexes] << index.data unless index.name == "PRIMARY"
258
249
  end
259
250
 
260
- return ret
251
+ ret
261
252
  end
262
253
 
263
254
  def insert(data)
264
- @db.insert(self.name, data)
255
+ @db.insert(name, data)
265
256
  end
266
257
 
267
258
  def clone(newname, args = {})
268
259
  raise "Invalid name." if newname.to_s.strip.empty?
269
260
 
270
- sql = "CREATE TABLE `#{@db.esc_table(newname)}` ("
261
+ sql = "CREATE TABLE `#{@db.escape_table(newname)}` ("
271
262
  first = true
272
263
  pkey_found = false
273
264
  pkeys = []
@@ -288,9 +279,7 @@ class Baza::Driver::Mysql::Table < Baza::Table
288
279
  col_data.delete(:primarykey)
289
280
  end
290
281
 
291
- if args[:all_cols_storage]
292
- col_data[:storage] = args[:all_cols_storage]
293
- end
282
+ col_data[:storage] = args[:all_cols_storage] if args[:all_cols_storage]
294
283
 
295
284
  sql << @db.cols.data_sql(col_data)
296
285
  end
@@ -302,7 +291,7 @@ class Baza::Driver::Mysql::Table < Baza::Table
302
291
  pkeys.each do |pkey|
303
292
  sql << ", " unless first
304
293
  first = false if first
305
- sql << "`#{@db.esc_col(pkey)}`"
294
+ sql << "`#{@db.escape_column(pkey)}`"
306
295
  end
307
296
 
308
297
  sql << ")"
@@ -313,15 +302,15 @@ class Baza::Driver::Mysql::Table < Baza::Table
313
302
  sql << " ENGINE=#{args[:engine]}" if args[:engine]
314
303
  sql << ";"
315
304
 
316
- #Create table.
305
+ # Create table.
317
306
  @db.query(sql)
318
307
 
319
308
 
320
- #Insert data of previous data in a single query.
321
- @db.query("INSERT INTO `#{@db.esc_table(newname)}` SELECT * FROM `#{@db.esc_table(self.name)}`")
309
+ # Insert data of previous data in a single query.
310
+ @db.query("INSERT INTO `#{@db.escape_table(newname)}` SELECT * FROM `#{@db.escape_table(name)}`")
322
311
 
323
312
 
324
- #Create indexes.
313
+ # Create indexes.
325
314
  new_table = @db.tables[newname]
326
315
  indexes_list = []
327
316
  indexes do |index|
@@ -331,20 +320,20 @@ class Baza::Driver::Mysql::Table < Baza::Table
331
320
  new_table.create_indexes(indexes_list)
332
321
 
333
322
 
334
- #Return new table.
335
- return new_table
323
+ # Return new table.
324
+ new_table
336
325
  end
337
326
 
338
- #Returns the current engine of the table.
327
+ # Returns the current engine of the table.
339
328
  def engine
340
- return @data[:Engine]
329
+ @data[:engine]
341
330
  end
342
331
 
343
- #Changes the engine for a table.
332
+ # Changes the engine for a table.
344
333
  def engine=(newengine)
345
334
  raise "Invalid engine: '#{newengine}'." unless newengine.to_s.match(/^[A-z]+$/)
346
- @db.query("ALTER TABLE `#{@db.esc_table(self.name)}` ENGINE = #{newengine}") if self.engine.to_s != newengine.to_s
347
- @data[:Engine] = newengine
335
+ @db.query("ALTER TABLE `#{@db.escape_table(name)}` ENGINE = #{newengine}") if engine.to_s != newengine.to_s
336
+ @data[:engine] = newengine
348
337
  end
349
338
 
350
339
  private
@@ -1,10 +1,10 @@
1
1
  require "monitor"
2
2
 
3
- #This class handels various MySQL-table-specific behaviour.
3
+ # This class handels various MySQL-table-specific behaviour.
4
4
  class Baza::Driver::Mysql::Tables
5
5
  attr_reader :db, :list
6
6
 
7
- #Constructor. This should not be called manually.
7
+ # Constructor. This should not be called manually.
8
8
  def initialize(args)
9
9
  @args = args
10
10
  @db = @args[:db]
@@ -13,57 +13,57 @@ class Baza::Driver::Mysql::Tables
13
13
  @list_should_be_reloaded = true
14
14
  end
15
15
 
16
- #Cleans the wref-map.
16
+ # Cleans the wref-map.
17
17
  def clean
18
18
  @list.clean
19
19
  end
20
20
 
21
- #Returns a table by the given table-name.
21
+ # Returns a table by the given table-name.
22
22
  def [](table_name)
23
- table_name = table_name.to_sym
23
+ table_name = table_name.to_s
24
24
 
25
25
  if table = @list[table_name]
26
26
  return table
27
27
  end
28
28
 
29
- tables = []
30
- list(name: table_name) do |table_obj|
31
- return table_obj if table_obj.name == table_name
29
+ list(name: table_name) do |table_i|
30
+ return table_i if table_i.name == table_name
32
31
  end
33
32
 
34
- list do |table_obj|
35
- tables << table_obj.name
36
- end
37
-
38
- raise Errno::ENOENT, "Table was not found: '#{table_name}' (#{table_name.class.name}) (tables: #{tables})."
33
+ raise Baza::Errors::TableNotFound, "Table was not found: '#{table_name}'"
39
34
  end
40
35
 
41
- #Yields the tables of the current database.
36
+ # Yields the tables of the current database.
42
37
  def list(args = {})
43
- ret = {} unless block_given?
38
+ ret = [] unless block_given?
39
+
40
+ where_args = {}
41
+ where_args["TABLE_NAME"] = args.fetch(:name) if args[:name]
44
42
 
45
- sql = "SHOW TABLE STATUS"
46
- sql << " WHERE `Name` = '#{@db.esc(args[:name])}'" if args[:name]
43
+ if args[:database]
44
+ where_args["TABLE_SCHEMA"] = args.fetch(:database)
45
+ else
46
+ where_args["TABLE_SCHEMA"] = @db.opts.fetch(:db)
47
+ end
47
48
 
48
49
  @list_mutex.synchronize do
49
- @db.q(sql) do |d_tables|
50
- raise "No name was given from: #{d_tables}" unless d_tables.is_a?(Hash) && d_tables[:Name]
51
- name = d_tables[:Name].to_sym
50
+ @db.select([:information_schema, :tables], where_args) do |d_tables|
51
+ name = d_tables.fetch(:TABLE_NAME)
52
52
  obj = @list.get(name)
53
53
 
54
54
  unless obj
55
55
  obj = Baza::Driver::Mysql::Table.new(
56
56
  db: @db,
57
- data: d_tables,
57
+ data: {name: name, engine: d_tables.fetch(:ENGINE)},
58
58
  tables: self
59
59
  )
60
60
  @list[name] = obj
61
61
  end
62
62
 
63
63
  if block_given?
64
- yield(obj)
64
+ yield obj
65
65
  else
66
- ret[name] = obj
66
+ ret << obj
67
67
  end
68
68
  end
69
69
  end
@@ -76,7 +76,7 @@ class Baza::Driver::Mysql::Tables
76
76
  end
77
77
 
78
78
  CREATE_ALLOWED_KEYS = [:columns, :indexes, :temp, :return_sql]
79
- #Creates a new table by the given name and data.
79
+ # Creates a new table by the given name and data.
80
80
  def create(name, data, args = nil)
81
81
  raise "No columns was given for '#{name}'." if !data[:columns] || data[:columns].empty?
82
82
 
@@ -94,13 +94,11 @@ class Baza::Driver::Mysql::Tables
94
94
 
95
95
  if data[:indexes] && !data[:indexes].empty?
96
96
  sql << ", "
97
- sql << Baza::Driver::Mysql::Table.create_indexes(data[:indexes], {
98
- db: @db,
99
- return_sql: true,
100
- create: false,
101
- on_table: false,
102
- table_name: name
103
- })
97
+ sql << Baza::Driver::Mysql::Table.create_indexes(data[:indexes], db: @db,
98
+ return_sql: true,
99
+ create: false,
100
+ on_table: false,
101
+ table_name: name)
104
102
  end
105
103
 
106
104
  sql << ")"
@@ -109,7 +107,7 @@ class Baza::Driver::Mysql::Tables
109
107
  @db.query(sql)
110
108
  end
111
109
 
112
- private
110
+ private
113
111
 
114
112
  def add_to_list(table)
115
113
  raise "Already exists: '#{table.name}'." if @list.key?(table.name) && @list[table.name].__id__ != table.__id__
@@ -1,6 +1,6 @@
1
- #This class controls the unbuffered result for the normal MySQL-driver.
1
+ # This class controls the unbuffered result for the normal MySQL-driver.
2
2
  class Baza::Driver::Mysql::UnbufferedResult < Baza::ResultBase
3
- #Constructor. This should not be called manually.
3
+ # Constructor. This should not be called manually.
4
4
  def initialize(conn, opts, result)
5
5
  @conn = conn
6
6
  @result = result
@@ -14,7 +14,7 @@ class Baza::Driver::Mysql::UnbufferedResult < Baza::ResultBase
14
14
  end
15
15
  end
16
16
 
17
- #Returns a single result.
17
+ # Returns a single result.
18
18
  def fetch
19
19
  if @enum
20
20
  begin
@@ -48,7 +48,7 @@ class Baza::Driver::Mysql::UnbufferedResult < Baza::ResultBase
48
48
  end
49
49
  end
50
50
 
51
- #Loops over every single result yielding it.
51
+ # Loops over every single result yielding it.
52
52
  def each
53
53
  while row = fetch
54
54
  yield row
@@ -57,7 +57,7 @@ class Baza::Driver::Mysql::UnbufferedResult < Baza::ResultBase
57
57
 
58
58
  private
59
59
 
60
- #Lods the keys for the object.
60
+ # Lods the keys for the object.
61
61
  def load_keys
62
62
  @keys = []
63
63
  keys = @res.fetch_fields