baza 0.0.20 → 0.0.21
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +84 -0
- data/.rubocop_todo.yml +17 -135
- data/.travis.yml +21 -0
- data/Gemfile +10 -7
- data/Gemfile.lock +39 -44
- data/README.md +61 -3
- data/VERSION +1 -1
- data/baza.gemspec +146 -98
- data/config/best_project_practice_rubocop.yml +8 -0
- data/config/best_project_practice_rubocop_todo.yml +6 -0
- data/lib/baza.rb +8 -12
- data/lib/baza/base_sql_driver.rb +198 -52
- data/lib/baza/cloner.rb +1 -0
- data/lib/baza/column.rb +26 -0
- data/lib/baza/database.rb +19 -3
- data/lib/baza/db.rb +69 -271
- data/lib/baza/driver.rb +1 -6
- data/lib/baza/{drivers → driver}/active_record.rb +65 -21
- data/lib/baza/{drivers → driver}/active_record/columns.rb +0 -0
- data/lib/baza/driver/active_record/commands.rb +10 -0
- data/lib/baza/driver/active_record/databases.rb +10 -0
- data/lib/baza/{drivers → driver}/active_record/indexes.rb +0 -0
- data/lib/baza/{drivers → driver}/active_record/result.rb +3 -1
- data/lib/baza/{drivers → driver}/active_record/tables.rb +0 -0
- data/lib/baza/driver/active_record/users.rb +12 -0
- data/lib/baza/{drivers → driver}/mysql.rb +9 -26
- data/lib/baza/{drivers → driver}/mysql/column.rb +14 -35
- data/lib/baza/{drivers → driver}/mysql/columns.rb +9 -12
- data/lib/baza/driver/mysql/commands.rb +39 -0
- data/lib/baza/driver/mysql/database.rb +64 -0
- data/lib/baza/driver/mysql/databases.rb +63 -0
- data/lib/baza/{drivers → driver}/mysql/index.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql/indexes.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql/result.rb +15 -7
- data/lib/baza/{drivers → driver}/mysql/sqlspecs.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql/table.rb +27 -43
- data/lib/baza/{drivers → driver}/mysql/tables.rb +5 -34
- data/lib/baza/{drivers → driver}/mysql/unbuffered_result.rb +8 -2
- data/lib/baza/driver/mysql/user.rb +22 -0
- data/lib/baza/driver/mysql/users.rb +39 -0
- data/lib/baza/{drivers → driver}/mysql2.rb +19 -49
- data/lib/baza/{drivers → driver}/mysql2/column.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql2/columns.rb +0 -0
- data/lib/baza/driver/mysql2/commands.rb +2 -0
- data/lib/baza/{drivers → driver}/mysql2/database.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql2/databases.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql2/index.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql2/indexes.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql2/result.rb +3 -1
- data/lib/baza/{drivers → driver}/mysql2/table.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql2/tables.rb +0 -0
- data/lib/baza/driver/mysql2/user.rb +2 -0
- data/lib/baza/driver/mysql2/users.rb +2 -0
- data/lib/baza/{drivers → driver}/mysql_java.rb +60 -38
- data/lib/baza/{drivers → driver}/mysql_java/column.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql_java/columns.rb +0 -0
- data/lib/baza/driver/mysql_java/commands.rb +2 -0
- data/lib/baza/driver/mysql_java/database.rb +2 -0
- data/lib/baza/driver/mysql_java/databases.rb +2 -0
- data/lib/baza/{drivers → driver}/mysql_java/index.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql_java/indexes.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql_java/table.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql_java/tables.rb +0 -0
- data/lib/baza/driver/mysql_java/user.rb +2 -0
- data/lib/baza/driver/mysql_java/users.rb +2 -0
- data/lib/baza/driver/pg.rb +80 -0
- data/lib/baza/driver/pg/column.rb +125 -0
- data/lib/baza/driver/pg/columns.rb +37 -0
- data/lib/baza/driver/pg/commands.rb +35 -0
- data/lib/baza/driver/pg/create_index_sql_creator.rb +51 -0
- data/lib/baza/driver/pg/database.rb +89 -0
- data/lib/baza/driver/pg/databases.rb +79 -0
- data/lib/baza/driver/pg/index.rb +35 -0
- data/lib/baza/driver/pg/indexes.rb +5 -0
- data/lib/baza/driver/pg/result.rb +139 -0
- data/lib/baza/driver/pg/table.rb +184 -0
- data/lib/baza/driver/pg/tables.rb +45 -0
- data/lib/baza/{drivers → driver}/sqlite3.rb +6 -24
- data/lib/baza/{drivers → driver}/sqlite3/column.rb +22 -24
- data/lib/baza/{drivers → driver}/sqlite3/columns.rb +6 -6
- data/lib/baza/driver/sqlite3/commands.rb +28 -0
- data/lib/baza/{drivers → driver}/sqlite3/database.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3/databases.rb +0 -1
- data/lib/baza/{drivers → driver}/sqlite3/index.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3/indexes.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3/result.rb +14 -6
- data/lib/baza/{drivers → driver}/sqlite3/sqlspecs.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3/table.rb +25 -16
- data/lib/baza/{drivers → driver}/sqlite3/tables.rb +5 -6
- data/lib/baza/{drivers → driver}/sqlite3/unbuffered_result.rb +8 -2
- data/lib/baza/{drivers → driver}/sqlite3_java.rb +13 -23
- data/lib/baza/{drivers → driver}/sqlite3_java/column.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/columns.rb +0 -0
- data/lib/baza/driver/sqlite3_java/commands.rb +2 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/database.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/index.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/indexes.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/table.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/tables.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/unbuffered_result.rb +14 -9
- data/lib/baza/{drivers → driver}/sqlite3_rhodes.rb +6 -24
- data/lib/baza/errors.rb +2 -0
- data/lib/baza/idquery.rb +15 -8
- data/lib/baza/index.rb +7 -0
- data/lib/baza/jdbc_driver.rb +4 -16
- data/lib/baza/jdbc_result.rb +20 -12
- data/lib/baza/mysql_base_driver.rb +7 -7
- data/lib/baza/query_buffer.rb +20 -19
- data/lib/baza/row.rb +16 -16
- data/lib/baza/sql_queries.rb +3 -0
- data/lib/baza/sql_queries/generic_insert.rb +81 -0
- data/lib/baza/sql_queries/generic_update.rb +31 -0
- data/lib/baza/sql_queries/mysql_upsert.rb +52 -0
- data/lib/baza/sql_queries/mysql_upsert_duplicate_key.rb +57 -0
- data/lib/baza/sql_queries/non_atomic_upsert.rb +25 -0
- data/lib/baza/sql_queries/postgres_upsert_duplicate_key.rb +118 -0
- data/lib/baza/sql_queries/select.rb +170 -0
- data/lib/baza/sql_queries/sqlite_upsert_duplicate_key.rb +99 -0
- data/lib/baza/table.rb +35 -8
- data/spec/active_record/models/user.rb +3 -0
- data/spec/{cloner_spec.rb → baza/cloner_spec.rb} +0 -0
- data/spec/drivers/active_record_mysql2_spec.rb +5 -3
- data/spec/drivers/active_record_mysql_spec.rb +2 -1
- data/spec/drivers/active_record_pg_spec.rb +20 -0
- data/spec/drivers/active_record_sqlite3_spec.rb +2 -1
- data/spec/drivers/mysql2_spec.rb +1 -1
- data/spec/drivers/mysql_spec.rb +10 -10
- data/spec/drivers/pg_spec.rb +18 -0
- data/spec/drivers/sqlite3_spec.rb +7 -8
- data/spec/info_active_record_example.rb +1 -1
- data/spec/{info_active_record_mysql2.rb → info_active_record_mysql2_example.rb} +3 -2
- data/spec/info_active_record_mysql2_travis.rb +35 -0
- data/spec/{info_active_record_mysql.rb → info_active_record_mysql_example.rb} +5 -4
- data/spec/info_active_record_mysql_travis.rb +36 -0
- data/spec/info_active_record_pg_example.rb +36 -0
- data/spec/info_active_record_pg_travis.rb +34 -0
- data/spec/info_active_record_sqlite3.rb +1 -1
- data/spec/info_mysql2_example.rb +1 -3
- data/spec/{info_mysql2_shippable.rb → info_mysql2_travis.rb} +2 -4
- data/spec/info_mysql_example.rb +1 -3
- data/spec/{info_mysql_shippable.rb → info_mysql_travis.rb} +2 -4
- data/spec/info_pg_example.rb +22 -0
- data/spec/info_pg_travis.rb +20 -0
- data/spec/info_sqlite3.rb +1 -3
- data/spec/spec_helper.rb +1 -1
- data/spec/support/driver_active_record_collection.rb +62 -0
- data/spec/support/driver_collection.rb +136 -121
- data/spec/support/driver_columns_collection.rb +19 -10
- data/spec/support/driver_databases_collection.rb +23 -1
- data/spec/support/driver_indexes_collection.rb +2 -2
- data/spec/support/driver_tables_collection.rb +24 -4
- data/spec/support/driver_users_collection.rb +53 -0
- metadata +185 -104
- data/lib/baza/drivers/mysql/database.rb +0 -28
- data/lib/baza/drivers/mysql/databases.rb +0 -35
- data/lib/baza/drivers/mysql_java/database.rb +0 -2
- data/lib/baza/drivers/mysql_java/databases.rb +0 -2
- data/lib/baza/model.rb +0 -875
- data/lib/baza/model_custom.rb +0 -155
- data/lib/baza/model_handler.rb +0 -910
- data/lib/baza/model_handler_sqlhelper.rb +0 -484
- data/lib/baza/revision.rb +0 -383
- data/shippable.yml +0 -17
- data/spec/info_active_record_mysql2_shippable.rb +0 -34
- data/spec/info_active_record_mysql_shippable.rb +0 -34
- data/spec/model_handler_spec.rb +0 -431
@@ -50,8 +50,14 @@ class Baza::Driver::Mysql::UnbufferedResult < Baza::ResultBase
|
|
50
50
|
|
51
51
|
# Loops over every single result yielding it.
|
52
52
|
def each
|
53
|
-
|
54
|
-
|
53
|
+
loop do
|
54
|
+
row = fetch
|
55
|
+
|
56
|
+
if row
|
57
|
+
yield row
|
58
|
+
else
|
59
|
+
break
|
60
|
+
end
|
55
61
|
end
|
56
62
|
end
|
57
63
|
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class Baza::Driver::Mysql::User
|
2
|
+
attr_reader :name
|
3
|
+
|
4
|
+
def initialize(args)
|
5
|
+
@args = args
|
6
|
+
@data = args.fetch(:data)
|
7
|
+
@db = args.fetch(:db)
|
8
|
+
end
|
9
|
+
|
10
|
+
def name
|
11
|
+
@data.fetch(:User)
|
12
|
+
end
|
13
|
+
|
14
|
+
def host
|
15
|
+
@data.fetch(:Host)
|
16
|
+
end
|
17
|
+
|
18
|
+
def drop
|
19
|
+
@db.query("DROP USER '#{@db.esc(name)}'@'#{@db.esc(host)}'")
|
20
|
+
true
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require_relative "user"
|
2
|
+
|
3
|
+
class Baza::Driver::Mysql::Users
|
4
|
+
def initialize(args)
|
5
|
+
@args = args
|
6
|
+
@db = @args.fetch(:db)
|
7
|
+
end
|
8
|
+
|
9
|
+
def list
|
10
|
+
result = []
|
11
|
+
@db.query("SELECT * FROM mysql.user") do |user_data|
|
12
|
+
user = Baza::Driver::Mysql::User.new(
|
13
|
+
db: @db,
|
14
|
+
data: user_data
|
15
|
+
)
|
16
|
+
|
17
|
+
if block_given?
|
18
|
+
yield user
|
19
|
+
else
|
20
|
+
result << user
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
result unless block_given?
|
25
|
+
end
|
26
|
+
|
27
|
+
def find_by_name(name)
|
28
|
+
list do |user|
|
29
|
+
return user if user.name == name.to_s
|
30
|
+
end
|
31
|
+
|
32
|
+
raise Baza::Errors::UserNotFound, "Could not find a user by that name: #{name}"
|
33
|
+
end
|
34
|
+
|
35
|
+
def create(data)
|
36
|
+
@db.query("CREATE USER '#{@db.esc(data.fetch(:name))}'@'#{@db.esc(data.fetch(:host))}' IDENTIFIED BY '#{data.fetch(:password)}'")
|
37
|
+
find_by_name(data.fetch(:name))
|
38
|
+
end
|
39
|
+
end
|
@@ -1,19 +1,7 @@
|
|
1
1
|
Baza.load_driver("mysql")
|
2
2
|
|
3
3
|
class Baza::Driver::Mysql2 < Baza::MysqlBaseDriver
|
4
|
-
|
5
|
-
|
6
|
-
autoload :Database, "#{path}/database"
|
7
|
-
autoload :Databases, "#{path}/databases"
|
8
|
-
autoload :Table, "#{path}/table"
|
9
|
-
autoload :Tables, "#{path}/tables"
|
10
|
-
autoload :Column, "#{path}/column"
|
11
|
-
autoload :Columns, "#{path}/columns"
|
12
|
-
autoload :Index, "#{path}/index"
|
13
|
-
autoload :Indexes, "#{path}/indexes"
|
14
|
-
autoload :Result, "#{path}/result"
|
15
|
-
autoload :UnbufferedResult, "#{path}/unbuffered_result"
|
16
|
-
autoload :Sqlspecs, "#{path}/sqlspecs"
|
4
|
+
AutoAutoloader.autoload_sub_classes(self, __FILE__)
|
17
5
|
|
18
6
|
attr_reader :conn, :conns
|
19
7
|
|
@@ -35,10 +23,10 @@ class Baza::Driver::Mysql2 < Baza::MysqlBaseDriver
|
|
35
23
|
nil
|
36
24
|
end
|
37
25
|
|
38
|
-
def initialize(
|
26
|
+
def initialize(db)
|
39
27
|
super
|
40
28
|
|
41
|
-
@opts = @
|
29
|
+
@opts = @db.opts
|
42
30
|
|
43
31
|
require "monitor"
|
44
32
|
@mutex = Monitor.new
|
@@ -49,8 +37,8 @@ class Baza::Driver::Mysql2 < Baza::MysqlBaseDriver
|
|
49
37
|
@encoding = "utf8"
|
50
38
|
end
|
51
39
|
|
52
|
-
if @
|
53
|
-
@port = @
|
40
|
+
if @db.opts.key?(:port)
|
41
|
+
@port = @db.opts[:port].to_i
|
54
42
|
else
|
55
43
|
@port = 3306
|
56
44
|
end
|
@@ -66,11 +54,13 @@ class Baza::Driver::Mysql2 < Baza::MysqlBaseDriver
|
|
66
54
|
# Respawns the connection to the MySQL-database.
|
67
55
|
def reconnect
|
68
56
|
@mutex.synchronize do
|
57
|
+
require "mysql2" unless ::Object.const_defined?("Mysql2")
|
58
|
+
|
69
59
|
args = {
|
70
|
-
host: @
|
71
|
-
username: @
|
72
|
-
password: @
|
73
|
-
database: @
|
60
|
+
host: @db.opts[:host],
|
61
|
+
username: @db.opts[:user],
|
62
|
+
password: @db.opts[:pass],
|
63
|
+
database: @db.opts[:db],
|
74
64
|
port: @port,
|
75
65
|
symbolize_keys: true,
|
76
66
|
cache_rows: false
|
@@ -78,35 +68,20 @@ class Baza::Driver::Mysql2 < Baza::MysqlBaseDriver
|
|
78
68
|
|
79
69
|
# Symbolize keys should also be given here, else table-data wont be symbolized for some reason - knj.
|
80
70
|
@query_args = {symbolize_keys: true}
|
81
|
-
@query_args[:cast] = false unless @
|
82
|
-
@query_args.merge!(@
|
71
|
+
@query_args[:cast] = false unless @db.opts[:type_translation]
|
72
|
+
@query_args.merge!(@db.opts[:query_args]) if @db.opts[:query_args]
|
83
73
|
|
84
74
|
pos_args = [:as, :async, :cast_booleans, :database_timezone, :application_timezone, :cache_rows, :connect_flags, :cast]
|
85
75
|
pos_args.each do |key|
|
86
|
-
args[key] = @
|
76
|
+
args[key] = @db.opts[key] if @db.opts.key?(key)
|
87
77
|
end
|
88
78
|
|
89
79
|
args[:as] = :array
|
90
80
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
@conn = @baza.opts[:conn]
|
96
|
-
else
|
97
|
-
require "mysql2"
|
98
|
-
@conn = Mysql2::Client.new(args)
|
99
|
-
end
|
100
|
-
rescue => e
|
101
|
-
if tries <= 3
|
102
|
-
if e.message == "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)"
|
103
|
-
sleep 1
|
104
|
-
tries += 1
|
105
|
-
retry
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
raise e
|
81
|
+
if @db.opts[:conn]
|
82
|
+
@conn = @db.opts[:conn]
|
83
|
+
else
|
84
|
+
@conn = ::Mysql2::Client.new(args)
|
110
85
|
end
|
111
86
|
|
112
87
|
query("SET NAMES '#{esc(@encoding)}'") if @encoding
|
@@ -152,11 +127,6 @@ class Baza::Driver::Mysql2 < Baza::MysqlBaseDriver
|
|
152
127
|
@conn.escape(string.to_s)
|
153
128
|
end
|
154
129
|
|
155
|
-
# Returns the last inserted ID for the connection.
|
156
|
-
def last_id
|
157
|
-
@mutex.synchronize { return @conn.last_id.to_i }
|
158
|
-
end
|
159
|
-
|
160
130
|
# Closes the connection threadsafe.
|
161
131
|
def close
|
162
132
|
@mutex.synchronize { @conn.close }
|
@@ -165,7 +135,7 @@ class Baza::Driver::Mysql2 < Baza::MysqlBaseDriver
|
|
165
135
|
# Destroyes the connection.
|
166
136
|
def destroy
|
167
137
|
@conn = nil
|
168
|
-
@
|
138
|
+
@db = nil
|
169
139
|
@mutex = nil
|
170
140
|
@encoding = nil
|
171
141
|
@query_args = nil
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -3,7 +3,7 @@ class Baza::Driver::Mysql2::Result < Baza::ResultBase
|
|
3
3
|
# Constructor. This should not be called manually.
|
4
4
|
def initialize(driver, result)
|
5
5
|
@result = result
|
6
|
-
@type_translation = driver.
|
6
|
+
@type_translation = driver.db.opts[:type_translation]
|
7
7
|
end
|
8
8
|
|
9
9
|
# Returns a single result.
|
@@ -15,6 +15,8 @@ class Baza::Driver::Mysql2::Result < Baza::ResultBase
|
|
15
15
|
|
16
16
|
# Loops over every single result yielding it.
|
17
17
|
def each
|
18
|
+
return unless @result
|
19
|
+
|
18
20
|
@result.each(as: :hash, symbolize_keys: true) do |row|
|
19
21
|
next unless row # This sometimes happens when streaming results...
|
20
22
|
row = Hash[row.map { |k, v| [k, v.to_s] }] if @type_translation == :string
|
File without changes
|
File without changes
|
@@ -1,19 +1,7 @@
|
|
1
1
|
Baza.load_driver("mysql")
|
2
2
|
|
3
3
|
class Baza::Driver::MysqlJava < Baza::JdbcDriver
|
4
|
-
|
5
|
-
|
6
|
-
autoload :Database, "#{path}/database"
|
7
|
-
autoload :Databases, "#{path}/databases"
|
8
|
-
autoload :Table, "#{path}/table"
|
9
|
-
autoload :Tables, "#{path}/tables"
|
10
|
-
autoload :Column, "#{path}/column"
|
11
|
-
autoload :Columns, "#{path}/columns"
|
12
|
-
autoload :Index, "#{path}/index"
|
13
|
-
autoload :Indexes, "#{path}/indexes"
|
14
|
-
autoload :Result, "#{path}/result"
|
15
|
-
autoload :UnbufferedResult, "#{path}/unbuffered_result"
|
16
|
-
autoload :Sqlspecs, "#{path}/sqlspecs"
|
4
|
+
AutoAutoloader.autoload_sub_classes(self, __FILE__)
|
17
5
|
|
18
6
|
attr_reader :conn, :conns
|
19
7
|
|
@@ -32,19 +20,36 @@ class Baza::Driver::MysqlJava < Baza::JdbcDriver
|
|
32
20
|
nil
|
33
21
|
end
|
34
22
|
|
35
|
-
def
|
36
|
-
|
23
|
+
def self.args
|
24
|
+
[{
|
25
|
+
label: "Host",
|
26
|
+
name: "host"
|
27
|
+
}, {
|
28
|
+
label: "Port",
|
29
|
+
name: "port"
|
30
|
+
}, {
|
31
|
+
label: "Username",
|
32
|
+
name: "user"
|
33
|
+
}, {
|
34
|
+
label: "Password",
|
35
|
+
name: "pass"
|
36
|
+
}, {
|
37
|
+
label: "Database",
|
38
|
+
name: "db"
|
39
|
+
}, {
|
40
|
+
label: "Encoding",
|
41
|
+
name: "encoding"
|
42
|
+
}]
|
43
|
+
end
|
37
44
|
|
38
|
-
|
45
|
+
def initialize(db)
|
46
|
+
super
|
39
47
|
|
40
|
-
|
41
|
-
|
42
|
-
else
|
43
|
-
@encoding = "utf8"
|
44
|
-
end
|
48
|
+
@opts = @db.opts
|
49
|
+
@encoding = @opts[:encoding] || "utf8"
|
45
50
|
|
46
|
-
if @
|
47
|
-
@port = @
|
51
|
+
if @db.opts.key?(:port)
|
52
|
+
@port = @db.opts[:port].to_i
|
48
53
|
else
|
49
54
|
@port = 3306
|
50
55
|
end
|
@@ -56,12 +61,12 @@ class Baza::Driver::MysqlJava < Baza::JdbcDriver
|
|
56
61
|
# Respawns the connection to the MySQL-database.
|
57
62
|
def reconnect
|
58
63
|
@mutex.synchronize do
|
59
|
-
if @
|
64
|
+
if @db.opts[:conn]
|
60
65
|
@jdbc_loaded = true
|
61
|
-
@conn = @
|
66
|
+
@conn = @db.opts.fetch(:conn)
|
62
67
|
else
|
63
68
|
com.mysql.jdbc.Driver
|
64
|
-
@conn = java.sql::DriverManager.getConnection(
|
69
|
+
@conn = java.sql::DriverManager.getConnection(jdbc_connect_command)
|
65
70
|
end
|
66
71
|
|
67
72
|
query_no_result_set("SET SQL_MODE = ''")
|
@@ -69,13 +74,6 @@ class Baza::Driver::MysqlJava < Baza::JdbcDriver
|
|
69
74
|
end
|
70
75
|
end
|
71
76
|
|
72
|
-
# Returns the last inserted ID for the connection.
|
73
|
-
def last_id
|
74
|
-
data = query("SELECT LAST_INSERT_ID() AS id").fetch
|
75
|
-
return data[:id].to_i if data[:id]
|
76
|
-
raise "Could not figure out last inserted ID."
|
77
|
-
end
|
78
|
-
|
79
77
|
# Closes the connection threadsafe.
|
80
78
|
def close
|
81
79
|
@mutex.synchronize { @conn.close }
|
@@ -84,7 +82,7 @@ class Baza::Driver::MysqlJava < Baza::JdbcDriver
|
|
84
82
|
# Destroyes the connection.
|
85
83
|
def destroy
|
86
84
|
@conn = nil
|
87
|
-
@
|
85
|
+
@db = nil
|
88
86
|
@mutex = nil
|
89
87
|
@encoding = nil
|
90
88
|
@query_args = nil
|
@@ -92,7 +90,7 @@ class Baza::Driver::MysqlJava < Baza::JdbcDriver
|
|
92
90
|
end
|
93
91
|
|
94
92
|
# Inserts multiple rows in a table. Can return the inserted IDs if asked to in arguments.
|
95
|
-
def insert_multi(tablename, arr_hashes, args =
|
93
|
+
def insert_multi(tablename, arr_hashes, args = {})
|
96
94
|
sql = "INSERT INTO `#{tablename}` ("
|
97
95
|
|
98
96
|
first = true
|
@@ -129,7 +127,7 @@ class Baza::Driver::MysqlJava < Baza::JdbcDriver
|
|
129
127
|
sql << ","
|
130
128
|
end
|
131
129
|
|
132
|
-
sql << @
|
130
|
+
sql << @db.sqlval(val)
|
133
131
|
end
|
134
132
|
else
|
135
133
|
hash.each do |_key, val|
|
@@ -139,7 +137,7 @@ class Baza::Driver::MysqlJava < Baza::JdbcDriver
|
|
139
137
|
sql << ","
|
140
138
|
end
|
141
139
|
|
142
|
-
sql << @
|
140
|
+
sql << @db.sqlval(val)
|
143
141
|
end
|
144
142
|
end
|
145
143
|
end
|
@@ -172,11 +170,35 @@ class Baza::Driver::MysqlJava < Baza::JdbcDriver
|
|
172
170
|
query_no_result_set("START TRANSACTION")
|
173
171
|
|
174
172
|
begin
|
175
|
-
yield @
|
173
|
+
yield @db
|
176
174
|
query_no_result_set("COMMIT")
|
177
175
|
rescue
|
178
176
|
query_no_result_set("ROLLBACK")
|
179
177
|
raise
|
180
178
|
end
|
181
179
|
end
|
180
|
+
|
181
|
+
private
|
182
|
+
|
183
|
+
def jdbc_connect_command
|
184
|
+
conn_options = {
|
185
|
+
"populateInsertRowWithDefaultValues" => true,
|
186
|
+
"zeroDateTimeBehavior" => "round",
|
187
|
+
"holdResultsOpenOverStatementClose" => true
|
188
|
+
}
|
189
|
+
|
190
|
+
conn_options["user"] = @db.opts.fetch(:user) if @db.opts[:user]
|
191
|
+
conn_options["password"] = @db.opts.fetch(:pass) if db.opts[:pass]
|
192
|
+
conn_options["characterEncoding"] = @encoding if @encoding
|
193
|
+
|
194
|
+
conn_command = "jdbc:mysql://#{@db.opts.fetch(:host)}:#{@port}/#{@db.opts.fetch(:db)}?"
|
195
|
+
first = true
|
196
|
+
conn_options.each do |key, value|
|
197
|
+
conn_command << "&" unless first
|
198
|
+
first = false if first
|
199
|
+
conn_command << "#{key}=#{value}"
|
200
|
+
end
|
201
|
+
|
202
|
+
conn_command
|
203
|
+
end
|
182
204
|
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|