<?php
/**
* Project: web.framework: the PHP5 MVC framework
* File: MySQLDriver.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
*/
/**
* MySQL's web.db driver
*
* @name MySQLDriver
* @version 1.0.0
* @package web.framework
* @subpackage web.db
*
* @author Marcin Staniszczak
* @copyright 2005 Marcin Staniszczak
*/
class MySQLDriver 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);
if ($blnPersistent) {
$this->resConnection = @mysql_pconnect($arrSettings['hostname'].(isset($arrSettings['port'])?':'.$arrSettings['port']:''), $arrSettings['userName'], $arrSettings['password']);
} else {
$this->resConnection = @mysql_connect($arrSettings['hostname'].(isset($arrSettings['port'])?':'.$arrSettings['port']:''), $arrSettings['userName'], $arrSettings['password']);
}
if (!$this->resConnection) {
throw new WD_Connection_Exception(Languages::$MESSAGES[$strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['CONNECT_FAILED'], (strlen(@mysql_error())>0?mysql_error():null));
}
if (@mysql_select_db($arrSettings['db'], $this->resConnection) === false) {
switch(mysql_errno($this->resConnection)) {
case 1044:
throw new WD_Connection_Exception(Languages::$MESSAGES[$strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['SELECT_DB']['ACCESS_VIOLATION'], mysql_error($this->resConnection));
break;
case 1049:
throw new WD_Connection_Exception(Languages::$MESSAGES[$strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['SELECT_DB']['NO_SUCH_DATABASE'], mysql_error($this->resConnection));
break;
default:
throw new WD_Connection_Exception(Languages::$MESSAGES[$strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['SELECT_DB']['CANNOT_SELECT_DATABASE'], mysql_error($this->resConnection));
break;
}
}
}
/**
* Return database name
*
* @access public
* @return database name (for example MySQL, PostreSQL)
*/
public function serverName() {
return 'MySQL';
}
/**
* Disconnect
*
* @access public
*/
public function disconnect() {
if (!$this->blnPersistent) {
@mysql_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.'MySQL'.DIRECTORY_SEPARATOR.'MySQLRecordSet.class.php');
$resResource = @mysql_query($strQuery, $this->resConnection);
if ($resResource===false) {
throw new WD_Query_Exception(Languages::$MESSAGES[$this->strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['EXECUTE_QUERY'], mysql_error($this->resConnection));
}
return new MySQLRecordSet($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, $this->resConnection).'\'';
} else {
return '\''.$strValue.'\'';
}
}
/**
* Begin transaction
*
* @access public
*/
public function Begin() {
$resResult = @mysql_query('SET AUTOCOMMIT=0', $this->resConnection);
$resResult = @mysql_query('BEGIN', $this->resConnection);
if (!$resResult) {
throw new WD_Query_Exception(Languages::$MESSAGES[$this->strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['BEGIN_TRANSACTION'], mysql_error($this->resConnection));
}
}
/**
* Commit transaction
*
* @access public
*/
public function Commit() {
$resResult = @mysql_query('COMMIT', $this->resConnection);
$resResult = @mysql_query('SET AUTOCOMMIT=1', $this->resConnection);
if (!$resResult) {
throw new WD_Query_Exception(Languages::$MESSAGES[$this->strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['CAN_NOT_COMMIT'], mysql_error($this->resConnection));
}
}
/**
* Rollback transaction
*
* @access public
*/
public function Rollback() {
$resResult = @mysql_query('ROLLBACK', $this->resConnection);
$resResult = @mysql_query('SET AUTOCOMMIT=1', $this->resConnection);
if (!$resResult) {
throw new WD_Query_Exception(Languages::$MESSAGES[$this->strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['COULD_NOT_ROLLBACK'], mysql_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 (int)$this->selectOne('SELECT LAST_INSERT_ID()');
}
/**
* 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 = mysql_query($strQuery, $this->resConnection);
if ($resResult === false) {
return false;
}
$arrResult = mysql_fetch_array($resResult, MYSQL_ASSOC);
return reset($arrResult);
} else {
throw new WD_Query_Exception(Languages::$MESSAGES[$this->strLanguage]['EXCEPTIONS']['WEBDB']['DRIVERS']['EXECUTE_QUERY'], 'selectOne require <i>select</i> query');
}
}
}
?>