Skip to content

Commit f2fa5c2

Browse files
committed
Migrating to DBAL
1 parent cbce00f commit f2fa5c2

File tree

6 files changed

+49
-24
lines changed

6 files changed

+49
-24
lines changed

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/QueryResult.php

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

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

67
use SQLParser\Query\Select;
@@ -72,27 +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->getFromLimitString($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
}
7879

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-
}
95-
9680
/**
9781
* Returns the SQL for this query-result (without pagination, but with parameters accounted for)
9882
* @return string
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/ConstNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function toSql(array $parameters = array(), Connection $dbConnection = nu
8686
if ($this->value === null) {
8787
return 'NULL';
8888
} elseif ($dbConnection != null) {
89-
return $dbConnection->quoteSmart($this->value);
89+
return $dbConnection->quote($this->value);
9090
} else {
9191
return "'".addslashes($this->value)."'";
9292
}

src/SQLParser/Node/NodeFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ public static function toSql($nodes, Connection $dbConnection = null, array $par
647647
*/
648648
public static function escapeDBItem($str, Connection $dbConnection = null) {
649649
if ($dbConnection) {
650-
return $dbConnection->escapeDBItem($str);
650+
return $dbConnection->quoteIdentifier($str);
651651
} else {
652652
return '`'.$str.'`';
653653
}

src/SQLParser/Node/Parameter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function toInstanceDescriptor(MoufManager $moufManager) {
143143
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $ignoreConditions = false) {
144144
if (isset($parameters[$this->name])) {
145145
if ($dbConnection) {
146-
return $dbConnection->quoteSmart($this->autoPrepend.$parameters[$this->name].$this->autoAppend);
146+
return $dbConnection->quote($this->autoPrepend.$parameters[$this->name].$this->autoAppend);
147147
} else {
148148
if ($parameters[$this->name] === null) {
149149
return NULL;

0 commit comments

Comments
 (0)