baza 0.0.35 → 0.0.36
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile +1 -1
- data/Gemfile.lock +14 -17
- data/VERSION +1 -1
- data/baza.gemspec +8 -7
- data/lib/baza/base_sql_driver.rb +38 -8
- data/lib/baza/cloner.rb +10 -0
- data/lib/baza/commands/select.rb +15 -8
- data/lib/baza/db.rb +8 -4
- data/lib/baza/driver/active_record.rb +7 -0
- data/lib/baza/driver/mysql/column.rb +4 -6
- data/lib/baza/driver/mysql/columns.rb +4 -4
- data/lib/baza/driver/mysql/database.rb +4 -4
- data/lib/baza/driver/mysql/databases.rb +3 -3
- data/lib/baza/driver/mysql/foreign_key.rb +2 -2
- data/lib/baza/driver/mysql/index.rb +1 -1
- data/lib/baza/driver/mysql/sql/column.rb +4 -4
- data/lib/baza/driver/mysql/sql/create_indexes.rb +3 -3
- data/lib/baza/driver/mysql/sql/create_table.rb +1 -1
- data/lib/baza/driver/mysql/table.rb +16 -16
- data/lib/baza/driver/mysql_java.rb +3 -3
- data/lib/baza/driver/pg/column.rb +5 -13
- data/lib/baza/driver/pg/columns.rb +4 -4
- data/lib/baza/driver/pg/create_index_sql_creator.rb +3 -3
- data/lib/baza/driver/pg/database.rb +4 -4
- data/lib/baza/driver/pg/databases.rb +1 -1
- data/lib/baza/driver/pg/foreign_key.rb +2 -2
- data/lib/baza/driver/pg/index.rb +1 -1
- data/lib/baza/driver/pg/table.rb +9 -9
- data/lib/baza/driver/pg/tables.rb +1 -1
- data/lib/baza/driver/sqlite3/column.rb +1 -1
- data/lib/baza/driver/sqlite3/columns.rb +1 -1
- data/lib/baza/driver/sqlite3/index.rb +1 -1
- data/lib/baza/driver/sqlite3/table.rb +4 -4
- data/lib/baza/driver/tiny.rb +83 -8
- data/lib/baza/driver/tiny/result.rb +18 -2
- data/lib/baza/idquery.rb +3 -3
- data/lib/baza/mysql_base_driver.rb +3 -3
- data/lib/baza/sql_queries/generic_insert.rb +3 -3
- data/lib/baza/sql_queries/generic_update.rb +2 -2
- data/lib/baza/sql_queries/mysql_upsert.rb +5 -5
- data/lib/baza/sql_queries/mysql_upsert_duplicate_key.rb +3 -3
- data/lib/baza/sql_queries/postgres_upsert_duplicate_key.rb +7 -7
- data/lib/baza/sql_queries/select.rb +8 -8
- data/lib/baza/sql_queries/sqlite_upsert_duplicate_key.rb +6 -6
- data/lib/baza/table.rb +3 -3
- data/spec/baza/cloner_spec.rb +2 -2
- data/spec/drivers/tiny_spec.rb +27 -0
- data/spec/info_active_record_pg_peak_flow.rb +2 -2
- data/spec/info_active_record_sqlite3.rb +6 -13
- data/spec/info_sqlite3.rb +3 -12
- metadata +8 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fae6535d9e34fae8460213071209a7657c8dbe97
|
4
|
+
data.tar.gz: a3289f1628a01ca32000f3d9e96beef9033b00f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a71d39ad1ba7a1b682a0b88800567ced7e94cfe3e489049f00e09b0dd8383645eca9ab04e45f68e8c3abf14e35b596efe5a58d8da647054c65b25b26bbee6f1
|
7
|
+
data.tar.gz: 1e5ec98de5eef606d1f483d67109a8563ca8bb65e9d52cff3c25e2cecd8eca809611552790ad9826b2499eca8ccbbebd831ed6000485b4349b260fb6ba43f9a2
|
data/Gemfile
CHANGED
@@ -14,7 +14,7 @@ gem "wref", "~> 0.0.8"
|
|
14
14
|
# Add dependencies to develop your gem here.
|
15
15
|
# Include everything needed to run rake, tests, features, etc.
|
16
16
|
group :development, :test do
|
17
|
-
gem "activerecord"
|
17
|
+
gem "activerecord"
|
18
18
|
gem "activerecord-jdbc-adapter", platform: :jruby
|
19
19
|
gem "best_practice_project", "0.0.9", require: false
|
20
20
|
gem "bundler"
|
data/Gemfile.lock
CHANGED
@@ -1,23 +1,21 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
activemodel (
|
5
|
-
activesupport (=
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
arel (~> 6.0)
|
4
|
+
activemodel (5.2.0)
|
5
|
+
activesupport (= 5.2.0)
|
6
|
+
activerecord (5.2.0)
|
7
|
+
activemodel (= 5.2.0)
|
8
|
+
activesupport (= 5.2.0)
|
9
|
+
arel (>= 9.0)
|
11
10
|
activerecord-jdbc-adapter (50.0)
|
12
11
|
activerecord (>= 2.2)
|
13
|
-
activesupport (
|
14
|
-
|
15
|
-
|
12
|
+
activesupport (5.2.0)
|
13
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
14
|
+
i18n (>= 0.7, < 2)
|
16
15
|
minitest (~> 5.1)
|
17
|
-
thread_safe (~> 0.3, >= 0.3.4)
|
18
16
|
tzinfo (~> 1.1)
|
19
17
|
addressable (2.4.0)
|
20
|
-
arel (
|
18
|
+
arel (9.0.0)
|
21
19
|
array_enumerator (0.0.10)
|
22
20
|
ast (2.4.0)
|
23
21
|
auto_autoloader (0.0.5)
|
@@ -35,6 +33,7 @@ GEM
|
|
35
33
|
event_handler (0.0.0)
|
36
34
|
faraday (0.9.2)
|
37
35
|
multipart-post (>= 1.2, < 3)
|
36
|
+
ffi (1.9.23)
|
38
37
|
ffi (1.9.23-java)
|
39
38
|
git (1.3.0)
|
40
39
|
github_api (0.16.0)
|
@@ -46,7 +45,7 @@ GEM
|
|
46
45
|
oauth2 (~> 1.0)
|
47
46
|
hashie (3.5.7)
|
48
47
|
highline (1.7.10)
|
49
|
-
i18n (0.
|
48
|
+
i18n (1.0.1)
|
50
49
|
concurrent-ruby (~> 1.0)
|
51
50
|
jar-dependencies (0.3.12)
|
52
51
|
jdbc-mysql (5.1.44)
|
@@ -62,8 +61,6 @@ GEM
|
|
62
61
|
rake
|
63
62
|
rdoc
|
64
63
|
semver2
|
65
|
-
json (1.8.6)
|
66
|
-
json (1.8.6-java)
|
67
64
|
jwt (1.5.6)
|
68
65
|
method_source (0.9.0)
|
69
66
|
mime-types (2.99.3)
|
@@ -139,7 +136,7 @@ PLATFORMS
|
|
139
136
|
ruby
|
140
137
|
|
141
138
|
DEPENDENCIES
|
142
|
-
activerecord
|
139
|
+
activerecord
|
143
140
|
activerecord-jdbc-adapter
|
144
141
|
array_enumerator (~> 0.0.10)
|
145
142
|
auto_autoloader (~> 0.0.5)
|
@@ -162,4 +159,4 @@ DEPENDENCIES
|
|
162
159
|
wref (~> 0.0.8)
|
163
160
|
|
164
161
|
BUNDLED WITH
|
165
|
-
1.16.
|
162
|
+
1.16.2
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.36
|
data/baza.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: baza 0.0.
|
5
|
+
# stub: baza 0.0.36 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "baza".freeze
|
9
|
-
s.version = "0.0.
|
9
|
+
s.version = "0.0.36"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib".freeze]
|
13
13
|
s.authors = ["Kasper Johansen".freeze]
|
14
|
-
s.date = "2018-06-
|
14
|
+
s.date = "2018-06-27"
|
15
15
|
s.description = "A database abstraction layer, model framework and database framework.".freeze
|
16
16
|
s.email = "kj@gfish.com".freeze
|
17
17
|
s.extra_rdoc_files = [
|
@@ -177,6 +177,7 @@ Gem::Specification.new do |s|
|
|
177
177
|
"spec/drivers/pg/columns_spec.rb",
|
178
178
|
"spec/drivers/pg_spec.rb",
|
179
179
|
"spec/drivers/sqlite3_spec.rb",
|
180
|
+
"spec/drivers/tiny_spec.rb",
|
180
181
|
"spec/info_active_record_example.rb",
|
181
182
|
"spec/info_active_record_mysql2_example.rb",
|
182
183
|
"spec/info_active_record_mysql2_peak_flow.rb",
|
@@ -205,7 +206,7 @@ Gem::Specification.new do |s|
|
|
205
206
|
]
|
206
207
|
s.homepage = "http://github.com/kaspernj/baza".freeze
|
207
208
|
s.licenses = ["MIT".freeze]
|
208
|
-
s.rubygems_version = "2.
|
209
|
+
s.rubygems_version = "2.6.14".freeze
|
209
210
|
s.summary = "A database abstraction layer, model framework and database framework.".freeze
|
210
211
|
|
211
212
|
if s.respond_to? :specification_version then
|
@@ -219,7 +220,7 @@ Gem::Specification.new do |s|
|
|
219
220
|
s.add_runtime_dependency(%q<simple_delegate>.freeze, ["~> 0.0.2"])
|
220
221
|
s.add_runtime_dependency(%q<string-cases>.freeze, ["~> 0.0.4"])
|
221
222
|
s.add_runtime_dependency(%q<wref>.freeze, ["~> 0.0.8"])
|
222
|
-
s.add_development_dependency(%q<activerecord>.freeze, ["
|
223
|
+
s.add_development_dependency(%q<activerecord>.freeze, [">= 0"])
|
223
224
|
s.add_development_dependency(%q<activerecord-jdbc-adapter>.freeze, [">= 0"])
|
224
225
|
s.add_development_dependency(%q<best_practice_project>.freeze, ["= 0.0.9"])
|
225
226
|
s.add_development_dependency(%q<bundler>.freeze, [">= 0"])
|
@@ -241,7 +242,7 @@ Gem::Specification.new do |s|
|
|
241
242
|
s.add_dependency(%q<simple_delegate>.freeze, ["~> 0.0.2"])
|
242
243
|
s.add_dependency(%q<string-cases>.freeze, ["~> 0.0.4"])
|
243
244
|
s.add_dependency(%q<wref>.freeze, ["~> 0.0.8"])
|
244
|
-
s.add_dependency(%q<activerecord>.freeze, ["
|
245
|
+
s.add_dependency(%q<activerecord>.freeze, [">= 0"])
|
245
246
|
s.add_dependency(%q<activerecord-jdbc-adapter>.freeze, [">= 0"])
|
246
247
|
s.add_dependency(%q<best_practice_project>.freeze, ["= 0.0.9"])
|
247
248
|
s.add_dependency(%q<bundler>.freeze, [">= 0"])
|
@@ -264,7 +265,7 @@ Gem::Specification.new do |s|
|
|
264
265
|
s.add_dependency(%q<simple_delegate>.freeze, ["~> 0.0.2"])
|
265
266
|
s.add_dependency(%q<string-cases>.freeze, ["~> 0.0.4"])
|
266
267
|
s.add_dependency(%q<wref>.freeze, ["~> 0.0.8"])
|
267
|
-
s.add_dependency(%q<activerecord>.freeze, ["
|
268
|
+
s.add_dependency(%q<activerecord>.freeze, [">= 0"])
|
268
269
|
s.add_dependency(%q<activerecord-jdbc-adapter>.freeze, [">= 0"])
|
269
270
|
s.add_dependency(%q<best_practice_project>.freeze, ["= 0.0.9"])
|
270
271
|
s.add_dependency(%q<bundler>.freeze, [">= 0"])
|
data/lib/baza/base_sql_driver.rb
CHANGED
@@ -54,6 +54,14 @@ class Baza::BaseSqlDriver
|
|
54
54
|
self.class.escape_column(string)
|
55
55
|
end
|
56
56
|
|
57
|
+
def self.quote_column(column_name)
|
58
|
+
"#{SEPARATOR_COLUMN}#{escape_column(column_name)}#{SEPARATOR_COLUMN}"
|
59
|
+
end
|
60
|
+
|
61
|
+
def quote_column(column_name)
|
62
|
+
"#{sep_col}#{escape_column(column_name)}#{sep_col}"
|
63
|
+
end
|
64
|
+
|
57
65
|
def self.escape_table(string)
|
58
66
|
string = string.to_s
|
59
67
|
raise "Invalid table-string: #{string}" if string.include?(SEPARATOR_TABLE)
|
@@ -64,6 +72,14 @@ class Baza::BaseSqlDriver
|
|
64
72
|
self.class.escape_table(string)
|
65
73
|
end
|
66
74
|
|
75
|
+
def self.quote_table(table_name)
|
76
|
+
"#{SEPARATOR_TABLE}#{escape_table(table_name)}#{SEPARATOR_TABLE}"
|
77
|
+
end
|
78
|
+
|
79
|
+
def quote_table(table_name)
|
80
|
+
"#{sep_table}#{escape_table(table_name)}#{sep_table}"
|
81
|
+
end
|
82
|
+
|
67
83
|
def self.escape_database(string)
|
68
84
|
string = string.to_s
|
69
85
|
raise "Invalid database-string: #{string}" if string.include?(SEPARATOR_DATABASE)
|
@@ -74,6 +90,14 @@ class Baza::BaseSqlDriver
|
|
74
90
|
self.class.escape_database(string)
|
75
91
|
end
|
76
92
|
|
93
|
+
def self.quote_database(database_name)
|
94
|
+
"#{SEPARATOR_DATABASE}#{escape_database(database_name)}#{SEPARATOR_DATABASE}"
|
95
|
+
end
|
96
|
+
|
97
|
+
def quote_database(database_name)
|
98
|
+
"#{sep_database}#{escape_database(database_name)}#{sep_database}"
|
99
|
+
end
|
100
|
+
|
77
101
|
def self.escape_index(string)
|
78
102
|
string = string.to_s
|
79
103
|
raise "Invalid index-string: #{string}" if string.include?(SEPARATOR_INDEX)
|
@@ -84,6 +108,14 @@ class Baza::BaseSqlDriver
|
|
84
108
|
self.class.escape_index(string)
|
85
109
|
end
|
86
110
|
|
111
|
+
def self.quote_index(index_name)
|
112
|
+
"#{SEPARATOR_INDEX}#{escape_index(index_name)}#{SEPARATOR_INDEX}"
|
113
|
+
end
|
114
|
+
|
115
|
+
def quote_index(index_name)
|
116
|
+
"#{sep_index}#{escape_index(index_name)}#{sep_index}"
|
117
|
+
end
|
118
|
+
|
87
119
|
def transaction
|
88
120
|
@db.q("BEGIN TRANSACTION")
|
89
121
|
|
@@ -146,7 +178,7 @@ class Baza::BaseSqlDriver
|
|
146
178
|
end
|
147
179
|
|
148
180
|
def count(tablename, arr_terms = nil)
|
149
|
-
sql = "SELECT COUNT(*) AS count FROM #{
|
181
|
+
sql = "SELECT COUNT(*) AS count FROM #{quote_table(tablename)}"
|
150
182
|
|
151
183
|
if !arr_terms.nil? && !arr_terms.empty?
|
152
184
|
sql << " WHERE #{sql_make_where(arr_terms)}"
|
@@ -169,7 +201,7 @@ class Baza::BaseSqlDriver
|
|
169
201
|
#===Examples
|
170
202
|
# db.delete(:users, {lastname: "Doe"})
|
171
203
|
def delete(tablename, arr_terms, args = nil)
|
172
|
-
sql = "DELETE FROM #{
|
204
|
+
sql = "DELETE FROM #{quote_table(tablename)}"
|
173
205
|
|
174
206
|
if !arr_terms.nil? && !arr_terms.empty?
|
175
207
|
sql << " WHERE #{sql_make_where(arr_terms)}"
|
@@ -199,11 +231,11 @@ class Baza::BaseSqlDriver
|
|
199
231
|
if value.is_a?(Array)
|
200
232
|
raise "Array for column '#{key}' was empty." if value.empty?
|
201
233
|
values = value.map { |v| "'#{escape(v)}'" }.join(",")
|
202
|
-
sql << "#{
|
234
|
+
sql << "#{quote_column(key)} IN (#{values})"
|
203
235
|
elsif value.is_a?(Hash)
|
204
236
|
raise "Dont know how to handle hash."
|
205
237
|
else
|
206
|
-
sql << "#{
|
238
|
+
sql << "#{quote_column(key)} = #{quote_value(value)}"
|
207
239
|
end
|
208
240
|
end
|
209
241
|
|
@@ -213,7 +245,7 @@ class Baza::BaseSqlDriver
|
|
213
245
|
# Returns the correct SQL-value for the given value.
|
214
246
|
# If it is a number, then just the raw number as a string will be returned.
|
215
247
|
# nil's will be NULL and strings will have quotes and will be escaped.
|
216
|
-
def self.
|
248
|
+
def self.quote_value(val)
|
217
249
|
if val.class.name == "Fixnum" || val.is_a?(Integer)
|
218
250
|
val.to_s
|
219
251
|
elsif val == nil
|
@@ -227,9 +259,7 @@ class Baza::BaseSqlDriver
|
|
227
259
|
end
|
228
260
|
end
|
229
261
|
|
230
|
-
def
|
231
|
-
return @conn.sqlval(val) if @conn.respond_to?(:sqlval)
|
232
|
-
|
262
|
+
def quote_value(val)
|
233
263
|
if val.class.name == "Fixnum" || val.is_a?(Integer)
|
234
264
|
val.to_s
|
235
265
|
elsif val == nil
|
data/lib/baza/cloner.rb
CHANGED
@@ -24,6 +24,16 @@ class Baza::Cloner
|
|
24
24
|
conn: connection
|
25
25
|
}
|
26
26
|
|
27
|
+
Baza::Db.new(db_args)
|
28
|
+
elsif connection.class.name.include?("PostgreSQLAdapter")
|
29
|
+
connection = connection.instance_variable_get(:@connection)
|
30
|
+
connection = connection.instance_variable_get(:@connection) if RUBY_PLATFORM == "java"
|
31
|
+
|
32
|
+
db_args = {
|
33
|
+
type: :pg,
|
34
|
+
conn: connection
|
35
|
+
}
|
36
|
+
|
27
37
|
Baza::Db.new(db_args)
|
28
38
|
else
|
29
39
|
raise "Unsupported adapter: #{connection.class.name}"
|
data/lib/baza/commands/select.rb
CHANGED
@@ -18,7 +18,7 @@ class Baza::Commands::Select
|
|
18
18
|
add_select_sql
|
19
19
|
add_terms_sql
|
20
20
|
add_order_sql
|
21
|
-
add_limit_sql
|
21
|
+
add_limit_sql unless @db.mssql?
|
22
22
|
|
23
23
|
result = execute_query
|
24
24
|
|
@@ -36,21 +36,23 @@ private
|
|
36
36
|
# Set up IDQuery-stuff if that is given in arguments.
|
37
37
|
if @args && @args[:idquery]
|
38
38
|
if @args.fetch(:idquery) == true
|
39
|
-
select_sql = "#{@db.
|
39
|
+
select_sql = "#{@db.quote_column(:id)}"
|
40
40
|
@col = :id
|
41
41
|
else
|
42
|
-
select_sql = "#{@db.
|
42
|
+
select_sql = "#{@db.quote_column(@args.fetch(:idquery))}"
|
43
43
|
@col = @args.fetch(:idquery)
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
47
|
select_sql ||= "*"
|
48
|
-
@sql << "SELECT
|
48
|
+
@sql << "SELECT"
|
49
|
+
@sql << top_sql if @db.mssql?
|
50
|
+
@sql << " #{select_sql} FROM"
|
49
51
|
|
50
52
|
if @table_name.is_a?(Array)
|
51
|
-
@sql << " #{@
|
53
|
+
@sql << " #{@db.quote_table(@table_name.first)}.#{@db.quote_table(@table_name.last)}"
|
52
54
|
else
|
53
|
-
@sql << " #{@
|
55
|
+
@sql << " #{@db.quote_table(@table_name)}"
|
54
56
|
end
|
55
57
|
end
|
56
58
|
|
@@ -69,10 +71,10 @@ private
|
|
69
71
|
@args.fetch(:orderby).each do |order_by|
|
70
72
|
@sql << "," unless first
|
71
73
|
first = false if first
|
72
|
-
@sql << " #{@db.
|
74
|
+
@sql << " #{@db.quote_column(order_by)}"
|
73
75
|
end
|
74
76
|
else
|
75
|
-
@sql << " #{@db.
|
77
|
+
@sql << " #{@db.quote_column(@args.fetch(:orderby))}"
|
76
78
|
end
|
77
79
|
end
|
78
80
|
end
|
@@ -99,6 +101,11 @@ private
|
|
99
101
|
end
|
100
102
|
end
|
101
103
|
|
104
|
+
def top_sql
|
105
|
+
return unless @args[:limit]
|
106
|
+
" TOP #{@args[:limit].to_i}"
|
107
|
+
end
|
108
|
+
|
102
109
|
def execute_query
|
103
110
|
# Do IDQuery if given in arguments.
|
104
111
|
if @args && @args[:idquery]
|
data/lib/baza/db.rb
CHANGED
@@ -16,7 +16,7 @@ class Baza::Db
|
|
16
16
|
|
17
17
|
delegate :last_id, :upsert, :upsert_duplicate_key, to: :commands
|
18
18
|
delegate :current_database, :current_database_name, :with_database, to: :databases
|
19
|
-
delegate
|
19
|
+
delegate *%i[close count delete esc escape escape_column escape_table escape_database escape_index quote_database quote_column quote_table quote_value quote_database quote_index insert select single quote_value sql_make_where], to: :driver
|
20
20
|
|
21
21
|
attr_reader :sep_database, :sep_col, :sep_table, :sep_val, :sep_index, :opts, :driver, :int_types
|
22
22
|
|
@@ -444,14 +444,18 @@ class Baza::Db
|
|
444
444
|
end
|
445
445
|
|
446
446
|
def sqlite?
|
447
|
-
@
|
447
|
+
@driver.class.name.downcase.include?("sqlite")
|
448
448
|
end
|
449
449
|
|
450
450
|
def mysql?
|
451
|
-
@
|
451
|
+
@driver.class.name.downcase.include?("mysql")
|
452
|
+
end
|
453
|
+
|
454
|
+
def mssql?
|
455
|
+
@driver.class.name.downcase.include?("tiny")
|
452
456
|
end
|
453
457
|
|
454
458
|
def postgres?
|
455
|
-
@
|
459
|
+
@driver.class.name.downcase.include?("pg")
|
456
460
|
end
|
457
461
|
end
|
@@ -80,10 +80,17 @@ class Baza::Driver::ActiveRecord < Baza::BaseSqlDriver
|
|
80
80
|
if conn_name.include?("mysql")
|
81
81
|
@db.opts[:db] ||= query("SELECT DATABASE()").fetch.fetch(:"DATABASE()")
|
82
82
|
elsif @driver_type == :pg
|
83
|
+
@conn.reconnect! unless postgres_connection_active?
|
83
84
|
@db.opts[:db] ||= query("SELECT current_database()").fetch.values.first
|
84
85
|
end
|
85
86
|
end
|
86
87
|
|
88
|
+
def postgres_connection_active?
|
89
|
+
@conn.active?
|
90
|
+
rescue PG::ConnectionBad
|
91
|
+
true
|
92
|
+
end
|
93
|
+
|
87
94
|
def query(sql)
|
88
95
|
@result_constant.new(self, @conn.execute(sql))
|
89
96
|
end
|
@@ -23,7 +23,7 @@ class Baza::Driver::Mysql::Column < Baza::Column
|
|
23
23
|
other_table = other_column.table
|
24
24
|
|
25
25
|
sql = "
|
26
|
-
ALTER TABLE #{@db.
|
26
|
+
ALTER TABLE #{@db.quote_table(table_name)}
|
27
27
|
ADD CONSTRAINT #{@db.escape_table(fk_name)}
|
28
28
|
FOREIGN KEY (#{@db.escape_table(name)})
|
29
29
|
REFERENCES #{@db.escape_table(other_table.name)} (#{@db.escape_column(other_column.name)})
|
@@ -39,7 +39,7 @@ class Baza::Driver::Mysql::Column < Baza::Column
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def reload
|
42
|
-
data = @db.query("SHOW FULL COLUMNS FROM
|
42
|
+
data = @db.query("SHOW FULL COLUMNS FROM #{@db.quote_table(table_name)} WHERE #{@db.quote_column("Field")} = #{@db.quote_value(name)}").fetch
|
43
43
|
raise Baza::Errors::ColumnNotFound unless data
|
44
44
|
@data = data
|
45
45
|
@type = nil
|
@@ -105,15 +105,13 @@ class Baza::Driver::Mysql::Column < Baza::Column
|
|
105
105
|
|
106
106
|
# Drops the column from the table.
|
107
107
|
def drop
|
108
|
-
@db.query("ALTER TABLE
|
108
|
+
@db.query("ALTER TABLE #{@db.quote_table(table_name)} DROP COLUMN #{@db.quote_column(name)}")
|
109
109
|
table.__send__(:remove_column_from_list, self)
|
110
110
|
nil
|
111
111
|
end
|
112
112
|
|
113
113
|
# Changes the column properties by the given hash.
|
114
114
|
def change(data)
|
115
|
-
col_escaped = "#{@db.sep_col}#{@db.escape_column(name)}#{@db.sep_col}"
|
116
|
-
table_escape = "#{@db.sep_table}#{@db.escape_table(table_name)}#{@db.sep_table}"
|
117
115
|
newdata = data.clone
|
118
116
|
|
119
117
|
newdata[:name] = name unless newdata.key?(:name)
|
@@ -126,7 +124,7 @@ class Baza::Driver::Mysql::Column < Baza::Column
|
|
126
124
|
drop_add = true if name.to_s != newdata[:name].to_s
|
127
125
|
|
128
126
|
table.__send__(:remove_column_from_list, self) if drop_add
|
129
|
-
@db.query("ALTER TABLE #{
|
127
|
+
@db.query("ALTER TABLE #{@db.quote_table(table_name)} CHANGE #{@db.quote_column(name)} #{@db.columns.data_sql(newdata)}")
|
130
128
|
@name = newdata[:name].to_s
|
131
129
|
reload
|
132
130
|
table.__send__(:add_column_to_list, self) if drop_add
|
@@ -14,7 +14,7 @@ class Baza::Driver::Mysql::Columns
|
|
14
14
|
|
15
15
|
data[:maxlength] = 255 if type == :varchar && data[:maxlength].to_s.strip.empty?
|
16
16
|
|
17
|
-
sql = "#{@db.
|
17
|
+
sql = "#{@db.quote_column(data.fetch(:name))} #{type}"
|
18
18
|
sql << "(#{data[:maxlength]})" if data[:maxlength]
|
19
19
|
sql << " PRIMARY KEY" if data[:primarykey]
|
20
20
|
sql << " AUTO_INCREMENT" if data[:autoincr]
|
@@ -23,11 +23,11 @@ class Baza::Driver::Mysql::Columns
|
|
23
23
|
if data.key?(:default_func)
|
24
24
|
sql << " DEFAULT #{data[:default_func]}"
|
25
25
|
elsif data.key?(:default) && !data[:default].nil?
|
26
|
-
sql << " DEFAULT #{@db.
|
26
|
+
sql << " DEFAULT #{@db.quote_value(data.fetch(:default))}"
|
27
27
|
end
|
28
28
|
|
29
|
-
sql << " COMMENT
|
30
|
-
sql << " AFTER #{@db.
|
29
|
+
sql << " COMMENT #{@db.quote_value(data.fetch(:comment))}" if data.key?(:comment)
|
30
|
+
sql << " AFTER #{@db.quote_column(data.fetch(:after))}" if data[:after] && !data[:first]
|
31
31
|
sql << " FIRST" if data[:first]
|
32
32
|
sql << " STORAGE #{data[:storage].to_s.upcase}" if data[:storage]
|
33
33
|
|