|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Soluble\Metadata\Reader; |
|
4
|
|
|
|
|
5
|
|
|
use Soluble\Metadata\ColumnsMetadata; |
|
6
|
|
|
use Soluble\Metadata\Exception; |
|
7
|
|
|
|
|
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) |
|
23
|
|
|
{ |
|
24
|
1 |
|
$this->cache_active = $active; |
|
25
|
1 |
|
return $this; |
|
26
|
|
|
} |
|
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) |
|
70
|
|
|
{ |
|
71
|
|
|
// see the reason why in Vision_Store_Adapter_ZendDbSelect::getMetatData |
|
72
|
|
|
//$sql = str_replace("('__innerselect'='__innerselect')", '(1=0)', $sql); |
|
73
|
|
|
|
|
74
|
9 |
|
$sql = preg_replace('/(\r\n|\r|\n|\t)+/', " ", strtolower($sql)); |
|
75
|
9 |
|
$sql = trim($sql); |
|
76
|
9 |
|
$sql = preg_replace('/\s+/', ' ', $sql); |
|
77
|
|
|
|
|
78
|
9 |
|
$replace_regexp = "LIMIT[\s]+[\d]+((\s*,\s*\d+)|(\s+OFFSET\s+\d+)){0,1}"; |
|
79
|
|
|
|
|
80
|
9 |
|
$search_regexp = "$replace_regexp"; |
|
81
|
9 |
|
if (!preg_match("/$search_regexp/i", $sql)) { |
|
82
|
|
|
// Limit is not already present |
|
83
|
9 |
|
$sql .= " LIMIT 0"; |
|
84
|
9 |
|
} else { |
|
85
|
|
|
// replace first if offset exists, then if not |
|
86
|
|
|
//preg_match_all("/($search_regexp)/i", $sql, $matches, PREG_PATTERN_ORDER); |
|
87
|
|
|
//var_dump($matches); |
|
88
|
|
|
|
|
89
|
1 |
|
$sql = preg_replace("/($replace_regexp)/i", "LIMIT 0", $sql); |
|
90
|
|
|
} |
|
91
|
9 |
|
return $sql; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|