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 <redheadedrod@hotmail.com>
24: * @author Richard Griffith <richard@geekwright.com>
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: * Turns the query being built into a bulk delete query that ranges over
36: * a certain table.
37: *
38: * <code>
39: * $qb = $conn->createQueryBuilder()
40: * ->delete('users', 'u')
41: * ->where('u.id = :user_id');
42: * ->setParameter(':user_id', 1);
43: * </code>
44: *
45: * @param string $delete The table whose rows are subject to the deletion.
46: * Adds table prefix to table.
47: * @param string $alias The table alias used in the constructed query.
48: *
49: * @return QueryBuilder This QueryBuilder instance.
50: */
51: public function deletePrefix($delete = null, $alias = null)
52: {
53: $delete = Connection::prefix($delete);
54: return $this->delete($delete, $alias);
55: }
56:
57: /**
58: * Turns the query being built into a bulk update query that ranges over
59: * a certain table
60: *
61: * <code>
62: * $qb = $conn->createQueryBuilder()
63: * ->update('users', 'u')
64: * ->set('u.password', md5('password'))
65: * ->where('u.id = ?');
66: * </code>
67: *
68: * @param string $update The table whose rows are subject to the update.
69: * Adds table prefix to table.
70: * @param string $alias The table alias used in the constructed query.
71: *
72: * @return QueryBuilder This QueryBuilder instance.
73: */
74: public function updatePrefix($update = null, $alias = null)
75: {
76: $update = Connection::prefix($update);
77: return $this->update($update, $alias);
78: }
79:
80: /**
81: * Turns the query being built into an insert query that inserts into
82: * a certain table
83: *
84: * <code>
85: * $qb = $conn->createQueryBuilder()
86: * ->insert('users')
87: * ->values(
88: * array(
89: * 'name' => '?',
90: * 'password' => '?'
91: * )
92: * );
93: * </code>
94: *
95: * @param string $insert The table into which the rows should be inserted.
96: * Adds table prefix to table.
97: *
98: * @return QueryBuilder This QueryBuilder instance.
99: */
100: public function insertPrefix($insert = null)
101: {
102: $insert = Connection::prefix($insert);
103: return $this->insert($insert);
104: }
105:
106: /**
107: * Create and add a query root corresponding to the table identified by the
108: * given alias, forming a cartesian product with any existing query roots.
109: *
110: * <code>
111: * $qb = $conn->createQueryBuilder()
112: * ->select('u.id')
113: * ->from('users', 'u')
114: * </code>
115: *
116: * @param string $from The table. Adds table prefix to table.
117: * @param string|null $alias The alias of the table.
118: *
119: * @return QueryBuilder This QueryBuilder instance.
120: */
121: public function fromPrefix($from, $alias = null)
122: {
123: $from = Connection::prefix($from);
124: return $this->from($from, $alias);
125: }
126:
127: /**
128: * Creates and adds a join to the query.
129: *
130: * <code>
131: * $qb = $conn->createQueryBuilder()
132: * ->select('u.name')
133: * ->from('users', 'u')
134: * ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1');
135: * </code>
136: *
137: * @param string $fromAlias The alias that points to a from clause
138: * @param string $join The table name to join. Adds table prefix to table.
139: * @param string $alias The alias of the join table
140: * @param string $condition The condition for the join
141: *
142: * @return QueryBuilder This QueryBuilder instance.
143: */
144: public function joinPrefix($fromAlias, $join, $alias, $condition = null)
145: {
146: $join = Connection::prefix($join);
147: return $this->join($fromAlias, $join, $alias, $condition);
148: }
149:
150:
151: /**
152: * Creates and adds a join to the query.
153: *
154: * <code>
155: * $qb = $conn->createQueryBuilder()
156: * ->select('u.name')
157: * ->from('users', 'u')
158: * ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
159: * </code>
160: *
161: * @param string $fromAlias The alias that points to a from clause
162: * @param string $join The table name to join. Adds table prefix to table.
163: * @param string $alias The alias of the join table
164: * @param string $condition The condition for the join
165: *
166: * @return QueryBuilder This QueryBuilder instance.
167: */
168: public function innerJoinPrefix($fromAlias, $join, $alias, $condition = null)
169: {
170: $join = Connection::prefix($join);
171: return $this->innerJoin($fromAlias, $join, $alias, $condition);
172: }
173:
174: /**
175: * Creates and adds a left join to the query.
176: *
177: * <code>
178: * $qb = $conn->createQueryBuilder()
179: * ->select('u.name')
180: * ->from('users', 'u')
181: * ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
182: * </code>
183: *
184: * @param string $fromAlias The alias that points to a from clause
185: * @param string $join The table name to join. Adds table prefix to table.
186: * @param string $alias The alias of the join table
187: * @param string $condition The condition for the join
188: *
189: * @return QueryBuilder This QueryBuilder instance.
190: */
191: public function leftJoinPrefix($fromAlias, $join, $alias, $condition = null)
192: {
193: $join = Connection::prefix($join);
194: return $this->leftJoin($fromAlias, $join, $alias, $condition);
195: }
196:
197: /**
198: * Creates and adds a right join to the query.
199: *
200: * <code>
201: * $qb = $conn->createQueryBuilder()
202: * ->select('u.name')
203: * ->from('users', 'u')
204: * ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
205: * </code>
206: *
207: * @param string $fromAlias The alias that points to a from clause
208: * @param string $join The table name to join. Adds table prefix to table.
209: * @param string $alias The alias of the join table
210: * @param string $condition The condition for the join
211: *
212: * @return QueryBuilder This QueryBuilder instance.
213: */
214: public function rightJoinPrefix($fromAlias, $join, $alias, $condition = null)
215: {
216: $join = Connection::prefix($join);
217: return $this->rightJoin($fromAlias, $join, $alias, $condition);
218: }
219: }
220: