1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mindplay\sql\model; |
4
|
|
|
|
5
|
|
|
use mindplay\sql\framework\Driver; |
6
|
|
|
use mindplay\sql\framework\MapperProvider; |
7
|
|
|
use mindplay\sql\framework\TypeProvider; |
8
|
|
|
use mindplay\sql\model\components\Mappers; |
9
|
|
|
use mindplay\sql\model\components\ReturnVars; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This class represents a SELECT query. |
13
|
|
|
* |
14
|
|
|
* This class implements `__toString()` magic, enabling the use of this query builder |
15
|
|
|
* in the SELECT, WHERE or ORDER BY clause of a parent SELECT (or other type of) query. |
16
|
|
|
* |
17
|
|
|
* Note that, when constructing nested queries, parameters must be bound against the |
18
|
|
|
* parent query - binding parameters or applying Mappers against a nested query has no effect. |
19
|
|
|
*/ |
20
|
|
|
class SelectQuery extends ProjectionQuery implements MapperProvider |
21
|
|
|
{ |
22
|
|
|
use Mappers; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ReturnVars |
26
|
|
|
*/ |
27
|
|
|
private $return_vars; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Table $root |
31
|
|
|
* @param Driver $driver |
32
|
|
|
* @param TypeProvider $types |
33
|
|
|
*/ |
34
|
1 |
|
public function __construct(Table $root, Driver $driver, TypeProvider $types) |
35
|
|
|
{ |
36
|
1 |
|
parent::__construct($root, $driver, $types); |
37
|
|
|
|
38
|
1 |
|
$this->return_vars = new ReturnVars($root, $driver, $types); |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Add all the Columns of a full Table to be selected and returned |
43
|
|
|
* |
44
|
|
|
* @param Table $table Table to select and return |
45
|
|
|
* |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
1 |
|
public function table(Table $table) |
49
|
|
|
{ |
50
|
1 |
|
$this->return_vars->addTable($table); |
51
|
|
|
|
52
|
1 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Add one or more Columns to select and return |
57
|
|
|
* |
58
|
|
|
* @param Column|Column[] one or more Columns to select and return |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
1 |
|
public function columns($cols) |
63
|
|
|
{ |
64
|
1 |
|
$this->return_vars->addColumns($cols); |
65
|
|
|
|
66
|
1 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Add an SQL expression to select and return |
71
|
|
|
* |
72
|
|
|
* @param string $expr return expression |
73
|
|
|
* @param string|null $name return variable name (optional, but usually required) |
74
|
|
|
* @param Type|string|null $type optional Type (or Type class-name) |
75
|
|
|
* |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
1 |
|
public function value($expr, $name = null, $type = null) |
79
|
|
|
{ |
80
|
1 |
|
$this->return_vars->addValue($expr, $name, $type); |
81
|
|
|
|
82
|
1 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritdoc |
87
|
|
|
*/ |
88
|
1 |
|
public function getMappers() |
89
|
|
|
{ |
90
|
1 |
|
return array_merge([$this->return_vars->createTypeMapper()], $this->mappers); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritdoc |
95
|
|
|
*/ |
96
|
1 |
View Code Duplication |
public function getSQL() |
|
|
|
|
97
|
|
|
{ |
98
|
1 |
|
$select = "SELECT " . $this->buildReturnVars(); |
99
|
|
|
|
100
|
1 |
|
$from = "\nFROM " . $this->buildNodes(); |
101
|
|
|
|
102
|
1 |
|
$where = count($this->conditions) |
103
|
1 |
|
? "\nWHERE " . $this->buildConditions() |
104
|
1 |
|
: ''; // no conditions present |
105
|
|
|
|
106
|
1 |
|
$order = count($this->order) |
107
|
1 |
|
? "\nORDER BY " . $this->buildOrderTerms() |
108
|
1 |
|
: ''; // no order terms |
109
|
|
|
|
110
|
1 |
|
$limit = $this->limit !== null |
111
|
1 |
|
? "\nLIMIT {$this->limit}" |
112
|
1 |
|
. ($this->offset !== null ? " OFFSET {$this->offset}" : '') |
113
|
1 |
|
: ''; // no limit or offset |
114
|
|
|
|
115
|
1 |
|
return "{$select}{$from}{$where}{$order}{$limit}"; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @ignore string magic (enables creation of nested SELECT queries) |
120
|
|
|
*/ |
121
|
1 |
|
public function __toString() |
122
|
|
|
{ |
123
|
1 |
|
return "(" . $this->getSQL() . ")"; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return string comma-separated return expressions (for use in the SELECT or RETURNING clause of an SQL query) |
128
|
|
|
*/ |
129
|
1 |
|
protected function buildReturnVars() |
130
|
|
|
{ |
131
|
1 |
|
return $this->return_vars->buildReturnVars(); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.