Skip to content

Commit f5cad53

Browse files
authored
Merge pull request #73 from sarahkemp/master
Add offset_compatibility_mode option for newer versions of DB2
2 parents fed6ccf + 1ee2072 commit f5cad53

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/Database/DB2Connection.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ protected function getDefaultQueryGrammar()
108108
$defaultGrammar->setDateFormat($this->config['date_format']);
109109
}
110110

111+
if (array_key_exists('offset_compatibility_mode', $this->config)) {
112+
$defaultGrammar->setOffsetCompatibilityMode($this->config['offset_compatibility_mode']);
113+
}
114+
111115
return $this->withTablePrefix($defaultGrammar);
112116
}
113117

src/Database/Query/Grammars/DB2Grammar.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ class DB2Grammar extends Grammar
1919
*/
2020
protected $dateFormat;
2121

22+
/**
23+
* Offset compatibility mode true triggers FETCH FIRST X ROWS and ROW_NUM behavior for older versions of DB2
24+
* @var bool
25+
*/
26+
protected $offsetCompatibilityMode = true;
27+
2228
/**
2329
* Wrap a single string in keyword identifiers.
2430
*
@@ -45,7 +51,10 @@ protected function wrapValue($value)
4551
*/
4652
protected function compileLimit(Builder $query, $limit)
4753
{
48-
return "FETCH FIRST $limit ROWS ONLY";
54+
if($this->offsetCompatibilityMode){
55+
return "FETCH FIRST $limit ROWS ONLY";
56+
}
57+
return parent::compileLimit($query, $limit);
4958
}
5059

5160
/**
@@ -57,6 +66,10 @@ protected function compileLimit(Builder $query, $limit)
5766
*/
5867
public function compileSelect(Builder $query)
5968
{
69+
if(!$this->offsetCompatibilityMode){
70+
return parent::compileSelect($query);
71+
}
72+
6073
if (is_null($query->columns)) {
6174
$query->columns = ['*'];
6275
}
@@ -183,7 +196,10 @@ protected function compileTableExpression($sql, $constraint)
183196
*/
184197
protected function compileOffset(Builder $query, $offset)
185198
{
186-
return '';
199+
if($this->offsetCompatibilityMode){
200+
return '';
201+
}
202+
return parent::compileOffset($query, $offset);
187203
}
188204

189205
/**
@@ -221,6 +237,16 @@ public function setDateFormat($dateFormat)
221237
$this->dateFormat = $dateFormat;
222238
}
223239

240+
/**
241+
* Set offset compatibility mode to trigger FETCH FIRST X ROWS and ROW_NUM behavior for older versions of DB2
242+
*
243+
* @param $bool
244+
*/
245+
public function setOffsetCompatibilityMode($bool)
246+
{
247+
$this->offsetCompatibilityMode = $bool;
248+
}
249+
224250
/**
225251
* Compile the SQL statement to define a savepoint.
226252
*

0 commit comments

Comments
 (0)