abstract_adapter.rb 15.5 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
require 'benchmark'
require 'date'

# Method that requires a library, ensuring that rubygems is loaded
# This is used in the database adaptors to require DB drivers. Reasons:
# (1) database drivers are the only third-party library that Rails depend upon
# (2) they are often installed as gems
def require_library_or_gem(library_name)
  begin
    require library_name
  rescue LoadError => cannot_require
    # 1. Requiring the module is unsuccessful, maybe it's a gem and nobody required rubygems yet. Try.
    begin
      require 'rubygems'
    rescue LoadError => rubygems_not_installed
      raise cannot_require
    end
    # 2. Rubygems is installed and loaded. Try to load the library again
    begin 
      require library_name
    rescue LoadError => gem_not_installed
      raise cannot_require
    end
  end
end

module ActiveRecord
  class Base
    class ConnectionSpecification #:nodoc:
      attr_reader :config, :adapter_method
      def initialize (config, adapter_method)
        @config, @adapter_method = config, adapter_method
      end
    end
    
    # The class -> [adapter_method, config] map
    @@defined_connections = {}

    # Establishes the connection to the database. Accepts a hash as input where
    # the :adapter key must be specified with the name of a database adapter (in lower-case)
    # example for regular databases (MySQL, Postgresql, etc):
    #
    #   ActiveRecord::Base.establish_connection(
    #     :adapter  => "mysql",
    #     :host     => "localhost",
    #     :username => "myuser",
    #     :password => "mypass",
    #     :database => "somedatabase"
    #   )
    #
    # Example for SQLite database:
    #
    #   ActiveRecord::Base.establish_connection(
    #     :adapter => "sqlite",
    #     :dbfile  => "path/to/dbfile"
    #   )
    #
    # Also accepts keys as strings (for parsing from yaml for example):
    #   ActiveRecord::Base.establish_connection(
    #     "adapter" => "sqlite",
    #     "dbfile"  => "path/to/dbfile"
    #   )
    #
    # The exceptions AdapterNotSpecified, AdapterNotFound and ArgumentError
    # may be returned on an error.
66 67 68 69 70 71 72 73
    def self.establish_connection(spec = nil)
      case spec
        when nil
          raise AdapterNotSpecified unless defined? RAILS_ENV
          establish_connection(RAILS_ENV)
        when ConnectionSpecification
          @@defined_connections[self] = spec
        when Symbol, String
74 75 76
          if configuration = configurations[spec.to_s]
            establish_connection(configuration)
          else
77
            raise AdapterNotSpecified, "#{spec} database is not configured"
78
          end
79
        else
80
          spec = spec.symbolize_keys
81
          unless spec.key?(:adapter) then raise AdapterNotSpecified, "database configuration does not specify adapter" end
82
          adapter_method = "#{spec[:adapter]}_connection"
83
          unless respond_to?(adapter_method) then raise AdapterNotFound, "database configuration specifies nonexistent #{spec[:adapter]} adapter" end
84 85
          remove_connection
          establish_connection(ConnectionSpecification.new(spec, adapter_method))
D
Initial  
David Heinemeier Hansson 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
      end
    end

    # Locate the connection of the nearest super class. This can be an
    # active or defined connections: if it is the latter, it will be
    # opened and set as the active connection for the class it was defined
    # for (not necessarily the current class).
    def self.retrieve_connection #:nodoc:
      klass = self
      until klass == ActiveRecord::Base.superclass
        Thread.current['active_connections'] ||= {}
        if Thread.current['active_connections'][klass]
          return Thread.current['active_connections'][klass]
        elsif @@defined_connections[klass]
          klass.connection = @@defined_connections[klass]
          return self.connection
        end
        klass = klass.superclass
      end
      raise ConnectionNotEstablished
    end
    
    # Returns true if a connection that's accessible to this class have already been opened.
    def self.connected?
      klass = self
      until klass == ActiveRecord::Base.superclass
        if Thread.current['active_connections'].is_a?(Hash) && Thread.current['active_connections'][klass]
          return true 
        else
          klass = klass.superclass
        end
      end
      return false
    end
    
    # Remove the connection for this class. This will close the active
    # connection and the defined connection (if they exist). The result
    # can be used as argument for establish_connection, for easy
    # re-establishing of the connection.
    def self.remove_connection(klass=self)
      conn = @@defined_connections[klass]
      @@defined_connections.delete(klass)
      Thread.current['active_connections'] ||= {}
      Thread.current['active_connections'][klass] = nil
      conn.config if conn
    end
    
    # Set the connection for the class.
    def self.connection=(spec)
      raise ConnectionNotEstablished unless spec
      conn = self.send(spec.adapter_method, spec.config)
      Thread.current['active_connections'] ||= {}
      Thread.current['active_connections'][self] = conn
    end

    # Converts all strings in a hash to symbols.
