Class: ODBCAdapter::Adapters::MySQLODBCAdapter

Inherits:
ActiveRecord::ConnectionAdapters::ODBCAdapter show all
Defined in:
lib/odbc_adapter/adapters/mysql_odbc_adapter.rb

Overview

Overrides specific to MySQL. Mostly taken from ActiveRecord::ConnectionAdapters::MySQLAdapter

Defined Under Namespace

Classes: BindSubstitution

Constant Summary collapse

PRIMARY_KEY =
'INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY'.freeze

Constants inherited from ActiveRecord::ConnectionAdapters::ODBCAdapter

ActiveRecord::ConnectionAdapters::ODBCAdapter::ADAPTER_NAME, ActiveRecord::ConnectionAdapters::ODBCAdapter::BOOLEAN_TYPE, ActiveRecord::ConnectionAdapters::ODBCAdapter::ERR_DUPLICATE_KEY_VALUE

Constants included from DatabaseStatements

DatabaseStatements::SQL_NO_NULLS, DatabaseStatements::SQL_NULLABLE, DatabaseStatements::SQL_NULLABLE_UNKNOWN

Instance Attribute Summary

Attributes inherited from ActiveRecord::ConnectionAdapters::ODBCAdapter

#dbms

Instance Method Summary collapse

Methods inherited from ActiveRecord::ConnectionAdapters::ODBCAdapter

#active?, #adapter_name, #disconnect!, #initialize, #new_column, #reconnect!, #supports_migrations?

Methods included from SchemaStatements

#columns, #current_database, #index_name, #native_database_types, #primary_key, #tables, #views

Methods included from Quoting

#quote_column_name, #quoted_date

Methods included from DatabaseStatements

#begin_db_transaction, #commit_db_transaction, #default_sequence_name, #exec_delete, #exec_query, #exec_rollback_db_transaction, #execute

Methods included from DatabaseLimits

#table_alias_length

Constructor Details

This class inherits a constructor from ActiveRecord::ConnectionAdapters::ODBCAdapter

Instance Method Details

#arel_visitorObject



12
13
14
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 12

def arel_visitor
  BindSubstitution.new(self)
end

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



116
117
118
119
120
121
122
123
124
125
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 116

def change_column(table_name, column_name, type, options = {})
  # column_name.to_s used in case column_name is a symbol
  unless options_include_default?(options)
    options[:default] = columns(table_name).find { |c| c.name == column_name.to_s }.default
  end

  change_column_sql = "ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
  add_column_options!(change_column_sql, options)
  execute(change_column_sql)
end

#change_column_default(table_name, column_name, default) ⇒ Object



127
128
129
130
131
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 127

def change_column_default(table_name, column_name, default)
  col = columns(table_name).detect { |c| c.name == column_name.to_s }
  change_column(table_name, column_name, col.type,
    default: default, limit: col.limit, precision: col.precision, scale: col.scale)
end

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

Create a new MySQL database with optional :charset and :collation. Charset defaults to utf8.

Example:

create_database 'charset_test', :charset => 'latin1', :collation => 'latin1_bin'
create_database 'matt_development'
create_database 'matt_development', :charset => :big5


91
92
93
94
95
96
97
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 91

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_table(name, options = {}) ⇒ Object



107
108
109
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 107

def create_table(name, options = {})
  super(name, { options: 'ENGINE=InnoDB' }.merge(options))
end

#disable_referential_integrity(&block) ⇒ Object

:nodoc:



73
74
75
76
77
78
79
80
81
82
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 73

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

Drops a MySQL database.

Example:

drop_database('sebastian_development')


103
104
105
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 103

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

#indexes(table_name, name = nil) ⇒ Object



140
141
142
143
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 140

def indexes(table_name, name = nil)
  # Skip primary key indexes
  super(table_name, name).delete_if { |i| i.unique && i.name =~ /^PRIMARY$/ }
end

#join_to_update(update, select) ⇒ Object

Taken from ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 35

def join_to_update(update, select) #:nodoc:
  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



30
31
32
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 30

def limited_update_conditions(where_sql, _quoted_table_name, _quoted_primary_key)
  where_sql
end

#options_include_default?(options) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
148
149
150
151
152
153
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 145

def options_include_default?(options)
  # MySQL 5.x doesn't allow DEFAULT NULL for first timestamp column in a table
  if options.include?(:default) && options[:default].nil?
    if options.include?(:column) && options[:column].native_type =~ /timestamp/i
      options.delete(:default)
    end
  end
  super(options)
end

#prepared_statementsObject

Explicitly turning off prepared statements in the MySQL adapter because of a weird bug with SQLDescribeParam returning a string type for LIMIT parameters. This is blocking them from running with an error:

You have an error in your SQL syntax; ...
... right syntax to use near ''1'' at line 1: ...


22
23
24
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 22

def prepared_statements
  false
end

#quote_string(string) ⇒ Object

Quotes a string, escaping any ‘ (single quote) and \ (backslash) characters.



53
54
55
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 53

def quote_string(string)
  string.gsub(/\\/, '\&\&').gsub(/'/, "''")
end

#quoted_falseObject



65
66
67
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 65

def quoted_false
  '0'
end

#quoted_trueObject



57
58
59
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 57

def quoted_true
  '1'
end

#rename_column(table_name, column_name, new_column_name) ⇒ Object



133
134
135
136
137
138
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 133

def rename_column(table_name, column_name, new_column_name)
  col = columns(table_name).detect { |c| c.name == column_name.to_s }
  current_type = col.native_type
  current_type << "(#{col.limit})" if col.limit
  execute("ALTER TABLE #{table_name} CHANGE #{column_name} #{new_column_name} #{current_type}")
end

#rename_table(name, new_name) ⇒ Object

Renames a table.



112
113
114
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 112

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

#structure_dumpObject



155
156
157
158
159
160
161
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 155

def structure_dump
  select_all("SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'").map do |table|
    table.delete('Table_type')
    sql = "SHOW CREATE TABLE #{quote_table_name(table.to_a.first.last)}"
    exec_query(sql).first['Create Table'] + ";\n\n"
  end.join
end

#truncate(table_name, name = nil) ⇒ Object



26
27
28
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 26

def truncate(table_name, name = nil)
  execute("TRUNCATE TABLE #{quote_table_name(table_name)}", name)
end

#unquoted_falseObject



69
70
71
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 69

def unquoted_false
  0
end

#unquoted_trueObject



61
62
63
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 61

def unquoted_true
  1
end