baza 0.0.19 → 0.0.20
Sign up to get free protection for your applications and to get access to all the features.
- 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/drivers/mysql.rb
CHANGED
@@ -1,220 +1,127 @@
|
|
1
|
-
class Baza::Driver::Mysql < Baza::
|
2
|
-
path = "#{File.dirname(__FILE__)}/mysql"
|
3
|
-
|
4
|
-
autoload :
|
5
|
-
autoload :
|
6
|
-
autoload :
|
7
|
-
autoload :
|
8
|
-
autoload :
|
9
|
-
autoload :
|
10
|
-
autoload :
|
11
|
-
autoload :
|
12
|
-
autoload :
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
@encoding =
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
@port =
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
#
|
99
|
-
def
|
100
|
-
@
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
@
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
@query_args = nil
|
129
|
-
@port = nil
|
130
|
-
end
|
131
|
-
|
132
|
-
#Inserts multiple rows in a table. Can return the inserted IDs if asked to in arguments.
|
133
|
-
def insert_multi(tablename, arr_hashes, args = nil)
|
134
|
-
sql = "INSERT INTO `#{tablename}` ("
|
135
|
-
|
136
|
-
first = true
|
137
|
-
if args && args[:keys]
|
138
|
-
keys = args[:keys]
|
139
|
-
elsif arr_hashes.first.is_a?(Hash)
|
140
|
-
keys = arr_hashes.first.keys
|
141
|
-
else
|
142
|
-
raise "Could not figure out keys."
|
143
|
-
end
|
144
|
-
|
145
|
-
keys.each do |col_name|
|
146
|
-
sql << "," unless first
|
147
|
-
first = false if first
|
148
|
-
sql << "`#{self.esc_col(col_name)}`"
|
149
|
-
end
|
150
|
-
|
151
|
-
sql << ") VALUES ("
|
152
|
-
|
153
|
-
first = true
|
154
|
-
arr_hashes.each do |hash|
|
155
|
-
if first
|
156
|
-
first = false
|
157
|
-
else
|
158
|
-
sql << "),("
|
159
|
-
end
|
160
|
-
|
161
|
-
first_key = true
|
162
|
-
if hash.is_a?(Array)
|
163
|
-
hash.each do |val|
|
164
|
-
if first_key
|
165
|
-
first_key = false
|
166
|
-
else
|
167
|
-
sql << ","
|
168
|
-
end
|
169
|
-
|
170
|
-
sql << @baza.sqlval(val)
|
171
|
-
end
|
172
|
-
else
|
173
|
-
hash.each do |key, val|
|
174
|
-
if first_key
|
175
|
-
first_key = false
|
176
|
-
else
|
177
|
-
sql << ","
|
178
|
-
end
|
179
|
-
|
180
|
-
sql << @baza.sqlval(val)
|
181
|
-
end
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
sql << ")"
|
186
|
-
|
187
|
-
return sql if args && args[:return_sql]
|
188
|
-
|
189
|
-
self.query(sql)
|
190
|
-
|
191
|
-
if args && args[:return_id]
|
192
|
-
first_id = self.last_id
|
193
|
-
raise "Invalid ID: #{first_id}" if first_id.to_i <= 0
|
194
|
-
ids = [first_id]
|
195
|
-
1.upto(arr_hashes.length - 1) do |count|
|
196
|
-
ids << first_id + count
|
197
|
-
end
|
198
|
-
|
199
|
-
ids_length = ids.length
|
200
|
-
arr_hashes_length = arr_hashes.length
|
201
|
-
raise "Invalid length (#{ids_length}, #{arr_hashes_length})." unless ids_length == arr_hashes_length
|
202
|
-
|
203
|
-
return ids
|
204
|
-
else
|
205
|
-
return nil
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
def transaction
|
210
|
-
@baza.q("START TRANSACTION")
|
211
|
-
|
212
|
-
begin
|
213
|
-
yield @baza
|
214
|
-
@baza.q("COMMIT")
|
215
|
-
rescue
|
216
|
-
@baza.q("ROLLBACK")
|
217
|
-
raise
|
218
|
-
end
|
219
|
-
end
|
220
|
-
end
|
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"
|
15
|
+
|
16
|
+
attr_reader :conn
|
17
|
+
|
18
|
+
def self.from_object(args)
|
19
|
+
raise "Mysql does not support auth extraction" if args[:object].class.name == "Mysql"
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize(baza)
|
23
|
+
super
|
24
|
+
|
25
|
+
@opts = @baza.opts
|
26
|
+
|
27
|
+
require "monitor"
|
28
|
+
@mutex = Monitor.new
|
29
|
+
|
30
|
+
if baza.opts[:conn]
|
31
|
+
@conn = baza.opts[:conn]
|
32
|
+
else
|
33
|
+
if @opts[:encoding]
|
34
|
+
@encoding = @opts[:encoding]
|
35
|
+
else
|
36
|
+
@encoding = "utf8"
|
37
|
+
end
|
38
|
+
|
39
|
+
if @baza.opts.key?(:port)
|
40
|
+
@port = @baza.opts[:port].to_i
|
41
|
+
else
|
42
|
+
@port = 3306
|
43
|
+
end
|
44
|
+
|
45
|
+
reconnect
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Cleans the wref-map holding the tables.
|
50
|
+
def clean
|
51
|
+
tables.clean if tables
|
52
|
+
end
|
53
|
+
|
54
|
+
# Respawns the connection to the MySQL-database.
|
55
|
+
def reconnect
|
56
|
+
@mutex.synchronize do
|
57
|
+
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)
|
59
|
+
query("SET NAMES '#{esc(@encoding)}'") if @encoding
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Executes a query and returns the result.
|
64
|
+
def query(str)
|
65
|
+
str = str.to_s
|
66
|
+
str = str.force_encoding("UTF-8") if @encoding == "utf8" && str.respond_to?(:force_encoding)
|
67
|
+
tries = 0
|
68
|
+
|
69
|
+
begin
|
70
|
+
tries += 1
|
71
|
+
@mutex.synchronize do
|
72
|
+
return Baza::Driver::Mysql::Result.new(self, @conn.query(str))
|
73
|
+
end
|
74
|
+
rescue => e
|
75
|
+
if tries <= 3
|
76
|
+
if e.message == "MySQL server has gone away" || e.message == "closed MySQL connection" || e.message == "Can't connect to local MySQL server through socket"
|
77
|
+
sleep 0.5
|
78
|
+
reconnect
|
79
|
+
retry
|
80
|
+
elsif e.message.include?("No operations allowed after connection closed") || e.message == "This connection is still waiting for a result, try again once you have the result" || e.message == "Lock wait timeout exceeded; try restarting transaction"
|
81
|
+
reconnect
|
82
|
+
retry
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
raise e
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
# Executes an unbuffered query and returns the result that can be used to access the data.
|
91
|
+
def query_ubuf(str)
|
92
|
+
@mutex.synchronize do
|
93
|
+
@conn.query_with_result = false
|
94
|
+
return Baza::Driver::Mysql::UnbufferedResult.new(@conn, @opts, @conn.query(str))
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# Escapes a string to be safe to use in a query.
|
99
|
+
def escape_alternative(string)
|
100
|
+
@conn.escape_string(string.to_s)
|
101
|
+
end
|
102
|
+
|
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
|
+
# Closes the connection threadsafe.
|
109
|
+
def close
|
110
|
+
@mutex.synchronize { @conn.close }
|
111
|
+
end
|
112
|
+
|
113
|
+
# Destroyes the connection.
|
114
|
+
def destroy
|
115
|
+
@conn = nil
|
116
|
+
@baza = nil
|
117
|
+
@mutex = nil
|
118
|
+
@subtype = nil
|
119
|
+
@encoding = nil
|
120
|
+
@query_args = nil
|
121
|
+
@port = nil
|
122
|
+
end
|
123
|
+
|
124
|
+
def supports_multiple_databases?
|
125
|
+
true
|
126
|
+
end
|
127
|
+
end
|
@@ -8,11 +8,9 @@ class Baza::Driver::Mysql2::Result < Baza::ResultBase
|
|
8
8
|
|
9
9
|
# Returns a single result.
|
10
10
|
def fetch
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
return false
|
15
|
-
end
|
11
|
+
return to_enum.next
|
12
|
+
rescue StopIteration
|
13
|
+
return false
|
16
14
|
end
|
17
15
|
|
18
16
|
# Loops over every single result yielding it.
|
data/lib/baza/drivers/mysql2.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
|
-
|
1
|
+
Baza.load_driver("mysql")
|
2
|
+
|
3
|
+
class Baza::Driver::Mysql2 < Baza::MysqlBaseDriver
|
2
4
|
path = "#{File.dirname(__FILE__)}/mysql2"
|
3
5
|
|
6
|
+
autoload :Database, "#{path}/database"
|
7
|
+
autoload :Databases, "#{path}/databases"
|
4
8
|
autoload :Table, "#{path}/table"
|
5
9
|
autoload :Tables, "#{path}/tables"
|
6
10
|
autoload :Column, "#{path}/column"
|
@@ -13,7 +17,7 @@ class Baza::Driver::Mysql2 < Baza::BaseSqlDriver
|
|
13
17
|
|
14
18
|
attr_reader :conn, :conns
|
15
19
|
|
16
|
-
#Helper to enable automatic registering of database using Baza::Db.from_object
|
20
|
+
# Helper to enable automatic registering of database using Baza::Db.from_object
|
17
21
|
def self.from_object(args)
|
18
22
|
if args[:object].class.name == "Mysql2::Client"
|
19
23
|
return {
|
@@ -28,7 +32,7 @@ class Baza::Driver::Mysql2 < Baza::BaseSqlDriver
|
|
28
32
|
}
|
29
33
|
end
|
30
34
|
|
31
|
-
|
35
|
+
nil
|
32
36
|
end
|
33
37
|
|
34
38
|
def initialize(baza)
|
@@ -54,12 +58,12 @@ class Baza::Driver::Mysql2 < Baza::BaseSqlDriver
|
|
54
58
|
reconnect
|
55
59
|
end
|
56
60
|
|
57
|
-
#Cleans the wref-map holding the tables.
|
61
|
+
# Cleans the wref-map holding the tables.
|
58
62
|
def clean
|
59
63
|
tables.clean if tables
|
60
64
|
end
|
61
65
|
|
62
|
-
#Respawns the connection to the MySQL-database.
|
66
|
+
# Respawns the connection to the MySQL-database.
|
63
67
|
def reconnect
|
64
68
|
@mutex.synchronize do
|
65
69
|
args = {
|
@@ -72,8 +76,8 @@ class Baza::Driver::Mysql2 < Baza::BaseSqlDriver
|
|
72
76
|
cache_rows: false
|
73
77
|
}
|
74
78
|
|
75
|
-
#Symbolize keys should also be given here, else table-data wont be symbolized for some reason - knj.
|
76
|
-
@query_args = {symbolize_keys: true
|
79
|
+
# Symbolize keys should also be given here, else table-data wont be symbolized for some reason - knj.
|
80
|
+
@query_args = {symbolize_keys: true}
|
77
81
|
@query_args[:cast] = false unless @baza.opts[:type_translation]
|
78
82
|
@query_args.merge!(@baza.opts[:query_args]) if @baza.opts[:query_args]
|
79
83
|
|
@@ -109,7 +113,7 @@ class Baza::Driver::Mysql2 < Baza::BaseSqlDriver
|
|
109
113
|
end
|
110
114
|
end
|
111
115
|
|
112
|
-
#Executes a query and returns the result.
|
116
|
+
# Executes a query and returns the result.
|
113
117
|
def query(str)
|
114
118
|
str = str.to_s
|
115
119
|
str = str.force_encoding("UTF-8") if @encoding == "utf8" && str.respond_to?(:force_encoding)
|
@@ -122,11 +126,11 @@ class Baza::Driver::Mysql2 < Baza::BaseSqlDriver
|
|
122
126
|
end
|
123
127
|
rescue => e
|
124
128
|
if tries <= 3
|
125
|
-
if e.message == "MySQL server has gone away" || e.message == "closed MySQL connection"
|
129
|
+
if e.message == "MySQL server has gone away" || e.message == "closed MySQL connection" || e.message == "Can't connect to local MySQL server through socket"
|
126
130
|
sleep 0.5
|
127
131
|
reconnect
|
128
132
|
retry
|
129
|
-
elsif e.message.include?("No operations allowed after connection closed")
|
133
|
+
elsif e.message.include?("No operations allowed after connection closed") || e.message == "This connection is still waiting for a result, try again once you have the result" || e.message == "Lock wait timeout exceeded; try restarting transaction"
|
130
134
|
reconnect
|
131
135
|
retry
|
132
136
|
end
|
@@ -136,29 +140,29 @@ class Baza::Driver::Mysql2 < Baza::BaseSqlDriver
|
|
136
140
|
end
|
137
141
|
end
|
138
142
|
|
139
|
-
#Executes an unbuffered query and returns the result that can be used to access the data.
|
140
|
-
def query_ubuf(str)
|
143
|
+
# Executes an unbuffered query and returns the result that can be used to access the data.
|
144
|
+
def query_ubuf(str, _args = nil, &_blk)
|
141
145
|
@mutex.synchronize do
|
142
146
|
return Baza::Driver::Mysql2::Result.new(self, @conn.query(str, @query_args.merge(stream: true)))
|
143
147
|
end
|
144
148
|
end
|
145
149
|
|
146
|
-
#Escapes a string to be safe to use in a query.
|
150
|
+
# Escapes a string to be safe to use in a query.
|
147
151
|
def escape(string)
|
148
|
-
|
152
|
+
@conn.escape(string.to_s)
|
149
153
|
end
|
150
154
|
|
151
|
-
#Returns the last inserted ID for the connection.
|
155
|
+
# Returns the last inserted ID for the connection.
|
152
156
|
def last_id
|
153
157
|
@mutex.synchronize { return @conn.last_id.to_i }
|
154
158
|
end
|
155
159
|
|
156
|
-
#Closes the connection threadsafe.
|
160
|
+
# Closes the connection threadsafe.
|
157
161
|
def close
|
158
162
|
@mutex.synchronize { @conn.close }
|
159
163
|
end
|
160
164
|
|
161
|
-
#Destroyes the connection.
|
165
|
+
# Destroyes the connection.
|
162
166
|
def destroy
|
163
167
|
@conn = nil
|
164
168
|
@baza = nil
|
@@ -167,93 +171,4 @@ class Baza::Driver::Mysql2 < Baza::BaseSqlDriver
|
|
167
171
|
@query_args = nil
|
168
172
|
@port = nil
|
169
173
|
end
|
170
|
-
|
171
|
-
#Inserts multiple rows in a table. Can return the inserted IDs if asked to in arguments.
|
172
|
-
def insert_multi(tablename, arr_hashes, args = nil)
|
173
|
-
sql = "INSERT INTO `#{tablename}` ("
|
174
|
-
|
175
|
-
first = true
|
176
|
-
if args && args[:keys]
|
177
|
-
keys = args[:keys]
|
178
|
-
elsif arr_hashes.first.is_a?(Hash)
|
179
|
-
keys = arr_hashes.first.keys
|
180
|
-
else
|
181
|
-
raise "Could not figure out keys."
|
182
|
-
end
|
183
|
-
|
184
|
-
keys.each do |col_name|
|
185
|
-
sql << "," unless first
|
186
|
-
first = false if first
|
187
|
-
sql << "`#{self.esc_col(col_name)}`"
|
188
|
-
end
|
189
|
-
|
190
|
-
sql << ") VALUES ("
|
191
|
-
|
192
|
-
first = true
|
193
|
-
arr_hashes.each do |hash|
|
194
|
-
if first
|
195
|
-
first = false
|
196
|
-
else
|
197
|
-
sql << "),("
|
198
|
-
end
|
199
|
-
|
200
|
-
first_key = true
|
201
|
-
if hash.is_a?(Array)
|
202
|
-
hash.each do |val|
|
203
|
-
if first_key
|
204
|
-
first_key = false
|
205
|
-
else
|
206
|
-
sql << ","
|
207
|
-
end
|
208
|
-
|
209
|
-
sql << @baza.sqlval(val)
|
210
|
-
end
|
211
|
-
else
|
212
|
-
hash.each do |key, val|
|
213
|
-
if first_key
|
214
|
-
first_key = false
|
215
|
-
else
|
216
|
-
sql << ","
|
217
|
-
end
|
218
|
-
|
219
|
-
sql << @baza.sqlval(val)
|
220
|
-
end
|
221
|
-
end
|
222
|
-
end
|
223
|
-
|
224
|
-
sql << ")"
|
225
|
-
|
226
|
-
return sql if args && args[:return_sql]
|
227
|
-
|
228
|
-
query(sql)
|
229
|
-
|
230
|
-
if args && args[:return_id]
|
231
|
-
first_id = self.last_id
|
232
|
-
raise "Invalid ID: #{first_id}" if first_id.to_i <= 0
|
233
|
-
ids = [first_id]
|
234
|
-
1.upto(arr_hashes.length - 1) do |count|
|
235
|
-
ids << first_id + count
|
236
|
-
end
|
237
|
-
|
238
|
-
ids_length = ids.length
|
239
|
-
arr_hashes_length = arr_hashes.length
|
240
|
-
raise "Invalid length (#{ids_length}, #{arr_hashes_length})." if ids_length != arr_hashes_length
|
241
|
-
|
242
|
-
return ids
|
243
|
-
else
|
244
|
-
return nil
|
245
|
-
end
|
246
|
-
end
|
247
|
-
|
248
|
-
def transaction
|
249
|
-
@baza.q("START TRANSACTION")
|
250
|
-
|
251
|
-
begin
|
252
|
-
yield @baza
|
253
|
-
@baza.q("COMMIT")
|
254
|
-
rescue
|
255
|
-
@baza.q("ROLLBACK")
|
256
|
-
raise
|
257
|
-
end
|
258
|
-
end
|
259
174
|
end
|