baza 0.0.38 → 0.0.39

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +0 -14
  3. data/lib/baza/database.rb +1 -1
  4. data/lib/baza/db.rb +5 -4
  5. data/lib/baza/driver/active_record/columns.rb +2 -2
  6. data/lib/baza/driver/active_record/commands.rb +2 -2
  7. data/lib/baza/driver/active_record/databases.rb +2 -2
  8. data/lib/baza/driver/active_record/foreign_keys.rb +13 -0
  9. data/lib/baza/driver/active_record/indexes.rb +2 -2
  10. data/lib/baza/driver/active_record/tables.rb +3 -3
  11. data/lib/baza/driver/active_record/users.rb +2 -2
  12. data/lib/baza/driver/active_record.rb +3 -1
  13. data/lib/baza/driver/mysql/columns.rb +1 -1
  14. data/lib/baza/driver/mysql/database.rb +11 -38
  15. data/lib/baza/driver/mysql/foreign_key.rb +2 -0
  16. data/lib/baza/driver/mysql/foreign_keys.rb +13 -0
  17. data/lib/baza/driver/mysql/sql/column.rb +3 -2
  18. data/lib/baza/driver/mysql/sql/create_table.rb +11 -0
  19. data/lib/baza/driver/mysql/table.rb +43 -4
  20. data/lib/baza/driver/mysql/tables.rb +22 -9
  21. data/lib/baza/driver/mysql2/foreign_keys.rb +2 -0
  22. data/lib/baza/driver/mysql2.rb +6 -5
  23. data/lib/baza/driver/mysql_java.rb +2 -2
  24. data/lib/baza/driver/pg/columns.rb +1 -1
  25. data/lib/baza/driver/pg/create_index_sql_creator.rb +2 -5
  26. data/lib/baza/driver/pg/database.rb +2 -2
  27. data/lib/baza/driver/pg/foreign_key.rb +2 -0
  28. data/lib/baza/driver/pg/foreign_keys.rb +13 -0
  29. data/lib/baza/driver/pg/result.rb +5 -1
  30. data/lib/baza/driver/pg/table.rb +43 -0
  31. data/lib/baza/driver/pg/tables.rb +20 -13
  32. data/lib/baza/driver/sqlite3/columns.rb +1 -1
  33. data/lib/baza/driver/sqlite3/foreign_key.rb +14 -0
  34. data/lib/baza/driver/sqlite3/foreign_keys.rb +11 -0
  35. data/lib/baza/driver/sqlite3/table.rb +41 -6
  36. data/lib/baza/driver/sqlite3/tables.rb +15 -14
  37. data/lib/baza/driver/sqlite3.rb +1 -1
  38. data/lib/baza/driver/sqlite3_java.rb +1 -1
  39. data/lib/baza/driver/sqlite3_rhodes.rb +1 -1
  40. data/lib/baza/dump.rb +3 -2
  41. data/lib/baza/foreign_key.rb +1 -1
  42. data/lib/baza/jdbc_driver.rb +1 -1
  43. data/lib/baza/mysql_base_driver.rb +1 -1
  44. data/lib/baza/query_buffer.rb +36 -6
  45. data/lib/baza/result_base.rb +2 -5
  46. data/lib/baza/tables.rb +1 -1
  47. metadata +26 -269
  48. data/.document +0 -5
  49. data/.github/dependabot.yml +0 -13
  50. data/.rspec +0 -1
  51. data/.rubocop.yml +0 -84
  52. data/.rubocop_todo.yml +0 -39
  53. data/.ruby-version +0 -1
  54. data/Gemfile +0 -32
  55. data/Gemfile.lock +0 -164
  56. data/VERSION +0 -1
  57. data/baza.gemspec +0 -264
  58. data/config/best_project_practice_rubocop.yml +0 -10
  59. data/config/best_project_practice_rubocop_todo.yml +0 -163
  60. data/peak_flow.yml +0 -31
  61. data/spec/active_record/models/user.rb +0 -3
  62. data/spec/baza/cloner_spec.rb +0 -10
  63. data/spec/baza/sql_queries/generic_insert_spec.rb +0 -26
  64. data/spec/baza/sql_queries/select_spec.rb +0 -38
  65. data/spec/drivers/active_record_mysql2_spec.rb +0 -22
  66. data/spec/drivers/active_record_mysql_spec.rb +0 -19
  67. data/spec/drivers/active_record_pg_spec.rb +0 -22
  68. data/spec/drivers/active_record_sqlite3_spec.rb +0 -18
  69. data/spec/drivers/mysql2_spec.rb +0 -19
  70. data/spec/drivers/mysql_spec.rb +0 -58
  71. data/spec/drivers/pg/columns_spec.rb +0 -45
  72. data/spec/drivers/pg/table_spec.rb +0 -20
  73. data/spec/drivers/pg_spec.rb +0 -20
  74. data/spec/drivers/sqlite3_spec.rb +0 -67
  75. data/spec/drivers/tiny_spec.rb +0 -27
  76. data/spec/info_active_record_example.rb +0 -35
  77. data/spec/info_active_record_mysql2_example.rb +0 -36
  78. data/spec/info_active_record_mysql2_peak_flow.rb +0 -36
  79. data/spec/info_active_record_mysql_example.rb +0 -37
  80. data/spec/info_active_record_mysql_peak_flow.rb +0 -37
  81. data/spec/info_active_record_pg_example.rb +0 -36
  82. data/spec/info_active_record_pg_peak_flow.rb +0 -36
  83. data/spec/info_active_record_sqlite3.rb +0 -29
  84. data/spec/info_mysql2_example.rb +0 -21
  85. data/spec/info_mysql2_peak_flow.rb +0 -21
  86. data/spec/info_mysql_example.rb +0 -21
  87. data/spec/info_mysql_peak_flow.rb +0 -21
  88. data/spec/info_pg_example.rb +0 -22
  89. data/spec/info_pg_peak_flow.rb +0 -22
  90. data/spec/info_sqlite3.rb +0 -19
  91. data/spec/spec_helper.rb +0 -18
  92. data/spec/support/driver_active_record_collection.rb +0 -62
  93. data/spec/support/driver_collection.rb +0 -396
  94. data/spec/support/driver_columns_collection.rb +0 -64
  95. data/spec/support/driver_databases_collection.rb +0 -48
  96. data/spec/support/driver_foreign_keys_collection.rb +0 -54
  97. data/spec/support/driver_importer_collection.rb +0 -38
  98. data/spec/support/driver_indexes_collection.rb +0 -66
  99. data/spec/support/driver_tables_collection.rb +0 -116
  100. data/spec/support/driver_users_collection.rb +0 -53
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a023d1752c079d9018cb06a5bdeaac2e86ecfc0734983a81db8e0212eae3e781
4
- data.tar.gz: 1435ebdda514818695aff2314609a8a05e0bc021cff3cd5650c3bb69fe9bbeb2
3
+ metadata.gz: de10fe5a0d1fced254dec4212f20adb172415c8a46d97280ddcdf7d1cd4af107
4
+ data.tar.gz: 4be4bf7592c14cd8bc7a1b1c5466db3a4909daa0305f4094d8bca494be24424c
5
5
  SHA512:
