Location: PHPKode > projects > Moc10 PHP Library > library/Moc10/Db/SQLite.php
<?php
/**
 * Moc10 Library
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.TXT.
 * It is also available through the world-wide-web at this URL:
 * http://www.moc10phplibrary.com/LICENSE.TXT
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to hide@address.com so we can send you a copy immediately.
 *
 * @category   Moc10
 * @package    Moc10_Db
 * @author     Nick Sagona, III <hide@address.com>
 * @copyright  Copyright (c) 2009-2011 Moc 10 Media, LLC. (http://www.moc10media.com)
 * @license    http://www.moc10phplibrary.com/LICENSE.TXT     New BSD License
 */

/**
 * Moc10_Db_SQLite
 *
 * @category   Moc10
 * @package    Moc10_Db
 * @author     Nick Sagona, III <hide@address.com>
 * @copyright  Copyright (c) 2009-2011 Moc 10 Media, LLC. (http://www.moc10media.com)
 * @license    http://www.moc10phplibrary.com/LICENSE.TXT     New BSD License
 * @version    1.9.7
 */

class Moc10_Db_SQLite
{

    /**
     * Database results
     * @var resource
     */
    public $result;

    /**
     * Default database connection
     * @var resource
     */
    public $connection;

    /**
     * Last result
     * @var resource
     */
    public $last_result;

    /**
     * Last SQL query
     * @var string
     */
    public $last_sql = null;

    /**
     * Database tables
     * @var array
     */
    protected $_tables = array();

    /**
     * Language object
     * @var Moc10_Language
     */
    protected $_lang = null;

    /**
     * Constructor
     *
     * Instantiate the SQLite database connection object.
     *
     * @param  string $dbase
     * @throws Exception
     * @return void
     */
    public function __construct($dbase)
    {

        $this->_lang = new Moc10_Language();

        // Select the DB to use, or display the SQL error.
        if (is_null($dbase)) {
            throw new Exception($this->_lang->__('Error: The database file was not passed.'));
        } else if (!file_exists($dbase)) {
            throw new Exception($this->_lang->__('Error: The database file does not exists.'));
        } else {
            $this->connection = new SQLite3($dbase);
        }

    }

    /**
     * Throw an exception upon a database error.
     *
     * @throws Exception
     * @return void
     */
    public function showError()
    {

        throw new Exception($this->_lang->__('Error:') . ' ' . $this->connection->lastErrorCode() . ' => ' . $this->connection->lastErrorMsg() . '.');

    }

    /**
     * Execute the SQL query and create a result resource, or display the SQL error.
     *
     * @param  string $sql
     * @return void
     */
    public function query($sql)
    {

        if (stripos($sql, 'select') !== false) {
            $this->last_sql = $sql;
        } else {
            $this->last_sql = null;
        }

        if (!($this->result = $this->connection->query($sql))) {
            $this->showError();
        }

    }

    /**
     * Return the results array from the results resource.
     *
     * @throws Exception
     * @return array
     */
    public function fetch()
    {

        if (!isset($this->result)) {
            throw new Exception($this->_lang->__('Error: The database result resource is not currently set.'));
        } else {
            return $this->result->fetcharray(SQLITE3_ASSOC);
        }

    }

    /**
     * Return the escapes string value.
     *
     * @param  string $value
     * @return string
     */
    public function escape($value)
    {

        return $this->connection->escapeString($value);

    }

    /**
     * Return the auto-increment ID of the last query.
     *
     * @return int
     */
    public function lastId()
    {

        return $this->connection->lastInsertRowID();

    }

    /**
     * Return the number of rows in the result.
     *
     * @return int
     */
    public function numRows()
    {

        if (is_null($this->last_sql)) {

            return $this->connection->changes();

        } else {

            if (!($this->last_result = $this->connection->query($this->last_sql))) {
                $this->showError();
            } else {
                $num = 0;
                while (($row = $this->last_result->fetcharray(SQLITE3_ASSOC)) != false) {
                    $num++;
                }
                return $num;
            }

        }

    }

    /**
     * Return the number of fields in the result.
     *
     * @throws Exception
     * @return int
     */
    public function numFields()
    {

        if (!isset($this->result)) {
            throw new Exception($this->_lang->__('Error: The database result resource is not currently set.'));
        } else {
            return $this->result->numColumns();
        }

    }

    /**
     * Get tables of the database.
     *
     * @return array
     */
    public function getTables()
    {

        $this->_tables = array();

        $sql = "SELECT name FROM sqlite_master WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%' UNION ALL SELECT name FROM sqlite_temp_master WHERE type IN ('table', 'view') ORDER BY 1";

        $this->query($sql);
        while (($row = $this->fetch()) != false) {
            $this->_tables[] = $row['name'];
        }

        return $this->_tables;

    }

    /**
     * Return the SQLite version.
     *
     * @return string
     */
    public function version()
    {

        $ver = $this->connection->version();
        return 'SQLite ' . $ver['versionString'];

    }

    /**
     * Close the DB connection.
     *
     * @return void
     */
    public function __destruct()
    {

        $this->connection->close();

    }

}
Return current item: Moc10 PHP Library