1 | <?php |
||
11 | class MysqlInformationSchema extends Source\AbstractSchemaSource |
||
12 | { |
||
13 | /** |
||
14 | * Schema name. |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $schema; |
||
19 | |||
20 | /** |
||
21 | * Whether to include full schema options like comment, collations... |
||
22 | * |
||
23 | * @var bool |
||
24 | */ |
||
25 | protected $include_options = true; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected static $localCache = []; |
||
31 | |||
32 | /** |
||
33 | * @var bool |
||
34 | */ |
||
35 | protected $useLocalCaching = true; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected static $fullyCachedSchemas = []; |
||
41 | |||
42 | /** |
||
43 | * @var Mysql\MysqlDriverInterface |
||
44 | */ |
||
45 | protected $driver; |
||
46 | |||
47 | /** |
||
48 | * Constructor. |
||
49 | * |
||
50 | * @param \PDO|\mysqli|AdapterInterface $adapter |
||
51 | * @param string|null $schema default schema, taken from adapter if not given |
||
52 | * |
||
53 | * @throws Exception\InvalidArgumentException for invalid connection |
||
54 | * @throws Exception\InvalidUsageException thrown if no schema can be found |
||
55 | */ |
||
56 | 23 | public function __construct($adapter, $schema = null) |
|
57 | { |
||
58 | 23 | if (!$adapter instanceof AdapterInterface) { |
|
59 | try { |
||
60 | 23 | $adapter = AdapterFactory::createAdapterFromResource($adapter); |
|
61 | } catch (Exception\InvalidArgumentException $e) { |
||
62 | $msg = "MysqlInformationSchema requires a valid 'mysqli', 'pdo:mysql' or AdapterInterface parameter ({$e->getMessage()})."; |
||
63 | throw new Exception\InvalidArgumentException($msg); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | 23 | parent::__construct($adapter, $schema); |
|
68 | |||
69 | 23 | $this->driver = new Mysql\MysqlDriver51($this->adapter, $this->schema); |
|
70 | 23 | } |
|
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | 1 | public function getUniqueKeys($table, $include_primary = false) |
|
76 | { |
||
77 | 1 | $this->loadCacheInformation($table); |
|
78 | 1 | $uniques = (array) self::$localCache[$this->schemaSignature]['tables'][$table]['unique_keys']; |
|
79 | 1 | if ($include_primary) { |
|
80 | try { |
||
81 | 1 | $pks = $this->getPrimaryKeys($table); |
|
82 | 1 | if (count($pks) > 0) { |
|
83 | 1 | $uniques = array_merge($uniques, ['PRIMARY' => $pks]); |
|
84 | } |
||
85 | 1 | } catch (Exception\NoPrimaryKeyException $e) { |
|
86 | // Ignore exception |
||
87 | } |
||
88 | } |
||
89 | |||
90 | 1 | return $uniques; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | public function getIndexesInformation($table) |
||
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | 6 | public function getPrimaryKey($table) |
|
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | 10 | public function getPrimaryKeys($table) |
|
121 | { |
||
122 | 10 | $this->loadCacheInformation($table); |
|
123 | 7 | $pks = self::$localCache[$this->schemaSignature]['tables'][$table]['primary_keys']; |
|
124 | 7 | if (count($pks) == 0) { |
|
125 | 3 | throw new Exception\NoPrimaryKeyException(__METHOD__ . ". No primary keys found on table '{$this->schemaSignature}'.'$table'."); |
|
126 | } |
||
127 | |||
128 | 5 | return $pks; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | 2 | public function getColumnsInformation($table) |
|
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | 1 | public function getForeignKeys($table) |
|
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | 1 | public function getReferences($table) |
|
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | 3 | public function getTablesInformation() |
|
170 | |||
171 | /** |
||
172 | * Get a table configuration. |
||
173 | * |
||
174 | * @throws Exception\ErrorException |
||
175 | * @throws Exception\TableNotFoundException |
||
176 | * |
||
177 | * @param string $table table name |
||
178 | * @param bool|null $include_options include extended information |
||
179 | * |
||
180 | * @return ArrayObject |
||
181 | */ |
||
182 | 14 | protected function getTableConfig($table, $include_options = null) |
|
210 | |||
211 | /** |
||
212 | * Get schema configuration. |
||
213 | * |
||
214 | * @throws Exception\ErrorException |
||
215 | * @throws Exception\SchemaNotFoundException |
||
216 | * |
||
217 | * @param bool|null $include_options include extended information |
||
218 | * |
||
219 | * @return ArrayObject |
||
220 | */ |
||
221 | 5 | public function getSchemaConfig($include_options = null) |
|
242 | |||
243 | /** |
||
244 | * @param string $table |
||
245 | * |
||
246 | * @throws Exception\InvalidArgumentException |
||
247 | * @throws Exception\TableNotFoundException |
||
248 | */ |
||
249 | 17 | protected function loadCacheInformation($table = null) |
|
265 | |||
266 | /** |
||
267 | * Clear local cache information for the current schema. |
||
268 | * |
||
269 | * @throws Exception\InvalidArgumentException |
||
270 | */ |
||
271 | 1 | public function clearCacheInformation() |
|
281 | } |
||
282 |