Skip to content

Commit 9bec55b

Browse files
authored
Merge pull request #42 from bhaveshdaswani93/master
Added support for pgsql batch update
2 parents 2137a8b + 625a35f commit 9bec55b

File tree

1 file changed

+43
-87
lines changed

1 file changed

+43
-87
lines changed

src/Batch.php

Lines changed: 43 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -49,97 +49,53 @@ 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-
foreach (array_keys($val) as $field) {
63-
if ($field !== $index) {
64-
$finalField = $raw ? Common::mysql_escape($val[$field]) : '"' . Common::mysql_escape($val[$field]) . '"';
65-
$value = (is_null($val[$field]) ? 'NULL' : $finalField);
66-
$final[$field][] = 'WHEN `' . $index . '` = "' . $val[$index] . '" THEN ' . $value . ' ';
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+
foreach (array_keys($val) as $field) {
63+
if ($field !== $index) {
64+
$finalField = $raw ? Common::mysql_escape($val[$field]) : "'" . Common::mysql_escape($val[$field]) . "'";
65+
$value = (is_null($val[$field]) ? 'NULL' : $finalField);
66+
$final[$field][] = 'WHEN "' . $index . '" = \'' . $val[$index] . '\' THEN ' . $value . ' ';
67+
}
6768
}
6869
}
69-
}
70-
71-
$cases = '';
72-
foreach ($final as $k => $v) {
73-
$cases .= '`' . $k . '` = (CASE ' . implode("\n", $v) . "\n"
74-
. 'ELSE `' . $k . '` END), ';
75-
}
76-
77-
$query = 'UPDATE `' . $this->getFullTableName($table) . '` SET ' . substr($cases, 0, -2) . " WHERE `$index` IN(" . '"' . implode('","', $ids) . '"' . ');';
78-
79-
return $this->db->connection($this->getConnectionName($table))->update($query);
80-
}
81-
82-
/**
83-
* Update multiple rows.
84-
*
85-
* @param Model $table
86-
* @param array $values
87-
* @param string $index
88-
* @param string|null $index2
89-
* @param bool $raw
90-
* @return bool|int
91-
* @updatedBy Ibrahim Sakr <ebrahimes@gmail.com>
92-
* @desc
93-
* Example
94-
* $table = 'users';
95-
* $value = [
96-
* [
97-
* 'id' => 1,
98-
* 'status' => 'active',
99-
* 'nickname' => 'Mohammad'
100-
* ] ,
101-
* [
102-
* 'id' => 5,
103-
* 'status' => 'deactive',
104-
* 'nickname' => 'Ghanbari'
105-
* ] ,
106-
* ];
107-
* $index = 'id';
108-
* $index2 = 'user_id';
109-
*/
110-
public function updateWithTwoIndex(Model $table, array $values, string $index = null, string $index2 = null, bool $raw = false)
111-
{
112-
$final = [];
113-
$ids = [];
114-
115-
if (! count($values)) {
116-
return false;
117-
}
118-
119-
if (! isset($index) || empty($index)) {
120-
$index = $table->getKeyName();
121-
}
122-
123-
foreach ($values as $key => $val) {
124-
$ids[] = $val[$index];
125-
$ids2[] = $val[$index2];
126-
foreach (array_keys($val) as $field) {
127-
if ($field !== $index || $field !== $index2) {
128-
$finalField = $raw ? Common::mysql_escape($val[$field]) : '"' . Common::mysql_escape($val[$field]) . '"';
129-
$value = (is_null($val[$field]) ? 'NULL' : $finalField);
130-
$final[$field][] = 'WHEN (`' . $index . '` = "' . Common::mysql_escape($val[$index]) . '" AND `' . $index2 . '` = "' . $val[$index2] . '") THEN ' . $value . ' ';
70+
71+
$connection = config('database.default');
72+
73+
$driver = config("database.connections.{$connection}.driver");
74+
75+
if ( $driver == 'pgsql' ){
76+
77+
$cases = '';
78+
foreach ($final as $k => $v) {
79+
$cases .= '"' . $k . '" = (CASE ' . implode("\n", $v) . "\n"
80+
. 'ELSE "' . $k . '" END), ';
81+
}
82+
83+
$query = "UPDATE \"" . $this->getFullTableName($table) . '" SET ' . substr($cases, 0, -2) . " WHERE \"$index\" IN('" . implode("','", $ids) . "');";
84+
85+
}else{
86+
87+
$cases = '';
88+
foreach ($final as $k => $v) {
89+
$cases .= '`' . $k . '` = (CASE ' . implode("\n", $v) . "\n"
90+
. 'ELSE `' . $k . '` END), ';
13191
}
92+
93+
$query = "UPDATE `" . $this->getFullTableName($table) . "` SET " . substr($cases, 0, -2) . " WHERE `$index` IN(" . '"' . implode('","', $ids) . '"' . ");";
94+
13295
}
133-
}
134-
135-
$cases = '';
136-
foreach ($final as $k => $v) {
137-
$cases .= '`' . $k . '` = (CASE ' . implode("\n", $v) . "\n"
138-
. 'ELSE `' . $k . '` END), ';
139-
}
140-
$query = 'UPDATE `' . $this->getFullTableName($table) . '` SET ' . substr($cases, 0, -2) . " WHERE `$index` IN(" . '"' . implode('","', $ids) . '")' . " AND `$index2` IN(" . '"' . implode('","', $ids2) . '"' . ' );';
141-
142-
return $this->db->connection($this->getConnectionName($table))->update($query);
96+
97+
98+
return $this->db->connection($this->getConnectionName($table))->update($query);
14399
}
144100

145101
/**

0 commit comments

Comments
 (0)