1 require 'active_record/connection_adapters/abstract/schema_definitions'
4 # Don't need to load native mysql adapter
5 $LOADED_FEATURES << "active_record/connection_adapters/mysql_adapter.rb"
7 module ActiveRecordExtensions
8 def mysql_connection(config)
11 config[:url] = config[:url]['?'] ? "#{config[:url]}&#{MySQL::URL_OPTIONS}" : "#{config[:url]}?#{MySQL::URL_OPTIONS}"
13 config[:url] = "jdbc:mysql://#{config[:host]}:#{config[:port]}/#{config[:database]}?#{MySQL::URL_OPTIONS}"
15 config[:driver] = "com.mysql.jdbc.Driver"
16 jdbc_connection(config)
21 URL_OPTIONS = "zeroDateTimeBehavior=convertToNull&jdbcCompliantTruncation=false&useUnicode=true&characterEncoding=utf8"
22 def self.column_selector
23 [/mysql/i, lambda {|cfg,col| col.extend(::JdbcSpec::MySQL::Column)}]
26 def self.adapter_selector
27 [/mysql/i, lambda {|cfg,adapt| adapt.extend(::JdbcSpec::MySQL)}]
30 def self.extended(adapter)
31 adapter.execute("SET SQL_AUTO_IS_NULL=0")
35 TYPES_ALLOWING_EMPTY_STRING_DEFAULT = Set.new([:binary, :string, :text])
37 def simplified_type(field_type)
38 return :boolean if field_type =~ /tinyint\(1\)|bit/i
39 return :string if field_type =~ /enum/i
43 def init_column(name, default, *args)
44 @original_default = default
45 @default = nil if missing_default_forged_as_empty_string?
48 # MySQL misreports NOT NULL column default when none is given.
49 # We can't detect this for columns which may have a legitimate ''
50 # default (string, text, binary) but we can for others (integer,
51 # datetime, boolean, and the rest).
53 # Test whether the column has default '', is not null, and is not
54 # a type allowing default ''.
55 def missing_default_forged_as_empty_string?
56 !null && @original_default == '' && !TYPES_ALLOWING_EMPTY_STRING_DEFAULT.include?(type)
61 tp[:primary_key] = "int(11) DEFAULT NULL auto_increment PRIMARY KEY"
62 tp[:decimal] = { :name => "decimal" }
63 tp[:timestamp] = { :name => "datetime" }
64 tp[:datetime][:limit] = nil
68 # QUOTING ==================================================
70 def quote(value, column = nil)
71 return value.quoted_id if value.respond_to?(:quoted_id)
73 if column && column.type == :primary_key
75 elsif column && String === value && column.type == :binary && column.class.respond_to?(:string_to_binary)
76 s = column.class.string_to_binary(value).unpack("H*")[0]
78 elsif BigDecimal === value
79 "'#{value.to_s("F")}'"
85 def quote_column_name(name) #:nodoc:
89 def quote_table_name(name) #:nodoc:
90 quote_column_name(name).gsub('.', '`.`')
101 def begin_db_transaction #:nodoc:
104 # Transactions aren't supported
107 def commit_db_transaction #:nodoc:
110 # Transactions aren't supported
113 def rollback_db_transaction #:nodoc:
116 # Transactions aren't supported
119 def disable_referential_integrity(&block) #:nodoc:
120 old = select_value("SELECT @@FOREIGN_KEY_CHECKS")
122 update("SET FOREIGN_KEY_CHECKS = 0")
125 update("SET FOREIGN_KEY_CHECKS = #{old}")
129 # SCHEMA STATEMENTS ========================================
131 def structure_dump #:nodoc:
133 sql = "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'"
138 select_all(sql).inject("") do |structure, table|
139 table.delete('Table_type')
141 hash = select_one("SHOW CREATE TABLE #{quote_table_name(table.to_a.first.last)}")
143 if(table = hash["Create Table"])
144 structure += table + ";\n\n"
145 elsif(view = hash["Create View"])
146 structure += view + ";\n\n"
151 def recreate_database(name) #:nodoc:
153 create_database(name)
156 def create_database(name, options = {}) #:nodoc:
157 if options[:collation]
158 execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
160 execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
164 def drop_database(name) #:nodoc:
165 execute "DROP DATABASE IF EXISTS `#{name}`"
169 select_one("SELECT DATABASE() as db")["db"]
172 def create_table(name, options = {}) #:nodoc:
173 super(name, {:options => "ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin"}.merge(options))
176 def rename_table(name, new_name)
177 execute "RENAME TABLE #{quote_table_name(name)} TO #{quote_table_name(new_name)}"
180 def change_column_default(table_name, column_name, default) #:nodoc:
181 current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"]
183 execute("ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{current_type} DEFAULT #{quote(default)}")
186 def change_column(table_name, column_name, type, options = {}) #:nodoc:
187 unless options_include_default?(options)
188 if column = columns(table_name).find { |c| c.name == column_name.to_s }
189 options[:default] = column.default
191 raise "No such column: #{table_name}.#{column_name}"
195 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])}"
196 add_column_options!(change_column_sql, options)
197 execute(change_column_sql)
200 def rename_column(table_name, column_name, new_column_name) #:nodoc:
201 cols = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")
202 current_type = cols["Type"] || cols["COLUMN_TYPE"]
203 execute "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_table_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}"
206 def add_limit_offset!(sql, options) #:nodoc:
207 if limit = options[:limit]
208 unless offset = options[:offset]
209 sql << " LIMIT #{limit}"
211 sql << " LIMIT #{offset}, #{limit}"
216 def show_variable(var)
217 res = execute("show variables like '#{var}'")
218 row = res.detect {|row| row["Variable_name"] == var }
223 show_variable("character_set_database")
227 show_variable("collation_database")