abstract_adapter.rb 4.0 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1 2 3
require 'benchmark'
require 'date'

4 5 6 7 8 9
require 'active_record/connection_adapters/abstract/schema_definitions'
require 'active_record/connection_adapters/abstract/schema_statements'
require 'active_record/connection_adapters/abstract/database_statements'
require 'active_record/connection_adapters/abstract/quoting'
require 'active_record/connection_adapters/abstract/connection_specification'

D
Initial  
David Heinemeier Hansson 已提交
10 11 12 13 14
module ActiveRecord
  module ConnectionAdapters # :nodoc:
    # 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.
15 16 17 18 19 20
    #
    # Most of the methods in the adapter are useful during migrations.  Most
    # notably, SchemaStatements#create_table, SchemaStatements#drop_table,
    # SchemaStatements#add_index, SchemaStatements#remove_index,
    # SchemaStatements#add_column, SchemaStatements#change_column and
    # SchemaStatements#remove_column are very useful.
D
Initial  
David Heinemeier Hansson 已提交
21
    class AbstractAdapter
22
      include Quoting, DatabaseStatements, SchemaStatements
D
Initial  
David Heinemeier Hansson 已提交
23 24
      @@row_even = true

25
      def initialize(connection, logger = nil) #:nodoc:
D
Initial  
David Heinemeier Hansson 已提交
26 27 28 29
        @connection, @logger = connection, logger
        @runtime = 0
      end

30 31
      # Returns the human-readable name of the adapter.  Use mixed case - one
      # can always use downcase if needed.
32
      def adapter_name
33 34
        'Abstract'
      end
35

36
      # Does this adapter support migrations?  Backend specific, as the
37
      # abstract adapter always returns +false+.
38 39
      def supports_migrations?
        false
40
      end
41 42 43 44 45 46
      
      # Does this adapter support using DISTINCT within COUNT?  This is +true+
      # for all adapters except sqlite.
      def supports_count_distinct?
        true
      end
47

48 49 50 51
      # Should primary key values be selected from their corresponding
      # sequence before the insert statement?  If true, next_sequence_value
      # is called before each insert to set the record's primary key.
      # This is false for all adapters but Firebird.
52
      def prefetch_primary_key?(table_name = nil)
53 54 55
        false
      end

56
      def reset_runtime #:nodoc:
57 58
        rt, @runtime = @runtime, 0
        rt
59
      end
60

61 62 63 64 65 66 67 68 69 70 71 72 73

      # CONNECTION MANAGEMENT ====================================

      # Is this connection active and ready to perform queries?
      def active?
        true
      end

      # Close this connection and open a new one in its place.
      def reconnect!
      end


74
      protected
75
        def log(sql, name)
76 77 78 79 80 81 82
          if block_given?
            if @logger and @logger.level <= Logger::INFO
              result = nil
              seconds = Benchmark.realtime { result = yield }
              @runtime += seconds
              log_info(sql, name, seconds)
              result
83
            else
84
              yield
D
Initial  
David Heinemeier Hansson 已提交
85
            end
86 87 88
          else
            log_info(sql, name, 0)
            nil
D
Initial  
David Heinemeier Hansson 已提交
89
          end
90
        rescue Exception => e
91
          # Log message and raise exception.
92 93 94
          message = "#{e.class.name}: #{e.message}: #{sql}"
          log_info(message, name, 0)
          raise ActiveRecord::StatementInvalid, message
D
Initial  
David Heinemeier Hansson 已提交
95 96 97
        end

        def log_info(sql, name, runtime)
98
          return unless @logger
D
Initial  
David Heinemeier Hansson 已提交
99

100
          @logger.debug(
D
Initial  
David Heinemeier Hansson 已提交
101
            format_log_entry(
102
              "#{name.nil? ? "SQL" : name} (#{sprintf("%f", runtime)})",
D
Initial  
David Heinemeier Hansson 已提交
103 104 105 106 107 108
              sql.gsub(/ +/, " ")
            )
          )
        end

        def format_log_entry(message, dump = nil)
109
          if ActiveRecord::Base.colorize_logging
110 111 112
            if @@row_even
              @@row_even = false
              message_color, dump_color = "4;36;1", "0;1"
113
            else
114 115
              @@row_even = true
              message_color, dump_color = "4;35;1", "0"
116
            end
117

118 119
            log_entry = "  \e[#{message_color}m#{message}\e[0m   "
            log_entry << "\e[#{dump_color}m%#{String === dump ? 's' : 'p'}\e[0m" % dump if dump
120
            log_entry
D
Initial  
David Heinemeier Hansson 已提交
121
          else
122
            "%s  %s" % [message, dump]
D
Initial  
David Heinemeier Hansson 已提交
123 124
          end
        end
125
    end
D
Initial  
David Heinemeier Hansson 已提交
126
  end
127
end