Module: ArJdbc::MySQL

Included in:
ActiveRecord::ConnectionAdapters::MysqlAdapter
Defined in:
lib/arjdbc/mysql/adapter.rb

Defined Under Namespace

Modules: Column

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.column_selectorObject



5
6
7
# File 'lib/arjdbc/mysql/adapter.rb', line 5

def self.column_selector
  [/mysql/i, lambda {|cfg,col| col.extend(::ArJdbc::MySQL::Column)}]
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_classObject



17
18
19
# File 'lib/arjdbc/mysql/adapter.rb', line 17

def self.jdbc_connection_class
  ::ActiveRecord::ConnectionAdapters::MySQLJdbcConnection
end

Instance Method Details

#adapter_nameObject

:nodoc:



97
98
99
# File 'lib/arjdbc/mysql/adapter.rb', line 97

def adapter_name #:nodoc:
  'MySQL'
end

#add_column(table_name, column_name, type, options = {}) ⇒ Object



240
241
242
243
244
245
# File 'lib/arjdbc/mysql/adapter.rb', line 240

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



332
333
334
335
336
337
338
# File 'lib/arjdbc/mysql/adapter.rb', line 332

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

:nodoc:



293
294
295
296
297
298
299
300
301
302
303
# File 'lib/arjdbc/mysql/adapter.rb', line 293

def add_limit_offset!(sql, options) #:nodoc:
  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

#arel2_visitorsObject



101
102
103
# File 'lib/arjdbc/mysql/adapter.rb', line 101

def arel2_visitors
  {'jdbcmysql' => ::Arel::Visitors::MySQL}
end

#begin_db_transactionObject

:nodoc:



138
139
140
141
142
# File 'lib/arjdbc/mysql/adapter.rb', line 138

def begin_db_transaction #:nodoc:
  @connection.begin
rescue Exception
  # Transactions aren't supported
end

#case_sensitive_equality_operatorObject



105
106
107
# File 'lib/arjdbc/mysql/adapter.rb', line 105

def case_sensitive_equality_operator
  "= BINARY"
end

#change_column(table_name, column_name, type, options = {}) ⇒ Object

:nodoc:



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/arjdbc/mysql/adapter.rb', line 262

def change_column(table_name, column_name, type, options = {}) #:nodoc:
  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

:nodoc:



247
248
249
250
# File 'lib/arjdbc/mysql/adapter.rb', line 247

def change_column_default(table_name, column_name, default) #:nodoc:
  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



252
253
254
255
256
257
258
259
260
# File 'lib/arjdbc/mysql/adapter.rb', line 252

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

#charsetObject



311
312
313
# File 'lib/arjdbc/mysql/adapter.rb', line 311

def charset
  show_variable("character_set_database")
end

#collationObject



315
316
317
# File 'lib/arjdbc/mysql/adapter.rb', line 315

def collation
  show_variable("collation_database")
end

#commit_db_transactionObject

:nodoc:



144
145
146
147
148
# File 'lib/arjdbc/mysql/adapter.rb', line 144

def commit_db_transaction #:nodoc:
  @connection.commit
rescue Exception
  # Transactions aren't supported
end

#configure_connectionObject



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

:nodoc:



216
217
218
219
220
221
222
# File 'lib/arjdbc/mysql/adapter.rb', line 216

def create_database(name, options = {}) #:nodoc:
  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_savepointObject



160
161
162
# File 'lib/arjdbc/mysql/adapter.rb', line 160

def create_savepoint
  execute("SAVEPOINT #{current_savepoint_name}")
end

#create_table(name, options = {}) ⇒ Object

:nodoc:



232
233
234
# File 'lib/arjdbc/mysql/adapter.rb', line 232

def create_table(name, options = {}) #:nodoc:
  super(name, {:options => "ENGINE=InnoDB"}.merge(options))
end

#current_databaseObject



228
229
230
# File 'lib/arjdbc/mysql/adapter.rb', line 228

def current_database
  select_one("SELECT DATABASE() as db")["db"]
