1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soluble\Schema\Source; |
4
|
|
|
|
5
|
|
|
use Soluble\Schema\Exception; |
6
|
|
|
use Soluble\DbWrapper\Adapter\AdapterInterface; |
7
|
|
|
|
8
|
|
|
abstract class AbstractSchemaSource implements SchemaSourceInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Default schema name. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $schema; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Schema signature. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $schemaSignature; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var AdapterInterface |
26
|
|
|
*/ |
27
|
|
|
protected $adapter; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor. |
31
|
|
|
* |
32
|
|
|
* @param AdapterInterface $adapter |
33
|
|
|
* @param string|null $schema default schema, taken from adapter if not given |
34
|
|
|
* |
35
|
|
|
* @throws Exception\InvalidArgumentException for invalid connection |
36
|
|
|
* @throws Exception\InvalidUsageException thrown if no schema can be found |
37
|
|
|
*/ |
38
|
23 |
|
public function __construct(AdapterInterface $adapter, $schema = null) |
39
|
|
|
{ |
40
|
23 |
|
$this->adapter = $adapter; |
41
|
23 |
|
if ($schema === null) { |
42
|
23 |
|
$schema = $this->adapter->getConnection()->getCurrentSchema(); |
43
|
23 |
|
if ($schema === false || $schema == '') { |
44
|
|
|
$msg = 'Database name (schema) parameter missing and no default schema set on connection'; |
45
|
|
|
throw new Exception\InvalidUsageException($msg); |
46
|
|
|
} |
47
|
|
|
} |
48
|
23 |
|
$this->setDefaultSchema($schema); |
49
|
23 |
|
$this->setSchemaSignature(); |
50
|
23 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
1 |
|
public function getColumns($table) |
56
|
|
|
{ |
57
|
1 |
|
return array_keys($this->getColumnsInformation($table)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
1 |
|
public function getTableInformation($table) |
64
|
|
|
{ |
65
|
1 |
|
$infos = $this->getTablesInformation(); |
66
|
|
|
|
67
|
1 |
|
return $infos[$table]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
1 |
|
public function getTables() |
74
|
|
|
{ |
75
|
1 |
|
return array_keys($this->getTablesInformation()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
1 |
|
public function hasTable($table) |
82
|
|
|
{ |
83
|
1 |
|
$tables = $this->getTables(); |
84
|
|
|
|
85
|
1 |
|
return in_array($table, $tables); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check whether a table parameter is valid and exists. |
90
|
|
|
* |
91
|
|
|
* @throws Exception\InvalidArgumentException |
92
|
|
|
* @throws Exception\ErrorException |
93
|
|
|
* @throws Exception\ExceptionInterface |
94
|
|
|
* @throws Exception\TableNotFoundException |
95
|
|
|
* |
96
|
|
|
* @param string $table |
97
|
|
|
* |
98
|
|
|
* @return self |
99
|
|
|
*/ |
100
|
|
|
protected function validateTable($table) |
101
|
|
|
{ |
102
|
|
|
$this->checkTableArgument($table); |
103
|
|
|
if (!$this->hasTable($table)) { |
104
|
|
|
throw new Exception\TableNotFoundException(__METHOD__ . ": Table '$table' does not exists in database '{$this->schema}'"); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Check whether a schema parameter is valid. |
112
|
|
|
* |
113
|
|
|
* @throws Exception\InvalidArgumentException |
114
|
|
|
* |
115
|
|
|
* @param string $schema |
116
|
|
|
* |
117
|
|
|
* @return self |
118
|
|
|
*/ |
119
|
23 |
|
protected function validateSchema($schema) |
120
|
|
|
{ |
121
|
23 |
|
if (!is_string($schema) || trim($schema) == '') { |
122
|
2 |
|
throw new Exception\InvalidArgumentException(__METHOD__ . ': Schema name must be a valid string or an empty string detected'); |
123
|
|
|
} |
124
|
|
|
|
125
|
23 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set default schema. |
130
|
|
|
* |
131
|
|
|
* @throws Exception\InvalidArgumentException |
132
|
|
|
* |
133
|
|
|
* @param string $schema |
134
|
|
|
* |
135
|
|
|
* @return self |
136
|
|
|
*/ |
137
|
23 |
|
protected function setDefaultSchema($schema) |
138
|
|
|
{ |
139
|
23 |
|
$this->validateSchema($schema); |
140
|
23 |
|
$this->schema = $schema; |
141
|
|
|
|
142
|
23 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $table |
147
|
|
|
* |
148
|
|
|
* @throws Exception\InvalidArgumentException |
149
|
|
|
*/ |
150
|
17 |
|
protected function checkTableArgument($table = null) |
151
|
|
|
{ |
152
|
17 |
|
if ($table !== null) { |
153
|
14 |
|
if (!is_string($table) || trim($table) == '') { |
154
|
2 |
|
throw new Exception\InvalidArgumentException(__METHOD__ . ' Table name must be a valid string or an empty string detected'); |
155
|
|
|
} |
156
|
|
|
} |
157
|
15 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Return current schema signature for caching. |
161
|
|
|
*/ |
162
|
23 |
|
protected function setSchemaSignature() |
163
|
|
|
{ |
164
|
23 |
|
$host = $this->adapter->getConnection()->getHost(); |
165
|
23 |
|
$schema = $this->schema; |
166
|
23 |
|
$this->schemaSignature = "$host:$schema"; |
167
|
23 |
|
} |
168
|
|
|
} |
169
|
|
|
|