baza 0.0.20 → 0.0.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +84 -0
  3. data/.rubocop_todo.yml +17 -135
  4. data/.travis.yml +21 -0
  5. data/Gemfile +10 -7
  6. data/Gemfile.lock +39 -44
  7. data/README.md +61 -3
  8. data/VERSION +1 -1
  9. data/baza.gemspec +146 -98
  10. data/config/best_project_practice_rubocop.yml +8 -0
  11. data/config/best_project_practice_rubocop_todo.yml +6 -0
  12. data/lib/baza.rb +8 -12
  13. data/lib/baza/base_sql_driver.rb +198 -52
  14. data/lib/baza/cloner.rb +1 -0
  15. data/lib/baza/column.rb +26 -0
  16. data/lib/baza/database.rb +19 -3
  17. data/lib/baza/db.rb +69 -271
  18. data/lib/baza/driver.rb +1 -6
  19. data/lib/baza/{drivers → driver}/active_record.rb +65 -21
  20. data/lib/baza/{drivers → driver}/active_record/columns.rb +0 -0
  21. data/lib/baza/driver/active_record/commands.rb +10 -0
  22. data/lib/baza/driver/active_record/databases.rb +10 -0
  23. data/lib/baza/{drivers → driver}/active_record/indexes.rb +0 -0
  24. data/lib/baza/{drivers → driver}/active_record/result.rb +3 -1
  25. data/lib/baza/{drivers → driver}/active_record/tables.rb +0 -0
  26. data/lib/baza/driver/active_record/users.rb +12 -0
  27. data/lib/baza/{drivers → driver}/mysql.rb +9 -26
  28. data/lib/baza/{drivers → driver}/mysql/column.rb +14 -35
  29. data/lib/baza/{drivers → driver}/mysql/columns.rb +9 -12
  30. data/lib/baza/driver/mysql/commands.rb +39 -0
  31. data/lib/baza/driver/mysql/database.rb +64 -0
  32. data/lib/baza/driver/mysql/databases.rb +63 -0
  33. data/lib/baza/{drivers → driver}/mysql/index.rb +0 -0
  34. data/lib/baza/{drivers → driver}/mysql/indexes.rb +0 -0
  35. data/lib/baza/{drivers → driver}/mysql/result.rb +15 -7
  36. data/lib/baza/{drivers → driver}/mysql/sqlspecs.rb +0 -0
  37. data/lib/baza/{drivers → driver}/mysql/table.rb +27 -43
  38. data/lib/baza/{drivers → driver}/mysql/tables.rb +5 -34
  39. data/lib/baza/{drivers → driver}/mysql/unbuffered_result.rb +8 -2
  40. data/lib/baza/driver/mysql/user.rb +22 -0
  41. data/lib/baza/driver/mysql/users.rb +39 -0
  42. data/lib/baza/{drivers → driver}/mysql2.rb +19 -49
  43. data/lib/baza/{drivers → driver}/mysql2/column.rb +0 -0
  44. data/lib/baza/{drivers → driver}/mysql2/columns.rb +0 -0
  45. data/lib/baza/driver/mysql2/commands.rb +2 -0
  46. data/lib/baza/{drivers → driver}/mysql2/database.rb +0 -0
  47. data/lib/baza/{drivers → driver}/mysql2/databases.rb +0 -0
  48. data/lib/baza/{drivers → driver}/mysql2/index.rb +0 -0
  49. data/lib/baza/{drivers → driver}/mysql2/indexes.rb +0 -0
  50. data/lib/baza/{drivers → driver}/mysql2/result.rb +3 -1
  51. data/lib/baza/{drivers → driver}/mysql2/table.rb +0 -0
  52. data/lib/baza/{drivers → driver}/mysql2/tables.rb +0 -0
  53. data/lib/baza/driver/mysql2/user.rb +2 -0
  54. data/lib/baza/driver/mysql2/users.rb +2 -0
  55. data/lib/baza/{drivers → driver}/mysql_java.rb +60 -38
  56. data/lib/baza/{drivers → driver}/mysql_java/column.rb +0 -0
  57. data/lib/baza/{drivers → driver}/mysql_java/columns.rb +0 -0
  58. data/lib/baza/driver/mysql_java/commands.rb +2 -0
  59. data/lib/baza/driver/mysql_java/database.rb +2 -0
  60. data/lib/baza/driver/mysql_java/databases.rb +2 -0
  61. data/lib/baza/{drivers → driver}/mysql_java/index.rb +0 -0
  62. data/lib/baza/{drivers → driver}/mysql_java/indexes.rb +0 -0
  63. data/lib/baza/{drivers → driver}/mysql_java/table.rb +0 -0
  64. data/lib/baza/{drivers → driver}/mysql_java/tables.rb +0 -0
  65. data/lib/baza/driver/mysql_java/user.rb +2 -0
  66. data/lib/baza/driver/mysql_java/users.rb +2 -0
  67. data/lib/baza/driver/pg.rb +80 -0
  68. data/lib/baza/driver/pg/column.rb +125 -0
  69. data/lib/baza/driver/pg/columns.rb +37 -0
  70. data/lib/baza/driver/pg/commands.rb +35 -0
  71. data/lib/baza/driver/pg/create_index_sql_creator.rb +51 -0
  72. data/lib/baza/driver/pg/database.rb +89 -0
  73. data/lib/baza/driver/pg/databases.rb +79 -0
  74. data/lib/baza/driver/pg/index.rb +35 -0
  75. data/lib/baza/driver/pg/indexes.rb +5 -0
  76. data/lib/baza/driver/pg/result.rb +139 -0
  77. data/lib/baza/driver/pg/table.rb +184 -0
  78. data/lib/baza/driver/pg/tables.rb +45 -0
  79. data/lib/baza/{drivers → driver}/sqlite3.rb +6 -24
  80. data/lib/baza/{drivers → driver}/sqlite3/column.rb +22 -24
  81. data/lib/baza/{drivers → driver}/sqlite3/columns.rb +6 -6
  82. data/lib/baza/driver/sqlite3/commands.rb +28 -0
  83. data/lib/baza/{drivers → driver}/sqlite3/database.rb +0 -0
  84. data/lib/baza/{drivers → driver}/sqlite3/databases.rb +0 -1
  85. data/lib/baza/{drivers → driver}/sqlite3/index.rb +0 -0
  86. data/lib/baza/{drivers → driver}/sqlite3/indexes.rb +0 -0
  87. data/lib/baza/{drivers → driver}/sqlite3/result.rb +14 -6
  88. data/lib/baza/{drivers → driver}/sqlite3/sqlspecs.rb +0 -0
  89. data/lib/baza/{drivers → driver}/sqlite3/table.rb +25 -16
  90. data/lib/baza/{drivers → driver}/sqlite3/tables.rb +5 -6
  91. data/lib/baza/{drivers → driver}/sqlite3/unbuffered_result.rb +8 -2
  92. data/lib/baza/{drivers → driver}/sqlite3_java.rb +13 -23
  93. data/lib/baza/{drivers → driver}/sqlite3_java/column.rb +0 -0
  94. data/lib/baza/{drivers → driver}/sqlite3_java/columns.rb +0 -0
  95. data/lib/baza/driver/sqlite3_java/commands.rb +2 -0
  96. data/lib/baza/{drivers → driver}/sqlite3_java/database.rb +0 -0
  97. data/lib/baza/{drivers → driver}/sqlite3_java/index.rb +0 -0
  98. data/lib/baza/{drivers → driver}/sqlite3_java/indexes.rb +0 -0
  99. data/lib/baza/{drivers → driver}/sqlite3_java/table.rb +0 -0
  100. data/lib/baza/{drivers → driver}/sqlite3_java/tables.rb +0 -0
  101. data/lib/baza/{drivers → driver}/sqlite3_java/unbuffered_result.rb +14 -9
  102. data/lib/baza/{drivers → driver}/sqlite3_rhodes.rb +6 -24
  103. data/lib/baza/errors.rb +2 -0
  104. data/lib/baza/idquery.rb +15 -8
  105. data/lib/baza/index.rb +7 -0
  106. data/lib/baza/jdbc_driver.rb +4 -16
  107. data/lib/baza/jdbc_result.rb +20 -12
  108. data/lib/baza/mysql_base_driver.rb +7 -7
  109. data/lib/baza/query_buffer.rb +20 -19
  110. data/lib/baza/row.rb +16 -16
  111. data/lib/baza/sql_queries.rb +3 -0
  112. data/lib/baza/sql_queries/generic_insert.rb +81 -0
  113. data/lib/baza/sql_queries/generic_update.rb +31 -0
  114. data/lib/baza/sql_queries/mysql_upsert.rb +52 -0
  115. data/lib/baza/sql_queries/mysql_upsert_duplicate_key.rb +57 -0
  116. data/lib/baza/sql_queries/non_atomic_upsert.rb +25 -0
  117. data/lib/baza/sql_queries/postgres_upsert_duplicate_key.rb +118 -0
  118. data/lib/baza/sql_queries/select.rb +170 -0
  119. data/lib/baza/sql_queries/sqlite_upsert_duplicate_key.rb +99 -0
  120. data/lib/baza/table.rb +35 -8
  121. data/spec/active_record/models/user.rb +3 -0
  122. data/spec/{cloner_spec.rb → baza/cloner_spec.rb} +0 -0
  123. data/spec/drivers/active_record_mysql2_spec.rb +5 -3
  124. data/spec/drivers/active_record_mysql_spec.rb +2 -1
  125. data/spec/drivers/active_record_pg_spec.rb +20 -0
  126. data/spec/drivers/active_record_sqlite3_spec.rb +2 -1
  127. data/spec/drivers/mysql2_spec.rb +1 -1
  128. data/spec/drivers/mysql_spec.rb +10 -10
  129. data/spec/drivers/pg_spec.rb +18 -0
  130. data/spec/drivers/sqlite3_spec.rb +7 -8
  131. data/spec/info_active_record_example.rb +1 -1
  132. data/spec/{info_active_record_mysql2.rb → info_active_record_mysql2_example.rb} +3 -2
  133. data/spec/info_active_record_mysql2_travis.rb +35 -0
  134. data/spec/{info_active_record_mysql.rb → info_active_record_mysql_example.rb} +5 -4
  135. data/spec/info_active_record_mysql_travis.rb +36 -0
  136. data/spec/info_active_record_pg_example.rb +36 -0
  137. data/spec/info_active_record_pg_travis.rb +34 -0
  138. data/spec/info_active_record_sqlite3.rb +1 -1
  139. data/spec/info_mysql2_example.rb +1 -3
  140. data/spec/{info_mysql2_shippable.rb → info_mysql2_travis.rb} +2 -4
  141. data/spec/info_mysql_example.rb +1 -3
  142. data/spec/{info_mysql_shippable.rb → info_mysql_travis.rb} +2 -4
  143. data/spec/info_pg_example.rb +22 -0
  144. data/spec/info_pg_travis.rb +20 -0
  145. data/spec/info_sqlite3.rb +1 -3
  146. data/spec/spec_helper.rb +1 -1
  147. data/spec/support/driver_active_record_collection.rb +62 -0
  148. data/spec/support/driver_collection.rb +136 -121
  149. data/spec/support/driver_columns_collection.rb +19 -10
  150. data/spec/support/driver_databases_collection.rb +23 -1
  151. data/spec/support/driver_indexes_collection.rb +2 -2
  152. data/spec/support/driver_tables_collection.rb +24 -4
  153. data/spec/support/driver_users_collection.rb +53 -0
  154. metadata +185 -104
  155. data/lib/baza/drivers/mysql/database.rb +0 -28
  156. data/lib/baza/drivers/mysql/databases.rb +0 -35
  157. data/lib/baza/drivers/mysql_java/database.rb +0 -2
  158. data/lib/baza/drivers/mysql_java/databases.rb +0 -2
  159. data/lib/baza/model.rb +0 -875
  160. data/lib/baza/model_custom.rb +0 -155
  161. data/lib/baza/model_handler.rb +0 -910
  162. data/lib/baza/model_handler_sqlhelper.rb +0 -484
  163. data/lib/baza/revision.rb +0 -383
  164. data/shippable.yml +0 -17
  165. data/spec/info_active_record_mysql2_shippable.rb +0 -34
  166. data/spec/info_active_record_mysql_shippable.rb +0 -34
  167. data/spec/model_handler_spec.rb +0 -431
