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

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

	/**
	 * 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->resConnection = @odbtp_connect($arrSettings['hostname'], "DRIVER={SQL Server};SERVER={$arrSettings['server']};UID={$arrSettings['userName']};PWD={$arrSettings['password']};DATABASE={$arrSettings['db']};");

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

		if ($arrSettings['row_cache']!==null) {
			$this->blnRowCachce = true;
		}
	}

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

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

		if ($this->blnRowCachce===true) {
			odbtp_use_row_cache();
		}

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

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

		return new ODBTPRecordSet($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);
			}

		}

		$strValue = str_replace("'", "''", str_replace("\0","\\\0", str_replace('\\','\\\\',$strValue)));

		return $strValue;
	}

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

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

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

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

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

		if (!$resResult) {
			throw new WD_Query_Exception(Languages::$MESSAGES[$this->strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['COULD_NOT_ROLLBACK'], odbtp_get_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='') {
		if (strcmp($strTable) > 0) {
			return (int)$this->selectOne("SELECT IDENT_CURRENT('$strTable')");
		} else {
			return (int)$this->selectOne("SELECT SCOPE_IDENTITY()");
		}
	}

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

			$arrResult = odbtp_fetch_assoc($resResult);
			return reset($arrResult);
		} 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