]> git.mxchange.org Git - mailer.git/blobdiff - inc/db/lib-mysql3.php
Some more rewrites to better functions
[mailer.git] / inc / db / lib-mysql3.php
index 9dfdfcf342556f9f52076d2755e9350715f35a30..4dfca09fd0a1bb53fe2b6bb4b24323125217ffd6 100644 (file)
@@ -66,7 +66,7 @@ function SQL_QUERY ($sqlString, $F, $L, $enableCodes = TRUE) {
                }
 
                // Remove \t, \n and \r from queries they may confuse some MySQL versions
-               $sqlStringModified = str_replace(array(chr(9), chr(10), chr(13)), array(' ', ' ', ' '), $sqlStringModified);
+               $sqlStringModified = str_replace(array(chr(9), PHP_EOL, chr(13)), array(' ', ' ', ' '), $sqlStringModified);
 
                // Compile config entries out
                $sqlStringModified = SQL_PREPARE_SQL_STRING($sqlStringModified, $enableCodes);
@@ -118,7 +118,7 @@ function SQL_QUERY ($sqlString, $F, $L, $enableCodes = TRUE) {
                } // END - if
 
                // Append debug line
-               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']));
+               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), PHP_EOL), array('', ' '), $GLOBALS['last_sql']));
        } // END - if
 
        // Increment stats entry
@@ -275,7 +275,7 @@ function SQL_QUERY_ESC ($sqlString, $data, $F, $L, $run = TRUE, $strip = TRUE, $
                return FALSE;
        } // END - if
 
-       // Init array for escaped data with SQL string
+       // Init array for escape'd data with SQL string
        $dataSecured = array(
                '__sql_string' => $sqlString
        );
