Completed
Push — master ( 335443...b386df )
by Sébastien
04:58
created

AbstractMetadataReader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 6
c 6
b 0
f 2
lcom 1
cbo 0
dl 0
loc 89
ccs 24
cts 24
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setStaticCache() 0 5 1
A getColumnsMetadata() 0 13 3
readColumnsMetadata() 0 1 ?
B getEmptyQuery() 0 27 2
1
<?php
2
3
namespace Soluble\Metadata\Reader;
4
5
use Soluble\Metadata\ColumnsMetadata;
6
use Soluble\Db\Metadata\Column\Exception\UnsupportedDatatypeException;
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 10
    public function getColumnsMetadata($sql)
38
    {
39 10
        if ($this->cache_active) {
40 9
            $cache_key = md5($sql);
41 9
            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 4
            return static::$metadata_cache[$cache_key];
46
        } else {
47 2
            return $this->readColumnsMetadata($sql);
48
        }
49 1
    }
50
51
    /**
52
     * Read metadata information from source
53
     *
54
     * @throws UnsupportedDatatypeException
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 getEmptyQuery($sql)
70
    {
71
        // see the reason why in Vision_Store_Adapter_ZendDbSelect::getMetatData
72
        //$sql = str_replace("('__innerselect'='__innerselect')", '(1=0)', $sql);
73
74
75 9
        $sql = preg_replace('/(\r\n|\r|\n|\t)+/', " ", strtolower($sql));
76 9
        $sql = trim($sql);
77 9
        $sql = preg_replace('/\s+/', ' ', $sql);
78
79
80
81 9
        $replace_regexp = "LIMIT[\s]+[\d]+((\s*,\s*\d+)|(\s+OFFSET\s+\d+)){0,1}";
82
83 9
        $search_regexp = "$replace_regexp";
84 9
        if (!preg_match("/$search_regexp/i", $sql)) {
85
            // Limit is not already present
86 9
            $sql .= " LIMIT 0";
87 9
        } else {
88
            // replace first if offset exists, then if not
89
            //preg_match_all("/($search_regexp)/i", $sql, $matches, PREG_PATTERN_ORDER);
90
            //var_dump($matches);
91
92 1
            $sql = preg_replace("/($replace_regexp)/i", "LIMIT 0", $sql);
93
        }
94 9
        return $sql;
95
    }
96
}
97