|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* You may not change or alter any portion of this comment or credits |
|
4
|
|
|
* of supporting developers from this source code or any supporting source code |
|
5
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
|
6
|
|
|
* |
|
7
|
|
|
* This program is distributed in the hope that it will be useful, |
|
8
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Xoops\Core\Database; |
|
13
|
|
|
|
|
14
|
|
|
use Xoops\Core\Database\Connection; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Connection wrapper for Doctrine DBAL Connection |
|
18
|
|
|
* |
|
19
|
|
|
* PHP version 5.3 |
|
20
|
|
|
* |
|
21
|
|
|
* @category Xoops\Core\Database\QueryBuilder |
|
22
|
|
|
* @package QueryBuilder |
|
23
|
|
|
* @author readheadedrod <[email protected]> |
|
24
|
|
|
* @author Richard Griffith <[email protected]> |
|
25
|
|
|
* @copyright 2013-2015 XOOPS Project (http://xoops.org) |
|
26
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
|
27
|
|
|
* @version Release: 2.6.0 |
|
28
|
|
|
* @link http://xoops.org |
|
29
|
|
|
* @since 2.6.0 |
|
30
|
|
|
*/ |
|
31
|
|
|
class QueryBuilder extends \Doctrine\DBAL\Query\QueryBuilder |
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Connection DBAL Connection |
|
36
|
|
|
*/ |
|
37
|
|
|
private $connection = null; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* __construct |
|
41
|
|
|
*/ |
|
42
|
90 |
|
public function __construct(Connection $connection) |
|
43
|
|
|
{ |
|
44
|
90 |
|
$this->connection = $connection; |
|
45
|
90 |
|
parent::__construct($connection); |
|
46
|
90 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Turns the query being built into a bulk delete query that ranges over |
|
50
|
|
|
* a certain table. |
|
51
|
|
|
* |
|
52
|
|
|
* <code> |
|
53
|
|
|
* $qb = $conn->createQueryBuilder() |
|
54
|
|
|
* ->delete('users', 'u') |
|
55
|
|
|
* ->where('u.id = :user_id'); |
|
56
|
|
|
* ->setParameter(':user_id', 1); |
|
57
|
|
|
* </code> |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $delete The table whose rows are subject to the deletion. |
|
60
|
|
|
* Adds table prefix to table. |
|
61
|
|
|
* @param string $alias The table alias used in the constructed query. |
|
62
|
|
|
* |
|
63
|
|
|
* @return QueryBuilder This QueryBuilder instance. |
|
64
|
|
|
*/ |
|
65
|
2 |
|
public function deletePrefix($delete = null, $alias = null) |
|
66
|
|
|
{ |
|
67
|
2 |
|
$delete = $this->connection->prefix($delete); |
|
68
|
2 |
|
return $this->delete($delete, $alias); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Turns the query being built into a bulk update query that ranges over |
|
73
|
|
|
* a certain table |
|
74
|
|
|
* |
|
75
|
|
|
* <code> |
|
76
|
|
|
* $qb = $conn->createQueryBuilder() |
|
77
|
|
|
* ->update('users', 'u') |
|
78
|
|
|
* ->set('u.password', md5('password')) |
|
79
|
|
|
* ->where('u.id = ?'); |
|
80
|
|
|
* </code> |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $update The table whose rows are subject to the update. |
|
83
|
|
|
* Adds table prefix to table. |
|
84
|
|
|
* @param string $alias The table alias used in the constructed query. |
|
85
|
|
|
* |
|
86
|
|
|
* @return QueryBuilder This QueryBuilder instance. |
|
87
|
|
|
*/ |
|
88
|
|
|
public function updatePrefix($update = null, $alias = null) |
|
89
|
|
|
{ |
|
90
|
|
|
$update = $this->connection->prefix($update); |
|
91
|
|
|
return $this->update($update, $alias); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Turns the query being built into an insert query that inserts into |
|
96
|
|
|
* a certain table |
|
97
|
|
|
* |
|
98
|
|
|
* <code> |
|
99
|
|
|
* $qb = $conn->createQueryBuilder() |
|
100
|
|
|
* ->insert('users') |
|
101
|
|
|
* ->values( |
|
102
|
|
|
* array( |
|
103
|
|
|
* 'name' => '?', |
|
104
|
|
|
* 'password' => '?' |
|
105
|
|
|
* ) |
|
106
|
|
|
* ); |
|
107
|
|
|
* </code> |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $insert The table into which the rows should be inserted. |
|
110
|
|
|
* Adds table prefix to table. |
|
111
|
|
|
* |
|
112
|
|
|
* @return QueryBuilder This QueryBuilder instance. |
|
113
|
|
|
*/ |
|
114
|
|
|
public function insertPrefix($insert = null) |
|
115
|
|
|
{ |
|
116
|
|
|
$insert = $this->connection->prefix($insert); |
|
117
|
|
|
return $this->insert($insert); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Create and add a query root corresponding to the table identified by the |
|
122
|
|
|
* given alias, forming a cartesian product with any existing query roots. |
|
123
|
|
|
* |
|
124
|
|
|
* <code> |
|
125
|
|
|
* $qb = $conn->createQueryBuilder() |
|
126
|
|
|
* ->select('u.id') |
|
127
|
|
|
* ->from('users', 'u') |
|
128
|
|
|
* </code> |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $from The table. Adds table prefix to table. |
|
131
|
|
|
* @param string|null $alias The alias of the table. |
|
132
|
|
|
* |
|
133
|
|
|
* @return QueryBuilder This QueryBuilder instance. |
|
134
|
|
|
*/ |
|
135
|
31 |
|
public function fromPrefix($from, $alias = null) |
|
136
|
|
|
{ |
|
137
|
31 |
|
$from = $this->connection->prefix($from); |
|
138
|
31 |
|
return $this->from($from, $alias); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Creates and adds a join to the query. |
|
143
|
|
|
* |
|
144
|
|
|
* <code> |
|
145
|
|
|
* $qb = $conn->createQueryBuilder() |
|
146
|
|
|
* ->select('u.name') |
|
147
|
|
|
* ->from('users', 'u') |
|
148
|
|
|
* ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
149
|
|
|
* </code> |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $fromAlias The alias that points to a from clause |
|
152
|
|
|
* @param string $join The table name to join. Adds table prefix to table. |
|
153
|
|
|
* @param string $alias The alias of the join table |
|
154
|
|
|
* @param string $condition The condition for the join |
|
155
|
|
|
* |
|
156
|
|
|
* @return QueryBuilder This QueryBuilder instance. |
|
157
|
|
|
*/ |
|
158
|
|
|
public function joinPrefix($fromAlias, $join, $alias, $condition = null) |
|
159
|
|
|
{ |
|
160
|
|
|
$join = $this->connection->prefix($join); |
|
161
|
|
|
return $this->join($fromAlias, $join, $alias, $condition); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Creates and adds a join to the query. |
|
167
|
|
|
* |
|
168
|
|
|
* <code> |
|
169
|
|
|
* $qb = $conn->createQueryBuilder() |
|
170
|
|
|
* ->select('u.name') |
|
171
|
|
|
* ->from('users', 'u') |
|
172
|
|
|
* ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
173
|
|
|
* </code> |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $fromAlias The alias that points to a from clause |
|
176
|
|
|
* @param string $join The table name to join. Adds table prefix to table. |
|
177
|
|
|
* @param string $alias The alias of the join table |
|
178
|
|
|
* @param string $condition The condition for the join |
|
179
|
|
|
* |
|
180
|
|
|
* @return QueryBuilder This QueryBuilder instance. |
|
181
|
|
|
*/ |
|
182
|
|
|
public function innerJoinPrefix($fromAlias, $join, $alias, $condition = null) |
|
183
|
|
|
{ |
|
184
|
|
|
$join = $this->connection->prefix($join); |
|
185
|
|
|
return $this->innerJoin($fromAlias, $join, $alias, $condition); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Creates and adds a left join to the query. |
|
190
|
|
|
* |
|
191
|
|
|
* <code> |
|
192
|
|
|
* $qb = $conn->createQueryBuilder() |
|
193
|
|
|
* ->select('u.name') |
|
194
|
|
|
* ->from('users', 'u') |
|
195
|
|
|
* ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
196
|
|
|
* </code> |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $fromAlias The alias that points to a from clause |
|
199
|
|
|
* @param string $join The table name to join. Adds table prefix to table. |
|
200
|
|
|
* @param string $alias The alias of the join table |
|
201
|
|
|
* @param string $condition The condition for the join |
|
202
|
|
|
* |
|
203
|
|
|
* @return QueryBuilder This QueryBuilder instance. |
|
204
|
|
|
*/ |
|
205
|
5 |
|
public function leftJoinPrefix($fromAlias, $join, $alias, $condition = null) |
|
206
|
|
|
{ |
|
207
|
5 |
|
$join = $this->connection->prefix($join); |
|
208
|
5 |
|
return $this->leftJoin($fromAlias, $join, $alias, $condition); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Creates and adds a right join to the query. |
|
213
|
|
|
* |
|
214
|
|
|
* <code> |
|
215
|
|
|
* $qb = $conn->createQueryBuilder() |
|
216
|
|
|
* ->select('u.name') |
|
217
|
|
|
* ->from('users', 'u') |
|
218
|
|
|
* ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
219
|
|
|
* </code> |
|
220
|
|
|
* |
|
221
|
|
|
* @param string $fromAlias The alias that points to a from clause |
|
222
|
|
|
* @param string $join The table name to join. Adds table prefix to table. |
|
223
|
|
|
* @param string $alias The alias of the join table |
|
224
|
|
|
* @param string $condition The condition for the join |
|
225
|
|
|
* |
|
226
|
|
|
* @return QueryBuilder This QueryBuilder instance. |
|
227
|
|
|
*/ |
|
228
|
|
|
public function rightJoinPrefix($fromAlias, $join, $alias, $condition = null) |
|
229
|
|
|
{ |
|
230
|
|
|
$join = $this->connection->prefix($join); |
|
231
|
|
|
return $this->rightJoin($fromAlias, $join, $alias, $condition); |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|