Skip to content

Commit

Permalink
Refactor versioning approach
Browse files Browse the repository at this point in the history
  • Loading branch information
harrygulliford committed Oct 27, 2020
1 parent e65d4d0 commit e82c3b1
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 92 deletions.
11 changes: 7 additions & 4 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Exception;
use Firebird\Query\Builder as FirebirdQueryBuilder;
use Firebird\Query\Grammars\FirebirdGrammar as FirebirdQueryGrammar;
use Firebird\Query\Grammars\Firebird1Grammar as Firebird1QueryGrammar;
use Firebird\Query\Grammars\Firebird2Grammar as Firebird2QueryGrammar;
use Firebird\Schema\Builder as FirebirdSchemaBuilder;
use Firebird\Schema\Grammars\FirebirdGrammar as FirebirdSchemaGrammar;
use Firebird\Support\Version;
Expand All @@ -19,9 +20,11 @@ class Connection extends DatabaseConnection
*/
protected function getDefaultQueryGrammar()
{
return new FirebirdQueryGrammar(
$this->getFirebirdVersion()
);
if ($this->getFirebirdVersion() == Version::FIREBIRD_15) {
return new Firebird1QueryGrammar;
}

return new Firebird2QueryGrammar;
}

/**
Expand Down
80 changes: 80 additions & 0 deletions src/Query/Grammars/Firebird1Grammar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Firebird\Query\Grammars;

use Illuminate\Database\Query\Builder;

class Firebird1Grammar extends Firebird2Grammar
{
/**
* The components that make up a select clause.
*
* @var array
*/
protected $selectComponents = [
'limit', 'offset', 'aggregate', 'columns', 'from', 'joins',
'wheres', 'groups', 'havings', 'orders', 'lock',
];

/**
* @param Builder $query
* @param array $columns
* @return string|null
*/
protected function compileColumns(Builder $query, $columns)
{
// See superclass.
if (! is_null($query->aggregate)) {
return;
}

// In Firebird 1.5, the correct syntax of pagination is...
// "select first [num_rows] skip [start_row] * from table" instead of...
// "select * from table rows X to Y".
// Reference: http://mc-computing.com/Databases/Firebird/SQL.html

$select = 'select ';

if ($query->limit) {
$select .= 'first '.$query->limit.' ';
}

if ($query->offset) {
$select .= 'skip '.$query->offset.' ';
}

if ($query->distinct) {
$select .= 'distinct ';
}

return $select.$this->columnize($columns);
}

/**
* Compile the "limit" portions of the query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param int $limit
* @return string
*/
protected function compileLimit(Builder $query, $limit)
{
// The compileColumns() function handles query limits for v1.5 due to
// different query ordering.
return '';
}

/**
* Compile the "offset" portions of the query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param int $offset
* @return string
*/
protected function compileOffset(Builder $query, $offset)
{
// The compileColumns() function handles query offsets for v1.5 due to
// different query ordering.
return '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@

namespace Firebird\Query\Grammars;

use Firebird\Support\Version;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Query\Grammars\Grammar;

class FirebirdGrammar extends Grammar
class Firebird2Grammar extends Grammar
{
/**
* Firebird database version.
*
* @var string
*/
protected $version;

/**
* All of the available clause operators.
*
Expand All @@ -26,16 +18,6 @@ class FirebirdGrammar extends Grammar
'similar to', 'not similar to',
];

/**
* Create a new Firebird query grammar instance.
*
* @param string $version
*/
public function __construct($version)
{
$this->version = $version;
}

/**
* Compile an aggregated select clause.
*
Expand Down Expand Up @@ -94,12 +76,6 @@ public function compileProcedure(Builder $query, $procedure, array $values = nul
*/
protected function compileLimit(Builder $query, $limit)
{
// The compileColumns() function handles query limits for v1.5 due to
// different query ordering.
if ($this->version == Version::FIREBIRD_15) {
return '';
}

if ($query->offset) {
$first = (int) $query->offset + 1;

Expand All @@ -109,44 +85,6 @@ protected function compileLimit(Builder $query, $limit)
}
}

/**
* @param Builder $query
* @param array $columns
* @return string|null
*/
protected function compileColumns(Builder $query, $columns)
{
if ($this->version != Version::FIREBIRD_15) {
// Use default logic for all Firebird versions not 1.5
return parent::compileColumns($query, $columns);
}

if (! is_null($query->aggregate)) {
return;
}

// In Firebird 1.5, the correct syntax of pagination is...
// "select first [num_rows] skip [start_row] * from table" instead of...
// "select * from table rows X to Y".
// Reference: http://mc-computing.com/Databases/Firebird/SQL.html

$select = 'select ';

if ($query->limit) {
$select .= 'first '.$query->limit.' ';
}

if ($query->offset) {
$select .= 'skip '.$query->offset.' ';
}

if ($query->distinct) {
$select .= 'distinct ';
}

return $select.$this->columnize($columns);
}

/**
* Compile the "offset" portions of the query.
*
Expand All @@ -156,12 +94,6 @@ protected function compileColumns(Builder $query, $columns)
*/
protected function compileOffset(Builder $query, $offset)
{
// The compileColumns() function handles query offsets for v1.5 due to
// different query ordering.
if ($this->version == Version::FIREBIRD_15) {
return '';
}

if ($query->limit) {
if ($offset) {
$end = (int) $query->limit + (int) $offset;
Expand All @@ -177,25 +109,6 @@ protected function compileOffset(Builder $query, $offset)
}
}

/**
* Compile the components necessary for a select clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @return array
*/
protected function compileComponents(Builder $query)
{
// Use modified select components for Firebird v1.5
if ($this->version == Version::FIREBIRD_15) {
$this->selectComponents = [
'limit', 'offset', 'aggregate', 'columns', 'from', 'joins',
'wheres', 'groups', 'havings', 'orders', 'lock',
];
}

return parent::compileComponents($query);
}

/**
* Compile the random statement into SQL.
*
Expand Down

0 comments on commit e82c3b1

Please sign in to comment.