fake_adapter.rb 1.1 KB
Newer Older
1
module ActiveRecord
J
Jon Leighton 已提交
2 3
  module ConnectionHandling
    def fake_connection(config)
4 5 6 7 8 9
      ConnectionAdapters::FakeAdapter.new nil, logger
    end
  end

  module ConnectionAdapters
    class FakeAdapter < AbstractAdapter
10
      attr_accessor :data_sources, :primary_keys
11

12 13 14 15 16
      @columns = Hash.new { |h,k| h[k] = [] }
      class << self
        attr_reader :columns
      end

17 18
      def initialize(connection, logger)
        super
19
        @data_sources = []
20
        @primary_keys = {}
21
        @columns      = self.class.columns
22 23 24
      end

      def primary_key(table)
25
        @primary_keys[table] || "id"
26 27 28 29 30 31
      end

      def merge_column(table_name, name, sql_type = nil, options = {})
        @columns[table_name] << ActiveRecord::ConnectionAdapters::Column.new(
          name.to_s,
          options[:default],
S
Sean Griffin 已提交
32
          fetch_type_metadata(sql_type),
33 34 35
          options[:null])
      end

36
      def columns(table_name)
37 38
        @columns[table_name]
      end
39

40
      def data_source_exists?(*)
41 42 43
        true
      end

44 45 46
      def active?
        true
      end
47 48 49
    end
  end
end