Revision 2127
Added by darkviper over 10 years ago
| WbDatabase.php | ||
|---|---|---|
| 21 | 21 |
* @category Core |
| 22 | 22 |
* @package Core_database |
| 23 | 23 |
* @author Manuela v.d.Decken <manuela@isteam.de> |
| 24 |
* @author Dietmar W. <dietmar.woellbrink@websitebaker.org> |
|
| 25 | 24 |
* @copyright Manuela v.d.Decken <manuela@isteam.de> |
| 26 | 25 |
* @license http://www.gnu.org/licenses/gpl.html GPL License |
| 27 |
* @version 0.0.9
|
|
| 26 |
* @version 0.1.1
|
|
| 28 | 27 |
* @revision $Revision$ |
| 29 | 28 |
* @lastmodified $Date$ |
| 30 |
* @deprecated from WB version number 2.9 |
|
| 31 |
* @description Mysql database wrapper for use with websitebaker up to version 2.8.4 |
|
| 29 |
* @description Mysqli database wrapper for use with websitebaker version 2.8.4 |
|
| 32 | 30 |
*/ |
| 33 | 31 |
|
| 34 | 32 |
/* -------------------------------------------------------- */ |
| 35 | 33 |
@define('DATABASE_CLASS_LOADED', true);
|
| 36 |
/* define the old mysql consts for Backward compatibility */ |
|
| 37 |
if (!defined('MYSQL_ASSOC'))
|
|
| 38 |
{
|
|
| 39 |
define('MYSQL_SEEK_LAST', -1);
|
|
| 40 |
define('MYSQL_SEEK_FIRST', 0);
|
|
| 41 |
define('MYSQL_ASSOC', 1);
|
|
| 42 |
define('MYSQL_NUM', 2);
|
|
| 43 |
define('MYSQL_BOTH', 3);
|
|
| 44 |
define('MYSQL_CLIENT_COMPRESS', 32);
|
|
| 45 |
define('MYSQL_CLIENT_IGNORE_SPACE', 256);
|
|
| 46 |
define('MYSQL_CLIENT_INTERACTIVE', 1024);
|
|
| 47 |
define('MYSQL_CLIENT_SSL', 2048);
|
|
| 48 |
} |
|
| 49 | 34 |
|
| 35 |
define('MYSQLI_SEEK_LAST', -1);
|
|
| 36 |
define('MYSQLI_SEEK_FIRST', 0);
|
|
| 37 |
/* define the old mysql consts for Backward compatibility */ |
|
| 38 |
if (!defined('MYSQL_ASSOC'))
|
|
| 39 |
{
|
|
| 40 |
define('MYSQL_SEEK_LAST', -1);
|
|
| 41 |
define('MYSQL_SEEK_FIRST', 0);
|
|
| 42 |
define('MYSQL_ASSOC', 1);
|
|
| 43 |
define('MYSQL_NUM', 2);
|
|
| 44 |
define('MYSQL_BOTH', 3);
|
|
| 45 |
define('MYSQL_CLIENT_COMPRESS', 32);
|
|
| 46 |
define('MYSQL_CLIENT_IGNORE_SPACE', 256);
|
|
| 47 |
define('MYSQL_CLIENT_INTERACTIVE', 1024);
|
|
| 48 |
define('MYSQL_CLIENT_SSL', 2048);
|
|
| 49 |
} |
|
| 50 |
|
|
| 50 | 51 |
class WbDatabase extends WbDatabaseHelper {
|
| 51 | 52 |
|
| 52 |
private static $_oInstances = array();
|
|
| 53 |
private static $_oInstances = array();
|
|
| 53 | 54 |
|
| 54 |
protected $oDbHandle = null; // readonly from outside
|
|
| 55 |
protected $sDbName = '';
|
|
| 56 |
protected $sInstanceIdentifier = '';
|
|
| 57 |
protected $sTablePrefix = '';
|
|
| 58 |
protected $sCharset = '';
|
|
| 59 |
protected $connected = false;
|
|
| 60 |
protected $error = '';
|
|
| 61 |
protected $error_type = '';
|
|
| 62 |
protected $iQueryCount = 0;
|
|
| 55 |
protected $oDbHandle = null; // readonly from outside
|
|
| 56 |
protected $sDbName = '';
|
|
| 57 |
protected $sInstanceIdentifier = '';
|
|
| 58 |
protected $sTablePrefix = '';
|
|
| 59 |
protected $sCharset = '';
|
|
| 60 |
protected $connected = false;
|
|
| 61 |
protected $error = '';
|
|
| 62 |
protected $error_type = '';
|
|
| 63 |
protected $iQueryCount = 0;
|
|
| 63 | 64 |
|
| 64 | 65 |
/** |
| 65 | 66 |
* __constructor |
| 66 | 67 |
* prevent from public instancing |
| 67 | 68 |
*/ |
| 68 |
final private function __construct() {}
|
|
| 69 |
final private function __construct() {}
|
|
| 69 | 70 |
/** |
| 70 | 71 |
* prevent from cloning |
| 71 | 72 |
*/ |
| 72 |
final private function __clone() {}
|
|
| 73 |
final private function __clone() {}
|
|
| 73 | 74 |
/** |
| 74 | 75 |
* get a valid instance of this class |
| 75 | 76 |
* @param string $sIdentifier selector for several different instances |
| 76 | 77 |
* @return WbDatabase object |
| 77 | 78 |
*/ |
| 78 |
final public static function getInstance($sIdentifier = 'core')
|
|
| 79 |
{
|
|
| 80 |
if( !isset(self::$_oInstances[$sIdentifier])) {
|
|
| 79 |
final public static function getInstance($sIdentifier = 'core')
|
|
| 80 |
{
|
|
| 81 |
if( !isset(self::$_oInstances[$sIdentifier])) {
|
|
| 81 | 82 |
$c = __CLASS__; |
| 82 |
$oInstance = new $c;
|
|
| 83 |
$oInstance->sInstanceIdentifier = $sIdentifier;
|
|
| 83 |
$oInstance = new $c;
|
|
| 84 |
$oInstance->sInstanceIdentifier = $sIdentifier;
|
|
| 84 | 85 |
self::$_oInstances[$sIdentifier] = $oInstance; |
| 85 |
}
|
|
| 86 |
return self::$_oInstances[$sIdentifier];
|
|
| 87 |
}
|
|
| 86 |
}
|
|
| 87 |
return self::$_oInstances[$sIdentifier];
|
|
| 88 |
}
|
|
| 88 | 89 |
/** |
| 89 | 90 |
* disconnect and kills an existing instance |
| 90 | 91 |
* @param string $sIdentifier selector for instance to kill |
| 91 | 92 |
*/ |
| 92 |
final public static function killInstance($sIdentifier)
|
|
| 93 |
{
|
|
| 94 |
if($sIdentifier != 'core') {
|
|
| 95 |
if( isset(self::$_oInstances[$sIdentifier])) {
|
|
| 96 |
self::$_oInstances[$sIdentifier]->disconnect();
|
|
| 97 |
unset(self::$_oInstances[$sIdentifier]);
|
|
| 98 |
}
|
|
| 99 |
}
|
|
| 100 |
}
|
|
| 93 |
final public static function killInstance($sIdentifier)
|
|
| 94 |
{
|
|
| 95 |
if($sIdentifier != 'core') {
|
|
| 96 |
if( isset(self::$_oInstances[$sIdentifier])) {
|
|
| 97 |
self::$_oInstances[$sIdentifier]->disconnect();
|
|
| 98 |
unset(self::$_oInstances[$sIdentifier]);
|
|
| 99 |
}
|
|
| 100 |
}
|
|
| 101 |
}
|
|
| 101 | 102 |
/** |
| 102 | 103 |
* Establish connection |
| 103 | 104 |
* @param string $url |
| ... | ... | |
| 106 | 107 |
* @description opens a connection using connect URL<br /> |
| 107 | 108 |
* Example for SQL-Url: 'mysql://user:password@example.com[:3306]/database?charset=utf8&tableprefix=xx_' |
| 108 | 109 |
*/ |
| 109 |
public function doConnect($url = '')
|
|
| 110 |
{
|
|
| 111 |
if ($this->connected) { return $this->connected; } // prevent from reconnecting
|
|
| 112 |
$this->connected = false;
|
|
| 113 |
if ($url != '') {
|
|
| 114 |
// parse URL and extract connection data
|
|
| 115 |
$aIni = parse_url($url);
|
|
| 116 |
$scheme = isset($aIni['scheme']) ? $aIni['scheme'] : 'mysqli';
|
|
| 117 |
$hostname = isset($aIni['host']) ? $aIni['host'] : '';
|
|
| 118 |
$username = isset($aIni['user']) ? $aIni['user'] : '';
|
|
| 119 |
$password = isset($aIni['pass']) ? $aIni['pass'] : '';
|
|
| 120 |
$hostport = isset($aIni['port']) ? $aIni['port'] : '3306';
|
|
| 121 |
$hostport = $hostport == '3306' ? null : $hostport;
|
|
| 122 |
$db_name = ltrim(isset($aIni['path']) ? $aIni['path'] : '', '/\\');
|
|
| 123 |
$sTmp = isset($aIni['query']) ? $aIni['query'] : '';
|
|
| 124 |
$aQuery = explode('&', $sTmp);
|
|
| 125 |
foreach ($aQuery as $sArgument) {
|
|
| 126 |
$aArg = explode('=', $sArgument);
|
|
| 127 |
switch (strtolower($aArg[0])) {
|
|
| 128 |
case 'charset':
|
|
| 129 |
$this->sCharset = strtolower(preg_replace('/[^a-z0-9]/i', '', $aArg[1]));
|
|
| 130 |
break;
|
|
| 131 |
case 'tableprefix':
|
|
| 132 |
$this->sTablePrefix = $aArg[1];
|
|
| 133 |
break;
|
|
| 134 |
default:
|
|
| 135 |
break;
|
|
| 136 |
}
|
|
| 137 |
}
|
|
| 138 |
$this->sDbName = $db_name;
|
|
| 139 |
} else {
|
|
| 140 |
throw new WbDatabaseException('Missing parameter: unable to connect database');
|
|
| 141 |
}
|
|
| 142 |
$this->oDbHandle = @mysqli_connect($hostname, $username, $password, $db_name, $hostport);
|
|
| 143 |
if (!$this->oDbHandle) {
|
|
| 144 |
throw new WbDatabaseException('unable to connect \''.$scheme.'://'.$hostname.':'.$hostport.'\'');
|
|
| 145 |
} else {
|
|
| 110 |
public function doConnect($url = '')
|
|
| 111 |
{
|
|
| 112 |
if ($this->connected) { return $this->connected; } // prevent from reconnecting
|
|
| 113 |
$this->connected = false;
|
|
| 114 |
if ($url != '') {
|
|
| 115 |
// parse URL and extract connection data
|
|
| 116 |
$aIni = parse_url($url);
|
|
| 117 |
$scheme = isset($aIni['scheme']) ? $aIni['scheme'] : 'mysqli';
|
|
| 118 |
$hostname = isset($aIni['host']) ? $aIni['host'] : '';
|
|
| 119 |
$username = isset($aIni['user']) ? $aIni['user'] : '';
|
|
| 120 |
$password = isset($aIni['pass']) ? $aIni['pass'] : '';
|
|
| 121 |
$hostport = isset($aIni['port']) ? $aIni['port'] : '3306';
|
|
| 122 |
$hostport = $hostport == '3306' ? null : $hostport;
|
|
| 123 |
$db_name = ltrim(isset($aIni['path']) ? $aIni['path'] : '', '/\\');
|
|
| 124 |
$sTmp = isset($aIni['query']) ? $aIni['query'] : '';
|
|
| 125 |
$aQuery = explode('&', $sTmp);
|
|
| 126 |
foreach ($aQuery as $sArgument) {
|
|
| 127 |
$aArg = explode('=', $sArgument);
|
|
| 128 |
switch (strtolower($aArg[0])) {
|
|
| 129 |
case 'charset':
|
|
| 130 |
$this->sCharset = strtolower(preg_replace('/[^a-z0-9]/i', '', $aArg[1]));
|
|
| 131 |
break;
|
|
| 132 |
case 'tableprefix':
|
|
| 133 |
$this->sTablePrefix = $aArg[1];
|
|
| 134 |
break;
|
|
| 135 |
default:
|
|
| 136 |
break;
|
|
| 137 |
}
|
|
| 138 |
}
|
|
| 139 |
$this->sDbName = $db_name;
|
|
| 140 |
} else {
|
|
| 141 |
throw new WbDatabaseException('Missing parameter: unable to connect database');
|
|
| 142 |
}
|
|
| 143 |
$this->oDbHandle = @mysqli_connect($hostname, $username, $password, $db_name, $hostport);
|
|
| 144 |
if (!$this->oDbHandle) {
|
|
| 145 |
throw new WbDatabaseException('unable to connect \''.$scheme.'://'.$hostname.':'.$hostport.'\'');
|
|
| 146 |
} else {
|
|
| 146 | 147 |
if ($this->sCharset) {
|
| 147 | 148 |
@mysqli_query($this->oDbHandle, 'SET NAMES '.$this->sCharset); |
| 148 | 149 |
mysqli_set_charset($this->oDbHandle, $this->sCharset); |
| 149 | 150 |
} |
| 150 | 151 |
$this->connected = true; |
| 151 |
}
|
|
| 152 |
return $this->connected;
|
|
| 153 |
}
|
|
| 152 |
}
|
|
| 153 |
return $this->connected;
|
|
| 154 |
}
|
|
| 154 | 155 |
/** |
| 155 | 156 |
* disconnect database |
| 156 | 157 |
* @return bool |
| 157 | 158 |
* @description Disconnect current object from the database<br /> |
| 158 | 159 |
* the 'core' connection can NOT be disconnected! |
| 159 | 160 |
*/ |
| 160 |
public function disconnect()
|
|
| 161 |
{
|
|
| 162 |
if ($this->connected == true && $oInstance->sInstanceIdentifier != 'core') {
|
|
| 163 |
mysqli_close($this->oDbHandle);
|
|
| 164 |
$this->connected = false;
|
|
| 165 |
return true;
|
|
| 166 |
}
|
|
| 167 |
return false;
|
|
| 168 |
}
|
|
| 161 |
public function disconnect()
|
|
| 162 |
{
|
|
| 163 |
if ($this->connected == true && $oInstance->sInstanceIdentifier != 'core') {
|
|
| 164 |
mysqli_close($this->oDbHandle);
|
|
| 165 |
$this->connected = false;
|
|
| 166 |
return true;
|
|
| 167 |
}
|
|
| 168 |
return false;
|
|
| 169 |
}
|
|
| 169 | 170 |
/** |
| 170 | 171 |
* Alias for doQuery() |
| 171 |
* @deprecated from WB-2.8.5 and higher
|
|
| 172 |
* @deprecated from WB-2.8.4 and higher
|
|
| 172 | 173 |
*/ |
| 173 |
public function query($statement) |
|
| 174 |
{
|
|
| 175 |
return $this->doQuery($statement); |
|
| 176 |
} |
|
| 174 |
public function query($statement) |
|
| 175 |
{
|
|
| 176 |
trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
|
|
| 177 |
return $this->doQuery($statement); |
|
| 178 |
} |
|
| 177 | 179 |
/** |
| 178 | 180 |
* execute query |
| 179 | 181 |
* @param string $statement the SQL-statement to execute |
| 180 | 182 |
* @return null|\mysql |
| 181 | 183 |
*/ |
| 182 |
public function doQuery($statement) {
|
|
| 183 |
$this->iQueryCount++;
|
|
| 184 |
$mysql = new mysql($this->oDbHandle);
|
|
| 185 |
$mysql->query($statement);
|
|
| 186 |
$this->set_error($mysql->error($this->oDbHandle));
|
|
| 187 |
if ($mysql->error()) {
|
|
| 188 |
return null;
|
|
| 189 |
} else {
|
|
| 190 |
return $mysql;
|
|
| 191 |
}
|
|
| 192 |
}
|
|
| 184 |
public function doQuery($statement) {
|
|
| 185 |
$oRetval = null;
|
|
| 186 |
$this->iQueryCount++;
|
|
| 187 |
$mysql = new mysql($this->oDbHandle, $statement);
|
|
| 188 |
$this->set_error($mysql->error($this->oDbHandle));
|
|
| 189 |
if (!$mysql->error()) {
|
|
| 190 |
$oRetval = $mysql;
|
|
| 191 |
}
|
|
| 192 |
$this->set_error($mysql->error($this->oDbHandle));
|
|
| 193 |
return $oRetval;
|
|
| 194 |
}
|
|
| 193 | 195 |
/** |
| 194 | 196 |
* Alias for getOne() |
| 195 |
* @deprecated from WB-2.8.5 and higher
|
|
| 197 |
* @deprecated from WB-2.8.4 and higher
|
|
| 196 | 198 |
*/ |
| 197 |
public function get_one( $statement ) |
|
| 198 |
{
|
|
| 199 |
return $this->getOne($statement); |
|
| 200 |
} |
|
| 201 |
// Gets the first column of the first row |
|
| 199 |
public function get_one( $statement ) |
|
| 200 |
{
|
|
| 201 |
trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
|
|
| 202 |
return $this->getOne($statement); |
|
| 203 |
} |
|
| 204 |
// Gets the first column of the first row |
|
| 202 | 205 |
/** |
| 203 | 206 |
* Gets the first column of the first row |
| 204 | 207 |
* @param string $statement SQL-statement |
| 205 | 208 |
* @return null|mixed |
| 206 | 209 |
*/ |
| 207 |
public function getOne( $statement ) |
|
| 208 |
{
|
|
| 209 |
$this->iQueryCount++; |
|
| 210 |
$fetch_row = mysqli_fetch_array(mysqli_query($this->oDbHandle, $statement)); |
|
| 211 |
$result = $fetch_row[0]; |
|
| 212 |
$this->set_error(null); |
|
| 213 |
if (mysqli_error($this->oDbHandle)) {
|
|
| 214 |
$this->set_error(mysqli_error($this->oDbHandle)); |
|
| 215 |
return null; |
|
| 216 |
} else {
|
|
| 217 |
return $result; |
|
| 218 |
} |
|
| 219 |
} |
|
| 210 |
public function getOne( $sStatement ) |
|
| 211 |
{
|
|
| 212 |
$sRetval = null; |
|
| 213 |
if (($oRecSet = $this->doQuery($sStatement))) {
|
|
| 214 |
if (($aRecord = $oRecSet->fetchArray(MYSQL_NUM))) {
|
|
| 215 |
$sRetval = $aRecord[0]; |
|
| 216 |
} |
|
| 217 |
} |
|
| 218 |
return ($this->isError() ? null : $sRetval); |
|
| 219 |
} |
|
| 220 | 220 |
/** |
| 221 | 221 |
* Alias for setError() |
| 222 |
* @deprecated from WB-2.8.5 and higher
|
|
| 222 |
* @deprecated from WB-2.8.4 and higher
|
|
| 223 | 223 |
*/ |
| 224 |
public function set_error($message = null) |
|
| 225 |
{
|
|
| 226 |
$this->setError($message = null); |
|
| 227 |
} |
|
| 228 |
// Set the DB error |
|
| 224 |
public function set_error($message = null) |
|
| 225 |
{
|
|
| 226 |
trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
|
|
| 227 |
$this->setError($message = null); |
|
| 228 |
} |
|
| 229 |
// Set the DB error |
|
| 229 | 230 |
/** |
| 230 | 231 |
* setError |
| 231 | 232 |
* @param string $message |
| 232 | 233 |
*/ |
| 233 |
public function setError($message = null)
|
|
| 234 |
{
|
|
| 235 |
$this->error = $message;
|
|
| 236 |
}
|
|
| 234 |
public function setError($message = null)
|
|
| 235 |
{
|
|
| 236 |
$this->error = $message;
|
|
| 237 |
}
|
|
| 237 | 238 |
/** |
| 238 | 239 |
* Alias for isError |
| 239 |
* @deprecated from WB-2.8.5 and higher
|
|
| 240 |
* @deprecated from WB-2.8.4 and higher
|
|
| 240 | 241 |
*/ |
| 241 |
public function is_error() |
|
| 242 |
{
|
|
| 243 |
return $this->isError(); |
|
| 244 |
} |
|
| 242 |
public function is_error() |
|
| 243 |
{
|
|
| 244 |
trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
|
|
| 245 |
return $this->isError(); |
|
| 246 |
} |
|
| 245 | 247 |
/** |
| 246 | 248 |
* isError |
| 247 | 249 |
* @return bool |
| 248 | 250 |
*/ |
| 249 |
public function isError()
|
|
| 250 |
{
|
|
| 251 |
return (!empty($this->error)) ? true : false;
|
|
| 252 |
}
|
|
| 251 |
public function isError()
|
|
| 252 |
{
|
|
| 253 |
return (!empty($this->error)) ? true : false;
|
|
| 254 |
}
|
|
| 253 | 255 |
/** |
| 254 | 256 |
* Alias for getError |
| 255 |
* @deprecated from WB-2.8.5 and higher
|
|
| 257 |
* @deprecated from WB-2.8.4 and higher
|
|
| 256 | 258 |
*/ |
| 257 |
public function get_error() |
|
| 258 |
{
|
|
| 259 |
return $this->getError(); |
|
| 260 |
} |
|
| 259 |
public function get_error() |
|
| 260 |
{
|
|
| 261 |
trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
|
|
| 262 |
return $this->getError(); |
|
| 263 |
} |
|
| 261 | 264 |
/** |
| 262 | 265 |
* get last Error |
| 263 | 266 |
* @return string |
| 264 | 267 |
*/ |
| 265 |
public function getError()
|
|
| 266 |
{
|
|
| 267 |
return $this->error;
|
|
| 268 |
}
|
|
| 268 |
public function getError()
|
|
| 269 |
{
|
|
| 270 |
return $this->error;
|
|
| 271 |
}
|
|
| 269 | 272 |
/** |
| 270 | 273 |
* Protect class from property injections |
| 271 | 274 |
* @param string name of property |
| 272 | 275 |
* @param mixed value |
| 273 | 276 |
* @throws WbDatabaseException |
| 274 |
*/
|
|
| 275 |
public function __set($name, $value)
|
|
| 276 |
{
|
|
| 277 |
throw new WbDatabaseException('tried to set a readonly or nonexisting property ['.$name.']!! ');
|
|
| 278 |
}
|
|
| 277 |
*/ |
|
| 278 |
public function __set($name, $value)
|
|
| 279 |
{
|
|
| 280 |
throw new WbDatabaseException('tried to set a readonly or nonexisting property ['.$name.']!! ');
|
|
| 281 |
}
|
|
| 279 | 282 |
/** |
| 280 | 283 |
* default Getter for some properties |
| 281 | 284 |
* @param string name of the Property |
| 282 | 285 |
* @return NULL on error | valid property |
| 283 | 286 |
*/ |
| 284 |
public function __get($sPropertyName) |
|
| 285 |
{
|
|
| 286 |
switch ($sPropertyName) {
|
|
| 287 |
case 'DbHandle': |
|
| 288 |
case 'getDbHandle': // << set deprecated |
|
| 289 |
case 'db_handle': // << set deprecated |
|
| 290 |
$retval = $this->oDbHandle; |
|
| 291 |
break; |
|
| 292 |
case 'LastInsertId': |
|
| 293 |
case 'getLastInsertId': // << set deprecated |
|
| 294 |
$retval = $this->getLastInsertId(); |
|
| 295 |
break; |
|
| 296 |
case 'DbName': |
|
| 297 |
case 'getDbName': // << set deprecated |
|
| 298 |
case 'db_name': // << set deprecated |
|
| 299 |
$retval = $this->sDbName; |
|
| 300 |
break; |
|
| 301 |
case 'TablePrefix': |
|
| 302 |
case 'getTablePrefix': // << set deprecated |
|
| 303 |
$retval = $this->sTablePrefix; |
|
| 304 |
break; |
|
| 305 |
case 'QueryCount': |
|
| 306 |
case 'getQueryCount': // << set deprecated |
|
| 307 |
$retval = $this->iQueryCount; |
|
| 308 |
break; |
|
| 309 |
default: |
|
| 310 |
$retval = null; |
|
| 311 |
break; |
|
| 312 |
} |
|
| 313 |
return $retval; |
|
| 314 |
} // __get() |
|
| 287 |
public function __get($sPropertyName) |
|
| 288 |
{
|
|
| 289 |
switch ($sPropertyName) {
|
|
| 290 |
case 'getDbHandle': // << set deprecated |
|
| 291 |
case 'db_handle': // << set deprecated |
|
| 292 |
trigger_error('Deprecated property call: '.__CLASS__.'::'.__METHOD__.'(getDbHandle|db_handle)', E_USER_DEPRECATED);
|
|
| 293 |
case 'DbHandle': |
|
| 294 |
$retval = $this->oDbHandle; |
|
| 295 |
break; |
|
| 296 |
case 'getLastInsertId': // << set deprecated |
|
| 297 |
trigger_error('Deprecated property call: '.__CLASS__.'::'.__METHOD__.'(getLastInsertId)', E_USER_DEPRECATED);
|
|
| 298 |
case 'LastInsertId': |
|
| 299 |
$retval = $this->getLastInsertId(); |
|
| 300 |
break; |
|
| 301 |
case 'getDbName': // << set deprecated |
|
| 302 |
case 'db_name': // << set deprecated |
|
| 303 |
trigger_error('Deprecated property call: '.__CLASS__.'::'.__METHOD__.'(getDbName|db_name)', E_USER_DEPRECATED);
|
|
| 304 |
case 'DbName': |
|
| 305 |
$retval = $this->sDbName; |
|
| 306 |
break; |
|
| 307 |
case 'getTablePrefix': // << set deprecated |
|
| 308 |
trigger_error('Deprecated property call: '.__CLASS__.'::'.__METHOD__.'(getTablePrefix)', E_USER_DEPRECATED);
|
|
| 309 |
case 'TablePrefix': |
|
| 310 |
$retval = $this->sTablePrefix; |
|
| 311 |
break; |
|
| 312 |
case 'getQueryCount': // << set deprecated |
|
| 313 |
trigger_error('Deprecated property call: '.__CLASS__.'::'.__METHOD__.'(getQueryCount)', E_USER_DEPRECATED);
|
|
| 314 |
case 'QueryCount': |
|
| 315 |
$retval = $this->iQueryCount; |
|
| 316 |
break; |
|
| 317 |
default: |
|
| 318 |
$retval = null; |
|
| 319 |
break; |
|
| 320 |
} |
|
| 321 |
return $retval; |
|
| 322 |
} // __get() |
|
| 315 | 323 |
/** |
| 316 | 324 |
* Escapes special characters in a string for use in an SQL statement |
| 317 | 325 |
* @param string $unescaped_string |
| 318 | 326 |
* @return string |
| 319 | 327 |
*/ |
| 320 |
public function escapeString($unescaped_string)
|
|
| 321 |
{
|
|
| 322 |
return mysqli_real_escape_string($this->oDbHandle, $unescaped_string);
|
|
| 323 |
}
|
|
| 328 |
public function escapeString($sUnescapedString)
|
|
| 329 |
{
|
|
| 330 |
return mysqli_real_escape_string($this->oDbHandle, $sUnescapedString);
|
|
| 331 |
}
|
|
| 324 | 332 |
/** |
| 333 |
* Escapes wildchar characters in a string for use in an SQL-LIKE statement |
|
| 334 |
* @param string $unescaped_string |
|
| 335 |
* @return string |
|
| 336 |
*/ |
|
| 337 |
public function escapeLike($sUnescapedString) |
|
| 338 |
{
|
|
| 339 |
return addcslashes($sUnescapedString, '_%'); |
|
| 340 |
} |
|
| 341 |
/** |
|
| 325 | 342 |
* Last inserted Id |
| 326 | 343 |
* @return bool|int false on error, 0 if no record inserted |
| 327 |
*/
|
|
| 328 |
public function getLastInsertId()
|
|
| 329 |
{
|
|
| 330 |
return mysqli_insert_id($this->oDbHandle);
|
|
| 331 |
}
|
|
| 344 |
*/ |
|
| 345 |
public function getLastInsertId()
|
|
| 346 |
{
|
|
| 347 |
return mysqli_insert_id($this->oDbHandle);
|
|
| 348 |
}
|
|
| 332 | 349 |
|
| 333 | 350 |
} /// end of class database |
| 334 | 351 |
// //////////////////////////////////////////////////////////////////////////////////// // |
| ... | ... | |
| 347 | 364 |
*/ |
| 348 | 365 |
class WbDatabaseException extends AppException {}
|
| 349 | 366 |
|
| 350 |
/* extend global constants of mysql */ |
|
| 351 |
if(!defined('MYSQL_SEEK_FIRST')) { define('MYSQL_SEEK_FIRST', 0); }
|
|
| 352 |
if(!defined('MYSQL_SEEK_LAST')) { define('MYSQL_SEEK_LAST', -1); }
|
|
| 353 |
|
|
| 354 | 367 |
/** |
| 355 | 368 |
* mysql |
| 356 | 369 |
* |
| ... | ... | |
| 367 | 380 |
*/ |
| 368 | 381 |
class mysql {
|
| 369 | 382 |
|
| 370 |
private $result = null;
|
|
| 371 |
private $oDbHandle = null;
|
|
| 383 |
private $result = null;
|
|
| 384 |
private $oDbHandle = null;
|
|
| 372 | 385 |
private $error = ''; |
| 373 | 386 |
|
| 374 |
public function __construct($oHandle) |
|
| 387 |
public function __construct($oHandle, $sStatement)
|
|
| 375 | 388 |
{
|
| 376 | 389 |
$this->oDbHandle = $oHandle; |
| 390 |
$this->query($sStatement); |
|
| 377 | 391 |
} |
| 378 | 392 |
/** |
| 379 | 393 |
* query sql statement |
| ... | ... | |
| 381 | 395 |
* @return object |
| 382 | 396 |
* @throws WbDatabaseException |
| 383 | 397 |
*/ |
| 384 |
public function query($sStatement)
|
|
| 385 |
{
|
|
| 386 |
$this->result = @mysqli_query($this->oDbHandle, $sStatement);
|
|
| 387 |
if ($this->result === false) {
|
|
| 388 |
if (DEBUG) {
|
|
| 389 |
throw new WbDatabaseException(mysqli_error($this->oDbHandle));
|
|
| 390 |
} else {
|
|
| 391 |
throw new WbDatabaseException('Error in SQL-Statement');
|
|
| 392 |
}
|
|
| 393 |
}
|
|
| 394 |
$this->error = mysqli_error($this->oDbHandle);
|
|
| 395 |
return $this->result;
|
|
| 396 |
}
|
|
| 398 |
public function query($sStatement)
|
|
| 399 |
{
|
|
| 400 |
$this->result = @mysqli_query($this->oDbHandle, $sStatement);
|
|
| 401 |
if ($this->result === false) {
|
|
| 402 |
if (DEBUG) {
|
|
| 403 |
throw new WbDatabaseException(mysqli_error($this->oDbHandle));
|
|
| 404 |
} else {
|
|
| 405 |
throw new WbDatabaseException('Error in SQL-Statement');
|
|
| 406 |
}
|
|
| 407 |
}
|
|
| 408 |
$this->error = mysqli_error($this->oDbHandle);
|
|
| 409 |
return $this->result;
|
|
| 410 |
}
|
|
| 397 | 411 |
/** |
| 398 | 412 |
* numRows |
| 399 | 413 |
* @return integer |
| 400 | 414 |
* @description number of returned records |
| 401 | 415 |
*/ |
| 402 |
public function numRows()
|
|
| 403 |
{
|
|
| 404 |
return mysqli_num_rows($this->result);
|
|
| 405 |
}
|
|
| 416 |
public function numRows()
|
|
| 417 |
{
|
|
| 418 |
return mysqli_num_rows($this->result);
|
|
| 419 |
}
|
|
| 406 | 420 |
/** |
| 407 | 421 |
* fetchRow |
| 408 |
* @param int $typ MYSQL_BOTH(default) | MYSQL_ASSOC | MYSQL_NUM |
|
| 409 |
* @return array |
|
| 422 |
* @param int $typ MYSQL_BOTH(default) | MYSQL_ASSOC | MYSQL_NUM // DEPRECATED
|
|
| 423 |
* @return array with numeric indexes
|
|
| 410 | 424 |
* @description get current record and increment pointer |
| 411 | 425 |
*/ |
| 412 |
public function fetchRow($typ = MYSQLI_BOTH) |
|
| 413 |
{
|
|
| 414 |
return mysqli_fetch_array($this->result, $typ); |
|
| 415 |
} |
|
| 426 |
public function fetchRow($typ = MYSQLI_BOTH) |
|
| 427 |
{
|
|
| 428 |
if ($typ != MYSQLI_NUM) {
|
|
| 429 |
trigger_error('Deprecated call: '.__CLASS__.'::'.__METHOD__.' for MYSQLI_ASSOC|MYSQL_BOTH', E_USER_DEPRECATED);
|
|
| 430 |
return mysqli_fetch_array($this->result, $typ); |
|
| 431 |
} else {
|
|
| 432 |
return mysqli_fetch_row($this->result); |
|
| 433 |
} |
|
| 434 |
} |
|
| 416 | 435 |
/** |
| 436 |
* fetchAssoc |
|
| 437 |
* @return array with assotiative indexes |
|
| 438 |
* @description get current record and increment pointer |
|
| 439 |
*/ |
|
| 440 |
public function fetchAssoc() |
|
| 441 |
{
|
|
| 442 |
if ($typ != MYSQLI_NUM) {
|
|
| 443 |
trigger_error('Deprecated call: '.__CLASS__.'::'.__METHOD__.' for MYSQLI_ASSOC|MYSQL_BOTH', E_USER_DEPRECATED);
|
|
| 444 |
return mysqli_fetch_array($this->result, $typ); |
|
| 445 |
} else {
|
|
| 446 |
return mysqli_fetch_row($this->result); |
|
| 447 |
} |
|
| 448 |
} |
|
| 449 |
/** |
|
| 417 | 450 |
* fetchObject |
| 418 | 451 |
* @param string $sClassname Name of the class to use. Is no given use stdClass |
| 419 | 452 |
* @param string $aParams optional array of arguments for the constructor |
| 420 | 453 |
* @return object |
| 421 | 454 |
* @description get current record as an object and increment pointer |
| 422 | 455 |
*/ |
| 423 |
public function fetchObject($sClassName = null, array $aParams = null)
|
|
| 424 |
{
|
|
| 425 |
if ($sClassName === null || class_exists($sClassName)) {
|
|
| 426 |
return mysqli_fetch_object($this->result, $sClassName, $aParams);
|
|
| 427 |
} else {
|
|
| 428 |
throw new WbDatabaseException('Class <'.$sClassName.'> not available on request of mysql_fetch_object()');
|
|
| 429 |
}
|
|
| 430 |
}
|
|
| 456 |
public function fetchObject($sClassName = null, array $aParams = null)
|
|
| 457 |
{
|
|
| 458 |
if ($sClassName === null || class_exists($sClassName)) {
|
|
| 459 |
return mysqli_fetch_object($this->result, $sClassName, $aParams);
|
|
| 460 |
} else {
|
|
| 461 |
throw new WbDatabaseException('Class <'.$sClassName.'> not available on request of mysqli_fetch_object()');
|
|
| 462 |
}
|
|
| 463 |
}
|
|
| 431 | 464 |
/** |
| 432 | 465 |
* fetchArray |
| 433 | 466 |
* @param int $iType MYSQL_ASSOC(default) | MYSQL_BOTH | MYSQL_NUM |
| 434 | 467 |
* @return array of current record |
| 435 | 468 |
* @description get current record and increment pointer |
| 436 | 469 |
*/ |
| 437 |
public function fetchArray($iType = MYSQLI_ASSOC)
|
|
| 438 |
{
|
|
| 470 |
public function fetchArray($iType = MYSQLI_ASSOC)
|
|
| 471 |
{
|
|
| 439 | 472 |
if ($iType < MYSQLI_ASSOC || $iType > MYSQLI_BOTH) {
|
| 440 | 473 |
$iType = MYSQLI_ASSOC; |
| 441 | 474 |
} |
| 442 |
return mysqli_fetch_array($this->result, $iType);
|
|
| 443 |
}
|
|
| 475 |
return mysqli_fetch_array($this->result, $iType);
|
|
| 476 |
}
|
|
| 444 | 477 |
/** |
| 445 | 478 |
* fetchAll |
| 446 | 479 |
* @param int $iType MYSQL_ASSOC(default) | MYSQL_NUM |
| 447 | 480 |
* @return array of rows |
| 448 | 481 |
* @description get all records of the result set |
| 449 | 482 |
*/ |
| 450 |
public function fetchAll($iType = MYSQL_ASSOC) |
|
| 483 |
public function fetchAll($iType = MYSQLI_ASSOC)
|
|
| 451 | 484 |
{
|
| 452 |
$iType = $iType != MYSQL_NUM ? MYSQL_ASSOC : MYSQL_NUM;
|
|
| 485 |
$iType = $iType != MYSQLI_NUM ? MYSQL_ASSOC : MYSQLI_NUM;
|
|
| 453 | 486 |
return mysqli_fetch_all($this->result, $iType); |
| 454 | 487 |
} |
| 455 | 488 |
/** |
| ... | ... | |
| 457 | 490 |
* @return bool |
| 458 | 491 |
* @description set the recordpointer to the first record || false on error |
| 459 | 492 |
*/ |
| 460 |
public function rewind()
|
|
| 461 |
{
|
|
| 462 |
return $this->seekRow(MYSQL_SEEK_FIRST);
|
|
| 463 |
}
|
|
| 493 |
public function rewind()
|
|
| 494 |
{
|
|
| 495 |
return $this->seekRow(MYSQLI_SEEK_FIRST);
|
|
| 496 |
}
|
|
| 464 | 497 |
/** |
| 465 | 498 |
* seekRow |
| 466 |
* @param int $position |
|
| 499 |
* @param int $position also can be MYSQLI_SEEK_FIRST||MYSQLI_SEEK_LAST
|
|
| 467 | 500 |
* @return bool |
| 468 | 501 |
* @description set the pointer to the given record || false on error |
| 469 | 502 |
*/ |
| 470 |
public function seekRow( $position = MYSQL_SEEK_FIRST )
|
|
| 471 |
{
|
|
| 472 |
$pmax = $this->numRows() - 1;
|
|
| 473 |
$p = (($position < 0 || $position > $pmax) ? $pmax : $position);
|
|
| 474 |
return mysqli_data_seek($this->result, $p);
|
|
| 475 |
}
|
|
| 503 |
public function seekRow( $position = MYSQLI_SEEK_FIRST )
|
|
| 504 |
{
|
|
| 505 |
$pmax = $this->numRows() - 1;
|
|
| 506 |
$p = (($position < 0 || $position > $pmax) ? $pmax : $position);
|
|
| 507 |
return mysqli_data_seek($this->result, $p);
|
|
| 508 |
}
|
|
| 476 | 509 |
/** |
| 477 | 510 |
* freeResult |
| 478 | 511 |
* @return bool |
| 479 | 512 |
* @description remove retult object from memeory |
| 480 | 513 |
*/ |
| 481 |
public function freeResult()
|
|
| 482 |
{
|
|
| 483 |
return mysqli_free_result($this->result);
|
|
| 484 |
}
|
|
| 485 |
/**
|
|
| 514 |
public function freeResult()
|
|
| 515 |
{
|
|
| 516 |
return mysqli_free_result($this->result);
|
|
| 517 |
}
|
|
| 518 |
/** |
|
| 486 | 519 |
* Get error |
| 487 | 520 |
* @return string || null if no error |
| 488 | 521 |
*/ |
| 489 |
public function error()
|
|
| 490 |
{
|
|
| 491 |
if (isset($this->error)) {
|
|
| 492 |
return $this->error;
|
|
| 493 |
} else {
|
|
| 494 |
return null;
|
|
| 495 |
}
|
|
| 496 |
}
|
|
| 522 |
public function error()
|
|
| 523 |
{
|
|
| 524 |
if (isset($this->error)) {
|
|
| 525 |
return $this->error;
|
|
| 526 |
} else {
|
|
| 527 |
return null;
|
|
| 528 |
}
|
|
| 529 |
}
|
|
| 497 | 530 |
|
| 498 | 531 |
} |
| 499 |
// //////////////////////////////////////////////////////////////////////////////////// // |
|
| 500 |
/* this function is placed inside this file temporarely until a better place is found */ |
|
| 501 |
/* function to update a var/value-pair(s) in table **************************** |
|
| 502 |
* nonexisting keys are inserted |
|
| 503 |
* @param string $table: name of table to use (without prefix) |
|
| 504 |
* @param mixed $key: a array of key->value pairs to update |
|
| 505 |
* or a string with name of the key to update |
|
| 506 |
* @param string $value: a sting with needed value, if $key is a string too |
|
| 507 |
* @return bool: true if any keys are updated, otherwise false |
|
| 508 |
*/ |
|
| 509 |
function db_update_key_value($table, $key, $value = '') |
|
| 510 |
{
|
|
| 511 |
$oDb = WbDatabase::getInstance(); |
|
| 512 |
$table = preg_replace('/^'.preg_quote($oDb->TablePrefix, '/').'/s', '', $table);
|
|
| 513 |
if (!is_array($key)) {
|
|
| 514 |
if (trim($key) != '') {
|
|
| 515 |
$key = array( trim($key) => trim($value) ); |
|
| 516 |
} else {
|
|
| 517 |
$key = array(); |
|
| 518 |
} |
|
| 519 |
} |
|
| 520 |
$retval = true; |
|
| 521 |
$sNameValPairs = ''; |
|
| 522 |
foreach ($key as $index => $val) {
|
|
| 523 |
$sNameValPairs .= ', (\''.$index.'\', \''.$val.'\')'; |
|
| 524 |
} |
|
| 525 |
$sValues = ltrim($sNameValPairs, ', '); |
|
| 526 |
if ($sValues != '') {
|
|
| 527 |
$sql = 'REPLACE INTO `'.$oDb->TablePrefix.$table.'` (`name`, `value`) ' |
|
| 528 |
. 'VALUES '.$sValues; |
|
| 529 |
if (!$oDb->doQuery($sql)) {
|
|
| 530 |
$retval = false; |
|
| 531 |
} |
|
| 532 |
} |
|
| 533 |
return $retval; |
|
| 534 |
} |
|
Also available in: Unified diff
! framework/ ~WbDatabaseHelper ~WbDatabase some little correction to different methods