Revision 1998
Added by darkviper almost 12 years ago
| WbDatabase.php | ||
|---|---|---|
| 20 | 20 |
* |
| 21 | 21 |
* @category Core |
| 22 | 22 |
* @package Core_database |
| 23 |
* @author Werner v.d.Decken <wkl@isteam.de>
|
|
| 23 |
* @author Manuela v.d.Decken <manuela@isteam.de>
|
|
| 24 | 24 |
* @author Dietmar W. <dietmar.woellbrink@websitebaker.org> |
| 25 |
* @copyright Werner v.d.Decken <wkl@isteam.de>
|
|
| 25 |
* @copyright Manuela v.d.Decken <manuela@isteam.de>
|
|
| 26 | 26 |
* @license http://www.gnu.org/licenses/gpl.html GPL License |
| 27 | 27 |
* @version 0.0.9 |
| 28 | 28 |
* @revision $Revision$ |
| ... | ... | |
| 38 | 38 |
|
| 39 | 39 |
private static $_oInstances = array(); |
| 40 | 40 |
|
| 41 |
private $_db_handle = null; // readonly from outside
|
|
| 42 |
private $_db_name = '';
|
|
| 43 |
private $_sInstanceIdentifier = '';
|
|
| 41 |
protected $oDbHandle = null; // readonly from outside
|
|
| 42 |
protected $sDbName = '';
|
|
| 43 |
protected $sInstanceIdentifier = '';
|
|
| 44 | 44 |
protected $sTablePrefix = ''; |
| 45 | 45 |
protected $sCharset = ''; |
| 46 | 46 |
protected $connected = false; |
| ... | ... | |
| 48 | 48 |
protected $error_type = ''; |
| 49 | 49 |
protected $iQueryCount = 0; |
| 50 | 50 |
|
| 51 |
/* prevent from public instancing */ |
|
| 51 |
/** |
|
| 52 |
* __constructor |
|
| 53 |
* prevent from public instancing |
|
| 54 |
*/ |
|
| 52 | 55 |
protected function __construct() {}
|
| 53 |
/* prevent from cloning */ |
|
| 56 |
/** |
|
| 57 |
* prevent from cloning |
|
| 58 |
*/ |
|
| 54 | 59 |
private function __clone() {}
|
| 55 | 60 |
/** |
| 56 | 61 |
* get a valid instance of this class |
| 57 | 62 |
* @param string $sIdentifier selector for several different instances |
| 58 |
* @return object |
|
| 63 |
* @return WbDatabase object
|
|
| 59 | 64 |
*/ |
| 60 |
public static function getInstance($sIdentifier = 'core') {
|
|
| 65 |
public static function getInstance($sIdentifier = 'core') |
|
| 66 |
{
|
|
| 61 | 67 |
if( !isset(self::$_oInstances[$sIdentifier])) {
|
| 62 | 68 |
$c = __CLASS__; |
| 63 | 69 |
$oInstance = new $c; |
| 64 |
$oInstance->_sInstanceIdentifier = $sIdentifier;
|
|
| 70 |
$oInstance->sInstanceIdentifier = $sIdentifier; |
|
| 65 | 71 |
self::$_oInstances[$sIdentifier] = $oInstance; |
| 66 | 72 |
} |
| 67 | 73 |
return self::$_oInstances[$sIdentifier]; |
| 68 | 74 |
} |
| 69 | 75 |
/** |
| 70 | 76 |
* disconnect and kills an existing instance |
| 71 |
* @param string $sIdentifier |
|
| 77 |
* @param string $sIdentifier selector for instance to kill
|
|
| 72 | 78 |
*/ |
| 73 |
public static function killInstance($sIdentifier) {
|
|
| 79 |
public static function killInstance($sIdentifier) |
|
| 80 |
{
|
|
| 74 | 81 |
if($sIdentifier != 'core') {
|
| 75 | 82 |
if( isset(self::$_oInstances[$sIdentifier])) {
|
| 76 | 83 |
self::$_oInstances[$sIdentifier]->disconnect(); |
| ... | ... | |
| 79 | 86 |
} |
| 80 | 87 |
} |
| 81 | 88 |
/** |
| 82 |
* Connect to the database
|
|
| 89 |
* Establish connection
|
|
| 83 | 90 |
* @param string $url |
| 84 | 91 |
* @return bool |
| 85 |
* |
|
| 86 |
* Example for SQL-Url: 'mysql://user:password@demo.de[:3306]/datenbank' |
|
| 92 |
* @throws WbDatabaseException |
|
| 93 |
* @description opens a connection using connect URL<br /> |
|
| 94 |
* Example for SQL-Url: 'mysql://user:password@example.com[:3306]/database?charset=utf8&tableprefix=xx_' |
|
| 87 | 95 |
*/ |
| 88 |
public function doConnect($url = '') {
|
|
| 89 |
if($this->connected) { return $this->connected; } // prevent from reconnecting
|
|
| 96 |
public function doConnect($url = '') |
|
| 97 |
{
|
|
| 98 |
if ($this->connected) { return $this->connected; } // prevent from reconnecting
|
|
| 90 | 99 |
$this->connected = false; |
| 91 |
if($url != '') {
|
|
| 100 |
if ($url != '') {
|
|
| 101 |
// parse URL and extract connection data |
|
| 92 | 102 |
$aIni = parse_url($url); |
| 93 |
|
|
| 94 | 103 |
$scheme = isset($aIni['scheme']) ? $aIni['scheme'] : 'mysql'; |
| 95 | 104 |
$hostname = isset($aIni['host']) ? $aIni['host'] : ''; |
| 96 | 105 |
$username = isset($aIni['user']) ? $aIni['user'] : ''; |
| ... | ... | |
| 100 | 109 |
$db_name = ltrim(isset($aIni['path']) ? $aIni['path'] : '', '/\\'); |
| 101 | 110 |
$sTmp = isset($aIni['query']) ? $aIni['query'] : ''; |
| 102 | 111 |
$aQuery = explode('&', $sTmp);
|
| 103 |
foreach($aQuery as $sArgument) {
|
|
| 112 |
foreach ($aQuery as $sArgument) {
|
|
| 104 | 113 |
$aArg = explode('=', $sArgument);
|
| 105 |
switch(strtolower($aArg[0])) {
|
|
| 114 |
switch (strtolower($aArg[0])) {
|
|
| 106 | 115 |
case 'charset': |
| 107 | 116 |
$this->sCharset = strtolower(preg_replace('/[^a-z0-9]/i', '', $aArg[1]));
|
| 108 | 117 |
break; |
| ... | ... | |
| 113 | 122 |
break; |
| 114 | 123 |
} |
| 115 | 124 |
} |
| 116 |
$this->_db_name = $db_name;
|
|
| 117 |
}else {
|
|
| 125 |
$this->sDbName = $db_name;
|
|
| 126 |
} else {
|
|
| 118 | 127 |
throw new WbDatabaseException('Missing parameter: unable to connect database');
|
| 119 | 128 |
} |
| 120 |
$this->_db_handle = @mysql_connect($hostname.$hostport, |
|
| 121 |
$username, |
|
| 122 |
$password, |
|
| 123 |
true); |
|
| 124 |
if(!$this->_db_handle) {
|
|
| 125 |
throw new WbDatabaseException('unable to connect \''.$scheme.'://'.
|
|
| 126 |
$hostname.$hostport.'\''); |
|
| 129 |
$this->oDbHandle = @mysql_connect($hostname.$hostport, $username, $password, true); |
|
| 130 |
if (!$this->oDbHandle) {
|
|
| 131 |
throw new WbDatabaseException('unable to connect \''.$scheme.'://'.$hostname.$hostport.'\'');
|
|
| 127 | 132 |
} else {
|
| 128 |
if(!@mysql_select_db($db_name, $this->_db_handle)) {
|
|
| 133 |
if (!@mysql_select_db($db_name, $this->oDbHandle)) {
|
|
| 129 | 134 |
throw new WbDatabaseException('unable to select database \''.$db_name.
|
| 130 |
'\' on \''.$scheme.'://'. |
|
| 131 |
$hostname.$hostport.'\''); |
|
| 135 |
'\' on \''.$scheme.'://'. |
|
| 136 |
$hostname.$hostport.'\'' |
|
| 137 |
); |
|
| 132 | 138 |
} else {
|
| 133 |
if($this->sCharset) {
|
|
| 134 |
@mysql_query('SET NAMES \''.$this->sCharset.'\'', $this->_db_handle);
|
|
| 139 |
if ($this->sCharset) {
|
|
| 140 |
@mysql_query('SET NAMES \''.$this->sCharset.'\'', $this->oDbHandle);
|
|
| 135 | 141 |
} |
| 136 | 142 |
$this->connected = true; |
| 137 | 143 |
} |
| 138 | 144 |
} |
| 139 | 145 |
return $this->connected; |
| 140 | 146 |
} |
| 141 |
|
|
| 142 |
// Disconnect from the database |
|
| 143 |
public function disconnect() {
|
|
| 144 |
if($this->connected == true && $oInstance->_sInstanceIdentifier != 'core' ) {
|
|
| 145 |
mysql_close($this->_db_handle); |
|
| 147 |
/** |
|
| 148 |
* disconnect database |
|
| 149 |
* @return bool |
|
| 150 |
* @description Disconnect current object from the database<br /> |
|
| 151 |
* the 'core' connection can NOT be disconnected! |
|
| 152 |
*/ |
|
| 153 |
public function disconnect() |
|
| 154 |
{
|
|
| 155 |
if ($this->connected == true && $oInstance->sInstanceIdentifier != 'core') {
|
|
| 156 |
mysql_close($this->oDbHandle); |
|
| 146 | 157 |
$this->connected = false; |
| 147 | 158 |
return true; |
| 148 | 159 |
} |
| 149 | 160 |
return false; |
| 150 | 161 |
} |
| 151 |
|
|
| 152 |
// Run a query |
|
| 153 |
public function query($statement) {
|
|
| 162 |
/** |
|
| 163 |
* Alias for doQuery() |
|
| 164 |
*/ |
|
| 165 |
public function query($statement) |
|
| 166 |
{
|
|
| 167 |
return $this->doQuery($statement); |
|
| 168 |
} |
|
| 169 |
/** |
|
| 170 |
* execute query |
|
| 171 |
* @param string $statement the SQL-statement to execute |
|
| 172 |
* @return null|\mysql |
|
| 173 |
*/ |
|
| 174 |
public function doQuery($statement) {
|
|
| 154 | 175 |
$this->iQueryCount++; |
| 155 | 176 |
$mysql = new mysql(); |
| 156 |
$mysql->query($statement, $this->_db_handle);
|
|
| 157 |
$this->set_error($mysql->error($this->_db_handle));
|
|
| 158 |
if($mysql->error($this->_db_handle)) {
|
|
| 177 |
$mysql->query($statement, $this->oDbHandle);
|
|
| 178 |
$this->set_error($mysql->error($this->oDbHandle));
|
|
| 179 |
if ($mysql->error($this->oDbHandle)) {
|
|
| 159 | 180 |
return null; |
| 160 | 181 |
} else {
|
| 161 | 182 |
return $mysql; |
| 162 | 183 |
} |
| 163 | 184 |
} |
| 164 |
|
|
| 165 |
// Gets the first column of the first row |
|
| 185 |
/** |
|
| 186 |
* Alias for getOne() |
|
| 187 |
*/ |
|
| 166 | 188 |
public function get_one( $statement ) |
| 167 | 189 |
{
|
| 190 |
return $this->getOne($statement); |
|
| 191 |
} |
|
| 192 |
// Gets the first column of the first row |
|
| 193 |
/** |
|
| 194 |
* Gets the first column of the first row |
|
| 195 |
* @param string $statement SQL-statement |
|
| 196 |
* @return null|mixed |
|
| 197 |
*/ |
|
| 198 |
public function getOne( $statement ) |
|
| 199 |
{
|
|
| 168 | 200 |
$this->iQueryCount++; |
| 169 |
$fetch_row = mysql_fetch_array(mysql_query($statement, $this->_db_handle));
|
|
| 201 |
$fetch_row = mysql_fetch_array(mysql_query($statement, $this->oDbHandle));
|
|
| 170 | 202 |
$result = $fetch_row[0]; |
| 171 |
$this->set_error(mysql_error($this->_db_handle));
|
|
| 172 |
if(mysql_error($this->_db_handle)) {
|
|
| 203 |
$this->set_error(mysql_error($this->oDbHandle));
|
|
| 204 |
if (mysql_error($this->oDbHandle)) {
|
|
| 173 | 205 |
return null; |
| 174 | 206 |
} else {
|
| 175 | 207 |
return $result; |
| 176 | 208 |
} |
| 177 | 209 |
} |
| 178 |
|
|
| 210 |
/** |
|
| 211 |
* Alias for setError() |
|
| 212 |
*/ |
|
| 213 |
public function set_error($message = null) |
|
| 214 |
{
|
|
| 215 |
$this->setError($message = null); |
|
| 216 |
} |
|
| 179 | 217 |
// Set the DB error |
| 180 |
public function set_error($message = null) {
|
|
| 181 |
global $TABLE_DOES_NOT_EXIST, $TABLE_UNKNOWN; |
|
| 218 |
/** |
|
| 219 |
* setError |
|
| 220 |
* @param string $message |
|
| 221 |
*/ |
|
| 222 |
public function setError($message = null) |
|
| 223 |
{
|
|
| 182 | 224 |
$this->error = $message; |
| 183 |
if(strpos($message, 'no such table')) {
|
|
| 184 |
$this->error_type = $TABLE_DOES_NOT_EXIST; |
|
| 185 |
} else {
|
|
| 186 |
$this->error_type = $TABLE_UNKNOWN; |
|
| 187 |
} |
|
| 188 | 225 |
} |
| 189 |
|
|
| 190 |
// Return true if there was an error |
|
| 191 |
public function is_error() {
|
|
| 226 |
/** |
|
| 227 |
* Alias for isError |
|
| 228 |
*/ |
|
| 229 |
public function is_error() |
|
| 230 |
{
|
|
| 231 |
return $this->isError(); |
|
| 232 |
} |
|
| 233 |
/** |
|
| 234 |
* isError |
|
| 235 |
* @return bool |
|
| 236 |
*/ |
|
| 237 |
public function isError() |
|
| 238 |
{
|
|
| 192 | 239 |
return (!empty($this->error)) ? true : false; |
| 193 | 240 |
} |
| 194 |
|
|
| 195 |
// Return the error |
|
| 196 |
public function get_error() {
|
|
| 241 |
/** |
|
| 242 |
* Alias for getError |
|
| 243 |
*/ |
|
| 244 |
public function get_error() |
|
| 245 |
{
|
|
| 246 |
return $this->getError(); |
|
| 247 |
} |
|
| 248 |
/** |
|
| 249 |
* get last Error |
|
| 250 |
* @return string |
|
| 251 |
*/ |
|
| 252 |
public function getError() |
|
| 253 |
{
|
|
| 197 | 254 |
return $this->error; |
| 198 | 255 |
} |
| 199 | 256 |
/** |
| ... | ... | |
| 202 | 259 |
* @param mixed value |
| 203 | 260 |
* @throws WbDatabaseException |
| 204 | 261 |
*/ |
| 205 |
public function __set($name, $value) {
|
|
| 262 |
public function __set($name, $value) |
|
| 263 |
{
|
|
| 206 | 264 |
throw new WbDatabaseException('tried to set a readonly or nonexisting property ['.$name.']!! ');
|
| 207 | 265 |
} |
| 208 | 266 |
/** |
| 209 | 267 |
* default Getter for some properties |
| 210 | 268 |
* @param string name of the Property |
| 211 |
* @return mixed NULL on error or missing property
|
|
| 269 |
* @return NULL on error | valid property
|
|
| 212 | 270 |
*/ |
| 213 | 271 |
public function __get($sPropertyName) |
| 214 | 272 |
{
|
| 215 |
switch ($sPropertyName): |
|
| 216 |
case 'db_handle': |
|
| 273 |
switch ($sPropertyName) {
|
|
| 217 | 274 |
case 'DbHandle': |
| 218 |
case 'getDbHandle': |
|
| 219 |
$retval = $this->_db_handle; |
|
| 275 |
case 'getDbHandle': // << set deprecated |
|
| 276 |
case 'db_handle': // << set deprecated |
|
| 277 |
$retval = $this->oDbHandle; |
|
| 220 | 278 |
break; |
| 221 | 279 |
case 'LastInsertId': |
| 222 |
case 'getLastInsertId': |
|
| 223 |
$retval = mysql_insert_id($this->_db_handle);
|
|
| 280 |
case 'getLastInsertId': // << set deprecated
|
|
| 281 |
$retval = mysql_insert_id($this->oDbHandle);
|
|
| 224 | 282 |
break; |
| 225 |
case 'db_name': |
|
| 226 | 283 |
case 'DbName': |
| 227 |
case 'getDbName': |
|
| 228 |
$retval = $this->_db_name; |
|
| 284 |
case 'getDbName': // << set deprecated |
|
| 285 |
case 'db_name': // << set deprecated |
|
| 286 |
$retval = $this->sDbName; |
|
| 229 | 287 |
break; |
| 230 | 288 |
case 'TablePrefix': |
| 231 |
case 'getTablePrefix': |
|
| 289 |
case 'getTablePrefix': // << set deprecated
|
|
| 232 | 290 |
$retval = $this->sTablePrefix; |
| 233 | 291 |
break; |
| 234 | 292 |
case 'QueryCount': |
| 235 |
case 'getQueryCount': |
|
| 293 |
case 'getQueryCount': // << set deprecated
|
|
| 236 | 294 |
$retval = $this->iQueryCount; |
| 237 | 295 |
break; |
| 238 | 296 |
default: |
| 239 | 297 |
$retval = null; |
| 240 | 298 |
break; |
| 241 |
endswitch;
|
|
| 299 |
}
|
|
| 242 | 300 |
return $retval; |
| 243 | 301 |
} // __get() |
| 244 | 302 |
/** |
| ... | ... | |
| 248 | 306 |
*/ |
| 249 | 307 |
public function escapeString($unescaped_string) |
| 250 | 308 |
{
|
| 251 |
return mysql_real_escape_string($unescaped_string, $this->_db_handle);
|
|
| 309 |
return mysql_real_escape_string($unescaped_string, $this->oDbHandle);
|
|
| 252 | 310 |
} |
| 253 | 311 |
/** |
| 254 | 312 |
* Last inserted Id |
| ... | ... | |
| 256 | 314 |
*/ |
| 257 | 315 |
public function getLastInsertId() |
| 258 | 316 |
{
|
| 259 |
return mysql_insert_id($this->_db_handle);
|
|
| 317 |
return mysql_insert_id($this->oDbHandle);
|
|
| 260 | 318 |
} |
| 319 |
/** |
|
| 320 |
* Alias for isField() |
|
| 321 |
*/ |
|
| 322 |
public function field_exists($table_name, $field_name) |
|
| 323 |
{
|
|
| 324 |
return $this->isField($table_name, $field_name); |
|
| 325 |
} |
|
| 261 | 326 |
/* |
| 262 | 327 |
* @param string full name of the table (incl. TABLE_PREFIX) |
| 263 | 328 |
* @param string name of the field to seek for |
| 264 | 329 |
* @return bool true if field exists |
| 265 | 330 |
*/ |
| 266 |
public function field_exists($table_name, $field_name)
|
|
| 331 |
public function isField($table_name, $field_name)
|
|
| 267 | 332 |
{
|
| 268 | 333 |
$sql = 'DESCRIBE `'.$table_name.'` `'.$field_name.'` '; |
| 269 |
$query = $this->query($sql, $this->_db_handle);
|
|
| 334 |
$query = $this->query($sql, $this->oDbHandle);
|
|
| 270 | 335 |
return ($query->numRows() != 0); |
| 271 | 336 |
} |
| 337 |
/** |
|
| 338 |
* Alias for isIndex() |
|
| 339 |
*/ |
|
| 340 |
public function index_exists($table_name, $index_name, $number_fields = 0) |
|
| 341 |
{
|
|
| 342 |
return $this->isIndex($table_name, $index_name, $number_fields = 0); |
|
| 343 |
} |
|
| 272 | 344 |
/* |
| 345 |
* isIndex |
|
| 273 | 346 |
* @param string full name of the table (incl. TABLE_PREFIX) |
| 274 | 347 |
* @param string name of the index to seek for |
| 275 | 348 |
* @return bool true if field exists |
| 276 | 349 |
*/ |
| 277 |
public function index_exists($table_name, $index_name, $number_fields = 0)
|
|
| 350 |
public function isIndex($table_name, $index_name, $number_fields = 0)
|
|
| 278 | 351 |
{
|
| 279 | 352 |
$number_fields = intval($number_fields); |
| 280 | 353 |
$keys = 0; |
| 281 | 354 |
$sql = 'SHOW INDEX FROM `'.$table_name.'`'; |
| 282 |
if( ($res_keys = $this->query($sql, $this->_db_handle)) ) |
|
| 283 |
{
|
|
| 284 |
while(($rec_key = $res_keys->fetchRow())) |
|
| 285 |
{
|
|
| 286 |
if( $rec_key['Key_name'] == $index_name ) |
|
| 287 |
{
|
|
| 355 |
if (($res_keys = $this->doQuery($sql, $this->oDbHandle))) {
|
|
| 356 |
while (($rec_key = $res_keys->fetchRow(MYSQL_ASSOC))) {
|
|
| 357 |
if ( $rec_key['Key_name'] == $index_name ) {
|
|
| 288 | 358 |
$keys++; |
| 289 | 359 |
} |
| 290 | 360 |
} |
| 291 | 361 |
|
| 292 | 362 |
} |
| 293 |
if( $number_fields == 0 ) |
|
| 294 |
{
|
|
| 363 |
if ( $number_fields == 0 ) {
|
|
| 295 | 364 |
return ($keys != $number_fields); |
| 296 |
}else |
|
| 297 |
{
|
|
| 365 |
} else {
|
|
| 298 | 366 |
return ($keys == $number_fields); |
| 299 | 367 |
} |
| 300 | 368 |
} |
| 301 |
|
|
| 369 |
/** |
|
| 370 |
* Alias for addField() |
|
| 371 |
*/ |
|
| 372 |
public function field_add($table_name, $field_name, $description) |
|
| 373 |
{
|
|
| 374 |
return $this->addField($table_name, $field_name, $description); |
|
| 375 |
} |
|
| 302 | 376 |
/* |
| 303 | 377 |
* @param string full name of the table (incl. TABLE_PREFIX) |
| 304 | 378 |
* @param string name of the field to add |
| 305 | 379 |
* @param string describes the new field like ( INT NOT NULL DEFAULT '0') |
| 306 | 380 |
* @return bool true if successful, otherwise false and error will be set |
| 307 | 381 |
*/ |
| 308 |
public function field_add($table_name, $field_name, $description)
|
|
| 382 |
public function addField($table_name, $field_name, $description)
|
|
| 309 | 383 |
{
|
| 310 |
if( !$this->field_exists($table_name, $field_name) )
|
|
| 311 |
{ // add new field into a table
|
|
| 384 |
if (!$this->isField($table_name, $field_name)) {
|
|
| 385 |
// add new field into a table |
|
| 312 | 386 |
$sql = 'ALTER TABLE `'.$table_name.'` ADD '.$field_name.' '.$description.' '; |
| 313 |
$query = $this->query($sql, $this->_db_handle); |
|
| 314 |
$this->set_error(mysql_error($this->_db_handle)); |
|
| 315 |
if( !$this->is_error() ) |
|
| 316 |
{
|
|
| 317 |
return ( $this->field_exists($table_name, $field_name) ) ? true : false; |
|
| 387 |
$query = $this->doQuery($sql, $this->oDbHandle); |
|
| 388 |
$this->set_error(mysql_error($this->oDbHandle)); |
|
| 389 |
if (!$this->isError()) {
|
|
| 390 |
return ( $this->isField($table_name, $field_name) ) ? true : false; |
|
| 318 | 391 |
} |
| 319 |
}else |
|
| 320 |
{
|
|
| 392 |
} else {
|
|
| 321 | 393 |
$this->set_error('field \''.$field_name.'\' already exists');
|
| 322 | 394 |
} |
| 323 | 395 |
return false; |
| 324 | 396 |
} |
| 325 |
|
|
| 397 |
/** |
|
| 398 |
* Alias for modifyField() |
|
| 399 |
*/ |
|
| 400 |
public function field_modify($table_name, $field_name, $description) |
|
| 401 |
{
|
|
| 402 |
return $this->modifyField($table_name, $field_name, $description); |
|
| 403 |
} |
|
| 326 | 404 |
/* |
| 327 | 405 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX) |
| 328 | 406 |
* @param string $field_name: name of the field to add |
| 329 | 407 |
* @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0') |
| 330 | 408 |
* @return bool: true if successful, otherwise false and error will be set |
| 331 | 409 |
*/ |
| 332 |
public function field_modify($table_name, $field_name, $description)
|
|
| 410 |
public function modifyField($table_name, $field_name, $description)
|
|
| 333 | 411 |
{
|
| 334 | 412 |
$retval = false; |
| 335 |
if( $this->field_exists($table_name, $field_name) )
|
|
| 336 |
{ // modify a existing field in a table
|
|
| 413 |
if ($this->isField($table_name, $field_name)) {
|
|
| 414 |
// modify a existing field in a table |
|
| 337 | 415 |
$sql = 'ALTER TABLE `'.$table_name.'` MODIFY `'.$field_name.'` '.$description; |
| 338 |
$retval = ( $this->query($sql, $this->_db_handle) ? true : false);
|
|
| 339 |
$this->set_error(mysql_error());
|
|
| 416 |
$retval = ( $this->doQuery($sql, $this->oDbHandle) ? true : false);
|
|
| 417 |
$this->setError(mysql_error());
|
|
| 340 | 418 |
} |
| 341 | 419 |
return $retval; |
| 342 | 420 |
} |
| 343 |
|
|
| 421 |
/** |
|
| 422 |
* Alias for removeField() |
|
| 423 |
*/ |
|
| 424 |
public function field_remove($table_name, $field_name) |
|
| 425 |
{
|
|
| 426 |
return $this->removeField($table_name, $field_name); |
|
| 427 |
} |
|
| 344 | 428 |
/* |
| 345 | 429 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX) |
| 346 | 430 |
* @param string $field_name: name of the field to remove |
| 347 | 431 |
* @return bool: true if successful, otherwise false and error will be set |
| 348 | 432 |
*/ |
| 349 |
public function field_remove($table_name, $field_name)
|
|
| 433 |
public function removeField($table_name, $field_name)
|
|
| 350 | 434 |
{
|
| 351 | 435 |
$retval = false; |
| 352 |
if( $this->field_exists($table_name, $field_name) )
|
|
| 353 |
{ // modify a existing field in a table
|
|
| 436 |
if ($this->isField($table_name, $field_name)) {
|
|
| 437 |
// modify a existing field in a table |
|
| 354 | 438 |
$sql = 'ALTER TABLE `'.$table_name.'` DROP `'.$field_name.'`'; |
| 355 |
$retval = ( $this->query($sql, $this->_db_handle) ? true : false );
|
|
| 439 |
$retval = ( $this->doQuery($sql, $this->oDbHandle) ? true : false );
|
|
| 356 | 440 |
} |
| 357 | 441 |
return $retval; |
| 358 | 442 |
} |
| 359 |
|
|
| 443 |
/** |
|
| 444 |
* Alias for addIndex() |
|
| 445 |
*/ |
|
| 446 |
public function index_add($table_name, $index_name, $field_list, $index_type = 'KEY') |
|
| 447 |
{
|
|
| 448 |
return $this->addIndex($table_name, $index_name, $field_list, $index_type); |
|
| 449 |
} |
|
| 360 | 450 |
/* |
| 361 | 451 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX) |
| 362 | 452 |
* @param string $index_name: name of the new index (empty string for PRIMARY) |
| ... | ... | |
| 364 | 454 |
* @param string $index_type: kind of index (PRIMARY, UNIQUE, KEY, FULLTEXT) |
| 365 | 455 |
* @return bool: true if successful, otherwise false and error will be set |
| 366 | 456 |
*/ |
| 367 |
public function index_add($table_name, $index_name, $field_list, $index_type = 'KEY')
|
|
| 457 |
public function addIndex($table_name, $index_name, $field_list, $index_type = 'KEY')
|
|
| 368 | 458 |
{
|
| 369 | 459 |
$retval = false; |
| 370 | 460 |
$field_list = explode(',', (str_replace(' ', '', $field_list)));
|
| 371 | 461 |
$number_fields = sizeof($field_list); |
| 372 | 462 |
$field_list = '`'.implode('`,`', $field_list).'`';
|
| 373 | 463 |
$index_name = $index_type == 'PRIMARY' ? $index_type : $index_name; |
| 374 |
if( $this->index_exists($table_name, $index_name, $number_fields) ||
|
|
| 375 |
$this->index_exists($table_name, $index_name))
|
|
| 464 |
if ( $this->isIndex($table_name, $index_name, $number_fields) ||
|
|
| 465 |
$this->isIndex($table_name, $index_name))
|
|
| 376 | 466 |
{
|
| 377 | 467 |
$sql = 'ALTER TABLE `'.$table_name.'` '; |
| 378 | 468 |
$sql .= 'DROP INDEX `'.$index_name.'`'; |
| 379 |
if( !$this->query($sql, $this->_db_handle)) { return false; }
|
|
| 469 |
if (!$this->doQuery($sql, $this->oDbHandle)) { return false; }
|
|
| 380 | 470 |
} |
| 381 | 471 |
$sql = 'ALTER TABLE `'.$table_name.'` '; |
| 382 | 472 |
$sql .= 'ADD '.$index_type.' '; |
| 383 | 473 |
$sql .= $index_type == 'PRIMARY' ? 'KEY ' : '`'.$index_name.'` '; |
| 384 | 474 |
$sql .= '( '.$field_list.' ); '; |
| 385 |
if( $this->query($sql, $this->_db_handle)) { $retval = true; }
|
|
| 475 |
if ($this->doQuery($sql, $this->oDbHandle)) { $retval = true; }
|
|
| 386 | 476 |
return $retval; |
| 387 | 477 |
} |
| 388 |
|
|
| 478 |
/** |
|
| 479 |
* Alias for removeIndex() |
|
| 480 |
*/ |
|
| 481 |
public function index_remove($table_name, $index_name) |
|
| 482 |
{
|
|
| 483 |
return $this->removeIndex($table_name, $index_name); |
|
| 484 |
} |
|
| 389 | 485 |
/* |
| 390 | 486 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX) |
| 391 | 487 |
* @param string $field_name: name of the field to remove |
| 392 | 488 |
* @return bool: true if successful, otherwise false and error will be set |
| 393 | 489 |
*/ |
| 394 |
public function index_remove($table_name, $index_name)
|
|
| 490 |
public function removeIndex($table_name, $index_name)
|
|
| 395 | 491 |
{
|
| 396 | 492 |
$retval = false; |
| 397 |
if( $this->index_exists($table_name, $index_name) )
|
|
| 398 |
{ // modify a existing field in a table
|
|
| 493 |
if ($this->isIndex($table_name, $index_name)) {
|
|
| 494 |
// modify a existing field in a table |
|
| 399 | 495 |
$sql = 'ALTER TABLE `'.$table_name.'` DROP INDEX `'.$index_name.'`'; |
| 400 |
$retval = ( $this->query($sql, $this->_db_handle) ? true : false );
|
|
| 496 |
$retval = ( $this->doQuery($sql, $this->oDbHandle) ? true : false );
|
|
| 401 | 497 |
} |
| 402 | 498 |
return $retval; |
| 403 | 499 |
} |
| 404 |
|
|
| 405 | 500 |
/** |
| 501 |
* Alias for importSql() |
|
| 502 |
*/ |
|
| 503 |
public function SqlImport($sSqlDump, |
|
| 504 |
$sTablePrefix = '', |
|
| 505 |
$bPreserve = true, |
|
| 506 |
$sEngine = 'MyISAM', |
|
| 507 |
$sCollation = 'utf8_unicode_ci') |
|
| 508 |
{
|
|
| 509 |
return $this->importSql($sSqlDump, $sTablePrefix, $bPreserve, $sEngine, $sCollation); |
|
| 510 |
} |
|
| 511 |
/** |
|
| 406 | 512 |
* Import a standard *.sql dump file |
| 407 | 513 |
* @param string $sSqlDump link to the sql-dumpfile |
| 408 | 514 |
* @param string $sTablePrefix |
| ... | ... | |
| 410 | 516 |
* @param string $sEngine can be 'MyISAM' or 'InnoDB' |
| 411 | 517 |
* @param string $sCollation one of the list of available collations |
| 412 | 518 |
* @return boolean true if import successful |
| 519 |
* @description Import a standard *.sql dump file<br /> |
|
| 520 |
* The file can include placeholders TABLE_PREFIX, TABLE_COLLATION and TABLE_ENGINE |
|
| 413 | 521 |
*/ |
| 414 |
public function SqlImport($sSqlDump,
|
|
| 415 |
$sTablePrefix = '', |
|
| 522 |
public function importSql($sSqlDump,
|
|
| 523 |
$sTablePrefix = '', /* unused argument, for backward compatibility only! */
|
|
| 416 | 524 |
$bPreserve = true, |
| 417 | 525 |
$sEngine = 'MyISAM', |
| 418 | 526 |
$sCollation = 'utf8_unicode_ci') |
| ... | ... | |
| 429 | 537 |
$aSql = file($sSqlDump); |
| 430 | 538 |
// $aSql[0] = preg_replace('/^\xEF\xBB\xBF/', '', $aSql[0]);
|
| 431 | 539 |
$aSql[0] = preg_replace('/^[\xAA-\xFF]{3}/', '', $aSql[0]);
|
| 432 |
while ( sizeof($aSql) > 0 ) {
|
|
| 540 |
while (sizeof($aSql) > 0) {
|
|
| 433 | 541 |
$sSqlLine = trim(array_shift($aSql)); |
| 434 | 542 |
if (!preg_match('/^[-\/]+.*/', $sSqlLine)) {
|
| 435 | 543 |
$sql = $sql.' '.$sSqlLine; |
| 436 | 544 |
if ((substr($sql,-1,1) == ';')) {
|
| 437 | 545 |
$sql = trim(str_replace( $aSearch, $aReplace, $sql)); |
| 438 | 546 |
if (!($bPreserve && preg_match('/^\s*DROP TABLE IF EXISTS/siU', $sql))) {
|
| 439 |
if(!mysql_query($sql, $this->_db_handle)) {
|
|
| 547 |
if (!mysql_query($sql, $this->oDbHandle)) {
|
|
| 440 | 548 |
$retval = false; |
| 441 |
$this->error = mysql_error($this->_db_handle);
|
|
| 549 |
$this->error = mysql_error($this->oDbHandle);
|
|
| 442 | 550 |
unset($aSql); |
| 443 | 551 |
break; |
| 444 | 552 |
} |
| ... | ... | |
| 449 | 557 |
} |
| 450 | 558 |
return $retval; |
| 451 | 559 |
} |
| 452 |
|
|
| 453 | 560 |
/** |
| 454 | 561 |
* retuns the type of the engine used for requested table |
| 455 | 562 |
* @param string $table name of the table, including prefix |
| ... | ... | |
| 458 | 565 |
public function getTableEngine($table) |
| 459 | 566 |
{
|
| 460 | 567 |
$retVal = false; |
| 461 |
$mysqlVersion = mysql_get_server_info($this->_db_handle);
|
|
| 568 |
$mysqlVersion = mysql_get_server_info($this->oDbHandle);
|
|
| 462 | 569 |
$engineValue = (version_compare($mysqlVersion, '5.0') < 0) ? 'Type' : 'Engine'; |
| 463 |
$sql = 'SHOW TABLE STATUS FROM `' . $this->_db_name . '` LIKE \'' . $table . '\'';
|
|
| 464 |
if(($result = $this->query($sql, $this->_db_handle))) {
|
|
| 465 |
if(($row = $result->fetchRow(MYSQL_ASSOC))) {
|
|
| 570 |
$sql = 'SHOW TABLE STATUS FROM `' . $this->sDbName . '` LIKE \'' . $table . '\'';
|
|
| 571 |
if (($result = $this->doQuery($sql, $this->oDbHandle))) {
|
|
| 572 |
if (($row = $result->fetchRow(MYSQL_ASSOC))) {
|
|
| 466 | 573 |
$retVal = $row[$engineValue]; |
| 467 | 574 |
} |
| 468 | 575 |
} |
| ... | ... | |
| 477 | 584 |
* |
| 478 | 585 |
* @category Core |
| 479 | 586 |
* @package Core_database |
| 480 |
* @author Werner v.d.Decken <wkl@isteam.de>
|
|
| 481 |
* @copyright Werner v.d.Decken <wkl@isteam.de>
|
|
| 587 |
* @author Manuela v.d.Decken <manuela@isteam.de>
|
|
| 588 |
* @copyright Manuela v.d.Decken <manuela@isteam.de>
|
|
| 482 | 589 |
* @license http://www.gnu.org/licenses/gpl.html GPL License |
| 483 | 590 |
* @version 2.9.0 |
| 484 | 591 |
* @revision $Revision$ |
| ... | ... | |
| 487 | 594 |
*/ |
| 488 | 595 |
class WbDatabaseException extends AppException {}
|
| 489 | 596 |
|
| 490 |
define('MYSQL_SEEK_FIRST', 0);
|
|
| 491 |
define('MYSQL_SEEK_LAST', -1);
|
|
| 597 |
/* extend global constants of mysql */ |
|
| 598 |
if(!defined('MYSQL_SEEK_FIRST')) { define('MYSQL_SEEK_FIRST', 0); }
|
|
| 599 |
if(!defined('MYSQL_SEEK_LAST')) { define('MYSQL_SEEK_LAST', -1); }
|
|
| 492 | 600 |
|
| 601 |
/** |
|
| 602 |
* mysql |
|
| 603 |
* |
|
| 604 |
* @category Core |
|
| 605 |
* @package Core_database |
|
| 606 |
* @author Manuela v.d.Decken <manuela@isteam.de> |
|
| 607 |
* @copyright Manuela v.d.Decken <manuela@isteam.de> |
|
| 608 |
* @license http://www.gnu.org/licenses/gpl.html GPL License |
|
| 609 |
* @version 2.9.0 |
|
| 610 |
* @revision $Revision$ |
|
| 611 |
* @lastmodified $Date$ |
|
| 612 |
* @description MYSQL result object for requests |
|
| 613 |
* |
|
| 614 |
*/ |
|
| 493 | 615 |
class mysql {
|
| 494 | 616 |
|
| 495 | 617 |
private $result = null; |
| 496 | 618 |
private $_db_handle = null; |
| 497 |
// Run a query |
|
| 498 |
function query($statement, $dbHandle) {
|
|
| 499 |
$this->_db_handle = $dbHandle; |
|
| 500 |
$this->result = @mysql_query($statement, $this->_db_handle); |
|
| 501 |
if($this->result === false) {
|
|
| 502 |
if(DEBUG) {
|
|
| 503 |
throw new WbDatabaseException(mysql_error($this->_db_handle)); |
|
| 504 |
}else{
|
|
| 619 |
|
|
| 620 |
/** |
|
| 621 |
* query sql statement |
|
| 622 |
* @param string $statement |
|
| 623 |
* @param object $dbHandle |
|
| 624 |
* @return object |
|
| 625 |
* @throws WbDatabaseException |
|
| 626 |
*/ |
|
| 627 |
function query($statement, $dbHandle) |
|
| 628 |
{
|
|
| 629 |
$this->oDbHandle = $dbHandle; |
|
| 630 |
$this->result = @mysql_query($statement, $this->oDbHandle); |
|
| 631 |
if ($this->result === false) {
|
|
| 632 |
if (DEBUG) {
|
|
| 633 |
throw new WbDatabaseException(mysql_error($this->oDbHandle)); |
|
| 634 |
} else {
|
|
| 505 | 635 |
throw new WbDatabaseException('Error in SQL-Statement');
|
| 506 | 636 |
} |
| 507 | 637 |
} |
| 508 |
$this->error = mysql_error($this->_db_handle);
|
|
| 638 |
$this->error = mysql_error($this->oDbHandle);
|
|
| 509 | 639 |
return $this->result; |
| 510 | 640 |
} |
| 511 |
|
|
| 512 |
// Fetch num rows |
|
| 513 |
function numRows() {
|
|
| 641 |
/** |
|
| 642 |
* numRows |
|
| 643 |
* @return integer |
|
| 644 |
* @description number of returned records |
|
| 645 |
*/ |
|
| 646 |
function numRows() |
|
| 647 |
{
|
|
| 514 | 648 |
return mysql_num_rows($this->result); |
| 515 | 649 |
} |
| 516 |
|
|
| 517 |
// Fetch row $typ = MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH |
|
| 518 |
function fetchRow($typ = MYSQL_BOTH) {
|
|
| 650 |
/** |
|
| 651 |
* fetchRow |
|
| 652 |
* @param int $typ MYSQL_BOTH(default) | MYSQL_ASSOC | MYSQL_NUM |
|
| 653 |
* @return array |
|
| 654 |
* @description get current record and increment pointer |
|
| 655 |
*/ |
|
| 656 |
function fetchRow($typ = MYSQL_BOTH) |
|
| 657 |
{
|
|
| 519 | 658 |
return mysql_fetch_array($this->result, $typ); |
| 520 | 659 |
} |
| 521 |
|
|
| 660 |
/** |
|
| 661 |
* fetchObject |
|
| 662 |
* @param string $sClassname Name of the class to use. Is no given use stdClass |
|
| 663 |
* @param string $aParams optional array of arguments for the constructor |
|
| 664 |
* @return object |
|
| 665 |
* @description get current record as an object and increment pointer |
|
| 666 |
*/ |
|
| 667 |
function fetchObject($sClassName = null, array $aParams = null) |
|
| 668 |
{
|
|
| 669 |
return mysql_fetch_object($this->result, $sClassName, $aParams); |
|
| 670 |
} |
|
| 671 |
/** |
|
| 672 |
* rewind |
|
| 673 |
* @return bool |
|
| 674 |
* @description set the recordpointer to the first record || false on error |
|
| 675 |
*/ |
|
| 522 | 676 |
function rewind() |
| 523 | 677 |
{
|
| 524 |
return $this->seekRow(); |
|
| 678 |
return $this->seekRow(MYSQL_SEEK_FIRST);
|
|
| 525 | 679 |
} |
| 526 |
|
|
| 680 |
/** |
|
| 681 |
* seekRow |
|
| 682 |
* @param int $position |
|
| 683 |
* @return bool |
|
| 684 |
* @description set the pointer to the given record || false on error |
|
| 685 |
*/ |
|
| 527 | 686 |
function seekRow( $position = MYSQL_SEEK_FIRST ) |
| 528 | 687 |
{
|
| 529 | 688 |
$pmax = $this->numRows() - 1; |
| 530 | 689 |
$p = (($position < 0 || $position > $pmax) ? $pmax : $position); |
| 531 | 690 |
return mysql_data_seek($this->result, $p); |
| 532 | 691 |
} |
| 533 |
|
|
| 534 |
// Get error |
|
| 535 |
function error() {
|
|
| 536 |
if(isset($this->error)) {
|
|
| 692 |
/** |
|
| 693 |
* freeResult |
|
| 694 |
* @return bool |
|
| 695 |
* @description remove retult object from memeory |
|
| 696 |
*/ |
|
| 697 |
function freeResult() |
|
| 698 |
{
|
|
| 699 |
return mysql_free_result($this->result); |
|
| 700 |
} |
|
| 701 |
/** |
|
| 702 |
* Get error |
|
| 703 |
* @return string || null if no error |
|
| 704 |
*/ |
|
| 705 |
function error() |
|
| 706 |
{
|
|
| 707 |
if (isset($this->error)) {
|
|
| 537 | 708 |
return $this->error; |
| 538 | 709 |
} else {
|
| 539 | 710 |
return null; |
| ... | ... | |
| 553 | 724 |
*/ |
| 554 | 725 |
function db_update_key_value($table, $key, $value = '') |
| 555 | 726 |
{
|
| 556 |
global $database; |
|
| 557 |
if( !is_array($key)) |
|
| 558 |
{
|
|
| 559 |
if( trim($key) != '' ) |
|
| 560 |
{
|
|
| 727 |
$oDb = WbDatabase::getInstance(); |
|
| 728 |
if (!is_array($key)) {
|
|
| 729 |
if (trim($key) != '') {
|
|
| 561 | 730 |
$key = array( trim($key) => trim($value) ); |
| 562 | 731 |
} else {
|
| 563 | 732 |
$key = array(); |
| ... | ... | |
| 568 | 737 |
{
|
| 569 | 738 |
$index = strtolower($index); |
| 570 | 739 |
$sql = 'SELECT COUNT(`setting_id`) ' |
| 571 |
. 'FROM `'.TABLE_PREFIX.$table.'` '
|
|
| 740 |
. 'FROM `'.$oDb->TablePrefix.$table.'` '
|
|
| 572 | 741 |
. 'WHERE `name` = \''.$index.'\' '; |
| 573 |
if($database->get_one($sql)) |
|
| 574 |
{
|
|
| 742 |
if ($oDb->getOne($sql)) {
|
|
| 575 | 743 |
$sql = 'UPDATE '; |
| 576 | 744 |
$sql_where = 'WHERE `name` = \''.$index.'\''; |
| 577 |
}else {
|
|
| 745 |
} else {
|
|
| 578 | 746 |
$sql = 'INSERT INTO '; |
| 579 | 747 |
$sql_where = ''; |
| 580 | 748 |
} |
| 581 |
$sql .= '`'.TABLE_PREFIX.$table.'` ';
|
|
| 749 |
$sql .= '`'.$oDb->TablePrefix.$table.'` ';
|
|
| 582 | 750 |
$sql .= 'SET `name` = \''.$index.'\', '; |
| 583 | 751 |
$sql .= '`value` = \''.$val.'\' '.$sql_where; |
| 584 |
if( !$database->query($sql) ) |
|
| 585 |
{
|
|
| 752 |
if (!$oDb->doQuery($sql)) {
|
|
| 586 | 753 |
$retval = false; |
| 587 | 754 |
} |
| 588 | 755 |
} |
Also available in: Unified diff
+ WbDatabase added new methods fetchObject(), rewind(), seekRow(), freeResult()
! renamed some methods and add aliases for the old names