diff options
author | Jamis Buck <jamis@37signals.com> | 2005-09-25 15:49:35 +0000 |
---|---|---|
committer | Jamis Buck <jamis@37signals.com> | 2005-09-25 15:49:35 +0000 |
commit | ea654654226924f9b900e7981fdbdbd452ca15d8 (patch) | |
tree | 606aefd628d9e31eababdefc59b02c3b29d51867 /activerecord/lib/active_record | |
parent | e7059fd28191a77d53e66389f8df5b22036699e8 (diff) | |
download | rails-ea654654226924f9b900e7981fdbdbd452ca15d8.tar.gz rails-ea654654226924f9b900e7981fdbdbd452ca15d8.tar.bz2 rails-ea654654226924f9b900e7981fdbdbd452ca15d8.zip |
Standardize the interpretation of boolean columns in the Mysql and Sqlite adapters. (Use MysqlAdapter.emulate_booleans = false to disable this behavior)
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2335 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
3 files changed, 33 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index fed51fea18..b9c06a21cb 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -158,7 +158,9 @@ module ActiveRecord # The type parameter should either contain :integer, :float, :datetime, :date, :text, or :string # The sql_type is just used for extracting the limit, such as 10 in "varchar(10)" def initialize(name, default, sql_type = nil, null = true) - @name, @default, @type, @null = name, type_cast(default), simplified_type(sql_type), null + @name, @type, @null = name, simplified_type(sql_type), null + # have to do this one separately because type_cast depends on #type + @default = type_cast(default) @limit = extract_limit(sql_type) unless sql_type.nil? end @@ -337,6 +339,9 @@ module ActiveRecord # raises an exception or returns false. def rollback_db_transaction() end + def quoted_true() "'t'" end + def quoted_false() "'f'" end + def quote(value, column = nil) case value when String @@ -348,8 +353,8 @@ module ActiveRecord "'#{quote_string(value)}'" # ' (for ruby-mode) end when NilClass then "NULL" - when TrueClass then (column && column.type == :boolean ? "'t'" : "1") - when FalseClass then (column && column.type == :boolean ? "'f'" : "0") + when TrueClass then (column && column.type == :boolean ? quoted_true : "1") + when FalseClass then (column && column.type == :boolean ? quoted_false : "0") when Float, Fixnum, Bignum then value.to_s when Date then "'#{value.to_s}'" when Time, DateTime then "'#{value.strftime("%Y-%m-%d %H:%M:%S")}'" @@ -502,7 +507,7 @@ module ActiveRecord def add_column_options!(sql, options) sql << " NOT NULL" if options[:null] == false - sql << " DEFAULT '#{options[:default]}'" unless options[:default].nil? + sql << " DEFAULT #{quote(options[:default], options[:column])}" unless options[:default].nil? end protected @@ -574,7 +579,7 @@ module ActiveRecord end def add_column_options!(sql, options) - base.add_column_options!(sql, options) + base.add_column_options!(sql, options.merge(:column => self)) end end diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 426d035bae..eea47817f6 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -41,6 +41,14 @@ module ActiveRecord end module ConnectionAdapters + class MysqlColumn < Column #:nodoc: + private + def simplified_type(field_type) + return :boolean if MysqlAdapter.emulate_booleans && field_type.downcase == "tinyint(1)" + super + end + end + # The MySQL adapter will work with both Ruby/MySQL, which is a Ruby-based MySQL adapter that comes bundled with Active Record, and with # the faster C-based MySQL/Ruby adapter (available both as a gem and from http://www.tmtm.org/en/mysql/ruby/). # @@ -56,7 +64,17 @@ module ActiveRecord # * <tt>:sslcert</tt> -- Necessary to use MySQL with an SSL connection # * <tt>:sslcapath</tt> -- Necessary to use MySQL with an SSL connection # * <tt>:sslcipher</tt> -- Necessary to use MySQL with an SSL connection + # + # By default, the MysqlAdapter will consider all columns of type tinyint(1) + # as boolean. If you wish to disable this emulation (which was the default + # behavior in versions 0.13.1 and earlier) you can add the following line + # to your environment.rb file: + # + # ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans = false class MysqlAdapter < AbstractAdapter + @@emulate_booleans = true + cattr_accessor :emulate_booleans + LOST_CONNECTION_ERROR_MESSAGES = [ "Server shutdown in progress", "Broken pipe", @@ -126,7 +144,7 @@ module ActiveRecord def columns(table_name, name = nil) sql = "SHOW FIELDS FROM #{table_name}" columns = [] - execute(sql, name).each { |field| columns << Column.new(field[0], field[4], field[1], field[2] == "YES") } + execute(sql, name).each { |field| columns << MysqlColumn.new(field[0], field[4], field[1], field[2] == "YES") } columns end @@ -184,6 +202,9 @@ module ActiveRecord end + def quoted_true() "1" end + def quoted_false() "0" end + def quote_column_name(name) "`#{name}`" end diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index e426149e02..ab42490020 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -99,7 +99,7 @@ module ActiveRecord :time => { :name => "datetime" }, :date => { :name => "date" }, :binary => { :name => "blob" }, - :boolean => { :name => "integer" } + :boolean => { :name => "boolean" } } end |