Skip to content

Commit

Permalink
Merge pull request #73 from sarahkemp/master
Browse files Browse the repository at this point in the history
Add offset_compatibility_mode option for newer versions of DB2
  • Loading branch information
cooperl22 authored Dec 10, 2021
2 parents fed6ccf + 1ee2072 commit f5cad53
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Database/DB2Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
30 changes: 28 additions & 2 deletions src/Database/Query/Grammars/DB2Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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);
}

/**
Expand All @@ -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 = ['*'];
}
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit f5cad53

Please sign in to comment.