2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 08/29/2004 *
4 * =================== Last change: 08/29/2004 *
6 * -------------------------------------------------------------------- *
7 * File : lib-mysql3.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Database layer for MySQL 3/4/5 server *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Datenbankschicht fuer MySQL 3/4/5 Server *
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 SQL_QUERY ($sqlString, $F, $L, $enableCodes = TRUE) {
46 if (!isset($GLOBALS[__FUNCTION__][$sqlString])) {
47 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Called: ' . $sqlString);
50 $sqlStringModified = trim($sqlString);
52 // Empty query string or link is not up?
53 if (empty($sqlStringModified)) {
55 reportBug(__FUNCTION__, __LINE__, sprintf("SQL string is empty, please fix this: file=%s, line=%s",
59 } elseif (!SQL_IS_LINK_UP()) {
60 // We should not quietly ignore this
61 reportBug(__FUNCTION__, __LINE__, sprintf("Cannot query database: sqlString=%s,file=%s,line=%s",
68 // Remove \t, \n and \r from queries they may confuse some MySQL versions
69 $sqlStringModified = str_replace(array(chr(9), chr(10), chr(13)), array(' ', ' ', ' '), $sqlStringModified);
71 // Compile config entries out
72 $sqlStringModified = SQL_PREPARE_SQL_STRING($sqlStringModified, $enableCodes);
74 // Cache it and remember as last SQL query
75 $GLOBALS[__FUNCTION__][$sqlString] = $sqlStringModified;
76 $GLOBALS['last_sql'] = $sqlStringModified;
77 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Stored cache: ' . $sqlStringModified);
79 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cache used: ' . $sqlString);
81 // Use cache (to save a lot function calls
82 $GLOBALS['last_sql'] = $GLOBALS[__FUNCTION__][$sqlString];
84 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cache is: ' . $sqlString);
88 $querytimeBefore = microtime(TRUE);
91 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'F=' . basename($F) . ',L=' . $L . ',sql=' . $GLOBALS['last_sql']);
92 $result = mysql_query($GLOBALS['last_sql'], SQL_GET_LINK())
93 or SQL_ERROR($F, $L, 'file='. basename($F) . ',line=' . $L . ':mysql_error()=' . mysql_error() . ',last_query=' . $GLOBALS['last_sql']);
94 //* DEBUG: */ logDebugMessage($F, $L, 'sql=' . $GLOBALS['last_sql'] . ',affected=' . SQL_AFFECTEDROWS() . ',numRows='.(is_resource($result) ? SQL_NUMROWS($result) : gettype($result)));
96 // Calculate query time
97 $queryTime = microtime(TRUE) - $querytimeBefore;
99 // Add this query to array including timing
100 addSqlToDebug($result, $GLOBALS['last_sql'], $queryTime, $F, $L);
102 // Save last successfull query
103 setConfigEntry('db_last_query', $GLOBALS['last_sql']);
105 // Count all query times
106 incrementConfigEntry('sql_time', $queryTime);
109 incrementConfigEntry('sql_count');
112 if (SQL_DEBUG_ENABLED()) {
113 // Is this the first call?
114 if (!isset($GLOBALS['sql_first_entry'])) {
116 appendLineToFile(getCachePath() . 'mysql.log', 'Module=' . getModule());
117 $GLOBALS['sql_first_entry'] = TRUE;
121 appendLineToFile(getCachePath() . 'mysql.log', basename($F) . '|LINE=' . $L . '|NUM=' . (is_resource($result) ? SQL_NUMROWS($result) : 'false') . '|AFFECTED=' . SQL_AFFECTEDROWS() . '|QUERYTIME:' . ($queryTime * 1000) . 'ms): ' . str_replace(array(chr(13), chr(10)), array('', ' '), $GLOBALS['last_sql']));
124 // Increment stats entry
125 incrementStatsEntry('db_hits');
132 function SQL_NUMROWS ($resource) {
133 // Valid link resource?
134 if (!SQL_IS_LINK_UP()) return FALSE;
136 // Link is not up, no rows by default
139 // Is the result a valid resource?
140 if (isset($GLOBALS['sql_numrows'][intval($resource)])) {
142 $lines = $GLOBALS['sql_numrows'][intval($resource)];
143 } elseif (is_resource($resource)) {
144 // Get the count of rows from database
145 $lines = mysql_num_rows($resource);
147 // Remember it in cache
148 $GLOBALS['sql_numrows'][intval($resource)] = $lines;
150 // No resource given, please fix this
151 reportBug(__FUNCTION__, __LINE__, 'No resource given! result[]=' . gettype($resource) . ',last_sql=' . $GLOBALS['last_sql']);
159 function SQL_AFFECTEDROWS() {
160 // Valid link resource?
161 if (!SQL_IS_LINK_UP()) return FALSE;
164 $lines = mysql_affected_rows(SQL_GET_LINK());
171 function SQL_FETCHROW ($resource) {
172 // Is a result resource set?
173 if ((!is_resource($resource)) || (!SQL_IS_LINK_UP())) return FALSE;
175 // Fetch the data and return it
176 return mysql_fetch_row($resource);
180 function SQL_FETCHARRAY ($resource) {
181 // Is a result resource set?
182 if ((!is_resource($resource)) || (!SQL_IS_LINK_UP())) return FALSE;
184 // Load row from database
185 $row = mysql_fetch_assoc($resource);
187 // Return only arrays here
188 if (is_array($row)) {
192 // Return a false, else some loops would go endless...
198 function SQL_RESULT ($resource, $row, $field = '0') {
199 // Is $resource valid?
200 if ((!is_resource($resource)) || (!SQL_IS_LINK_UP())) return FALSE;
202 // Run the result command
203 $result = mysql_result($resource, $row, $field);
205 // ... and return the result
210 function SQL_CONNECT ($host, $login, $password, $F, $L) {
212 $linkResource = mysql_connect($host, $login, $password) or SQL_ERROR($F, $L, mysql_error());
214 // Set the link resource
215 if (is_resource($linkResource)) {
217 * A non-resource (boolean) may happen on installation phase which
218 * shall not be set here. Only valid link resources shall be set so
219 * SQL_IS_LINK_UP() will only return 'true' if there is really a
220 * working database link.
222 SQL_SET_LINK($linkResource);
225 // Return the resource
226 return $linkResource;
229 // SQL select database
230 function SQL_SELECT_DB ($dbName, $F, $L) {
231 // Is there still a valid link? If not, skip it.
232 if (!SQL_IS_LINK_UP()) return FALSE;
235 return mysql_select_db($dbName, SQL_GET_LINK()) or SQL_ERROR($F, $L, mysql_error());
239 function SQL_CLOSE ($F, $L) {
240 if (!SQL_IS_LINK_UP()) {
245 // Close database link and forget the link
246 $close = mysql_close(SQL_GET_LINK()) or SQL_ERROR($F, $L, mysql_error());
256 function SQL_FREERESULT ($resource) {
257 if ((!is_resource($resource)) || (!SQL_IS_LINK_UP())) {
263 $res = mysql_free_result($resource);
265 // And return that result of freeing it...
269 // SQL string escaping
270 function SQL_QUERY_ESC ($sqlString, $data, $F, $L, $run = TRUE, $strip = TRUE, $secure = TRUE) {
272 if ((!SQL_IS_LINK_UP()) || (!is_array($data))) {
273 // Link is down or data is not an array
274 /* DEBUG: */ logDebugMessage($F, $L, 'SQL_IS_LINK_UP()=' . intval(SQL_IS_LINK_UP()) . ',data[]=' . gettype($data) . ',sqlString=' . $sqlString . ': ABORTING!');
278 // Init array for escape'd data with SQL string
279 $dataSecured = array(
280 '__sql_string' => $sqlString
284 foreach ($data as $key => $value) {
285 $dataSecured[$key] = SQL_ESCAPE($value, $secure, $strip);
289 $query = call_user_func_array('sprintf', $dataSecured);
292 // Run SQL query (default)
293 return SQL_QUERY($query, $F, $L);
295 // Return secured string
300 // Get id from last INSERT command
301 function SQL_INSERTID () {
302 if (!SQL_IS_LINK_UP()) return FALSE;
303 return mysql_insert_id();
306 // Escape a string for the database
307 function SQL_ESCAPE ($str, $secureString = TRUE, $strip = TRUE) {
309 if (!isset($GLOBALS['sql_escapes']['' . $str . ''])) {
311 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ' - BEFORE!');
313 // Prepare the string here
314 $str = SQL_PREPARE_SQL_STRING($str);
317 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ' - AFTER!');
319 // Secure string first? (which is the default behaviour!)
320 if ($secureString === TRUE) {
322 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ',strip=' . intval($strip) . ' - BEFORE!');
325 $str = secureString($str, $strip);
328 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ',strip=' . intval($strip) . ' - AFTER!');
331 // Init (invalid) value
334 if (!SQL_IS_LINK_UP()) {
335 // Fall-back to escapeQuotes() when there is no link
336 $ret = escapeQuotes($str);
337 } elseif (function_exists('mysql_real_escape_string')) {
339 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str);
341 // The new and improved version
342 $ret = mysql_real_escape_string($str, SQL_GET_LINK());
345 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ',ret=' . $ret);
346 } elseif (function_exists('mysql_escape_string')) {
347 // The obsolete function
348 $ret = mysql_escape_string($str, SQL_GET_LINK());
350 // If nothing else works, fall back to escapeQuotes() again
351 $ret = escapeQuotes($str);
355 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ',ret=' . $ret);
358 $GLOBALS['sql_escapes']['' . $str . ''] = $ret;
362 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ',sql_escapes=' . $GLOBALS['sql_escapes']['' . $str . '']);
365 return $GLOBALS['sql_escapes']['' . $str . ''];
368 // SELECT query string from table, columns and so on... ;-)
369 function SQL_RESULT_FROM_ARRAY ($table, $columns, $idRow, $id, $F, $L) {
370 // Is columns an array?
371 if (!is_array($columns)) {
373 reportBug(__FUNCTION__, __LINE__, sprintf("columns is not an array. %s != array, file=%s, line=%s",
379 // Abort here with 'false'
383 // Is this is a simple array?
384 if ((is_array($columns[0])) && (isset($columns[0]['column']))) {
385 // Begin with SQL query
388 // No, it comes from XML, so get it back from it
389 $sql .= getSqlPartFromXmlArray($columns);
392 $sql .= " FROM `{?_MYSQL_PREFIX?}_%s` WHERE `%s`='%s' LIMIT 1";
394 // Yes, prepare the SQL statement
395 $sql = 'SELECT `' . implode('`, `', $columns) . "` FROM `{?_MYSQL_PREFIX?}_%s` WHERE `%s`='%s' LIMIT 1";
399 return SQL_QUERY_ESC($sql,
408 // ALTER TABLE wrapper function
409 function SQL_ALTER_TABLE ($sql, $F, $L, $enableCodes = TRUE) {
410 // Abort if link is down
411 if (!SQL_IS_LINK_UP()) return FALSE;
413 // This is the default result...
416 // Determine index/fulltext/unique word
419 isInString('INDEX', $sql)
421 isInString('KEY', $sql)
423 isInString('FULLTEXT', $sql)
425 isInString('UNIQUE', $sql)
429 // Extract table name
430 $tableArray = explode(' ', $sql);
431 $tableName = str_replace('`', '', $tableArray[2]);
434 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sql=' . $sql . ',tableName=' . $tableName . ',tableArray=<pre>' . print_r($tableArray, TRUE) . '</pre>,isAlterIndex=' . intval($isAlterIndex));
436 // Shall we add/drop?
437 if (((isInString('ADD', $sql)) || (isInString('DROP', $sql)) || (isInString('CHANGE', $sql))) && ($isAlterIndex === FALSE)) {
438 // Try two columns, one should fix
439 foreach (array(4,5) as $idx) {
440 // If an entry is not set, abort here
441 if (!isset($tableArray[$idx])) {
443 logDebugMessage(__FUNCTION__, __LINE__, 'columnName=' . $columnName . ',idx=' . $idx . ',sql=' . $sql . ' is missing!');
447 // And column name as well
448 $columnName = $tableArray[$idx];
451 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'columnName=' . $columnName . ',idx=' . $idx . ',sql=' . $sql . ',hasZeroNums=' . intval(ifSqlColumnExists($tableName, $columnName)));
453 // Is there no entry on ADD or an entry on DROP/CHANGE?
454 if (((!ifSqlColumnExists($tableName, $columnName)) && (isInString('ADD', $sql))) || ((ifSqlColumnExists($tableName, $columnName)) && ((isInString('DROP', $sql)) || ((isInString('CHANGE', $sql)) && ($idx == 4) && ((!ifSqlColumnExists($tableName, $tableArray[5])) || ($columnName == $tableArray[5])))))) {
456 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Executing: ' . $sql);
457 $result = SQL_QUERY($sql, $F, $L, FALSE);
459 // Skip further attempt(s)
461 } elseif ((((ifSqlColumnExists($tableName, $columnName)) && (isInString('ADD', $sql))) || ((!ifSqlColumnExists($tableName, $columnName)) && ((isInString('DROP', $sql))) || (isInString('CHANGE', $sql)))) && ($columnName != 'KEY')) {
462 // Abort here because it is alreay there
463 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Skipped: sql=' . $sql . ',columnName=' . $columnName . ',idx=' . $idx);
465 } elseif ((!ifSqlColumnExists($tableName, $columnName)) && (isInString('DROP', $sql))) {
466 // Abort here because we tried to drop a column which is not there (never created maybe)
467 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'No drop: ' . $sql);
469 } elseif ($columnName != 'KEY') {
470 // Something didn't fit, we better log it
471 logDebugMessage(__FUNCTION__, __LINE__, 'Possible problem: ' . $sql . ',hasZeroNums=' . intval(ifSqlColumnExists($tableName, $columnName)) . '');
474 } elseif ((getTableType() == 'InnoDB') && (isInString('FULLTEXT', $sql))) {
475 // Skip this query silently because InnoDB does not understand fulltext indexes
476 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("Skipped FULLTEXT: sql=%s,tableName=%s,hasZeroNums=%d,file=%s,line=%s", $sql, $tableName, intval((is_bool($result)) ? 0 : ifSqlColumnExists($columnName)), $F, $L));
477 } elseif ($isAlterIndex === TRUE) {
478 // And column name as well without backticks
479 $keyName = str_replace('`', '', $tableArray[5]);
480 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'keyName=' . $keyName . ',tableArray=<pre>' . print_r($tableArray, TRUE) . '</pre>');
482 // Is this "UNIQUE" or so? FULLTEXT has been handled the elseif() block above
483 if (in_array(strtoupper($tableArray[4]), array('INDEX', 'UNIQUE', 'KEY', 'FULLTEXT'))) {
487 while (isInString(',', $keyName)) {
489 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'keyName=' . $keyName . 'begin=' . $begin . ' - BEFORE');
490 $keyName = str_replace('`', '', $tableArray[count($tableArray) - $begin]);
491 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'keyName=' . $keyName . 'begin=' . $begin . ' - BETWEEN');
494 $keyName = str_replace(array('(', ')'), array('', ''), $keyName);
495 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'keyName=' . $keyName . 'begin=' . $begin . ' - AFTER');
503 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ', tableArray[3]=' . $tableArray[3] . ',keyName=' . $keyName);
504 if (($tableArray[3] == 'ADD') && (!isSqlTableIndexFound($tableName, $keyName))) {
505 // Send it to the SQL_QUERY() function to add it
506 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sql=' . $sql . ' - ADDING!');
507 $result = SQL_QUERY($sql, $F, $L, $enableCodes);
508 } elseif (($tableArray[3] == 'DROP') && (isSqlTableIndexFound($tableName, $keyName))) {
509 // Send it to the SQL_QUERY() function to drop it
510 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sql=' . $sql . ' - DROPPING!');
511 $result = SQL_QUERY($sql, $F, $L, $enableCodes);
514 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Not executed: ' . $sql);
517 // Other ALTER TABLE query
518 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $sql);
519 $result = SQL_QUERY($sql, $F, $L, $enableCodes);
526 // Getter for SQL link
527 function SQL_GET_LINK () {
531 // Is it in the globals?
532 if (isset($GLOBALS['sql_link'])) {
534 $link = $GLOBALS['sql_link'];
542 function SQL_SET_LINK ($link) {
543 // Is this a resource or null?
544 if ((ifFatalErrorsDetected()) && (isInstallationPhase())) {
545 // This may happen in installation phase
547 } elseif ((!is_resource($link)) && (!is_null($link))) {
548 // This should never happen!
549 reportBug(__FUNCTION__, __LINE__, sprintf("Type of link is not resource or null, type=%s", gettype($link)));
553 $GLOBALS['sql_link'] = $link;
556 $GLOBALS['is_sql_link_up'] = is_resource($link);
559 // Checks if the link is up
560 function SQL_IS_LINK_UP () {
561 // Is there cached this?
562 if (!isset($GLOBALS['is_sql_link_up'])) {
564 $GLOBALS['is_sql_link_up'] = is_resource(SQL_GET_LINK());
568 return $GLOBALS['is_sql_link_up'];
571 // Wrapper function to make code more readable
572 function SQL_HASZERONUMS ($result) {
573 // Just pass it through
574 return (SQL_NUMROWS($result) === 0);
577 // Wrapper function to make code more readable
578 function SQL_HASZEROAFFECTED () {
579 // Just pass it through
580 return (SQL_AFFECTEDROWS() === 0);
583 // Private function to prepare the SQL query string
584 function SQL_PREPARE_SQL_STRING ($sqlString, $enableCodes = TRUE) {
586 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString=' . $sqlString . ',enableCodes=' . intval($enableCodes) . ' - ENTERED!');
588 // Is it already cached?
589 if (!isset($GLOBALS['sql_strings']['' . $sqlString . ''])) {
590 // Preserve escaping and compile URI codes+config+expression code
591 $sqlString2 = FILTER_COMPILE_EXPRESSION_CODE(FILTER_COMPILE_CONFIG($sqlString));
594 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString2=' . $sqlString2);
596 // Do final compilation and revert {ESCAPE}
597 $GLOBALS['sql_strings']['' . $sqlString . ''] = doFinalCompilation($sqlString2, FALSE, $enableCodes);
600 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString=' . $sqlString . ' - CACHE!');
604 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString=' . $sqlString . ',enableCodes=' . intval($enableCodes) . ',sql_strings=' . $GLOBALS['sql_strings']['' . $sqlString . ''] . ' - EXIT!');
607 return $GLOBALS['sql_strings']['' . $sqlString . ''];
610 // Creates a MySQL TIMESTAMP compatible string from given Uni* timestamp
611 function SQL_EPOCHE_TO_TIMESTAMP ($timestamp) {
612 return generateDateTime($timestamp, 7);
615 // Log SQL errors to debug.log in installation phase or call reportBug()
616 function SQL_ERROR ($file, $line, $message) {
617 // Remember plain error in last_sql_error
618 $GLOBALS['last_sql_error'] = mysql_error();
620 // Is there installation phase?
621 if (isInstallationPhase()) {
623 * In installation phase, we don't want SQL errors abort e.g. connection
624 * tests, so just log it away.
626 logDebugMessage($file, $line, $message);
628 // Regular mode, then call reportBug()
629 reportBug($file, $line, $message);
633 // Check if there is a SQL table created
634 function ifSqlTableExists ($tableName) {
635 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ' - ENTERED!');
637 if (!isset($GLOBALS[__FUNCTION__][$tableName])) {
638 // Check if the table is there
639 $result = SQL_QUERY_ESC("SHOW TABLES FROM `{?__DB_NAME?}` WHERE `Tables_in_{?__DB_NAME?}`='{?_MYSQL_PREFIX?}_%s'",
640 array($tableName), __FILE__, __LINE__);
642 // Is there an entry?
643 $GLOBALS[__FUNCTION__][$tableName] = (SQL_NUMROWS($result) == 1);
644 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',numRows=' . intval($GLOBALS[__FUNCTION__][$tableName]));
648 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',result=' . intval($GLOBALS[__FUNCTION__][$tableName]) . ' - EXIT!');
649 return $GLOBALS[__FUNCTION__][$tableName];
652 // Is a table column there?
653 function ifSqlColumnExists ($tableName, $columnName) {
654 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',columnName=' . $columnName . ' - ENTERED!');
656 if (!isset($GLOBALS[__FUNCTION__][$tableName][$columnName])) {
657 // And column name as well
658 $columnName = str_replace('`', '', $columnName);
660 // Get column information
661 $result = SQL_QUERY_ESC("SHOW COLUMNS FROM `%s` LIKE '%s'",
665 ), __FUNCTION__, __LINE__);
668 $GLOBALS[__FUNCTION__][$tableName][$columnName] = (!SQL_HASZERONUMS($result));
669 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',columnName=' . $columnName . ',hasZeroNums=' . intval(SQL_HASZERONUMS($result)) . ',numRows=' . intval($GLOBALS[__FUNCTION__][$tableName][$columnName]));
672 SQL_FREERESULT($result);
676 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',columnName=' . $columnName . ',result=' . intval($GLOBALS[__FUNCTION__][$tableName][$columnName]) . ' - EXIT!');
677 return $GLOBALS[__FUNCTION__][$tableName][$columnName];
680 // Checks depending on the mode if the index is there
681 function isSqlTableIndexFound ($tableName, $keyName) {
682 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',columnName=' . $keyName . ' - ENTERED!');
684 if (!isset($GLOBALS[__FUNCTION__][$tableName][$keyName])) {
686 $result = SQL_QUERY_ESC("SHOW INDEX FROM `%s`", array($tableName), __FUNCTION__, __LINE__);
688 // The column is not found by default
689 $GLOBALS[__FUNCTION__][$tableName][$keyName] = FALSE;
692 while ($content = SQL_FETCHARRAY($result)) {
693 // Add all entries for better caching behavior
694 $GLOBALS[__FUNCTION__][$tableName][$content['Key_name']] = TRUE;
698 SQL_FREERESULT($result);
701 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',columnName=' . $keyName . ',result=' . intval($GLOBALS[__FUNCTION__][$tableName][$keyName]) . ' - CACHE!');
705 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',columnName=' . $keyName . ',result=' . intval($GLOBALS[__FUNCTION__][$tableName][$keyName]) . ' - EXIT!');
706 return $GLOBALS[__FUNCTION__][$tableName][$keyName];
709 // Init database layer
710 function initDatabaseLayer () {
711 // Set all required variables:
712 $GLOBALS['last_sql_error'] = '';
715 // Get last SQL error
716 function getLastSqlError () {
717 return $GLOBALS['last_sql_error'];
720 // Gets an array (or false if none is found) from all supported engines
721 function getArrayFromSupportedSqlEngines ($support = 'YES') {
725 // This also worked, now we need to check if the selected database type is supported
726 $result = SQL_QUERY('SHOW ENGINES', __FUNCTION__, __LINE__);
728 // Are there entries? (Bad if not)
729 if (SQL_NUMROWS($result) > 0) {
730 // Load all and check for active entries
731 while ($content = SQL_FETCHARRAY($result)) {
732 // Is this supported?
733 if (($support == 'ALL') || ($content['Support'] == $support)) {
735 array_push($engines, $content);
736 } elseif (isDebugModeEnabled()) {
737 // Log it away in debug mode
738 logDebugMessage(__FUNCTION__, __LINE__, 'Engine ' . $content['Engine'] . ' is not supported (' . $content['Supported'] . ')');
747 SQL_FREERESULT($result);