@@ -501,11 +501,11 @@ function SQL_ALTER_TABLE ($sql, $F, $L, $enableCodes = TRUE) {
 
                // Shall we run it?
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ', tableArray[3]=' . $tableArray[3] . ',keyName=' . $keyName);
-               if (($tableArray[3] == 'ADD') && (!isSqlTableIndexFound($tableName, $keyName))) {
+               if (($tableArray[3] == 'ADD') && (!ifSqlTableIndexExist($tableName, $keyName))) {
                        // Send it to the SQL_QUERY() function to add it
                        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sql=' . $sql . ' - ADDING!');
                        $result = SQL_QUERY($sql, $F, $L, $enableCodes);
-               } elseif (($tableArray[3] == 'DROP') && (isSqlTableIndexFound($tableName, $keyName))) {
+               } elseif (($tableArray[3] == 'DROP') && (ifSqlTableIndexExist($tableName, $keyName))) {
                        // Send it to the SQL_QUERY() function to drop it
                        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sql=' . $sql . ' - DROPPING!');
                        $result = SQL_QUERY($sql, $F, $L, $enableCodes);
@@ -587,13 +587,13 @@ function SQL_PREPARE_SQL_STRING ($sqlString, $enableCodes = TRUE) {
 
        // Is it already cached?
        if (!isset($GLOBALS['sql_strings']['' . $sqlString . ''])) {
-               // Compile URI codes+config+expression code
-               $sqlString2 = FILTER_COMPILE_EXPRESSION_CODE(FILTER_COMPILE_CONFIG(compileUriCode($sqlString)));
+               // Preserve escaping and compile URI codes+config+expression code
+               $sqlString2 = FILTER_COMPILE_EXPRESSION_CODE(FILTER_COMPILE_CONFIG($sqlString));
 
                // Debug message
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString2=' . $sqlString2);
 
-               // Do final compilation
+               // Do final compilation and revert {ESCAPE}
                $GLOBALS['sql_strings']['' . $sqlString . ''] = doFinalCompilation($sqlString2, FALSE, $enableCodes);
        } else {
                // Log message
@@ -639,6 +639,19 @@ function ifSqlTableExists ($tableName) {
                $result = SQL_QUERY_ESC("SHOW TABLES FROM `{?__DB_NAME?}` WHERE `Tables_in_{?__DB_NAME?}`='{?_MYSQL_PREFIX?}_%s'",
                        array($tableName), __FILE__, __LINE__);
 
+               // Is a link there?
+               if (!is_resource($result)) {
+                       // Is installation phase?
+                       if (isInstallationPhase()) {
+                               // Then silently abort here
+                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ',isLinkUp=' . intval(SQL_IS_LINK_UP()) . ',tableName=' . $tableName . ' - Returning FALSE ...');
+                               return FALSE;
+                       } else {
+                               // Please report this
+                               reportBug(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ' is not a resource.');
+                       }
+               } // END - if
+
                // Is there an entry?
                $GLOBALS[__FUNCTION__][$tableName] = (SQL_NUMROWS($result) == 1);
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',numRows=' . intval($GLOBALS[__FUNCTION__][$tableName]));
@@ -657,6 +670,12 @@ function ifSqlColumnExists ($tableName, $columnName) {
                // And column name as well
                $columnName = str_replace('`', '', $columnName);
 
+               // If the table is not there, it is okay
+               if (!ifSqlTableExists($tableName)) {
+                       // Then abort here
+                       return FALSE;
+               } // END - if
+
                // Get column information
                $result = SQL_QUERY_ESC("SHOW COLUMNS FROM `%s` LIKE '%s'",
                        array(
@@ -664,6 +683,19 @@ function ifSqlColumnExists ($tableName, $columnName) {
                                $columnName
                        ), __FUNCTION__, __LINE__);
 
+               // Is a link there?
+               if (!is_resource($result)) {
+                       // Is installation phase?
+                       if (isInstallationPhase()) {
+                               // Then silently abort here
+                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ',isLinkUp=' . intval(SQL_IS_LINK_UP()) . ',tableName=' . $tableName . ',columnName=' . $columnName . ' - Returning FALSE ...');
+                               return FALSE;
+                       } else {
+                               // Please report this
+                               reportBug(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ' is not a resource.');
+                       }
+               } // END - if
+
                // 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]));
@@ -678,13 +710,32 @@ function ifSqlColumnExists ($tableName, $columnName) {
 }
 
 // Checks depending on the mode if the index is there
-function isSqlTableIndexFound ($tableName, $keyName) {
+function ifSqlTableIndexExist ($tableName, $keyName) {
        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',columnName=' . $keyName . ' - ENTERED!');
        // Is there cache?
        if (!isset($GLOBALS[__FUNCTION__][$tableName][$keyName])) {
+               // If the table is not there, it is okay
+               if (!ifSqlTableExists($tableName)) {
+                       // Then abort here
+                       return FALSE;
+               } // END - if
+
                // Show indexes
                $result = SQL_QUERY_ESC("SHOW INDEX FROM `%s`", array($tableName), __FUNCTION__, __LINE__);
 
+               // Is a link there?
+               if (!is_resource($result)) {
+                       // Is installation phase?
+                       if (isInstallationPhase()) {
+                               // Then silently abort here
+                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ',isLinkUp=' . intval(SQL_IS_LINK_UP()) . ',tableName=' . $tableName . ',keyName=' . $keyName . ' - Returning FALSE ...');
+                               return FALSE;
+                       } else {
+                               // Please report this
+                               reportBug(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ' is not a resource.');
+                       }
+               } // END - if
+
                // The column is not found by default
                $GLOBALS[__FUNCTION__][$tableName][$keyName] = FALSE;
 
@@ -718,7 +769,7 @@ function getLastSqlError () {
 }
 
 // Gets an array (or false if none is found) from all supported engines
-function getArrayFromSupportedSqlEngines ($support = 'YES') {
+function getArrayFromSupportedSqlEngines ($requestedEngine = 'ALL') {
        // Init array
        $engines = array();
 
@@ -726,16 +777,19 @@ function getArrayFromSupportedSqlEngines ($support = 'YES') {
        $result = SQL_QUERY('SHOW ENGINES', __FUNCTION__, __LINE__);
 
        // Are there entries? (Bad if not)
-       if (SQL_NUMROWS($result) > 0) {
+       if (!SQL_HASZERONUMS($result)) {
                // Load all and check for active entries
                while ($content = SQL_FETCHARRAY($result)) {
+                       // Debug message
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'support=' . $requestedEngine . ',Engine=' . $content['Engine'] . ',Support=' . $content['Support']);
+
                        // Is this supported?
-                       if (($support == 'ALL') || ($content['Support'] == $support)) {
+                       if ((($requestedEngine == 'ALL') || ($content['Engine'] == $requestedEngine)) && (in_array($content['Support'], array('YES', 'DEFAULT')))) {
                                // Add it
                                array_push($engines, $content);
                        } elseif (isDebugModeEnabled()) {
                                // Log it away in debug mode
-                               logDebugMessage(__FUNCTION__, __LINE__, 'Engine ' . $content['Engine'] . ' is not supported (' . $content['Supported'] . ')');
+                               logDebugMessage(__FUNCTION__, __LINE__, 'Engine ' . $content['Engine'] . ' is not supported (' . $content['Supported'] . ' - ' . $requestedEngine . ')');
                        }
                } // END - if
        } else {