@@ -0,0 +1,39 @@
1
+ class Baza::Driver::Mysql::Commands
2
+ def initialize(args)
3
+ @db = args.fetch(:db)
4
+ end
5
+
6
+ def upsert_duplicate_key(table_name, updates, terms, args = {})
7
+ Baza::SqlQueries::MysqlUpsertDuplicateKey.new(
8
+ db: @db,
9
+ table_name: table_name,
10
+ updates: updates,
11
+ terms: terms,
12
+ buffer: args[:buffer],
13
+ return_id: args[:return_id]
14
+ ).execute
15
+ end
16
+
17
+ def upsert(table_name, updates, terms, args = {})
18
+ if args[:buffer]
19
+ Baza::SqlQueries::NonAtomicUpsert.new(
20
+ db: @db,
21
+ table_name: table_name,
22
+ buffer: args[:buffer],
23
+ terms: terms,
24
+ updates: updates
25
+ ).execute
26
+ else
27
+ Baza::SqlQueries::MysqlUpsert.new(
28
+ db: @db,
29
+ table_name: table_name,
30
+ updates: updates,
31
+ terms: terms
32
+ ).execute
33
+ end
34
+ end
35
+
36
+ def last_id
37
+ @db.query("SELECT LAST_INSERT_ID() AS `id`").fetch.fetch(:id).to_i
38
+ end
39
+ end
@@ -0,0 +1,64 @@
1
+ class Baza::Driver::Mysql::Database < Baza::Database
2
+ def save!
3
+ rename(name) unless name.to_s == name_was
4
+ self
5
+ end
6
+
7
+ def drop
8
+ sql = "DROP DATABASE `#{@db.escape_database(name)}`"
9
+ @db.query(sql)
10
+ self
11
+ end
12
+
13
+ CREATE_ALLOWED_KEYS = [:columns, :indexes, :temp, :return_sql].freeze
14
+ # Creates a new table by the given name and data.
15
+ def create_table(name, data, args = nil)
16
+ raise "No columns was given for '#{name}'." if !data[:columns] || data[:columns].empty?
17
+
18
+ sql = "CREATE"
19
+ sql << " TEMPORARY" if data[:temp]
20
+ sql << " TABLE #{db.sep_table}#{@db.escape_table(name)}#{db.sep_table} ("
21
+
22
+ first = true
23
+ data[:columns].each do |col_data|
24
+ sql << ", " unless first
25
+ first = false if first
26
+ col_data.delete(:after) if col_data[:after]
27
+ sql << @db.columns.data_sql(col_data)
28
+ end
29
+
30
+ if data[:indexes] && !data[:indexes].empty?
31
+ sql << ", "
32
+ sql << Baza::Driver::Mysql::Table.create_indexes(
33
+ data[:indexes],
34
+ db: @db,
35
+ return_sql: true,
36
+ create: false,
37
+ on_table: false,
38
+ table_name: name
39
+ )
40
+ end
41
+
42
+ sql << ")"
43
+
44
+ return [sql] if args && args[:return_sql]
45
+
46
+ use { @db.query(sql) }
47
+ end
48
+
49
+ private
50
+
51
+ def rename(new_name)
52
+ new_name = new_name.to_s
53
+ @db.databases.create(name: new_name)
54
+
55
+ tables.each do |table|
56
+ @db.query("ALTER TABLE `#{@db.escape_database(name_was)}`.`#{@db.escape_table(table.name)}` RENAME `#{@db.escape_database(name)}`.`#{@db.escape_table(table.name)}`")
57
+ end
58
+
59
+ @db.query("DROP DATABASE `#{@db.escape_database(name_was)}`")
60
+
61
+ @name = new_name
62
+ @name_was = new_name
63
+ end
64
+ end
@@ -0,0 +1,63 @@
1
+ class Baza::Driver::Mysql::Databases
2
+ def initialize(args)
3
+ @db = args.fetch(:db)
4
+ end
5
+
6
+ def create(args)
7
+ sql = "CREATE DATABASE"
8
+ sql << " IF NOT EXISTS" if args[:if_not_exists]
9
+ sql << " #{@db.sep_database}#{@db.escape_table(args.fetch(:name))}#{@db.sep_database}"
10
+
11
+ @db.query(sql)
12
+ true
13
+ end
14
+
15
+ def [](name)
16
+ name = name.to_s
17
+ list.each do |database|
18
+ return database if database.name == name
19
+ end
20
+
21
+ raise Baza::Errors::DatabaseNotFound
22
+ end
23
+
24
+ def list
25
+ ArrayEnumerator.new do |yielder|
26
+ @db.query("SHOW DATABASES") do |data|
27
+ yielder << Baza::Driver::Mysql::Database.new(
28
+ name: data.fetch(:Database),
29
+ driver: self,
30
+ db: @db
31
+ )
32
+ end
33
+ end
34
+ end
35
+
36
+ def with_database(name)
37
+ if @db.opts[:db].to_s == name.to_s
38
+ yield if block_given?
39
+ return self
40
+ end
41
+
42
+ previous_db_name = @db.current_database_name
43
+ @db.query("USE #{@db.sep_database}#{@db.escape_database(name)}#{@db.sep_database}")
44
+
45
+ if block_given?
46
+ begin
47
+ yield
48
+ ensure
49
+ @db.query("USE #{@db.sep_database}#{@db.escape_database(previous_db_name)}#{@db.sep_database}")
50
+ end
51
+ end
52
+
53
+ self
54
+ end
55
+
56
+ def current_database_name
57
+ @db.query("SELECT DATABASE()").fetch.values.first
58
+ end
59
+
60
+ def current_database
61
+ @db.databases[current_database_name]
62
+ end
63
+ end
@@ -5,25 +5,25 @@ class Baza::Driver::Mysql::Result < Baza::ResultBase
5
5
  ::Mysql::Field::TYPE_TINY => true,
