@@ -34,9 +34,9 @@ class MagicQuery
34
34
private $ enableTwig = false ;
35
35
36
36
/**
37
- * @param \Doctrine\DBAL\Connection $connection
37
+ * @param \Doctrine\DBAL\Connection $connection
38
38
* @param \Doctrine\Common\Cache\Cache $cache
39
- * @param SchemaAnalyzer $schemaAnalyzer (optional). If not set, it is initialized from the connection.
39
+ * @param SchemaAnalyzer $schemaAnalyzer (optional). If not set, it is initialized from the connection.
40
40
*/
41
41
public function __construct ($ connection = null , $ cache = null , SchemaAnalyzer $ schemaAnalyzer = null )
42
42
{
@@ -54,11 +54,15 @@ public function __construct($connection = null, $cache = null, SchemaAnalyzer $s
54
54
/**
55
55
* Whether Twig parsing should be enabled or not.
56
56
* Defaults to false.
57
+ *
57
58
* @param bool $enableTwig
59
+ *
58
60
* @return $this
59
61
*/
60
- public function setEnableTwig ($ enableTwig = true ) {
62
+ public function setEnableTwig ($ enableTwig = true )
63
+ {
61
64
$ this ->enableTwig = $ enableTwig ;
65
+
62
66
return $ this ;
63
67
}
64
68
@@ -79,6 +83,7 @@ public function build($sql, array $parameters = array())
79
83
$ sql = $ this ->getTwigEnvironment ()->render ($ sql , $ parameters );
80
84
}
81
85
$ select = $ this ->parse ($ sql );
86
+
82
87
return $ this ->toSql ($ select , $ parameters );
83
88
}
84
89
@@ -87,13 +92,16 @@ public function build($sql, array $parameters = array())
87
92
* This tree representation can be used to manipulate the SQL.
88
93
*
89
94
* @param string $sql
95
+ *
90
96
* @return NodeInterface
97
+ *
91
98
* @throws MagicQueryMissingConnectionException
92
99
* @throws MagicQueryParserException
93
100
*/
94
- public function parse ($ sql ) {
101
+ public function parse ($ sql )
102
+ {
95
103
// We choose md4 because it is fast.
96
- $ cacheKey = " request_ " .hash (" md4 " , $ sql );
104
+ $ cacheKey = ' request_ ' .hash (' md4 ' , $ sql );
97
105
$ select = $ this ->cache ->fetch ($ cacheKey );
98
106
99
107
if ($ select === false ) {
@@ -111,27 +119,32 @@ public function parse($sql) {
111
119
// Let's store the tree
112
120
$ this ->cache ->save ($ cacheKey , $ select );
113
121
}
122
+
114
123
return $ select ;
115
124
}
116
125
117
126
/**
118
127
* Transforms back a tree of SQL node into a SQL string.
119
128
*
120
129
* @param NodeInterface $sqlNode
121
- * @param array $parameters
130
+ * @param array $parameters
131
+ *
122
132
* @return string
123
133
*/
124
- public function toSql (NodeInterface $ sqlNode , array $ parameters = array ()) {
134
+ public function toSql (NodeInterface $ sqlNode , array $ parameters = array ())
135
+ {
125
136
return $ sqlNode ->toSql ($ parameters , $ this ->connection , 0 , SqlRenderInterface::CONDITION_GUESS );
126
137
}
127
138
128
139
/**
129
140
* Scans the SQL statement and replaces the "magicjoin" part with the correct joins.
130
141
*
131
142
* @param NodeInterface $select
143
+ *
132
144
* @throws MagicQueryMissingConnectionException
133
145
*/
134
- private function magicJoin (NodeInterface $ select ) {
146
+ private function magicJoin (NodeInterface $ select )
147
+ {
135
148
// Let's find if this is a MagicJoin query.
136
149
$ magicJoinDetector = new DetectMagicJoinSelectVisitor ();
137
150
$ nodeTraverser = new NodeTraverser ();
@@ -147,11 +160,14 @@ private function magicJoin(NodeInterface $select) {
147
160
}
148
161
149
162
/**
150
- * For one given MagicJoin select, let's apply MagicJoin
163
+ * For one given MagicJoin select, let's apply MagicJoin.
164
+ *
151
165
* @param MagicJoinSelect $magicJoinSelect
166
+ *
152
167
* @return Select
153
168
*/
154
- private function magicJoinOnOneQuery (MagicJoinSelect $ magicJoinSelect ) {
169
+ private function magicJoinOnOneQuery (MagicJoinSelect $ magicJoinSelect )
170
+ {
155
171
$ tableSearchNodeTraverser = new NodeTraverser ();
156
172
$ detectTableVisitor = new DetectTablesVisitor ($ magicJoinSelect ->getMainTable ());
157
173
$ tableSearchNodeTraverser ->addVisitor ($ detectTableVisitor );
@@ -183,7 +199,7 @@ private function magicJoinOnOneQuery(MagicJoinSelect $magicJoinSelect) {
183
199
$ tableNode = new Table ();
184
200
$ tableNode ->setTable ($ mainTable );
185
201
$ tables = [
186
- $ mainTable => $ tableNode
202
+ $ mainTable => $ tableNode,
187
203
];
188
204
189
205
foreach ($ completePath as $ foreignKey ) {
@@ -202,7 +218,7 @@ private function magicJoinOnOneQuery(MagicJoinSelect $magicJoinSelect) {
202
218
$ onNode ->setRightOperand ($ rightCol );
203
219
204
220
$ tableNode = new Table ();
205
- $ tableNode ->setJoinType (" LEFT JOIN " );
221
+ $ tableNode ->setJoinType (' LEFT JOIN ' );
206
222
$ tableNode ->setRefClause ($ onNode );
207
223
208
224
if (isset ($ tables [$ foreignKey ->getLocalTableName ()])) {
@@ -215,34 +231,38 @@ private function magicJoinOnOneQuery(MagicJoinSelect $magicJoinSelect) {
215
231
}
216
232
217
233
$ select ->setFrom ($ tables );
218
-
219
234
}
220
235
221
236
/**
222
237
* @return SchemaAnalyzer
223
238
*/
224
- private function getSchemaAnalyzer () {
239
+ private function getSchemaAnalyzer ()
240
+ {
225
241
if ($ this ->schemaAnalyzer === null ) {
226
242
if (!$ this ->connection ) {
227
243
throw new MagicQueryMissingConnectionException ('In order to use MagicJoin, you need to configure a DBAL connection. ' );
228
244
}
229
245
230
246
$ this ->schemaAnalyzer = new SchemaAnalyzer ($ this ->connection ->getSchemaManager (), $ this ->cache , $ this ->getConnectionUniqueId ());
231
247
}
248
+
232
249
return $ this ->schemaAnalyzer ;
233
250
}
234
251
235
- private function getConnectionUniqueId () {
236
- return hash ('md4 ' , $ this ->connection ->getHost ()."- " .$ this ->connection ->getPort ()."- " .$ this ->connection ->getDatabase ()."- " .$ this ->connection ->getDriver ()->getName ());
252
+ private function getConnectionUniqueId ()
253
+ {
254
+ return hash ('md4 ' , $ this ->connection ->getHost ().'- ' .$ this ->connection ->getPort ().'- ' .$ this ->connection ->getDatabase ().'- ' .$ this ->connection ->getDriver ()->getName ());
237
255
}
238
256
239
257
/**
240
258
* @return \Twig_Environment
241
259
*/
242
- private function getTwigEnvironment () {
260
+ private function getTwigEnvironment ()
261
+ {
243
262
if ($ this ->twigEnvironment === null ) {
244
263
$ this ->twigEnvironment = SqlTwigEnvironmentFactory::getTwigEnvironment ();
245
264
}
265
+
246
266
return $ this ->twigEnvironment ;
247
267
}
248
268
}
0 commit comments