Skip to content

Commit

Permalink
fixed driver postgres method update
Browse files Browse the repository at this point in the history
  • Loading branch information
mavinoo committed Dec 30, 2020
1 parent c6949ca commit 063d73f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 58 deletions.
114 changes: 58 additions & 56 deletions src/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,62 +49,64 @@ public function update(Model $table, array $values, string $index = null, bool $
$final = [];
$ids = [];

if (!count($values)) {
return false;
}

if (!isset($index) || empty($index)) {
$index = $table->getKeyName();
}

foreach ($values as $key => $val) {
$ids[] = $val[$index];

if ($table->usesTimestamps()) {
$updatedAtColumn = $table->getUpdatedAtColumn();

if (!isset($val[$updatedAtColumn])) {
$val[$updatedAtColumn] = now()->format($table->getDateFormat());
}
if (!count($values)) {
return false;
}

if (!isset($index) || empty($index)) {
$index = $table->getKeyName();
}

$connection = config('database.default');
$driver = config("database.connections.{$connection}.driver");

foreach ($values as $key => $val) {
$ids[] = $val[$index];

if ($table->usesTimestamps()) {
$updatedAtColumn = $table->getUpdatedAtColumn();

if (!isset($val[$updatedAtColumn])) {
$val[$updatedAtColumn] = now()->format($table->getDateFormat());
}
}

foreach (array_keys($val) as $field) {
if ($field !== $index) {
$finalField = $raw ? Common::mysql_escape($val[$field]) : "'" . Common::mysql_escape($val[$field]) . "'";
$value = (is_null($val[$field]) ? 'NULL' : $finalField);
foreach (array_keys($val) as $field) {
if ($field !== $index) {
$finalField = $raw ? Common::mysql_escape($val[$field]) : "'" . Common::mysql_escape($val[$field]) . "'";
$value = (is_null($val[$field]) ? 'NULL' : $finalField);
if ($driver == 'pgsql')
$final[$field][] = 'WHEN ' . $index . ' = \'' . $val[$index] . '\' THEN ' . $value . ' ';
else
$final[$field][] = 'WHEN `' . $index . '` = \'' . $val[$index] . '\' THEN ' . $value . ' ';
}
}
}

$connection = config('database.default');

$driver = config("database.connections.{$connection}.driver");

if ( $driver == 'pgsql' ){

$cases = '';
foreach ($final as $k => $v) {
$cases .= '"' . $k . '" = (CASE ' . implode("\n", $v) . "\n"
. 'ELSE "' . $k . '" END), ';
}

$query = "UPDATE \"" . $this->getFullTableName($table) . '" SET ' . substr($cases, 0, -2) . " WHERE \"$index\" IN('" . implode("','", $ids) . "');";

}else{

$cases = '';
foreach ($final as $k => $v) {
$cases .= '`' . $k . '` = (CASE ' . implode("\n", $v) . "\n"
. 'ELSE `' . $k . '` END), ';
}

$query = "UPDATE `" . $this->getFullTableName($table) . "` SET " . substr($cases, 0, -2) . " WHERE `$index` IN(" . '"' . implode('","', $ids) . '"' . ");";

}

if ($driver == 'pgsql') {

$cases = '';
foreach ($final as $k => $v) {
$cases .= '"' . $k . '" = (CASE ' . implode("\n", $v) . "\n"
. 'ELSE "' . $k . '" END), ';
}


return $this->db->connection($this->getConnectionName($table))->update($query);

$query = "UPDATE \"" . $this->getFullTableName($table) . '" SET ' . substr($cases, 0, -2) . " WHERE \"$index\" IN('" . implode("','", $ids) . "');";

} else {

$cases = '';
foreach ($final as $k => $v) {
$cases .= '`' . $k . '` = (CASE ' . implode("\n", $v) . "\n"
. 'ELSE `' . $k . '` END), ';
}

$query = "UPDATE `" . $this->getFullTableName($table) . "` SET " . substr($cases, 0, -2) . " WHERE `$index` IN(" . '"' . implode('","', $ids) . '"' . ");";

}


return $this->db->connection($this->getConnectionName($table))->update($query);
}

/**
Expand Down Expand Up @@ -150,23 +152,23 @@ public function updateWithTwoIndex(Model $table, array $values, string $index =
}

foreach ($values as $key => $val) {
$ids[] = $val[$index];
$ids[] = $val[$index];
$ids2[] = $val[$index2];
foreach (array_keys($val) as $field) {
if ($field !== $index || $field !== $index2 ) {
if ($field !== $index || $field !== $index2) {
$finalField = $raw ? Common::mysql_escape($val[$field]) : '"' . Common::mysql_escape($val[$field]) . '"';
$value = (is_null($val[$field]) ? 'NULL' : $finalField);
$final[$field][] = 'WHEN (`' . $index . '` = "' . Common::mysql_escape($val[$index]) .'" AND `'. $index2 . '` = "' . $val[$index2] .'") THEN ' . $value . ' ';
$final[$field][] = 'WHEN (`' . $index . '` = "' . Common::mysql_escape($val[$index]) . '" AND `' . $index2 . '` = "' . $val[$index2] . '") THEN ' . $value . ' ';
}
}
}

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

return $this->db->connection($this->getConnectionName($table))->update($query);
}
Expand Down Expand Up @@ -328,7 +330,7 @@ private function getFullTableName(Model $model)
*/
private function getConnectionName(Model $model)
{
if (! is_null($cn = $model->getConnectionName())) {
if (!is_null($cn = $model->getConnectionName())) {
return $cn;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Common/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static function mysql_escape($fieldValue)
return array_map(__METHOD__, $fieldValue);
}

if (! empty($fieldValue) && is_string($fieldValue)) {
if (!empty($fieldValue) && is_string($fieldValue)) {
return str_replace(
['\\', "\0", "\n", "\r", "'", '"', "\x1a"],
['\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'],
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Helpers.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

if (! function_exists('batch')) {
if (!function_exists('batch')) {
/**
* Batch helper to get Mavino\Batch\Batch instance.
*
Expand Down

0 comments on commit 063d73f

Please sign in to comment.