6
6
  ::Mysql::Field::TYPE_LONG => true,
7
7
  ::Mysql::Field::TYPE_YEAR => true
8
- }
8
+ }.freeze
9
9
  FLOAT_TYPES = {
10
10
  ::Mysql::Field::TYPE_DECIMAL => true,
11
11
  ::Mysql::Field::TYPE_FLOAT => true,
12
12
  ::Mysql::Field::TYPE_DOUBLE => true
13
- }
13
+ }.freeze
14
14
  TIME_TYPES = {
15
15
  ::Mysql::Field::TYPE_DATETIME => true
16
- }
16
+ }.freeze
17
17
  DATE_TYPES = {
18
18
  ::Mysql::Field::TYPE_DATE => true
19
- }
19
+ }.freeze
20
20
 
21
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
- @type_translation = driver.baza.opts[:type_translation]
26
+ @type_translation = driver.db.opts[:type_translation]
27
27
 
28
28
  return unless @result
29
29
 
@@ -49,6 +49,8 @@ class Baza::Driver::Mysql::Result < Baza::ResultBase
49
49
  fetched.collect!.with_index do |value, count|
50
50
  translate_value_to_type(value, @types[count])
51
51
  end
52
+ elsif @type_translation == :string
53
+ fetched.collect!(&:to_s)
52
54
  end
