Skip to content

Commit 3a94bdc

Browse files
author
Radovan Janjic
committed
Update
1 parent 2e81bed commit 3a94bdc

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

MySQL_wrapper.class.php

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ protected function __construct($server = NULL, $username = NULL, $password = NUL
184184
$this->database = $database;
185185
}
186186

187-
/** Private clone method to prevent cloning of the instance of the MySQL_wrapper instance.
187+
/** Private clone method to prevent cloning of the MySQL_wrapper instance.
188188
* @return void
189189
*/
190190
private function __clone() {
@@ -401,7 +401,9 @@ public function fetchQueryToArray($sql, $fetchFirst = FALSE) {
401401
if ($fetchFirst && $this->affected > 0) {
402402
$array = $this->fetchArray($q);
403403
} else {
404-
while ($row = $this->fetchArray($q)) $array[] = $row;
404+
while ($row = $this->fetchArray($q)) {
405+
$array[] = $row;
406+
}
405407
}
406408
$this->freeResult($q);
407409
return $array;
@@ -527,10 +529,14 @@ public function importCSV2Table($file, $table, $delimiter = ',', $enclosure = '"
527529
$line = fgets($f);
528530
fclose($f);
529531
$columns = explode($delimiter, str_replace($enclosure, NULL, trim($line)));
530-
foreach ($columns as $c) preg_match($this->REGEX['COLUMN'], $c) or $this->error("ERROR", "Invalid Column Name: {$c} in CSV file: {$file}. Data can not be loaded into table: {$table}.");
532+
foreach ($columns as $c) {
533+
preg_match($this->REGEX['COLUMN'], $c) or $this->error("ERROR", "Invalid Column Name: {$c} in CSV file: {$file}. Data can not be loaded into table: {$table}.");
534+
}
531535
}
532536

533-
foreach ($columns as &$c) $c = (in_array($c, array_keys($update))) ? '@' . $c : "`{$c}`";
537+
foreach ($columns as &$c) {
538+
$c = (in_array($c, array_keys($update))) ? '@' . $c : "`{$c}`";
539+
}
534540
$sql .= " (" . implode(', ', $columns) . ") ";
535541

536542
$fields = array();
@@ -793,7 +799,7 @@ public function copyTable($table, $new_table, $data = TRUE) {
793799
* @return resource or false
794800
*/
795801
public function truncateTable($table) {
796-
return $this->query("TRUNCATE TABLE `" . $table . "`;");
802+
return $this->query("TRUNCATE TABLE `{$table}`;");
797803
}
798804

799805
/** Drop table(s)
@@ -806,7 +812,6 @@ public function dropTable($table, $if_exists = TRUE) {
806812
}
807813

808814
/** Data Base size in B / KB / MB / GB / TB
809-
* @param string $sizeIn - Size in B / KB / MB / GB / TB
810815
* @param string $sizeIn - Size in B / KB / MB / GB / TB
811816
* @param integer $round - Round on decimals
812817
* @return - Size in B / KB / MB / GB / TB
@@ -896,16 +901,16 @@ public function query2XML($query, $rootElementName, $childElementName, $file = N
896901

897902
// XML header
898903
if ($saveToFile) {
899-
file_put_contents($file, "<?xml version=\"1.0\" encoding=\"" . strtoupper($this->charset) . "\" ?>\n<" . $rootElementName . ">\n", LOCK_EX);
904+
file_put_contents($file, "<?xml version=\"1.0\" encoding=\"" . strtoupper($this->charset) . "\" ?>" . PHP_EOL . "<{$rootElementName}>" . PHP_EOL, LOCK_EX);
900905
} else {
901-
$xml = "<?xml version=\"1.0\" encoding=\"" . strtoupper($this->charset) . "\" ?>\n";
902-
$xml .= "<" . $rootElementName . ">\n";
906+
$xml = "<?xml version=\"1.0\" encoding=\"" . strtoupper($this->charset) . "\" ?>" . PHP_EOL;
907+
$xml .= "<{$rootElementName}>" . PHP_EOL;
903908
}
904909

905910
// Query rows
906-
while($row = $this->call('fetch_object', $r)) {
911+
while ($row = $this->call('fetch_object', $r)) {
907912
// Create the first child element
908-
$record = "\t<" . $childElementName . ">\n";
913+
$record = "\t<{$childElementName}>" . PHP_EOL;
909914
for ($i = 0; $i < $this->call('num_fields', $r); $i++) {
910915
// Different methods of getting field name for mysql and mysqli
911916
if ($this->extension == 'mysql') {
@@ -915,16 +920,16 @@ public function query2XML($query, $rootElementName, $childElementName, $file = N
915920
$fieldName = $colObj->name;
916921
}
917922
// The child will take the name of the result column name
918-
$record .= "\t\t<" . $fieldName . ">";
923+
$record .= "\t\t<{$fieldName}>";
919924
// Set empty columns with NULL and escape XML entities
920925
if(!empty($row->$fieldName)) {
921926
$record .= htmlspecialchars($row->$fieldName, ENT_XML1);
922927
} else {
923928
$record .= NULL;
924929
}
925-
$record .= "</" . $fieldName . ">\n";
930+
$record .= "</{$fieldName}>" . PHP_EOL;
926931
}
927-
$record .= "\t</" . $childElementName . ">\n";
932+
$record .= "\t</{$childElementName}>" . PHP_EOL;
928933
if ($saveToFile) {
929934
file_put_contents($file, $record, FILE_APPEND | LOCK_EX);
930935
} else {
@@ -934,22 +939,14 @@ public function query2XML($query, $rootElementName, $childElementName, $file = N
934939

935940
// Output
936941
if ($saveToFile) {
937-
file_put_contents($file, "</" . $rootElementName . ">\n", FILE_APPEND | LOCK_EX);
942+
file_put_contents($file, "</{$rootElementName}>" . PHP_EOL, FILE_APPEND | LOCK_EX);
938943
return TRUE;
939944
} else {
940-
$xml .= "</" . $rootElementName . ">\n";
945+
$xml .= "</{$rootElementName}>" . PHP_EOL;
941946
return $xml;
942947
}
943948
}
944949

945-
/** Begin Transaction
946-
* @param void
947-
*/
948-
public function begin() {
949-
$this->query("START TRANSACTION");
950-
return $this->query("BEGIN");
951-
}
952-
953950
/** Replace all occurrences of the search string with the replacement string in MySQL Table Column(s).
954951
* @param string $table - Table name or "*" to replace in whole db
955952
* @param mixed $columns - Search & Replace affected Table columns. An array may be used to designate multiple replacements.
@@ -962,7 +959,7 @@ public function begin() {
962959
public function strReplace($table, $columns, $search, $replace, $where = NULL, $limit = 0) {
963960
// Replace in whole DB
964961
if ($table == '*') {
965-
if (!is_array($columns)){
962+
if (!is_array($columns)) {
966963
$stringColumns = $columns;
967964
if ($stringColumns != '*') {
968965
// Put columns into array
@@ -995,7 +992,7 @@ public function strReplace($table, $columns, $search, $replace, $where = NULL, $
995992
}
996993

997994
// Columns
998-
if (!is_array($columns)){
995+
if (!is_array($columns)) {
999996
$stringColumns = $columns;
1000997
$columns = array();
1001998
if ($stringColumns == '*') {
@@ -1025,19 +1022,27 @@ public function strReplace($table, $columns, $search, $replace, $where = NULL, $
10251022
$this->query("UPDATE `{$table}` SET " . implode(', ', $update) . ($where ? " WHERE {$where}" : NULL) . ($limit ? " LIMIT {$limit}" : NULL) . ";");
10261023
return $this->affected;
10271024
}
1025+
1026+
/** Begin Transaction
1027+
* @param void
1028+
*/
1029+
public function begin() {
1030+
$this->query("START TRANSACTION;");
1031+
return $this->query("BEGIN;");
1032+
}
10281033

10291034
/** Commit
10301035
* @param void
10311036
*/
10321037
public function commit() {
1033-
return $this->query("COMMIT");
1038+
return $this->query("COMMIT;");
10341039
}
10351040

10361041
/** Rollback
10371042
* @param void
10381043
*/
10391044
public function rollback() {
1040-
return $this->query("ROLLBACK");
1045+
return $this->query("ROLLBACK;");
10411046
}
10421047

10431048
/** Transaction
@@ -1307,4 +1312,4 @@ private function detectEOL($file) {
13071312
}
13081313
return FALSE;
13091314
}
1310-
}
1315+
}

0 commit comments

Comments
 (0)