+// Getter for SQL link
+function SQL_GET_LINK () {
+ // Init link
+ $link = null;
+
+ // Is it in the globals?
+ if (isset($GLOBALS['sql_link'])) {
+ // Then take it
+ $link = $GLOBALS['sql_link'];
+ } // END - if
+
+ // Return it
+ return $link;
+}
+
+// Setter for link
+function SQL_SET_LINK ($link) {
+ // Is this a resource or null?
+ if ((ifFatalErrorsDetected()) && (isInstallationPhase())) {
+ // This may happen in installation phase
+ return;
+ } elseif ((!is_resource($link)) && (!is_null($link))) {
+ // This should never happen!
+ debug_report_bug(__FUNCTION__, __LINE__, sprintf("Type of link is not resource or null, type=%s", gettype($link)));
+ } // END - if
+
+ // Set it
+ $GLOBALS['sql_link'] = $link;
+
+ // Re-init cache
+ $GLOBALS['is_sql_link_up'] = is_resource($link);
+}
+
+// Checks if the link is up
+function SQL_IS_LINK_UP () {
+ // Default is not up
+ $linkUp = false;
+
+ // Do we have cached this?
+ if (isset($GLOBALS['is_sql_link_up'])) {
+ // Then use this
+ $linkUp = $GLOBALS['is_sql_link_up'];
+ } else {
+ // Get it
+ $linkUp = is_resource(SQL_GET_LINK());
+
+ // And cache it
+ $GLOBALS['is_sql_link_up'] = $linkUp;
+ }
+
+ // Return the result
+ return $linkUp;
+}
+
+// Wrapper function to make code more readable
+function SQL_HASZERONUMS ($result) {
+ // Just pass it through
+ return (SQL_NUMROWS($result) === 0);
+}
+
+// Wrapper function to make code more readable
+function SQL_HASZEROAFFECTED () {
+ // Just pass it through
+ return (SQL_AFFECTEDROWS() === 0);
+}
+
+// Private function to prepare the SQL query string
+function SQL_PREPARE_SQL_STRING ($sqlString, $enableCodes = true) {
+ // Is it already cached?
+ if (!isset($GLOBALS['sql_strings'][$sqlString])) {
+ // Compile config+expression code
+ $sqlString2 = FILTER_COMPILE_EXPRESSION_CODE(FILTER_COMPILE_CONFIG($sqlString));
+
+ // Do final compilation
+ $GLOBALS['sql_strings'][$sqlString] = doFinalCompilation($sqlString2, false, $enableCodes);
+ //die($sqlString.'<br />'.$sqlString2.'<br />'.$GLOBALS['sql_strings'][$sqlString]);
+ } // END - if
+
+ // Return it
+ return $GLOBALS['sql_strings'][$sqlString];
+}
+
+// Check if there is a SQL table created
+function isSqlTableCreated ($tableName) {
+ // Do we have cache?
+ if (!isset($GLOBALS[__FUNCTION__][$tableName])) {
+ // Check if the table is there
+ $result = SQL_QUERY_ESC("SHOW TABLES FROM `{?__DB_NAME?}` WHERE `Tables_in_{?__DB_NAME?}`='{?_MYSQL_PREFIX?}_%s'",
+ array($tableName), __FILE__, __LINE__);
+
+ // Is there an entry?
+ $GLOBALS[__FUNCTION__][$tableName] = (SQL_NUMROWS($result) == 1);
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',numRows=' . intval($GLOBALS[__FUNCTION__][$tableName]));
+ } // END - if
+
+ // Return cache
+ return $GLOBALS[__FUNCTION__][$tableName];
+}
+
+// Is a table column there?
+function isSqlTableColumnFound ($tableName, $columnName) {
+ // Do we have cache?
+ if (!isset($GLOBALS[__FUNCTION__][$tableName][$columnName])) {
+ // And column name as well
+ $columnName = str_replace('`', '', $columnName);
+
+ // Get column information
+ $result = SQL_QUERY_ESC("SHOW COLUMNS FROM `%s` LIKE '%s'",
+ array($tableName, $columnName), __FUNCTION__, __LINE__);
+
+ // Determine it
+ $GLOBALS[__FUNCTION__][$tableName][$columnName] = (!SQL_HASZERONUMS($result));
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',columnName=' . $columnName . ',hasZeroNums=' . intval(SQL_HASZERONUMS($result)) . ',numRows=' . intval($GLOBALS[__FUNCTION__][$tableName][$columnName]));
+
+ // Free result
+ SQL_FREERESULT($result);
+ } // END - if
+
+ // Return cache
+ return $GLOBALS[__FUNCTION__][$tableName][$columnName];
+}
+
+// Checks depending on the mode if the table is there
+function isSqlTableIndexAdded ($tableName, $mode, $keyName) {
+ // Do we have cache?
+ if (!isset($GLOBALS[__FUNCTION__][$tableName][$mode])) {
+ // Show indexes
+ $result = SQL_QUERY_ESC("SHOW INDEX FROM `%s`", array($tableName), __FUNCTION__, __LINE__);
+
+ // Non-skipping is default for ADD
+ $GLOBALS[__FUNCTION__][$tableName][$mode] = false;
+
+ // But should we DROP?
+ if ($mode == 'DROP') {
+ // Then skip if nothing found
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Going to drop key ' . $keyName);
+ $GLOBALS[__FUNCTION__][$tableName][$mode] = true;
+ } // END - if
+
+ // Walk through all
+ while ($content = SQL_FETCHARRAY($result)) {
+ // Is it found?
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'columnName='.$keyName.',content=<pre>' . print_r($content, true) . '</pre>');
+ if (($content['Key_name'] == $keyName) && ($mode == 'ADD')) {
+ // Skip this query!
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("ADD: Skiped: %s", $sql));
+ $GLOBALS[__FUNCTION__][$tableName][$mode] = true;
+ break;
+ } elseif (($content['Key_name'] == $keyName) && ($mode == 'DROP')) {
+ // Don't skip this!
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("DROP: Not skiped: %s", $sql));
+ $GLOBALS[__FUNCTION__][$tableName][$mode] = false;
+ break;
+ }
+ } // END - while
+
+ // Free result
+ SQL_FREERESULT($result);
+ } // END - if
+
+ // Return cache
+ return $GLOBALS[__FUNCTION__][$tableName][$mode];
+}
+
+// [EOF]