end

#disable_referential_integrity(&block) ⇒ Object

:nodoc:



172
173
174
175
176
177
178
179
180
# File 'lib/arjdbc/mysql/adapter.rb', line 172

def disable_referential_integrity(&block) #:nodoc:
  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

:nodoc:



224
225
226
# File 'lib/arjdbc/mysql/adapter.rb', line 224

def drop_database(name) #:nodoc:
  execute "DROP DATABASE IF EXISTS `#{name}`"
end

#jdbc_columns(table_name, name = nil) ⇒ Object

:nodoc:



204
205
206
207
208
209
# File 'lib/arjdbc/mysql/adapter.rb', line 204

def jdbc_columns(table_name, name = nil)#:nodoc:
  sql = "SHOW FIELDS FROM #{quote_table_name(table_name)}"
  execute(sql, :skip_logging).map do |field|
    ::ActiveRecord::ConnectionAdapters::MysqlColumn.new(field["Field"], field["Default"], field["Type"], field["Null"] == "YES")
  end
end

#limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key) ⇒ Object



109
110
111
# File 'lib/arjdbc/mysql/adapter.rb', line 109

def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
  where_sql
end

#modify_types(tp) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/arjdbc/mysql/adapter.rb', line 88

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

#quote(value, column = nil) ⇒ Object

QUOTING ==================================================



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/arjdbc/mysql/adapter.rb', line 115

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

#quoted_falseObject



134
135
136
# File 'lib/arjdbc/mysql/adapter.rb', line 134

def quoted_false
    "0"
end

#quoted_trueObject



130
131
132
# File 'lib/arjdbc/mysql/adapter.rb', line 130

def quoted_true
    "1"
end

#recreate_database(name, options = {}) ⇒ Object

:nodoc:



211
212
213
214
# File 'lib/arjdbc/mysql/adapter.rb', line 211

def recreate_database(name, options = {}) #:nodoc:
  drop_database(name)
  create_database(name, options)
end

#release_savepointObject



168
169
170
# File 'lib/arjdbc/mysql/adapter.rb', line 168

def release_savepoint
  execute("RELEASE SAVEPOINT #{current_savepoint_name}")
end

#rename_column(table_name, column_name, new_column_name) ⇒ Object

:nodoc:



279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/arjdbc/mysql/adapter.rb', line 279

def rename_column(table_name, column_name, new_column_name) #:nodoc:
  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



236
237
238
# File 'lib/arjdbc/mysql/adapter.rb', line 236

def rename_table(name, new_name)
  execute "RENAME TABLE #{quote_table_name(name)} TO #{quote_table_name(new_name)}"
end

#rollback_db_transactionObject

:nodoc:



150
151
152
153
154
# File 'lib/arjdbc/mysql/adapter.rb', line 150

def rollback_db_transaction #:nodoc:
  @connection.rollback
rescue Exception
  # Transactions aren't supported
end

#rollback_to_savepointObject



164
165
166
# File 'lib/arjdbc/mysql/adapter.rb', line 164

def rollback_to_savepoint
  execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
end

#show_variable(var) ⇒ Object



305
306
307
308
309
# File 'lib/arjdbc/mysql/adapter.rb', line 305

def show_variable(var)
  res = execute("show variables like '#{var}'")
  row = res.detect {|row| row["Variable_name"] == var }
  row && row["Value"]
end

#structure_dumpObject

SCHEMA STATEMENTS ========================================



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/arjdbc/mysql/adapter.rb', line 184

def structure_dump #:nodoc:
  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

:nodoc:

Returns:

  • (Boolean)


156
157
158
# File 'lib/arjdbc/mysql/adapter.rb', line 156

def supports_savepoints? #:nodoc:
  true
end

#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/arjdbc/mysql/adapter.rb', line 319

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)'  # compatibility with MySQL default
  when 5..8; 'bigint'
  else raise(ActiveRecordError, "No integer type has byte size #{limit}")
  end
end