1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
namespace Spiral\Database\Entities; |
9
|
|
|
|
10
|
|
|
use Spiral\Database\Builders\DeleteQuery; |
11
|
|
|
use Spiral\Database\Builders\SelectQuery; |
12
|
|
|
use Spiral\Database\Builders\UpdateQuery; |
13
|
|
|
use Spiral\Database\Entities\Schemas\AbstractTable; |
14
|
|
|
use Spiral\Database\Query\QueryResult; |
15
|
|
|
use Spiral\Database\TableInterface; |
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, TableInterface |
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, $name) |
43
|
|
|
{ |
44
|
|
|
$this->name = $name; |
45
|
|
|
$this->database = $database; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Related table database. |
50
|
|
|
* |
51
|
|
|
* @return Database |
52
|
|
|
*/ |
53
|
|
|
public function database() |
54
|
|
|
{ |
55
|
|
|
return $this->database; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
* |
61
|
|
|
* @return AbstractTable |
62
|
|
|
*/ |
63
|
|
|
public function schema() |
64
|
|
|
{ |
65
|
|
|
return $this->database->driver()->tableSchema( |
66
|
|
|
$this->realName(), |
67
|
|
|
$this->database->getPrefix() |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Check if table exists. |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function exists() |
77
|
|
|
{ |
78
|
|
|
return $this->database->hasTable($this->name); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function getName() |
85
|
|
|
{ |
86
|
|
|
return $this->name; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Real table name, will include database prefix. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function realName() |
95
|
|
|
{ |
96
|
|
|
return $this->database->getPrefix() . $this->name; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get list of column names associated with their abstract types. |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function getColumns() |
105
|
|
|
{ |
106
|
|
|
$columns = []; |
107
|
|
|
foreach ($this->schema()->getColumns() as $column) { |
108
|
|
|
$columns[$column->getName()] = $column->abstractType(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $columns; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function truncate() |
118
|
|
|
{ |
119
|
|
|
$this->database->driver()->truncate($this->realName()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function insert(array $rowset = []) |
126
|
|
|
{ |
127
|
|
|
return $this->database->insert($this->name)->values($rowset)->run(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Perform batch insert into table, every rowset should have identical amount of values matched |
132
|
|
|
* with column names provided in first argument. Method will return lastInsertID on success. |
133
|
|
|
* |
134
|
|
|
* Example: |
135
|
|
|
* $table->insert(["name", "balance"], array(["Bob", 10], ["Jack", 20])) |
136
|
|
|
* |
137
|
|
|
* @param array $columns Array of columns. |
138
|
|
|
* @param array $rowsets Array of rowsets. |
139
|
|
|
* @return mixed |
140
|
|
|
*/ |
141
|
|
|
public function batchInsert(array $columns = [], array $rowsets = []) |
142
|
|
|
{ |
143
|
|
|
return $this->database->insert($this->name)->columns($columns)->values($rowsets)->run(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get SelectQuery builder with pre-populated from tables. |
148
|
|
|
* |
149
|
|
|
* @param string $columns |
150
|
|
|
* @return SelectQuery |
151
|
|
|
*/ |
152
|
|
|
public function select($columns = '*') |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
return $this->database->select(func_num_args() ? func_get_args() : '*')->from($this->name); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get DeleteQuery builder with pre-populated table name. This is NOT table delete method, use |
159
|
|
|
* schema()->drop() for this purposes. If you want to remove all records from table use |
160
|
|
|
* Table->truncate() method. Call ->run() to perform query. |
161
|
|
|
* |
162
|
|
|
* @param array $where Initial set of where rules specified as array. |
163
|
|
|
* @return DeleteQuery |
164
|
|
|
*/ |
165
|
|
|
public function delete(array $where = []) |
166
|
|
|
{ |
167
|
|
|
return $this->database->delete($this->name, $where); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get UpdateQuery builder with pre-populated table name and set of columns to update. Columns |
172
|
|
|
* can be scalar values, Parameter objects or even SQLFragments. Call ->run() to perform query. |
173
|
|
|
* |
174
|
|
|
* @param array $values Initial set of columns associated with values. |
175
|
|
|
* @param array $where Initial set of where rules specified as array. |
176
|
|
|
* @return UpdateQuery |
177
|
|
|
*/ |
178
|
|
|
public function update(array $values = [], array $where = []) |
179
|
|
|
{ |
180
|
|
|
return $this->database->update($this->name, $values, $where); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Count number of records in table. |
185
|
|
|
* |
186
|
|
|
* @return int |
187
|
|
|
*/ |
188
|
|
|
public function count() |
189
|
|
|
{ |
190
|
|
|
return $this->select()->count(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Retrieve an external iterator, SelectBuilder will return QueryResult as iterator. |
195
|
|
|
* |
196
|
|
|
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
197
|
|
|
* @return SelectQuery |
198
|
|
|
*/ |
199
|
|
|
public function getIterator() |
200
|
|
|
{ |
201
|
|
|
return $this->select(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* A simple alias for table query without condition. |
206
|
|
|
* |
207
|
|
|
* @return QueryResult |
208
|
|
|
*/ |
209
|
|
|
public function all() |
210
|
|
|
{ |
211
|
|
|
return $this->select()->all(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* {@inheritdoc} |
216
|
|
|
*/ |
217
|
|
|
public function jsonSerialize() |
218
|
|
|
{ |
219
|
|
|
return $this->select()->jsonSerialize(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Bypass call to SelectQuery builder. |
224
|
|
|
* |
225
|
|
|
* @param string $method |
226
|
|
|
* @param array $arguments |
227
|
|
|
* @return SelectQuery |
228
|
|
|
*/ |
229
|
|
|
public function __call($method, array $arguments) |
230
|
|
|
{ |
231
|
|
|
return call_user_func_array([$this->select(), $method], $arguments); |
232
|
|
|
} |
233
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.