Completed
Push — master ( 4370c7...e76084 )
by Thomas
22s queued 10s
created

QueryBuilder::columns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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