|
2 | 2 |
|
3 | 3 | namespace Mouf\Database;
|
4 | 4 |
|
| 5 | +use function array_filter; |
| 6 | +use function array_keys; |
5 | 7 | use Doctrine\Common\Cache\VoidCache;
|
6 | 8 | use function hash;
|
| 9 | +use function implode; |
7 | 10 | use Mouf\Database\MagicQuery\Twig\SqlTwigEnvironmentFactory;
|
8 | 11 | use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
|
9 | 12 | use PHPSQLParser\PHPSQLParser;
|
@@ -85,13 +88,36 @@ public function build($sql, array $parameters = array())
|
85 | 88 | $sql = $this->getTwigEnvironment()->render($sql, $parameters);
|
86 | 89 | }
|
87 | 90 |
|
| 91 | + $select = $this->parse($sql); |
| 92 | + $newSql = $this->toSql($select, $parameters, true); |
| 93 | + |
| 94 | + return $newSql; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Returns modified SQL from $sql and $parameters. Any parameters not available will be striped down |
| 99 | + * from the SQL. Unlike with the `build` method, the parameters are NOT merged into the SQL. |
| 100 | + * This method is more efficient than `build` (because result is cached and statements interpolation |
| 101 | + * can be delegated to the database. |
| 102 | + * |
| 103 | + * @param string $sql |
| 104 | + * @param array $parameters |
| 105 | + * |
| 106 | + * @return string |
| 107 | + */ |
| 108 | + public function buildPreparedStatement(string $sql, array $parameters = []): string |
| 109 | + { |
| 110 | + if ($this->enableTwig) { |
| 111 | + $sql = $this->getTwigEnvironment()->render($sql, $parameters); |
| 112 | + } |
| 113 | + |
88 | 114 | $availableParameterKeys = array_keys(array_filter($parameters, static function($param) { return $param !== null;}));
|
89 | 115 | // We choose md4 because it is fast.
|
90 | 116 | $cacheKey = 'request_build_'.hash('md4', $sql.'__'.implode('_/_', $availableParameterKeys));
|
91 | 117 | $newSql = $this->cache->fetch($cacheKey);
|
92 | 118 | if ($newSql === false) {
|
93 | 119 | $select = $this->parse($sql);
|
94 |
| - $newSql = $this->toSql($select, $parameters); |
| 120 | + $newSql = $this->toSql($select, $parameters, false); |
95 | 121 |
|
96 | 122 | $this->cache->save($cacheKey, $newSql);
|
97 | 123 | }
|
@@ -140,13 +166,14 @@ public function parse($sql)
|
140 | 166 | * Transforms back a tree of SQL node into a SQL string.
|
141 | 167 | *
|
142 | 168 | * @param NodeInterface $sqlNode
|
143 |
| - * @param array $parameters |
144 |
| - * |
| 169 | + * @param array $parameters |
| 170 | + * @param bool $extrapolateParameters Whether the parameters should be fed into the returned SQL query |
| 171 | +
|
145 | 172 | * @return string
|
146 | 173 | */
|
147 |
| - public function toSql(NodeInterface $sqlNode, array $parameters = array()) |
| 174 | + public function toSql(NodeInterface $sqlNode, array $parameters = array(), bool $extrapolateParameters = true) |
148 | 175 | {
|
149 |
| - return $sqlNode->toSql($parameters, $this->connection, 0, SqlRenderInterface::CONDITION_GUESS); |
| 176 | + return $sqlNode->toSql($parameters, $this->connection, 0, SqlRenderInterface::CONDITION_GUESS, $extrapolateParameters); |
150 | 177 | }
|
151 | 178 |
|
152 | 179 | /**
|
|
0 commit comments