Class: ActiveRecord::ConnectionAdapters::EmPostgreSQLAdapter

Inherits:
PostgreSQLAdapter
  • Object
show all
Defined in:
lib/active_record/connection_adapters/em_postgresql_adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection, logger, host_parameters, connection_parameters, config) ⇒ EmPostgreSQLAdapter

checkin :logi checkout :logo

def logo

puts "#{Fiber.current.object_id} #{self.object_id} checkout"

end def logi

puts "#{Fiber.current.object_id} #{self.object_id} checkin"

end



27
28
29
30
31
32
# File 'lib/active_record/connection_adapters/em_postgresql_adapter.rb', line 27

def initialize(connection, logger, host_parameters, connection_parameters, config)
  @hostname = host_parameters[0]
  @port = host_parameters[1]
  @connect_parameters, @config = connection_parameters, config
  super(connection, logger, nil, config)
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


86
87
88
89
90
# File 'lib/active_record/connection_adapters/em_postgresql_adapter.rb', line 86

def active?
  !@connection.closed? && @connection.exec('SELECT 1')
rescue RuntimeError => re
  false
end

#connectObject

Raises:

  • (RuntimeError)


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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/active_record/connection_adapters/em_postgresql_adapter.rb', line 34

def connect
  @logger.info "Connecting to #{@hostname}:#{@port}"
  @connection = ::EM.connect(@hostname, @port, ::EM::P::PostgresConnection)

  fiber = Fiber.current
  yielding = true
  result = false
  message = nil
  task = @connection.connect(*@connect_parameters)
  task.callback do |rc, msg|
    result = rc
    message = msg
    fiber.resume
  end
  task.errback do |msg|
    result = false
    message = msg
    yielding = false
  end
  Fiber.yield if yielding

  raise RuntimeError, "Connection failed: #{message}" if !result
  
  # Use escape string syntax if available. We cannot do this lazily when encountering
  # the first string, because that could then break any transactions in progress.
  # See: http://www.postgresql.org/docs/current/static/runtime-config-compatible.html
  # If PostgreSQL doesn't know the standard_conforming_strings parameter then it doesn't
  # support escape string syntax. Don't override the inherited quoted_string_prefix.
  if supports_standard_conforming_strings?
    self.class.instance_eval do
      define_method(:quoted_string_prefix) { 'E' }
    end
  end

  # Money type has a fixed precision of 10 in PostgreSQL 8.2 and below, and as of
  # PostgreSQL 8.3 it has a fixed precision of 19. PostgreSQLColumn.extract_precision
  # should know about this but can't detect it there, so deal with it here.
  money_precision = (postgresql_version >= 80300) ? 19 : 10
  PostgreSQLColumn.module_eval(<<-end_eval)
    def extract_precision(sql_type)  # def extract_precision(sql_type)
      if sql_type =~ /^money$/       #   if sql_type =~ /^money$/
        #{money_precision}           #     19
      else                           #   else
        super                        #     super
      end                            #   end
    end                              # end
  end_eval

  configure_connection
  @connection
end