| 1 |
2120
|
darkviper
|
<?php
|
| 2 |
|
|
/**
|
| 3 |
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
| 4 |
|
|
*
|
| 5 |
|
|
* This program is free software: you can redistribute it and/or modify
|
| 6 |
|
|
* it under the terms of the GNU General Public License as published by
|
| 7 |
|
|
* the Free Software Foundation, either version 3 of the License, or
|
| 8 |
|
|
* (at your option) any later version.
|
| 9 |
|
|
*
|
| 10 |
|
|
* This program is distributed in the hope that it will be useful,
|
| 11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 13 |
|
|
* GNU General Public License for more details.
|
| 14 |
|
|
*
|
| 15 |
|
|
* You should have received a copy of the GNU General Public License
|
| 16 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
| 17 |
|
|
*/
|
| 18 |
|
|
/**
|
| 19 |
|
|
* WbDatabaseHelper.php
|
| 20 |
|
|
*
|
| 21 |
|
|
* @category Core
|
| 22 |
|
|
* @package Core_database
|
| 23 |
|
|
* @author Manuela v.d.Decken <manuela@isteam.de>
|
| 24 |
|
|
* @author Dietmar W. <dietmar.woellbrink@websitebaker.org>
|
| 25 |
|
|
* @copyright Manuela v.d.Decken <manuela@isteam.de>
|
| 26 |
|
|
* @license http://www.gnu.org/licenses/gpl.html GPL License
|
| 27 |
|
|
* @version 0.0.9
|
| 28 |
|
|
* @revision $Revision: 2105 $
|
| 29 |
|
|
* @lastmodified $Date: 2014-11-24 18:41:13 +0100 (Mo, 24 Nov 2014) $
|
| 30 |
|
|
* @deprecated from WB version number 2.9
|
| 31 |
|
|
* @description Mysql database wrapper for use with websitebaker up to version 2.8.4
|
| 32 |
|
|
*/
|
| 33 |
|
|
|
| 34 |
|
|
abstract class WbDatabaseHelper {
|
| 35 |
|
|
|
| 36 |
|
|
|
| 37 |
|
|
/**
|
| 38 |
|
|
* Alias for isField()
|
| 39 |
|
|
* @deprecated from WB-2.8.5 and higher
|
| 40 |
|
|
*/
|
| 41 |
|
|
public function field_exists($table_name, $field_name)
|
| 42 |
|
|
{
|
| 43 |
|
|
return $this->isField($table_name, $field_name);
|
| 44 |
|
|
}
|
| 45 |
|
|
/*
|
| 46 |
|
|
* @param string full name of the table (incl. TABLE_PREFIX)
|
| 47 |
|
|
* @param string name of the field to seek for
|
| 48 |
|
|
* @return bool true if field exists
|
| 49 |
|
|
*/
|
| 50 |
|
|
public function isField($table_name, $field_name)
|
| 51 |
|
|
{
|
| 52 |
|
|
$sql = 'DESCRIBE `'.$table_name.'` `'.$field_name.'` ';
|
| 53 |
|
|
$query = $this->doQuery($sql);
|
| 54 |
|
|
return ($query->numRows() != 0);
|
| 55 |
|
|
}
|
| 56 |
|
|
/**
|
| 57 |
|
|
* Alias for isIndex()
|
| 58 |
|
|
* @deprecated from WB-2.8.5 and higher
|
| 59 |
|
|
*/
|
| 60 |
|
|
public function index_exists($table_name, $index_name, $number_fields = 0)
|
| 61 |
|
|
{
|
| 62 |
|
|
return $this->isIndex($table_name, $index_name, $number_fields = 0);
|
| 63 |
|
|
}
|
| 64 |
|
|
/*
|
| 65 |
|
|
* isIndex
|
| 66 |
|
|
* @param string full name of the table (incl. TABLE_PREFIX)
|
| 67 |
|
|
* @param string name of the index to seek for
|
| 68 |
|
|
* @return bool true if field exists
|
| 69 |
|
|
*/
|
| 70 |
|
|
public function isIndex($table_name, $index_name, $number_fields = 0)
|
| 71 |
|
|
{
|
| 72 |
|
|
$number_fields = intval($number_fields);
|
| 73 |
|
|
$keys = 0;
|
| 74 |
|
|
$sql = 'SHOW INDEX FROM `'.$table_name.'`';
|
| 75 |
|
|
if (($res_keys = $this->doQuery($sql))) {
|
| 76 |
|
|
while (($rec_key = $res_keys->fetchRow(MYSQL_ASSOC))) {
|
| 77 |
|
|
if ( $rec_key['Key_name'] == $index_name ) {
|
| 78 |
|
|
$keys++;
|
| 79 |
|
|
}
|
| 80 |
|
|
}
|
| 81 |
|
|
|
| 82 |
|
|
}
|
| 83 |
|
|
if ( $number_fields == 0 ) {
|
| 84 |
|
|
return ($keys != $number_fields);
|
| 85 |
|
|
} else {
|
| 86 |
|
|
return ($keys == $number_fields);
|
| 87 |
|
|
}
|
| 88 |
|
|
}
|
| 89 |
|
|
/**
|
| 90 |
|
|
* Alias for addField()
|
| 91 |
|
|
* @deprecated from WB-2.8.5 and higher
|
| 92 |
|
|
*/
|
| 93 |
|
|
public function field_add($table_name, $field_name, $description)
|
| 94 |
|
|
{
|
| 95 |
|
|
return $this->addField($table_name, $field_name, $description);
|
| 96 |
|
|
}
|
| 97 |
|
|
/*
|
| 98 |
|
|
* @param string full name of the table (incl. TABLE_PREFIX)
|
| 99 |
|
|
* @param string name of the field to add
|
| 100 |
|
|
* @param string describes the new field like ( INT NOT NULL DEFAULT '0')
|
| 101 |
|
|
* @return bool true if successful, otherwise false and error will be set
|
| 102 |
|
|
*/
|
| 103 |
|
|
public function addField($table_name, $field_name, $description)
|
| 104 |
|
|
{
|
| 105 |
|
|
if (!$this->isField($table_name, $field_name)) {
|
| 106 |
|
|
// add new field into a table
|
| 107 |
|
|
$sql = 'ALTER TABLE `'.$table_name.'` ADD '.$field_name.' '.$description.' ';
|
| 108 |
|
|
$query = $this->doQuery($sql);
|
| 109 |
|
|
$this->set_error(mysqli_error($this->oDbHandle));
|
| 110 |
|
|
if (!$this->isError()) {
|
| 111 |
|
|
return ( $this->isField($table_name, $field_name) ) ? true : false;
|
| 112 |
|
|
}
|
| 113 |
|
|
} else {
|
| 114 |
|
|
$this->set_error('field \''.$field_name.'\' already exists');
|
| 115 |
|
|
}
|
| 116 |
|
|
return false;
|
| 117 |
|
|
}
|
| 118 |
|
|
/**
|
| 119 |
|
|
* Alias for modifyField()
|
| 120 |
|
|
* @deprecated from WB-2.8.5 and higher
|
| 121 |
|
|
*/
|
| 122 |
|
|
public function field_modify($table_name, $field_name, $description)
|
| 123 |
|
|
{
|
| 124 |
|
|
return $this->modifyField($table_name, $field_name, $description);
|
| 125 |
|
|
}
|
| 126 |
|
|
/*
|
| 127 |
|
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
| 128 |
|
|
* @param string $field_name: name of the field to add
|
| 129 |
|
|
* @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
|
| 130 |
|
|
* @return bool: true if successful, otherwise false and error will be set
|
| 131 |
|
|
*/
|
| 132 |
|
|
public function modifyField($table_name, $field_name, $description)
|
| 133 |
|
|
{
|
| 134 |
|
|
$retval = false;
|
| 135 |
|
|
if ($this->isField($table_name, $field_name)) {
|
| 136 |
|
|
// modify a existing field in a table
|
| 137 |
|
|
$sql = 'ALTER TABLE `'.$table_name.'` MODIFY `'.$field_name.'` '.$description;
|
| 138 |
|
|
$retval = ( $this->doQuery($sql) ? true : false);
|
| 139 |
|
|
$this->setError(mysqli_error($this->oDbHandle));
|
| 140 |
|
|
}
|
| 141 |
|
|
return $retval;
|
| 142 |
|
|
}
|
| 143 |
|
|
/**
|
| 144 |
|
|
* Alias for removeField()
|
| 145 |
|
|
* @deprecated from WB-2.8.5 and higher
|
| 146 |
|
|
*/
|
| 147 |
|
|
public function field_remove($table_name, $field_name)
|
| 148 |
|
|
{
|
| 149 |
|
|
return $this->removeField($table_name, $field_name);
|
| 150 |
|
|
}
|
| 151 |
|
|
/*
|
| 152 |
|
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
| 153 |
|
|
* @param string $field_name: name of the field to remove
|
| 154 |
|
|
* @return bool: true if successful, otherwise false and error will be set
|
| 155 |
|
|
*/
|
| 156 |
|
|
public function removeField($table_name, $field_name)
|
| 157 |
|
|
{
|
| 158 |
|
|
$retval = false;
|
| 159 |
|
|
if ($this->isField($table_name, $field_name)) {
|
| 160 |
|
|
// modify a existing field in a table
|
| 161 |
|
|
$sql = 'ALTER TABLE `'.$table_name.'` DROP `'.$field_name.'`';
|
| 162 |
|
|
$retval = ( $this->doQuery($sql, $this->oDbHandle) ? true : false );
|
| 163 |
|
|
}
|
| 164 |
|
|
return $retval;
|
| 165 |
|
|
}
|
| 166 |
|
|
/**
|
| 167 |
|
|
* Alias for addIndex()
|
| 168 |
|
|
* @deprecated from WB-2.8.5 and higher
|
| 169 |
|
|
*/
|
| 170 |
|
|
public function index_add($table_name, $index_name, $field_list, $index_type = 'KEY')
|
| 171 |
|
|
{
|
| 172 |
|
|
return $this->addIndex($table_name, $index_name, $field_list, $index_type);
|
| 173 |
|
|
}
|
| 174 |
|
|
/*
|
| 175 |
|
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
| 176 |
|
|
* @param string $index_name: name of the new index (empty string for PRIMARY)
|
| 177 |
|
|
* @param string $field_list: comma seperated list of fields for this index
|
| 178 |
|
|
* @param string $index_type: kind of index (PRIMARY, UNIQUE, KEY, FULLTEXT)
|
| 179 |
|
|
* @return bool: true if successful, otherwise false and error will be set
|
| 180 |
|
|
*/
|
| 181 |
|
|
public function addIndex($table_name, $index_name, $field_list, $index_type = 'KEY')
|
| 182 |
|
|
{
|
| 183 |
|
|
$retval = false;
|
| 184 |
|
|
$field_list = explode(',', (str_replace(' ', '', $field_list)));
|
| 185 |
|
|
$number_fields = sizeof($field_list);
|
| 186 |
|
|
$field_list = '`'.implode('`,`', $field_list).'`';
|
| 187 |
|
|
$index_name = $index_type == 'PRIMARY' ? $index_type : $index_name;
|
| 188 |
|
|
if ( $this->isIndex($table_name, $index_name, $number_fields) ||
|
| 189 |
|
|
$this->isIndex($table_name, $index_name))
|
| 190 |
|
|
{
|
| 191 |
|
|
$sql = 'ALTER TABLE `'.$table_name.'` ';
|
| 192 |
|
|
$sql .= 'DROP INDEX `'.$index_name.'`';
|
| 193 |
|
|
if (!$this->doQuery($sql)) { return false; }
|
| 194 |
|
|
}
|
| 195 |
|
|
$sql = 'ALTER TABLE `'.$table_name.'` ';
|
| 196 |
|
|
$sql .= 'ADD '.$index_type.' ';
|
| 197 |
|
|
$sql .= $index_type == 'PRIMARY' ? 'KEY ' : '`'.$index_name.'` ';
|
| 198 |
|
|
$sql .= '( '.$field_list.' ); ';
|
| 199 |
|
|
if ($this->doQuery($sql)) { $retval = true; }
|
| 200 |
|
|
return $retval;
|
| 201 |
|
|
}
|
| 202 |
|
|
/**
|
| 203 |
|
|
* Alias for removeIndex()
|
| 204 |
|
|
* @deprecated from WB-2.8.5 and higher
|
| 205 |
|
|
*/
|
| 206 |
|
|
public function index_remove($table_name, $index_name)
|
| 207 |
|
|
{
|
| 208 |
|
|
return $this->removeIndex($table_name, $index_name);
|
| 209 |
|
|
}
|
| 210 |
|
|
/*
|
| 211 |
|
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
| 212 |
|
|
* @param string $field_name: name of the field to remove
|
| 213 |
|
|
* @return bool: true if successful, otherwise false and error will be set
|
| 214 |
|
|
*/
|
| 215 |
|
|
public function removeIndex($table_name, $index_name)
|
| 216 |
|
|
{
|
| 217 |
|
|
$retval = false;
|
| 218 |
|
|
if ($this->isIndex($table_name, $index_name)) {
|
| 219 |
|
|
// modify a existing field in a table
|
| 220 |
|
|
$sql = 'ALTER TABLE `'.$table_name.'` DROP INDEX `'.$index_name.'`';
|
| 221 |
|
|
$retval = ( $this->doQuery($sql) ? true : false );
|
| 222 |
|
|
}
|
| 223 |
|
|
return $retval;
|
| 224 |
|
|
}
|
| 225 |
|
|
/**
|
| 226 |
|
|
* Alias for importSql()
|
| 227 |
|
|
* @deprecated from WB-2.8.5 and higher
|
| 228 |
|
|
*/
|
| 229 |
|
|
public function SqlImport($sSqlDump,
|
| 230 |
|
|
$sTablePrefix = '',
|
| 231 |
|
|
$sAction = 'install',
|
| 232 |
|
|
$sEngine = 'MyISAM',
|
| 233 |
|
|
$sCollation = 'utf8_unicode_ci')
|
| 234 |
|
|
{
|
| 235 |
|
|
return $this->importSql($sSqlDump, $sTablePrefix, $sAction, $sEngine, $sCollation);
|
| 236 |
|
|
}
|
| 237 |
|
|
|
| 238 |
|
|
/**
|
| 239 |
|
|
* retuns the type of the engine used for requested table
|
| 240 |
|
|
* @param string $table name of the table, including prefix
|
| 241 |
|
|
* @return boolean/string false on error, or name of the engine (myIsam/InnoDb)
|
| 242 |
|
|
*/
|
| 243 |
|
|
public function getTableEngine($table)
|
| 244 |
|
|
{
|
| 245 |
|
|
$retVal = false;
|
| 246 |
|
|
$mysqlVersion = mysqli_get_server_info($this->oDbHandle);
|
| 247 |
|
|
$engineValue = (version_compare($mysqlVersion, '5.0') < 0) ? 'Type' : 'Engine';
|
| 248 |
|
|
$sql = 'SHOW TABLE STATUS FROM `' . $this->sDbName . '` LIKE \'' . $table . '\'';
|
| 249 |
|
|
if (($result = $this->doQuery($sql))) {
|
| 250 |
|
|
if (($row = $result->fetchRow(MYSQL_ASSOC))) {
|
| 251 |
|
|
$retVal = $row[$engineValue];
|
| 252 |
|
|
}
|
| 253 |
|
|
}
|
| 254 |
|
|
return $retVal;
|
| 255 |
|
|
}
|
| 256 |
|
|
|
| 257 |
|
|
}
|
| 258 |
|
|
|
| 259 |
|
|
// end of class WbDatabaseHelper
|