/cod_daemon/lib/classes/DatabaseSQLite.php
PHP | 243 lines | 97 code | 29 blank | 117 comment | 7 complexity | 8c0e06fa69f2d268b537fa7d819f41c8 MD5 | raw file
- <?php defined('ROOT_PATH') or die('No direct script access.');
-
- /**
- * Database system
- *
- * Copyright (c) 2011, EpicLegion
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
- * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
- * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- * @author EpicLegion
- * @package cod_daemon
- * @subpackage core
- * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
- */
-
- class DatabaseSQLite
- {
- /**
- * Last instance
- *
- * @var Database
- */
- protected static $instance = NULL;
-
- /**
- * SQLite3
- *
- * @var SQLite3
- */
- protected $link = NULL;
-
- /**
- * Constructor
- *
- * @param array $config
- * @throws Exception
- */
- public function __construct(array $config = array())
- {
- // Empty config
- if (empty($config))
- {
- throw new Exception('Invalid database configuration. Please check your config/core/database.yml file.');
- }
-
- // Define as const
- if(!defined('PREFIX')) define('PREFIX', '');
-
- // Connect
- $this->link = new SQLite3(ROOT_PATH.'data/'.$config['filename']);
-
- // Success?
- if(!$this->link)
- {
- throw new Exception('Cannot connect to database');
- }
- }
-
- /**
- * Close connection
- */
- public function close()
- {
- $this->link->close();
- $this->link = NULL;
- self::$instance = NULL;
- }
-
- /**
- * Execute query and return affected rows
- *
- * @param string $statement
- * @param array $params
- * @return int
- */
- public function exec($statement, array $params = array())
- {
- // Prepare
- $statement = $this->prepareStatement($statement, $params);
-
- // Query
- $statement = $this->query($statement);
-
- // Return
- return $this->link->changes();
- }
-
- /**
- * Get last insert ID
- *
- * @return int
- */
- public function getLastInsertID()
- {
- return $this->link->lastInsertRowID();
- }
-
- /**
- * Get all possible rows
- *
- * @param string $statement
- * @param array $params
- * @param string $sortByField
- * @param array
- */
- public function getAll($statement, array $params = array(), $sortByField = NULL)
- {
- // Prepare
- $statement = $this->prepareStatement($statement, $params);
-
- // Query
- $statement = $this->query($statement);
-
- // Rows
- $rows = array();
-
- // Fetch all
- while($row = $statement->fetchArray(SQLITE3_ASSOC))
- {
- // Sort by field?
- if($sortByField AND isset($row[$sortByField]))
- {
- $rows[$row[$sortByField]] = $row;
- }
- else
- {
- $rows[] = $row;
- }
- }
-
- // Return
- return $rows;
- }
-
- /**
- * Get a single row
- *
- * @param string $statement
- * @param array $params
- * @param bool|array
- */
- public function getSingle($statement, array $params = array())
- {
- // Prepare
- $statement = $this->prepareStatement($statement, $params);
-
- // Query
- $statement = $this->query($statement);
-
- // Return
- return $statement->fetchArray(SQLITE3_ASSOC);
- }
-
- /**
- * Get/create db instance
- *
- * @param array $config
- * @return Database
- */
- public static function instance(array $config = array())
- {
- // Instance
- if (self::$instance === NULL)
- {
- self::$instance = new Database($config);
- }
-
- // Return
- return self::$instance;
- }
-
- /**
- * Prepare statement
- *
- * @param string $statement
- * @param array $params
- * @return string
- */
- protected function prepareStatement($statement, array $params = array())
- {
- // Prefix
- $statement = str_replace('[prefix]', '', $statement);
-
- // Parameters
- foreach($params as $k => $v)
- {
- // Prepare value
- if($v === NULL)
- {
- $v = 'NULL';
- }
- elseif(is_string($v))
- {
- $v = "'".SQLite3::escapeString($v)."'";
- }
- elseif(is_bool($v))
- {
- $v = $v ? 1 : 0;
- }
-
- // Replace
- $statement = str_replace($k, $v, $statement);
- }
-
- // Return
- return $statement;
- }
-
- /**
- * Execute query
- *
- * @param string $statement
- * @throws Exception
- * @return mixed
- */
- public function query($statement)
- {
- // Send query
- $statement = $this->link->query($statement);
-
- // Error?
- if (!$statement)
- {
- throw new Exception('SQLite error: ['.$this->link->lastErrorMsg().']');
- }
-
- // Return result
- return $statement;
- }
- }