From ebef9958a47fb7c67fea34d87db2eaf43a8ab0fb Mon Sep 17 00:00:00 2001 From: wjonkerhulst <54948500+wjonkerhulst@users.noreply.github.com> Date: Wed, 4 Mar 2020 09:21:34 +0100 Subject: [PATCH 1/6] Laravel 7.0 update * Updated illuminate/database dependency to 7.0 * Updated php version to match laravel 7.0 php version constrained --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 67e39e8..d4e0602 100644 --- a/composer.json +++ b/composer.json @@ -16,8 +16,8 @@ } ], "require": { - "php": "^7.2", - "illuminate/database": "^6.0" + "php": "^7.2.5", + "illuminate/database": "^7.0" }, "require-dev": { }, From 13912d1fcb70650c4a878b8a2224c14ccfe511f3 Mon Sep 17 00:00:00 2001 From: Bennett Black Date: Fri, 23 Apr 2021 16:03:18 -0500 Subject: [PATCH 2/6] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 385c8a3..64b646a 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "cooperl/laravel-db2", + "name": "bwicompanies/laravel-db2", "description": "laravel-db2 is a simple DB2 service provider for Laravel. It provides DB2 Connection by extending the Illuminate Database component of the laravel framework.", "keywords": [ "laravel", From 1ddba6e723f1ec07d197af28399d21f496756762 Mon Sep 17 00:00:00 2001 From: Wilfried Jonker Date: Wed, 28 Apr 2021 08:41:46 +0000 Subject: [PATCH 3/6] Add upsert functionality --- src/Database/Query/Grammars/DB2Grammar.php | 64 ++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/Database/Query/Grammars/DB2Grammar.php b/src/Database/Query/Grammars/DB2Grammar.php index 1d119f0..fb5897c 100644 --- a/src/Database/Query/Grammars/DB2Grammar.php +++ b/src/Database/Query/Grammars/DB2Grammar.php @@ -231,4 +231,68 @@ public function compileSavepoint($name) { return 'SAVEPOINT '.$name.' ON ROLLBACK RETAIN CURSORS'; } + + /** + * Compile an "upsert" statement into SQL. + * + * Based on and modified from : + * - https://github.com/laravel/framework/blob/338ffa625e4b16ccd3d3481371c9312a245fdc30/src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php + * - https://nandaibmi.com/index.php/2019/04/16/db2-upsert-using-merge-into/ + * + * @param \Illuminate\Database\Query\Builder $query + * @param array $values + * @param array $uniqueBy + * @param array $update + * + * @return string + */ + public function compileUpsert(Builder $query, array $values, array $uniqueBy, array $update) + { + $columns = $this->columnize(array_keys(reset($values))); + + $sql = 'merge into ' . $query->from . ' as temp '; + + $parameters = collect($values)->map(function ($record) { + return '(' . $this->parameterizeUpsert($record) . ')'; + })->implode(', '); + + $sql .= 'using (values ' . $parameters . ') as merge (' . $columns . ') '; + + $on = collect($uniqueBy)->map(function ($column) { + return 'temp.' . $column . ' = merge.' . $column; + })->implode(' and '); + + $sql .= 'on ' . $on . ' '; + + if ($update) { + $update = collect($update)->map(function ($value) { + return 'temp.' . $value . ' = ' . 'merge.' . $value; + })->implode(', '); + + $sql .= 'when matched then update set ' . $update . ' '; + } + + $mergeValues = collect(array_keys(reset($values)))->map(function ($column) { + return 'merge.' . $column; + })->implode(', '); + + $sql .= 'when not matched then insert (' . $columns . ') values (' . $mergeValues . ')'; + + return $sql; + } + + /** + * Parameterize Upsert. Values need to be casted + * Form VARCHAR it is almost always posible to cast back to another type. See table (Table 1. Supported Casts between Built-in Data Types) https://www.ibm.com/support/producthub/db2/docs/content/SSEPGG_11.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0008478.html + * + * @param array $values + * + * @return string + */ + private function parameterizeUpsert($record) + { + return collect($record)->map(function ($value, $key) { + return 'CAST(' . $this->parameter($value) . ' as VARCHAR(' . strlen((string) $value) . '))'; + })->implode(', '); + } } From ee009aa7da7e2f9399c4284ded62b804b020943b Mon Sep 17 00:00:00 2001 From: Wilfried Jonker Date: Wed, 28 Apr 2021 09:05:52 +0000 Subject: [PATCH 4/6] Fix spelling --- src/Database/Query/Grammars/DB2Grammar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Query/Grammars/DB2Grammar.php b/src/Database/Query/Grammars/DB2Grammar.php index fb5897c..5c59101 100644 --- a/src/Database/Query/Grammars/DB2Grammar.php +++ b/src/Database/Query/Grammars/DB2Grammar.php @@ -283,7 +283,7 @@ public function compileUpsert(Builder $query, array $values, array $uniqueBy, ar /** * Parameterize Upsert. Values need to be casted - * Form VARCHAR it is almost always posible to cast back to another type. See table (Table 1. Supported Casts between Built-in Data Types) https://www.ibm.com/support/producthub/db2/docs/content/SSEPGG_11.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0008478.html + * From VARCHAR it is almost always posible to cast back to another type. See table (Table 1. Supported Casts between Built-in Data Types) https://www.ibm.com/support/producthub/db2/docs/content/SSEPGG_11.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0008478.html * * @param array $values * From d348325446c6f20cf99564202a57021947b720e3 Mon Sep 17 00:00:00 2001 From: Wilfried Jonker Date: Thu, 29 Apr 2021 07:44:22 +0000 Subject: [PATCH 5/6] Use multibyte aware strlen function --- src/Database/Query/Grammars/DB2Grammar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Query/Grammars/DB2Grammar.php b/src/Database/Query/Grammars/DB2Grammar.php index 5c59101..ec5f408 100644 --- a/src/Database/Query/Grammars/DB2Grammar.php +++ b/src/Database/Query/Grammars/DB2Grammar.php @@ -292,7 +292,7 @@ public function compileUpsert(Builder $query, array $values, array $uniqueBy, ar private function parameterizeUpsert($record) { return collect($record)->map(function ($value, $key) { - return 'CAST(' . $this->parameter($value) . ' as VARCHAR(' . strlen((string) $value) . '))'; + return 'CAST(' . $this->parameter($value) . ' as VARCHAR(' . mb_strlen((string) $value) . '))'; })->implode(', '); } } From 4b493ad7fa75f9e8cb34126e68238411cc69d0c6 Mon Sep 17 00:00:00 2001 From: wjonkerhulst <54948500+wjonkerhulst@users.noreply.github.com> Date: Sat, 18 Dec 2021 10:47:43 +0100 Subject: [PATCH 6/6] Composer namespace fix --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 64b646a..385c8a3 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "bwicompanies/laravel-db2", + "name": "cooperl/laravel-db2", "description": "laravel-db2 is a simple DB2 service provider for Laravel. It provides DB2 Connection by extending the Illuminate Database component of the laravel framework.", "keywords": [ "laravel",