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 // Initializes the SQL link by bringing it up if set
222 function initSqlLink () {
223 // Do this only if link is down
224 assert(!SQL_IS_LINK_UP());
226 // Is the configuration data set?
227 if ((!empty($GLOBALS['mysql']['host'])) && (!empty($GLOBALS['mysql']['login'])) && (!empty($GLOBALS['mysql']['dbase']))) {
229 unset($GLOBALS['is_sql_link_up']);
232 SQL_CONNECT($GLOBALS['mysql']['host'], $GLOBALS['mysql']['login'], $GLOBALS['mysql']['password'], __FILE__, __LINE__);
234 // Is the link valid?
235 if (SQL_IS_LINK_UP()) {
236 // Enable exit on error
239 // Is it a valid resource?
240 if (SQL_SELECT_DB($GLOBALS['mysql']['dbase'], __FILE__, __LINE__) === TRUE) {
241 // Set database name (required for ext-optimize and ifSqlTableExists())
242 setConfigEntry('__DB_NAME', $GLOBALS['mysql']['dbase']);
244 // Remove MySQL array from namespace
245 unset($GLOBALS['mysql']);
248 loadIncludeOnce('inc/load_cache.php');
251 reportBug(__FILE__, __LINE__, 'Wrong database selected.');
254 // No link to database!
255 reportBug(__FILE__, __LINE__, 'Database link is not yet up.');
258 // Maybe you forgot to enter your database login?
259 reportBug(__FILE__, __LINE__, 'Database login is missing.');
263 // Imports given SQL dump from given (relative) path and adds them to $sqlPool
264 function importSqlDump ($path, $dumpName, $sqlPool) {
266 $FQFN = getPath() . $path . '/' . $dumpName . '.sql';
268 // Is the file readable?
269 if (!isFileReadable($FQFN)) {
270 // Not found, which is bad
271 reportBug(__FILE__, __LINE__, sprintf("SQL dump %s/%s.sql is not readable.", $path, $dumpName));
275 $fileContent = readSqlDump($FQFN);
277 // Merge it with existing SQL statements
278 mergeSqls(explode(";\n", $fileContent), $sqlPool);
281 // SQL string escaping
282 function SQL_QUERY_ESC ($sqlString, $data, $F, $L, $run = TRUE, $strip = TRUE, $secure = TRUE) {
284 if ((!SQL_IS_LINK_UP()) || (!is_array($data))) {
285 // Link is down or data is not an array
286 /* DEBUG: */ logDebugMessage($F, $L, 'SQL_IS_LINK_UP()=' . intval(SQL_IS_LINK_UP()) . ',data[]=' . gettype($data) . ',sqlString=' . $sqlString . ': ABORTING!');
290 // Init array for escape'd data with SQL string
291 $dataSecured = array(
292 '__sql_string' => $sqlString
296 foreach ($data as $key => $value) {
297 $dataSecured[$key] = SQL_ESCAPE($value, $secure, $strip);
301 $query = call_user_func_array('sprintf', $dataSecured);
304 // Run SQL query (default)
305 return SQL_QUERY($query, $F, $L);
307 // Return secured string
312 // SELECT query string from table, columns and so on... ;-)
313 function SQL_RESULT_FROM_ARRAY ($table, $columns, $idRow, $id, $F, $L) {
314 // Is columns an array?
315 if (!is_array($columns)) {
317 reportBug(__FUNCTION__, __LINE__, sprintf("columns is not an array. %s != array, file=%s, line=%s",
323 // Abort here with 'false'
327 // Is this is a simple array?
328 if ((is_array($columns[0])) && (isset($columns[0]['column']))) {
329 // Begin with SQL query
332 // No, it comes from XML, so get it back from it
333 $sql .= getSqlPartFromXmlArray($columns);
336 $sql .= " FROM `{?_MYSQL_PREFIX?}_%s` WHERE `%s`='%s' LIMIT 1";
338 // Yes, prepare the SQL statement
339 $sql = 'SELECT `' . implode('`, `', $columns) . "` FROM `{?_MYSQL_PREFIX?}_%s` WHERE `%s`='%s' LIMIT 1";
343 return SQL_QUERY_ESC($sql,
352 // ALTER TABLE wrapper function
353 function SQL_ALTER_TABLE ($sql, $F, $L, $enableCodes = TRUE) {
354 // Abort if link is down
355 if (!SQL_IS_LINK_UP()) return FALSE;
357 // This is the default result...
360 // Determine index/fulltext/unique word
363 isInString('INDEX', $sql)
365 isInString('KEY', $sql)
367 isInString('FULLTEXT', $sql)
369 isInString('UNIQUE', $sql)
373 // Extract table name
374 $tableArray = explode(' ', $sql);
375 $tableName = str_replace('`', '', $tableArray[2]);
378 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sql=' . $sql . ',tableName=' . $tableName . ',tableArray=<pre>' . print_r($tableArray, TRUE) . '</pre>,isAlterIndex=' . intval($isAlterIndex));
380 // Shall we add/drop?
381 if (((isInString('ADD', $sql)) || (isInString('DROP', $sql)) || (isInString('CHANGE', $sql))) && ($isAlterIndex === FALSE)) {
382 // Try two columns, one should fix
383 foreach (array(4,5) as $idx) {
384 // If an entry is not set, abort here
385 if (!isset($tableArray[$idx])) {
387 logDebugMessage(__FUNCTION__, __LINE__, 'columnName=' . $columnName . ',idx=' . $idx . ',sql=' . $sql . ' is missing!');
391 // And column name as well
392 $columnName = $tableArray[$idx];
395 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'columnName=' . $columnName . ',idx=' . $idx . ',sql=' . $sql . ',hasZeroNums=' . intval(ifSqlTableColumnExists($tableName, $columnName)));
397 // Is there no entry on ADD or an entry on DROP/CHANGE?
398 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])))))) {
400 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Executing: ' . $sql);
401 $result = SQL_QUERY($sql, $F, $L, FALSE);
403 // Skip further attempt(s)
405 } elseif ((((ifSqlTableColumnExists($tableName, $columnName)) && (isInString('ADD', $sql))) || ((!ifSqlTableColumnExists($tableName, $columnName)) && ((isInString('DROP', $sql))) || (isInString('CHANGE', $sql)))) && ($columnName != 'KEY')) {
406 // Abort here because it is alreay there
407 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Skipped: sql=' . $sql . ',columnName=' . $columnName . ',idx=' . $idx);
409 } elseif ((!ifSqlTableColumnExists($tableName, $columnName)) && (isInString('DROP', $sql))) {
410 // Abort here because we tried to drop a column which is not there (never created maybe)
411 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'No drop: ' . $sql);
413 } elseif ($columnName != 'KEY') {
414 // Something didn't fit, we better log it
415 logDebugMessage(__FUNCTION__, __LINE__, 'Possible problem: ' . $sql . ',hasZeroNums=' . intval(ifSqlTableColumnExists($tableName, $columnName)) . '');
418 } elseif ((getTableType() == 'InnoDB') && (isInString('FULLTEXT', $sql))) {
419 // Skip this query silently because InnoDB does not understand fulltext indexes
420 //* 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));
421 } elseif ($isAlterIndex === TRUE) {
422 // And column name as well without backticks
423 $keyName = str_replace('`', '', $tableArray[5]);
424 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'keyName=' . $keyName . ',tableArray=<pre>' . print_r($tableArray, TRUE) . '</pre>');
426 // Is this "UNIQUE" or so? FULLTEXT has been handled the elseif() block above
427 if (in_array(strtoupper($tableArray[4]), array('INDEX', 'UNIQUE', 'KEY', 'FULLTEXT'))) {
431 while (isInString(',', $keyName)) {
433 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'keyName=' . $keyName . 'begin=' . $begin . ' - BEFORE');
434 $keyName = str_replace('`', '', $tableArray[count($tableArray) - $begin]);
435 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'keyName=' . $keyName . 'begin=' . $begin . ' - BETWEEN');
438 $keyName = str_replace(array('(', ')'), array('', ''), $keyName);
439 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'keyName=' . $keyName . 'begin=' . $begin . ' - AFTER');
447 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ', tableArray[3]=' . $tableArray[3] . ',keyName=' . $keyName);
448 if (($tableArray[3] == 'ADD') && (!ifSqlTableIndexExist($tableName, $keyName))) {
449 // Send it to the SQL_QUERY() function to add it
450 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sql=' . $sql . ' - ADDING!');
451 $result = SQL_QUERY($sql, $F, $L, $enableCodes);
452 } elseif (($tableArray[3] == 'DROP') && (ifSqlTableIndexExist($tableName, $keyName))) {
453 // Send it to the SQL_QUERY() function to drop it
454 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sql=' . $sql . ' - DROPPING!');
455 $result = SQL_QUERY($sql, $F, $L, $enableCodes);
458 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Not executed: ' . $sql);
461 // Other ALTER TABLE query
462 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $sql);
463 $result = SQL_QUERY($sql, $F, $L, $enableCodes);
470 // Getter for SQL link
471 function SQL_GET_LINK () {
475 // Is it in the globals?
476 if (isset($GLOBALS['sql_link'])) {
478 $link = $GLOBALS['sql_link'];
486 function SQL_SET_LINK ($link) {
487 // Is this a resource or null?
488 if ((ifFatalErrorsDetected()) && (isInstallationPhase())) {
489 // This may happen in installation phase
491 } elseif ((!is_resource($link)) && (!is_null($link))) {
492 // This should never happen!
493 reportBug(__FUNCTION__, __LINE__, sprintf("Type of link is not resource or null, type=%s", gettype($link)));
497 $GLOBALS['sql_link'] = $link;
500 $GLOBALS['is_sql_link_up'] = is_resource($link);
503 // Checks if the link is up
504 function SQL_IS_LINK_UP () {
505 // Is there cached this?
506 if (!isset($GLOBALS['is_sql_link_up'])) {
508 $GLOBALS['is_sql_link_up'] = is_resource(SQL_GET_LINK());
512 return $GLOBALS['is_sql_link_up'];
515 // Wrapper function to make code more readable
516 function SQL_HASZERONUMS ($result) {
517 // Just pass it through
518 return (SQL_NUMROWS($result) === 0);
521 // Wrapper function to make code more readable
522 function SQL_HASZEROAFFECTED () {
523 // Just pass it through
524 return (SQL_AFFECTEDROWS() === 0);
527 // Private function to prepare the SQL query string
528 function SQL_PREPARE_SQL_STRING ($sqlString, $enableCodes = TRUE) {
530 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString=' . $sqlString . ',enableCodes=' . intval($enableCodes) . ' - ENTERED!');
532 // Is it already cached?
533 if (!isset($GLOBALS['sql_strings']['' . $sqlString . ''])) {
534 // Preserve escaping and compile URI codes+config+expression code
535 $sqlString2 = FILTER_COMPILE_EXPRESSION_CODE(FILTER_COMPILE_CONFIG($sqlString));
538 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString2=' . $sqlString2);
540 // Do final compilation and revert {ESCAPE}
541 $GLOBALS['sql_strings']['' . $sqlString . ''] = doFinalCompilation($sqlString2, FALSE, $enableCodes);
544 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString=' . $sqlString . ' - CACHE!');
548 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString=' . $sqlString . ',enableCodes=' . intval($enableCodes) . ',sql_strings=' . $GLOBALS['sql_strings']['' . $sqlString . ''] . ' - EXIT!');
551 return $GLOBALS['sql_strings']['' . $sqlString . ''];
554 // Creates a MySQL TIMESTAMP compatible string from given Uni* timestamp
555 function SQL_EPOCHE_TO_TIMESTAMP ($timestamp) {
556 return generateDateTime($timestamp, 7);
559 // Check if there is a SQL table created
560 function ifSqlTableExists ($tableName) {
561 // Make sure double-prefixes are being removed
562 $tableName = str_replace('{?_MYSQL_PREFIX?}_', '', $tableName);
565 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ' - ENTERED!');
568 if (!isset($GLOBALS[__FUNCTION__][$tableName])) {
569 // Check if the table is there
570 $result = SQL_QUERY_ESC("SHOW TABLES FROM `{?__DB_NAME?}` WHERE `Tables_in_{?__DB_NAME?}`='{?_MYSQL_PREFIX?}_%s'",
571 array($tableName), __FILE__, __LINE__);
574 if (!is_resource($result)) {
575 // Is installation phase?
576 if (isInstallationPhase()) {
577 // Then silently abort here
578 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ',isLinkUp=' . intval(SQL_IS_LINK_UP()) . ',tableName=' . $tableName . ' - Returning FALSE ...');
581 // Please report this
582 reportBug(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ' is not a resource.');
586 // Is there an entry?
587 $GLOBALS[__FUNCTION__][$tableName] = (SQL_NUMROWS($result) == 1);
588 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',numRows=' . intval($GLOBALS[__FUNCTION__][$tableName]));
592 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',result=' . intval($GLOBALS[__FUNCTION__][$tableName]) . ' - EXIT!');
593 return $GLOBALS[__FUNCTION__][$tableName];
596 // Is a table column there?
597 function ifSqlTableColumnExists ($tableName, $columnName, $forceFound = FALSE) {
599 $columnName = str_replace('`', '', $columnName);
602 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',columnName=' . $columnName . ' - ENTERED!');
604 // If the table is not there, it is okay
605 if (!ifSqlTableExists($tableName)) {
607 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Table ' . $tableName . ' does not exist, columnName=' . $columnName . ',forceFound=' . intval($forceFound));
608 return (($forceFound === FALSE) && (isInstallationPhase()));
611 // Get column information
612 $result = SQL_QUERY_ESC("SHOW COLUMNS FROM `%s` LIKE '%s'",
616 ), __FUNCTION__, __LINE__);
619 if (!is_resource($result)) {
620 // Is installation phase?
621 if (isInstallationPhase()) {
622 // Then silently abort here
623 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ',isLinkUp=' . intval(SQL_IS_LINK_UP()) . ',tableName=' . $tableName . ',columnName=' . $columnName . ' - Returning FALSE ...');
626 // Please report this
627 reportBug(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ' is not a resource.');
632 $doesExist = (!SQL_HASZERONUMS($result));
635 SQL_FREERESULT($result);
638 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',columnName=' . $columnName . ',doesExist=' . intval($doesExist) . ' - EXIT!');
642 // Checks depending on the mode if the index is there
643 function ifSqlTableIndexExist ($tableName, $keyName, $forceFound = FALSE) {
645 $keyName = str_replace('`', '', $keyName);
648 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',keyName=' . $keyName . ' - ENTERED!');
650 // If the table is not there, it is okay
651 if (!ifSqlTableExists($tableName)) {
653 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Table ' . $tableName . ' does not exist, keyName=' . $keyName . ',forceFound=' . intval($forceFound));
654 return (($forceFound === FALSE) && (isInstallationPhase()));
658 $result = SQL_QUERY_ESC("SHOW INDEX FROM `%s`", array($tableName), __FUNCTION__, __LINE__);
661 if (!is_resource($result)) {
662 // Is installation phase?
663 if (isInstallationPhase()) {
664 // Then silently abort here
665 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ',isLinkUp=' . intval(SQL_IS_LINK_UP()) . ',tableName=' . $tableName . ',keyName=' . $keyName . ' - Returning FALSE ...');
668 // Please report this
669 reportBug(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ' is not a resource.');
673 // The key is not found by default
677 while ($content = SQL_FETCHARRAY($result)) {
678 // Is it the requested one?
679 if ($content['Key_name'] == $keyName) {
680 // Then it is found and exit
687 SQL_FREERESULT($result);
690 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',keyName=' . $keyName . ',doesExist=' . intval($doesExist) . ' - EXIT!');
694 // Init database layer
695 function initDatabaseLayer () {
696 // Set all required variables:
697 $GLOBALS['last_sql_error'] = '';
700 // Get last SQL error
701 function getLastSqlError () {
702 return $GLOBALS['last_sql_error'];
705 // Gets an array (or false if none is found) from all supported engines
706 function getArrayFromSupportedSqlEngines ($requestedEngine = 'ALL') {
710 // This also worked, now we need to check if the selected database type is supported
711 $result = SQL_QUERY('SHOW ENGINES', __FUNCTION__, __LINE__);
713 // Are there entries? (Bad if not)
714 if (!SQL_HASZERONUMS($result)) {
715 // Load all and check for active entries
716 while ($content = SQL_FETCHARRAY($result)) {
718 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'support=' . $requestedEngine . ',Engine=' . $content['Engine'] . ',Support=' . $content['Support']);
720 // Is this supported?
721 if ((($requestedEngine == 'ALL') || ($content['Engine'] == $requestedEngine)) && (in_array($content['Support'], array('YES', 'DEFAULT')))) {
723 array_push($engines, $content);
724 } elseif (isDebugModeEnabled()) {
725 // Log it away in debug mode
726 logDebugMessage(__FUNCTION__, __LINE__, 'Engine ' . $content['Engine'] . ' is not supported (' . $content['Supported'] . ' - ' . $requestedEngine . ')');
735 SQL_FREERESULT($result);