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::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
Instance Method Summary collapse
- #change_column(table_name, column_name, type, options = {}) ⇒ Object
- #change_column_default(table_name, column_name, default) ⇒ Object
-
#create_database(name, options = {}) ⇒ Object
Create a new MySQL database with optional
:charsetand:collation. - #create_table(name, options = {}) ⇒ Object
-
#disable_referential_integrity(&block) ⇒ Object
:nodoc:.
-
#drop_database(name) ⇒ Object
Drops a MySQL database.
- #indexes(table_name, name = nil) ⇒ Object
-
#join_to_update(update, select) ⇒ Object
Taken from ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.
- #limited_update_conditions(where_sql, _quoted_table_name, _quoted_primary_key) ⇒ Object
- #options_include_default?(options) ⇒ Boolean
-
#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.
- #structure_dump ⇒ Object
- #truncate(table_name, name = nil) ⇒ Object
- #unquoted_false ⇒ Object
- #unquoted_true ⇒ Object
Methods inherited from ActiveRecord::ConnectionAdapters::ODBCAdapter
#active?, #adapter_name, #disconnect!, #initialize, #reconnect!, #supports_migrations?
Methods included from SchemaStatements
#columns, #current_database, #index_name, #native_database_types, #primary_key, #tables
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, #select_rows
Methods included from DatabaseLimits
Constructor Details
This class inherits a constructor from ActiveRecord::ConnectionAdapters::ODBCAdapter
Instance Method Details
#change_column(table_name, column_name, type, options = {}) ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 102 def change_column(table_name, column_name, type, = {}) # column_name.to_s used in case column_name is a symbol unless () [: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])}" (change_column_sql, ) execute(change_column_sql) end |
#change_column_default(table_name, column_name, default) ⇒ Object
113 114 115 116 117 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 113 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
77 78 79 80 81 82 83 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 77 def create_database(name, = {}) if [: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
93 94 95 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 93 def create_table(name, = {}) super(name, { options: 'ENGINE=InnoDB' }.merge()) end |
#disable_referential_integrity(&block) ⇒ Object
:nodoc:
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 59 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')
89 90 91 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 89 def drop_database(name) #:nodoc: execute("DROP DATABASE IF EXISTS `#{name}`") end |
#indexes(table_name, name = nil) ⇒ Object
126 127 128 129 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 126 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
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 21 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
16 17 18 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 16 def limited_update_conditions(where_sql, _quoted_table_name, _quoted_primary_key) where_sql end |
#options_include_default?(options) ⇒ Boolean
131 132 133 134 135 136 137 138 139 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 131 def () # MySQL 5.x doesn't allow DEFAULT NULL for first timestamp column in a table if .include?(:default) && [:default].nil? if .include?(:column) && [:column].native_type =~ /timestamp/i .delete(:default) end end super() end |
#quote_string(string) ⇒ Object
Quotes a string, escaping any ‘ (single quote) and \ (backslash) characters.
39 40 41 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 39 def quote_string(string) string.gsub(/\\/, '\&\&').gsub(/'/, "''") end |
#quoted_false ⇒ Object
51 52 53 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 51 def quoted_false '0' end |
#quoted_true ⇒ Object
43 44 45 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 43 def quoted_true '1' end |
#rename_column(table_name, column_name, new_column_name) ⇒ Object
119 120 121 122 123 124 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 119 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.
98 99 100 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 98 def rename_table(name, new_name) execute("RENAME TABLE #{quote_table_name(name)} TO #{quote_table_name(new_name)}") end |
#structure_dump ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 141 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
12 13 14 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 12 def truncate(table_name, name = nil) execute("TRUNCATE TABLE #{quote_table_name(table_name)}", name) end |
#unquoted_false ⇒ Object
55 56 57 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 55 def unquoted_false 0 end |
#unquoted_true ⇒ Object
47 48 49 |
# File 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb', line 47 def unquoted_true 1 end |