baza 0.0.19 → 0.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +157 -0
- data/Gemfile +11 -9
- data/Gemfile.lock +41 -9
- data/README.md +1 -1
- data/Rakefile +19 -16
- data/VERSION +1 -1
- data/baza.gemspec +31 -7
- data/config/best_project_practice_rubocop.yml +2 -0
- data/config/best_project_practice_rubocop_todo.yml +157 -0
- data/lib/baza/base_sql_driver.rb +79 -20
- data/lib/baza/cloner.rb +2 -2
- data/lib/baza/column.rb +6 -0
- data/lib/baza/database.rb +33 -0
- data/lib/baza/database_model.rb +26 -0
- data/lib/baza/database_model_functionality.rb +10 -0
- data/lib/baza/database_model_name.rb +16 -0
- data/lib/baza/db.rb +210 -413
- data/lib/baza/dbtime.rb +20 -20
- data/lib/baza/driver.rb +3 -3
- data/lib/baza/drivers/active_record/columns.rb +1 -1
- data/lib/baza/drivers/active_record/indexes.rb +1 -1
- data/lib/baza/drivers/active_record/result.rb +3 -5
- data/lib/baza/drivers/active_record/tables.rb +2 -2
- data/lib/baza/drivers/active_record.rb +30 -16
- data/lib/baza/drivers/mysql/column.rb +51 -48
- data/lib/baza/drivers/mysql/columns.rb +8 -8
- data/lib/baza/drivers/mysql/database.rb +28 -0
- data/lib/baza/drivers/mysql/databases.rb +35 -0
- data/lib/baza/drivers/mysql/index.rb +32 -22
- data/lib/baza/drivers/mysql/result.rb +24 -24
- data/lib/baza/drivers/mysql/sqlspecs.rb +1 -1
- data/lib/baza/drivers/mysql/table.rb +80 -91
- data/lib/baza/drivers/mysql/tables.rb +30 -32
- data/lib/baza/drivers/mysql/unbuffered_result.rb +5 -5
- data/lib/baza/drivers/mysql.rb +127 -220
- data/lib/baza/drivers/mysql2/database.rb +2 -0
- data/lib/baza/drivers/mysql2/databases.rb +2 -0
- data/lib/baza/drivers/mysql2/result.rb +3 -5
- data/lib/baza/drivers/mysql2.rb +21 -106
- data/lib/baza/drivers/mysql_java/database.rb +2 -0
- data/lib/baza/drivers/mysql_java/databases.rb +2 -0
- data/lib/baza/drivers/mysql_java.rb +16 -12
- data/lib/baza/drivers/sqlite3/column.rb +33 -33
- data/lib/baza/drivers/sqlite3/columns.rb +6 -6
- data/lib/baza/drivers/sqlite3/database.rb +2 -0
- data/lib/baza/drivers/sqlite3/databases.rb +25 -0
- data/lib/baza/drivers/sqlite3/index.rb +20 -6
- data/lib/baza/drivers/sqlite3/result.rb +16 -17
- data/lib/baza/drivers/sqlite3/sqlspecs.rb +1 -1
- data/lib/baza/drivers/sqlite3/table.rb +99 -100
- data/lib/baza/drivers/sqlite3/tables.rb +30 -23
- data/lib/baza/drivers/sqlite3/unbuffered_result.rb +2 -2
- data/lib/baza/drivers/sqlite3.rb +27 -14
- data/lib/baza/drivers/sqlite3_java/database.rb +2 -0
- data/lib/baza/drivers/sqlite3_java/unbuffered_result.rb +2 -2
- data/lib/baza/drivers/sqlite3_java.rb +12 -10
- data/lib/baza/drivers/sqlite3_rhodes.rb +9 -7
- data/lib/baza/dump.rb +55 -55
- data/lib/baza/errors.rb +5 -0
- data/lib/baza/idquery.rb +25 -25
- data/lib/baza/index.rb +6 -0
- data/lib/baza/jdbc_driver.rb +6 -8
- data/lib/baza/jdbc_result.rb +1 -1
- data/lib/baza/model.rb +299 -309
- data/lib/baza/model_custom.rb +74 -72
- data/lib/baza/model_handler.rb +244 -255
- data/lib/baza/model_handler_sqlhelper.rb +122 -132
- data/lib/baza/mysql_base_driver.rb +112 -0
- data/lib/baza/query_buffer.rb +23 -23
- data/lib/baza/result_base.rb +2 -2
- data/lib/baza/revision.rb +67 -67
- data/lib/baza/row.rb +82 -84
- data/lib/baza/sqlspecs.rb +1 -1
- data/lib/baza/table.rb +31 -0
- data/lib/baza.rb +43 -10
- data/shippable.yml +1 -0
- data/spec/cloner_spec.rb +4 -4
- data/spec/drivers/active_record_mysql2_spec.rb +3 -3
- data/spec/drivers/active_record_mysql_spec.rb +2 -2
- data/spec/drivers/active_record_sqlite3_spec.rb +2 -2
- data/spec/drivers/mysql2_spec.rb +1 -0
- data/spec/drivers/mysql_spec.rb +1 -0
- data/spec/drivers/sqlite3_spec.rb +4 -5
- data/spec/info_active_record_example.rb +4 -6
- data/spec/info_active_record_mysql.rb +6 -7
- data/spec/info_active_record_mysql2.rb +2 -4
- data/spec/info_active_record_mysql2_shippable.rb +2 -4
- data/spec/info_active_record_mysql_shippable.rb +2 -4
- data/spec/info_active_record_sqlite3.rb +5 -6
- data/spec/info_mysql2_example.rb +1 -1
- data/spec/info_mysql2_shippable.rb +1 -1
- data/spec/info_mysql_example.rb +1 -1
- data/spec/info_mysql_shippable.rb +1 -1
- data/spec/info_sqlite3.rb +4 -3
- data/spec/model_handler_spec.rb +102 -115
- data/spec/spec_helper.rb +5 -5
- data/spec/support/driver_collection.rb +98 -95
- data/spec/support/driver_columns_collection.rb +18 -22
- data/spec/support/driver_databases_collection.rb +29 -0
- data/spec/support/driver_indexes_collection.rb +7 -6
- data/spec/support/driver_tables_collection.rb +38 -26
- metadata +49 -3
data/lib/baza/dbtime.rb
CHANGED
@@ -1,35 +1,35 @@
|
|
1
|
-
#This class helps handeling time-columns in databases.
|
1
|
+
# This class helps handeling time-columns in databases.
|
2
2
|
class Baza::Dbtime
|
3
|
-
#These variables return information about the object.
|
3
|
+
# These variables return information about the object.
|
4
4
|
attr_reader :hours, :mins, :secs, :total_secs
|
5
|
-
|
6
|
-
#Initializes the object from arguments useually given by Baza::Datarow.
|
5
|
+
|
6
|
+
# Initializes the object from arguments useually given by Baza::Datarow.
|
7
7
|
def initialize(args)
|
8
|
-
args = {:
|
9
|
-
|
10
|
-
raise "Invalid arguments given: #{args}"
|
11
|
-
raise "No time given."
|
12
|
-
raise "Invalid time given: #{args[:time].class.name}"
|
13
|
-
|
8
|
+
args = {time: args} if args.is_a?(String)
|
9
|
+
|
10
|
+
raise "Invalid arguments given: #{args}" unless args.is_a?(Hash)
|
11
|
+
raise "No time given." unless args[:time]
|
12
|
+
raise "Invalid time given: #{args[:time].class.name}" unless args[:time].is_a?(String)
|
13
|
+
|
14
14
|
match = args[:time].match(/^(\d+):(\d+):(\d+)$/)
|
15
|
-
raise "Could not understand time format."
|
16
|
-
|
15
|
+
raise "Could not understand time format." unless match
|
16
|
+
|
17
17
|
@hours = match[1].to_i
|
18
18
|
@mins = match[2].to_i
|
19
19
|
@secs = match[3].to_i
|
20
|
-
|
20
|
+
|
21
21
|
@total_secs = @hours * 3600
|
22
22
|
@total_secs += @mins * 60
|
23
23
|
@total_secs += @secs
|
24
24
|
end
|
25
|
-
|
26
|
-
#Returns the total amount of hours.
|
25
|
+
|
26
|
+
# Returns the total amount of hours.
|
27
27
|
def hours_total
|
28
|
-
|
28
|
+
(@total_secs.to_f / 3600)
|
29
29
|
end
|
30
|
-
|
31
|
-
#Return the total amount of minutes.
|
30
|
+
|
31
|
+
# Return the total amount of minutes.
|
32
32
|
def mins_total
|
33
|
-
|
33
|
+
(@total_secs.to_f / 60)
|
34
34
|
end
|
35
|
-
end
|
35
|
+
end
|
data/lib/baza/driver.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
#Subclass that contains all the drivers as further subclasses.
|
1
|
+
# Subclass that contains all the drivers as further subclasses.
|
2
2
|
class Baza::Driver
|
3
|
-
#Autoloader for drivers.
|
3
|
+
# Autoloader for drivers.
|
4
4
|
def self.const_missing(name)
|
5
5
|
require_relative "drivers/#{StringCases.camel_to_snake(name)}.rb"
|
6
6
|
raise LoadError, "Still not loaded: '#{name}'." unless Baza::Driver.const_defined?(name)
|
7
|
-
|
7
|
+
Baza::Driver.const_get(name)
|
8
8
|
end
|
9
9
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
class Baza::Driver::ActiveRecord::Columns
|
2
2
|
def initialize(args)
|
3
3
|
@args = args
|
4
|
-
@proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(@args[:db].
|
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
7
|
def method_missing(name, *args, &blk)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
class Baza::Driver::ActiveRecord::Indexes
|
2
2
|
def initialize(args)
|
3
3
|
@args = args
|
4
|
-
@proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(@args[:db].
|
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
7
|
def method_missing(name, *args, &blk)
|
@@ -2,8 +2,8 @@ class Baza::Driver::ActiveRecord::Tables
|
|
2
2
|
def initialize(args)
|
3
3
|
@args = args
|
4
4
|
|
5
|
-
require "#{File.dirname(__FILE__)}/../#{@args
|
6
|
-
@proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(@args
|
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)
|
7
7
|
end
|
8
8
|
|
9
9
|
def method_missing(name, *args, &blk)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
class Baza::Driver::ActiveRecord
|
1
|
+
class Baza::Driver::ActiveRecord < Baza::BaseSqlDriver
|
2
2
|
path = "#{File.dirname(__FILE__)}/active_record"
|
3
3
|
|
4
4
|
autoload :Tables, "#{path}/tables"
|
@@ -6,7 +6,7 @@ class Baza::Driver::ActiveRecord
|
|
6
6
|
autoload :Indexes, "#{path}/indexes"
|
7
7
|
autoload :Result, "#{path}/result"
|
8
8
|
|
9
|
-
attr_reader :baza, :conn, :sep_table, :sep_col, :sep_val, :symbolize, :
|
9
|
+
attr_reader :baza, :conn, :sep_table, :sep_col, :sep_val, :symbolize, :driver_type
|
10
10
|
attr_accessor :tables, :cols, :indexes
|
11
11
|
|
12
12
|
def self.from_object(args)
|
@@ -26,38 +26,48 @@ class Baza::Driver::ActiveRecord
|
|
26
26
|
}
|
27
27
|
end
|
28
28
|
|
29
|
-
|
29
|
+
nil
|
30
30
|
end
|
31
31
|
|
32
32
|
def initialize(baza)
|
33
33
|
@baza = baza
|
34
|
-
@conn = @baza.opts
|
34
|
+
@conn = @baza.opts.fetch(:conn)
|
35
35
|
|
36
|
-
raise
|
36
|
+
raise "No conn given" unless @conn
|
37
37
|
|
38
38
|
conn_name = @conn.class.name.to_s.downcase
|
39
39
|
|
40
40
|
if conn_name.include?("mysql2")
|
41
|
+
require_relative "mysql2"
|
42
|
+
require_relative "mysql2/result"
|
43
|
+
|
41
44
|
@sep_table = "`"
|
42
45
|
@sep_col = "`"
|
43
46
|
@sep_val = "'"
|
44
|
-
@
|
47
|
+
@driver_type = :mysql2
|
45
48
|
@result_constant = Baza::Driver::Mysql2::Result
|
46
49
|
elsif conn_name.include?("mysql")
|
50
|
+
require_relative "mysql"
|
51
|
+
require_relative "mysql/result"
|
52
|
+
|
47
53
|
@sep_table = "`"
|
48
54
|
@sep_col = "`"
|
49
55
|
@sep_val = "'"
|
50
|
-
@
|
51
|
-
@result_constant = Baza::Driver::Mysql::Result unless RUBY_PLATFORM ==
|
56
|
+
@driver_type = :mysql
|
57
|
+
@result_constant = Baza::Driver::Mysql::Result unless RUBY_PLATFORM == "java"
|
52
58
|
elsif conn_name.include?("sqlite")
|
53
59
|
@sep_table = "`"
|
54
60
|
@sep_col = "`"
|
55
61
|
@sep_val = "'"
|
56
|
-
@
|
62
|
+
@driver_type = :sqlite3
|
57
63
|
else
|
58
64
|
raise "Unknown type: '#{conn_name}'."
|
59
65
|
end
|
60
66
|
|
67
|
+
if conn_name.include?("mysql")
|
68
|
+
@baza.opts[:db] ||= query("SELECT DATABASE()").fetch.fetch(:"DATABASE()")
|
69
|
+
end
|
70
|
+
|
61
71
|
@result_constant ||= Baza::Driver::ActiveRecord::Result
|
62
72
|
end
|
63
73
|
|
@@ -65,22 +75,22 @@ class Baza::Driver::ActiveRecord
|
|
65
75
|
@result_constant.new(self, @conn.execute(sql))
|
66
76
|
end
|
67
77
|
|
68
|
-
|
78
|
+
alias_method :query_ubuf, :query
|
69
79
|
|
70
80
|
def escape(str)
|
71
81
|
@conn.quote_string(str.to_s)
|
72
82
|
end
|
73
83
|
|
74
|
-
def
|
84
|
+
def escape_column(string)
|
75
85
|
string = string.to_s
|
76
86
|
raise "Invalid column-string: #{string}" if string.include?(@sep_col)
|
77
|
-
|
87
|
+
string
|
78
88
|
end
|
79
89
|
|
80
|
-
def
|
90
|
+
def escape_table(string)
|
81
91
|
string = string.to_s
|
82
92
|
raise "Invalid column-string: #{string}" if string.include?(@sep_col)
|
83
|
-
|
93
|
+
string
|
84
94
|
end
|
85
95
|
|
86
96
|
def close
|
@@ -88,9 +98,9 @@ class Baza::Driver::ActiveRecord
|
|
88
98
|
end
|
89
99
|
|
90
100
|
def transaction
|
91
|
-
if @
|
101
|
+
if @driver_type == :mysql || @driver_type == :mysql2
|
92
102
|
query("START TRANSACTION")
|
93
|
-
elsif @
|
103
|
+
elsif @driver_type == :sqlite3
|
94
104
|
query("BEGIN TRANSACTION")
|
95
105
|
else
|
96
106
|
raise "Don't know how to start transaction"
|
@@ -104,4 +114,8 @@ class Baza::Driver::ActiveRecord
|
|
104
114
|
raise
|
105
115
|
end
|
106
116
|
end
|
117
|
+
|
118
|
+
def supports_multiple_databases?
|
119
|
+
conn_name.include?("mysql")
|
120
|
+
end
|
107
121
|
end
|
@@ -1,31 +1,32 @@
|
|
1
|
-
#This class handels every MySQL-column, that can be returned from a table-object.
|
1
|
+
# This class handels every MySQL-column, that can be returned from a table-object.
|
2
2
|
class Baza::Driver::Mysql::Column < Baza::Column
|
3
3
|
attr_reader :args, :name
|
4
4
|
|
5
|
-
#Constructor. Should not be called manually.
|
5
|
+
# Constructor. Should not be called manually.
|
6
6
|
def initialize(args)
|
7
7
|
@args = args
|
8
|
-
@
|
9
|
-
@
|
8
|
+
@data = @args.delete(:data)
|
9
|
+
@name = @data.fetch(:Field)
|
10
|
+
@db = @args.fetch(:db)
|
10
11
|
end
|
11
12
|
|
12
|
-
#Used to validate in Wref::Map.
|
13
|
+
# Used to validate in Wref::Map.
|
13
14
|
def __object_unique_id__
|
14
|
-
|
15
|
+
@name
|
15
16
|
end
|
16
17
|
|
17
18
|
def table_name
|
18
|
-
@args
|
19
|
+
@args.fetch(:table_name)
|
19
20
|
end
|
20
21
|
|
21
|
-
#Returns the table-object that this column belongs to.
|
22
|
+
# Returns the table-object that this column belongs to.
|
22
23
|
def table
|
23
|
-
|
24
|
+
@db.tables[table_name]
|
24
25
|
end
|
25
26
|
|
26
|
-
#Returns all data of the column in the knjdb-format.
|
27
|
+
# Returns all data of the column in the knjdb-format.
|
27
28
|
def data
|
28
|
-
|
29
|
+
{
|
29
30
|
type: type,
|
30
31
|
name: name,
|
31
32
|
null: null?,
|
@@ -37,96 +38,98 @@ class Baza::Driver::Mysql::Column < Baza::Column
|
|
37
38
|
end
|
38
39
|
|
39
40
|
def reload
|
40
|
-
|
41
|
+
data = @db.query("SHOW FULL COLUMNS FROM `#{@db.escape_table(table_name)}` WHERE `Field` = '#{@db.esc(name)}'").fetch
|
42
|
+
raise Baza::Errors::ColumnNotFound unless data
|
43
|
+
@data = data
|
41
44
|
@type = nil
|
42
45
|
end
|
43
46
|
|
44
|
-
#Returns the type of the column (integer, varchar etc.).
|
47
|
+
# Returns the type of the column (integer, varchar etc.).
|
45
48
|
def type
|
46
|
-
|
47
|
-
if match = @
|
49
|
+
unless @type
|
50
|
+
if match = @data[:Type].match(/^([A-z]+)$/)
|
48
51
|
@maxlength = false
|
49
52
|
@type = match[0].to_sym
|
50
|
-
elsif match = @
|
53
|
+
elsif match = @data[:Type].match(/^decimal\((\d+),(\d+)\)$/)
|
51
54
|
@maxlength = "#{match[1]},#{match[2]}"
|
52
55
|
@type = :decimal
|
53
|
-
elsif match = @
|
56
|
+
elsif match = @data[:Type].match(/^enum\((.+)\)$/)
|
54
57
|
@maxlength = match[1]
|
55
58
|
@type = :enum
|
56
|
-
elsif match = @
|
59
|
+
elsif match = @data[:Type].match(/^(.+)\((\d+)\)/)
|
57
60
|
@maxlength = match[2].to_i
|
58
61
|
@type = match[1].to_sym
|
59
62
|
end
|
60
63
|
|
61
|
-
raise "Still not type from: '#{@
|
64
|
+
raise "Still not type from: '#{@data[:Type]}'." if @type.to_s.strip.empty?
|
62
65
|
end
|
63
66
|
|
64
|
-
|
67
|
+
@type
|
65
68
|
end
|
66
69
|
|
67
|
-
#Return true if the columns allows null. Otherwise false.
|
70
|
+
# Return true if the columns allows null. Otherwise false.
|
68
71
|
def null?
|
69
|
-
return false if @
|
70
|
-
|
72
|
+
return false if @data[:Null] == "NO"
|
73
|
+
true
|
71
74
|
end
|
72
75
|
|
73
|
-
#Returns the maxlength.
|
76
|
+
# Returns the maxlength.
|
74
77
|
def maxlength
|
75
|
-
|
78
|
+
type unless @maxlength
|
76
79
|
return @maxlength if @maxlength
|
77
|
-
|
80
|
+
false
|
78
81
|
end
|
79
82
|
|
80
|
-
#Returns the default value for the column.
|
83
|
+
# Returns the default value for the column.
|
81
84
|
def default
|
82
|
-
return false if (
|
83
|
-
return false if (
|
84
|
-
return false
|
85
|
-
|
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]
|
88
|
+
@data.fetch(:Default)
|
86
89
|
end
|
87
90
|
|
88
|
-
#Returns true if the column is the primary key. Otherwise false.
|
91
|
+
# Returns true if the column is the primary key. Otherwise false.
|
89
92
|
def primarykey?
|
90
|
-
return true if @
|
91
|
-
|
93
|
+
return true if @data.fetch(:Key) == "PRI"
|
94
|
+
false
|
92
95
|
end
|
93
96
|
|
94
|
-
#Returns true if the column is auto-increasing. Otherwise false.
|
97
|
+
# Returns true if the column is auto-increasing. Otherwise false.
|
95
98
|
def autoincr?
|
96
|
-
return true if @
|
97
|
-
|
99
|
+
return true if @data.fetch(:Extra).include?("auto_increment")
|
100
|
+
false
|
98
101
|
end
|
99
102
|
|
100
|
-
#Returns the comment for the column.
|
103
|
+
# Returns the comment for the column.
|
101
104
|
def comment
|
102
|
-
|
105
|
+
@data.fetch(:Comment)
|
103
106
|
end
|
104
107
|
|
105
|
-
#Drops the column from the table.
|
108
|
+
# Drops the column from the table.
|
106
109
|
def drop
|
107
|
-
@
|
110
|
+
@db.query("ALTER TABLE `#{@db.escape_table(table_name)}` DROP COLUMN `#{@db.escape_column(name)}`")
|
108
111
|
table.__send__(:remove_column_from_list, self)
|
109
|
-
|
112
|
+
nil
|
110
113
|
end
|
111
114
|
|
112
|
-
#Changes the column properties by the given hash.
|
115
|
+
# Changes the column properties by the given hash.
|
113
116
|
def change(data)
|
114
|
-
col_escaped = "`#{@db.
|
115
|
-
table_escape = "`#{@db.
|
117
|
+
col_escaped = "`#{@db.escape_column(name)}`"
|
118
|
+
table_escape = "`#{@db.escape_table(table_name)}`"
|
116
119
|
newdata = data.clone
|
117
120
|
|
118
121
|
newdata[:name] = name unless newdata.key?(:name)
|
119
122
|
newdata[:type] = type unless newdata.key?(:type)
|
120
123
|
newdata[:maxlength] = maxlength if !newdata.key?(:maxlength) && maxlength
|
121
124
|
newdata[:null] = null? unless newdata.key?(:null)
|
122
|
-
newdata[:default] =
|
125
|
+
newdata[:default] = default if !newdata.key?(:default) && default
|
123
126
|
newdata.delete(:primarykey) if newdata.key?(:primarykey)
|
124
127
|
|
125
128
|
drop_add = true if name.to_s != newdata[:name].to_s
|
126
129
|
|
127
130
|
table.__send__(:remove_column_from_list, self) if drop_add
|
128
|
-
@db.query("ALTER TABLE #{table_escape} CHANGE #{col_escaped} #{@
|
129
|
-
@name = newdata[:name].
|
131
|
+
@db.query("ALTER TABLE #{table_escape} CHANGE #{col_escaped} #{@db.cols.data_sql(newdata)}")
|
132
|
+
@name = newdata[:name].to_s
|
130
133
|
reload
|
131
134
|
table.__send__(:add_column_to_list, self) if drop_add
|
132
135
|
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
#This class handels various MySQL-column-specific operations.
|
1
|
+
# This class handels various MySQL-column-specific operations.
|
2
2
|
class Baza::Driver::Mysql::Columns
|
3
|
-
#Constructor. Should not be called manually.
|
3
|
+
# Constructor. Should not be called manually.
|
4
4
|
def initialize(args)
|
5
5
|
@args = args
|
6
6
|
end
|
7
7
|
|
8
|
-
#Returns the SQL for this column.
|
8
|
+
# Returns the SQL for this column.
|
9
9
|
DATA_SQL_ALLOWED_KEYS = [:type, :maxlength, :name, :primarykey, :autoincr, :default, :comment, :after, :first, :storage, :null, :renames]
|
10
10
|
def data_sql(data)
|
11
|
-
data.
|
12
|
-
raise "Invalid key: '#{key}' (#{key.class.name})."
|
11
|
+
data.each_key do |key|
|
12
|
+
raise "Invalid key: '#{key}' (#{key.class.name})." unless DATA_SQL_ALLOWED_KEYS.include?(key)
|
13
13
|
end
|
14
14
|
|
15
15
|
raise "No type given." unless data[:type]
|
@@ -21,7 +21,7 @@ class Baza::Driver::Mysql::Columns
|
|
21
21
|
sql << "(#{data[:maxlength]})" if data[:maxlength]
|
22
22
|
sql << " PRIMARY KEY" if data[:primarykey]
|
23
23
|
sql << " AUTO_INCREMENT" if data[:autoincr]
|
24
|
-
sql << " NOT NULL"
|
24
|
+
sql << " NOT NULL" unless data[:null]
|
25
25
|
|
26
26
|
if data.key?(:default_func)
|
27
27
|
sql << " DEFAULT #{data[:default_func]}"
|
@@ -30,10 +30,10 @@ class Baza::Driver::Mysql::Columns
|
|
30
30
|
end
|
31
31
|
|
32
32
|
sql << " COMMENT '#{@args[:db].escape(data[:comment])}'" if data.key?(:comment)
|
33
|
-
sql << " AFTER `#{@args[:db].
|
33
|
+
sql << " AFTER `#{@args[:db].escape_column(data[:after])}`" if data[:after] && !data[:first]
|
34
34
|
sql << " FIRST" if data[:first]
|
35
35
|
sql << " STORAGE #{data[:storage].to_s.upcase}" if data[:storage]
|
36
36
|
|
37
|
-
|
37
|
+
sql
|
38
38
|
end
|
39
39
|
end
|
@@ -0,0 +1,28 @@
|
|
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
|
+
private
|
14
|
+
|
15
|
+
def rename(new_name)
|
16
|
+
new_name = new_name.to_s
|
17
|
+
@db.databases.create(name: new_name)
|
18
|
+
|
19
|
+
tables.each do |table|
|
20
|
+
@db.query("ALTER TABLE `#{@db.escape_database(name_was)}`.`#{@db.escape_table(table.name)}` RENAME `#{@db.escape_database(name)}`.`#{@db.escape_table(table.name)}`")
|
21
|
+
end
|
22
|
+
|
23
|
+
@db.query("DROP DATABASE `#{@db.escape_database(name_was)}`")
|
24
|
+
|
25
|
+
@name = new_name
|
26
|
+
@name_was = new_name
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,35 @@
|
|
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.escape_table(args.fetch(:name))}`"
|
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
|
+
end
|
@@ -1,68 +1,78 @@
|
|
1
1
|
class Baza::Driver::Mysql::Index < Baza::Index
|
2
2
|
attr_reader :args, :columns
|
3
|
+
attr_accessor :table_name
|
3
4
|
|
4
5
|
def initialize(args)
|
5
|
-
@
|
6
|
+
@db = args.fetch(:db)
|
7
|
+
@data = args.fetch(:data)
|
8
|
+
@table_name = args.fetch(:table_name)
|
6
9
|
@columns = []
|
7
10
|
end
|
8
11
|
|
9
|
-
#Used to validate in Wref::Map.
|
12
|
+
# Used to validate in Wref::Map.
|
10
13
|
def __object_unique_id__
|
11
|
-
|
14
|
+
name
|
12
15
|
end
|
13
16
|
|
14
17
|
def name
|
15
|
-
|
18
|
+
@data.fetch(:Key_name)
|
16
19
|
end
|
17
20
|
|
18
21
|
def table
|
19
|
-
|
22
|
+
@db.tables[@table_name]
|
20
23
|
end
|
21
24
|
|
22
25
|
def drop
|
23
|
-
sql = "DROP INDEX `#{
|
26
|
+
sql = "DROP INDEX `#{name}` ON `#{@table_name}`"
|
24
27
|
|
25
28
|
begin
|
26
|
-
@
|
29
|
+
@db.query(sql)
|
27
30
|
rescue => e
|
28
|
-
#The index has already been dropped - ignore.
|
29
|
-
if e.message.index("check that column/key exists")
|
30
|
-
#ignore.
|
31
|
-
else
|
32
|
-
raise e
|
33
|
-
end
|
31
|
+
# The index has already been dropped - ignore.
|
32
|
+
raise e if e.message.index("check that column/key exists") == nil
|
34
33
|
end
|
34
|
+
|
35
|
+
self
|
35
36
|
end
|
36
37
|
|
37
|
-
def rename
|
38
|
-
newname = newname.
|
38
|
+
def rename(newname)
|
39
|
+
newname = newname.to_s
|
39
40
|
create_args = data
|
40
41
|
create_args[:name] = newname
|
41
42
|
|
42
43
|
drop
|
43
44
|
table.create_indexes([create_args])
|
44
|
-
@
|
45
|
+
@data[:Key_name] = newname
|
46
|
+
|
47
|
+
self
|
45
48
|
end
|
46
49
|
|
47
50
|
def data
|
48
|
-
|
51
|
+
{
|
49
52
|
name: name,
|
50
53
|
columns: @columns
|
51
54
|
}
|
52
55
|
end
|
53
56
|
|
54
|
-
#Returns true if the index is a unique-index.
|
57
|
+
# Returns true if the index is a unique-index.
|
55
58
|
def unique?
|
56
|
-
if @
|
59
|
+
if @data.fetch(:Index_type) == "UNIQUE" || @data.fetch(:Non_unique).to_i == 0
|
57
60
|
return true
|
58
61
|
else
|
59
62
|
return false
|
60
63
|
end
|
61
64
|
end
|
62
65
|
|
63
|
-
#Returns true if the index is a primary-index.
|
66
|
+
# Returns true if the index is a primary-index.
|
64
67
|
def primary?
|
65
|
-
return true if @
|
66
|
-
|
68
|
+
return true if @data.fetch(:Key_name) == "PRIMARY"
|
69
|
+
false
|
70
|
+
end
|
71
|
+
|
72
|
+
def reload
|
73
|
+
data = @db.query("SHOW INDEX FROM `#{@db.escape_table(@table_name)}` WHERE `Key_name` = '#{@db.esc(name)}'").fetch
|
74
|
+
raise Baza::Errors::IndexNotFound unless data
|
75
|
+
@data = data
|
76
|
+
self
|
67
77
|
end
|
68
78
|
end
|