1 | <?php |
||
8 | abstract class AbstractMetadataReader |
||
9 | { |
||
10 | |||
11 | /** |
||
12 | * Keep static cache in memory |
||
13 | * @var boolean |
||
14 | */ |
||
15 | protected $cache_active = true; |
||
16 | |||
17 | /** |
||
18 | * |
||
19 | * @param boolean $active |
||
20 | * @return AbstractMetadataReader |
||
21 | */ |
||
22 | 1 | public function setStaticCache($active = true) |
|
27 | |||
28 | /** |
||
29 | * Return columns metadata from query |
||
30 | * |
||
31 | * @throws UnsupportedDatatypeException |
||
32 | * @throws Exception\AmbiguousColumnException |
||
33 | * |
||
34 | * @param string $sql |
||
35 | * @return ColumnsMetadata |
||
36 | */ |
||
37 | 15 | public function getColumnsMetadata($sql) |
|
38 | { |
||
39 | 15 | if ($this->cache_active) { |
|
40 | 14 | $cache_key = md5($sql); |
|
41 | 14 | if (!array_key_exists($cache_key, static::$metadata_cache)) { |
|
42 | 9 | $md = $this->readColumnsMetadata($sql); |
|
43 | 4 | static::$metadata_cache[$cache_key] = $md; |
|
44 | 4 | } |
|
45 | 9 | return static::$metadata_cache[$cache_key]; |
|
46 | 1 | } else { |
|
47 | 1 | return $this->readColumnsMetadata($sql); |
|
48 | 1 | } |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * Read metadata information from source |
||
53 | * |
||
54 | * @throws Exception\UnsupportedTypeException |
||
55 | * @throws Exception\AmbiguousColumnException |
||
56 | * |
||
57 | * @param string $sql |
||
58 | * @return ColumnsMetadata |
||
59 | */ |
||
60 | abstract protected function readColumnsMetadata($sql); |
||
61 | |||
62 | /** |
||
63 | * Optimization, will add false condition to the query |
||
64 | * so the metadata loading will be faster |
||
65 | * |
||
66 | * @param string $sql query string |
||
67 | * @return string |
||
68 | */ |
||
69 | 9 | protected function getEmptiedQuery($sql) |
|
93 | } |
||
94 |