Class: ODBCAdapter::Adapters::MySQLODBCAdapter
- Inherits:
-
ActiveRecord::ConnectionAdapters::ODBCAdapter
- Object
- AbstractAdapter
- ActiveRecord::ConnectionAdapters::ODBCAdapter
- ODBCAdapter::Adapters::MySQLODBCAdapter
- Defined in:
- lib/odbc_adapter/adapters/mysql_odbc_adapter.rb
Overview
Overrides specific to MySQL. Mostly taken from ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
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_CONNECTION_FAILED_MESSAGE, ActiveRecord::ConnectionAdapters::ODBCAdapter::ERR_CONNECTION_FAILED_REGEX, ActiveRecord::ConnectionAdapters::ODBCAdapter::ERR_DUPLICATE_KEY_VALUE, ActiveRecord::ConnectionAdapters::ODBCAdapter::ERR_QUERY_TIMED_OUT, ActiveRecord::ConnectionAdapters::ODBCAdapter::ERR_QUERY_TIMED_OUT_MESSAGE
Constants included from DatabaseStatements
DatabaseStatements::SQL_NO_NULLS, DatabaseStatements::SQL_NULLABLE, DatabaseStatements::SQL_NULLABLE_UNKNOWN
Instance Attribute Summary
Attributes inherited from ActiveRecord::ConnectionAdapters::ODBCAdapter
Instance Method Summary collapse
- #arel_visitor ⇒ Object
- #change_column(table_name, column_name, type, options = {}) ⇒ Object
- #change_column_default(table_name, column_name, default_or_changes) ⇒ Object
- #change_column_null(table_name, column_name, null, default = nil) ⇒ Object
-
#create_database(name, options = {}) ⇒ Object
Create a new MySQL database with optional
:charset
and:collation
. - #create_table(name, options = {}) ⇒ Object
- #disable_referential_integrity(&_block) ⇒ Object
-
#drop_database(name) ⇒ Object
Drops a MySQL database.
-
#indexes(table_name, name = nil) ⇒ Object
Skip primary key indexes.
-
#options_include_default?(options) ⇒ Boolean
MySQL 5.x doesn’t allow DEFAULT NULL for first timestamp column in a table.
-
#prepared_statements ⇒ Object
Explicitly turning off prepared statements in the MySQL adapter because of a weird bug with SQLDescribeParam returning a string type for LIMIT parameters.
-
#quote_string(string) ⇒ Object
Quotes a string, escaping any ‘ (single quote) and \ (backslash) characters.
- #quoted_false ⇒ Object
- #quoted_true ⇒ Object
- #rename_column(table_name, column_name, new_column_name) ⇒ Object
-
#rename_table(name, new_name) ⇒ Object
Renames a table.
- #truncate(table_name, name = nil) ⇒ Object
- #unquoted_false ⇒ Object
- #unquoted_true ⇒ Object
Methods inherited from ActiveRecord::ConnectionAdapters::ODBCAdapter
#active?, #adapter_name, #disconnect!, #initialize, #new_column, #reconnect!, #supports_migrations?
Methods included from SchemaStatements
#columns, #current_database, #foreign_keys, #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
Constructor Details
This class inherits a constructor from ActiveRecord::ConnectionAdapters::ODBCAdapter
Instance Method Details
#arel_visitor ⇒ Object
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
96 97 98 99 100 101 102 103 104 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 96 def change_column(table_name, column_name, type, = {}) unless () [:default] = column_for(table_name, column_name).default end change_column_sql = "ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{type_to_sql(type, [:limit], [:precision], [:scale])}" (change_column_sql, ) execute(change_column_sql) end |
#change_column_default(table_name, column_name, default_or_changes) ⇒ Object
106 107 108 109 110 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 106 def change_column_default(table_name, column_name, default_or_changes) default = extract_new_default_value(default_or_changes) 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
112 113 114 115 116 117 118 119 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 112 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 |
#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 'rails_development'
create_database 'rails_development', charset: :big5
71 72 73 74 75 76 77 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 71 def create_database(name, = {}) if [:collation] execute("CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{[:charset] || 'utf8'}` COLLATE `#{[:collation]}`") else execute("CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{[:charset] || 'utf8'}`") end end |
#create_table(name, options = {}) ⇒ Object
87 88 89 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 87 def create_table(name, = {}) super(name, { options: 'ENGINE=InnoDB' }.merge()) end |
#disable_referential_integrity(&_block) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 52 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
Drops a MySQL database.
Example:
drop_database('rails_development')
83 84 85 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 83 def drop_database(name) execute("DROP DATABASE IF EXISTS `#{name}`") end |
#indexes(table_name, name = nil) ⇒ Object
Skip primary key indexes
129 130 131 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 129 def indexes(table_name, name = nil) super(table_name, name).reject { |i| i.unique && i.name =~ /^PRIMARY$/ } end |
#options_include_default?(options) ⇒ Boolean
MySQL 5.x doesn’t allow DEFAULT NULL for first timestamp column in a table
135 136 137 138 139 140 141 142 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 135 def () if .include?(:default) && [:default].nil? if .include?(:column) && [:column].native_type =~ /timestamp/i .delete(:default) end end super() end |
#prepared_statements ⇒ Object
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.
32 33 34 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 32 def quote_string(string) string.gsub(/\\/, '\&\&').gsub(/'/, "''") end |
#quoted_false ⇒ Object
44 45 46 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 44 def quoted_false '0' end |
#quoted_true ⇒ Object
36 37 38 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 36 def quoted_true '1' end |
#rename_column(table_name, column_name, new_column_name) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 121 def rename_column(table_name, column_name, new_column_name) column = column_for(table_name, column_name) current_type = column.native_type current_type << "(#{column.limit})" if column.limit execute("ALTER TABLE #{table_name} CHANGE #{column_name} #{new_column_name} #{current_type}") end |
#rename_table(name, new_name) ⇒ Object
Renames a table.
92 93 94 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 92 def rename_table(name, new_name) execute("RENAME TABLE #{quote_table_name(name)} TO #{quote_table_name(new_name)}") 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_false ⇒ Object
48 49 50 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 48 def unquoted_false 0 end |
#unquoted_true ⇒ Object
40 41 42 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 40 def unquoted_true 1 end |