Location: PHPKode > projects > web.framework > web.framework-1.0.0/web.framework/DB/Drivers/SQLite/SQLiteDriver.class.php
<?php
/**
 * Project:     web.framework: the PHP5 MVC framework
 * File:		SQLIteDriver.class.php
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * You may contact the authors of web.framework by e-mail at:
 * hide@address.com
 *
 * The latest version of web.framework can be obtained from:
 * http://sourceforge.net/projects/webframework
 *
 * @link http://sourceforge.net/projects/webframework
 * @copyright 2005 Marcin Staniszczak
 * @author Marcin Staniszczak <hide@address.com>
 * @version 1.0.0
 */

/**
 * SQLite's web.db driver
 *
 * @name SQLiteDriver
 * @version 1.0.0
 * @package web.framework
 * @subpackage web.db
 *
 * @author Marcin Staniszczak
 * @copyright 2005 Marcin Staniszczak
 */
class SQLiteDriver extends AWebDBDriver {
	private
		$resConnection = null;

	/**
	 * The class constructor (initializing DB connection)
	 *
	 * @access public
	 * @param string messages language
	 * @param array array with connection settings -> array('hostname'=>hostname, 'db'=>db name, 'port'=>port, 'userName'=>db user name, 'password'=>db user password)
	 * @param boolean for presistant connection set true, otherwise set false
	 * @throws WD_Connection_Exception
	 */
	public function __construct($strLanguage, $arrSettings, $blnPersistent) {
		//parent::__construct($strLanguage, $arrSettings, $blnPersistent);
		$this->strLanguage = $strLanguage;
		$this->blnPersistent = $blnPersistent;

		if (!isset($arrSettings['filename']) || !isset($arrSettings['mode'])) {
			throw new WD_Connection_Exception(WDLanguages::$MESSAGES[$strLanguages]['EXCEPTIONS']['WEBDB']['DRIVERS']['CONNECT_PARAMETERS']);
		}

		$strErrorMessage = null;

		if ($blnPersistent) {
			$this->resConnection = @sqlite_popen($arrSettings['filename'], $arrSettings['mode'], $strErrorMessage);
		} else {
			$this->resConnection = @sqlite_open($arrSettings['filename'], $arrSettings['mode'], $strErrorMessage);
		}

		if (!$this->resConnection) {
			throw new WD_Connection_Exception(WDLanguages::$MESSAGES[$strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['CONNECT_FAILED'], ($strErrorMessage?$strErrorMessage:null));
		}
	}


	/**
	 * Return database name
	 *
	 * @access public
	 * @return database name (for example MySQL, PostreSQL)
	 */
	public function serverName() {
		return 'SQLite';
	}

	/**
	 * Disconnect
	 *
	 * @access public
	 */
	public function disconnect() {
		if (!$this->blnPersistent) {
			@sqlite_close($this->resConnection);
		}
	}

	/**
	 * Execute SQL query
	 *
	 * @access public
	 * @param string SQL query
	 * @return AWebDBRecordSet's object
	 * @throws WD_Query_Exception
	 */
	public function query($strQuery) {
		require_once(WEBDB_DRIVERS_DIR.'SQLite'.DIRECTORY_SEPARATOR.'SQLiteRecordSet.class.php');

		$resResource = @sqlite_query($this->resConnection, $strQuery);

		if ($resResource===false) {
			throw new WD_Query_Exception(WDLanguages::$MESSAGES[$this->strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['EXECUTE_QUERY'], sqlite_error_string(sqlite_last_error($this->resConnection)));
		}

		$this->resResult = $resResource;

		return new SQLiteRecordSet($this->resConnection, $resResource);
	}

	/**
	 * Quote SQL query string
	 *
	 * @access public
	 * @param string query string to quote
	 * @return string quoted string
	 */
	public function quote($strValue, $blnGPC=true) {
		$blnGPCState = get_magic_quotes_gpc();
		if (!$blnGPCState || !$blnGPC) {
			if ($blnGPCState) {
				$strValue = stripslashes($strValue);
			}

			return '\''hide@address.com($strValue).'\'';
		} else {
			return '\''.$strValue.'\'';
		}
	}

	/**
	 * Begin transaction
	 *
	 * @access public
	 */
	public function Begin() {
		$resResult = @sqlite_query($this->resConnection, 'BEGIN');

		if (!$resResult) {
			throw new WD_Query_Exception(WDLanguages::$MESSAGES[$this->strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['BEGIN_TRANSACTION'], sqlite_last_error($this->resConnection));
		}
}

	/**
	 * Commit transaction
	 *
	 * @access public
	 */
	public function Commit() {
		$resResult = @sqlite_query($this->resConnection, 'COMMIT');

		if (!$resResult) {
			throw new WD_Query_Exception(WDLanguages::$MESSAGES[$this->strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['CAN_NOT_COMMIT'], sqlite_last_error($this->resConnection));
		}
	}

	/**
	 * Rollback transaction
	 *
	 * @access public
	 */
	public function Rollback() {
		$resResult = @sqlite_query($this->resConnection, 'ROLLBACK');

		if (!$resResult) {
			throw new WD_Query_Exception(WDLanguages::$MESSAGES[$this->strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['COULD_NOT_ROLLBACK'], sqlite_last_error($this->resConnection));
		}
	}


	/**
	 * Return last generated ID
	 *
	 * @access public
	 * @param string table name
	 * @param string column name
	 * @return integer last generated ID
	 */
	public function getLastID($strTable='', $strColumn='') {
		return sqlite_last_insert_rowid($this->resConnection);
	}

	/**
	 * Return one record from SQL query
	 *
	 * @access public
	 * @param string SQL query
	 * @return array one record returned from SQL query
	 */
	public function selectOne($strQuery) {
		if (strncasecmp($strQuery, 'select', 6) == 0) {
			$resResult = @sqlite_query($this->resConnection, $strQuery);
			$arrResult = sqlite_fetch_aray($resResult, SQLITE_ASSOC);
			$this->resResult = $resResult;

			return reset($arrResult);
		} else {
			throw new WD_Query_Exception(WDLanguages::$MESSAGES[$this->strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['EXECUTE_QUERY'], 'selectOne require <i>select</i> query');
		}
	}
}
?>
Return current item: web.framework