Code Search for Developers
 
 
  

Abstract.php from Astrum Futura at Krugle


Show Abstract.php syntax highlighted

<?php
/**
 * @internal
 * Quantum Game Library
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the text file LICENSE located in the root
 * directory of this library.
 * It is also available through the internet at this URL:
 * http://doc.astrumfutura.com/license.html
 *
 * If you did not receive a copy of the license and are unable to
 * obtain it through the internet, please send an email
 * to license@astrumfutura.com so we can send you a copy.
 *
 * @package    Db
 * @subpackage Driver
 * @category   Quantum
 * @copyright  Copyright (c) 2007 The QGL Group (refer to COPYRIGHT file)
 * @version    $Id: Mysql.php 243 2007-02-15 09:41:19Z maugrim_t_r $
 * @license    http://doc.astrumfutura.com/license.html     New BSD License
 */

/** Quantum_Db_Driver_Interface */
require_once 'Quantum/Db/Driver/Interface.php';

/** Quantum_Db_Result_Pdo_Mysql */
//require_once 'Quantum/Db/Result/Pdo/Mysql.php'; - using ArrayIterator for now.

/**
 * @package    Db
 * @subpackage Driver
 * @category   Quantum
 * @author     Pádraic Brady (http://blog.astrumfutura.com)
 *
 * Abstract access to the pdo extension. The abstraction in general
 * deliberately follows the PDO interface where possible for common methods.
 * subclasses hold specific methods which are implemented depending on the
 * RDBMS type.
 */
class Quantum_Db_Driver_Pdo_Abstract implements Quantum_Db_Driver_Interface
{

    /**
     * Holds the current PDO connection object.
     *
     * @var PDO
     */
    protected $_connection = null;

    /**
     * Holds the current PDOStatement object.
     *
     * @var PDOStatement
     */
    protected $_statement = null;

    /**
     * Holds the current Result iterator object.
     *
     * @var ArrayIterator
     */
    protected $_result = null;

    /**
     * Holds the last SQL string prepared.
     *
     * @var string
     */
    protected $_lastPreparedStatement = '';

    /**
     * Constructor - eventually callable from a Factory.
     * Constructs a PDO connection based on the parameters.
     *
     * @param string $dsn
     * @param string $userName
     * @param string $password
     * @return void
     */
    public function __construct(array $config)
    {
        /*
         * Construct a valid PDO DSN string
         */
        $dsn = $this->_pdoType . ':host=' . $config['host'];
        if(!empty($config['port'])) $dsn .= ';port=' . $config['port'];
        $dsn .= ';dbname=' . $config['dbname'];

        /*
         * Attempt to make a connection.
         */
        try
        {
            $this->_connection = new PDO($dsn, $config['username'], $config['password']);
            /*
             * Force lower case as standard.
             */
            $this->_connection->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
            /*
             * Errors throw Exceptions
             */
            $this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch (Exception $e)
        {
            throw new Quantum_Db_Driver_Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * Prepare an SQL string for a query (SELECT) and return an ArrayIterator
     * based on the resultset returned from calling PDOStatement::execute().
     *
     * @param string $sql
     * @param array $fields
     * @return ArrayIterator
     * @todo Figure out the most efficient return type.
     */
    public function query($sql, $fields = null)
    {
        if(isset($fields) && !is_array($fields))
        {
            throw new Quantum_Db_Driver_Exception('$fields parameter is not an array');
        }
        try
        {
            /*
             * Store the SQL; if this particular SQL string was already
             * prepared, there's no point calling prepare() again.
             */
            if($this->_lastPreparedStatement != $sql)
            {
                $this->_lastPreparedStatement = $sql;
                $this->_statement = $this->_connection->prepare($sql);
            }
            if(strpos(trim(strtolower($sql)), 'select') === 0)
            {
                return $this->_getResult($fields);
            }
            return $this->_exec($fields);
        }
        catch (Exception $e)
        {
            throw new Quantum_Db_Driver_Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * Execute an SQL statement which returns a PDOStatement.
     *
     * @return bool
     */
    protected function _getResult($fields = null)
    {
        $this->_statement->execute($fields);
        $this->_statement->setFetchMode(PDO::FETCH_ASSOC);
        /*
         * Return a buffered ArrayIterator with all records.
         * fetchAll() is flexible, at the cost of a large rowset.
         * Reasoning is MySQL's lack of unbuffered parallel query support
         * and also to keep the returned result simple.
         */
        if($this->_statement->columnCount() == 0)
        {
            return false;
        }
        $this->_result = new ArrayIterator( $this->_statement->fetchAll() );
        return $this->_result;
    }

    /**
     * Execute an SQL statement which does not require a returned PDOStatement.
     *
     * @return bool
     */
    protected function _exec($fields = null)
    {
        return $this->_statement->execute($fields);
    }

    /**
     * Quote and escape the passed value for inclusion in an SQL statement.
     *
     * @param mixed $value
     * @return mixed
     */
    public function quote($value)
    {
        return $this->_connection->quote($value);
    }

    /**
     * Return the last autoincrement or Sequential value from an INSERT.
     * The optional parameter is for PostgreSQL sequences.
     *
     * @param mixed $sequence
     * @return int
     */
    public function lastInsertId($sequence = null)
    {
        return $this->_connection->lastInsertId();
    }

    /**
     * Begin a transaction.
     *
     * @return void
     */
    public function beginTransaction()
    {
        $this->_connection->beginTransaction();
    }

    /**
     * Commit a transaction.
     *
     * @return void
     */
    public function commit()
    {
        $this->_connection->commit();
    }

    /**
     * Roll back a transaction.
     *
     * @return void
     */
    public function rollBack()
    {
        $this->_connection->rollBack();
    }

    /**
     * Close the current connection; just set the PDO variable to NULL.
     *
     * @return void
     */
    public function close()
    {
        $this->_connection = null;
    }

}



See more files for this project here

Astrum Futura

Multiplayer space strategy game written in PHP5 with the Zend Framework. User interface uses Javascript/AJAX for dynamic interaction. Players compete across a hexagonal map of 10,000 sectors, planets, stars and other locations through trade and combat.

Project homepage: http://sourceforge.net/projects/astrumfutura
Programming language(s): PHP,XML
License: other

  Abstract.php
  Mysql.php