6
- metadata.gz: 100e78cf3b0f1c84e45049260190e2d3e134a4f47b62b50df2955a6aa0fb85adde9fcf4a36c797480359473a81b3845a97c99e26f07432f8901a6b045622d581
7
- data.tar.gz: 330e48088ea16f46d78f685e0238882b2b7acf4497e18d80b020790fcc01b78fd62168cf53364f01f149d6befdcf5f6ee5796100aae8884a7c1ccec4e88410aa
6
+ metadata.gz: 53b739c18362c3bb57893f421bb1f15fad244590faead114b8bd2c36c9755779c06b21a737a0ce73454e7f6bb594823e27c88296cdf9009ff5124a98d2cc7638
7
+ data.tar.gz: bd82734d7a67b82945806ba35551b87d97bdf2df96c1b0e2f91c03162122596fc032dcebc8a59dd053b08ceeb922b517efe8018d8f2887e6ff3d2704b929ef19
data/Rakefile CHANGED
@@ -11,20 +11,6 @@ rescue Bundler::BundlerError => e
11
11
  end
12
12
  require "rake"
13
13
 
14
- require "jeweler"
15
- Jeweler::Tasks.new do |gem|
16
- # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
- gem.name = "baza"
18
- gem.homepage = "http://github.com/kaspernj/baza"
19
- gem.license = "MIT"
20
- gem.summary = %(A database abstraction layer, model framework and database framework.)
21
- gem.description = %(A database abstraction layer, model framework and database framework.)
22
- gem.email = "kj@gfish.com"
23
- gem.authors = ["Kasper Johansen"]
24
- # dependencies defined in Gemfile
25
- end
26
- Jeweler::RubygemsDotOrgTasks.new
27
-
28
14
  require "rspec/core"