53
55
 
54
56
  Hash[*@keys.zip(fetched).flatten]
@@ -56,8 +58,14 @@ class Baza::Driver::Mysql::Result < Baza::ResultBase
56
58
 
57
59
  # Loops over every result yielding it.
58
60
  def each
59
- while data = fetch
60
- yield data
61
+ loop do
62
+ data = fetch
63
+
64
+ if data
65
+ yield data
66
+ else
67
+ break
68
+ end
61
69
  end
62
70
  end
63
71
 
@@ -6,25 +6,33 @@ class Baza::Driver::Mysql::Table < Baza::Table
6
6
  @data = args.fetch(:data)
7
7
  @list = Wref::Map.new
8
8
  @indexes_list = Wref::Map.new
9
- @name = @data.fetch(:name)
9
+ @name = @data.fetch(:TABLE_NAME)
10
10
  @tables = args.fetch(:tables)
11
11
  end
12
12
 
13
13
  def reload
14
- data = @db.q("SHOW TABLE STATUS WHERE `Name` = '#{@db.esc(name)}'").fetch
14
+ data = @db.single([:information_schema, :tables], "TABLE_SCHEMA" => database_name, "TABLE_NAME" => name)
15
15
  raise Baza::Errors::TableNotFound unless data
16
16
  @data = data
