Class: SqlPostgres::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/sqlpostgres/Connection.rb

Overview

This class holds a connection to the database.

Constant Summary collapse

@@pgClass =
PG
@@default =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Connection

Create a connection.

To create a new connection, pass zero or more of the following arguments They get passed through to PGconn.connect.

‘host_name’

The host to connect to. Defaults to ‘localhost’

‘port’

The port to connect to. Defaults to 5432

‘options’

Back end options. Defaults to ”

‘tty’

Name of TTY for back end messages. Defaults to ”

‘db_name’

Database name. ”, the default, means to use the database with the same name as the user.

‘login’

Login name. nil, the default, means to use the user’s name. to nil.

‘password’

Password. nil, the default, means no password.

‘encoding’

Client encoding.

To wrap an existing connection, pass this argument:

‘connection’

The PGConn instance to wrap.

The following arguments influence SqlPostgres’s behavior; they’re not actually used to establish the connection to postgres:

‘statement_in_exception’

If true, add the offending statement PGError exceptions. Defaults to true.

Raises:

  • (ArgumentError)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/sqlpostgres/Connection.rb', line 101

def initialize(args = {})
  raise ArgumentError, "Block not allowed" if block_given?
  @pgconn = args['connection']
  if @pgconn.nil?
    hostName = args['host_name'] || "localhost"
    dbName = args['db_name'] || ""
    port = args['port'] || 5432
    options = args['options'] || ""
    tty = args['tty'] || ""
     = args['login']
    password = args['password']
    client_encoding = args['encoding']
    @pgconn = @@pgClass.connect(hostName, port, options, tty, dbName, 
                                , password)
    if client_encoding
      @pgconn.set_client_encoding(client_encoding)
    end
  end
  @statement_in_exception = args['statement_in_exception']
  @statement_in_exception = true if @statement_in_exception.nil?
end

Instance Attribute Details

#pgconnObject (readonly)

The underlying instance of PGconn. This is for when PGconn does something that this library doesn’t do.



17
18
19
# File 'lib/sqlpostgres/Connection.rb', line 17

def pgconn
  @pgconn
end

#statement_in_exceptionObject

If true, then PGError exceptions have the offending statement added to them.



12
13
14
# File 'lib/sqlpostgres/Connection.rb', line 12

def statement_in_exception
  @statement_in_exception
end

Class Method Details

.defaultObject

Get the default connection. If there isn’t one, returns the NullConnection instead.



25
26
27
28
29
30
31
# File 'lib/sqlpostgres/Connection.rb', line 25

def Connection.default
  if @@default.nil?
    NullConnection.new
  else
    @@default
  end
end

.default=(value) ⇒ Object

Set the default connection.



35
36
37
# File 'lib/sqlpostgres/Connection.rb', line 35

def Connection.default=(value)
  @@default = value
end

.open(*args) ⇒ Object

Open a connection, pass it to the block, then close it. The connection is closed even if an exception occurs. Returns the result of the block.

Takes the same arguments as new.

Note: If an exception occurs, then any exception from closing the connection is ignored. This is to avoid masking the original (and presumably more important) exception.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sqlpostgres/Connection.rb', line 49

def Connection.open(*args)
  connection = Connection.new(*args)
  begin
    result = yield(connection)
  rescue
    begin
      connection.close
    rescue
    end
    raise
  else
    connection.close
  end
  result
end

Instance Method Details

#closeObject

close the connection. If it’s already closed, do nothing.



125
126
127
128
129
# File 'lib/sqlpostgres/Connection.rb', line 125

def close
  return if @pgconn.nil?
  @pgconn.close
  @pgconn = nil
end

#exec(statement) ⇒ Object

Send an SQL statement to the backend. Returns a PGResult. This is just a thin wrapper around PGConn.exec, and exists for when you want to do some SQL that Insert, Update, Select, or Transaction won’t do for you (ie, “create temporary table”).

If a PGError exception occurs and statement_in_exception is true, then statement is added to the exception.



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/sqlpostgres/Connection.rb', line 139

def exec(statement)
  begin
    @pgconn.exec(statement)
  rescue PGError => e
    if statement_in_exception
      e = e.exception(e.message + 
                      "The offending statement is: #{statement.inspect}")
    end
    raise e
  end
end

#exec_and_translate(statement, columns) ⇒ Object

This is a hook for rspec (mock this method to find out what sql is being executed, and to inject translated results).



164
165
166
# File 'lib/sqlpostgres/Connection.rb', line 164

def exec_and_translate(statement, columns)
  translate_pgresult(exec(statement), columns)
end

#query(statement) ⇒ Object

Send an SQL statement to the backend. Returns an array of arrays.

If a PGError exception occurs and statement_in_exception is true, then statement is added to the exception.



156
157
158
159
# File 'lib/sqlpostgres/Connection.rb', line 156

def query(statement)
  result = exec(statement)
  result.send(result_method(result))
end