Location: PHPKode > scripts > Piragibe > piragibe/PGBConnection/PGBDbxConnection.php
<?php
require_once 'Piragibe/PGBConnection.php';
/**
 * A connection to a generic database, implemented as a wrapper to a DBX connection.
 * 
 * This is basically a DBX connection with some add-ons, like a transaction safe environment.
 * @package Piragibe
 * @subpackage PGBConnection
 * @author Francisco Piragibe
 * @version 1.00
 * @copyright Copyright &copy; 2006, Francisco Piragibe
 * This file is part of The PIRAGIBE Framework.
 *
 * The PIRAGIBE Framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * The PIRAGIBE Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with The PIRAGIBE Framework; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
class PGBDbxConnection extends PGBConnection {

    /**
     * @var mixed the DBX connection
     */
    var $m_conn;
    /**
     * @var mixed the underlying database (DBX_PGSQL, DBX_ORACLE, etc)
     */
    var $m_tipo;                // tipo da conexao (DBX_MYSQL, DBX_PGSQL, etc)

    /**
     * Standard constructor.
     * 
     * @param mixed $pTipo the DBX database driver to use (DBX_PGSQL, for instance)
     * @param string $pHost hostname or IP address of database server
     * @param string $pDb database name
     * @param string $pUser database username
     * @param string $pPw database password
     */
    function PGBDbxConnection( $pTipo, $pHost, $pDb, $pUser, $pPw ) {
        parent::PGBConnection( $pHost, $pDb, $pUser, $pPw );
        $this->m_tipo       = $pTipo;
    }
    
    /**
     * Getter for the DBX database type.
     * @return mixed the DBX database driver name
     */
    function getTipo() {
        return $this->m_tipo;
    }
    
    /**
     * Starts an ACID transaction against the database.
     */
    function beginTrans() {
    // inicia uma transacao atomica 
        $c   = $this->getConnection();
        if ( ! $this->isInTrans() ) {
            dbx_query( $c, "begin" );
        }
        parent::beginTrans();
    }

    /**
     * Commit a transaction.
     * 
     * In order to allow for a permanent transaction-safe status, starts another transaction
     * after commiting the current one.
     */
    function commit() {
        $c   = $this->getConnection();
        if ( $this->isInTrans() ) {
            dbx_query( $c, "commit" );
        }
        parent::commit();
    }

    /**
     * Rollback a transaction.
     * 
     * In order to allow for a permanent transaction-safe status, starts another transaction
     * after rolling back the current one.
     */
    function rollback() {
        $c   = $this->getConnection();
        if ( $this->isInTrans() ) {
            dbx_query( $c, "rollback" );
        }
        parent::rollback();
    }

    /**
     * Close the connection, rolling back any pending transactions before doing this.
     */
    function close() {
    // fecha a conexao, dando rollback na ultima transacao em curso
        $c   = $this->getConnection();
        parent::close();
        dbx_close( $c );
        $_SESSION['dbx_handle'] = null;
    }

    /**
     * Getter for the undelying DBX connection.
     * @return mixed a reference to the underlying DBX connection
     */
    function &getConnection() {
    // abre e retorna uma conexao
        $this->m_conn = dbx_connect( $this->m_tipo, $this->m_host, $this->m_dbname, $this->m_username, $this->m_pw );
        return $this->m_conn;
    }
    
    /**
     * Executes a SQL query through a DBX connection, returning the result without
     * any special formatting.
     * @return array the array returned by dbx_query
     * @param string $sql a SQL statement
     */
    function rawQuery( $sql ) {
    // executa uma query SQL, retornando o resultado "cru"
        $c   = $this->getConnection();
        //print '<pre>' . $sql . '</pre>';
        $r   = dbx_query( $c, $sql, DBX_RESULT_INFO | DBX_RESULT_ASSOC );
        return $r;
    }        

    /**
     * Executes a SQL query through the underlying DBX connection.
     * @return PGBDbxRecordSet a recordset built from the query
     * @param string $sql the SQL query
     * @param string $pXo the type of each record (defaults to {@link PGBDbxRecord}
     */
    function query( $sql, $pXo = 'PGBDbxRecord' ) {
    // executa uma query atraves da conexao, retornando um PGBDbxRecordSet com o resultado
        $r   = $this->rawQuery( $sql );
        $rs  = new PGBDbxRecordSet( $this, $pXo );
        $rs->setSql( $sql );
        if ( is_object( $r ) ) {
            $rs->initialize( $r );
            $rs->firstRecord();
        }
        return $rs;
    }
    
    /**
     * Executes a SQL query through the underlying DBX connection, storing the
     * resulting recordset in the session context.
     * 
     * This allow for recordset persistency through a session.
     * @return PGBDbxRecordSet a recordset built from the query
     * @param string $nome the query logical name (i.e. the name under which it'll be stored in the session context)
     * @param string $sql the SQL query
     * @param string $pXo the type of each record (defaults to {@link PGBDbxRecord}
     */
    function &namedQuery( $nome, $sql, $pXo = 'PGBDbxRecord' ) {
    // executa uma query, armazenando o resultado na sessao
        $rs = &parent::namedQuery( $nome, $sql, $pXo );
        return $rs;
    }
    
    /**
     * Executes a SQL query through the underlying DBX connection, storing the
     * resulting recordset in the session context.
     * 
     * If the recordset is already in the session context, return the recordset
     * without rebuilding it.
     * @return PGBDbxRecordSet a recordset built from the query
     * @param string $nome the query logical name (i.e. the name under which it'll be stored in the session context)
     * @param string $sql the SQL query
     * @param string $pXo the type of each record (defaults to {@link PGBDbxRecord}
     */
    function &getNamedRs( $nome, $sql = NULL, $pXo = 'PGBDbxRecord'  ) {
    // recupera uma query nomeada anteriormente executada
        $rs = &parent::getNamedRs( $nome, $sql, $pXo );
        return $rs;
    }
    
}
?>
Return current item: Piragibe