From 4ca6ed046a31c8fc08a4b74debb9983f24c33757 Mon Sep 17 00:00:00 2001 From: sarahkemp Date: Tue, 29 Dec 2020 11:01:22 -0800 Subject: [PATCH 1/2] Add $offsetCompatibilityMode to DB2Grammar --- src/Database/Query/Grammars/DB2Grammar.php | 30 ++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Database/Query/Grammars/DB2Grammar.php b/src/Database/Query/Grammars/DB2Grammar.php index 1d119f0..fb1ee6e 100644 --- a/src/Database/Query/Grammars/DB2Grammar.php +++ b/src/Database/Query/Grammars/DB2Grammar.php @@ -19,6 +19,12 @@ class DB2Grammar extends Grammar */ protected $dateFormat; + /** + * Offset compatibility mode true triggers FETCH FIRST X ROWS and ROW_NUM behavior for older versions of DB2 + * @var bool + */ + protected $offsetCompatibilityMode = true; + /** * Wrap a single string in keyword identifiers. * @@ -45,7 +51,10 @@ protected function wrapValue($value) */ protected function compileLimit(Builder $query, $limit) { - return "FETCH FIRST $limit ROWS ONLY"; + if($this->offsetCompatibilityMode){ + return "FETCH FIRST $limit ROWS ONLY"; + } + return parent::compileLimit($query, $limit); } /** @@ -57,6 +66,10 @@ protected function compileLimit(Builder $query, $limit) */ public function compileSelect(Builder $query) { + if(!$this->offsetCompatibilityMode){ + return parent::compileSelect($query); + } + if (is_null($query->columns)) { $query->columns = ['*']; } @@ -183,7 +196,10 @@ protected function compileTableExpression($sql, $constraint) */ protected function compileOffset(Builder $query, $offset) { - return ''; + if($this->offsetCompatibilityMode){ + return ''; + } + return parent::compileOffset($query, $offset); } /** @@ -221,6 +237,16 @@ public function setDateFormat($dateFormat) $this->dateFormat = $dateFormat; } + /** + * Set offset compatibility mode to trigger FETCH FIRST X ROWS and ROW_NUM behavior for older versions of DB2 + * + * @param $bool + */ + public function setOffsetCompatibilityMode($bool) + { + $this->offsetCompatibilityMode = $bool; + } + /** * Compile the SQL statement to define a savepoint. * From 1ee2072af42ba5b6c8d94eaeb44527da051896ce Mon Sep 17 00:00:00 2001 From: sarahkemp Date: Tue, 29 Dec 2020 11:02:21 -0800 Subject: [PATCH 2/2] Pull offset_compatibility_mode from options --- src/Database/DB2Connection.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Database/DB2Connection.php b/src/Database/DB2Connection.php index 3899d02..4cf4dfb 100644 --- a/src/Database/DB2Connection.php +++ b/src/Database/DB2Connection.php @@ -108,6 +108,10 @@ protected function getDefaultQueryGrammar() $defaultGrammar->setDateFormat($this->config['date_format']); } + if (array_key_exists('offset_compatibility_mode', $this->config)) { + $defaultGrammar->setOffsetCompatibilityMode($this->config['offset_compatibility_mode']); + } + return $this->withTablePrefix($defaultGrammar); }