142
    def self.symbolize_strings_in_hash(hash) #:nodoc:
143
      hash.symbolize_keys
D
Initial  
David Heinemeier Hansson 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    end
  end

  module ConnectionAdapters # :nodoc:
    class Column # :nodoc:
      attr_reader :name, :default, :type, :limit
      # The name should contain the name of the column, such as "name" in "name varchar(250)"
      # The default should contain the type-casted default of the column, such as 1 in "count int(11) DEFAULT 1"
      # 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)
        @name, @default, @type = name, default, simplified_type(sql_type)
        @limit = extract_limit(sql_type) unless sql_type.nil?
      end

      def default
        type_cast(@default)
      end

      def klass
        case type
          when :integer       then Fixnum
          when :float         then Float
          when :datetime      then Time
          when :date          then Date
169 170
          when :timestamp     then Time
          when :time          then Time
D
Initial  
David Heinemeier Hansson 已提交
171
          when :text, :string then String
172
          when :binary        then String
D
Initial  
David Heinemeier Hansson 已提交
173 174 175 176 177 178 179
          when :boolean       then Object
        end
      end
      
      def type_cast(value)
        if value.nil? then return nil end
        case type
180 181 182 183 184 185 186 187
          when :string    then value
          when :text      then value
          when :integer   then value.to_i
          when :float     then value.to_f
          when :datetime  then string_to_time(value)
          when :timestamp then string_to_time(value)
          when :time      then string_to_dummy_time(value)
          when :date      then string_to_date(value)
188
          when :binary    then binary_to_string(value)
189
          when :boolean   then (value == "t" or value == true ? true : false)
D
Initial  
David Heinemeier Hansson 已提交
190 191 192 193 194 195 196
          else value
        end
      end
      
      def human_name
        Base.human_attribute_name(@name)
      end
197 198 199 200 201 202 203 204
      
      def string_to_binary(value)
        value
      end     

      def binary_to_string(value)
        value
      end     
D
Initial  
David Heinemeier Hansson 已提交
205 206 207

      private
        def string_to_date(string)
208
          return string if string.is_a?(Date)
D
Initial  
David Heinemeier Hansson 已提交
209 210 211 212 213 214
          date_array = ParseDate.parsedate(string)
          # treat 0000-00-00 as nil
          Date.new(date_array[0], date_array[1], date_array[2]) rescue nil
        end
        
        def string_to_time(string)
215
          return string if string.is_a?(Time)
D
Initial  
David Heinemeier Hansson 已提交
216 217
          time_array = ParseDate.parsedate(string).compact
          # treat 0000-00-00 00:00:00 as nil
218
          Time.send(Base.default_timezone, *time_array) rescue nil
D
Initial  
David Heinemeier Hansson 已提交
219 220
        end

221
        def string_to_dummy_time(string)
222
          return string if string.is_a?(Time)
223 224 225 226
          time_array = ParseDate.parsedate(string)
          # pad the resulting array with dummy date information
          time_array[0] = 2000; time_array[1] = 1; time_array[2] = 1;
          Time.send(Base.default_timezone, *time_array) rescue nil
227
        end 
228
        
D
Initial  
David Heinemeier Hansson 已提交
229 230 231
        def extract_limit(sql_type)
          $1.to_i if sql_type =~ /\((.*)\)/
        end
232
        
D
Initial  
David Heinemeier Hansson 已提交
233 234 235 236 237 238
        def simplified_type(field_type)
          case field_type
            when /int/i
              :integer
            when /float|double|decimal|numeric/i
              :float
