Skip to content

Commit cb61679

Browse files
committed
Starting working on DBAL migration
1 parent 8ae7588 commit cb61679

16 files changed

+95
-77
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"mouf/utils.common.conditioninterface": "~2.0",
1919
"mouf/utils.value.value-interface": "~1.0",
2020
"mouf/utils.common.paginable-interface": "~1.0",
21-
"mouf/utils.common.sortable-interface": "~1.0"
21+
"mouf/utils.common.sortable-interface": "~1.0",
22+
"html.widgets.evolugrid": "~3.0"
2223
},
2324
"autoload": {
2425
"psr-0": {

src/Mouf/Database/QueryWriter/CountNbResult.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php
22
namespace Mouf\Database\QueryWriter;
33

4-
use Mouf\Utils\Value\IntValueInterface;
5-
6-
use Mouf\Database\DBConnection\ConnectionInterface;
4+
use Doctrine\DBAL\Connection;
5+
use Mouf\Utils\Value\IntValueInterface;
76

87
/**
98
* A utility class that can compute the number of results returned by a query.
@@ -24,16 +23,16 @@ class CountNbResult implements IntValueInterface {
2423
/**
2524
* The connection to the database.
2625
*
27-
* @var ConnectionInterface
26+
* @var Connection
2827
*/
2928
private $connection;
3029

3130
/**
3231
* @Important $select
3332
* @param QueryResult $queryResult The query we will perform "count" upon.
34-
* @param ConnectionInterface $connection
33+
* @param Connection $connection
3534
*/
36-
public function __construct(QueryResult $queryResult, ConnectionInterface $connection) {
35+
public function __construct(QueryResult $queryResult, Connection $connection) {
3736
$this->queryResult = $queryResult;
3837
$this->connection = $connection;
3938
}
@@ -44,7 +43,7 @@ public function __construct(QueryResult $queryResult, ConnectionInterface $conne
4443
*/
4544
public function val() {
4645
$sql = "SELECT count(*) as cnt FROM (".$this->queryResult->toSql().") tmp";
47-
return $this->connection->getOne($sql);
46+
return $this->connection->fetchColumn($sql);
4847
}
4948

5049
}

src/Mouf/Database/QueryWriter/QueryResult.php

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?php
22
namespace Mouf\Database\QueryWriter;
33

4-
use Mouf\Utils\Value\ValueUtils;
4+
use Mouf\Utils\Value\ValueUtils;
55

6-
use SQLParser\Query\Select;
6+
use SQLParser\Query\Select;
77

88
use Mouf\Utils\Common\PaginableInterface;
99

1010
use Mouf\Utils\Value\ArrayValueInterface;
1111
use Mouf\Utils\Value\ValueInterface;
1212

13-
use Mouf\Database\DBConnection\ConnectionInterface;
13+
use Doctrine\DBAL\Connection;
1414
use Mouf\Utils\Common\SortableInterface;
1515
use SQLParser\Node\NodeInterface;
1616
use SQLParser\Node\ColRef;
@@ -33,7 +33,7 @@ class QueryResult implements ArrayValueInterface, PaginableInterface, SortableIn
3333
/**
3434
* The connection to the database.
3535
*
36-
* @var ConnectionInterface
36+
* @var Connection
3737
*/
3838
private $connection;
3939

@@ -50,9 +50,9 @@ class QueryResult implements ArrayValueInterface, PaginableInterface, SortableIn
5050
/**
5151
*
5252
* @param Select $select
53-
* @param ConnectionInterface $connection
53+
* @param Connection $connection
5454
*/
55-
public function __construct(Select $select, ConnectionInterface $connection) {
55+
public function __construct(Select $select, Connection $connection) {
5656
$this->select = $select;
5757
$this->connection = $connection;
5858
}
@@ -72,9 +72,26 @@ public function setParameters($parameters) {
7272
*/
7373
public function val() {
7474
$parameters = ValueUtils::val($this->parameters);
75-
$pdoStatement = $this->connection->query($this->select->toSql($parameters, $this->connection), $this->offset, $this->limit);
75+
$pdoStatement = $this->connection->query($this->select->toSql($parameters, $this->connection).$this->getFromLimitString($this->offset, $this->limit));
7676
return new ResultSet($pdoStatement);
7777
}
78+
79+
private static function getFromLimitString($from = null, $limit = null) {
80+
if ($limit !== null) {
81+
$limitInt = (int)$limit;
82+
$queryStr = " LIMIT ".$limitInt;
83+
84+
if ($from !== null) {
85+
$fromInt = (int)$from;
86+
$queryStr .= " OFFSET ".$fromInt;
87+
}
88+
return $queryStr;
89+
}
90+
else
91+
{
92+
return "";
93+
}
94+
}
7895

7996
/**
8097
* Returns the SQL for this query-result (without pagination, but with parameters accounted for)
@@ -85,17 +102,18 @@ public function toSql() {
85102
return $this->select->toSql($parameters, $this->connection);
86103
}
87104

88-
/**
89-
* Paginates the result set.
90-
*
91-
* @param int $limit
92-
* @param int $offset
105+
/**
106+
* Paginates the result set.
107+
*
108+
* @param int $limit
109+
* @param int $offset
93110
*/
94111
public function paginate($limit, $offset = 0) {
95112
$this->limit = $limit;
96113
$this->offset = $offset;
97114
}
98-
115+
116+
99117
/* (non-PHPdoc)
100118
* @see \Mouf\Utils\Common\SortableInterface::sort()
101119
*/
@@ -139,5 +157,5 @@ private function findColumnByKey($key) {
139157
}
140158
}
141159
return null;
142-
}
160+
}
143161
}
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
namespace Mouf\Database\QueryWriter;
33

4-
use Mouf\Utils\Value\ArrayValueInterface;
4+
use Mouf\Utils\Value\ArrayValueInterface;
55

6-
use Mouf\Database\DBConnection\ConnectionInterface;
6+
use Doctrine\DBAL\Connection;
77

88
/**
99
* Wraps the results of a PDOStatement.
@@ -28,27 +28,27 @@ public function __construct(\PDOStatement $statement, $castToClass = "") {
2828
$this->castToClass = $castToClass;
2929
}
3030

31-
31+
3232
function rewind() {
3333
$this->rewindCalls++;
3434
if ($this->rewindCalls == 2) {
3535
throw new \Exception("Error: rewind is not possible in a database rowset. You can call 'foreach' on the rowset only once. Use CachedResultSet to be able to call the result several times. TODO: develop CachedResultSet");
36-
}
37-
}
38-
36+
}
37+
}
38+
3939
function current() {
4040

4141
if (!$this->fetched) {
4242
$this->fetch();
4343
}
4444

45-
return $this->result;
46-
}
47-
48-
function key() {
49-
return $this->key;
50-
}
51-
45+
return $this->result;
46+
}
47+
48+
function key() {
49+
return $this->key;
50+
}
51+
5252
function next() {
5353
++$this->key;
5454
$this->fetched = false;
@@ -58,14 +58,14 @@ function next() {
5858
private function fetch() {
5959
$this->result = $this->statement->fetch(\PDO::FETCH_ASSOC);
6060
$this->fetched = true;
61-
}
62-
61+
}
62+
6363
function valid() {
6464

65-
if (!$this->fetched) {
66-
$this->fetch();
67-
}
65+
if (!$this->fetched) {
66+
$this->fetch();
67+
}
6868

69-
return $this->result !== false;
69+
return $this->result !== false;
7070
}
7171
}

src/SQLParser/Node/AbstractManyInstancesOperator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace SQLParser\Node;
33

4-
use Mouf\Database\DBConnection\ConnectionInterface;
4+
use Doctrine\DBAL\Connection;
55

66
use Mouf\MoufManager;
77
use Mouf\MoufInstanceDescriptor;
@@ -49,13 +49,13 @@ public function toInstanceDescriptor(MoufManager $moufManager) {
4949
/**
5050
* Renders the object as a SQL string
5151
*
52-
* @param ConnectionInterface $dbConnection
52+
* @param Connection $dbConnection
5353
* @param array $parameters
5454
* @param number $indent
5555
* @param bool $ignoreConditions
5656
* @return string
5757
*/
58-
public function toSql(array $parameters = array(), ConnectionInterface $dbConnection = null, $indent = 0, $ignoreConditions = false) {
58+
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $ignoreConditions = false) {
5959
$sqlOperands = array();
6060
foreach ($this->operands as $operand) {
6161
$sql = NodeFactory::toSql($operand, $dbConnection, $parameters, ' ', true, $indent, $ignoreConditions);

src/SQLParser/Node/AbstractTwoOperandsOperator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use Mouf\Utils\Common\ConditionInterface\ConditionTrait;
55

6-
use Mouf\Database\DBConnection\ConnectionInterface;
6+
use Doctrine\DBAL\Connection;
77

88
use Mouf\MoufManager;
99

@@ -81,13 +81,13 @@ public function toInstanceDescriptor(MoufManager $moufManager) {
8181
/**
8282
* Renders the object as a SQL string
8383
*
84-
* @param ConnectionInterface $dbConnection
84+
* @param Connection $dbConnection
8585
* @param array $parameters
8686
* @param number $indent
8787
* @param bool $ignoreConditions
8888
* @return string
8989
*/
90-
public function toSql(array $parameters = array(), ConnectionInterface $dbConnection = null, $indent = 0, $ignoreConditions = false) {
90+
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $ignoreConditions = false) {
9191
if ($ignoreConditions || !$this->condition || $this->condition->isOk($parameters)) {
9292
$sql = NodeFactory::toSql($this->leftOperand, $dbConnection, $parameters, ' ', false, $indent, $ignoreConditions);
9393
$sql .= ' '.$this->getOperatorSymbol().' ';

src/SQLParser/Node/AggregateFunction.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
namespace SQLParser\Node;
3434

35-
use Mouf\Database\DBConnection\ConnectionInterface;
35+
use Doctrine\DBAL\Connection;
3636

3737
use Mouf\MoufManager;
3838

@@ -114,13 +114,13 @@ public function toInstanceDescriptor(MoufManager $moufManager) {
114114
/**
115115
* Renders the object as a SQL string
116116
*
117-
* @param ConnectionInterface $dbConnection
117+
* @param Connection $dbConnection
118118
* @param array $parameters
119119
* @param number $indent
120120
* @param bool $ignoreConditions
121121
* @return string
122122
*/
123-
public function toSql(array $parameters = array(), ConnectionInterface $dbConnection = null, $indent = 0, $ignoreConditions = false) {
123+
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $ignoreConditions = false) {
124124
$subTreeSql = NodeFactory::toSql($this->subTree, $dbConnection, $parameters, ' ', false, $indent, $ignoreConditions);
125125
if ($subTreeSql !== null) {
126126
$sql = $this->functionName.'(';

src/SQLParser/Node/ColRef.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
namespace SQLParser\Node;
3434

35-
use Mouf\Database\DBConnection\ConnectionInterface;
35+
use Doctrine\DBAL\Connection;
3636

3737
use Mouf\MoufManager;
3838

@@ -147,13 +147,13 @@ public function toInstanceDescriptor(MoufManager $moufManager) {
147147
/**
148148
* Renders the object as a SQL string
149149
*
150-
* @param ConnectionInterface $dbConnection
150+
* @param Connection $dbConnection
151151
* @param array $parameters
152152
* @param number $indent
153153
* @param bool $ignoreConditions
154154
* @return string
155155
*/
156-
public function toSql(array $parameters = array(), ConnectionInterface $dbConnection = null, $indent = 0, $ignoreConditions = false) {
156+
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $ignoreConditions = false) {
157157
$sql = '';
158158
if ($this->table) {
159159
$sql .= NodeFactory::escapeDBItem($this->table, $dbConnection).'.';

src/SQLParser/Node/ConstNode.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
namespace SQLParser\Node;
3434

35-
use Mouf\Database\DBConnection\ConnectionInterface;
35+
use Doctrine\DBAL\Connection;
3636

3737
use Mouf\MoufManager;
3838

@@ -76,13 +76,13 @@ public function toInstanceDescriptor(MoufManager $moufManager) {
7676
/**
7777
* Renders the object as a SQL string
7878
*
79-
* @param ConnectionInterface $dbConnection
79+
* @param Connection $dbConnection
8080
* @param array $parameters
8181
* @param number $indent
8282
* @param bool $ignoreConditions
8383
* @return string
8484
*/
85-
public function toSql(array $parameters = array(), ConnectionInterface $dbConnection = null, $indent = 0, $ignoreConditions = false) {
85+
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $ignoreConditions = false) {
8686
if ($this->value === null) {
8787
return 'NULL';
8888
} elseif ($dbConnection != null) {

src/SQLParser/Node/Expression.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
namespace SQLParser\Node;
3434

35-
use Mouf\Database\DBConnection\ConnectionInterface;
35+
use Doctrine\DBAL\Connection;
3636

3737
use Mouf\MoufInstanceDescriptor;
3838

@@ -153,13 +153,13 @@ public function toInstanceDescriptor(MoufManager $moufManager) {
153153
/**
154154
* Renders the object as a SQL string
155155
*
156-
* @param ConnectionInterface $dbConnection
156+
* @param Connection $dbConnection
157157
* @param array $parameters
158158
* @param number $indent
159159
* @param bool $ignoreConditions
160160
* @return string
161161
*/
162-
public function toSql(array $parameters = array(), ConnectionInterface $dbConnection = null, $indent = 0, $ignoreConditions = false) {
162+
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $ignoreConditions = false) {
163163
$sql = NodeFactory::toSql($this->subTree, $dbConnection, $parameters, ' ', false, $indent, $ignoreConditions);
164164
if ($this->alias) {
165165
$sql .= " AS ".$this->alias;

0 commit comments

Comments
 (0)