17
17
  self
18
18
  end
19
19
 
20
+ def database_name
21
+ @data.fetch(:TABLE_SCHEMA)
22
+ end
23
+
20
24
  # Used to validate in Wref::Map.
21
25
  def __object_unique_id__
22
26
  name
23
27
  end
24
28
 
25
29
  def drop
26
- raise "Cant drop native table: '#{name}'." if self.native?
27
- @db.query("DROP TABLE `#{@db.escape_table(name)}`")
30
+ raise "Cant drop native table: '#{name}'" if native?
31
+
32
+ @db.with_database(database_name) do
33
+ @db.query("DROP TABLE `#{@db.escape_table(name)}`")
34
+ end
35
+
28
36
  @tables.__send__(:remove_from_list, self)
29
37
  nil
30
38
  end
@@ -41,16 +49,11 @@ class Baza::Driver::Mysql::Table < Baza::Table
41
49
  self
42
50
  end
43
51
 
44
- def rows_count
45
- @db.query("SELECT COUNT(*) AS count FROM `#{@db.escape_table(name)}`").fetch.fetch(:count).to_i
46
- end
47
-
48
52
  def column(name)
49
53
  name = name.to_s
50
54
 
51
- if col = @list.get(name)
52
- return @list[name]
53
- end
55
+ col = @list.get(name)
56
+ return col if col
54
57
 
