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
@@ -1,9 +1,4 @@
1
1
  # Subclass that contains all the drivers as further subclasses.
2
2
  class Baza::Driver
3
- # Autoloader for drivers.
4
- def self.const_missing(name)
5
- require_relative "drivers/#{StringCases.camel_to_snake(name)}.rb"
6
- raise LoadError, "Still not loaded: '#{name}'." unless Baza::Driver.const_defined?(name)
7
- Baza::Driver.const_get(name)
8
- end
3
+ AutoAutoloader.autoload_sub_classes(self, __FILE__)
9
4
  end
@@ -1,12 +1,7 @@
1
1
  class Baza::Driver::ActiveRecord < Baza::BaseSqlDriver
2
- path = "#{File.dirname(__FILE__)}/active_record"
2
+ AutoAutoloader.autoload_sub_classes(self, __FILE__)
3
3
 
4
- autoload :Tables, "#{path}/tables"
5
- autoload :Columns, "#{path}/columns"
6
- autoload :Indexes, "#{path}/indexes"
7
- autoload :Result, "#{path}/result"
8
-
9
- attr_reader :baza, :conn, :sep_table, :sep_col, :sep_val, :symbolize, :driver_type
4
+ attr_reader :db, :conn, :sep_table, :sep_col, :sep_val, :symbolize, :driver_type
10
5
  attr_accessor :tables, :cols, :indexes
11
6
 
12
7
  def self.from_object(args)
@@ -29,9 +24,9 @@ class Baza::Driver::ActiveRecord < Baza::BaseSqlDriver
29
24
  nil
30
25
  end
31
26
 
32
- def initialize(baza)
33
- @baza = baza
34
- @conn = @baza.opts.fetch(:conn)
27
+ def initialize(db)
28
+ @db = db
29
+ @conn = @db.opts.fetch(:conn)
35
30
 
36
31
  raise "No conn given" unless @conn
37
32
 
@@ -41,41 +36,59 @@ class Baza::Driver::ActiveRecord < Baza::BaseSqlDriver
41
36
  require_relative "mysql2"
42
37
  require_relative "mysql2/result"
43
38
 
39
+ @sep_database = "`"
44
40
  @sep_table = "`"
45
41
  @sep_col = "`"
46
42
  @sep_val = "'"
43
+ @sep_index = "`"
47
44
  @driver_type = :mysql2
48
45
  @result_constant = Baza::Driver::Mysql2::Result
49
46
  elsif conn_name.include?("mysql")
50
- require_relative "mysql"
51
- require_relative "mysql/result"
47
+ unless RUBY_PLATFORM == "java"
48
+ require_relative "mysql"
49
+ require_relative "mysql/result"
50
+ @result_constant = Baza::Driver::Mysql::Result
51
+ end
52
52
 
53
+ @sep_database = "`"
53
54
  @sep_table = "`"
54
55
  @sep_col = "`"
55
56
  @sep_val = "'"
57
+ @sep_index = "`"
56
58
  @driver_type = :mysql
57
- @result_constant = Baza::Driver::Mysql::Result unless RUBY_PLATFORM == "java"
58
59
  elsif conn_name.include?("sqlite")
60
+ @sep_database = "`"
59
61
  @sep_table = "`"
60
62
  @sep_col = "`"
61
63
  @sep_val = "'"
64
+ @sep_index = "`"
62
65
  @driver_type = :sqlite3
66
+ elsif conn_name.include?("postgresqladapter")
67
+ @sep_database = '"'
68
+ @sep_table = '"'
69
+ @sep_col = '"'
70
+ @sep_index = '"'
71
+ @sep_val = "'"
72
+ @driver_type = :pg
73
+ @result_constant = Baza::Driver::Pg::Result
63
74
  else
64
- raise "Unknown type: '#{conn_name}'."
75
+ raise "Unknown type: '#{conn_name}'"
65
76
  end
66
77
 
78
+ @result_constant ||= Baza::Driver::ActiveRecord::Result
79
+
67
80
  if conn_name.include?("mysql")
68
- @baza.opts[:db] ||= query("SELECT DATABASE()").fetch.fetch(:"DATABASE()")
81
+ @db.opts[:db] ||= query("SELECT DATABASE()").fetch.fetch(:"DATABASE()")
82
+ elsif @driver_type == :pg
83
+ @db.opts[:db] ||= query("SELECT current_database()").fetch.values.first
69
84
  end
