Revision 2127
Added by darkviper over 9 years ago
WbDatabaseHelper.php | ||
---|---|---|
38 | 38 |
* Alias for isField() |
39 | 39 |
* @deprecated from WB-2.8.5 and higher |
40 | 40 |
*/ |
41 |
public function field_exists($table_name, $field_name) |
|
42 |
{ |
|
43 |
return $this->isField($table_name, $field_name); |
|
44 |
} |
|
41 |
public function field_exists($table_name, $field_name) |
|
42 |
{ |
|
43 |
trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED); |
|
44 |
return $this->isField($table_name, $field_name); |
|
45 |
} |
|
45 | 46 |
/* |
46 | 47 |
* @param string full name of the table (incl. TABLE_PREFIX) |
47 | 48 |
* @param string name of the field to seek for |
48 | 49 |
* @return bool true if field exists |
49 | 50 |
*/ |
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 |
}
|
|
51 |
public function isField($table_name, $field_name)
|
|
52 |
{
|
|
53 |
$sql = 'DESCRIBE `'.$table_name.'` `'.$field_name.'` ';
|
|
54 |
$query = $this->doQuery($sql);
|
|
55 |
return ($query->numRows() != 0);
|
|
56 |
}
|
|
56 | 57 |
/** |
57 | 58 |
* Alias for isIndex() |
58 | 59 |
* @deprecated from WB-2.8.5 and higher |
59 | 60 |
*/ |
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 |
} |
|
61 |
public function index_exists($table_name, $index_name, $number_fields = 0) |
|
62 |
{ |
|
63 |
trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED); |
|
64 |
return $this->isIndex($table_name, $index_name, $number_fields = 0); |
|
65 |
} |
|
64 | 66 |
/* |
65 | 67 |
* isIndex |
66 | 68 |
* @param string full name of the table (incl. TABLE_PREFIX) |
67 | 69 |
* @param string name of the index to seek for |
68 | 70 |
* @return bool true if field exists |
69 | 71 |
*/ |
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 |
}
|
|
72 |
public function isIndex($table_name, $index_name, $number_fields = 0)
|
|
73 |
{
|
|
74 |
$number_fields = intval($number_fields);
|
|
75 |
$keys = 0;
|
|
76 |
$sql = 'SHOW INDEX FROM `'.$table_name.'`';
|
|
77 |
if (($res_keys = $this->doQuery($sql))) {
|
|
78 |
while (($rec_key = $res_keys->fetchRow(MYSQL_ASSOC))) {
|
|
79 |
if ( $rec_key['Key_name'] == $index_name ) {
|
|
80 |
$keys++;
|
|
81 |
}
|
|
82 |
}
|
|
81 | 83 |
|
82 |
}
|
|
83 |
if ( $number_fields == 0 ) {
|
|
84 |
return ($keys != $number_fields);
|
|
85 |
} else {
|
|
86 |
return ($keys == $number_fields);
|
|
87 |
}
|
|
88 |
}
|
|
84 |
}
|
|
85 |
if ( $number_fields == 0 ) {
|
|
86 |
return ($keys != $number_fields);
|
|
87 |
} else {
|
|
88 |
return ($keys == $number_fields);
|
|
89 |
}
|
|
90 |
}
|
|
89 | 91 |
/** |
90 | 92 |
* Alias for addField() |
91 | 93 |
* @deprecated from WB-2.8.5 and higher |
92 | 94 |
*/ |
93 |
public function field_add($table_name, $field_name, $description) |
|
94 |
{ |
|
95 |
return $this->addField($table_name, $field_name, $description); |
|
96 |
} |
|
95 |
public function field_add($table_name, $field_name, $description) |
|
96 |
{ |
|
97 |
trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED); |
|
98 |
return $this->addField($table_name, $field_name, $description); |
|
99 |
} |
|
97 | 100 |
/* |
98 | 101 |
* @param string full name of the table (incl. TABLE_PREFIX) |
99 | 102 |
* @param string name of the field to add |
100 | 103 |
* @param string describes the new field like ( INT NOT NULL DEFAULT '0') |
101 | 104 |
* @return bool true if successful, otherwise false and error will be set |
102 | 105 |
*/ |
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 |
}
|
|
106 |
public function addField($table_name, $field_name, $description)
|
|
107 |
{
|
|
108 |
if (!$this->isField($table_name, $field_name)) {
|
|
109 |
// add new field into a table
|
|
110 |
$sql = 'ALTER TABLE `'.$table_name.'` ADD '.$field_name.' '.$description.' ';
|
|
111 |
$query = $this->doQuery($sql);
|
|
112 |
$this->set_error(mysqli_error($this->oDbHandle));
|
|
113 |
if (!$this->isError()) {
|
|
114 |
return ( $this->isField($table_name, $field_name) ) ? true : false;
|
|
115 |
}
|
|
116 |
} else {
|
|
117 |
$this->set_error('field \''.$field_name.'\' already exists');
|
|
118 |
}
|
|
119 |
return false;
|
|
120 |
}
|
|
118 | 121 |
/** |
119 | 122 |
* Alias for modifyField() |
120 | 123 |
* @deprecated from WB-2.8.5 and higher |
121 | 124 |
*/ |
122 |
public function field_modify($table_name, $field_name, $description) |
|
123 |
{ |
|
124 |
return $this->modifyField($table_name, $field_name, $description); |
|
125 |
} |
|
125 |
public function field_modify($table_name, $field_name, $description) |
|
126 |
{ |
|
127 |
trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED); |
|
128 |
return $this->modifyField($table_name, $field_name, $description); |
|
129 |
} |
|
126 | 130 |
/* |
127 | 131 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX) |
128 | 132 |
* @param string $field_name: name of the field to add |
129 | 133 |
* @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0') |
130 | 134 |
* @return bool: true if successful, otherwise false and error will be set |
131 | 135 |
*/ |
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 |
}
|
|
136 |
public function modifyField($table_name, $field_name, $description)
|
|
137 |
{
|
|
138 |
$retval = false;
|
|
139 |
if ($this->isField($table_name, $field_name)) {
|
|
140 |
// modify a existing field in a table
|
|
141 |
$sql = 'ALTER TABLE `'.$table_name.'` MODIFY `'.$field_name.'` '.$description;
|
|
142 |
$retval = ( $this->doQuery($sql) ? true : false);
|
|
143 |
$this->setError(mysqli_error($this->oDbHandle));
|
|
144 |
}
|
|
145 |
return $retval;
|
|
146 |
}
|
|
143 | 147 |
/** |
144 | 148 |
* Alias for removeField() |
145 | 149 |
* @deprecated from WB-2.8.5 and higher |
146 | 150 |
*/ |
147 |
public function field_remove($table_name, $field_name) |
|
148 |
{ |
|
149 |
return $this->removeField($table_name, $field_name); |
|
150 |
} |
|
151 |
public function field_remove($table_name, $field_name) |
|
152 |
{ |
|
153 |
trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED); |
|
154 |
return $this->removeField($table_name, $field_name); |
|
155 |
} |
|
151 | 156 |
/* |
152 | 157 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX) |
153 | 158 |
* @param string $field_name: name of the field to remove |
154 | 159 |
* @return bool: true if successful, otherwise false and error will be set |
155 | 160 |
*/ |
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 |
}
|
|
161 |
public function removeField($table_name, $field_name)
|
|
162 |
{
|
|
163 |
$retval = false;
|
|
164 |
if ($this->isField($table_name, $field_name)) {
|
|
165 |
// modify a existing field in a table
|
|
166 |
$sql = 'ALTER TABLE `'.$table_name.'` DROP `'.$field_name.'`';
|
|
167 |
$retval = ( $this->doQuery($sql, $this->oDbHandle) ? true : false );
|
|
168 |
}
|
|
169 |
return $retval;
|
|
170 |
}
|
|
166 | 171 |
/** |
167 | 172 |
* Alias for addIndex() |
168 | 173 |
* @deprecated from WB-2.8.5 and higher |
169 | 174 |
*/ |
170 | 175 |
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 |
} |
|
176 |
{ |
|
177 |
trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED); |
|
178 |
return $this->addIndex($table_name, $index_name, $field_list, $index_type); |
|
179 |
} |
|
174 | 180 |
/* |
175 | 181 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX) |
176 | 182 |
* @param string $index_name: name of the new index (empty string for PRIMARY) |
... | ... | |
178 | 184 |
* @param string $index_type: kind of index (PRIMARY, UNIQUE, KEY, FULLTEXT) |
179 | 185 |
* @return bool: true if successful, otherwise false and error will be set |
180 | 186 |
*/ |
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 |
}
|
|
187 |
public function addIndex($table_name, $index_name, $field_list, $index_type = 'KEY') |
|
188 |
{ |
|
189 |
$retval = false; |
|
190 |
$field_list = explode(',', (str_replace(' ', '', $field_list))); |
|
191 |
$number_fields = sizeof($field_list); |
|
192 |
$field_list = '`'.implode('`,`', $field_list).'`'; |
|
193 |
$index_name = $index_type == 'PRIMARY' ? $index_type : $index_name; |
|
194 |
if ( $this->isIndex($table_name, $index_name, $number_fields) || |
|
195 |
$this->isIndex($table_name, $index_name)) |
|
196 |
{ |
|
197 |
$sql = 'ALTER TABLE `'.$table_name.'` '; |
|
198 |
$sql .= 'DROP INDEX `'.$index_name.'`'; |
|
199 |
if (!$this->doQuery($sql)) { return false; } |
|
200 |
} |
|
201 |
$sql = 'ALTER TABLE `'.$table_name.'` '; |
|
202 |
$sql .= 'ADD '.$index_type.' '; |
|
203 |
$sql .= $index_type == 'PRIMARY' ? 'KEY ' : '`'.$index_name.'` '; |
|
204 |
$sql .= '( '.$field_list.' ); '; |
|
205 |
if ($this->doQuery($sql)) { $retval = true; } |
|
206 |
return $retval; |
|
207 |
} |
|
202 | 208 |
/** |
203 | 209 |
* Alias for removeIndex() |
204 | 210 |
* @deprecated from WB-2.8.5 and higher |
205 | 211 |
*/ |
206 |
public function index_remove($table_name, $index_name) |
|
207 |
{ |
|
208 |
return $this->removeIndex($table_name, $index_name); |
|
209 |
} |
|
212 |
public function index_remove($table_name, $index_name) |
|
213 |
{ |
|
214 |
trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED); |
|
215 |
return $this->removeIndex($table_name, $index_name); |
|
216 |
} |
|
210 | 217 |
/* |
211 | 218 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX) |
212 | 219 |
* @param string $field_name: name of the field to remove |
213 | 220 |
* @return bool: true if successful, otherwise false and error will be set |
214 | 221 |
*/ |
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 |
}
|
|
222 |
public function removeIndex($table_name, $index_name)
|
|
223 |
{
|
|
224 |
$retval = false;
|
|
225 |
if ($this->isIndex($table_name, $index_name)) {
|
|
226 |
// modify a existing field in a table
|
|
227 |
$sql = 'ALTER TABLE `'.$table_name.'` DROP INDEX `'.$index_name.'`';
|
|
228 |
$retval = ( $this->doQuery($sql) ? true : false );
|
|
229 |
}
|
|
230 |
return $retval;
|
|
231 |
}
|
|
225 | 232 |
/** |
226 | 233 |
* Alias for importSql() |
227 | 234 |
* @deprecated from WB-2.8.5 and higher |
228 | 235 |
*/ |
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 |
} |
|
236 |
public function SqlImport($sSqlDump, |
|
237 |
$sTablePrefix = '', |
|
238 |
$sAction = 'install', |
|
239 |
$sEngine = 'MyISAM', |
|
240 |
$sCollation = 'utf8_unicode_ci') |
|
241 |
{ |
|
242 |
trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED); |
|
243 |
$oImport = new SqlImport($this, $sSqlDump); |
|
244 |
return $oImport->doImport($sAction); |
|
245 |
} |
|
237 | 246 |
|
238 | 247 |
/** |
239 | 248 |
* retuns the type of the engine used for requested table |
240 | 249 |
* @param string $table name of the table, including prefix |
241 | 250 |
* @return boolean/string false on error, or name of the engine (myIsam/InnoDb) |
242 | 251 |
*/ |
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 |
|
|
252 |
public function getTableEngine($table)
|
|
253 |
{
|
|
254 |
trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
|
|
255 |
$retVal = false;
|
|
256 |
$mysqlVersion = mysqli_get_server_info($this->oDbHandle);
|
|
257 |
$engineValue = (version_compare($mysqlVersion, '5.0') < 0) ? 'Type' : 'Engine';
|
|
258 |
$sql = 'SHOW TABLE STATUS FROM `' . $this->sDbName . '` LIKE \'' . $table . '\'';
|
|
259 |
if (($result = $this->doQuery($sql))) {
|
|
260 |
if (($row = $result->fetchRow(MYSQL_ASSOC))) {
|
|
261 |
$retVal = $row[$engineValue];
|
|
262 |
}
|
|
263 |
}
|
|
264 |
return $retVal;
|
|
265 |
} |
|
257 | 266 |
} |
258 | 267 |
|
259 | 268 |
// end of class WbDatabaseHelper |
269 |
// //////////////////////////////////////////////////////////////////////////////////// // |
|
270 |
/* this functions are placed inside this file temporarely until a better place is found */ |
|
271 |
/* function to update a var/value-pair(s) in table **************************** |
|
272 |
* nonexisting keys are inserted |
|
273 |
* @param string $table: name of table to use (without prefix) |
|
274 |
* @param mixed $key: a array of key->value pairs to update |
|
275 |
* or a string with name of the key to update |
|
276 |
* @param string $value: a sting with needed value, if $key is a string too |
|
277 |
* @return bool: true if any keys are updated, otherwise false |
|
278 |
*/ |
|
279 |
function updateDbKeyValue($table, $key, $value = '') |
|
280 |
{ |
|
281 |
$oDb = WbDatabase::getInstance(); |
|
282 |
$table = preg_replace('/^'.preg_quote($oDb->TablePrefix, '/').'/s', '', $table); |
|
283 |
if (!is_array($key)) { |
|
284 |
if (trim($key) != '') { |
|
285 |
$key = array( trim($key) => trim($value) ); |
|
286 |
} else { |
|
287 |
$key = array(); |
|
288 |
} |
|
289 |
} |
|
290 |
$retval = true; |
|
291 |
$sNameValPairs = ''; |
|
292 |
foreach ($key as $index => $val) { |
|
293 |
$sNameValPairs .= ', (\''.$index.'\', \''.$val.'\')'; |
|
294 |
} |
|
295 |
$sValues = ltrim($sNameValPairs, ', '); |
|
296 |
if ($sValues != '') { |
|
297 |
$sql = 'REPLACE INTO `'.$oDb->TablePrefix.$table.'` (`name`, `value`) ' |
|
298 |
. 'VALUES '.$sValues; |
|
299 |
if (!$oDb->doQuery($sql)) { |
|
300 |
$retval = false; |
|
301 |
} |
|
302 |
} |
|
303 |
return $retval; |
|
304 |
} |
|
305 |
/** |
|
306 |
* Alias for updateDbKeyValue() |
|
307 |
* @param string $table: name of table to use (without prefix) |
|
308 |
* @param mixed $key: a array of key->value pairs to update |
|
309 |
* or a string with name of the key to update |
|
310 |
* @param string $value: a sting with needed value, if $key is a string too |
|
311 |
* @return bool: true if any keys are updated, otherwise false |
|
312 |
* @deprecated from 2.8.4 |
|
313 |
*/ |
|
314 |
function db_update_key_value($table, $key, $value = '') |
|
315 |
{ |
|
316 |
trigger_error('Deprecated function call: '.basename(__FILE__).'::'.__FUNCTION__, E_USER_DEPRECATED); |
|
317 |
return updateDbKeyValue($table, $key, $value); |
|
318 |
} |
Also available in: Unified diff
! framework/ ~WbDatabaseHelper ~WbDatabase some little correction to different methods