Skip to content

Commit

Permalink
added ability to use macroses on laravel builder, fixed wrong result …
Browse files Browse the repository at this point in the history
…merging in selectAsync, added array join ability
  • Loading branch information
FacedSID committed Sep 5, 2017
1 parent c395080 commit 3fb53d1
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/Integrations/Laravel/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

namespace Tinderbox\ClickhouseBuilder\Integrations\Laravel;

use Illuminate\Support\Traits\Macroable;
use Tinderbox\Clickhouse\Common\Format;
use Tinderbox\ClickhouseBuilder\Query\BaseBuilder;
use Tinderbox\ClickhouseBuilder\Query\Grammar;

class Builder extends BaseBuilder
{
use Macroable {
__call as macroCall;
}

/**
* Connection which is used to perform queries.
*
Expand Down
11 changes: 5 additions & 6 deletions src/Integrations/Laravel/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,18 +291,17 @@ public function select($query, $bindings = [], $tables = [])
*/
public function selectAsync(array $queries)
{
$results = $this->getClient()->selectAsync($queries);
$queriesKeys = array_keys($queries);

$results = array_combine($queriesKeys, $this->getClient()->selectAsync($queries));

foreach ($results as $i => $result) {
/* @var \Tinderbox\Clickhouse\Query\Result $result */

$query = $queries[$queriesKeys[$i]][0];
$bindings = $queries[$queriesKeys[$i]][1] ?? [];
$query = $queries[$i][0];
$bindings = $queries[$i][1] ?? [];

$this->logQuery($query, $bindings, $result->getStatistic()->getTime());

$results[$queriesKeys[$i]] = $result->getRows();
$results[$i] = $result->getRows();
}

return $results;
Expand Down
61 changes: 61 additions & 0 deletions src/Query/ArrayJoinClause.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Tinderbox\ClickhouseBuilder\Query;

use Tinderbox\ClickhouseBuilder\Query\Enums\JoinStrict;
use Tinderbox\ClickhouseBuilder\Query\Enums\JoinType;

class ArrayJoinClause
{
/**
* Identifier of array to join
*
* @var Expression|Identifier
*/
private $arrayIdentifier;

/**
* Builder which initiated join.
*
* @var Builder
*/
private $query;

/**
* JoinClause constructor.
*
* @param BaseBuilder $query
*/
public function __construct(BaseBuilder $query)
{
$this->query = $query;
}

/**
* Set array identifier for join.
*
* @param string|Expression $arrayIdentifier
*
* @return ArrayJoinClause
*/
public function array($arrayIdentifier): self
{
if (is_string($arrayIdentifier)) {
$arrayIdentifier = new Identifier($arrayIdentifier);
}

$this->arrayIdentifier = $arrayIdentifier;

return $this;
}

/**
* Get array identifier to join.
*
* @return Expression|Identifier
*/
public function getArrayIdentifier()
{
return $this->arrayIdentifier;
}
}
34 changes: 34 additions & 0 deletions src/Query/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ abstract class BaseBuilder
* @var JoinClause
*/
protected $join;

/**
* Array join clause.
*
* @var ArrayJoinClause
*/
protected $arrayJoin;

/**
* Prewhere statements.
Expand Down Expand Up @@ -276,6 +283,8 @@ public function from($table, string $alias = null, bool $isFinal = null)
*/
if ($table instanceof BaseBuilder) {
$this->from->query($table);

$this->files = array_merge($this->files, $table->getFiles());
}

/*
Expand Down Expand Up @@ -401,6 +410,21 @@ public function final(bool $final = true)

return $this;
}

/**
* Add array join to query.
*
* @param string|Expression $arrayIdentifier
*
* @return static
*/
public function arrayJoin($arrayIdentifier)
{
$this->arrayJoin = new ArrayJoinClause($this);
$this->arrayJoin->array($arrayIdentifier);

return $this;
}

/**
* Add join to query.
Expand Down Expand Up @@ -1797,6 +1821,16 @@ public function getFrom() : ?From
{
return $this->from;
}

/**
* Get ArrayJoinClause
*
* @return null|ArrayJoinClause
*/
public function getArrayJoin() : ?ArrayJoinClause
{
return $this->arrayJoin;
}

/**
* Get JoinClause.
Expand Down
3 changes: 3 additions & 0 deletions src/Query/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Tinderbox\ClickhouseBuilder\Query\Traits\FromComponentCompiler;
use Tinderbox\ClickhouseBuilder\Query\Traits\GroupsComponentCompiler;
use Tinderbox\ClickhouseBuilder\Query\Traits\HavingsComponentCompiler;
use Tinderbox\ClickhouseBuilder\Query\Traits\ArrayJoinComponentCompiler;
use Tinderbox\ClickhouseBuilder\Query\Traits\JoinComponentCompiler;
use Tinderbox\ClickhouseBuilder\Query\Traits\LimitByComponentCompiler;
use Tinderbox\ClickhouseBuilder\Query\Traits\LimitComponentCompiler;
Expand All @@ -25,6 +26,7 @@ class Grammar
{
use ColumnsComponentCompiler,
FromComponentCompiler,
ArrayJoinComponentCompiler,
JoinComponentCompiler,
TwoElementsLogicExpressionsCompiler,
WheresComponentCompiler,
Expand All @@ -43,6 +45,7 @@ class Grammar
'columns',
'from',
'sample',
'arrayJoin',
'join',
'prewheres',
'wheres',
Expand Down
26 changes: 26 additions & 0 deletions src/Query/Traits/ArrayJoinComponentCompiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Tinderbox\ClickhouseBuilder\Query\Traits;

use Tinderbox\ClickhouseBuilder\Query\ArrayJoinClause;
use Tinderbox\ClickhouseBuilder\Query\BaseBuilder as Builder;

trait ArrayJoinComponentCompiler
{
/**
* Compiles join to string to pass this string in query.
*
* @param Builder $query
* @param ArrayJoinClause $join
*
* @return string
*/
protected function compileArrayJoinComponent(Builder $query, ArrayJoinClause $join) : string
{
$result = [];
$result[] = 'ARRAY JOIN';
$result[] = $this->wrap($join->getArrayIdentifier());

return implode(' ', $result);
}
}

0 comments on commit 3fb53d1

Please sign in to comment.