70
-
71
- @result_constant ||= Baza::Driver::ActiveRecord::Result
72
85
  end
73
86
 
74
87
  def query(sql)
75
88
  @result_constant.new(self, @conn.execute(sql))
76
89
  end
77
90
 
78
- alias_method :query_ubuf, :query
91
+ alias query_ubuf query
79
92
 
80
93
  def escape(str)
81
94
  @conn.quote_string(str.to_s)
@@ -98,7 +111,7 @@ class Baza::Driver::ActiveRecord < Baza::BaseSqlDriver
98
111
  end
99
112
 
100
113
  def transaction
101
- if @driver_type == :mysql || @driver_type == :mysql2
114
+ if @driver_type == :mysql || @driver_type == :mysql2 || @driver_type == :pg
102
115
  query("START TRANSACTION")
103
116
  elsif @driver_type == :sqlite3
104
117
  query("BEGIN TRANSACTION")
@@ -107,7 +120,7 @@ class Baza::Driver::ActiveRecord < Baza::BaseSqlDriver
107
120
  end
108
121
 
109
122
  begin
110
- yield @baza
123
+ yield @db
111
124
  query("COMMIT")
112
125
  rescue
113
126
  query("ROLLBACK")
@@ -116,6 +129,37 @@ class Baza::Driver::ActiveRecord < Baza::BaseSqlDriver
116
129
  end
117
130
 
118
131
  def supports_multiple_databases?
119
- conn_name.include?("mysql")
132
+ conn_name.include?("mysql") || @driver_type == :pg
133
+ end
134
+
135
+ def save_model!(model, args = {})
136
+ raise ActiveRecord::InvalidRecord, model if (!args.key?(:validate) || args[:validate]) && !model.valid?
137
+
138
+ attributes = {}
139
+ model.changes.each do |column_name, value_array|
140
+ attributes[column_name.to_s] = value_array.last
141
+ end
142
+
143
+ attributes = attributes.delete_if { |_key, value| value.nil? } if model.new_record?
144
+
145
+ table_name = model.class.table_name
146
+
147
+ if model.new_record?
148
+ if args[:update_on_duplicate_key]
149
+ id = @db.upsert_duplicate_key(table_name, attributes, {}, return_id: true)
150
+ else
151
+ id = @db.insert(table_name, attributes, return_id: true)
152
+ end
153
+
154
+ if id && id.to_i > 0
155
+ model.id = id
156
+ else
157
+ raise "Invalid ID: #{id}" unless id.to_i > 0
158
+ end
159
+ else
160
+ @db.update(table_name, attributes, id: model.id)
161
+ end
162
+
163
+ model.reload
120
164
  end
121
165
  end
@@ -0,0 +1,10 @@
1
+ class Baza::Driver::ActiveRecord::Commands
2
+ def initialize(args)
3
+ @db = args.fetch(:db)
4
+ @proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(@db.driver.driver_type)).const_get(:Commands).new(args)
5
+ end
6
+
7
+ def method_missing(name, *args, &blk)
8
+ @proxy_to.__send__(name, *args, &blk)
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class Baza::Driver::ActiveRecord::Databases
2
+ def initialize(args)
3
+ @args = args
4
+ @proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(@args[:db].driver.driver_type)).const_get(:Databases).new(@args)
5
+ end
6
+
7
+ def method_missing(name, *args, &blk)
8
+ @proxy_to.__send__(name, *args, &blk)
9
+ end
10
+ end
@@ -1,7 +1,7 @@
1
1
  class Baza::Driver::ActiveRecord::Result < Baza::ResultBase
2
2
  def initialize(driver, result)
3
3
  @result = result
4
- @type_translation = driver.baza.opts[:type_translation]
4
+ @type_translation = driver.db.opts[:type_translation]
5
5
  end
6
6
 
7
7
  def fetch
@@ -14,7 +14,9 @@ class Baza::Driver::ActiveRecord::Result < Baza::ResultBase
14
14
  return unless @result
15
15
 
16
16
  @result.each do |result|
17
+ result = result.delete_if { |k, _v| k.is_a?(Fixnum) } # Seems like this happens depending on the version installed? - kaspernj
17
18
  result = Hash[result.map { |k, v| [k, v.to_s] }] if @type_translation == :string
