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:



93
94
95
# File 'lib/arjdbc/mysql/adapter.rb', line 93

def adapter_name #:nodoc:
  'MySQL'
end

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



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

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



328
329
330
331
332
333
334
# File 'lib/arjdbc/mysql/adapter.rb', line 328

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:



289
290
291
292
293
294
295
296
297
298
299
# File 'lib/arjdbc/mysql/adapter.rb', line 289

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



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

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

#begin_db_transactionObject

:nodoc:



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

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

#case_sensitive_equality_operatorObject



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

def case_sensitive_equality_operator
  "= BINARY"
end

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

:nodoc:



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/arjdbc/mysql/adapter.rb', line 258

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:



243
244
245
246
# File 'lib/arjdbc/mysql/adapter.rb', line 243

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



248
249
250
251
252
253
254
255
256
# File 'lib/arjdbc/mysql/adapter.rb', line 248

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



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

def charset
  show_variable("character_set_database")
end

#collationObject



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

def collation
  show_variable("collation_database")
end

#commit_db_transactionObject

:nodoc:



140
141
142
143
144
# File 'lib/arjdbc/mysql/adapter.rb', line 140

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:



212
213
214
215
216
217
218
# File 'lib/arjdbc/mysql/adapter.rb', line 212

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



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

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

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

:nodoc:



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

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

#current_databaseObject



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

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

#disable_referential_integrity(&block) ⇒ Object

:nodoc:



168
169
170
171
172
173
174
175
176
# File 'lib/arjdbc/mysql/adapter.rb', line 168

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:



220
221
222
# File 'lib/arjdbc/mysql/adapter.rb', line 220

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

#jdbc_columns(table_name, name = nil) ⇒ Object

:nodoc:



200
201
202
203
204
205
# File 'lib/arjdbc/mysql/adapter.rb', line 200

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



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

def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
  where_sql
end

#modify_types(tp) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/arjdbc/mysql/adapter.rb', line 84

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 ==================================================



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/arjdbc/mysql/adapter.rb', line 111

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



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

def quoted_false
    "0"
end

#quoted_trueObject



126
127
128
# File 'lib/arjdbc/mysql/adapter.rb', line 126

def quoted_true
    "1"
end

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

:nodoc:



207
208
209
210
# File 'lib/arjdbc/mysql/adapter.rb', line 207

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

#release_savepointObject



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

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

#rename_column(table_name, column_name, new_column_name) ⇒ Object

:nodoc:



275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/arjdbc/mysql/adapter.rb', line 275

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



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

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

#rollback_db_transactionObject

:nodoc:



146
147
148
149
150
# File 'lib/arjdbc/mysql/adapter.rb', line 146

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

#rollback_to_savepointObject



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

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

#show_variable(var) ⇒ Object



301
302
303
304
305
# File 'lib/arjdbc/mysql/adapter.rb', line 301

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 ========================================



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/arjdbc/mysql/adapter.rb', line 180

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)


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

def supports_savepoints? #:nodoc:
  true
end

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



315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/arjdbc/mysql/adapter.rb', line 315

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