29
15
  require "rspec/core/rake_task"
30
16
  RSpec::Core::RakeTask.new(:spec) do |spec|
data/lib/baza/database.rb CHANGED
@@ -38,7 +38,7 @@ class Baza::Database
38
38
  list_args[:name] = args.fetch(:name) if args[:name]
39
39
 
40
40
  ArrayEnumerator.new do |yielder|
41
- @db.tables.list(list_args) do |table|
41
+ @db.tables.list(**list_args) do |table|
42
42
  yielder << table
43
43
  end
44
44
  end
data/lib/baza/db.rb CHANGED
@@ -184,7 +184,7 @@ class Baza::Db
184
184
 
185
185
  table_name = table.delete(:name)
186
186
  puts "Creating table: '#{table_name}'." if debug
187
- db.tables.create(table_name, table)
187
+ db.tables.create(table_name, **table)
188
188
 
189
189
  limit_from = 0
190
190
  limit_incr = 1000
@@ -196,7 +196,7 @@ class Baza::Db
196
196
  col_args = nil
197
197
 
198
198
  if table_args && table_args[:columns]
199
- d_rows.each do |col_name, _col_data|
199
+ d_rows.each_key do |col_name|
200
200
  col_args = table_args[:columns][col_name] if table_args && table_args[:columns]
201
201
  d_rows[col_name] = "" if col_args && col_args[:empty]
202
202
  end
@@ -371,7 +371,7 @@ class Baza::Db
371
371
  end
372
372
 
373
373
  # Defines all the driver methods: tables, columns and so on
374
- DRIVER_PARTS = [:databases, :tables, :commands, :columns, :indexes, :users, :sqlspecs].freeze
374
+ DRIVER_PARTS = [:databases, :foreign_keys, :tables, :commands, :columns, :indexes, :users, :sqlspecs].freeze
375
375
  DRIVER_PARTS.each do |driver_part|
376
376
  define_method(driver_part) do
377
377
  if instance_variable_defined?(:"@#{driver_part}")
@@ -444,7 +444,8 @@ class Baza::Db
444
444
  end
445
445
 
446
446
  def sqlite?
447
- @driver.class.name.downcase.include?("sqlite")
447
+ @driver.class.name.downcase.include?("sqlite") ||
448
+ (@driver.class.name == "Baza::Driver::ActiveRecord" && @driver.driver_type == :sqlite3)
448
449
  end
449
450
 
450
451
  def mysql?
@@ -4,7 +4,7 @@ class Baza::Driver::ActiveRecord::Columns
4
4
  @proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(@args[:db].driver.driver_type)).const_get(:Columns).new(@args)
5
5
  end
6
6
 
7
- def method_missing(name, *args, &blk)
8
- @proxy_to.__send__(name, *args, &blk)
7
+ def method_missing(...)
8
+ @proxy_to.__send__(...)
9
9
  end
10
10
  end
@@ -4,7 +4,7 @@ class Baza::Driver::ActiveRecord::Commands
4
4
  @proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(@db.driver.driver_type)).const_get(:Commands).new(args)
5
5
  end
6
6
 
7
- def method_missing(name, *args, &blk)
8
- @proxy_to.__send__(name, *args, &blk)
7
+ def method_missing(...)
8
+ @proxy_to.__send__(...)
9
9
  end
10
10
  end
@@ -4,7 +4,7 @@ class Baza::Driver::ActiveRecord::Databases
4
4
  @proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(@args[:db].driver.driver_type)).const_get(:Databases).new(@args)
5
5
  end
6
6
 
7
- def method_missing(name, *args, &blk)
8
- @proxy_to.__send__(name, *args, &blk)
7
+ def method_missing(...)
8
+ @proxy_to.__send__(...)
9
9
  end
10
10
  end
@@ -0,0 +1,13 @@
1
+ class Baza::Driver::ActiveRecord::ForeignKeys
2
+ def initialize(db:)
3
+ @proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(db.driver.driver_type)).const_get(:ForeignKeys).new(db: db)
4
+ end
5
+
6
+ def method_missing(...)
7
+ @proxy_to.__send__(...)
8
+ end
9
+
10
+ def respond_to_missing?(method_name)
11
+ @proxy.respond_to?(method_name) || super
12
+ end
13
+ end
@@ -4,7 +4,7 @@ class Baza::Driver::ActiveRecord::Indexes
4
4
  @proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(@args[:db].driver.driver_type)).const_get(:Indexes).new(@args)
5
5
  end
6
6
 
7
- def method_missing(name, *args, &blk)
8
- @proxy_to.__send__(name, *args, &blk)
7
+ def method_missing(...)
8
+ @proxy_to.__send__(...)
9
9
  end
10
10
  end
@@ -3,10 +3,10 @@ class Baza::Driver::ActiveRecord::Tables
3
3
  @args = args
4
4
 
5
5
  require "#{File.dirname(__FILE__)}/../#{@args.fetch(:db).driver.driver_type}"
6
- @proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(@args.fetch(:db).driver.driver_type)).const_get(:Tables).new(@args)
6
+ @proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(@args.fetch(:db).driver.driver_type)).const_get(:Tables).new(**@args)
7
7
  end
8
8
 
9
- def method_missing(name, *args, &blk)
10
- @proxy_to.__send__(name, *args, &blk)
9
+ def method_missing(...)
10
+ @proxy_to.__send__(...)
11
11
  end
12
12
  end
@@ -6,7 +6,7 @@ class Baza::Driver::ActiveRecord::Users
6
6
  @proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(@args.fetch(:db).driver.driver_type)).const_get(:Users).new(@args)
7
7
  end
8
8
 
9
- def method_missing(name, *args, &blk)
10
- @proxy_to.__send__(name, *args, &blk)
9
+ def method_missing(...)
10
+ @proxy_to.__send__(...)
11
11
  end
12
12
  end
@@ -115,6 +115,8 @@ class Baza::Driver::ActiveRecord < Baza::BaseSqlDriver
115
115
 
116
116
  def close
117
117
  @conn.close
118
+ rescue ActiveRecord::ActiveRecordError => e
119
+ raise e unless e.message == "Cannot expire connection, it is not currently leased."
118
120
  end
119
121
 
120
122
  def transaction
@@ -147,7 +149,7 @@ class Baza::Driver::ActiveRecord < Baza::BaseSqlDriver
147
149
  attributes[column_name.to_s] = value_array.last
148
150
  end
149
151
 
150
- attributes = attributes.delete_if { |_key, value| value.nil? } if model.new_record?
152
+ attributes = attributes.compact if model.new_record?
151
153
 
152
154
  table_name = model.class.table_name
153
155
 
@@ -3,7 +3,7 @@ class Baza::Driver::Mysql::Columns
3
3
  @db = args.fetch(:db)
4
4
  end
5
5
 
6
- DATA_SQL_ALLOWED_KEYS = [:type, :maxlength, :name, :primarykey, :autoincr, :default, :comment, :after, :first, :storage, :null, :renames].freeze
6
+ DATA_SQL_ALLOWED_KEYS = %i[foreign_key type maxlength name primarykey autoincr default comment after first storage null renames].freeze
7
7
  def data_sql(data)
8
8
  data.each_key do |key|
9
9
  raise "Invalid key: '#{key}' (#{key.class.name})." unless DATA_SQL_ALLOWED_KEYS.include?(key)
@@ -10,47 +10,20 @@ class Baza::Driver::Mysql::Database < Baza::Database
10
10
  self
11
11
  end
12
12
 
13
- CREATE_ALLOWED_KEYS = [:columns, :indexes, :temp, :return_sql].freeze
14
13
  # 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.quote_table(name)} ("
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
14
+ def create_table(name, columns:, indexes: nil, return_sql: false, temp: false)
15
+ raise "No columns was given for '#{name}'." if !columns || columns.empty?
16
+
17
+ sql = Baza::Driver::Mysql::Sql::CreateTable
18
+ .new(
19
+ columns: columns,
20
+ indexes: indexes,
21
+ name: name,
22
+ temporary: temp
39
23
  )
