Class: Sequel::Postgres::Adapter

Inherits:
PGconn
  • Object
show all
Includes:
AdapterMethods
Defined in:
lib/sequel/adapters/postgres.rb

Overview

PGconn subclass for connection specific methods used with the pg, postgres, or postgres-pr driver.

Constant Summary collapse

DISCONNECT_ERROR_RE =
/\Acould not receive data from server: Software caused connection abort/

Constants included from AdapterMethods

Sequel::Postgres::AdapterMethods::SELECT_CURRVAL, Sequel::Postgres::AdapterMethods::SELECT_CUSTOM_SEQUENCE, Sequel::Postgres::AdapterMethods::SELECT_PK, Sequel::Postgres::AdapterMethods::SELECT_SERIAL_SEQUENCE

Instance Attribute Summary collapse

Attributes included from AdapterMethods

#db, #transaction_depth

Instance Method Summary collapse

Methods included from AdapterMethods

#last_insert_id, #primary_key, #sequence

Instance Attribute Details

#prepared_statementsObject (readonly)

Hash of prepared statements for this connection. Keys are string names of the server side prepared statement, and values are SQL strings.



143
144
145
# File 'lib/sequel/adapters/postgres.rb', line 143

def prepared_statements
  @prepared_statements
end

Instance Method Details

#apply_connection_settingsObject

Apply connection settings for this connection. Current sets the date style to ISO in order make Date object creation in ruby faster, if Postgres.use_iso_date_format is true.



148
149
150
151
152
153
154
155
# File 'lib/sequel/adapters/postgres.rb', line 148

def apply_connection_settings
  super
  if Postgres.use_iso_date_format
    sql = "SET DateStyle = 'ISO'"
    execute(sql)
  end
  @prepared_statements = {} if SEQUEL_POSTGRES_USES_PG
end

#check_disconnect_errorsObject

Raise a Sequel::DatabaseDisconnectError if a PGError is raised and the connection status cannot be determined or it is not OK.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/sequel/adapters/postgres.rb', line 159

def check_disconnect_errors
  begin
    yield
  rescue PGError =>e
    disconnect = false
    begin
      s = status
    rescue PGError
      disconnect = true
    end
    status_ok = (s == Adapter::CONNECTION_OK)
    disconnect ||= !status_ok
    disconnect ||= e.message =~ DISCONNECT_ERROR_RE
    disconnect ? raise(Sequel.convert_exception_class(e, Sequel::DatabaseDisconnectError)) : raise
  ensure
    block if status_ok
  end
end

#execute(sql, args = nil) ⇒ Object

Execute the given SQL with this connection. If a block is given, yield the results, otherwise, return the number of changed rows.



180
181
182
183
184
185
186
187
# File 'lib/sequel/adapters/postgres.rb', line 180

def execute(sql, args=nil)
  q = check_disconnect_errors{@db.log_yield(sql, args){args ? async_exec(sql, args) : async_exec(sql)}}
  begin
    block_given? ? yield(q) : q.cmd_tuples
  ensure
    q.clear if q
  end
end