Skip to content

Commit 5247ca0

Browse files
committed
Adding conditions support and continuing development
1 parent e46183d commit 5247ca0

18 files changed

+285
-169
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace Mouf\Database\QueryWriter\Condition;
3+
4+
use Mouf\Utils\Common\ConditionInterface\ConditionInterface;
5+
6+
/**
7+
* This condition returns true if the SQL parameter has been passed and is not empty.
8+
*
9+
* @author David Negrier
10+
*/
11+
class ParamAvailableCondition implements ConditionInterface {
12+
13+
private $parameterName;
14+
15+
/**
16+
* @Important
17+
* @param string $parameterName The name of the parameter to check
18+
*/
19+
public function __construct($parameterName) {
20+
$this->parameterName = $parameterName;
21+
}
22+
23+
/**
24+
* @param string $caller
25+
*/
26+
public function isOk($parameters = null) {
27+
return isset($parameters[$this->parameterName]) && !empty($parameters[$this->parameterName]);
28+
}
29+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
namespace Mouf\Database\QueryWriter\Condition;
3+
4+
use Mouf\Utils\Value\ValueUtils;
5+
6+
use Mouf\Utils\Value\ValueInterface;
7+
8+
use Mouf\Utils\Common\ConditionInterface\ConditionInterface;
9+
10+
/**
11+
* This condition returns true if the SQL parameter is equal to the passed value.
12+
*
13+
* @author David Negrier
14+
*/
15+
class ParamEqualsCondition implements ConditionInterface {
16+
17+
private $parameterName;
18+
private $value;
19+
20+
/**
21+
* @Important
22+
* @param string $parameterName The name of the parameter to check
23+
* @param string|ValueInterface $value
24+
*/
25+
public function __construct($parameterName, $value) {
26+
$this->parameterName = $parameterName;
27+
$this->value = $value;
28+
}
29+
30+
/**
31+
* @param string $caller
32+
*/
33+
public function isOk($parameters = null) {
34+
return isset($parameters[$this->parameterName]) && $parameters[$this->parameterName] == ValueUtils::val($this->value);
35+
}
36+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace Mouf\Database\QueryWriter\Condition;
3+
4+
use Mouf\Utils\Common\ConditionInterface\ConditionInterface;
5+
6+
/**
7+
* This condition returns true if the SQL parameter has not been passed or if it is empty.
8+
*
9+
* @author David Negrier
10+
*/
11+
class ParamNotAvailableCondition implements ConditionInterface {
12+
13+
private $parameterName;
14+
15+
/**
16+
* @Important
17+
* @param string $parameterName The name of the parameter to check
18+
*/
19+
public function __construct($parameterName) {
20+
$this->parameterName = $parameterName;
21+
}
22+
23+
/**
24+
* @param string $caller
25+
*/
26+
public function isOk($parameters = null) {
27+
return !isset($parameters[$this->parameterName]) || empty($parameters[$this->parameterName]);
28+
}
29+
}

src/Mouf/Database/QueryWriter/Controllers/SelectController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function defaultAction($name, $selfedit="false") {
4343
$this->initController($name, $selfedit);
4444

4545
$select = MoufManager::getMoufManagerHiddenInstance()->getInstance($name);
46-
$this->sql = $select->toSql(null);
46+
$this->sql = $select->toSql(null, array(), 0, true);
4747

4848
$this->content->addFile(dirname(__FILE__)."/../../../../views/parseSql.php", $this);
4949
$this->template->toHtml();
@@ -59,7 +59,7 @@ public function defaultAction($name, $selfedit="false") {
5959
public function parse($name, $sql,$selfedit="false") {
6060
$this->initController($name, $selfedit);
6161

62-
require_once __DIR__.'/../../../../php-sql-parser/php-sql-parser.php';
62+
//require_once __DIR__.'/../../../../php-sql-parser/php-sql-parser.php';
6363

6464
/*$parser = new \PHPSQLParser();
6565
$parsed = $parser->parse($sql);

src/Mouf/Database/QueryWriter/Select.php

Lines changed: 0 additions & 126 deletions
This file was deleted.

src/SQLParser/Node/AbstractManyInstancesOperator.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,23 @@ public function toInstanceDescriptor(MoufManager $moufManager) {
4848

4949
/**
5050
* Renders the object as a SQL string
51-
*
51+
*
5252
* @param ConnectionInterface $dbConnection
53+
* @param array $parameters
54+
* @param number $indent
55+
* @param bool $ignoreConditions
5356
* @return string
5457
*/
55-
public function toSql(ConnectionInterface $dbConnection = null) {
56-
$sqlOperands = array_map(function($item) use ($dbConnection) {
57-
return NodeFactory::toSql($item, $dbConnection, ' ', true);
58-
}, $this->operands);
58+
public function toSql(ConnectionInterface $dbConnection = null, array $parameters = array(), $indent = 0, $ignoreConditions = false) {
59+
$sqlOperands = array();
60+
foreach ($this->operands as $operand) {
61+
$sql = NodeFactory::toSql($operand, $dbConnection, $parameters, ' ', true, $indent, $ignoreConditions);
62+
if ($sql != null) {
63+
$sqlOperands[] = $sql;
64+
}
65+
}
5966

60-
return implode("\n ".$this->getOperatorSymbol().' ', $sqlOperands);
67+
return implode("\n".str_repeat(' ', $indent).$this->getOperatorSymbol().' ', $sqlOperands);
6168
}
6269

6370
/**

src/SQLParser/Node/AbstractTwoOperandsOperator.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,38 @@ public function toInstanceDescriptor(MoufManager $moufManager) {
6363

6464
if ($this->leftOperand instanceof Parameter) {
6565
// Let's add a condition on the parameter.
66-
// TODO
66+
$conditionDescriptor = $moufManager->createInstance("Mouf\\Database\\QueryWriter\\Condition\\ParamAvailableCondition");
67+
$conditionDescriptor->getProperty("parameterName")->setValue($this->leftOperand->getName());
68+
$instanceDescriptor->getProperty("condition")->setValue($conditionDescriptor);
69+
}
70+
// TODO: manage cases where both leftOperand and rightOperand are parameters.
71+
if ($this->rightOperand instanceof Parameter) {
72+
// Let's add a condition on the parameter.
73+
$conditionDescriptor = $moufManager->createInstance("Mouf\\Database\\QueryWriter\\Condition\\ParamAvailableCondition");
74+
$conditionDescriptor->getProperty("parameterName")->setValue($this->rightOperand->getName());
75+
$instanceDescriptor->getProperty("condition")->setValue($conditionDescriptor);
6776
}
6877

6978
return $instanceDescriptor;
7079
}
7180

7281
/**
7382
* Renders the object as a SQL string
74-
*
83+
*
7584
* @param ConnectionInterface $dbConnection
85+
* @param array $parameters
86+
* @param number $indent
87+
* @param bool $ignoreConditions
7688
* @return string
7789
*/
78-
public function toSql(ConnectionInterface $dbConnection = null) {
79-
$sql = NodeFactory::toSql($this->leftOperand, $dbConnection, ' ', false);
80-
$sql .= ' '.$this->getOperatorSymbol().' ';
81-
$sql .= NodeFactory::toSql($this->rightOperand, $dbConnection, ' ', false);
90+
public function toSql(ConnectionInterface $dbConnection = null, array $parameters = array(), $indent = 0, $ignoreConditions = false) {
91+
if ($ignoreConditions || $this->condition->isOk($parameters)) {
92+
$sql = NodeFactory::toSql($this->leftOperand, $dbConnection, $parameters, ' ', false, $indent, $ignoreConditions);
93+
$sql .= ' '.$this->getOperatorSymbol().' ';
94+
$sql .= NodeFactory::toSql($this->rightOperand, $dbConnection, $parameters, ' ', false, $indent, $ignoreConditions);
95+
} else {
96+
$sql = null;
97+
}
8298

8399
return $sql;
84100
}

src/SQLParser/Node/AggregateFunction.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
namespace SQLParser\Node;
3434

35+
use Mouf\Database\DBConnection\ConnectionInterface;
36+
3537
use Mouf\MoufManager;
3638

3739
use Mouf\MoufInstanceDescriptor;
@@ -57,6 +59,7 @@ public function getFunctionName() {
5759
/**
5860
* Sets the base expression (the string that generated this expression).
5961
*
62+
* @Important
6063
* @param string $functionName
6164
*/
6265
public function setFunctionName($functionName) {
@@ -72,6 +75,7 @@ public function getSubTree() {
7275
/**
7376
* Sets the subtree
7477
*
78+
* @Important
7579
* @param array<NodeInterface> $subTree
7680
*/
7781
public function setSubTree($subTree) {
@@ -106,4 +110,29 @@ public function toInstanceDescriptor(MoufManager $moufManager) {
106110
$instanceDescriptor->getProperty("alias")->setValue($this->alias, $moufManager);
107111
return $instanceDescriptor;
108112
}
113+
114+
/**
115+
* Renders the object as a SQL string
116+
*
117+
* @param ConnectionInterface $dbConnection
118+
* @param array $parameters
119+
* @param number $indent
120+
* @param bool $ignoreConditions
121+
* @return string
122+
*/
123+
public function toSql(ConnectionInterface $dbConnection = null, array $parameters = array(), $indent = 0, $ignoreConditions = false) {
124+
$subTreeSql = NodeFactory::toSql($this->subTree, $dbConnection, $parameters, ' ', false, $indent, $ignoreConditions);
125+
if ($subTreeSql !== null) {
126+
$sql = $this->functionName.'(';
127+
$sql .= $subTreeSql;
128+
$sql .= ')';
129+
if ($this->alias) {
130+
$sql .= ' AS '.$this->alias;
131+
}
132+
} else {
133+
$sql = null;
134+
}
135+
136+
return $sql;
137+
}
109138
}

0 commit comments

Comments
 (0)