40
- end
41
-
42
- sql << ")"
43
-
44
- # return [sql] if args && args[:return_sql]
45
-
46
- sql = Baza::Driver::Mysql::Sql::CreateTable.new(
47
- columns: data.fetch(:columns),
48
- indexes: data[:indexes],
49
- name: name,
50
- temporary: data[:temp]
51
- ).sql
24
+ .sql
52
25
 
53
- return sql if args && args[:return_sql]
26
+ return sql if return_sql
54
27
 
55
28
  use do
56
29
  sql.each do |sql_i|
@@ -7,6 +7,8 @@ class Baza::Driver::Mysql::ForeignKey < Baza::ForeignKey
7
7
  @column_name = data.fetch(:COLUMN_NAME)
8
8
  @name = data.fetch(:CONSTRAINT_NAME)
9
9
  @table_name = data.fetch(:TABLE_NAME)
10
+ @referenced_table_name = data.fetch(:REFERENCED_TABLE_NAME)
11
+ @referenced_column_name = data.fetch(:REFERENCED_COLUMN_NAME)
10
12
  end
11
13
 
12
14
  def drop
@@ -0,0 +1,13 @@
1
+ class Baza::Driver::Mysql::ForeignKeys
2
+ attr_reader :db
3
+
4
+ def initialize(db:)
5
+ @db = db
6
+ end
7
+
8
+ def create(from:, to:)
9
+ sql = "ALTER TABLE #{from[0]} ADD FOREIGN KEY (#{from[1]}) REFERENCES #{to[0]}(#{to[1]})"
10
+
11
+ db.query(sql)
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  class Baza::Driver::Mysql::Sql::Column
2
- DATA_SQL_ALLOWED_KEYS = [:type, :maxlength, :name, :primarykey, :autoincr, :default, :comment, :after, :first, :storage, :null, :renames].freeze
2
+ DATA_SQL_ALLOWED_KEYS = %i[foreign_key type maxlength name primarykey autoincr default comment after first storage null renames].freeze
3
3
 
4
4
  attr_reader :data
5
5
 
@@ -7,7 +7,7 @@ class Baza::Driver::Mysql::Sql::Column
7
7
  @data = data
8
8
  end
9
9
 
10
- def sql
10
+ def sql # rubocop:disable Metrics/AbcSize
11
11
  data.each_key do |key|
12
12
  raise "Invalid key: '#{key}' (#{key.class.name})." unless DATA_SQL_ALLOWED_KEYS.include?(key)
13
13
  end
@@ -22,6 +22,7 @@ class Baza::Driver::Mysql::Sql::Column
22
22
  sql << " PRIMARY KEY" if data[:primarykey]
23
23
  sql << " AUTO_INCREMENT" if data[:autoincr]
24
24
  sql << " NOT NULL" if data.key?(:null) && !data[:null]
25
+ sql << " REFERENCES #{data.fetch(:foreign_key).fetch(:to).fetch(0)}(#{data.fetch(:foreign_key).fetch(:to).fetch(1)})" if data[:foreign_key]
25
26
 
26
27
  if data.key?(:default_func)
27
28
  sql << " DEFAULT #{data[:default_func]}"
@@ -15,7 +15,10 @@ class Baza::Driver::Mysql::Sql::CreateTable
15
15
  @columns.each do |col_data|
16
16
  sql << ", " unless first
17
17
  first = false if first
18
+
19
+ col_data = col_data.clone
18
20
  col_data.delete(:after) if col_data[:after]
21
+ col_data.delete(:foreign_key)
19
22
 
20
23
  sql << Baza::Driver::Mysql::Sql::Column.new(col_data).sql.first
21
24
  end
@@ -30,6 +33,14 @@ class Baza::Driver::Mysql::Sql::CreateTable
30
33
  ).sql.first
31
34
  end
32
35
 
