Momomoto Connection class

Methods
Included Modules
Public Class methods
config( conf )

Eases the use of config.

# File lib/momomoto/database.rb, line 40
    def self.config( conf )
      instance.config( conf )
    end
connect()

Eases the use of connect.

  Momomoto::Database.config( :database=>:test, :username => 'test' )
  Momomoto::Database.connect
  # configure and connect
# File lib/momomoto/database.rb, line 69
    def self.connect
      instance.connect
    end
escape_bytea( input )

escapes the given binary data input

# File lib/momomoto/database.rb, line 196
    def self.escape_bytea( input )
      PGconn.escape_bytea( input )
    end
escape_string( input )

escapes the given string input

# File lib/momomoto/database.rb, line 191
    def self.escape_string( input )
      PGconn.escape( input )
    end
Public Instance methods
begin()
# File lib/momomoto/database.rb, line 157
    def begin
      execute( "BEGIN;" )
      @transaction_active = true
    end
commit()

commit the current transaction

# File lib/momomoto/database.rb, line 177
    def commit
      raise Error if not @transaction_active
      execute( "COMMIT;" )
      @transaction_active = false
    end
config( config )

establish connection to the database expects a hash with the following keys: host, port, database, username, password, pgoptions and pgtty

# File lib/momomoto/database.rb, line 29
    def config( config )
      config ||= {}
      # we also accept String keys in the config hash
      config.each do | key, value |
        config[key.to_sym] = value unless key.kind_of?( Symbol )
        config[key.to_sym] = value.to_s if value.kind_of?(Symbol)
      end
      @config = config
    end
connect()

Connects to database

  Momomoto::Database.config( :database=>:test, :username => 'test' )
  Momomoto::Database.connect
  # configure and connect
# File lib/momomoto/database.rb, line 53
    def connect
      @connection.close if @connection
      @transaction_active = false
      PGconn.translate_results = true
      @connection = PGconn.connect( @config[:host], @config[:port], @config[:pgoptions],
                      @config[:pgtty], @config[:database], @config[:username],
                      @config[:password])
    rescue => e
      raise CriticalError, "Connection to database failed: #{e}"
    end
disconnect()

terminate this connection

# File lib/momomoto/database.rb, line 74
    def disconnect
      @connection.close
      @connection = nil
      @transaction_active = false
    end
rollback()

roll the transaction back

# File lib/momomoto/database.rb, line 184
    def rollback
      raise Error if not @transaction_active
      execute( "ROLLBACK;" )
      @transaction_active = false
    end
transaction() {|| ...}

executes the block and commits the transaction if a block is given otherwise simply starts a new transaction

# File lib/momomoto/database.rb, line 164
    def transaction
      raise Error, "Transaction active" if @transaction_active
      self.begin
      begin
        yield
      rescue => e
        rollback
        raise e
      end
      commit
    end