X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=inc%2Fsql-functions.php;h=e61f3938041f1fbd57f12cc4327744f1167fe54e;hb=64b24587e9e280798311c903cb11440c62f2844d;hp=6e6e3bd28dfb5953fdb58ab75b157e791df9ed81;hpb=20741b93fd58620af677a7f1039ffd16ea6ec689;p=mailer.git diff --git a/inc/sql-functions.php b/inc/sql-functions.php index 6e6e3bd28d..e61f393804 100644 --- a/inc/sql-functions.php +++ b/inc/sql-functions.php @@ -16,7 +16,7 @@ * $Author:: $ * * -------------------------------------------------------------------- * * Copyright (c) 2003 - 2009 by Roland Haeder * - * Copyright (c) 2009 - 2011 by Mailer Developer Team * + * Copyright (c) 2009 - 2012 by Mailer Developer Team * * For more information visit: http://mxchange.org * * * * This program is free software; you can redistribute it and/or modify * @@ -42,10 +42,11 @@ if (!defined('__SECURITY')) { // Init SQLs array function initSqls () { - setSqlsArray(array()); + // Init generic array + setSqlsArray(array('generic' => array())); } -// Checks wether the sqls array is initialized +// Checks whether the sqls array is initialized function isSqlsInitialized () { return ((isset($GLOBALS['sqls'])) && (is_array($GLOBALS['sqls']))); } @@ -69,7 +70,7 @@ function getSqls () { // Add an SQL to the list function addSql ($sql) { //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("sql=%s, count=%d", $sql, countSqls())); - $GLOBALS['sqls']['generic'][] = (string) $sql; + array_push($GLOBALS['sqls']['generic'], $sql); } // Merge SQLs together @@ -112,7 +113,7 @@ function countSqls () { return $count; } -// Checks wether the SQLs array is filled +// Checks whether the SQLs array is filled function isSqlsValid () { //* DEBUG: */ debugOutput(__FUNCTION__ . ':' . intval(isSqlsInitialized()) . '/' . countSqls() . '/' . getCurrentExtensionName()); return ( @@ -124,5 +125,158 @@ function isSqlsValid () { ); } +// Generates an updating SQL query from given array +function getUpdateSqlFromArray ($array, $tableName, $whereColumn, $whereData, $excludedFields, $multiDimId = NULL) { + // Begin SQL query + $SQL = 'UPDATE `{?_MYSQL_PREFIX?}_' . $tableName . '` SET '; + + // Insert all data + foreach ($array as $entry => $value) { + // Skip login/id entry + if (in_array($entry, $excludedFields)) { + continue; + } // END - if + + // Is there a non-string (e.g. number, NULL, SQL function or back-tick at the beginning? + if (is_null($multiDimId)) { + // Handle one-dimensional data + if (is_null($value)) { + // NULL detected + $SQL .= '`' . $entry . '`=NULL,'; + } elseif ((substr($value, -2, 2) == '()') || (substr($value, 0, 1) == '`')) { + // SQL function needs no ticks (') + $SQL .= '`' . $entry . '`=' . SQL_ESCAPE($value) . ','; + } elseif ('' . bigintval($value, TRUE, FALSE) . '' == '' . $value . '') { + // No need for ticks (') + $SQL .= '`' . $entry . '`=' . $value . ','; + } else { + // Strings need ticks (') around them + $SQL .= '`' . $entry . "`='" . SQL_ESCAPE($value) . "',"; + } + } else { + // Handle multi-dimensional data + if (is_null($value[$multiDimId])) { + // NULL detected + $SQL .= '`' . $entry . '`=NULL,'; + } elseif ((substr($value[$multiDimId], -2, 2) == '()') || (substr($value[$multiDimId], 0, 1) == '`')) { + // SQL function needs no ticks (') + $SQL .= '`' . $entry . '`=' . SQL_ESCAPE($value[$multiDimId]) . ','; + } elseif (('' . bigintval($value[$multiDimId], TRUE, FALSE) . '' == '' . $value[$multiDimId] . '')) { + // No need for ticks (') + $SQL .= '`' . $entry . '`=' . $value[$multiDimId] . ','; + } else { + // Strings need ticks (') around them + $SQL .= '`' . $entry . "`='" . SQL_ESCAPE($value[$multiDimId]) . "',"; + } + } + } // END - foreach + + // Remove last 2 chars and finish query + $SQL = substr($SQL, 0, -1) . ' WHERE `' . $whereColumn . '`=' . $whereData . ' LIMIT 1'; + + // Return SQL query + return $SQL; +} + +// "Getter" for an "INSERT INTO" SQL query +function getInsertSqlFromArray ($array, $tableName) { + // Init SQL + $SQL = 'INSERT INTO +`{?_MYSQL_PREFIX?}_' . $tableName . '` +( +`' . implode('`, `', array_keys(postRequestArray())) . '` +) VALUES ('; + + // Walk through all entries + foreach (postRequestArray() as $key => $value) { + // Log debug message + //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',key=' . $key . ',value=' . $value); + + // Add all entries + if (is_null($value)) { + // Add NULL + $SQL .= 'NULL,'; + } elseif (substr($value, -2, 2) == '()') { + // SQL function needs no ticks (') + $SQL .= SQL_ESCAPE($value) . ','; + } elseif ('' . bigintval($value, TRUE, FALSE) . '' == '' . $value . '') { + // Number detected, no need for ticks (') + $SQL .= bigintval($value) . ','; + } elseif ('' . (float) $value . '' == '' . $value . '') { + // Float number detected + $SQL .= sprintf('%01.5f', $value); + } else { + // Everything else might be a string, so add ticks around it + $SQL .= chr(39) . SQL_ESCAPE($value) . chr(39) . ','; + } + } // END - foreach + + // Finish SQL query + $SQL = substr($SQL, 0, -1) . ')'; + + // Return SQL query + return $SQL; +} + +// Initializes the SQL link by bringing it up if set +function initSqlLink () { + // Do this only if link is down + assert(!SQL_IS_LINK_UP()); + + // Is the configuration data set? + if ((!empty($GLOBALS['mysql']['host'])) && (!empty($GLOBALS['mysql']['login'])) && (!empty($GLOBALS['mysql']['dbase']))) { + // Remove cache + unset($GLOBALS['is_sql_link_up']); + + // Connect to DB + SQL_CONNECT($GLOBALS['mysql']['host'], $GLOBALS['mysql']['login'], $GLOBALS['mysql']['password'], __FILE__, __LINE__); + + // Is the link valid? + if (SQL_IS_LINK_UP()) { + // Enable exit on error + enableExitOnError(); + + // Is it a valid resource? + if (SQL_SELECT_DB($GLOBALS['mysql']['dbase'], __FILE__, __LINE__) === TRUE) { + // Set database name (required for ext-optimize and ifSqlTableExists()) + setConfigEntry('__DB_NAME', $GLOBALS['mysql']['dbase']); + + // Remove MySQL array from namespace + unset($GLOBALS['mysql']); + + // Load cache + loadIncludeOnce('inc/load_cache.php'); + } else { + // Wrong database? + reportBug(__FILE__, __LINE__, 'Wrong database selected.'); + } + } else { + // No link to database! + reportBug(__FILE__, __LINE__, 'Database link is not yet up.'); + } + } else { + // Maybe you forgot to enter your database login? + reportBug(__FILE__, __LINE__, 'Database login is missing.'); + } +} + +// Imports given SQL dump from given (relative) path and adds them to $sqlPool +function importSqlDump ($path, $dumpName, $sqlPool) { + // Construct FQFN + $FQFN = getPath() . $path . '/' . $dumpName . '.sql'; + + // Is the file readable? + if (!isFileReadable($FQFN)) { + // Not found, which is bad + reportBug(__FILE__, __LINE__, sprintf("SQL dump %s/%s.sql is not readable.", $path, $dumpName)); + } // END - if + + // Then read it + $fileContent = readSqlDump($FQFN); + + // Merge it with existing SQL statements + mergeSqls(explode(";\n", $fileContent), $sqlPool); +} + // [EOF] ?>