@@ -114,7 +114,7 @@ class MySQL_wrapper {
114114 /** Reserved words for array to ( insert / update )
115115 * @var array
116116 */
117- var $ reserved = array ('null ' , 'now() ' , 'curtime() ' , 'localtime() ' , 'localtime ' , 'utc_date() ' , 'utc_time() ' , 'utc_timestamp() ' );
117+ var $ reserved = array ('null ' , 'now() ' , 'current_timestamp ' , ' curtime() ' , 'localtime() ' , 'localtime ' , 'utc_date() ' , 'utc_time() ' , 'utc_timestamp() ' );
118118
119119 /** Constructor
120120 * @param string $server - MySQL Host name
@@ -267,14 +267,26 @@ function fetchArray($query = 0) {
267267
268268 /** Returns array with fetched associative rows.
269269 * @param string $sql - MySQL Query
270+ * @param string $fetchFirst - Fetch only first row
270271 * @param resource $link - Link identifier
271272 * @return array
272273 */
273- function fetchQueryToArray ($ sql , $ link = 0 ) {
274- $ this ->link = $ link ? $ link : $ this ->link ;
274+ function fetchQueryToArray ($ sql , $ fetchFirst = FALSE , $ link = 0 ) {
275+ $ this ->link = $ link ? $ link : $ this ->link ;
276+ if ($ fetchFirst ) {
277+ $ sql = rtrim (trim ($ sql ), '; ' );
278+ $ sql = preg_replace ('/limit(([\s]+([\d]+)[\s]*,[\s]*([\d]+))|([\s]+([\d]+)))$/i ' , 'LIMIT 1; ' , $ sql );
279+ if (substr ($ sql , -strlen ('LIMIT 1; ' )) !== 'LIMIT 1; ' ) {
280+ $ sql .= ' LIMIT 1; ' ;
281+ }
282+ }
275283 $ q = $ this ->query ($ sql , $ this ->link );
276284 $ array = array ();
277- while ($ row = $ this ->fetchArray ($ q )) $ array [] = $ row ;
285+ if ($ fetchFirst && $ this ->affected > 0 ) {
286+ $ array = $ this ->fetchArray ($ q );
287+ } else {
288+ while ($ row = $ this ->fetchArray ($ q )) $ array [] = $ row ;
289+ }
278290 $ this ->freeResult ($ q );
279291 return $ array ;
280292 }
@@ -306,16 +318,32 @@ function arrayToUpdate($table, $data, $where = NULL, $limit = 0, $link = 0) {
306318
307319 /** Creates an sql string from an associate array
308320 * @param string $table - Table name
309- * @param array $data - Data array Eg. $data[ 'column'] = 'val';
321+ * @param array $data - Data array Eg. array( 'column' => 'val') or multirows array(array('column' => 'val'), array('column' => 'val2'))
310322 * @param boolean $ingore - INSERT IGNORE (row won't actually be inserted if it results in a duplicate key)
311323 * @param string $duplicateupdate - ON DUPLICATE KEY UPDATE (The ON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas.)
312324 * @param resource $link - link identifier
313325 * @return insert id or false
314326 */
315327 function arrayToInsert ($ table , $ data , $ ignore = FALSE , $ duplicateupdate = NULL , $ link = 0 ) {
316328 $ this ->link = $ link ? $ link : $ this ->link ;
317- foreach ($ data as &$ val ) $ val = (in_array (strtolower ($ val ), $ this ->reserved )) ? strtoupper ($ val ) : "' " . $ this ->escape ($ val ) . "' " ;
318- return (!empty ($ data )) ? $ this ->query ("INSERT " . ($ ignore ? " IGNORE " : NULL ) . " INTO ` {$ table }` ( ` " . implode ('`, ` ' , array_keys ($ data )) . "` ) VALUES ( " . implode (', ' , array_values ($ data )) . " ) " . ($ duplicateupdate ? " ON DUPLICATE KEY UPDATE {$ duplicateupdate }" : NULL ) . "; " ) ? $ this ->insertId ($ this ->link ) : FALSE : FALSE ;
329+ $ depth = create_function ('$a,$callback ' , '$m = 1; foreach ($a as $v) if (is_array($v)) { $d = $callback($v,$callback) + 1; if ($d > $m) $m = $d; } return $m; ' );
330+ $ multirow = ($ depth ($ data , $ depth ) == 2 );
331+ if ($ multirow ) {
332+ $ c = implode ('`, ` ' , array_keys ($ data [0 ]));
333+ $ dat = array ();
334+ foreach ($ data as &$ val ) {
335+ foreach ($ val as &$ v ) {
336+ $ v = (in_array (strtolower ($ v ), $ this ->reserved )) ? strtoupper ($ v ) : "' " . $ this ->escape ($ v ) . "' " ;
337+ }
338+ $ dat [] = "( " . implode (', ' , $ val ) . " ) " ;
339+ }
340+ $ v = implode (', ' , $ dat );
341+ } else {
342+ $ c = implode ('`, ` ' , array_keys ($ data ));
343+ foreach ($ data as &$ val ) $ val = (in_array (strtolower ($ val ), $ this ->reserved )) ? strtoupper ($ val ) : "' " . $ this ->escape ($ val ) . "' " ;
344+ $ v = "( " . implode (', ' , $ data ) . " ) " ;
345+ }
346+ return (!empty ($ data )) ? $ this ->query ("INSERT " . ($ ignore ? " IGNORE " : NULL ) . " INTO ` {$ table }` ( ` {$ c }` ) VALUES {$ v }" . ($ duplicateupdate ? " ON DUPLICATE KEY UPDATE {$ duplicateupdate }" : NULL ) . "; " ) ? ($ multirow ? TRUE : $ this ->insertId ($ this ->link )) : FALSE : FALSE ;
319347 }
320348
321349 /** Imports CSV data to Table with possibility to update rows while import.
0 commit comments