55
58
  columns(name: name) do |col_i|
56
59
  return col_i if col_i.name == name
@@ -60,7 +63,7 @@ class Baza::Driver::Mysql::Table < Baza::Table
60
63
  end
61
64
 
62
65
  def columns(args = nil)
63
- @db.cols
66
+ @db.columns
64
67
  ret = []
65
68
  sql = "SHOW FULL COLUMNS FROM `#{@db.escape_table(name)}`"
66
69
  sql << " WHERE `Field` = '#{@db.esc(args.fetch(:name))}'" if args && args.key?(:name)
@@ -131,9 +134,8 @@ class Baza::Driver::Mysql::Table < Baza::Table
131
134
  def index(name)
132
135
  name = name.to_s
133
136
 
134
- if index = @indexes_list.get(name)
135
- return index
136
- end
137
+ index = @indexes_list.get(name)
138
+ return index if index
137
139
 
138
140
  indexes(name: name) do |index_i|
139
141
  return index_i if index_i.name == name
@@ -142,15 +144,6 @@ class Baza::Driver::Mysql::Table < Baza::Table
142
144
  raise Baza::Errors::IndexNotFound, "Index not found: #{name}."
143
145
  end
144
146
 
145
- def create_columns(col_arr)
146
- @db.transaction do
147
- col_arr.each do |col_data|
148
- sql = "ALTER TABLE `#{name}` ADD COLUMN #{@db.cols.data_sql(col_data)};"
149
- @db.query(sql)
150
- end
151
- end
152
- end
153
-
154
147
  def create_indexes(index_arr, args = {})
