1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleCrud\Queries\Mysql; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait with common functions used in queries. |
7
|
|
|
* |
8
|
|
|
* @property \SimpleCrud\Table $table |
9
|
|
|
*/ |
10
|
|
|
trait SelectionTrait |
11
|
|
|
{ |
12
|
|
|
protected $where = []; |
13
|
|
|
protected $marks = []; |
14
|
|
|
protected $limit; |
15
|
|
|
protected $offset; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Adds new marks to the query. |
19
|
|
|
* |
20
|
|
|
* @param array $marks |
21
|
|
|
* |
22
|
|
|
* @return self |
23
|
|
|
*/ |
24
|
|
|
public function marks(array $marks) |
25
|
|
|
{ |
26
|
|
|
$this->marks += $marks; |
27
|
|
|
|
28
|
|
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Adds a WHERE clause. |
33
|
|
|
* |
34
|
|
|
* @param string $where |
35
|
|
|
* @param null|array $marks |
36
|
|
|
* |
37
|
|
|
* @return self |
38
|
|
|
*/ |
39
|
|
|
public function where($where, $marks = null) |
40
|
|
|
{ |
41
|
|
|
$this->where[] = $where; |
42
|
|
|
|
43
|
|
|
if ($marks) { |
44
|
|
|
$this->marks += $marks; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Adds a OR WHERE clause. |
52
|
|
|
* |
53
|
|
|
* @param string $where |
54
|
|
|
* @param null|array $marks |
55
|
|
|
* |
56
|
|
|
* @return self |
57
|
|
|
*/ |
58
|
|
|
public function orWhere($where, $marks = null) |
59
|
|
|
{ |
60
|
|
|
if (!isset($this->where['or'])) { |
61
|
|
|
$this->where['or'] = [$where]; |
62
|
|
|
} else { |
63
|
|
|
$this->where['or'][] = $where; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($marks) { |
67
|
|
|
$this->marks += $marks; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Adds a WHERE field = :value clause. |
75
|
|
|
* |
76
|
|
|
* @param string $field |
77
|
|
|
* @param null|int|array $value |
78
|
|
|
* |
79
|
|
|
* @return self |
80
|
|
|
*/ |
81
|
|
|
public function by($field, $value) |
82
|
|
|
{ |
83
|
|
|
if (is_array($value)) { |
84
|
|
|
if (empty($value)) { |
85
|
|
|
return $this->where("`{$this->table->name}`.`{$field}` IS NULL"); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->where("`{$this->table->name}`.`{$field}` IN (:{$field})", [":{$field}" => $value]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($value === null) { |
92
|
|
|
return $this->where("`{$this->table->name}`.`{$field}` IS NULL"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this->where("`{$this->table->name}`.`{$field}` = :{$field}", [":{$field}" => $value]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Adds a WHERE id = :id clause. |
100
|
|
|
* |
101
|
|
|
* @param null|int|array $id |
102
|
|
|
* |
103
|
|
|
* @return self |
104
|
|
|
*/ |
105
|
|
|
public function byId($id) |
106
|
|
|
{ |
107
|
|
|
if ($this->limit === null) { |
108
|
|
|
$this->limit(is_array($id) ? count($id) : 1); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this->by('id', $id); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Adds a LIMIT clause. |
116
|
|
|
* |
117
|
|
|
* @param int $limit |
118
|
|
|
* |
119
|
|
|
* @return self |
120
|
|
|
*/ |
121
|
|
|
public function limit($limit) |
122
|
|
|
{ |
123
|
|
|
$this->limit = $limit; |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Adds an offset to the LIMIT clause. |
130
|
|
|
* |
131
|
|
|
* @param int $offset |
132
|
|
|
* |
133
|
|
|
* @return self |
134
|
|
|
*/ |
135
|
|
|
public function offset($offset) |
136
|
|
|
{ |
137
|
|
|
$this->offset = $offset; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Generate WHERE clause. |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
protected function whereToString() |
148
|
|
|
{ |
149
|
|
|
if (!empty($this->where)) { |
150
|
|
|
if (isset($this->where['or'])) { |
151
|
|
|
$this->where['or'] = '('.implode(') OR (', $this->where['or']).')'; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return ' WHERE ('.implode(') AND (', $this->where).')'; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return ''; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Generate LIMIT clause. |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
protected function limitToString() |
166
|
|
|
{ |
167
|
|
|
if (!empty($this->limit)) { |
168
|
|
|
$query = ' LIMIT'; |
169
|
|
|
|
170
|
|
|
if (!empty($this->offset)) { |
171
|
|
|
$query .= ' '.$this->offset.','; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $query.' '.$this->limit; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return ''; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|