Location: PHPKode > projects > web.framework > web.framework-1.0.0/web.framework/DB/Drivers/PostgreSQL/PostgreSQLDriver.class.php
<?php
/**
 * Project:     web.framework: the PHP5 MVC framework
 * File:		PostgreSQLDriver.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
 */

/**
 * PostgreSQL's web.db driver
 *
 * @name PostgreSQLDriver
 * @version 1.0.0
 * @package web.framework
 * @subpackage web.db
 *
 * @author Marcin Staniszczak
 * @copyright 2005 Marcin Staniszczak
 */
class PostgreSQLDriver 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);

		$strPort = (isset($arrSettings['port'])?'port='.$arrSettings['port']:'');
		$strConnection = "host={$arrSettings['hostname']} $strPort dbname={$arrSettings['db']} user={$arrSettings['userName']} password={$arrSettings['password']}";

		if ($blnPersistent) {
			$this->resConnection = @pg_pconnect($strConnection);
		} else {
			$this->resConnection = @pg_connect($strConnection);
		}

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

	/**
	 * Return database name
	 *
	 * @access public
	 * @return database name (np. MySQL, PostreSQL)
	 */
	public function serverName() {
		return 'PostgreSQL';
	}

	/**
	 * Disconnect
	 *
	 * @access public
	 */
	public function disconnect() {
		if (!$this->blnPersistent) {
			@pg_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.'PostgreSQL'.DIRECTORY_SEPARATOR.'PostgreSQLRecordSet.class.php');

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

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

		$this->resResult = $resResource;

		return new PostgreSQLRecordSet($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 = @pg_query($this->resConnection, 'BEGIN');
		if (!$resResult) {
			throw new WD_Query_Exception(Languages::$MESSAGES[$this->strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['BEGIN_TRANSACTION'], pg_last_error($this->resConnection));
		}
	}

	/**
	 * Commit transaction
	 *
	 * @access public
	 */
	public function Commit() {
		$resResult = @pg_query($this->resConnection, 'COMMIT');
		if (!$resResult) {
			throw new WD_Query_Exception(Languages::$MESSAGES[$this->strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['CAN_NOT_COMMIT'], pg_last_error($this->resConnection));
		}
	}

	/**
	 * Rollback transaction
	 *
	 * @access public
	 */
	public function Rollback() {
		$resResult = @pg_query($this->resConnection, 'ROLLBACK');
		if (!$resResult) {
			throw new WD_Query_Exception(Languages::$MESSAGES[$this->strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['COULD_NOT_ROLLBACK'], pg_last_error($this->resConnection));
		}
	}


	/**
	 * Return last generated ID
	 *
	 * @access public
	 * @param string table name
	 * @param string column name
	 * @return integer
	 * @todo config for any PostgreSQL version!
	 */
	public function getLastID($strTable='', $strColumn='') {
		$arrServerInfo = pg_version($this->resConnection);
		$arrPGVersion = explode('.', $arrServerInfo['server']);

		if ($arrPGVersion[0]<=7 && $arrPGVersion[1]<2) {
			echo 1;
			$intOID = pg_last_oid($this->resResult);

			return empty($strTable) || empty($strColumn) ? $intOID : $this->selectOne("SELECT $strColumn FROM $strTable WHERE oid=".(int)$intOID);
		}  elseif ($arrPGVersion[0]>=8) {
			$strTable = $this->quote($strTable);
			$strColumn = $this->quote($strColumn);

			return $this->selectOne("SELECT currval(pg_get_serial_sequence($strTable,$strColumn))");
		} else {
			$strTable = trim($this->quote($strTable), '\'');
			$strColumn = trim($this->quote($strColumn), '\'');

			return $this->selectOne("SELECT last_value FROM {$strTable}_{$strColumn}_seq");
		}
	}

	/**
	 * 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 = @pg_query($this->resConnection, $strQuery);

			if ($resResult === false) {
				return false;
			}

			$arrResult = pg_fetch_assoc($resResult);
			$this->resResult = $resResult;

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