2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 10/23/2009 *
4 * =================== Last change: 10/23/2009 *
6 * -------------------------------------------------------------------- *
7 * File : sql-functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : SQL functions to handle queries *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : SQL-Funktionen fuer Queries *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2012 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
44 function initSqls () {
46 setSqlsArray(array('generic' => array()));
49 // Checks whether the sqls array is initialized
50 function isSqlsInitialized () {
51 return ((isset($GLOBALS['sqls'])) && (is_array($GLOBALS['sqls'])));
54 // Setter for SQLs array
55 function setSqlsArray ($SQLs) {
56 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'count()='.count($SQLs));
57 $GLOBALS['sqls'] = (array) $SQLs;
60 // Remover for SQLs array
61 function unsetSqls () {
62 unset($GLOBALS['sqls']);
65 // Getter for SQLs array
67 return $GLOBALS['sqls'];
70 // Add an SQL to the list
71 function addSql ($sql) {
72 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("sql=%s, count=%d", $sql, countSqls()));
73 array_push($GLOBALS['sqls']['generic'], $sql);
76 // Merge SQLs together
77 function mergeSqls ($SQLs, $type = '') {
78 // Should we merge full array or partial?
80 // Merge full array (may kill entries)
81 setSqlsArray(merge_array(getSqls(), $SQLs));
83 // Merge sub array, so get it
86 // Is the sub array there?
87 if (isset($array[$type])) {
88 // Then get it and merge it with the new one
89 $array[$type] = merge_array($array[$type], $SQLs);
92 $array[$type] = $SQLs;
100 // Counter for SQLs array
101 function countSqls () {
105 // Is the array there?
106 if (isSqlsInitialized()) {
108 $count = count($GLOBALS['sqls']);
109 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("count=%d", $count));
116 // Checks whether the SQLs array is filled
117 function isSqlsValid () {
118 //* DEBUG: */ debugOutput(__FUNCTION__ . ':' . intval(isSqlsInitialized()) . '/' . countSqls() . '/' . getCurrentExtensionName());
128 // Generates an updating SQL query from given array
129 function getUpdateSqlFromArray ($array, $tableName, $whereColumn, $whereData, $excludedFields, $multiDimId = NULL) {
131 $SQL = 'UPDATE `{?_MYSQL_PREFIX?}_' . $tableName . '` SET ';
134 foreach ($array as $entry => $value) {
135 // Skip login/id entry
136 if (in_array($entry, $excludedFields)) {
140 // Is there a non-string (e.g. number, NULL, SQL function or back-tick at the beginning?
141 if (is_null($multiDimId)) {
142 // Handle one-dimensional data
143 if (is_null($value)) {
145 $SQL .= '`' . $entry . '`=NULL,';
146 } elseif ((substr($value, -2, 2) == '()') || (substr($value, 0, 1) == '`')) {
147 // SQL function needs no ticks (')
148 $SQL .= '`' . $entry . '`=' . SQL_ESCAPE($value) . ',';
149 } elseif ('' . bigintval($value, TRUE, FALSE) . '' == '' . $value . '') {
150 // No need for ticks (')
151 $SQL .= '`' . $entry . '`=' . $value . ',';
153 // Strings need ticks (') around them
154 $SQL .= '`' . $entry . "`='" . SQL_ESCAPE($value) . "',";
157 // Handle multi-dimensional data
158 if (is_null($value[$multiDimId])) {
160 $SQL .= '`' . $entry . '`=NULL,';
161 } elseif ((substr($value[$multiDimId], -2, 2) == '()') || (substr($value[$multiDimId], 0, 1) == '`')) {
162 // SQL function needs no ticks (')
163 $SQL .= '`' . $entry . '`=' . SQL_ESCAPE($value[$multiDimId]) . ',';
164 } elseif (('' . bigintval($value[$multiDimId], TRUE, FALSE) . '' == '' . $value[$multiDimId] . '')) {
165 // No need for ticks (')
166 $SQL .= '`' . $entry . '`=' . $value[$multiDimId] . ',';
168 // Strings need ticks (') around them
169 $SQL .= '`' . $entry . "`='" . SQL_ESCAPE($value[$multiDimId]) . "',";
174 // Remove last 2 chars and finish query
175 $SQL = substr($SQL, 0, -1) . ' WHERE `' . $whereColumn . '`=' . $whereData . ' LIMIT 1';
181 // "Getter" for an "INSERT INTO" SQL query
182 function getInsertSqlFromArray ($array, $tableName) {
185 `{?_MYSQL_PREFIX?}_' . $tableName . '`
187 `' . implode('`, `', array_keys(postRequestArray())) . '`
190 // Walk through all entries
191 foreach (postRequestArray() as $key => $value) {
193 //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',key=' . $key . ',value=' . $value);
196 if (is_null($value)) {
199 } elseif (substr($value, -2, 2) == '()') {
200 // SQL function needs no ticks (')
201 $SQL .= SQL_ESCAPE($value) . ',';
202 } elseif ('' . bigintval($value, TRUE, FALSE) . '' == '' . $value . '') {
203 // Number detected, no need for ticks (')
204 $SQL .= bigintval($value) . ',';
205 } elseif ('' . (float) $value . '' == '' . $value . '') {
206 // Float number detected
207 $SQL .= sprintf('%01.5f', $value);
209 // Everything else might be a string, so add ticks around it
210 $SQL .= chr(39) . SQL_ESCAPE($value) . chr(39) . ',';
215 $SQL = substr($SQL, 0, -1) . ')';
221 // Function to unset __is_sql_link_up
222 function unsetSqlLinkUp ($F, $L) {
224 //* DEBUG: */ logDebugMessage($F, $L, __FUNCTION__ . ': Called!');
225 SQL_SET_LINK($F, $L, NULL);
228 // Initializes the SQL link by bringing it up if set
229 function initSqlLink () {
231 unsetSqlLinkUp(__FUNCTION__, __LINE__);
233 // Do this only if link is down
234 assert(!SQL_IS_LINK_UP());
236 // Is the configuration data set?
237 if ((!empty($GLOBALS['mysql']['host'])) && (!empty($GLOBALS['mysql']['login'])) && (!empty($GLOBALS['mysql']['dbase']))) {
239 unsetSqlLinkUp(__FUNCTION__, __LINE__);
242 SQL_CONNECT($GLOBALS['mysql']['host'], $GLOBALS['mysql']['login'], $GLOBALS['mysql']['password'], __FUNCTION__, __LINE__);
244 // Is the link valid?
245 if (SQL_IS_LINK_UP()) {
246 // Enable exit on error
249 // Is it a valid resource?
250 if (SQL_SELECT_DB($GLOBALS['mysql']['dbase'], __FUNCTION__, __LINE__) === TRUE) {
251 // Set database name (required for ext-optimize and ifSqlTableExists())
252 setConfigEntry('__DB_NAME', $GLOBALS['mysql']['dbase']);
254 // Remove MySQL array from namespace
255 unset($GLOBALS['mysql']);
258 loadIncludeOnce('inc/load_cache.php');
261 reportBug(__FUNCTION__, __LINE__, 'Wrong database selected.');
264 // No link to database!
265 reportBug(__FUNCTION__, __LINE__, 'Database link is not yet up.');
268 // Maybe you forgot to enter your database login?
269 reportBug(__FUNCTION__, __LINE__, 'Database login is missing.');
273 // Imports given SQL dump from given (relative) path and adds them to $sqlPool
274 function importSqlDump ($path, $dumpName, $sqlPool) {
276 $FQFN = getPath() . $path . '/' . $dumpName . '.sql';
278 // Is the file readable?
279 if (!isFileReadable($FQFN)) {
280 // Not found, which is bad
281 reportBug(__FUNCTION__, __LINE__, sprintf("SQL dump %s/%s.sql is not readable.", $path, $dumpName));
285 $fileContent = readSqlDump($FQFN);
287 // Merge it with existing SQL statements
288 mergeSqls(explode(";\n", $fileContent), $sqlPool);
291 // SQL string escaping
292 function SQL_QUERY_ESC ($sqlString, $data, $F, $L, $run = TRUE, $strip = TRUE, $secure = TRUE) {
294 if ((!SQL_IS_LINK_UP()) || (!is_array($data))) {
295 // Link is down or data is not an array
296 //* DEBUG: */ logDebugMessage($F, $L, 'SQL_IS_LINK_UP()=' . intval(SQL_IS_LINK_UP()) . ',data[]=' . gettype($data) . ',sqlString=' . $sqlString . ': ABORTING!');
300 // Init array for escape'd data with SQL string
301 $dataSecured = array(
302 '__sql_string' => $sqlString
306 foreach ($data as $key => $value) {
307 $dataSecured[$key] = SQL_ESCAPE($value, $secure, $strip);
311 $query = call_user_func_array('sprintf', $dataSecured);
314 // Run SQL query (default)
315 return SQL_QUERY($query, $F, $L);
317 // Return secured string
322 // SELECT query string from table, columns and so on... ;-)
323 function SQL_RESULT_FROM_ARRAY ($table, $columns, $idRow, $id, $F, $L) {
324 // Is columns an array?
325 if (!is_array($columns)) {
327 reportBug(__FUNCTION__, __LINE__, sprintf("columns is not an array. %s != array, file=%s, line=%s",
333 // Abort here with 'false'
337 // Is this is a simple array?
338 if ((is_array($columns[0])) && (isset($columns[0]['column']))) {
339 // Begin with SQL query
342 // No, it comes from XML, so get it back from it
343 $sql .= getSqlPartFromXmlArray($columns);
346 $sql .= " FROM `{?_MYSQL_PREFIX?}_%s` WHERE `%s`='%s' LIMIT 1";
348 // Yes, prepare the SQL statement
349 $sql = 'SELECT `' . implode('`, `', $columns) . "` FROM `{?_MYSQL_PREFIX?}_%s` WHERE `%s`='%s' LIMIT 1";
353 return SQL_QUERY_ESC($sql,
362 // ALTER TABLE wrapper function
363 function SQL_ALTER_TABLE ($sql, $F, $L, $enableCodes = TRUE) {
364 // Abort if link is down
365 if (!SQL_IS_LINK_UP()) return FALSE;
367 // This is the default result...
370 // Determine index/fulltext/unique word
373 isInString('INDEX', $sql)
375 isInString('KEY', $sql)
377 isInString('FULLTEXT', $sql)
379 isInString('UNIQUE', $sql)
383 // Extract table name
384 $tableArray = explode(' ', $sql);
385 $tableName = str_replace('`', '', $tableArray[2]);
388 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sql=' . $sql . ',tableName=' . $tableName . ',tableArray=<pre>' . print_r($tableArray, TRUE) . '</pre>,isAlterIndex=' . intval($isAlterIndex));
390 // Shall we add/drop?
391 if (((isInString('ADD', $sql)) || (isInString('DROP', $sql)) || (isInString('CHANGE', $sql))) && ($isAlterIndex === FALSE)) {
392 // Try two columns, one should fix
393 foreach (array(4,5) as $idx) {
394 // If an entry is not set, abort here
395 if (!isset($tableArray[$idx])) {
397 logDebugMessage(__FUNCTION__, __LINE__, 'columnName=' . $columnName . ',idx=' . $idx . ',sql=' . $sql . ' is missing!');
401 // And column name as well
402 $columnName = $tableArray[$idx];
405 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'columnName=' . $columnName . ',idx=' . $idx . ',sql=' . $sql . ',hasZeroNums=' . intval(ifSqlTableColumnExists($tableName, $columnName)));
407 // Is there no entry on ADD or an entry on DROP/CHANGE?
408 if (((!ifSqlTableColumnExists($tableName, $columnName)) && (isInString('ADD', $sql))) || ((ifSqlTableColumnExists($tableName, $columnName)) && ((isInString('DROP', $sql)) || ((isInString('CHANGE', $sql)) && ($idx == 4) && ((!ifSqlTableColumnExists($tableName, $tableArray[5])) || ($columnName == $tableArray[5])))))) {
410 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Executing: ' . $sql);
411 $result = SQL_QUERY($sql, $F, $L, FALSE);
413 // Skip further attempt(s)
415 } elseif ((((ifSqlTableColumnExists($tableName, $columnName)) && (isInString('ADD', $sql))) || ((!ifSqlTableColumnExists($tableName, $columnName)) && ((isInString('DROP', $sql))) || (isInString('CHANGE', $sql)))) && ($columnName != 'KEY')) {
416 // Abort here because it is alreay there
417 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Skipped: sql=' . $sql . ',columnName=' . $columnName . ',idx=' . $idx);
419 } elseif ((!ifSqlTableColumnExists($tableName, $columnName)) && (isInString('DROP', $sql))) {
420 // Abort here because we tried to drop a column which is not there (never created maybe)
421 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'No drop: ' . $sql);
423 } elseif ($columnName != 'KEY') {
424 // Something didn't fit, we better log it
425 logDebugMessage(__FUNCTION__, __LINE__, 'Possible problem: ' . $sql . ',hasZeroNums=' . intval(ifSqlTableColumnExists($tableName, $columnName)) . '');
428 } elseif ((getTableType() == 'InnoDB') && (isInString('FULLTEXT', $sql))) {
429 // Skip this query silently because InnoDB does not understand fulltext indexes
430 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("Skipped FULLTEXT: sql=%s,tableName=%s,hasZeroNums=%d,file=%s,line=%s", $sql, $tableName, intval((is_bool($result)) ? 0 : ifSqlTableColumnExists($columnName)), $F, $L));
431 } elseif ($isAlterIndex === TRUE) {
432 // And column name as well without backticks
433 $keyName = str_replace('`', '', $tableArray[5]);
434 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'keyName=' . $keyName . ',tableArray=<pre>' . print_r($tableArray, TRUE) . '</pre>');
436 // Is this "UNIQUE" or so? FULLTEXT has been handled the elseif() block above
437 if (in_array(strtoupper($tableArray[4]), array('INDEX', 'UNIQUE', 'KEY', 'FULLTEXT'))) {
441 while (isInString(',', $keyName)) {
443 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'keyName=' . $keyName . 'begin=' . $begin . ' - BEFORE');
444 $keyName = str_replace('`', '', $tableArray[count($tableArray) - $begin]);
445 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'keyName=' . $keyName . 'begin=' . $begin . ' - BETWEEN');
448 $keyName = str_replace(array('(', ')'), array('', ''), $keyName);
449 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'keyName=' . $keyName . 'begin=' . $begin . ' - AFTER');
457 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ', tableArray[3]=' . $tableArray[3] . ',keyName=' . $keyName);
458 if (($tableArray[3] == 'ADD') && (!ifSqlTableIndexExist($tableName, $keyName))) {
459 // Send it to the SQL_QUERY() function to add it
460 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sql=' . $sql . ' - ADDING!');
461 $result = SQL_QUERY($sql, $F, $L, $enableCodes);
462 } elseif (($tableArray[3] == 'DROP') && (ifSqlTableIndexExist($tableName, $keyName))) {
463 // Send it to the SQL_QUERY() function to drop it
464 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sql=' . $sql . ' - DROPPING!');
465 $result = SQL_QUERY($sql, $F, $L, $enableCodes);
468 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Not executed: ' . $sql);
471 // Other ALTER TABLE query
472 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $sql);
473 $result = SQL_QUERY($sql, $F, $L, $enableCodes);
480 // Getter for SQL link
481 function SQL_GET_LINK () {
485 // Is it in the globals?
486 if (isset($GLOBALS['__sql_link'])) {
488 $link = $GLOBALS['__sql_link'];
489 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'link[]=' . gettype($link) . ' - FROM GLOBALS!');
493 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'link[]=' . gettype($link) . ' - EXIT!');
498 function SQL_SET_LINK ($F, $L, $link) {
499 //* DEBUG: */ logDebugMessage($F . ':' . __FUNCTION__, $L . ':' . __LINE__, 'link[]=' . gettype($link) . ' - ENTERED!');
500 // Is this a resource or null?
501 if ((ifFatalErrorsDetected()) && (isInstallationPhase())) {
502 // This may happen in installation phase
503 //* DEBUG: */ logDebugMessage($F . ':' . __FUNCTION__, $L . ':' . __LINE__, 'Some fatal errors detected in installation phase.');
505 } elseif ((!is_resource($link)) && (!is_null($link))) {
506 // This should never happen!
507 reportBug($F . ':' . __FUNCTION__, $L . ':' . __LINE__, sprintf("Type of link is not resource or null, type=%s", gettype($link)));
511 $GLOBALS['__sql_link'] = $link;
514 $GLOBALS['__is_sql_link_up'] = is_resource($link);
515 //* DEBUG: */ logDebugMessage($F . ':' . __FUNCTION__, $L . ':' . __LINE__, '__is_sql_link_up=' . intval($GLOBALS['__is_sql_link_up']) . ' - EXIT!');
518 // Checks if the link is up
519 function SQL_IS_LINK_UP () {
520 // Is there cached this?
521 if (!isset($GLOBALS['__is_sql_link_up'])) {
522 // Something bad went wrong
523 reportBug(__FUNCTION__, __LINE__, 'Called before SQL_SET_LINK() was called!');
527 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, '__is_sql_link_up=' . intval($GLOBALS['__is_sql_link_up']) . ' - EXIT!');
528 return $GLOBALS['__is_sql_link_up'];
531 // Wrapper function to make code more readable
532 function SQL_HASZERONUMS ($result) {
533 // Just pass it through
534 return (SQL_NUMROWS($result) === 0);
537 // Wrapper function to make code more readable
538 function SQL_HASZEROAFFECTED () {
539 // Just pass it through
540 return (SQL_AFFECTEDROWS() === 0);
543 // Private function to prepare the SQL query string
544 function SQL_PREPARE_SQL_STRING ($sqlString, $enableCodes = TRUE) {
546 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString=' . $sqlString . ',enableCodes=' . intval($enableCodes) . ' - ENTERED!');
548 // Is it already cached?
549 if (!isset($GLOBALS['sql_strings']['' . $sqlString . ''])) {
550 // Preserve escaping and compile URI codes+config+expression code
551 $sqlString2 = FILTER_COMPILE_EXPRESSION_CODE(FILTER_COMPILE_CONFIG($sqlString));
554 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString2=' . $sqlString2);
556 // Do final compilation and revert {ESCAPE}
557 $GLOBALS['sql_strings']['' . $sqlString . ''] = doFinalCompilation($sqlString2, FALSE, $enableCodes);
560 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString=' . $sqlString . ' - CACHE!');
564 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString=' . $sqlString . ',enableCodes=' . intval($enableCodes) . ',sql_strings=' . $GLOBALS['sql_strings']['' . $sqlString . ''] . ' - EXIT!');
567 return $GLOBALS['sql_strings']['' . $sqlString . ''];
570 // Creates a MySQL TIMESTAMP compatible string from given Uni* timestamp
571 function SQL_EPOCHE_TO_TIMESTAMP ($timestamp) {
572 return generateDateTime($timestamp, 7);
575 // Check if there is a SQL table created
576 function ifSqlTableExists ($tableName) {
577 // Make sure double-prefixes are being removed
578 $tableName = str_replace('{?_MYSQL_PREFIX?}_', '', $tableName);
581 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ' - ENTERED!');
584 if (!isset($GLOBALS[__FUNCTION__][$tableName])) {
585 // Check if the table is there
586 $result = SQL_QUERY_ESC("SHOW TABLES FROM `{?__DB_NAME?}` WHERE `Tables_in_{?__DB_NAME?}`='{?_MYSQL_PREFIX?}_%s'",
587 array($tableName), __FUNCTION__, __LINE__);
590 if (!is_resource($result)) {
591 // Is installation phase?
592 if (isInstallationPhase()) {
593 // Then silently abort here
594 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ',isLinkUp=' . intval(SQL_IS_LINK_UP()) . ',tableName=' . $tableName . ' - Returning FALSE ...');
597 // Please report this
598 reportBug(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ' is not a resource.');
602 // Is there an entry?
603 $GLOBALS[__FUNCTION__][$tableName] = (SQL_NUMROWS($result) == 1);
604 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',numRows=' . intval($GLOBALS[__FUNCTION__][$tableName]));
608 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',result=' . intval($GLOBALS[__FUNCTION__][$tableName]) . ' - EXIT!');
609 return $GLOBALS[__FUNCTION__][$tableName];
612 // Is a table column there?
613 function ifSqlTableColumnExists ($tableName, $columnName, $forceFound = FALSE) {
615 $columnName = str_replace('`', '', $columnName);
618 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',columnName=' . $columnName . ' - ENTERED!');
620 // If the table is not there, it is okay
621 if (!ifSqlTableExists($tableName)) {
623 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Table ' . $tableName . ' does not exist, columnName=' . $columnName . ',forceFound=' . intval($forceFound));
624 return (($forceFound === FALSE) && (isInstallationPhase()));
627 // Get column information
628 $result = SQL_QUERY_ESC("SHOW COLUMNS FROM `%s` LIKE '%s'",
632 ), __FUNCTION__, __LINE__);
635 if (!is_resource($result)) {
636 // Is installation phase?
637 if (isInstallationPhase()) {
638 // Then silently abort here
639 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ',isLinkUp=' . intval(SQL_IS_LINK_UP()) . ',tableName=' . $tableName . ',columnName=' . $columnName . ' - Returning FALSE ...');
642 // Please report this
643 reportBug(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ' is not a resource.');
648 $doesExist = (!SQL_HASZERONUMS($result));
651 SQL_FREERESULT($result);
654 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',columnName=' . $columnName . ',doesExist=' . intval($doesExist) . ' - EXIT!');
658 // Checks depending on the mode if the index is there
659 function ifSqlTableIndexExist ($tableName, $keyName, $forceFound = FALSE) {
661 $keyName = str_replace('`', '', $keyName);
664 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',keyName=' . $keyName . ' - ENTERED!');
666 // If the table is not there, it is okay
667 if (!ifSqlTableExists($tableName)) {
669 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Table ' . $tableName . ' does not exist, keyName=' . $keyName . ',forceFound=' . intval($forceFound));
670 return (($forceFound === FALSE) && (isInstallationPhase()));
674 $result = SQL_QUERY_ESC("SHOW INDEX FROM `%s`", array($tableName), __FUNCTION__, __LINE__);
677 if (!is_resource($result)) {
678 // Is installation phase?
679 if (isInstallationPhase()) {
680 // Then silently abort here
681 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ',isLinkUp=' . intval(SQL_IS_LINK_UP()) . ',tableName=' . $tableName . ',keyName=' . $keyName . ' - Returning FALSE ...');
684 // Please report this
685 reportBug(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ' is not a resource.');
689 // The key is not found by default
693 while ($content = SQL_FETCHARRAY($result)) {
694 // Is it the requested one?
695 if ($content['Key_name'] == $keyName) {
696 // Then it is found and exit
703 SQL_FREERESULT($result);
706 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',keyName=' . $keyName . ',doesExist=' . intval($doesExist) . ' - EXIT!');
710 // Init database layer
711 function initDatabaseLayer () {
712 // Set all required variables:
713 $GLOBALS['last_sql_error'] = '';
716 // Get last SQL error
717 function getLastSqlError () {
718 return $GLOBALS['last_sql_error'];
721 // Gets an array (or false if none is found) from all supported engines
722 function getArrayFromSupportedSqlEngines ($requestedEngine = 'ALL') {
726 // This also worked, now we need to check if the selected database type is supported
727 $result = SQL_QUERY('SHOW ENGINES', __FUNCTION__, __LINE__);
729 // Are there entries? (Bad if not)
730 if (!SQL_HASZERONUMS($result)) {
731 // Load all and check for active entries
732 while ($content = SQL_FETCHARRAY($result)) {
734 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'support=' . $requestedEngine . ',Engine=' . $content['Engine'] . ',Support=' . $content['Support']);
736 // Is this supported?
737 if ((($requestedEngine == 'ALL') || ($content['Engine'] == $requestedEngine)) && (in_array($content['Support'], array('YES', 'DEFAULT')))) {
739 array_push($engines, $content);
740 } elseif (isDebugModeEnabled()) {
741 // Log it away in debug mode
742 logDebugMessage(__FUNCTION__, __LINE__, 'Engine ' . $content['Engine'] . ' is not supported (' . $content['Supported'] . ' - ' . $requestedEngine . ')');
751 SQL_FREERESULT($result);