155
148
  Baza::Driver::Mysql::Table.create_indexes(index_arr, args.merge(table_name: name, db: @db))
156
149
  end
@@ -184,10 +177,10 @@ class Baza::Driver::Mysql::Table < Baza::Table
184
177
  end
185
178
 
186
179
  sql << " UNIQUE" if index_data[:unique]
187
- sql << " INDEX `#{db.escape_column(index_data.fetch(:name))}`"
180
+ sql << " INDEX #{db.sep_index}#{db.escape_index(index_data.fetch(:name))}#{db.sep_index}"
188
181
 
189
182
  if args[:on_table] || !args.key?(:on_table)
190
- sql << " ON `#{db.escape_table(args.fetch(:table_name))}`"
183
+ sql << " ON #{db.sep_table}#{db.escape_table(args.fetch(:table_name))}#{db.sep_table}"
191
184
  end
192
185
 
193
186
  sql << " ("
@@ -197,7 +190,7 @@ class Baza::Driver::Mysql::Table < Baza::Table
197
190
  sql << ", " unless first
198
191
  first = false if first
199
192
 
200
- sql << "`#{db.escape_column(col_name)}`"
193
+ sql << "#{db.sep_col}#{db.escape_column(col_name)}#{db.sep_col}"
201
194
  end
202
195
 
203
196
  sql << ")"
@@ -228,11 +221,6 @@ class Baza::Driver::Mysql::Table < Baza::Table
228
221
  end
229
222
  end
230
223
 
231
- def truncate
232
- @db.query("TRUNCATE `#{@db.escape_table(name)}`")
233
- self
234
- end
235
-
236
224
  def data
