class RDBI::Driver::PostgreSQL::Database

Attributes

pg_conn[RW]

Public Class Methods

new( *args ) click to toggle source
Calls superclass method
# File lib/rdbi/driver/postgresql.rb, line 17
def initialize( *args )
  super( *args )
  self.database_name = @connect_args[:dbname] || @connect_args[:database] || @connect_args[:db]
  @pg_conn = PG::Connection.new(
    @connect_args[:host] || @connect_args[:hostname],
    @connect_args[:port],
    @connect_args[:options],
    @connect_args[:tty],
    self.database_name,
    @connect_args[:user] || @connect_args[:username],
    @connect_args[:password] || @connect_args[:auth]
  )

  @preprocess_quoter = proc do |x, named, indexed|
    quote(named[x] || indexed[x])
  end
end

Public Instance Methods

commit() click to toggle source
Calls superclass method
# File lib/rdbi/driver/postgresql.rb, line 55
def commit
  if ! in_transaction?
    raise RDBI::TransactionError.new( "Cannot commit when not in a transaction" )
  end
  execute('COMMIT').finish
  super
end
disconnect() click to toggle source
Calls superclass method
# File lib/rdbi/driver/postgresql.rb, line 35
def disconnect
  super
  @pg_conn.close
end
new_statement( query ) click to toggle source
# File lib/rdbi/driver/postgresql.rb, line 63
def new_statement( query )
  Statement.new( query, self )
end
ping() click to toggle source
# File lib/rdbi/driver/postgresql.rb, line 127
def ping
  start = Time.now
  rows = begin
           execute("SELECT 1").result_count
         rescue PG::Error => e
           # XXX Sorry this sucks. PG::Connection is completely useless without a
           # connection... like asking it if it's connected.
           raise RDBI::DisconnectedError.new(e.message)
         end

  stop = Time.now

  if rows > 0
    stop.to_i - start.to_i
  else
    raise RDBI::DisconnectedError, "disconnected during ping"
  end
end
quote(item) click to toggle source
# File lib/rdbi/driver/postgresql.rb, line 146
def quote(item)
  case item
  when Numeric
    item.to_s
  when TrueClass
    'true'
  when FalseClass
    'false'
  when NilClass
    'NULL'
  else
    "E'#{@pg_conn.escape_string(item.to_s)}'"
  end
end
rollback() click to toggle source
Calls superclass method
# File lib/rdbi/driver/postgresql.rb, line 48
def rollback
  if ! in_transaction?
    raise RDBI::TransactionError.new( "Cannot rollback when not in a transaction" )
  end
  execute('ROLLBACK').finish
  super
end
schema( pg_schema = 'public' ) click to toggle source
# File lib/rdbi/driver/postgresql.rb, line 115
def schema( pg_schema = 'public' )
  schemata = []
  execute(%Q[
    SELECT table_name
    FROM information_schema.tables
    WHERE table_schema = '#{pg_schema}'
  ]).fetch( :all ).each do |row|
    schemata << table_schema( row[0], pg_schema )
  end
  schemata
end
table_schema( table_name, pg_schema = 'public' ) click to toggle source
# File lib/rdbi/driver/postgresql.rb, line 67
def table_schema( table_name, pg_schema = 'public' )
  info_row = execute(
    "SELECT table_type FROM information_schema.tables WHERE table_schema = ? AND table_name = ?",
    pg_schema,
    table_name
  ).fetch( :first ) rescue nil
  if info_row.nil?
    return nil
  end

  sch = RDBI::Schema.new( [], [] )
  sch.tables << table_name.to_sym

  case info_row[ 0 ]
  when 'BASE TABLE'
    sch.type = :table
  when 'VIEW'
    sch.type = :view
  else
    sch.type = :table
  end

  execute( %q[
      SELECT c.column_name, c.data_type, c.is_nullable, tc.constraint_type
      FROM information_schema.columns c
        LEFT JOIN information_schema.key_column_usage kcu
          ON kcu.column_name = c.column_name
          AND kcu.table_name = c.table_name
          LEFT JOIN information_schema.table_constraints tc
            ON tc.constraint_name = kcu.constraint_name
      WHERE c.table_schema = ? and c.table_name = ?
    ],
    pg_schema,
    table_name
  ).fetch( :all ).each do |row|
    col = RDBI::Column.new
    col.name       = row[0].to_sym
    col.type       = row[1].to_sym
    # TODO: ensure this ruby_type is solid, especially re: dates and times
    col.ruby_type  = row[1].to_sym
    col.nullable   = row[2] == "YES"
    col.primary_key = row[3] == "PRIMARY KEY"
    sch.columns << col
  end

  sch
end
transaction( &block ) click to toggle source
Calls superclass method
# File lib/rdbi/driver/postgresql.rb, line 40
def transaction( &block )
  if in_transaction?
    raise RDBI::TransactionError.new( "Already in transaction (not supported by PostgreSQL)" )
  end
  execute('BEGIN').finish
  super(&block)
end