Skip to content

Commit 063d73f

Browse files
committed
fixed driver postgres method update
1 parent c6949ca commit 063d73f

File tree

3 files changed

+60
-58
lines changed

3 files changed

+60
-58
lines changed

src/Batch.php

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -49,62 +49,64 @@ public function update(Model $table, array $values, string $index = null, bool $
4949
$final = [];
5050
$ids = [];
5151

52-
if (!count($values)) {
53-
return false;
54-
}
55-
56-
if (!isset($index) || empty($index)) {
57-
$index = $table->getKeyName();
58-
}
59-
60-
foreach ($values as $key => $val) {
61-
$ids[] = $val[$index];
62-
63-
if ($table->usesTimestamps()) {
64-
$updatedAtColumn = $table->getUpdatedAtColumn();
65-
66-
if (!isset($val[$updatedAtColumn])) {
67-
$val[$updatedAtColumn] = now()->format($table->getDateFormat());
68-
}
52+
if (!count($values)) {
53+
return false;
54+
}
55+
56+
if (!isset($index) || empty($index)) {
57+
$index = $table->getKeyName();
58+
}
59+
60+
$connection = config('database.default');
61+
$driver = config("database.connections.{$connection}.driver");
62+
63+
foreach ($values as $key => $val) {
64+
$ids[] = $val[$index];
65+
66+
if ($table->usesTimestamps()) {
67+
$updatedAtColumn = $table->getUpdatedAtColumn();
68+
69+
if (!isset($val[$updatedAtColumn])) {
70+
$val[$updatedAtColumn] = now()->format($table->getDateFormat());
6971
}
72+
}
7073

71-
foreach (array_keys($val) as $field) {
72-
if ($field !== $index) {
73-
$finalField = $raw ? Common::mysql_escape($val[$field]) : "'" . Common::mysql_escape($val[$field]) . "'";
74-
$value = (is_null($val[$field]) ? 'NULL' : $finalField);
74+
foreach (array_keys($val) as $field) {
75+
if ($field !== $index) {
76+
$finalField = $raw ? Common::mysql_escape($val[$field]) : "'" . Common::mysql_escape($val[$field]) . "'";
77+
$value = (is_null($val[$field]) ? 'NULL' : $finalField);
78+
if ($driver == 'pgsql')
79+
$final[$field][] = 'WHEN ' . $index . ' = \'' . $val[$index] . '\' THEN ' . $value . ' ';
80+
else
7581
$final[$field][] = 'WHEN `' . $index . '` = \'' . $val[$index] . '\' THEN ' . $value . ' ';
76-
}
7782
}
7883
}
79-
80-
$connection = config('database.default');
81-
82-
$driver = config("database.connections.{$connection}.driver");
83-
84-
if ( $driver == 'pgsql' ){
85-
86-
$cases = '';
87-
foreach ($final as $k => $v) {
88-
$cases .= '"' . $k . '" = (CASE ' . implode("\n", $v) . "\n"
89-
. 'ELSE "' . $k . '" END), ';
90-
}
91-
92-
$query = "UPDATE \"" . $this->getFullTableName($table) . '" SET ' . substr($cases, 0, -2) . " WHERE \"$index\" IN('" . implode("','", $ids) . "');";
93-
94-
}else{
95-
96-
$cases = '';
97-
foreach ($final as $k => $v) {
98-
$cases .= '`' . $k . '` = (CASE ' . implode("\n", $v) . "\n"
99-
. 'ELSE `' . $k . '` END), ';
100-
}
101-
102-
$query = "UPDATE `" . $this->getFullTableName($table) . "` SET " . substr($cases, 0, -2) . " WHERE `$index` IN(" . '"' . implode('","', $ids) . '"' . ");";
103-
84+
}
85+
86+
if ($driver == 'pgsql') {
87+
88+
$cases = '';
89+
foreach ($final as $k => $v) {
90+
$cases .= '"' . $k . '" = (CASE ' . implode("\n", $v) . "\n"
91+
. 'ELSE "' . $k . '" END), ';
10492
}
105-
106-
107-
return $this->db->connection($this->getConnectionName($table))->update($query);
93+
94+
$query = "UPDATE \"" . $this->getFullTableName($table) . '" SET ' . substr($cases, 0, -2) . " WHERE \"$index\" IN('" . implode("','", $ids) . "');";
95+
96+
} else {
97+
98+
$cases = '';
99+
foreach ($final as $k => $v) {
100+
$cases .= '`' . $k . '` = (CASE ' . implode("\n", $v) . "\n"
101+
. 'ELSE `' . $k . '` END), ';
102+
}
103+
104+
$query = "UPDATE `" . $this->getFullTableName($table) . "` SET " . substr($cases, 0, -2) . " WHERE `$index` IN(" . '"' . implode('","', $ids) . '"' . ");";
105+
106+
}
107+
108+
109+
return $this->db->connection($this->getConnectionName($table))->update($query);
108110
}
109111

110112
/**
@@ -150,23 +152,23 @@ public function updateWithTwoIndex(Model $table, array $values, string $index =
150152
}
151153

152154
foreach ($values as $key => $val) {
153-
$ids[] = $val[$index];
155+
$ids[] = $val[$index];
154156
$ids2[] = $val[$index2];
155157
foreach (array_keys($val) as $field) {
156-
if ($field !== $index || $field !== $index2 ) {
158+
if ($field !== $index || $field !== $index2) {
157159
$finalField = $raw ? Common::mysql_escape($val[$field]) : '"' . Common::mysql_escape($val[$field]) . '"';
158160
$value = (is_null($val[$field]) ? 'NULL' : $finalField);
159-
$final[$field][] = 'WHEN (`' . $index . '` = "' . Common::mysql_escape($val[$index]) .'" AND `'. $index2 . '` = "' . $val[$index2] .'") THEN ' . $value . ' ';
161+
$final[$field][] = 'WHEN (`' . $index . '` = "' . Common::mysql_escape($val[$index]) . '" AND `' . $index2 . '` = "' . $val[$index2] . '") THEN ' . $value . ' ';
160162
}
161163
}
162164
}
163165

164166
$cases = '';
165167
foreach ($final as $k => $v) {
166168
$cases .= '`' . $k . '` = (CASE ' . implode("\n", $v) . "\n"
167-
. 'ELSE `' . $k . '` END), ';
169+
. 'ELSE `' . $k . '` END), ';
168170
}
169-
$query = "UPDATE `" . $this->getFullTableName($table) . "` SET " . substr($cases, 0, -2) . " WHERE `$index` IN(" . '"' . implode('","', $ids) . '")' . " AND `$index2` IN(" . '"' . implode('","', $ids2) . '"' ." );";
171+
$query = "UPDATE `" . $this->getFullTableName($table) . "` SET " . substr($cases, 0, -2) . " WHERE `$index` IN(" . '"' . implode('","', $ids) . '")' . " AND `$index2` IN(" . '"' . implode('","', $ids2) . '"' . " );";
170172

171173
return $this->db->connection($this->getConnectionName($table))->update($query);
172174
}
@@ -328,7 +330,7 @@ private function getFullTableName(Model $model)
328330
*/
329331
private function getConnectionName(Model $model)
330332
{
331-
if (! is_null($cn = $model->getConnectionName())) {
333+
if (!is_null($cn = $model->getConnectionName())) {
332334
return $cn;
333335
}
334336

src/Common/Common.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static function mysql_escape($fieldValue)
1616
return array_map(__METHOD__, $fieldValue);
1717
}
1818

19-
if (! empty($fieldValue) && is_string($fieldValue)) {
19+
if (!empty($fieldValue) && is_string($fieldValue)) {
2020
return str_replace(
2121
['\\', "\0", "\n", "\r", "'", '"', "\x1a"],
2222
['\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'],

src/Common/Helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types=1);
22

3-
if (! function_exists('batch')) {
3+
if (!function_exists('batch')) {
44
/**
55
* Batch helper to get Mavino\Batch\Batch instance.
66
*

0 commit comments

Comments
 (0)