Skip to content

Commit e0b62e8

Browse files
author
uzi88
committed
New functionality, bug fix & speed improvement
- added arrayToUpdate multirow - fixed exportTable2CSV limit bug - improved multirow detection speed
1 parent 94dc3d1 commit e0b62e8

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

MySQL_wrapper.class.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,17 @@ function escape($string, $link = 0) {
311311
*/
312312
function arrayToUpdate($table, $data, $where = NULL, $limit = 0, $link = 0) {
313313
$this->link = $link ? $link : $this->link;
314+
if (is_array(reset($data))) {
315+
$cols = array();
316+
foreach (array_keys($data[0]) as $c) {
317+
$cols[] = "`{$c}` = VALUES(`{$c}`)";
318+
}
319+
return $this->arrayToInsert($table, $data, TRUE, implode(', ', $cols));
320+
}
314321
$fields = array();
315-
foreach ($data as $key => $val) $fields[] = (in_array(strtolower($val), $this->reserved)) ? "`$key` = " . strtoupper($val) : "`$key` = '" . $this->escape($val) . "'";
322+
foreach ($data as $key => $val) {
323+
$fields[] = (in_array(strtolower($val), $this->reserved)) ? "`$key` = " . strtoupper($val) : "`$key` = '" . $this->escape($val) . "'";
324+
}
316325
return (!empty($fields)) ? $this->query("UPDATE `{$table}` SET " . implode(', ', $fields) . ($where ? " WHERE {$where}" : NULL) . ($limit ? " LIMIT {$limit}" : NULL) . ";", $this->link) ? $this->affected : FALSE : FALSE;
317326
}
318327

@@ -326,8 +335,7 @@ function arrayToUpdate($table, $data, $where = NULL, $limit = 0, $link = 0) {
326335
*/
327336
function arrayToInsert($table, $data, $ignore = FALSE, $duplicateupdate = NULL, $link = 0) {
328337
$this->link = $link ? $link : $this->link;
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);
338+
$multirow = is_array(reset($data));
331339
if ($multirow) {
332340
$c = implode('`, `', array_keys($data[0]));
333341
$dat = array();
@@ -432,17 +440,16 @@ function exportTable2CSV($table, $file, $columns = '*', $where = NULL, $limit =
432440
foreach ($columns as $c)
433441
$tableColumnsArr[] = "'{$c}' AS `{$c}`";
434442
}
435-
$columnsSQL = "SELECT " . implode(', ', $tableColumnsArr) . " UNION ALL ";
443+
$columnsSQL = "SELECT " . implode(', ', $tableColumnsArr);
436444
}
437-
438-
$sql = (($showColumns) ? $columnsSQL : NULL) .
439-
"SELECT " . (is_array($columns) ? '`' . implode('`, `', $columns) . '`' : $columns) . " " .
445+
446+
$sql = "SELECT " . (is_array($columns) ? '`' . implode('`, `', $columns) . '`' : $columns) . " FROM `{$table}`" . ($where ? " WHERE {$where}" : NULL) . ($limit ? " LIMIT {$limit}" : NULL);
447+
$sql = (($showColumns) ? "SELECT * FROM ( ( " . $columnsSQL . " ) UNION ALL ( {$sql} ) ) `a` " : "{$sql} ") .
440448
"INTO OUTFILE '{$this->escape($file)}' " .
441449
"FIELDS TERMINATED BY '{$delimiter}' " .
442450
"OPTIONALLY ENCLOSED BY '{$enclosure}' " .
443451
"ESCAPED BY '{$this->escape($escape)}' " .
444-
"LINES TERMINATED BY '{$newLine}' " .
445-
"FROM `{$table}`" . ($where ? " WHERE {$where}" : NULL) . ($limit ? " LIMIT {$limit}" : NULL) . ";";
452+
"LINES TERMINATED BY '{$newLine}';";
446453
return ($this->query($sql, $this->link)) ? $file : FALSE;
447454
}
448455

example.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,27 @@
305305
if($db->affected){
306306
echo "<hr /><strong>Example 11 (array to update)</strong><br />Updated: {$db->affected} row(s).<br />";
307307
}
308+
309+
// Array data
310+
// [fealdname] = feald value
311+
$data = array();
312+
$data['id'] = 1; // key
313+
$data['firstname'] = 'foo';
314+
$data['surname'] = 'bar';
315+
$data['email'] = '[email protected]';
316+
$data['date'] = 'now()';
317+
318+
// [fealdname] = feald value
319+
$data2 = array();
320+
$data2['id'] = 2; // key
321+
$data2['firstname'] = 'Radovana';
322+
$data2['surname'] = 'Janjic';
323+
$data2['email'] = '[email protected]';
324+
$data2['date'] = 'now()';
325+
326+
// $db->arrayToUpdate( ... ) multirow returns TRUE on success
327+
$db->arrayToUpdate('table', array($data, $data2 /*, $data3 .... */ ));
328+
308329
// More options
309330
/** Creates an sql string from an associate array
310331
* @param string $table - Table name
@@ -431,6 +452,8 @@
431452
$db->exportTable2CSV('table', 'test-2.txt', 'firstname, surname');
432453
// Export two or more columns using array
433454
$db->exportTable2CSV('table', 'test-3.txt', array('firstname', 'surname', 'date'));
455+
// Export all columns where id < 8 and limit 1, 5
456+
$db->exportTable2CSV('table', 'test-1.txt', '*', 'id < 8', '1,5');
434457
// More options
435458
/** Export table data to CSV file.
436459
* @param string $table - Table name

0 commit comments

Comments
 (0)