|
1
|
|
|
<?php |
|
2
|
|
|
namespace Soluble\Normalist\Synthetic; |
|
3
|
|
|
|
|
4
|
|
|
use Soluble\Normalist\Driver; |
|
5
|
|
|
use Soluble\Db\Sql\Select; |
|
6
|
|
|
use Soluble\Schema\Source; |
|
7
|
|
|
use Zend\Db\Adapter\Adapter; |
|
8
|
|
|
use Zend\Db\Sql\Sql; |
|
9
|
|
|
use ArrayObject; |
|
10
|
|
|
|
|
11
|
|
|
class TableManager |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* |
|
15
|
|
|
* @param Adapter $adapter |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $adapter; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* |
|
21
|
|
|
* @var Source\AbstractSchemaSource |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $metadata; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Global table prefix |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $table_prefix; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* |
|
34
|
|
|
* @var \Zend\Db\Sql\Sql |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $sql; |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* |
|
41
|
|
|
* @var Transaction |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $transaction; |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* |
|
48
|
|
|
* @var \ArrayObject |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $localTableCache; |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* |
|
55
|
|
|
* @var Driver\DriverInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $driver; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* |
|
61
|
|
|
* @param Driver\DriverInterface $driver |
|
62
|
|
|
*/ |
|
63
|
131 |
|
public function __construct(Driver\DriverInterface $driver) |
|
64
|
|
|
{ |
|
65
|
131 |
|
$this->localTableCache = new ArrayObject(); |
|
66
|
131 |
|
$this->driver = $driver; |
|
67
|
131 |
|
$this->setDbAdapter($driver->getDbAdapter()); |
|
68
|
|
|
|
|
69
|
131 |
|
$this->sql = new Sql($this->adapter); |
|
70
|
131 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Return a synthetic table |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $table_name table name |
|
79
|
|
|
* |
|
80
|
|
|
* @throws Exception\InvalidArgumentException if table name is not valid |
|
81
|
|
|
* |
|
82
|
|
|
* @return Table |
|
83
|
|
|
*/ |
|
84
|
128 |
|
public function table($table_name) |
|
85
|
|
|
{ |
|
86
|
128 |
|
if (!is_string($table_name)) { |
|
87
|
1 |
|
throw new Exception\InvalidArgumentException(__METHOD__ . ": Table name must be a string"); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
128 |
|
if (!$this->localTableCache->offsetExists($table_name)) { |
|
91
|
128 |
|
$tables = $this->metadata()->getTables(); |
|
92
|
128 |
|
if (!in_array($table_name, $tables)) { |
|
93
|
1 |
|
throw new Exception\TableNotFoundException(__METHOD__ . ": Table $table_name is not found in database, if table exists please make sure cache is updated."); |
|
94
|
|
|
} |
|
95
|
128 |
|
$table = new Table($table_name, $this); |
|
96
|
128 |
|
$this->localTableCache->offsetSet($table_name, $table); |
|
97
|
128 |
|
} |
|
98
|
128 |
|
return $this->localTableCache->offsetGet($table_name); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Return a generic select |
|
103
|
|
|
* |
|
104
|
|
|
* @return Select |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function select() |
|
107
|
|
|
{ |
|
108
|
1 |
|
$select = new Select(); |
|
109
|
1 |
|
$select->setDbAdapter($this->adapter); |
|
110
|
1 |
|
return $select; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Return a transaction object |
|
117
|
|
|
* |
|
118
|
|
|
* @return Transaction |
|
119
|
|
|
*/ |
|
120
|
3 |
|
public function transaction() |
|
121
|
|
|
{ |
|
122
|
3 |
|
if ($this->transaction === null) { |
|
123
|
3 |
|
$this->transaction = new Transaction($this->adapter); |
|
124
|
3 |
|
} |
|
125
|
3 |
|
return $this->transaction; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Return underlyng Zend\Db\Adapter\Adapter |
|
133
|
|
|
* |
|
134
|
|
|
* @return Adapter $adapter |
|
135
|
|
|
*/ |
|
136
|
131 |
|
public function getDbAdapter() |
|
137
|
|
|
{ |
|
138
|
131 |
|
return $this->adapter; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Set global table prefix |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $table_prefix |
|
148
|
|
|
* @return TableManager |
|
149
|
|
|
*/ |
|
150
|
3 |
|
public function setTablePrefix($table_prefix) |
|
151
|
|
|
{ |
|
152
|
3 |
|
$this->table_prefix = $table_prefix; |
|
153
|
3 |
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Return global table prefix |
|
158
|
|
|
* |
|
159
|
|
|
* @return string |
|
160
|
|
|
*/ |
|
161
|
128 |
|
public function getTablePrefix() |
|
162
|
|
|
{ |
|
163
|
128 |
|
return $this->table_prefix; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Return prefixed table name |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $table |
|
173
|
|
|
* @return string |
|
174
|
|
|
*/ |
|
175
|
6 |
|
public function getPrefixedTable($table) |
|
176
|
|
|
{ |
|
177
|
6 |
|
return $this->table_prefix . $table; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Return a metadata reader |
|
185
|
|
|
* |
|
186
|
|
|
* @return Source\AbstractSchemaSource |
|
187
|
|
|
*/ |
|
188
|
128 |
|
public function metadata() |
|
189
|
|
|
{ |
|
190
|
128 |
|
if ($this->metadata === null) { |
|
191
|
128 |
|
$this->loadDefaultMetadata(); |
|
192
|
128 |
|
} |
|
193
|
128 |
|
return $this->metadata; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Set the database adapter |
|
198
|
|
|
* |
|
199
|
|
|
* @param Adapter $adapter |
|
200
|
|
|
* @return TableManager |
|
201
|
|
|
*/ |
|
202
|
131 |
|
protected function setDbAdapter(Adapter $adapter) |
|
203
|
|
|
{ |
|
204
|
131 |
|
$this->adapter = $adapter; |
|
205
|
131 |
|
return $this; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Return default metadata reader associated to an adapter |
|
210
|
|
|
* |
|
211
|
|
|
* @throws Exception\UnsupportedFeatureException |
|
212
|
|
|
*/ |
|
213
|
128 |
|
protected function loadDefaultMetadata() |
|
214
|
|
|
{ |
|
215
|
128 |
|
$adapterName = $this->adapter->getPlatform()->getName(); |
|
216
|
128 |
|
switch (strtolower($adapterName)) { |
|
217
|
128 |
|
case 'mysql': |
|
218
|
|
|
// supported |
|
219
|
128 |
|
break; |
|
220
|
1 |
|
default: |
|
221
|
1 |
|
throw new Exception\UnsupportedFeatureException(__METHOD__ . ": Adapter '$adapterName' is not yet supported."); |
|
222
|
128 |
|
} |
|
223
|
128 |
|
$this->metadata = $this->driver->getMetadata(); |
|
224
|
128 |
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* |
|
228
|
|
|
* @param Source\AbstractSchemaSource $metadata |
|
229
|
|
|
* @return TableManager |
|
230
|
|
|
*/ |
|
231
|
1 |
|
public function setMetadata(Source\AbstractSchemaSource $metadata) |
|
232
|
|
|
{ |
|
233
|
1 |
|
$this->metadata = $metadata; |
|
234
|
1 |
|
return $this; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|