Fixes and cleanups
[mailer.git] / inc / sql-functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 10/23/2009 *
4  * ===================                          Last change: 10/23/2009 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : sql-functions.php                                *
8  * -------------------------------------------------------------------- *
9  * Short description : SQL functions to handle queries                  *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : SQL-Funktionen fuer Queries                      *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
20  * For more information visit: http://mxchange.org                      *
21  *                                                                      *
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.                                  *
26  *                                                                      *
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.                         *
31  *                                                                      *
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,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 // Init SQLs array
44 function initSqls () {
45         setSqlsArray(array());
46 }
47
48 // Checks wether the sqls array is initialized
49 function isSqlsInitialized () {
50         return ((isset($GLOBALS['sqls'])) && (is_array($GLOBALS['sqls'])));
51 }
52
53 // Setter for SQLs array
54 function setSqlsArray ($SQLs) {
55         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'count()='.count($SQLs));
56         $GLOBALS['sqls'] = (array) $SQLs;
57 }
58
59 // Remover for SQLs array
60 function unsetSqls () {
61         unset($GLOBALS['sqls']);
62 }
63
64 // Getter for SQLs array
65 function getSqls () {
66         return $GLOBALS['sqls'];
67 }
68
69 // Add an SQL to the list
70 function addSql ($sql) {
71         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("sql=%s, count=%d", $sql, countSqls()));
72         $GLOBALS['sqls']['generic'][] = (string) $sql;
73 }
74
75 // Merge SQLs together
76 function mergeSqls ($SQLs, $type = '') {
77         // Should we merge full array or partial?
78         if (empty($type)) {
79                 // Merge full array (may kill entries)
80                 setSqlsArray(merge_array(getSqls(), $SQLs));
81         } else {
82                 // Merge sub array, so get it
83                 $array = getSqls();
84
85                 // Is the sub array there?
86                 if (isset($array[$type])) {
87                         // Then get it and merge it with the new one
88                         $array[$type] = merge_array($array[$type], $SQLs);
89                 } else {
90                         // Use new array
91                         $array[$type] = $SQLs;
92                 }
93
94                 // Call again..
95                 mergeSqls($array);
96         }
97 }
98
99 // Counter for SQLs array
100 function countSqls () {
101         // Default is false
102         $count = '0';
103
104         // Is the array there?
105         if (isSqlsInitialized()) {
106                 // Then count it
107                 $count = count($GLOBALS['sqls']);
108                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("count=%d", $count));
109         } // END - if
110
111         // Return it
112         return $count;
113 }
114
115 // Checks wether the SQLs array is filled
116 function isSqlsValid () {
117         //* DEBUG: */ debugOutput(__FUNCTION__ . ':' . intval(isSqlsInitialized()) . '/' . countSqls() . '/' . getCurrentExtensionName());
118         return (
119                 (
120                         isSqlsInitialized()
121                 ) && (
122                         countSqls() > 0
123                 )
124         );
125 }
126
127 // Generates an updating SQL query from given array
128 function getUpdateSqlFromArray ($array, $tableName, $whereColumn, $whereData, $excludedFields, $multiDimId = NULL) {
129         // Begin SQL query
130         $SQL = 'UPDATE `{?_MYSQL_PREFIX?}_' . $tableName . '` SET ';
131
132         // Insert all data
133         foreach ($array as $entry => $value) {
134                 // Skip login/id entry
135                 if (in_array($entry, $excludedFields)) {
136                         continue;
137                 } // END - if
138
139                 // Do we have a non-string (e.g. number, NULL, NOW() or back-tick at the beginning?
140                 if (is_null($multiDimId)) {
141                         // Handle one-dimensional data
142                         if (is_null($value)) {
143                                 // NULL detected
144                                 $SQL .= '`' . $entry . '`=NULL,';
145                         } elseif (('' . bigintval($value, true, false) . '' == '' . $value . '') || ($value == 'NOW()') || (substr($value, 0, 1) == '`'))  {
146                                 // No need for ticks (')
147                                 $SQL .= '`' . $entry . '`=' . $value . ',';
148                         } else {
149                                 // Strings need ticks (') around them
150                                 $SQL .= '`' . $entry . "`='" . SQL_ESCAPE($value) . "', ";
151                         }
152                 } else {
153                         // Handle multi-dimensional data
154                         if (is_null($value[$multiDimId])) {
155                                 // NULL detected
156                                 $SQL .= '`' . $entry . '`=NULL, ';
157                         } elseif (('' . bigintval($value[$multiDimId], true, false) . '' == '' . $value[$multiDimId] . '') || ($value[$multiDimId] == 'NOW()') || (substr($value[$multiDimId], 0, 1) == '`'))  {
158                                 // No need for ticks (')
159                                 $SQL .= '`' . $entry . '`=' . $value[$multiDimId] . ',';
160                         } else {
161                                 // Strings need ticks (') around them
162                                 $SQL .= '`' . $entry . "`='" . SQL_ESCAPE($value[$multiDimId]) . "',";
163                         }
164                 }
165         } // END - foreach
166
167         // Remove last 2 chars and finish query
168         $SQL = substr($SQL, 0, -1) . ' WHERE `' . $whereColumn . '`=' . $whereData . ' LIMIT 1';
169
170         // Return SQL query
171         return $SQL;
172 }
173
174 // "Getter" for an "INSERT INTO" SQL query
175 function getInsertSqlFromArray ($array, $tableName) {
176         // Init SQL
177         $SQL = 'INSERT INTO
178 `{?_MYSQL_PREFIX?}_' . $tableName . '`
179 (
180 `' . implode('`,`', array_keys(postRequestArray())) . '`
181 ) VALUES (';
182
183         // Walk through all entries
184         foreach (postRequestArray() as $key => $value) {
185                 // Log debug message
186                 /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',key=' . $key . ',value=' . $value);
187
188                 // Add all entries
189                 if (is_null($value)) {
190                         // Add NULL
191                         $SQL .= 'NULL,';
192                 } elseif ('' . bigintval($value, true, false) . '' == '' . $value . '') {
193                         // Number detected, no need for ticks (')
194                         $SQL .= bigintval($value) . ',';
195                 } elseif ('' . (float) $value . '' == '' . $value . '') {
196                         // Float number detected
197                         $SQL .= sprintf('%01.5f', $value);
198                 } else {
199                         // Everything else might be a string, so add ticks around it
200                         $SQL .= "'" . SQL_ESCAPE($value) . "',";
201                 }
202         } // END - foreach
203
204         // Finish SQL query
205         $SQL = substr($SQL, 0, -1) . ')';
206
207         // Return SQL query
208         return $SQL;
209 }
210
211 // [EOF]
212 ?>