The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Gantry::Utils::DB - Database wrapper fucntions, specfic to PostgreSQL

SYNOPSIS

  db_commit
    db_commit( $dbh );

  db_connect
    $dbh = db_connect( $db_type, $user, $pass, $server, $db, $commit );
    $dbh = db_connect( %config_hash );

  db_disconnect
    db_disconnect( $dbh );

  db_finish
    db_finish( $sth );

  db_lastseq
    $last_value = db_lastseq( $dbh, $sequence_name );

  db_next
    ( @values ) = db_next( $sth );

  db_nextvals
    $hash_reference = db_nextvals( $handle );

  db_query
    $sth = db_query( $dbh, $description, @sql_query );

  db_rollback
    db_rollback( $dbh );

  db_rowcount
    $rows = db_rowcount( $sth );

  db_run
    db_run( $dbh, $description, @sql_query );

DESCRIPTION

These functions wrap the common DBI calls to Databases with error checking.

FUNCTIONS

db_commit( $dbh )

Takes a database handle and commits all pending transactions if AutoCommit is not enabled, otherwise does nothing. Returns no value.

$dbh = db_connect( %config_hash )
$dbh = db_connect( $db_type, $user, $pass, $server, $db, $commit )

Creates a connection to the database specified by $db on host $server. It then returns a $dbh variable containing the connection. The hash has the values db_type, usr, pwd, db, srv, commit for the respective variables. Commit should be specified as the text 'on' or 'off', case does not matter. 'db_type' should be a valid DBI database type ( eg. 'Pg' for postgres. ).

db_disconnect( $dbh )

Takes a database handle and disconnects that connection to the database, it will also rollback any pending transactions that have not been commited with db_commit(). Returns no value.

db_finish( $sth )

Finishes a statement handle after a db_query() is completed. Returns nothing.

$last_value = db_lastseq( $dbh, $sequence_name )

Takes a database handle and the name of the sequence. It returns the last value that the sequence handed out. Usefully during transactions when the id of the last inserted SQL is needed. Will croak() if there is no database handle passed in or if no sequence is passed in. If no sequence is passed in, before croak()ing it will preform a rollback.

( @values ) = db_next( $sth )

Takes a statement handle and returns the next row as an array. The function will croak() if there is no statement handle passed in.

$hash_reference = db_nextvals( $sth )

This function takes a sql statement handle, $sth, and returns the next row from the statement as a hash reference with the column names as the keys and the values set from the row in the query.

$sth = db_query( $dbh, $description, @sql_query )

This function takes a database handler, $dbh, a description of the call, $description, and a sql query, @sql_query. The sql query can be either an array or a string, it will be joined with spaces if it is an array. The query is then run against the database specified in $dbh. The function will return a statment handler, $sth, or if there is an error while executing the sql query it will croak().

db_rollback( $dbh )

Takes a database handle and preforms a rollback on the handle. Returns nothing.

$rows = db_rowcount( $sth )

Takes a statement handle and returns an integer count of the number of rows affected in the statement handle ( ie. the number of rows in a select ).

db_run( $dbh, $description, @sql_query )

This function behaves identcally to db_query(), save it uses the DBI->do vs the DBI->execute method to run the sql query. This means this function will never return a statement handle.

METHODS

new

Not currently used, since there are no other methods to act on the object.

SEE ALSO

Gantry::Utils::SQL(3), DBI(3), DBD::Pg(3)

LIMITATIONS

This library is untested with databases other than Postgresql.

AUTHOR

Nicholas Studt <nstudt@angrydwarf.org>

COPYRIGHT and LICENSE

Copyright (c) 2005, Nicholas Studt.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.