19
+
18
20
  yield result.symbolize_keys
19
21
  end
20
22
  end
@@ -0,0 +1,12 @@
1
+ class Baza::Driver::ActiveRecord::Users
2
+ def initialize(args)
3
+ @args = args
4
+
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(:Users).new(@args)
7
+ end
8
+
9
+ def method_missing(name, *args, &blk)
10
+ @proxy_to.__send__(name, *args, &blk)
11
+ end
12
+ end
@@ -1,17 +1,5 @@
1
1
  class Baza::Driver::Mysql < Baza::MysqlBaseDriver
2
- path = "#{File.dirname(__FILE__)}/mysql"
3
-
4
- autoload :Database, "#{path}/database"
5
- autoload :Databases, "#{path}/databases"
6
- autoload :Table, "#{path}/table"
7
- autoload :Tables, "#{path}/tables"
8
- autoload :Column, "#{path}/column"
9
- autoload :Columns, "#{path}/columns"
10
- autoload :Index, "#{path}/index"
11
- autoload :Indexes, "#{path}/indexes"
12
- autoload :Result, "#{path}/result"
13
- autoload :UnbufferedResult, "#{path}/unbuffered_result"
14
- autoload :Sqlspecs, "#{path}/sqlspecs"
2
+ AutoAutoloader.autoload_sub_classes(self, __FILE__)
15
3
 
16
4
  attr_reader :conn
17
5
 
@@ -19,16 +7,16 @@ class Baza::Driver::Mysql < Baza::MysqlBaseDriver
19
7
  raise "Mysql does not support auth extraction" if args[:object].class.name == "Mysql"
20
8
  end
21
9
 
22
- def initialize(baza)
10
+ def initialize(db)
23
11
  super
24
12
 
25
- @opts = @baza.opts
13
+ @opts = @db.opts
26
14
 
27
15
  require "monitor"
28
16
  @mutex = Monitor.new
29
17
 
30
- if baza.opts[:conn]
31
- @conn = baza.opts[:conn]
18
+ if db.opts[:conn]
19
+ @conn = db.opts[:conn]
32
20
  else
33
21
  if @opts[:encoding]
34
22
  @encoding = @opts[:encoding]
@@ -36,8 +24,8 @@ class Baza::Driver::Mysql < Baza::MysqlBaseDriver
36
24
  @encoding = "utf8"
37
25
  end
38
26
 
39
- if @baza.opts.key?(:port)
40
- @port = @baza.opts[:port].to_i
27
+ if @db.opts.key?(:port)
28
+ @port = @db.opts[:port].to_i
41
29
  else
42
30
  @port = 3306
43
31
  end
@@ -55,7 +43,7 @@ class Baza::Driver::Mysql < Baza::MysqlBaseDriver
55
43
  def reconnect
56
44
  @mutex.synchronize do
57
45
  require "mysql" unless ::Object.const_defined?(:Mysql)
58
- @conn = ::Mysql.real_connect(@baza.opts[:host], @baza.opts[:user], @baza.opts[:pass], @baza.opts[:db], @port)
46
+ @conn = ::Mysql.real_connect(@db.opts[:host], @db.opts[:user], @db.opts[:pass], @db.opts[:db], @port)
59
47
  query("SET NAMES '#{esc(@encoding)}'") if @encoding
60
48
  end
61
49
  end
@@ -100,11 +88,6 @@ class Baza::Driver::Mysql < Baza::MysqlBaseDriver
100
88
  @conn.escape_string(string.to_s)
101
89
  end
102
90
 
103
- # Returns the last inserted ID for the connection.
104
- def last_id
105
- @mutex.synchronize { return @conn.insert_id.to_i }
106
- end
107
-
108
91
  # Closes the connection threadsafe.
109
92
  def close
110
93
  @mutex.synchronize { @conn.close }
@@ -113,7 +96,7 @@ class Baza::Driver::Mysql < Baza::MysqlBaseDriver
113
96
  # Destroyes the connection.
114
97
  def destroy
115
98
  @conn = nil
116
- @baza = nil
99
+ @db = nil
117
100
  @mutex = nil
118
101
  @subtype = nil
119
102
  @encoding = nil
