Skip to content

Commit 6ecea04

Browse files
author
Radovan Janjic
committed
New functionality & improvement
- UPDATED: strReplace - added search and replace in whole db - ADDED: sql:: statement for skipping string escape - BUG FIX
1 parent 6a2dc85 commit 6ecea04

File tree

2 files changed

+76
-11
lines changed

2 files changed

+76
-11
lines changed

MySQL_wrapper.class.php

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class MySQL_wrapper {
3434
/** Class Version
3535
* @var float
3636
*/
37-
var $version = '1.4';
37+
var $version = '1.5';
3838

3939
/** MySQL Host name
4040
* @var string
@@ -116,6 +116,11 @@ class MySQL_wrapper {
116116
*/
117117
var $reserved = array('null', 'now()', 'current_timestamp', 'curtime()', 'localtime()', 'localtime', 'utc_date()', 'utc_time()', 'utc_timestamp()');
118118

119+
/** Start of MySQL statement for array to ( insert / update )
120+
* @var string
121+
*/
122+
var $statementStart = 'sql::';
123+
119124
/** REGEX
120125
* @var array
121126
*/
@@ -218,7 +223,11 @@ function query($sql) {
218223
}
219224
foreach ($l as $k => $v) {
220225
$p['search'][] = "@{$k}";
221-
$p['replace'][] = $this->escape($v);
226+
if (preg_match('/^' . preg_quote($this->statementStart) . '/i', $v)) {
227+
$p['replace'][] = preg_replace('/^' . preg_quote($this->statementStart) . '/i', NULL, $v);
228+
} else {
229+
$p['replace'][] = "'{$this->escape($v)}'";
230+
}
222231
}
223232
$sql = str_replace($p['search'], $p['replace'], $sql);
224233
unset($l, $p);
@@ -330,7 +339,13 @@ function arrayToUpdate($table, $data, $where = NULL, $limit = 0) {
330339
}
331340
$fields = array();
332341
foreach ($data as $key => $val) {
333-
$fields[] = (in_array(strtolower($val), $this->reserved)) ? "`{$key}` = " . strtoupper($val) : "`$key` = '{$this->escape($val)}'";
342+
if (in_array(strtolower($val), $this->reserved)) {
343+
$fields[] = "`{$key}` = " . strtoupper($val);
344+
} elseif (preg_match('/^' . preg_quote($this->statementStart) . '/i', $val)) {
345+
$fields[] = "`{$key}` = " . preg_replace('/^' . preg_quote($this->statementStart) . '/i', NULL, $val);
346+
} else {
347+
$fields[] = "`{$key}` = '{$this->escape($val)}'";
348+
}
334349
}
335350
return (!empty($fields)) ? $this->query("UPDATE `{$table}` SET " . implode(', ', $fields) . ($where ? " WHERE {$where}" : NULL) . ($limit ? " LIMIT {$limit}" : NULL) . ";") ? $this->affected : FALSE : FALSE;
336351
}
@@ -349,14 +364,28 @@ function arrayToInsert($table, $data, $ignore = FALSE, $duplicateupdate = NULL)
349364
$dat = array();
350365
foreach ($data as &$val) {
351366
foreach ($val as &$v) {
352-
$v = (in_array(strtolower($v), $this->reserved)) ? strtoupper($v) : "'{$this->escape($v)}'";
367+
if (in_array(strtolower($v), $this->reserved)) {
368+
$v = strtoupper($v);
369+
} elseif (preg_match('/^' . preg_quote($this->statementStart) . '/i', $v)) {
370+
$v = preg_replace('/^' . preg_quote($this->statementStart) . '/i', NULL, $v);
371+
} else {
372+
$v = "'{$this->escape($v)}'";
373+
}
353374
}
354375
$dat[] = "( " . implode(', ', $val) . " )";
355376
}
356377
$v = implode(', ', $dat);
357378
} else {
358379
$c = implode('`, `', array_keys($data));
359-
foreach ($data as &$val) $val = (in_array(strtolower($val), $this->reserved)) ? strtoupper($val) : "'{$this->escape($val)}'";
380+
foreach ($data as &$val) {
381+
if (in_array(strtolower($val), $this->reserved)) {
382+
$val = strtoupper($val);
383+
} elseif (preg_match('/^' . preg_quote($this->statementStart) . '/i', $val)) {
384+
$val = preg_replace('/^' . preg_quote($this->statementStart) . '/i', NULL, $val);
385+
} else {
386+
$val = "'{$this->escape($val)}'";
387+
}
388+
}
360389
$v = "( " . implode(', ', $data) . " )";
361390
}
362391
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()) : FALSE : FALSE;
@@ -487,7 +516,7 @@ function importUpdateCSV2Table($file, $table, $delimiter = ',', $enclosure = '"'
487516
// Drop tmp table
488517
$this->query("DROP TEMPORARY TABLE `{$tmp_name}`;");
489518

490-
return ($i > 0) ? $i / 2 : $i;
519+
return $i;
491520
}
492521

