Skip to content

Commit

Permalink
- Abstracted several expression to their base
Browse files Browse the repository at this point in the history
- Arrays and object values are now recursively normalized allowing for the use of attribute pointers and functions within an object or List
- Improved performance of several syntax checking methods especially concerning operators.
  • Loading branch information
bas committed Oct 20, 2019
1 parent 4acc2d7 commit 54e4fb1
Show file tree
Hide file tree
Showing 33 changed files with 608 additions and 428 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"phpstan/phpstan": "^0.12.0@dev",
"phpunit/phpunit": "8.*",
"sebastian/phpcpd": "^4.1",
"triagens/arangodb": "3.5.*"
"triagens/arangodb": "^3.5"
},
"autoload": {
"psr-4": {
Expand Down
10 changes: 5 additions & 5 deletions src/API/hasGraphClauses.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function with() : QueryBuilder
$collections = func_get_args();
foreach ($collections as $key => $collection) {
$this->registerCollections($collection, 'read');
$collections[$key] = $this->normalizeArgument($collection, 'collection');
$collections[$key] = $this->normalizeArgument($collection, 'Collection');
}

$this->addCommand(new WithClause($collections));
Expand All @@ -50,11 +50,11 @@ public function with() : QueryBuilder
*/
public function traverse($fromVertex, $inDirection = 'outbound', $toVertex = null, $kShortestPaths = false) : QueryBuilder
{
$fromVertex = $this->normalizeArgument($fromVertex, 'id');
$inDirection = $this->normalizeArgument($inDirection, 'direction');
$fromVertex = $this->normalizeArgument($fromVertex, 'Id');
$inDirection = $this->normalizeArgument($inDirection, 'Direction');

if ($toVertex !== null) {
$toVertex = $this->normalizeArgument($toVertex, 'id');
$toVertex = $this->normalizeArgument($toVertex, 'Id');
}


Expand Down Expand Up @@ -105,7 +105,7 @@ public function kShortestPaths($fromVertex, $inDirection, $toVertex) : QueryBuil
*/
public function graph(string $graphName) : QueryBuilder
{
$graphName = $this->normalizeArgument($graphName, 'graph');
$graphName = $this->normalizeArgument($graphName, 'Graph');

$this->addCommand(new GraphClause($graphName));

Expand Down
8 changes: 3 additions & 5 deletions src/API/hasMiscellaneousFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
namespace LaravelFreelancerNL\FluentAQL\API;

use LaravelFreelancerNL\FluentAQL\Expressions\FunctionExpression;
use LaravelFreelancerNL\FluentAQL\Expressions\KeyExpression;
use LaravelFreelancerNL\FluentAQL\Expressions\ListExpression;
use LaravelFreelancerNL\FluentAQL\Expressions\LiteralExpression;

/**
* Trait hasFunctions
Expand All @@ -21,7 +19,7 @@ trait hasMiscellaneousFunctions
* @link https://www.arangodb.com/docs/stable/aql/functions-miscellaneous.html#document
*
* @param $collection
* @param null|string|array|KeyExpression|ListExpression $id
* @param null|string|array|ListExpression $id
* @return FunctionExpression
*/
public function document($collection, $id = null)
Expand All @@ -34,9 +32,9 @@ public function document($collection, $id = null)
}

if ($collection !== null) {
$arguments['collection'] = $this->normalizeArgument($collection, ['collection', 'id']);
$arguments['collection'] = $this->normalizeArgument($collection, ['Collection', 'Id']);
}
$arguments['id'] = $this->normalizeArgument($id, ['query', 'list', 'key', 'id']);
$arguments['id'] = $this->normalizeArgument($id, ['Id', 'Key', 'Query', 'List']);

return new FunctionExpression('DOCUMENT', $arguments);
}
Expand Down
64 changes: 40 additions & 24 deletions src/API/hasQueryClauses.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use LaravelFreelancerNL\FluentAQL\Clauses\CollectClause;
use LaravelFreelancerNL\FluentAQL\Clauses\FilterClause;
use LaravelFreelancerNL\FluentAQL\Clauses\GroupClause;
use LaravelFreelancerNL\FluentAQL\Clauses\InClause;
use LaravelFreelancerNL\FluentAQL\Clauses\KeepClause;
use LaravelFreelancerNL\FluentAQL\Clauses\LimitClause;
use LaravelFreelancerNL\FluentAQL\Clauses\OptionsClause;
Expand All @@ -14,9 +13,7 @@
use LaravelFreelancerNL\FluentAQL\Clauses\ReturnClause;
use LaravelFreelancerNL\FluentAQL\Clauses\SearchClause;
use LaravelFreelancerNL\FluentAQL\Clauses\SortClause;
use LaravelFreelancerNL\FluentAQL\Clauses\WithClause;
use LaravelFreelancerNL\FluentAQL\Clauses\WithCountClause;
use LaravelFreelancerNL\FluentAQL\Expressions\NullExpression;
use LaravelFreelancerNL\FluentAQL\QueryBuilder;

/**
Expand All @@ -33,20 +30,31 @@ trait hasQueryClauses
* You HAVE TO prepare user input yourself or be open to injection attacks.
*
* @param string $aql
* @param null $bindings
* @param null $collections
* @return $this
* @param null $binds
* @param array|null $collections
* @return QueryBuilder
*/
public function raw(string $aql, $bindings = [], $collections = []) : QueryBuilder
public function raw(string $aql, $binds = null, $collections = null) : QueryBuilder
{
if (is_array($binds)) {
foreach ($binds as $key => $value) {
$this->bind($value, $key);
}
}
if (is_array($binds)) {
foreach ($collections as $mode => $modeCollections) {
$this->registerCollections($modeCollections, $mode);
}
}

$this->addCommand(new RawClause($aql));

return $this;
}

public function options($options) : QueryBuilder
{
$options = $this->normalizeArgument($options, 'document');
$options = $this->normalizeArgument($options, 'Object');

$this->addCommand(new OptionsClause($options));

Expand All @@ -56,7 +64,7 @@ public function options($options) : QueryBuilder

/**
* Create a for clause
* @link https://www.arangodb.com/docs/3.4/aql/operations-for.html
* @link https://www.arangodb.com/docs/stable/aql/operations-for.html
*
* @param string|array $variableName
* @param mixed $in
Expand All @@ -69,11 +77,12 @@ public function for($variableName, $in = null) : QueryBuilder
}

foreach ($variableName as $key => $value) {
$variableName[$key] = $this->normalizeArgument($value, 'variable');
$variableName[$key] = $this->normalizeArgument($value, 'Variable');
$this->registerVariable($variableName[$key]);
}

if ($in !== null) {
$in = $this->normalizeArgument($in, ['collection', 'range', 'list', 'query']);
$in = $this->normalizeArgument($in, ['Collection', 'Range', 'List', 'Query']);
}

$this->addCommand(new ForClause($variableName, $in));
Expand Down Expand Up @@ -142,10 +151,10 @@ public function search($attribute, $comparisonOperator = '==', $value = null, $l
public function collect($variableName = null, $expression = null) : QueryBuilder
{
if (isset($variableName)) {
$variableName = $this->normalizeArgument($variableName, 'variable');
$variableName = $this->normalizeArgument($variableName, 'Variable');
}
if (isset($expression)) {
$expression = $this->normalizeArgument($expression, ['attribute', 'function', 'query', 'bind']);
$expression = $this->normalizeArgument($expression, ['VariableAttribute', 'Function', 'Query', 'Bind']);
}

$this->addCommand(new CollectClause($variableName, $expression));
Expand All @@ -164,9 +173,11 @@ public function collect($variableName = null, $expression = null) : QueryBuilder
*/
public function group($groupsVariable, $projectionExpression = null) : QueryBuilder
{
$groupsVariable = $this->normalizeArgument($groupsVariable, 'variable');
$groupsVariable = $this->normalizeArgument($groupsVariable, 'Variable');
$this->registerVariable($groupsVariable);

if (isset($projectionExpression)) {
$projectionExpression = $this->normalizeArgument($projectionExpression, ['attribute', 'document', 'function', 'query', 'bind']);
$projectionExpression = $this->normalizeArgument($projectionExpression, ['VariableAttribute', 'Object', 'Function', 'Query', 'Bind']);
}

$this->addCommand(new GroupClause($groupsVariable, $projectionExpression));
Expand All @@ -184,7 +195,8 @@ public function group($groupsVariable, $projectionExpression = null) : QueryBuil
*/
public function keep($keepVariable) : QueryBuilder
{
$keepVariable = $this->normalizeArgument($keepVariable, 'variable');
$keepVariable = $this->normalizeArgument($keepVariable, 'Variable');
$this->registerVariable($keepVariable);

$this->addCommand(new KeepClause($keepVariable));

Expand All @@ -203,7 +215,8 @@ public function keep($keepVariable) : QueryBuilder
*/
public function withCount($countVariableName) : QueryBuilder
{
$countVariableName = $this->normalizeArgument($countVariableName, 'variable');
$countVariableName = $this->normalizeArgument($countVariableName, 'Variable');
$this->registerVariable($countVariableName);

$this->addCommand(new WithCountClause($countVariableName));

Expand All @@ -221,8 +234,10 @@ public function withCount($countVariableName) : QueryBuilder
*/
public function aggregate($variableName, $aggregateExpression) : QueryBuilder
{
$variableName = $this->normalizeArgument($variableName, 'variable');
$aggregateExpression = $this->normalizeArgument($aggregateExpression, 'attribute', 'function', 'query', 'bind');
$variableName = $this->normalizeArgument($variableName, 'Variable');
$this->registerVariable($variableName);

$aggregateExpression = $this->normalizeArgument($aggregateExpression, ['VariableAttribute', 'Function', 'Query', 'Bind']);

$this->addCommand(new AggregateClause($variableName, $aggregateExpression));

Expand Down Expand Up @@ -261,15 +276,14 @@ public function sort($sortBy = null, $direction = null) : QueryBuilder
* Limit results
* @link https://www.arangodb.com/docs/stable/aql/operations-limit.html
*
* @param $offsetOrCount
* @param null $count
* @param int $offsetOrCount
* @param int $count
* @return $this
*/
public function limit($offsetOrCount, $count = null)
public function limit(int $offsetOrCount, int $count = null)
{
$offsetOrCount = $this->normalizeArgument($offsetOrCount, 'numeric');
if ($count !== null) {
$count = $this->normalizeArgument($count, 'numeric');
$count = (int) $count;
}

$this->addCommand(new LimitClause($offsetOrCount, $count));
Expand All @@ -287,6 +301,8 @@ public function limit($offsetOrCount, $count = null)
*/
public function return($expression, $distinct = false) : QueryBuilder
{
$expression = $this->normalizeArgument($expression, ['Variable', 'VariableAttribute', 'Object', 'Function', 'Query', 'Bind']);

$this->addCommand(new ReturnClause($expression, $distinct));

return $this;
Expand Down
46 changes: 24 additions & 22 deletions src/API/hasStatementClauses.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ trait hasStatementClauses
{
/**
* Assign a value to a variable.
* @link https://www.arangodb.com/docs/3.4/aql/operations-let.html
* @link https://www.arangodb.com/docs/stable/aql/operations-let.html
*
* @param $variableName
* @param $expression
* @return $this
*/
public function let($variableName, $expression)
{
$variableName = $this->normalizeArgument($variableName, 'variable');
$expression = $this->normalizeArgument($expression, ['query', 'list', 'range', 'numeric', 'bind']);
$variableName = $this->normalizeArgument($variableName, 'Variable');
$this->registerVariable($variableName);

$expression = $this->normalizeArgument($expression, ['List', 'Object', 'Query', 'Range', 'Number', 'Bind']);

$this->addCommand(new LetClause($variableName, $expression));

Expand All @@ -37,18 +39,18 @@ public function let($variableName, $expression)

/**
* Insert a document in a collection
* @link https://www.arangodb.com/docs/3.4/aql/operations-insert.html
* @link https://www.arangodb.com/docs/stable/aql/operations-insert.html
*
* @param $document
* @param string $collection
* @return QueryBuilder
*/
public function insert($document, string $collection) : QueryBuilder
{
$document = $this->normalizeArgument($document, ['key', 'variable', 'bind']);
$document = $this->normalizeArgument($document, ['Key', 'Variable', 'Bind']);

$this->registerCollections($collection);
$collection = $this->normalizeArgument($collection, ['collection', 'bind']);
$collection = $this->normalizeArgument($collection, ['Collection', 'Bind']);

$this->addCommand(new InsertClause($document, $collection));

Expand All @@ -57,7 +59,7 @@ public function insert($document, string $collection) : QueryBuilder

/**
* Update a document in a collection with the supplied data
* @link https://www.arangodb.com/docs/3.4/aql/operations-update.html
* @link https://www.arangodb.com/docs/stable/aql/operations-update.html
*
* @param $document
* @param $with
Expand All @@ -66,10 +68,10 @@ public function insert($document, string $collection) : QueryBuilder
*/
public function update($document, $with, $collection) : QueryBuilder
{
$document = $this->normalizeArgument($document, ['key', 'variable', 'bind']);
$with = $this->normalizeArgument($with, ['numeric', 'bind']);
$document = $this->normalizeArgument($document, ['Key', 'Variable', 'Bind']);
$with = $this->normalizeArgument($with, ['Number', 'Bind']);
$this->registerCollections($collection);
$collection = $this->normalizeArgument($collection, ['collection', 'bind']);
$collection = $this->normalizeArgument($collection, ['Collection', 'Bind']);

$this->addCommand(new UpdateClause($document, $with, $collection));

Expand All @@ -78,7 +80,7 @@ public function update($document, $with, $collection) : QueryBuilder

/**
* Replace a document in a collection with the supplied data
* @link https://www.arangodb.com/docs/3.4/aql/operations-replace.html
* @link https://www.arangodb.com/docs/stable/aql/operations-replace.html
*
* @param $document
* @param $with
Expand All @@ -87,10 +89,10 @@ public function update($document, $with, $collection) : QueryBuilder
*/
public function replace($document, $with, string $collection) : QueryBuilder
{
$document = $this->normalizeArgument($document, ['key', 'variable', 'bind']);
$with = $this->normalizeArgument($with, ['numeric', 'bind']);
$document = $this->normalizeArgument($document, ['Key', 'Variable', 'Bind']);
$with = $this->normalizeArgument($with, ['Number', 'Bind']);
$this->registerCollections($collection);
$collection = $this->normalizeArgument($collection, ['collection', 'bind']);
$collection = $this->normalizeArgument($collection, ['Collection', 'Bind']);

$this->addCommand(new ReplaceClause($document, $with, $collection));

Expand All @@ -99,7 +101,7 @@ public function replace($document, $with, string $collection) : QueryBuilder

/**
* Update, replace or insert documents in a collection with the supplied data.
* @link https://www.arangodb.com/docs/3.4/aql/operations-upsert.html
* @link https://www.arangodb.com/docs/stable/aql/operations-upsert.html
*
* @param mixed $search
* @param mixed $insert
Expand All @@ -110,11 +112,11 @@ public function replace($document, $with, string $collection) : QueryBuilder
*/
public function upsert($search, $insert, $with, string $collection, bool $replace = false) : QueryBuilder
{
$search = $this->normalizeArgument($search, ['key', 'variable', 'bind']);
$insert = $this->normalizeArgument($insert, ['key', 'variable', 'bind']);
$with = $this->normalizeArgument($with, ['numeric', 'bind']);
$search = $this->normalizeArgument($search, ['Key', 'Variable', 'Bind']);
$insert = $this->normalizeArgument($insert, ['Key', 'Variable', 'Bind']);
$with = $this->normalizeArgument($with, ['Number', 'Bind']);
$collection = $this->normalizeArgument($collection, ['Collection', 'Bind']);
$this->registerCollections($collection);
$collection = $this->normalizeArgument($collection, ['collection', 'bind']);

$this->addCommand(new UpsertClause($search, $insert, $with, $collection, $replace));

Expand All @@ -123,17 +125,17 @@ public function upsert($search, $insert, $with, string $collection, bool $replac

/**
* Remove a document from a collection
* @link https://www.arangodb.com/docs/3.4/aql/operations-remove.html
* @link https://www.arangodb.com/docs/stable/aql/operations-remove.html
*
* @param mixed $document
* @param string $collection
* @return QueryBuilder
*/
public function remove($document, string $collection) : QueryBuilder
{
$document = $this->normalizeArgument($document, ['key', 'variable', 'bind']);
$document = $this->normalizeArgument($document, ['Key', 'Variable', 'Bind']);
$this->registerCollections($collection);
$collection = $this->normalizeArgument($collection, ['collection', 'bind']);
$collection = $this->normalizeArgument($collection, ['Collection', 'Bind']);

$this->addCommand(new RemoveClause($document, $collection));

Expand Down
3 changes: 0 additions & 3 deletions src/Clauses/AggregateClause.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?php
namespace LaravelFreelancerNL\FluentAQL\Clauses;

use LaravelFreelancerNL\FluentAQL\Expressions\ExpressionInterface;
use LaravelFreelancerNL\FluentAQL\Expressions\VariableExpression;

class AggregateClause extends Clause
{
protected $variableName;
Expand Down
1 change: 0 additions & 1 deletion src/Clauses/CollectClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace LaravelFreelancerNL\FluentAQL\Clauses;

use LaravelFreelancerNL\FluentAQL\Expressions\ExpressionInterface;
use LaravelFreelancerNL\FluentAQL\Expressions\VariableExpression;

class CollectClause extends Clause
{
Expand Down
Loading

0 comments on commit 54e4fb1

Please sign in to comment.