add_column(table_name, column_name, type, options = {})
click to toggle source
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)
click to toggle source
def add_column_position!(sql, options)
if options[:first]
sql << " FIRST"
elsif options[:after]
sql << " AFTER #{quote_column_name(options[:after])}"
end
end
case_sensitive_equality_operator()
click to toggle source
def case_sensitive_equality_operator
"= BINARY"
end
case_sensitive_modifier(node)
click to toggle source
def case_sensitive_modifier(node)
Arel::Nodes::Bin.new(node)
end
change_column_null(table_name, column_name, null, default = nil)
click to toggle source
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
charset()
click to toggle source
def charset
show_variable("character_set_database")
end
collation()
click to toggle source
def collation
show_variable("collation_database")
end
create_savepoint()
click to toggle source
def create_savepoint
execute("SAVEPOINT #{current_savepoint_name}")
end
current_database()
click to toggle source
def current_database
select_one("SELECT DATABASE() as db")["db"]
end
exec_delete(sql, name, binds)
click to toggle source
exec_insert(sql, name, binds)
click to toggle source
DATABASE STATEMENTS ======================================
def exec_insert(sql, name, binds)
execute sql, name, binds
end
exec_update(sql, name, binds)
click to toggle source
limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
click to toggle source
def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
where_sql
end
modify_types(types)
click to toggle source
def modify_types(types)
types[:primary_key] = "int(11) DEFAULT NULL auto_increment PRIMARY KEY"
types[:integer] = { :name => 'int', :limit => 4 }
types[:decimal] = { :name => "decimal" }
types[:timestamp] = { :name => "datetime" }
types[:datetime][:limit] = nil
types
end
native_database_types()
click to toggle source
def native_database_types
NATIVE_DATABASE_TYPES
end
primary_key(table)
click to toggle source
Returns just a table's primary key
def primary_key(table)
pk_and_sequence = pk_and_sequence_for(table)
pk_and_sequence && pk_and_sequence.first
end
quote(value, column = nil)
click to toggle source
QUOTING ==================================================
def quote(value, column = nil)
return value.quoted_id if value.respond_to?(:quoted_id)
return value.to_s if column && column.type == :primary_key
if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
"x'#{column.class.string_to_binary(value).unpack("H*")[0]}'"
elsif value.kind_of?(BigDecimal)
value.to_s("F")
else
super
end
end
release_savepoint()
click to toggle source
def release_savepoint
execute("RELEASE SAVEPOINT #{current_savepoint_name}")
end
rename_table(name, new_name)
click to toggle source
def rename_table(name, new_name)
execute "RENAME TABLE #{quote_table_name(name)} TO #{quote_table_name(new_name)}"
end
rollback_to_savepoint()
click to toggle source
def rollback_to_savepoint
execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
end
show_variable(var)
click to toggle source
def show_variable(var)
res = execute("show variables like '#{var}'")
result_row = res.detect {|row| row["Variable_name"] == var }
result_row && result_row["Value"]
end
supports_migrations?()
click to toggle source
Returns true, since this connection adapter supports migrations.
def supports_migrations?
true
end
type_to_sql(type, limit = nil, precision = nil, scale = nil)
click to toggle source
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
case type.to_s
when 'binary'
case limit
when 0..0xfff; "varbinary(#{limit})"
when nil; "blob"
when 0x1000..0xffffffff; "blob(#{limit})"
else raise(ActiveRecordError, "No binary type has character length #{limit}")
end
when 'integer'
case limit
when 1; 'tinyint'
when 2; 'smallint'
when 3; 'mediumint'
when nil, 4, 11; 'int(11)'
when 5..8; 'bigint'
else raise(ActiveRecordError, "No integer type has byte size #{limit}")
end
when 'text'
case limit
when 0..0xff; 'tinytext'
when nil, 0x100..0xffff; 'text'
when 0x10000..0xffffff; 'mediumtext'
when 0x1000000..0xffffffff; 'longtext'
else raise(ActiveRecordError, "No text type has character length #{limit}")
end
else
super
end
end