36
+ @columns.each do |col_data|
37
+ next unless col_data.key?(:foreign_key)
38
+
39
+ sql << ","
40
+ sql << " CONSTRAINT `#{col_data.fetch(:foreign_key).fetch(:name)}`" if col_data.fetch(:foreign_key)[:name]
41
+ sql << " FOREIGN KEY (`#{col_data.fetch(:name)}`) REFERENCES `#{col_data.fetch(:foreign_key).fetch(:to).fetch(0)}` (`#{col_data.fetch(:foreign_key).fetch(:to).fetch(1)}`)"
42
+ end
43
+
33
44
  sql << ")"
34
45
 
35
46
  [sql]
@@ -108,7 +108,8 @@ class Baza::Driver::Mysql::Table < Baza::Table
108
108
  INFORMATION_SCHEMA.KEY_COLUMN_USAGE
109
109
 
110
110
  WHERE
111
- REFERENCED_TABLE_SCHEMA = #{@db.quote_value(@db.current_database_name)} AND
111
+ TABLE_SCHEMA = (SELECT DATABASE()) AND
112
+ CONSTRAINT_NAME != 'PRIMARY' AND
112
113
  TABLE_NAME = #{@db.quote_value(name)}
113
114
  "
114
115
 
@@ -132,6 +133,44 @@ class Baza::Driver::Mysql::Table < Baza::Table
132
133
  result
133
134
  end
134
135
 
136
+ def referenced_foreign_keys(args = {})
137
+ sql = "
138
+ SELECT
139
+ TABLE_NAME,
140
+ COLUMN_NAME,
141
+ CONSTRAINT_NAME,
142
+ REFERENCED_TABLE_NAME,
143
+ REFERENCED_COLUMN_NAME
144
+
145
+ FROM
146
+ INFORMATION_SCHEMA.KEY_COLUMN_USAGE
147
+
148
+ WHERE
149
+ TABLE_SCHEMA = (SELECT DATABASE()) AND
150
+ CONSTRAINT_NAME != 'PRIMARY' AND
151
+ REFERENCED_TABLE_NAME = #{@db.quote_value(name)}
152
+ "
153
+
154
+ sql << " AND CONSTRAINT_NAME = #{@db.quote_value(args.fetch(:name))}" if args[:name]
155
+
156
+ result = [] unless block_given?
157
+
158
+ @db.query(sql) do |data|
159
+ foreign_key = Baza::Driver::Mysql::ForeignKey.new(
160
+ db: @db,
161
+ data: data
162
+ )
163
+
164
+ if block_given?
165
+ yield foreign_key
166
+ else
167
+ result << foreign_key
168
+ end
169
+ end
170
+
171
+ result
172
+ end
173
+
135
174
  def foreign_key(name)
136
175
  foreign_keys(name: name) do |foreign_key|
137
176
  return foreign_key
@@ -253,11 +292,11 @@ class Baza::Driver::Mysql::Table < Baza::Table
253
292
  @name = newname
254
293
  @tables.__send__(:add_to_list, self)
255
294
 
256
- @list.each do |_name, column|
295
+ @list.each_value do |column|
257
296
  column.args[:table_name] = newname
258
297
  end
259
298
 
260
- @indexes_list.each do |_name, index|
299
+ @indexes_list.each_value do |index|
261
300
  index.table_name = newname
262
301
  end
263
302
  end
@@ -356,7 +395,7 @@ class Baza::Driver::Mysql::Table < Baza::Table
356
395
 
357
396
  # Changes the engine for a table.
358
397
  def engine=(newengine)
359
- raise "Invalid engine: '#{newengine}'." unless newengine.to_s =~ /^[A-z]+$/
398
+ raise "Invalid engine: '#{newengine}'." unless newengine.to_s.match?(/^[A-z]+$/)
360
399
  @db.query("ALTER TABLE #{@db.quote_table(name)} ENGINE = #{newengine}") if engine.to_s != newengine.to_s
361
400
  @data[:ENGINE] = newengine
362
401
  end
@@ -5,9 +5,9 @@ class Baza::Driver::Mysql::Tables < Baza::Tables
5
5
  attr_reader :db, :list
6
6
 
7
7
  # Constructor. This should not be called manually.
8
- def initialize(args)
8
+ def initialize(db:, **args)
9
9
  @args = args
10
- @db = @args.fetch(:db)
10
+ @db = db
11
11
  @list_mutex = Monitor.new
