Completed
Pull Request — master (#26)
by Thomas
02:46
created

QueryBuilder::createWhereCondition()   D

Complexity

Conditions 9
Paths 14

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 9

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 34
ccs 19
cts 19
cp 1
rs 4.909
cc 9
eloc 22
nc 14
nop 3
crap 9
1
<?php
2
3
namespace ORM\QueryBuilder;
4
5
use ORM\EntityManager;
6
use ORM\Exception;
7
use ORM\Exceptions\NoOperator;
8
9
/**
10
 * Build a ansi sql query / select statement
11
 *
12
 * If you need more specific queries you write them yourself. If you need just more specific where clause you can pass
13
 * them to the *where() methods.
14
 *
15
 * Supported:
16
 *  - joins with on clause (and alias)
17
 *  - joins with using (and alias)
18
 *  - where conditions
19
 *  - parenthesis
20
 *  - order by one or more columns / expressions
21
 *  - group by one or more columns / expressions
22
 *  - limit and offset
23
 *
24
 * @package ORM
25
 * @author Thomas Flori <[email protected]>
26
 */
27
class QueryBuilder extends Parenthesis implements QueryBuilderInterface
28
{
29
    /** The table to query
30
     * @var string */
31
    protected $tableName = '';
32
33
    /** The alias of the main table
34
     * @var string */
35
    protected $alias = '';
36
37
    /** Columns to fetch (null is equal to ['*'])
38
     * @var array|null */
39
    protected $columns = null;
40
41
    /** Joins get concatenated with space
42
     * @var string[] */
43
    protected $joins = [];
44
45
    /** Limit amount of rows
46
     * @var int */
47
    protected $limit;
48
49
    /** Offset to start from
50
     * @var int */
51
    protected $offset;
52
53
    /** Group by conditions get concatenated with comma
54
     * @var string[] */
55
    protected $groupBy = [];
56
57
    /** Order by conditions get concatenated with comma
58
     * @var string[] */
59
    protected $orderBy = [];
60
61
    /** Modifiers get concatenated with space
62
     * @var string[] */
63
    protected $modifier = [];
64
65
    /** EntityManager to use for quoting
66
     * @var EntityManager */
67
    protected $entityManager;
68
69
    /** The default EntityManager to use to for quoting
70
     * @var EntityManager */
71
    public static $defaultEntityManager;
72
73
    /** @noinspection PhpMissingParentConstructorInspection */
74
    /**
75
     * Constructor
76
     *
77
     * Create a select statement for $tableName with an object oriented interface.
78
     *
79
     * It uses static::$defaultEntityManager if $entityManager is not given.
80
     *
81
     * @param string        $tableName     The main table to use in FROM clause
82
     * @param string        $alias         An alias for the table
83
     * @param EntityManager $entityManager EntityManager for quoting
84
     */
85 116
    public function __construct($tableName, $alias = '', EntityManager $entityManager = null)
86
    {
87 116
        $this->tableName = $tableName;
88 116
        $this->alias = $alias;
89 116
        $this->entityManager = $entityManager;
90 116
    }
91
92
    /**
93
     * Replaces question marks in $expression with $args
94
     *
95
     * @param string      $expression Expression with placeholders
96
     * @param array|mixed $args       Arguments for placeholders
97
     * @return string
98
     * @throws \ORM\Exceptions\NoConnection
99
     * @throws \ORM\Exceptions\NotScalar
100
     */
101 136
    protected function convertPlaceholders(
102
        $expression,
103
        $args
104
    ) {
105 136
        if (strpos($expression, '?') === false) {
106 86
            return $expression;
107
        }
108
109 94
        if (!is_array($args)) {
110 60
            $args = [$args];
111
        }
112
113 94
        $parts = explode('?', $expression);
114 94
        $expression = '';
115 94
        while ($part = array_shift($parts)) {
116 94
            $expression .= $part;
117 94
            if (count($args)) {
118 94
                $expression .= $this->getEntityManager()->escapeValue(array_shift($args));
119 40
            } elseif (count($parts)) {
120 1
                $expression .= '?';
121
            }
122
        }
123
124 94
        return $expression;
125
    }
126
127
    /**
128
     * @return EntityManager
129
     */
130 96
    public function getEntityManager()
131
    {
132 96
        return $this->entityManager ?: static::$defaultEntityManager;
133
    }
134
135
    /**
136
     * Common implementation for creating a where condition
137
     *
138
     * @param string $column   Column or expression with placeholders
139
     * @param string $operator Operator or value if operator is omited
140
     * @param string $value    Value or array of values
141
     * @return string
142
     * @throws NoOperator
143
     * @internal
144
     */
145 94
    public function createWhereCondition($column, $operator = null, $value = null)
146
    {
147 94
        if (strpos($column, '?') !== false) {
148 16
            $expression = $column;
149 16
            $value      = $operator;
150 89
        } elseif ($operator === null && $value === null) {
151 52
            $expression = $column;
152
        } else {
153 67
            if ($value === null) {
154 45
                $value = $operator;
155 45
                $operator = null;
156
            }
157
158 67
            if ($operator === null) {
159 46
                if (is_array($value)) {
160 14
                    $operator = 'IN';
161
                } else {
162 32
                    $operator = '=';
163
                }
164
            }
165
166 67
            $expression = $column . ' ' . $operator;
167
168 67
            if (in_array(strtoupper($operator), ['IN', 'NOT IN']) && is_array($value)) {
169 22
                $expression .= ' (?' . str_repeat(',?', count($value) - 1) . ')';
170
            } else {
171 46
                $expression .= ' ?';
172
            }
173
        }
174
175 94
        $whereCondition = $this->convertPlaceholders($expression, $value);
176
177 93
        return $whereCondition;
178
    }
179
180
    /** {@inheritdoc} */
181 2
    public function columns(array $columns = null)
182
    {
183 2
        $this->columns = $columns;
184
185 2
        return $this;
186
    }
187
188
    /** {@inheritdoc} */
189 5
    public function column($column, $args = [], $alias = '')
190
    {
191 5
        if ($this->columns === null) {
192 5
            $this->columns = [];
193
        }
194
195 5
        $expression = $this->convertPlaceholders($column, $args);
196
197 5
        $this->columns[] = $expression . ($alias ? ' AS ' . $alias : '');
198
199 5
        return $this;
200
    }
201
202
    /** @internal
203
     * @return self */
204 1
    public function close()
205
    {
206 1
        return $this;
207
    }
208
209
    /**
210
     * Common implementation for *Join methods
211
     *
212
     * @param string      $join       The join type (e. g. `LEFT JOIN`)
213
     * @param string      $tableName  Table name to join
214
     * @param string      $expression Expression to use in on clause or single column for USING
215
     * @param string      $alias      Alias for the table
216
     * @param array|mixed $args       Arguments to use in $expression
217
     * @param bool        $empty      Create an empty join (without USING and ON)
218
     * @return ParenthesisInterface|QueryBuilder
219
     * @throws \ORM\Exceptions\NoConnection
220
     * @throws \ORM\Exceptions\NotScalar
221
     * @internal
222
     */
223 42
    protected function createJoin($join, $tableName, $expression, $alias, $args, $empty)
224
    {
225 42
        $join = $join . ' ' . $tableName
226 42
                . ($alias ? ' AS ' . $alias : '');
227
228 42
        if (preg_match('/^[A-Za-z_]+$/', $expression)) {
229 4
            $join .= ' USING (' . $expression . ')';
230 4
            $this->joins[] = $join;
231 38
        } elseif ($expression) {
232 30
            $expression = $this->convertPlaceholders($expression, $args);
233
234 30
            $join .= ' ON ' . $expression;
235 30
            $this->joins[] = $join;
236 8
        } elseif ($empty) {
237 4
            $this->joins[] = $join;
238
        } else {
239 4
            return new Parenthesis(function (ParenthesisInterface $parenthesis) use ($join) {
240 4
                $join .= ' ON ' . $parenthesis->getExpression();
241 4
                $this->joins[] = $join;
242 4
                return $this;
243 4
            }, $this);
244
        }
245
246 38
        return $this;
247
    }
248
249
    /** {@inheritdoc} */
250 17
    public function join($tableName, $expression = '', $alias = '', $args = [])
251
    {
252 17
        return $this->createJoin(
253 17
            'JOIN',
254
            $tableName,
255 17
            is_string($expression) ? $expression : '',
256
            $alias,
257
            $args,
258 17
            is_bool($expression) ? $expression : false
259
        );
260
    }
261
262
    /** {@inheritdoc} */
263 11
    public function leftJoin($tableName, $expression = '', $alias = '', $args = [])
264
    {
265 11
        return $this->createJoin(
266 11
            'LEFT JOIN',
267
            $tableName,
268 11
            is_string($expression) ? $expression : '',
269
            $alias,
270
            $args,
271 11
            is_bool($expression) ? $expression : false
272
        );
273
    }
274
275
    /** {@inheritdoc} */
276 7
    public function rightJoin($tableName, $expression = '', $alias = '', $args = [])
277
    {
278 7
        return $this->createJoin(
279 7
            'RIGHT JOIN',
280
            $tableName,
281 7
            is_string($expression) ? $expression : '',
282
            $alias,
283
            $args,
284 7
            is_bool($expression) ? $expression : false
285
        );
286
    }
287
288
    /** {@inheritdoc} */
289 7
    public function fullJoin($tableName, $expression = '', $alias = '', $args = [])
290
    {
291 7
        return $this->createJoin(
292 7
            'FULL JOIN',
293
            $tableName,
294 7
            is_string($expression) ? $expression : '',
295
            $alias,
296
            $args,
297 7
            is_bool($expression) ? $expression : true
298
        );
299
    }
300
301
    /** {@inheritdoc} */
302 4
    public function groupBy($column, $args = [])
303
    {
304 4
        $this->groupBy[] = $this->convertPlaceholders($column, $args);
305
306 4
        return $this;
307
    }
308
309
    /** {@inheritdoc} */
310 5
    public function orderBy($column, $direction = self::DIRECTION_ASCENDING, $args = [])
311
    {
312 5
        $expression = $this->convertPlaceholders($column, $args);
313
314 5
        $this->orderBy[] = $expression . ' ' . $direction;
315
316 5
        return $this;
317
    }
318
319
    /** {@inheritdoc} */
320 2
    public function limit($limit)
321
    {
322 2
        $this->limit = (int) $limit;
323
324 2
        return $this;
325
    }
326
327
    /** {@inheritdoc} */
328 1
    public function offset($offset)
329
    {
330 1
        $this->offset = (int) $offset;
331
332 1
        return $this;
333
    }
334
335
    /** {@inheritdoc} */
336 156
    public function getQuery()
337
    {
338
        return 'SELECT '
339 156
               . (!empty($this->modifier) ? implode(' ', $this->modifier) . ' ' : '')
340 156
               . ($this->columns ? implode(',', $this->columns) : '*')
341 156
               . ' FROM ' . $this->tableName . ($this->alias ? ' AS ' . $this->alias : '')
342 156
               . (!empty($this->joins) ? ' ' . implode(' ', $this->joins) : '')
343 156
               . (!empty($this->where) ? ' WHERE ' . implode(' ', $this->where) : '')
344 156
               . (!empty($this->groupBy) ? ' GROUP BY ' . implode(',', $this->groupBy) : '')
345 156
               . (!empty($this->orderBy) ? ' ORDER BY ' . implode(',', $this->orderBy) : '')
346 156
               . ($this->limit ? ' LIMIT ' . $this->limit . ($this->offset ? ' OFFSET ' . $this->offset : '') : '');
347
    }
348
349
    /** {@inheritdoc} */
350 1
    public function modifier($modifier)
351
    {
352 1
        $this->modifier[] = $modifier;
353
354 1
        return $this;
355
    }
356
}
357