Module: ArJdbc::MySQL
Defined Under Namespace
Modules: ColumnExtensions
Class Method Summary
collapse
Instance Method Summary
collapse
-
#adapter_name ⇒ Object
-
#add_column(table_name, column_name, type, options = {}) ⇒ Object
-
#add_column_position!(sql, options) ⇒ Object
-
#add_limit_offset!(sql, options) ⇒ Object
-
#case_sensitive_equality_operator ⇒ Object
-
#case_sensitive_modifier(node) ⇒ Object
-
#change_column(table_name, column_name, type, options = {}) ⇒ Object
-
#change_column_default(table_name, column_name, default) ⇒ Object
-
#change_column_null(table_name, column_name, null, default = nil) ⇒ Object
-
#charset ⇒ Object
-
#collation ⇒ Object
-
#configure_connection ⇒ Object
-
#create_database(name, options = {}) ⇒ Object
-
#create_savepoint ⇒ Object
-
#create_table(name, options = {}) ⇒ Object
-
#current_database ⇒ Object
-
#disable_referential_integrity(&block) ⇒ Object
-
#drop_database(name) ⇒ Object
-
#indexes(table_name, name = nil) ⇒ Object
-
#jdbc_columns(table_name, name = nil) ⇒ Object
-
#join_to_update(update, select) ⇒ Object
-
#limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key) ⇒ Object
-
#modify_types(tp) ⇒ Object
-
#pk_and_sequence_for(table) ⇒ Object
-
#primary_key(table) ⇒ Object
Returns just a table’s primary key.
-
#quote(value, column = nil) ⇒ Object
QUOTING ==================================================.
-
#quote_column_name(name) ⇒ Object
-
#quoted_false ⇒ Object
-
#quoted_true ⇒ Object
-
#recreate_database(name, options = {}) ⇒ Object
-
#release_savepoint ⇒ Object
-
#rename_column(table_name, column_name, new_column_name) ⇒ Object
-
#rename_table(name, new_name) ⇒ Object
-
#rollback_to_savepoint ⇒ Object
-
#show_variable(var) ⇒ Object
-
#structure_dump ⇒ Object
SCHEMA STATEMENTS ========================================.
-
#supports_savepoints? ⇒ Boolean
-
#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
Class Method Details
.arel2_visitors(config) ⇒ Object
102
103
104
|
# File 'lib/arjdbc/mysql/adapter.rb', line 102
def self.arel2_visitors(config)
{}.tap {|v| %w(mysql mysql2 jdbcmysql).each {|a| v[a] = ::Arel::Visitors::MySQL } }
end
|
.column_selector ⇒ Object
5
6
7
|
# File 'lib/arjdbc/mysql/adapter.rb', line 5
def self.column_selector
[/mysql/i, lambda {|cfg,col| col.extend(::ArJdbc::MySQL::ColumnExtensions)}]
end
|
.extended(adapter) ⇒ Object
9
10
11
|
# File 'lib/arjdbc/mysql/adapter.rb', line 9
def self.extended(adapter)
adapter.configure_connection
end
|
.jdbc_connection_class ⇒ Object
Instance Method Details
#adapter_name ⇒ Object
98
99
100
|
# File 'lib/arjdbc/mysql/adapter.rb', line 98
def adapter_name 'MySQL'
end
|
#add_column(table_name, column_name, type, options = {}) ⇒ Object
272
273
274
275
276
277
|
# File 'lib/arjdbc/mysql/adapter.rb', line 272
def add_column(table_name, column_name, type, options = {})
add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
add_column_options!(add_column_sql, options)
add_column_position!(add_column_sql, options)
execute(add_column_sql)
end
|
#add_column_position!(sql, options) ⇒ Object
387
388
389
390
391
392
393
|
# File 'lib/arjdbc/mysql/adapter.rb', line 387
def add_column_position!(sql, options)
if options[:first]
sql << " FIRST"
elsif options[:after]
sql << " AFTER #{quote_column_name(options[:after])}"
end
end
|
#add_limit_offset!(sql, options) ⇒ Object
325
326
327
328
329
330
331
332
333
334
335
|
# File 'lib/arjdbc/mysql/adapter.rb', line 325
def add_limit_offset!(sql, options) limit, offset = options[:limit], options[:offset]
if limit && offset
sql << " LIMIT #{offset.to_i}, #{sanitize_limit(limit)}"
elsif limit
sql << " LIMIT #{sanitize_limit(limit)}"
elsif offset
sql << " OFFSET #{offset.to_i}"
end
sql
end
|
#case_sensitive_equality_operator ⇒ Object
106
107
108
|
# File 'lib/arjdbc/mysql/adapter.rb', line 106
def case_sensitive_equality_operator
"= BINARY"
end
|
#case_sensitive_modifier(node) ⇒ Object
110
111
112
|
# File 'lib/arjdbc/mysql/adapter.rb', line 110
def case_sensitive_modifier(node)
Arel::Nodes::Bin.new(node)
end
|
#change_column(table_name, column_name, type, options = {}) ⇒ Object
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
# File 'lib/arjdbc/mysql/adapter.rb', line 294
def change_column(table_name, column_name, type, options = {}) column = column_for(table_name, column_name)
unless options_include_default?(options)
options[:default] = column.default
end
unless options.has_key?(:null)
options[:null] = column.null
end
change_column_sql = "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
add_column_options!(change_column_sql, options)
add_column_position!(change_column_sql, options)
execute(change_column_sql)
end
|
#change_column_default(table_name, column_name, default) ⇒ Object
279
280
281
282
|
# File 'lib/arjdbc/mysql/adapter.rb', line 279
def change_column_default(table_name, column_name, default) column = column_for(table_name, column_name)
change_column table_name, column_name, column.sql_type, :default => default
end
|
#change_column_null(table_name, column_name, null, default = nil) ⇒ Object
284
285
286
287
288
289
290
291
292
|
# File 'lib/arjdbc/mysql/adapter.rb', line 284
def change_column_null(table_name, column_name, null, default = nil)
column = column_for(table_name, column_name)
unless null || default.nil?
execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
end
change_column table_name, column_name, column.sql_type, :null => null
end
|
#charset ⇒ Object
366
367
368
|
# File 'lib/arjdbc/mysql/adapter.rb', line 366
def charset
show_variable("character_set_database")
end
|
#collation ⇒ Object
370
371
372
|
# File 'lib/arjdbc/mysql/adapter.rb', line 370
def collation
show_variable("collation_database")
end
|
13
14
15
|
# File 'lib/arjdbc/mysql/adapter.rb', line 13
def configure_connection
execute("SET SQL_AUTO_IS_NULL=0")
end
|
#create_database(name, options = {}) ⇒ Object
248
249
250
251
252
253
254
|
# File 'lib/arjdbc/mysql/adapter.rb', line 248
def create_database(name, options = {}) if options[:collation]
execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
else
execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
end
end
|
#create_savepoint ⇒ Object
151
152
153
|
# File 'lib/arjdbc/mysql/adapter.rb', line 151
def create_savepoint
execute("SAVEPOINT #{current_savepoint_name}")
end
|
#create_table(name, options = {}) ⇒ Object
264
265
266
|
# File 'lib/arjdbc/mysql/adapter.rb', line 264
def create_table(name, options = {}) super(name, {:options => "ENGINE=InnoDB DEFAULT CHARSET=utf8"}.merge(options))
end
|
#current_database ⇒ Object
260
261
262
|
# File 'lib/arjdbc/mysql/adapter.rb', line 260
def current_database
select_one("SELECT DATABASE() as db")["db"]
end
|
#disable_referential_integrity(&block) ⇒ Object
163
164
165
166
167
168
169
170
171
|
# File 'lib/arjdbc/mysql/adapter.rb', line 163
def disable_referential_integrity(&block) old = select_value("SELECT @@FOREIGN_KEY_CHECKS")
begin
update("SET FOREIGN_KEY_CHECKS = 0")
yield
ensure
update("SET FOREIGN_KEY_CHECKS = #{old}")
end
end
|
#drop_database(name) ⇒ Object
256
257
258
|
# File 'lib/arjdbc/mysql/adapter.rb', line 256
def drop_database(name) execute "DROP DATABASE IF EXISTS `#{name}`"
end
|
#indexes(table_name, name = nil) ⇒ Object
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
# File 'lib/arjdbc/mysql/adapter.rb', line 211
def indexes(table_name, name = nil) indexes = []
current_index = nil
result = execute("SHOW KEYS FROM #{quote_table_name(table_name)}", name)
result.each do |row|
key_name = row["Key_name"]
if current_index != key_name
next if key_name == "PRIMARY" current_index = key_name
indexes << ::ActiveRecord::ConnectionAdapters::IndexDefinition.new(
row["Table"], key_name, row["Non_unique"] == 0, [], [])
end
indexes.last.columns << row["Column_name"]
indexes.last.lengths << row["Sub_part"]
end
indexes
end
|
#jdbc_columns(table_name, name = nil) ⇒ Object
230
231
232
233
234
235
|
# File 'lib/arjdbc/mysql/adapter.rb', line 230
def jdbc_columns(table_name, name = nil) sql = "SHOW FIELDS FROM #{quote_table_name(table_name)}"
execute(sql, 'SCHEMA').map do |field|
::ActiveRecord::ConnectionAdapters::MysqlColumn.new(field["Field"], field["Default"], field["Type"], field["Null"] == "YES")
end
end
|
#join_to_update(update, select) ⇒ Object
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
# File 'lib/arjdbc/mysql/adapter.rb', line 344
def join_to_update(update, select) if select.limit || select.offset || select.orders.any?
subsubselect = select.clone
subsubselect.projections = [update.key]
subselect = Arel::SelectManager.new(select.engine)
subselect.project Arel.sql(update.key.name)
subselect.from subsubselect.as('__active_record_temp')
update.where update.key.in(subselect)
else
update.table select.source
update.wheres = select.constraints
end
end
|
#limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key) ⇒ Object
114
115
116
|
# File 'lib/arjdbc/mysql/adapter.rb', line 114
def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
where_sql
end
|
#modify_types(tp) ⇒ Object
89
90
91
92
93
94
95
96
|
# File 'lib/arjdbc/mysql/adapter.rb', line 89
def modify_types(tp)
tp[:primary_key] = "int(11) DEFAULT NULL auto_increment PRIMARY KEY"
tp[:integer] = { :name => 'int', :limit => 4 }
tp[:decimal] = { :name => "decimal" }
tp[:timestamp] = { :name => "datetime" }
tp[:datetime][:limit] = nil
tp
end
|
#pk_and_sequence_for(table) ⇒ Object
199
200
201
202
203
204
205
206
|
# File 'lib/arjdbc/mysql/adapter.rb', line 199
def pk_and_sequence_for(table) keys = []
result = execute("SHOW INDEX FROM #{quote_table_name(table)} WHERE Key_name = 'PRIMARY'", 'SCHEMA')
result.each do |h|
keys << h["Column_name"]
end
keys.length == 1 ? [keys.first, nil] : nil
end
|
#primary_key(table) ⇒ Object
Returns just a table’s primary key
238
239
240
241
|
# File 'lib/arjdbc/mysql/adapter.rb', line 238
def primary_key(table)
pk_and_sequence = pk_and_sequence_for(table)
pk_and_sequence && pk_and_sequence.first
end
|
#quote(value, column = nil) ⇒ Object
QUOTING ==================================================
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/arjdbc/mysql/adapter.rb', line 120
def quote(value, column = nil)
return value.quoted_id if value.respond_to?(:quoted_id)
if column && column.type == :primary_key
value.to_s
elsif column && String === value && column.type == :binary && column.class.respond_to?(:string_to_binary)
s = column.class.string_to_binary(value).unpack("H*")[0]
"x'#{s}'"
elsif BigDecimal === value
"'#{value.to_s("F")}'"
else
super
end
end
|
#quote_column_name(name) ⇒ Object
135
136
137
|
# File 'lib/arjdbc/mysql/adapter.rb', line 135
def quote_column_name(name)
"`#{name.to_s.gsub('`', '``')}`"
end
|
#quoted_false ⇒ Object
143
144
145
|
# File 'lib/arjdbc/mysql/adapter.rb', line 143
def quoted_false
"0"
end
|
#quoted_true ⇒ Object
139
140
141
|
# File 'lib/arjdbc/mysql/adapter.rb', line 139
def quoted_true
"1"
end
|
#recreate_database(name, options = {}) ⇒ Object
243
244
245
246
|
# File 'lib/arjdbc/mysql/adapter.rb', line 243
def recreate_database(name, options = {}) drop_database(name)
create_database(name, options)
end
|
#release_savepoint ⇒ Object
159
160
161
|
# File 'lib/arjdbc/mysql/adapter.rb', line 159
def release_savepoint
execute("RELEASE SAVEPOINT #{current_savepoint_name}")
end
|
#rename_column(table_name, column_name, new_column_name) ⇒ Object
311
312
313
314
315
316
317
318
319
320
321
322
323
|
# File 'lib/arjdbc/mysql/adapter.rb', line 311
def rename_column(table_name, column_name, new_column_name) options = {}
if column = columns(table_name).find { |c| c.name == column_name.to_s }
options[:default] = column.default
options[:null] = column.null
else
raise ActiveRecord::ActiveRecordError, "No such column: #{table_name}.#{column_name}"
end
current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"]
rename_column_sql = "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}"
add_column_options!(rename_column_sql, options)
execute(rename_column_sql)
end
|
#rename_table(name, new_name) ⇒ Object
268
269
270
|
# File 'lib/arjdbc/mysql/adapter.rb', line 268
def rename_table(name, new_name)
execute "RENAME TABLE #{quote_table_name(name)} TO #{quote_table_name(new_name)}"
end
|
#rollback_to_savepoint ⇒ Object
155
156
157
|
# File 'lib/arjdbc/mysql/adapter.rb', line 155
def rollback_to_savepoint
execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
end
|
#show_variable(var) ⇒ Object
360
361
362
363
364
|
# File 'lib/arjdbc/mysql/adapter.rb', line 360
def show_variable(var)
res = execute("show variables like '#{var}'")
result_row = res.detect {|row| row["Variable_name"] == var }
result_row && result_row["Value"]
end
|
#structure_dump ⇒ Object
SCHEMA STATEMENTS ========================================
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/arjdbc/mysql/adapter.rb', line 175
def structure_dump if supports_views?
sql = "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'"
else
sql = "SHOW TABLES"
end
select_all(sql).inject("") do |structure, table|
table.delete('Table_type')
hash = show_create_table(table.to_a.first.last)
if(table = hash["Create Table"])
structure += table + ";\n\n"
elsif(view = hash["Create View"])
structure += view + ";\n\n"
end
end
end
|
#supports_savepoints? ⇒ Boolean
147
148
149
|
# File 'lib/arjdbc/mysql/adapter.rb', line 147
def supports_savepoints? true
end
|
#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
374
375
376
377
378
379
380
381
382
383
384
385
|
# File 'lib/arjdbc/mysql/adapter.rb', line 374
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
return super unless type.to_s == 'integer'
case limit
when 1; 'tinyint'
when 2; 'smallint'
when 3; 'mediumint'
when nil, 4, 11; 'int(11)' when 5..8; 'bigint'
else raise(ActiveRecordError, "No integer type has byte size #{limit}")
end
end
|