@@ -19,24 +19,6 @@ class Baza::Driver::Mysql::Column < Baza::Column
19
19
  @args.fetch(:table_name)
20
20
  end
21
21
 
22
- # Returns the table-object that this column belongs to.
23
- def table
24
- @db.tables[table_name]
25
- end
26
-
27
- # Returns all data of the column in the knjdb-format.
28
- def data
29
- {
30
- type: type,
31
- name: name,
32
- null: null?,
33
- maxlength: maxlength,
34
- default: default,
35
- primarykey: primarykey?,
36
- autoincr: autoincr?
37
- }
38
- end
39
-
40
22
  def reload
41
23
  data = @db.query("SHOW FULL COLUMNS FROM `#{@db.escape_table(table_name)}` WHERE `Field` = '#{@db.esc(name)}'").fetch
42
24
  raise Baza::Errors::ColumnNotFound unless data
@@ -47,21 +29,21 @@ class Baza::Driver::Mysql::Column < Baza::Column
47
29
  # Returns the type of the column (integer, varchar etc.).
48
30
  def type
49
31
  unless @type
50
- if match = @data[:Type].match(/^([A-z]+)$/)
32
+ if (match = @data.fetch(:Type).match(/^([A-z]+)$/))
51
33
  @maxlength = false
52
34
  @type = match[0].to_sym
53
- elsif match = @data[:Type].match(/^decimal\((\d+),(\d+)\)$/)
35
+ elsif (match = @data.fetch(:Type).match(/^decimal\((\d+),(\d+)\)$/))
54
36
  @maxlength = "#{match[1]},#{match[2]}"
55
37
  @type = :decimal
56
- elsif match = @data[:Type].match(/^enum\((.+)\)$/)
38
+ elsif (match = @data.fetch(:Type).match(/^enum\((.+)\)$/))
57
39
  @maxlength = match[1]
58
40
  @type = :enum
59
- elsif match = @data[:Type].match(/^(.+)\((\d+)\)/)
41
+ elsif (match = @data.fetch(:Type).match(/^(.+)\((\d+)\)/))
60
42
  @maxlength = match[2].to_i
61
43
  @type = match[1].to_sym
62
44
  end
63
45
 
64
- raise "Still not type from: '#{@data[:Type]}'." if @type.to_s.strip.empty?
46
+ raise "Still no type from: '#{@data.fetch(:Type)}'" if @type.to_s.strip.empty?
65
47
  end
66
48
 
67
49
  @type
@@ -69,8 +51,7 @@ class Baza::Driver::Mysql::Column < Baza::Column
69
51
 
70
52
  # Return true if the columns allows null. Otherwise false.
71
53
  def null?
72
- return false if @data[:Null] == "NO"
73
- true
54
+ @data[:Null] != "NO"
74
55
  end
75
56
 
76
57
  # Returns the maxlength.
@@ -82,22 +63,20 @@ class Baza::Driver::Mysql::Column < Baza::Column
82
63
 
83
64
  # Returns the default value for the column.
84
65
  def default
85
- return false if (type == :datetime || type == :date) && @data[:Default].to_s.strip.empty?
86
- return false if (type == :int || type == :bigint) && @data[:Default].to_s.strip.empty?
87
- return false unless @data[:Default]
66
+ return nil if (type == :datetime || type == :date) && @data[:Default].to_s.strip.empty?
67
+ return nil if (type == :int || type == :bigint) && @data[:Default].to_s.strip.empty?
68
+ return nil unless @data[:Default]
88
69
  @data.fetch(:Default)
89
70
  end
90
71
 
91
72
  # Returns true if the column is the primary key. Otherwise false.
92
73
  def primarykey?
93
- return true if @data.fetch(:Key) == "PRI"
94
- false
74
+ @data.fetch(:Key) == "PRI"
95
75
  end
96
76
 
97
77
  # Returns true if the column is auto-increasing. Otherwise false.
98
78
  def autoincr?
99
- return true if @data.fetch(:Extra).include?("auto_increment")
100
- false
79
+ @data.fetch(:Extra).include?("auto_increment")
101
80
  end
102
81
 
103
82
  # Returns the comment for the column.
@@ -114,8 +93,8 @@ class Baza::Driver::Mysql::Column < Baza::Column
114
93
 
115
94
  # Changes the column properties by the given hash.
