X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=inc%2Fsql-functions.php;h=fae901b2733c39f2ef10059e8474e31e79648685;hb=15b33f67108fcf717fc56fc9cb931f7c22a69737;hp=15fe91d98c0d5edfde224d217f11f13f2b5ab843;hpb=263a089d8a499e0e26d0af9e7aa7639f88b8ca60;p=mailer.git diff --git a/inc/sql-functions.php b/inc/sql-functions.php index 15fe91d98c..fae901b273 100644 --- a/inc/sql-functions.php +++ b/inc/sql-functions.php @@ -6,19 +6,18 @@ * -------------------------------------------------------------------- * * File : sql-functions.php * * -------------------------------------------------------------------- * - * Short description : All MySQL-related functions * + * Short description : SQL functions to handle queries * * -------------------------------------------------------------------- * - * Kurzbeschreibung : Alle MySQL-Relevanten Funktionen * + * Kurzbeschreibung : SQL-Funktionen fuer Queries * * -------------------------------------------------------------------- * * $Revision:: $ * * $Date:: $ * * $Tag:: 0.2.1-FINAL $ * * $Author:: $ * - * Needs to be in all Files and every File needs "svn propset * - * svn:keywords Date Revision" (autoprobset!) at least!!!!!! * * -------------------------------------------------------------------- * * Copyright (c) 2003 - 2009 by Roland Haeder * - * For more information visit: http://www.mxchange.org * + * Copyright (c) 2009 - 2011 by Mailer Developer Team * + * For more information visit: http://mxchange.org * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -53,6 +52,7 @@ function isSqlsInitialized () { // Setter for SQLs array function setSqlsArray ($SQLs) { + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'count()='.count($SQLs)); $GLOBALS['sqls'] = (array) $SQLs; } @@ -114,7 +114,7 @@ function countSqls () { // Checks wether the SQLs array is filled function isSqlsValid () { - //* DEBUG: */ print __FUNCTION__.':'.intval(isSqlsInitialized()).'/'.countSqls().'/'.getCurrentExtensionName().'
'; + //* DEBUG: */ debugOutput(__FUNCTION__ . ':' . intval(isSqlsInitialized()) . '/' . countSqls() . '/' . getCurrentExtensionName()); return ( ( isSqlsInitialized() @@ -124,5 +124,98 @@ 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 + + // Do we have 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 .= "'" . SQL_ESCAPE($value) . "',"; + } + } // END - foreach + + // Finish SQL query + $SQL = substr($SQL, 0, -1) . ')'; + + // Return SQL query + return $SQL; +} + // [EOF] ?>