Skip to content

Commit a8983dd

Browse files
committed
Adapting Querywriter to ValueInterface
1 parent 4e9502a commit a8983dd

17 files changed

+216
-52
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"require": {
1616
"php": ">=5.3.0",
1717
"mouf/database.dbconnection": "~2.0",
18-
"mouf/utils.common.conditioninterface": "~2.0"
18+
"mouf/utils.common.conditioninterface": "~2.0",
19+
"mouf/utils.value.value-interface": "~1.0"
1920
},
2021
"autoload": {
2122
"psr-0": {

src/Mouf/Database/QueryWriter/Filters/AndFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function setFilters($filters) {
4646
*
4747
* @param array<FilterInterface> $filters
4848
*/
49-
public function AndFilter($filters=null) {
49+
public function __construct($filters=null) {
5050
$this->filters = $filters;
5151
}
5252

src/Mouf/Database/QueryWriter/Filters/BetweenFilter.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
namespace Mouf\Database\QueryWriter\Filters;
2121

22+
use Mouf\Utils\Value\ValueUtils;
23+
24+
use Mouf\Utils\Value\ScalarValueInterface;
25+
2226
use Mouf\Database\DBConnection\ConnectionInterface;
2327

2428
/**
@@ -60,7 +64,7 @@ public function setColumnName($columnName) {
6064
*
6165
* @Property
6266
* @Compulsory
63-
* @param string $value1
67+
* @param string|ScalarValueInterface|Param $value1
6468
*/
6569
public function setValue1($value1) {
6670
$this->value1 = $value1;
@@ -71,7 +75,7 @@ public function setValue1($value1) {
7175
*
7276
* @Property
7377
* @Compulsory
74-
* @param string $value2
78+
* @param string|ScalarValueInterface|Param $value2
7579
*/
7680
public function setValue2($value2) {
7781
$this->value2 = $value2;
@@ -94,11 +98,16 @@ public function setEnableCondition($enableCondition) {
9498
* Default constructor to build the filter.
9599
* All parameters are optional and can later be set using the setters.
96100
*
101+
* @Important $tableName
102+
* @Important $columnName
103+
* @Important $value1
104+
* @Important $value2
97105
* @param string $tableName
98106
* @param string $columnName
99-
* @param string $value
107+
* @param string|ScalarValueInterface|Param $value1
108+
* @param string|ScalarValueInterface|Param $value2
100109
*/
101-
public function BetweenFilter($tableName=null, $columnName=null, $value1=null, $value2=null) {
110+
public function __construct($tableName=null, $columnName=null, $value1=null, $value2=null) {
102111
$this->tableName = $tableName;
103112
$this->columnName = $columnName;
104113
$this->value1 = $value1;
@@ -115,13 +124,15 @@ public function toSql(ConnectionInterface $dbConnection) {
115124
if ($this->enableCondition != null && !$this->enableCondition->isOk()) {
116125
return "";
117126
}
118-
119127

120-
if ($this->value1 === null || $this->value2 === null) {
128+
$value1 = ValueUtils::val($this->value1);
129+
$value2 = ValueUtils::val($this->value2);
130+
131+
if ($value1 === null || $value2 === null) {
121132
throw new Exception('Error in BetweenFilter: one of the value passed is NULL.');
122133
}
123134

124-
return $this->tableName.'.'.$this->columnName.' BETWEEN '.$dbConnection->quoteSmart($this->value1)." AND ".$dbConnection->quoteSmart($this->value2);
135+
return $this->tableName.'.'.$this->columnName.' BETWEEN '.SqlValueUtils::toSql($this->value1)." AND ".SqlValueUtils::toSql($this->value2);
125136
}
126137

127138
/**

src/Mouf/Database/QueryWriter/Filters/DifferentFilter.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
namespace Mouf\Database\QueryWriter\Filters;
2121

22+
use Mouf\Utils\Value\ValueUtils;
23+
24+
use Mouf\Utils\Value\ScalarValueInterface;
25+
2226
use Mouf\Database\DBConnection\ConnectionInterface;
2327

2428
/**
@@ -58,7 +62,7 @@ public function setColumnName($columnName) {
5862
* The value to compare to in the filter.
5963
*
6064
* @Property
61-
* @param string $value
65+
* @param string|ScalarValueInterface|Param $value
6266
*/
6367
public function setValue($value) {
6468
$this->value = $value;
@@ -81,11 +85,15 @@ public function setEnableCondition($enableCondition) {
8185
* Default constructor to build the filter.
8286
* All parameters are optional and can later be set using the setters.
8387
*
88+
*
89+
* @Important $tableName
90+
* @Important $columnName
91+
* @Important $value
8492
* @param string $tableName
8593
* @param string $columnName
86-
* @param string $value
94+
* @param string|ScalarValueInterface|Param $value
8795
*/
88-
public function DifferentFilter($tableName=null, $columnName=null, $value=null) {
96+
public function __construct($tableName=null, $columnName=null, $value=null) {
8997
$this->tableName = $tableName;
9098
$this->columnName = $columnName;
9199
$this->value = $value;
@@ -106,7 +114,7 @@ public function toSql(ConnectionInterface $dbConnection) {
106114
if ($this->value === null) {
107115
$str_value = ' IS NOT NULL';
108116
} else {
109-
$str_value = "<>".$dbConnection->quoteSmart($this->value);
117+
$str_value = "<>".SqlValueUtils::toSql($this->value);
110118
}
111119

112120
return $this->tableName.'.'.$this->columnName.$str_value;

src/Mouf/Database/QueryWriter/Filters/EqualFilter.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
namespace Mouf\Database\QueryWriter\Filters;
2121

22+
use Mouf\Utils\Value\ValueUtils;
23+
24+
use Mouf\Utils\Value\ScalarValueInterface;
25+
2226
use Mouf\Database\DBConnection\ConnectionInterface;
2327

2428
/**
@@ -58,7 +62,7 @@ public function setColumnName($columnName) {
5862
* The value to compare to in the filter.
5963
*
6064
* @Property
61-
* @param string $value
65+
* @param string|ScalarValueInterface|Param $value
6266
*/
6367
public function setValue($value) {
6468
$this->value = $value;
@@ -80,11 +84,14 @@ public function setEnableCondition($enableCondition) {
8084
* Default constructor to build the filter.
8185
* All parameters are optional and can later be set using the setters.
8286
*
83-
* @param string $tableName
84-
* @param string $columnName
85-
* @param string $value
87+
* @Important $tableName
88+
* @Important $columnName
89+
* @Important $value
90+
* @param string $tableName The name of the table or alias the equals applies on
91+
* @param string $columnName The name of the column
92+
* @param string|ScalarValueInterface|Param $value The value
8693
*/
87-
public function EqualFilter($tableName=null, $columnName=null, $value=null) {
94+
public function __construct($tableName=null, $columnName=null, $value=null) {
8895
$this->tableName = $tableName;
8996
$this->columnName = $columnName;
9097
$this->value = $value;
@@ -105,7 +112,7 @@ public function toSql(ConnectionInterface $dbConnection) {
105112
if ($this->value === null) {
106113
$str_value = ' IS NULL';
107114
} else {
108-
$str_value = "=".$dbConnection->quoteSmart($this->value);
115+
$str_value = "=".SqlValueUtils::toSql($this->value);
109116
}
110117

111118
return $this->tableName.'.'.$this->columnName.$str_value;

src/Mouf/Database/QueryWriter/Filters/GreaterFilter.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
namespace Mouf\Database\QueryWriter\Filters;
2121

22+
use Mouf\Utils\Value\ValueUtils;
23+
24+
use Mouf\Utils\Value\ScalarValueInterface;
25+
2226
use Mouf\Database\DBConnection\ConnectionInterface;
2327

2428
/**
@@ -58,7 +62,7 @@ public function setColumnName($columnName) {
5862
* The value to compare to in the filter.
5963
*
6064
* @Property
61-
* @param string $value
65+
* @param string|ScalarValueInterface|Param $value
6266
*/
6367
public function setValue($value) {
6468
$this->value = $value;
@@ -80,11 +84,14 @@ public function setEnableCondition($enableCondition) {
8084
* Default constructor to build the filter.
8185
* All parameters are optional and can later be set using the setters.
8286
*
87+
* @Important $tableName
88+
* @Important $columnName
89+
* @Important $value
8390
* @param string $tableName
8491
* @param string $columnName
85-
* @param string $value
92+
* @param string|ScalarValueInterface|Param $value
8693
*/
87-
public function GreaterFilter($tableName=null, $columnName=null, $value=null) {
94+
public function __construct($tableName=null, $columnName=null, $value=null) {
8895
$this->tableName = $tableName;
8996
$this->columnName = $columnName;
9097
$this->value = $value;
@@ -106,7 +113,7 @@ public function toSql(ConnectionInterface $dbConnection) {
106113
throw new Exception("Error in GreaterFilter: trying to compare $this->tableName.$this->columnName with NULL.");
107114
}
108115

109-
return $this->tableName.'.'.$this->columnName.">".$dbConnection->quoteSmart($this->value);
116+
return $this->tableName.'.'.$this->columnName.">".SqlValueUtils::toSql($this->value);
110117
}
111118

112119
/**

src/Mouf/Database/QueryWriter/Filters/GreaterOrEqualFilter.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
namespace Mouf\Database\QueryWriter\Filters;
2121

22+
use Mouf\Utils\Value\ValueUtils;
23+
24+
use Mouf\Utils\Value\ScalarValueInterface;
25+
2226
use Mouf\Database\DBConnection\ConnectionInterface;
2327

2428
/**
@@ -58,7 +62,7 @@ public function setColumnName($columnName) {
5862
* The value to compare to in the filter.
5963
*
6064
* @Property
61-
* @param string $value
65+
* @param string|ScalarValueInterface|Param $value
6266
*/
6367
public function setValue($value) {
6468
$this->value = $value;
@@ -81,11 +85,14 @@ public function setEnableCondition($enableCondition) {
8185
* Default constructor to build the filter.
8286
* All parameters are optional and can later be set using the setters.
8387
*
88+
* @Important $tableName
89+
* @Important $columnName
90+
* @Important $value
8491
* @param string $tableName
8592
* @param string $columnName
86-
* @param string $value
93+
* @param string|ScalarValueInterface|Param $value
8794
*/
88-
public function GreaterOrEqualFilter($tableName=null, $columnName=null, $value=null) {
95+
public function __construct($tableName=null, $columnName=null, $value=null) {
8996
$this->tableName = $tableName;
9097
$this->columnName = $columnName;
9198
$this->value = $value;
@@ -107,7 +114,7 @@ public function toSql(ConnectionInterface $dbConnection) {
107114
throw new Exception("Error in GreaterOrEqualFilter: trying to compare $this->tableName.$this->columnName with NULL.");
108115
}
109116

110-
return $this->tableName.'.'.$this->columnName.">=".$dbConnection->quoteSmart($this->value);
117+
return $this->tableName.'.'.$this->columnName.">=".SqlValueUtils::toSql($this->value);
111118
}
112119

113120
/**

src/Mouf/Database/QueryWriter/Filters/InFilter.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919

2020
namespace Mouf\Database\QueryWriter\Filters;
2121

22+
use Mouf\Utils\Value\ValueUtils;
23+
24+
use Mouf\Utils\Value\ArrayValueInterface;
25+
26+
use Mouf\Utils\Value\ScalarValueInterface;
27+
2228
use Mouf\Database\DBConnection\ConnectionInterface;
2329

2430
/**
@@ -59,7 +65,7 @@ public function setColumnName($columnName) {
5965
*
6066
* @Property
6167
* @Compulsory
62-
* @param array<string> $values
68+
* @param array<string>|array<ScalarValueInterface>|ArrayValueInterface $values
6369
*/
6470
public function setValues($values) {
6571
$this->values = $values;
@@ -81,11 +87,14 @@ public function setEnableCondition($enableCondition) {
8187
* Default constructor to build the filter.
8288
* All parameters are optional and can later be set using the setters.
8389
*
90+
* @Important $tableName
91+
* @Important $columnName
92+
* @Important $values
8493
* @param string $tableName
8594
* @param string $columnName
86-
* @param array<string> $values
95+
* @param array<string>|array<ScalarValueInterface> $values
8796
*/
88-
public function InFilter($tableName=null, $columnName=null, $values=array()) {
97+
public function __construct($tableName=null, $columnName=null, $values=array()) {
8998
$this->tableName = $tableName;
9099
$this->columnName = $columnName;
91100
$this->values = $values;
@@ -109,11 +118,11 @@ public function toSql(ConnectionInterface $dbConnection) {
109118

110119
$values_sql = array();
111120

112-
foreach ($this->values as $value) {
121+
foreach (ValueUtils::val($this->values) as $value) {
113122
if ($value === null) {
114123
$values_sql[] = 'NULL';
115124
} else {
116-
$values_sql[] = $dbConnection->quoteSmart($value);
125+
$values_sql[] = $dbConnection->quoteSmart(ValueUtils::val($value));
117126
}
118127
}
119128

src/Mouf/Database/QueryWriter/Filters/LessFilter.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
namespace Mouf\Database\QueryWriter\Filters;
2121

22+
use Mouf\Utils\Value\ValueUtils;
23+
24+
use Mouf\Utils\Value\ScalarValueInterface;
25+
2226
use Mouf\Database\DBConnection\ConnectionInterface;
2327

2428
/**
@@ -58,7 +62,7 @@ public function setColumnName($columnName) {
5862
* The value to compare to in the filter.
5963
*
6064
* @Property
61-
* @param string $value
65+
* @param string|ScalarValueInterface|Param $value
6266
*/
6367
public function setValue($value) {
6468
$this->value = $value;
@@ -80,11 +84,14 @@ public function setEnableCondition($enableCondition) {
8084
* Default constructor to build the filter.
8185
* All parameters are optional and can later be set using the setters.
8286
*
87+
* @Important $tableName
88+
* @Important $columnName
89+
* @Important $value
8390
* @param string $tableName
8491
* @param string $columnName
85-
* @param string $value
92+
* @param string|ScalarValueInterface|Param $value
8693
*/
87-
public function LessFilter($tableName=null, $columnName=null, $value=null) {
94+
public function __construct($tableName=null, $columnName=null, $value=null) {
8895
$this->tableName = $tableName;
8996
$this->columnName = $columnName;
9097
$this->value = $value;
@@ -106,7 +113,7 @@ public function toSql(ConnectionInterface $dbConnection) {
106113
throw new Exception("Error in LessFilter: trying to compare $this->tableName.$this->columnName with NULL.");
107114
}
108115

109-
return $this->tableName.'.'.$this->columnName."<".$dbConnection->quoteSmart($this->value);
116+
return $this->tableName.'.'.$this->columnName."<".SqlValueUtils::toSql($this->value);
110117
}
111118

112119
/**

0 commit comments

Comments
 (0)