Skip to content

Commit 253bb65

Browse files
committed
Merge pull request #4 from moufmouf/4.0
Migrating to DBAL
2 parents 4dd2be7 + f2fa5c2 commit 253bb65

18 files changed

+128
-85
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
],
1515
"require": {
1616
"php": ">=5.3.0",
17-
"mouf/database.dbconnection": "~2.0",
17+
"mouf/database.doctrine-dbal-wrapper": "~1.1",
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+
"mouf/html.widgets.evolugrid": "~3.0"
2223
},
2324
"autoload": {
2425
"psr-0": {

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

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

4+
use Mouf\ClassProxy;
45
use Mouf\Database\QueryWriter\Utils\FindParametersService;
56

67
use Mouf\MoufPropertyDescriptor;
@@ -196,9 +197,9 @@ public function runQuery($name, $parameters = array(), $offset = null, $limit =
196197
$sql = $select->toSql($parameters);
197198

198199
// TODO: point to the right dbConnection
199-
$dbConnection = new InstanceProxy("dbConnection");
200-
$results = $dbConnection->getAll($sql, \PDO::FETCH_ASSOC, "stdClass", $offset, $limit);
201-
200+
$dbHelper = new ClassProxy("Mouf\\Database\\QueryWriter\\Utils\\DbHelper");
201+
$results = $dbHelper->getAll($sql, $offset, $limit);
202+
202203
$evolugridResultSet = new EvoluGridResultSet();
203204
$evolugridResultSet->setResults($results);
204205

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: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
<?php
22
namespace Mouf\Database\QueryWriter;
33

4-
use Mouf\Utils\Value\ValueUtils;
4+
use Mouf\Database\QueryWriter\Utils\DbHelper;
5+
use Mouf\Utils\Value\ValueUtils;
56

6-
use SQLParser\Query\Select;
7+
use SQLParser\Query\Select;
78

89
use Mouf\Utils\Common\PaginableInterface;
910

1011
use Mouf\Utils\Value\ArrayValueInterface;
1112
use Mouf\Utils\Value\ValueInterface;
1213

13-
use Mouf\Database\DBConnection\ConnectionInterface;
14+
use Doctrine\DBAL\Connection;
1415
use Mouf\Utils\Common\SortableInterface;
1516
use SQLParser\Node\NodeInterface;
1617
use SQLParser\Node\ColRef;
@@ -33,7 +34,7 @@ class QueryResult implements ArrayValueInterface, PaginableInterface, SortableIn
3334
/**
3435
* The connection to the database.
3536
*
36-
* @var ConnectionInterface
37+
* @var Connection
3738
*/
3839
private $connection;
3940

@@ -50,9 +51,9 @@ class QueryResult implements ArrayValueInterface, PaginableInterface, SortableIn
5051
/**
5152
*
5253
* @param Select $select
53-
* @param ConnectionInterface $connection
54+
* @param Connection $connection
5455
*/
55-
public function __construct(Select $select, ConnectionInterface $connection) {
56+
public function __construct(Select $select, Connection $connection) {
5657
$this->select = $select;
5758
$this->connection = $connection;
5859
}
@@ -72,10 +73,10 @@ public function setParameters($parameters) {
7273
*/
7374
public function val() {
7475
$parameters = ValueUtils::val($this->parameters);
75-
$pdoStatement = $this->connection->query($this->select->toSql($parameters, $this->connection), $this->offset, $this->limit);
76+
$pdoStatement = $this->connection->query($this->select->toSql($parameters, $this->connection).DbHelper::getFromLimitString($this->offset, $this->limit));
7677
return new ResultSet($pdoStatement);
7778
}
78-
79+
7980
/**
8081
* Returns the SQL for this query-result (without pagination, but with parameters accounted for)
8182
* @return string
@@ -85,17 +86,18 @@ public function toSql() {
8586
return $this->select->toSql($parameters, $this->connection);
8687
}
8788

88-
/**
89-
* Paginates the result set.
90-
*
91-
* @param int $limit
92-
* @param int $offset
89+
/**
90+
* Paginates the result set.
91+
*
92+
* @param int $limit
93+
* @param int $offset
9394
*/
9495
public function paginate($limit, $offset = 0) {
9596
$this->limit = $limit;
9697
$this->offset = $offset;
9798
}
98-
99+
100+
99101
/* (non-PHPdoc)
100102
* @see \Mouf\Utils\Common\SortableInterface::sort()
101103
*/
@@ -139,5 +141,5 @@ private function findColumnByKey($key) {
139141
}
140142
}
141143
return null;
142-
}
144+
}
143145
}
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
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
namespace Mouf\Database\QueryWriter\Utils;
3+
4+
5+
use Mouf\MoufManager;
6+
7+
class DbHelper
8+
{
9+
public static function getAll($sql, $offset, $limit) {
10+
$dbalConnection = MoufManager::getMoufManager()->get('dbalConnection');
11+
/* @var $dbalConnection \Doctrine\DBAL\Connection */
12+
$sql .= self::getFromLimitString($offset, $limit);
13+
$statement = $dbalConnection->executeQuery($sql);
14+
$results = $statement->fetchAll();
15+
16+
$array = [];
17+
18+
foreach ($results as $result) {
19+
$array[] = $result;
20+
}
21+
return $array;
22+
}
23+
24+
public static function getFromLimitString($from = null, $limit = null) {
25+
if ($limit !== null) {
26+
$limitInt = (int)$limit;
27+
$queryStr = " LIMIT ".$limitInt;
28+
29+
if ($from !== null) {
30+
$fromInt = (int)$from;
31+
$queryStr .= " OFFSET ".$fromInt;
32+
}
33+
return $queryStr;
34+
}
35+
else
36+
{
37+
return "";
38+
}
39+
}
40+
}

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).'.';

0 commit comments

Comments
 (0)