12
12
  @list = Wref::Map.new
13
13
  @list_should_be_reloaded = true
@@ -18,11 +18,24 @@ class Baza::Driver::Mysql::Tables < Baza::Tables
18
18
  @list.clean
19
19
  end
20
20
 
21
+ def exists?(table_name)
22
+ table_name = table_name.to_s
23
+ table = @list[table_name]
24
+
25
+ return true if table
26
+
27
+ list(name: table_name) do
28
+ return true
29
+ end
30
+
31
+ false
32
+ end
33
+
21
34
  # Returns a table by the given table-name.
22
35
  def [](table_name)
23
36
  table_name = table_name.to_s
24
-
25
37
  table = @list[table_name]
38
+
26
39
  return table if table
27
40
 
28
41
  list(name: table_name) do |table_i|
@@ -33,14 +46,14 @@ class Baza::Driver::Mysql::Tables < Baza::Tables
33
46
  end
34
47
 
35
48
  # Yields the tables of the current database.
36
- def list(args = {})
49
+ def list(database: nil, name: nil)
37
50
  ret = [] unless block_given?
38
51
 
39
52
  where_args = {}
40
- where_args["TABLE_NAME"] = args.fetch(:name) if args[:name]
53
+ where_args["TABLE_NAME"] = name if name
41
54
 
42
- if args[:database]
43
- where_args["TABLE_SCHEMA"] = args.fetch(:database)
55
+ if database
56
+ where_args["TABLE_SCHEMA"] = database
44
57
  else
45
58
  where_args["TABLE_SCHEMA"] = @db.opts.fetch(:db)
46
59
  end
@@ -74,8 +87,8 @@ class Baza::Driver::Mysql::Tables < Baza::Tables
74
87
  end
75
88
  end
76
89
 
77
- def create(name, data, args = nil)
78
- @db.current_database.create_table(name, data, args)
90
+ def create(name, **opts)
91
+ @db.current_database.create_table(name, **opts)
79
92
  end
80
93
 
81
94
  private
@@ -0,0 +1,2 @@
1
+ class Baza::Driver::Mysql2::ForeignKeys < Baza::Driver::Mysql::ForeignKeys
2
+ end
@@ -3,7 +3,7 @@ Baza.load_driver("mysql")
3
3
  class Baza::Driver::Mysql2 < Baza::MysqlBaseDriver
4
4
  AutoAutoloader.autoload_sub_classes(self, __FILE__)
5
5
 
6
- attr_reader :conn, :conns
6
+ attr_reader :conn, :conns, :encoding
7
7
 
8
8
  # Helper to enable automatic registering of database using Baza::Db.from_object
9
9
  def self.from_object(args)
@@ -54,7 +54,7 @@ class Baza::Driver::Mysql2 < Baza::MysqlBaseDriver
54
54
  # Respawns the connection to the MySQL-database.
55
55
  def reconnect
56
56
  @mutex.synchronize do
57
- require "mysql2" unless ::Object.const_defined?("Mysql2")
57
+ require "mysql2" unless ::Object.const_defined?(:Mysql2)
58
58
 
59
59
  args = {
60
60
  host: @db.opts[:host],
@@ -63,7 +63,8 @@ class Baza::Driver::Mysql2 < Baza::MysqlBaseDriver
63
63
  database: @db.opts[:db],
64
64
  port: @port,
65
65
  symbolize_keys: true,
66
- cache_rows: false
66
+ cache_rows: false,
67
+ encoding: encoding
67
68
  }
68
69
 
69
70
  # Symbolize keys should also be given here, else table-data wont be symbolized for some reason - knj.
@@ -84,14 +85,14 @@ class Baza::Driver::Mysql2 < Baza::MysqlBaseDriver
84
85
  @conn = ::Mysql2::Client.new(args)
85
86
  end
86
87
 
87
- query("SET NAMES '#{esc(@encoding)}'") if @encoding
88
+ query("SET NAMES '#{esc(encoding)}'") if encoding
88
89
  end
89
90
  end
90
91
 
91
92
  # Executes a query and returns the result.
92
93
  def query(str)
93
94
  str = str.to_s
