Class: DataObjects::Connection

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/data_objects/connection.rb

Overview

An abstract connection to a DataObjects resource. The physical connection may be broken and re-established from time to time.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log

Constructor Details

#initialize(_uri) ⇒ Connection

:nodoc:

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/data_objects/connection.rb', line 68

def initialize(_uri) # :nodoc:
  raise NotImplementedError
end

Class Method Details

.concrete_commandObject



90
91
92
93
# File 'lib/data_objects/connection.rb', line 90

def self.concrete_command
  constant_name = name&.split('::')
  @concrete_command ||= DataObjects.const_get(constant_name[-2]).const_get('Command')
end

.inherited(target) ⇒ Object

Ensure that all Connection subclasses handle pooling and logging uniformly. See also DataObjects::Pooling and DataObjects::Logger



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
# File 'lib/data_objects/connection.rb', line 30

def self.inherited(target)
  target.class_eval do
    # Allocate a Connection object from the pool, creating one if necessary. This method is active in Connection subclasses only.
    def self.new(*args)
      instance = allocate
      instance.send(:initialize, *args)
      instance
    end

    include Quoting
  end

  return unless (driver_module_name = target.name.split('::')[-2])

  driver_module = DataObjects.const_get(driver_module_name)
  driver_module.class_eval <<-EOS, __FILE__, __LINE__ + 1
      def self.logger
        @logger
      end

      def self.logger=(logger)
        @logger = logger
      end
  EOS

  driver_module.logger = DataObjects::Logger.new(nil, :off)
end

.new(uri_s) ⇒ Object

Make a connection to the database using the DataObjects::URI given. Note that the physical connection may be delayed until the first command is issued, so success here doesn’t necessarily mean you can connect.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/data_objects/connection.rb', line 9

def self.new(uri_s)
  uri = DataObjects::URI.parse(uri_s)

  driver   = uri.scheme
  conn_uri = uri

  # Exceptions to how a driver class is determined for a given URI
  driver_class = driver.capitalize

  clazz = DataObjects.const_get(driver_class)::Connection
  unless clazz.method_defined? :close
    clazz.class_eval do
      include Pooling
      alias_method :close, :release
    end
  end
  clazz.new(conn_uri)
end

Instance Method Details

#create_command(text) ⇒ Object

Create a Command object of the right subclass using the given text



77
78
79
# File 'lib/data_objects/connection.rb', line 77

def create_command(text)
  self.class.concrete_command.new(self, text)
end

#disposeObject

:nodoc:

Raises:

  • (NotImplementedError)


72
73
74
# File 'lib/data_objects/connection.rb', line 72

def dispose # :nodoc:
  raise NotImplementedError
end

#extensionObject



81
82
83
# File 'lib/data_objects/connection.rb', line 81

def extension
  driver_namespace.const_get('Extension').new(self)
end

#to_sObject

Show the URI for this connection, without the password the connection was setup with



64
65
66
# File 'lib/data_objects/connection.rb', line 64

def to_s
  @uri.to_s
end