493522
/** Export table data to CSV file.
@@ -756,7 +785,7 @@ function begin() {
756785
}
757786

758787
/** Replace all occurrences of the search string with the replacement string in MySQL Table Column(s).
759-
* @param string $table - Table name
788+
* @param string $table - Table name or "*" to replace in whole db
760789
* @param mixed $columns - Search & Replace affected Table columns. An array may be used to designate multiple replacements.
761790
* @param mixed $search - The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
762791
* @param mixed $replace - The replacement value that replaces found search values. An array may be used to designate multiple replacements.
@@ -765,6 +794,42 @@ function begin() {
765794
* @return integer - Affected rows
766795
*/
767796
function strReplace($table, $columns, $search, $replace, $where = NULL, $limit = 0) {
797+
// Replace in whole DB
798+
if ($table == '*') {
799+
if (!is_array($columns)){
800+
$stringColumns = $columns;
801+
if ($stringColumns != '*') {
802+
// Put columns into array
803+
$columns = array();
804+
if (preg_match($this->REGEX['COLUMN'], $stringColumns)) {
805+
$columns[] = $stringColumns;
806+
} else {
807+
foreach (explode(',', $stringColumns) as $c) {
808+
$columns[] = trim(str_replace(array("'", "`", "\""), NULL, $c));
809+
}
810+
}
811+
if (empty($columns)) {
812+
return FALSE;
813+
}
814+
}
815+
}
816+
$q = $this->query(
817+
"SELECT DISTINCT `table_name` AS `table`, GROUP_CONCAT(DISTINCT `column_name` ORDER BY `column_name`) AS `columns` FROM `information_schema`.`columns` " .
818+
"WHERE (`data_type` LIKE '%char%' OR `data_type` LIKE '%text' OR `data_type` LIKE '%binary')" . (($stringColumns != '*') ? " AND `column_name` IN('" . implode("', '", $columns) . "')" : NULL) . " AND `table_schema` = '{$this->database}' " .
819+
"GROUP BY `table_name` ORDER BY `table_name`;"
820+
);
821+
$affected = 0;
822+
if ($this->affected > 0) {
823+
while ($row = $this->fetchArray($q)) {
824+
if ($row['columns'] != '') {
825+
$affected += $this->strReplace($row['table'], $row['columns'], $search, $replace, $where, $limit);
826+
}
827+
}
828+
}
829+
$this->freeResult($q);
830+
return $affected;
831+
}
832+
768833
// Columns
769834
if (!is_array($columns)){
770835
$stringColumns = $columns;
@@ -781,6 +846,7 @@ function strReplace($table, $columns, $search, $replace, $where = NULL, $limit =
781846
}
782847
}
783848
}
849+
784850
// Update
785851
$update = array();
786852
foreach ($columns as $col) {

example.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// create test table
2020
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
2121
$db->connect();
22-
22+
/*
2323
// test table sql for examples
2424
$db->query("CREATE TABLE IF NOT EXISTS `table` (
2525
`id` int(11) NOT NULL AUTO_INCREMENT,
@@ -44,7 +44,7 @@
4444
(8, 'Radovan', 'Janjic', 'rade@it-radionica.com', '2012-11-04'),
4545
(9, 'Radovan', 'Janjic', 'rade@it-radionica.com', '2012-11-04'),
4646
(10, 'Radovan', 'Janjic', 'rade@it-radionica.com', '2012-11-04');");
47-
47+
*/
4848

4949
///////////////////////////////////////////////////////////////////////////////////////////
5050

@@ -54,8 +54,7 @@
5454
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
5555

5656
// Connect
57-
$db->connect();
58-
// Import and update all data
57+
$db->connect();
5958
// Close connection
6059
$db->close();
6160
///////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)