94
- str = str.force_encoding("UTF-8") if @encoding == "utf8" && str.respond_to?(:force_encoding)
95
+ str = str.force_encoding("UTF-8") if encoding == "utf8" && str.respond_to?(:force_encoding)
95
96
  tries = 0
96
97
 
97
98
  begin
@@ -65,7 +65,7 @@ class Baza::Driver::MysqlJava < Baza::JdbcDriver
65
65
  @jdbc_loaded = true
66
66
  @conn = @db.opts.fetch(:conn)
67
67
  else
68
- com.mysql.jdbc.Driver
68
+ com.mysql.cj.jdbc.Driver
69
69
  @conn = java.sql::DriverManager.getConnection(jdbc_connect_command)
70
70
  end
71
71
 
@@ -130,7 +130,7 @@ class Baza::Driver::MysqlJava < Baza::JdbcDriver
130
130
  sql << @db.quote_value(val)
131
131
  end
132
132
  else
133
- hash.each do |_key, val|
133
+ hash.each_value do |val|
134
134
  if first_key
135
135
  first_key = false
136
136
  else
@@ -3,7 +3,7 @@ class Baza::Driver::Pg::Columns
3
3
  @db = args.fetch(:db)
4
4
  end
5
5
 
6
- DATA_SQL_ALLOWED_KEYS = [:type, :maxlength, :name, :primarykey, :autoincr, :default, :comment, :after, :first, :storage, :null, :renames].freeze
6
+ DATA_SQL_ALLOWED_KEYS = %i[foreign_key type maxlength name primarykey autoincr default comment after first storage null renames].freeze
7
7
  def data_sql(data)
8
8
  data.each_key do |key|
9
9
  raise "Invalid key: '#{key}' (#{key.class.name})." unless DATA_SQL_ALLOWED_KEYS.include?(key)
@@ -6,12 +6,9 @@ class Baza::Driver::Pg::CreateIndexSqlCreator
6
6
  end
7
7
 
8
8
  def sqls
9
- sqls = []
10
- @indexes.each do |index_data|
11
- sqls << create_sql(index_data, @create_args)
9
+ @indexes.map do |index_data|
10
+ create_sql(index_data, @create_args)
12
11
  end
13
-
14
- sqls
15
12
  end
16
13
 
17
14
  def name_from_table_and_columns(table_name, column_names)
@@ -55,9 +55,9 @@ class Baza::Driver::Pg::Database < Baza::Database
55
55
 
56
56
  CREATE_ALLOWED_KEYS = [:columns, :indexes, :temp, :return_sql].freeze
57
57
  # Creates a new table by the given name and data.
58
- def create_table(table_name, data, args = nil)
58
+ def create_table(table_name, **args)
59
59
  use do
60
- db.tables.create(table_name, data, args)
60
+ db.tables.create(table_name, **args)
61
61
  end
62
62
  end
63
63
 
@@ -7,6 +7,8 @@ class Baza::Driver::Pg::ForeignKey < Baza::ForeignKey
7
7
  @column_name = data.fetch(:column_name)
8
8
  @name = data.fetch(:constraint_name)
9
9
  @table_name = data.fetch(:table_name)
10
+ @referenced_column_name = data.fetch(:foreign_column_name)
11
+ @referenced_table_name = data.fetch(:foreign_table_name)
10
12
  end
11
13
 
12
14
  def drop
@@ -0,0 +1,13 @@
1
+ class Baza::Driver::Pg::ForeignKeys
2
+ attr_reader :db
3
+
4
+ def initialize(db:)
5
+ @db = db
6
+ end
7
+
8
+ def create(from:, to:)
9
+ sql = "ALTER TABLE #{from[0]} ADD FOREIGN KEY (#{from[1]}) REFERENCES #{to[0]}(#{to[1]})"
10
+
11
+ db.query(sql)
12
+ end
13
+ end
@@ -122,7 +122,11 @@ private
122
122
  elsif type_sym == :float
123
123
  value.to_f
124
124
  elsif type_sym == :time
125
- Time.parse(value)
125
+ if value.is_a?(Time) # ActiveRecord might already have parsed it for us
126
+ value
127
+ else
128
+ Time.parse(value)
129
+ end
126
130
  elsif type_sym == :date
127
131
  Date.parse(value)
128
132
  elsif type_sym == :string