239
            when /datetime/i
D
Initial  
David Heinemeier Hansson 已提交
240
              :datetime
241 242 243 244
            when /timestamp/i
              :timestamp
            when /time/i
              :time
D
Initial  
David Heinemeier Hansson 已提交
245 246
            when /date/i
              :date
247
            when /clob/i, /text/i
D
Initial  
David Heinemeier Hansson 已提交
248
              :text
249 250
            when /blob/i, /binary/i
              :binary
D
Initial  
David Heinemeier Hansson 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
            when /char/i, /string/i
              :string
            when /boolean/i
              :boolean
          end
        end
    end

    # All the concrete database adapters follow the interface laid down in this class.
    # You can use this interface directly by borrowing the database connection from the Base with
    # Base.connection.
    class AbstractAdapter
      @@row_even = true

      include Benchmark

      def initialize(connection, logger = nil) # :nodoc:
        @connection, @logger = connection, logger
        @runtime = 0
      end

      # Returns an array of record hashes with the column names as a keys and fields as values.
      def select_all(sql, name = nil) end
      
      # Returns a record hash with the column names as a keys and fields as values.
      def select_one(sql, name = nil) end

      # Returns an array of column objects for the table specified by +table_name+.
      def columns(table_name, name = nil) end

      # Returns the last auto-generated ID from the affected table.
      def insert(sql, name = nil, pk = nil, id_value = nil) end

284
      # Executes the update statement and returns the number of rows affected.
D
Initial  
David Heinemeier Hansson 已提交
285 286
      def update(sql, name = nil) end

287
      # Executes the delete statement and returns the number of rows affected.
D
Initial  
David Heinemeier Hansson 已提交
288 289 290 291 292 293 294 295 296
      def delete(sql, name = nil) end

      def reset_runtime # :nodoc:
        rt = @runtime
        @runtime = 0
        return rt
      end

      # Wrap a block in a transaction.  Returns result of block.
297
      def transaction(start_db_transaction = true)
D
Initial  
David Heinemeier Hansson 已提交
298 299
        begin
          if block_given?
300
            begin_db_transaction if start_db_transaction
D
Initial  
David Heinemeier Hansson 已提交
301
            result = yield
302
            commit_db_transaction if start_db_transaction
D
Initial  
David Heinemeier Hansson 已提交
303 304 305
            result
          end
        rescue Exception => database_transaction_rollback
306
          rollback_db_transaction if start_db_transaction
D
Initial  
David Heinemeier Hansson 已提交
307 308 309 310 311 312 313 314 315 316
          raise
        end
      end

      # Begins the transaction (and turns off auto-committing).
      def begin_db_transaction()    end
      
      # Commits the transaction (and turns on auto-committing). 
      def commit_db_transaction()   end

317
      # Rolls back the transaction (and turns on auto-committing). Must be done if the transaction block
D
Initial  
David Heinemeier Hansson 已提交
318 319 320 321 322
      # raises an exception or returns false.
      def rollback_db_transaction() end

      def quote(value, column = nil)
        case value
323 324 325 326 327 328
          when String                
            if column && column.type == :binary
              "'#{quote_string(column.string_to_binary(value))}'" # ' (for ruby-mode)
            else
              "'#{quote_string(value)}'" # ' (for ruby-mode)
            end
329 330 331 332 333 334 335
          when NilClass              then "NULL"
          when TrueClass             then (column && column.type == :boolean ? "'t'" : "1")
          when FalseClass            then (column && column.type == :boolean ? "'f'" : "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")}'"
          else                            "'#{quote_string(value.to_yaml)}'"
