File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -108,6 +108,20 @@ public function build()
108
108
*/
109
109
$ query = '' ;
110
110
111
+ /**
112
+ * Clauses which were built already.
113
+ *
114
+ * It is required to keep track of built clauses because some fields,
115
+ * for example `join` is used by multiple clauses (`JOIN`, `LEFT JOIN`,
116
+ * `LEFT OUTER JOIN`, etc.). The same happens for `VALUE` and `VALUES`.
117
+ *
118
+ * A clause is considered built just after fields' value
119
+ * (`$this->field`) was used in building.
120
+ *
121
+ * @var array
122
+ */
123
+ $ built = array ();
124
+
111
125
foreach (static ::$ CLAUSES as $ clause ) {
112
126
/**
113
127
* The name of the clause.
@@ -144,6 +158,15 @@ public function build()
144
158
continue ;
145
159
}
146
160
161
+ // Checking if this field was already built.
162
+ if ($ type & 1 ) {
163
+ if (!empty ($ built [$ field ])) {
164
+ continue ;
165
+ }
166
+
167
+ $ built [$ field ] = true ;
168
+ }
169
+
147
170
// Checking if the name of the clause should be added.
148
171
if ($ type & 2 ) {
149
172
$ query .= $ name . ' ' ;
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace SqlParser \Tests \Builder ;
4
+
5
+ use SqlParser \Parser ;
6
+
7
+ use SqlParser \Tests \TestCase ;
8
+
9
+ class SelectStatementTest extends TestCase
10
+ {
11
+
12
+ public function testBuilder ()
13
+ {
14
+ $ query = 'SELECT * FROM t1 LEFT JOIN (t2, t3, t4) '
15
+ . 'ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c) ' ;
16
+
17
+ $ parser = new Parser ($ query );
18
+ $ stmt = $ parser ->statements [0 ];
19
+
20
+ $ this ->assertEquals (
21
+ 'SELECT * FROM t1 LEFT JOIN (t2, t3, t4) '
22
+ . 'ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c) ' ,
23
+ $ stmt ->build ()
24
+ );
25
+ }
26
+ }
You can’t perform that action at this time.
0 commit comments