|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Spiral Framework. |
|
4
|
|
|
* |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
* @author Anton Titov (Wolfy-J) |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Spiral\Database\Entities; |
|
10
|
|
|
|
|
11
|
|
|
use Spiral\Database\Builders\DeleteQuery; |
|
12
|
|
|
use Spiral\Database\Builders\SelectQuery; |
|
13
|
|
|
use Spiral\Database\Builders\UpdateQuery; |
|
14
|
|
|
use Spiral\Database\Exceptions\BuilderException; |
|
15
|
|
|
use Spiral\Database\Schemas\Prototypes\AbstractTable; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Represent table level abstraction with simplified access to SelectQuery associated with such |
|
19
|
|
|
* table. |
|
20
|
|
|
* |
|
21
|
|
|
* @method int avg($identifier) Perform aggregation (AVG) based on column or expression value. |
|
22
|
|
|
* @method int min($identifier) Perform aggregation (MIN) based on column or expression value. |
|
23
|
|
|
* @method int max($identifier) Perform aggregation (MAX) based on column or expression value. |
|
24
|
|
|
* @method int sum($identifier) Perform aggregation (SUM) based on column or expression value. |
|
25
|
|
|
*/ |
|
26
|
|
|
class Table implements \JsonSerializable, \IteratorAggregate |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $name = ''; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var Database |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $database = null; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param Database $database Parent DBAL database. |
|
40
|
|
|
* @param string $name Table name without prefix. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(Database $database, string $name) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->name = $name; |
|
45
|
|
|
$this->database = $database; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
* |
|
51
|
|
|
* @return Database |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getDatabase(): Database |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->database; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
* |
|
61
|
|
|
* @return AbstractTable |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getSchema(): AbstractTable |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->database->getDriver()->tableSchema($this->name, $this->database->getPrefix()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getName(): string |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->name; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Real table name, will include database prefix. |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
public function fullName(): string |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->database->getPrefix() . $this->name; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Check if table exists. |
|
88
|
|
|
* |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
public function exists(): bool |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->database->hasTable($this->name); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
|
|
public function truncateData() |
|
100
|
|
|
{ |
|
101
|
|
|
$this->database->getDriver()->truncateData($this->fullName()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get list of column names associated with their abstract types. |
|
106
|
|
|
* |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getColumns(): array |
|
110
|
|
|
{ |
|
111
|
|
|
$columns = []; |
|
112
|
|
|
foreach ($this->getSchema()->getColumns() as $column) { |
|
113
|
|
|
$columns[$column->getName()] = $column->abstractType(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $columns; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Insert one fieldset into table and return last inserted id. |
|
121
|
|
|
* |
|
122
|
|
|
* Example: |
|
123
|
|
|
* $table->insertOne(["name" => "Wolfy-J", "balance" => 10]); |
|
124
|
|
|
* |
|
125
|
|
|
* @param array $rowset |
|
126
|
|
|
* |
|
127
|
|
|
* @return int |
|
128
|
|
|
* |
|
129
|
|
|
* @throws BuilderException |
|
130
|
|
|
*/ |
|
131
|
|
|
public function insertOne(array $rowset = []): int |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->database->insert($this->name)->values($rowset)->run(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Perform batch insert into table, every rowset should have identical amount of values matched |
|
138
|
|
|
* with column names provided in first argument. Method will return lastInsertID on success. |
|
139
|
|
|
* |
|
140
|
|
|
* Example: |
|
141
|
|
|
* $table->insertMultiple(["name", "balance"], array(["Bob", 10], ["Jack", 20])) |
|
142
|
|
|
* |
|
143
|
|
|
* @param array $columns Array of columns. |
|
144
|
|
|
* @param array $rowsets Array of rowsets. |
|
145
|
|
|
*/ |
|
146
|
|
|
public function insertMultiple(array $columns = [], array $rowsets = []) |
|
147
|
|
|
{ |
|
148
|
|
|
//No return value |
|
149
|
|
|
$this->database->insert($this->name)->columns($columns)->values($rowsets)->run(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get SelectQuery builder with pre-populated from tables. |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $columns |
|
156
|
|
|
* |
|
157
|
|
|
* @return SelectQuery |
|
158
|
|
|
*/ |
|
159
|
|
|
public function select($columns = '*'): SelectQuery |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->database->select(func_num_args() ? func_get_args() : '*')->from($this->name); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Get DeleteQuery builder with pre-populated table name. This is NOT table delete method, use |
|
166
|
|
|
* schema()->drop() for this purposes. If you want to remove all records from table use |
|
167
|
|
|
* Table->truncate() method. Call ->run() to perform query. |
|
168
|
|
|
* |
|
169
|
|
|
* @param array $where Initial set of where rules specified as array. |
|
170
|
|
|
* |
|
171
|
|
|
* @return DeleteQuery |
|
172
|
|
|
*/ |
|
173
|
|
|
public function delete(array $where = []): DeleteQuery |
|
174
|
|
|
{ |
|
175
|
|
|
return $this->database->delete($this->name, $where); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Get UpdateQuery builder with pre-populated table name and set of columns to update. Columns |
|
180
|
|
|
* can be scalar values, Parameter objects or even SQLFragments. Call ->run() to perform query. |
|
181
|
|
|
* |
|
182
|
|
|
* @param array $values Initial set of columns associated with values. |
|
183
|
|
|
* @param array $where Initial set of where rules specified as array. |
|
184
|
|
|
* |
|
185
|
|
|
* @return UpdateQuery |
|
186
|
|
|
*/ |
|
187
|
|
|
public function update(array $values = [], array $where = []): UpdateQuery |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->database->update($this->name, $values, $where); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Count number of records in table. |
|
194
|
|
|
* |
|
195
|
|
|
* @return int |
|
196
|
|
|
*/ |
|
197
|
|
|
public function count(): int |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->select()->count(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Retrieve an external iterator, SelectBuilder will return PDOResult as iterator. |
|
204
|
|
|
* |
|
205
|
|
|
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
|
206
|
|
|
* |
|
207
|
|
|
* @return SelectQuery |
|
208
|
|
|
*/ |
|
209
|
|
|
public function getIterator(): SelectQuery |
|
210
|
|
|
{ |
|
211
|
|
|
return $this->select(); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* A simple alias for table query without condition (returns array of rows). |
|
216
|
|
|
* |
|
217
|
|
|
* @return array |
|
218
|
|
|
*/ |
|
219
|
|
|
public function fetchAll(): array |
|
220
|
|
|
{ |
|
221
|
|
|
return $this->select()->fetchAll(); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* {@inheritdoc} |
|
226
|
|
|
*/ |
|
227
|
|
|
public function jsonSerialize() |
|
228
|
|
|
{ |
|
229
|
|
|
return $this->select()->jsonSerialize(); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Bypass call to SelectQuery builder. |
|
234
|
|
|
* |
|
235
|
|
|
* @param string $method |
|
236
|
|
|
* @param array $arguments |
|
237
|
|
|
* |
|
238
|
|
|
* @return SelectQuery|mixed |
|
239
|
|
|
*/ |
|
240
|
|
|
public function __call($method, array $arguments) |
|
241
|
|
|
{ |
|
242
|
|
|
return call_user_func_array([$this->select(), $method], $arguments); |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|