D
Initial  
David Heinemeier Hansson 已提交
336 337 338 339 340 341 342 343
        end
      end

      def quote_string(s)
        s.gsub(/\\/, '\&\&').gsub(/'/, "''") # ' (for ruby-mode)
      end

      def quote_column_name(name)
344
        name
D
Initial  
David Heinemeier Hansson 已提交
345 346
      end

347 348 349 350 351
      # Returns the human-readable name of the adapter.  Use mixed case - one can always use downcase if needed.
      def adapter_name()
        'Abstract'
      end

D
Initial  
David Heinemeier Hansson 已提交
352 353 354
      # Returns a string of the CREATE TABLE SQL statements for recreating the entire structure of the database.
      def structure_dump() end

355 356 357 358
      def add_limit!(sql, limit)
        sql << " LIMIT #{limit}"
      end

359 360 361

      def initialize_schema_information
        begin
362
          execute "CREATE TABLE schema_info (version #{native_database_types[:integer][:name]}#{native_database_types[:integer][:limit]})"
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
          insert "INSERT INTO schema_info (version) VALUES(0)"
        rescue ActiveRecord::StatementInvalid
          # Schema has been intialized
        end
      end

      def create_table(name)
        execute "CREATE TABLE #{name} (id #{native_database_types[:primary_key]})"
        table_definition = yield TableDefinition.new
        table_definition.columns.each { |column_name, type, options| add_column(name, column_name, type, options) }
      end

      def drop_table(name)
        execute "DROP TABLE #{name}"
      end

      def add_column(table_name, column_name, type, options = {})
380 381 382
        native_type = native_database_types[type]
        add_column_sql = "ALTER TABLE #{table_name} ADD #{column_name} #{native_type[:name]}"
        add_column_sql << "(#{options[:limit] || native_type[:limit]})" if options[:limit] || native_type[:limit]
383 384 385 386 387 388 389 390 391
        add_column_sql << " DEFAULT '#{options[:default]}'" if options[:default]
        execute(add_column_sql)
      end

      def remove_column(table_name, column_name)
        execute "ALTER TABLE #{table_name} DROP #{column_name}"
      end


D
Initial  
David Heinemeier Hansson 已提交
392
      protected
393 394
        def log(sql, name, connection = nil)
          connection ||= @connection
D
Initial  
David Heinemeier Hansson 已提交
395
          begin
396
            if @logger.nil? || @logger.level > Logger::INFO
397 398
              yield connection
            elsif block_given?
D
Initial  
David Heinemeier Hansson 已提交
399
              result = nil
400
              bm = measure { result = yield connection }
D
Initial  
David Heinemeier Hansson 已提交
401 402 403
              @runtime += bm.real
              log_info(sql, name, bm.real)
              result
404 405 406
            else
              log_info(sql, name, 0)
              nil
D
Initial  
David Heinemeier Hansson 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
            end
          rescue => e
            log_info("#{e.message}: #{sql}", name, 0)
            raise ActiveRecord::StatementInvalid, "#{e.message}: #{sql}"
          end
        end

        def log_info(sql, name, runtime)
          if @logger.nil? then return end

          @logger.info(
            format_log_entry(
              "#{name.nil? ? "SQL" : name} (#{sprintf("%f", runtime)})", 
              sql.gsub(/ +/, " ")
            )
          )
        end

        def format_log_entry(message, dump = nil)
426 427 428 429 430 431 432 433 434 435 436
          if ActiveRecord::Base.colorize_logging
            if @@row_even then
              @@row_even = false; caller_color = "1;32"; message_color = "4;33"; dump_color = "1;37"
            else
              @@row_even = true; caller_color = "1;36"; message_color = "4;35"; dump_color = "0;37"
            end
          
            log_entry = "  \e[#{message_color}m#{message}\e[m"
            log_entry << "   \e[#{dump_color}m%s\e[m" % dump if dump.kind_of?(String) && !dump.nil?
            log_entry << "   \e[#{dump_color}m%p\e[m" % dump if !dump.kind_of?(String) && !dump.nil?
            log_entry
D
Initial  
David Heinemeier Hansson 已提交
437
          else
438
            "%s  %s" % [message, dump]
D
Initial  
David Heinemeier Hansson 已提交
439 440 441
          end
        end
    end
442 443 444 445 446 447 448 449 450 451 452 453 454

    class TableDefinition
      attr_accessor :columns
      
      def initialize
        @columns = []
      end

      def column(name, type, options = {})
        @columns << [ name, type, options ]
        self
      end
    end
D
Initial  
David Heinemeier Hansson 已提交
455
  end
456
end