116
95
  def change(data)
117
- col_escaped = "`#{@db.escape_column(name)}`"
118
- table_escape = "`#{@db.escape_table(table_name)}`"
96
+ col_escaped = "#{@db.sep_col}#{@db.escape_column(name)}#{@db.sep_col}"
97
+ table_escape = "#{@db.sep_table}#{@db.escape_table(table_name)}#{@db.sep_table}"
119
98
  newdata = data.clone
120
99
 
121
100
  newdata[:name] = name unless newdata.key?(:name)
@@ -128,7 +107,7 @@ class Baza::Driver::Mysql::Column < Baza::Column
128
107
  drop_add = true if name.to_s != newdata[:name].to_s
129
108
 
130
109
  table.__send__(:remove_column_from_list, self) if drop_add
131
- @db.query("ALTER TABLE #{table_escape} CHANGE #{col_escaped} #{@db.cols.data_sql(newdata)}")
110
+ @db.query("ALTER TABLE #{table_escape} CHANGE #{col_escaped} #{@db.columns.data_sql(newdata)}")
132
111
  @name = newdata[:name].to_s
133
112
  reload
134
113
  table.__send__(:add_column_to_list, self) if drop_add
@@ -1,12 +1,9 @@
1
- # This class handels various MySQL-column-specific operations.
2
1
  class Baza::Driver::Mysql::Columns
3
- # Constructor. Should not be called manually.
4
2
  def initialize(args)
5
- @args = args
3
+ @db = args.fetch(:db)
6
4
  end
7
5
 
8
- # Returns the SQL for this column.
9
- DATA_SQL_ALLOWED_KEYS = [:type, :maxlength, :name, :primarykey, :autoincr, :default, :comment, :after, :first, :storage, :null, :renames]
6
+ DATA_SQL_ALLOWED_KEYS = [:type, :maxlength, :name, :primarykey, :autoincr, :default, :comment, :after, :first, :storage, :null, :renames].freeze
10
7
  def data_sql(data)
11
8
  data.each_key do |key|
12
9
  raise "Invalid key: '#{key}' (#{key.class.name})." unless DATA_SQL_ALLOWED_KEYS.include?(key)
@@ -15,22 +12,22 @@ class Baza::Driver::Mysql::Columns
15
12
  raise "No type given." unless data[:type]
16
13
  type = data[:type].to_sym
17
14
 
18
- data[:maxlength] = 255 if type == :varchar && !data.key?(:maxlength)
15
+ data[:maxlength] = 255 if type == :varchar && data[:maxlength].to_s.strip.empty?
19
16
 
20
- sql = "`#{data[:name]}` #{type}"
17
+ sql = "#{@db.sep_col}#{@db.escape_column(data.fetch(:name))}#{@db.sep_col} #{type}"
21
18
  sql << "(#{data[:maxlength]})" if data[:maxlength]
22
19
  sql << " PRIMARY KEY" if data[:primarykey]
23
20
  sql << " AUTO_INCREMENT" if data[:autoincr]
24
- sql << " NOT NULL" unless data[:null]
21
+ sql << " NOT NULL" if data.key?(:null) && !data[:null]
25
22
 
26
23
  if data.key?(:default_func)
27
24
  sql << " DEFAULT #{data[:default_func]}"
28
- elsif data.key?(:default) && data[:default] != false
29
- sql << " DEFAULT '#{@args[:db].escape(data[:default])}'"
25
+ elsif data.key?(:default) && !data[:default].nil?
26
+ sql << " DEFAULT #{@db.sqlval(data.fetch(:default))}"
30
27
  end
31
28
 
32
- sql << " COMMENT '#{@args[:db].escape(data[:comment])}'" if data.key?(:comment)
33
- sql << " AFTER `#{@args[:db].escape_column(data[:after])}`" if data[:after] && !data[:first]
29
+ sql << " COMMENT '#{@db.escape(data.fetch(:comment))}'" if data.key?(:comment)
30
+ sql << " AFTER #{@db.sep_col}#{@db.escape_column(data.fetch(:after))}#{@db.sep_col}" if data[:after] && !data[:first]
34
31
  sql << " FIRST" if data[:first]
35
32
  sql << " STORAGE #{data[:storage].to_s.upcase}" if data[:storage]
36
33