1 | <?php |
||
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) |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 1 | public function getTableInformation($table) |
|
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | 1 | public function getTables() |
|
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | 1 | public function hasTable($table) |
|
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) |
||
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) |
|
158 | |||
159 | /** |
||
160 | * Return current schema signature for caching. |
||
161 | */ |
||
162 | 23 | protected function setSchemaSignature() |
|
168 | } |
||
169 |