Skip to content

version support #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/Firebird/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace Firebird;

use Firebird\Query\Builder as QueryBuilder;
use Firebird\Query\Grammars\Firebird25Grammar as QueryGrammar;
use Firebird\Query\Grammars\Firebird15Grammar as QueryGrammar10;
use Firebird\Query\Grammars\Firebird25Grammar as QueryGrammar20;
use Firebird\Query\Grammars\Firebird30Grammar as QueryGrammar30;
use Firebird\Query\Processors\FirebirdProcessor as Processor;
use Firebird\Schema\Builder as SchemaBuilder;
Expand Down Expand Up @@ -56,14 +57,21 @@ protected function getMajorEngineVersion()
/**
* Get the default query grammar instance
*
* @return Query\Grammars\Firebird25Grammar
* @return QueryGrammar10|QueryGrammar20|QueryGrammar30
*/
protected function getDefaultQueryGrammar()
{
if ($this->getMajorEngineVersion() >= 3) {
return new QueryGrammar30;
switch ($this->getMajorEngineVersion()){
case 1:
return new QueryGrammar10;
break;
case 3:
return new QueryGrammar30;
break;
default:
return new QueryGrammar20;
break;
}
return new QueryGrammar;
}

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

namespace Firebird\Query\Grammars;

use Illuminate\Database\Query\Builder;
use Illuminate\Database\Query\Grammars\Grammar;

class Firebird15Grammar extends Grammar
{

/**
* The components that make up a select clause.
*
* @var array
*/
protected $selectComponents = array(
'aggregate',
'limit',
'offset',
'columns',
'from',
'joins',
'wheres',
'groups',
'havings',
'orders'
);

/**
* Compile the "select *" portion of the query.
* As Firebird adds the "limit" and "offset" after the "select", this must not work this way.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $columns
* @return string
*/
protected function compileColumns(Builder $query, $columns)
{
// If the query is actually performing an aggregating select, we will let that
// compiler handle the building of the select clauses, as it will need some
// more syntax that is best handled by that function to keep things neat.
if ( ! is_null($query->aggregate)) return;
$select = '';
if (count($columns) > 0 && $query->limit == null && $query->aggregate == null)
{
$select = $query->distinct ? 'select distinct ' : 'select ';
}

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

/**
* Compile a select query into SQL.
*
* @param Illuminate\Database\Query\Builder
*
* @return string
*/
public function compileSelect(Builder $query)
{
if (is_null($query->columns)) $query->columns = array('*');

return trim($this->concatenate($this->compileComponents($query)));
}

/**
* Compile an aggregated select clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $aggregate
* @return string
*/
protected function compileAggregate(Builder $query, $aggregate)
{
$column = $this->columnize($aggregate['columns']);

// If the query has a "distinct" constraint and we're not asking for all columns
// we need to prepend "distinct" onto the column name so that the query takes
// it into account when it performs the aggregating operations on the data.
if ($query->distinct && $column !== '*')
{
$column = 'distinct '.$column;
}

return 'select '.$aggregate['function'].'('.$column.') as aggregate';
}

/**
* Compile first instead of limit
*
* @param \Illuminate\Database\Query\Builder $query
* @param int $limit
* @return string
*/
protected function compileLimit(Builder $query, $limit)
{
return 'select first '.(int) $limit;
}

/**
* Compile skip instead of offset
*
* @param \Illuminate\Database\Query\Builder $query
* @param int $limit
* @return string
*/
protected function compileOffset(Builder $query, $limit)
{
return 'skip '.(int) $limit;
}

}