237
225
  ret = {
238
226
  name: name,
@@ -251,14 +239,10 @@ class Baza::Driver::Mysql::Table < Baza::Table
251
239
  ret
252
240
  end
253
241
 
254
- def insert(data)
255
- @db.insert(name, data)
256
- end
257
-
258
242
  def clone(newname, args = {})
259
243
  raise "Invalid name." if newname.to_s.strip.empty?
260
244
 
261
- sql = "CREATE TABLE `#{@db.escape_table(newname)}` ("
245
+ sql = "CREATE TABLE #{@db.sep_table}#{@db.escape_table(newname)}#{@db.sep_table} ("
262
246
  first = true
263
247
  pkey_found = false
264
248
  pkeys = []
@@ -281,7 +265,7 @@ class Baza::Driver::Mysql::Table < Baza::Table
281
265
 
282
266
  col_data[:storage] = args[:all_cols_storage] if args[:all_cols_storage]
283
267
 
284
- sql << @db.cols.data_sql(col_data)
268
+ sql << @db.columns.data_sql(col_data)
285
269
  end
286
270
 
287
271
  unless pkeys.empty?
@@ -291,7 +275,7 @@ class Baza::Driver::Mysql::Table < Baza::Table
291
275
  pkeys.each do |pkey|
292
276
  sql << ", " unless first
293
277
  first = false if first
294
- sql << "`#{@db.escape_column(pkey)}`"
278
+ sql << "#{@db.sep_col}#{@db.escape_column(pkey)}#{@db.sep_col}"
295
279
  end
296
280
 
297
281
  sql << ")"
@@ -307,7 +291,7 @@ class Baza::Driver::Mysql::Table < Baza::Table
307
291
 
308
292
 
309
293
  # Insert data of previous data in a single query.
310
- @db.query("INSERT INTO `#{@db.escape_table(newname)}` SELECT * FROM `#{@db.escape_table(name)}`")
294
+ @db.query("INSERT INTO #{@db.sep_table}#{@db.escape_table(newname)}#{@db.sep_table} SELECT * FROM #{@db.sep_table}#{@db.escape_table(name)}#{@db.sep_table}")
311
295
 
312
296
 
313
297
  # Create indexes.
@@ -326,14 +310,14 @@ class Baza::Driver::Mysql::Table < Baza::Table
326
310
 
327
311
  # Returns the current engine of the table.
328
312
  def engine
329
- @data[:engine]
313
+ @data.fetch(:ENGINE)
330
314
  end
331
315
 
332
316
  # Changes the engine for a table.
333
317
  def engine=(newengine)
334
- raise "Invalid engine: '#{newengine}'." unless newengine.to_s.match(/^[A-z]+$/)
318
+ raise "Invalid engine: '#{newengine}'." unless newengine.to_s =~ /^[A-z]+$/
335
319
  @db.query("ALTER TABLE `#{@db.escape_table(name)}` ENGINE = #{newengine}") if engine.to_s != newengine.to_s
336
- @data[:engine] = newengine
320
+ @data[:ENGINE] = newengine
337
321
  end
338
322
 
339
323
  private
@@ -7,7 +7,7 @@ class Baza::Driver::Mysql::Tables
7
7
  # Constructor. This should not be called manually.
8
8
  def initialize(args)
9
9
  @args = args
10
- @db = @args[:db]
10
+ @db = @args.fetch(:db)
11
11
  @list_mutex = Monitor.new
12
12
  @list = Wref::Map.new
13
13
  @list_should_be_reloaded = true
@@ -22,9 +22,8 @@ class Baza::Driver::Mysql::Tables
22
22
  def [](table_name)
23
23
  table_name = table_name.to_s
24
24
 
25
- if table = @list[table_name]
26
- return table
27
- end
25
+ table = @list[table_name]
26
+ return table if table
28
27
 
29
28
  list(name: table_name) do |table_i|
30
29
  return table_i if table_i.name == table_name
@@ -54,7 +53,7 @@ class Baza::Driver::Mysql::Tables
54
53
  unless obj
55
54
  obj = Baza::Driver::Mysql::Table.new(
56
55
  db: @db,
57
- data: {name: name, engine: d_tables.fetch(:ENGINE)},
56
+ data: d_tables,
58
57
  tables: self
59
58
  )
60
59
  @list[name] = obj
@@ -75,36 +74,8 @@ class Baza::Driver::Mysql::Tables
75
74
  end
76
75
  end
77
76
 
78
- CREATE_ALLOWED_KEYS = [:columns, :indexes, :temp, :return_sql]
79
- # Creates a new table by the given name and data.
80
77
  def create(name, data, args = nil)
81
- raise "No columns was given for '#{name}'." if !data[:columns] || data[:columns].empty?
82
-
83
- sql = "CREATE"
84
- sql << " TEMPORARY" if data[:temp]
85
- sql << " TABLE `#{name}` ("
86
-
87
- first = true
88
- data[:columns].each do |col_data|
89
- sql << ", " unless first
90
- first = false if first
91
- col_data.delete(:after) if col_data[:after]
92
- sql << @db.cols.data_sql(col_data)
93
- end
94
-
95
- if data[:indexes] && !data[:indexes].empty?
96
- sql << ", "
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)
102
- end
103
-
104
- sql << ")"
105
-
106
- return [sql] if args && args[:return_sql]
107
- @db.query(sql)
78
+ @db.current_database.create_table(name, data, args)
108
79
  end
109
80
 
110
81
  private