Completed
Push — master ( 5db684...7fb55c )
by Sébastien
02:10
created

MysqlConnectionAdapter::queryMysqli()   C

Complexity

Conditions 8
Paths 17

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 10.3697

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 25
ccs 12
cts 18
cp 0.6667
rs 5.3846
cc 8
eloc 17
nc 17
nop 2
crap 10.3697
1
<?php
2
3
namespace Soluble\Schema\Source\Mysql;
4
5
/**
6
 * Wrapper for basic mysqli/pdo usage
7
 */
8
9
use Soluble\Schema\Exception;
10
use ArrayObject;
11
12
class MysqlConnectionAdapter
13
{
14
15
    const DRIVER_TYPE_PDO = 'pdo';
16
    const DRIVER_TYPE_MYSQLI = 'mysqli';
17
18
    /**
19
     *
20
     * @var string
21
     */
22
    protected $driver;
23
24
    /**
25
     *
26
     * @var \mysqli|null
27
     */
28
    protected $mysqli;
29
30
    /**
31
     *
32
     * @var \PDO|null
33
     */
34
    protected $pdo;
35
36
    /**
37
     *
38
     * @var string
39
     */
40
    protected $type;
41
    
42
    /**
43
     *
44
     * @throws Exception\InvalidArgumentException
45
     * @param \mysqli|\PDO $conn
46
     */
47 26
    public function __construct($conn)
48
    {
49 26
        if ($conn instanceof \mysqli) {
50 4
            $this->mysqli = $conn;
51 4
            $this->type = self::DRIVER_TYPE_MYSQLI;
52 26
        } elseif ($conn instanceof \PDO && $conn->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'mysql') {
53 25
            $this->pdo = $conn;
54 25
            $this->type = self::DRIVER_TYPE_PDO;
55 25
        } else {
56
            $msg = "MysqlConnectionAdapter requires connection to be either 'pdo:mysql' or 'mysqli'";
57
            throw new Exception\InvalidArgumentException($msg);
58
        }
59 26
    }
60
61
    /**
62
     * Return current schema name
63
     * @return string|false
64
     */
65 26
    public function getCurrentSchema()
66
    {
67 26
        $query = 'SELECT DATABASE() as current_schema';
68 26
        $results = $this->query($query);
69 26
        if (count($results) == 0) {
70
            return false;
71
        }
72 26
        return $results[0]['current_schema'];
73
    }
74
75
    /**
76
     *
77
     * @param string $value
78
     * @return string
79
     */
80 6
    public function quoteValue($value)
81
    {
82 6
        if ($this->type == self::DRIVER_TYPE_MYSQLI) {
83
            $quoted = "'" . $this->mysqli->real_escape_string($value) . "'";
84
        } else {
85 6
            $quoted = $this->pdo->quote($value);
86
        }
87 6
        return $quoted;
88
    }
89
90
    /**
91
     * Execute query and return query as an ArrayObject
92
     *
93
     * @param string $query
94
     * @return ArrayObject
95
     */
96 26
    public function query($query)
97
    {
98 26
        if ($this->type == self::DRIVER_TYPE_MYSQLI) {
99 4
            $results = $this->queryMysqli($query);
100 4
        } else {
101 25
            $results = $this->queryPDO($query);
102
        }
103 26
        return $results;
104
    }
105
    
106
    /**
107
     * Execute special sql like set names...
108
     * @param string $query
109
     * @return void
110
     */
111 6
    public function execute($query)
112
    {
113 6
        if ($this->type == self::DRIVER_TYPE_MYSQLI) {
114
            $this->queryMysqli($query, false);
115
        } else {
116 6
            $this->executePDO($query);
117
        }
118 6
    }
119
120
    /**
121
     *
122
     * @param string $query
123
     * @return void
124
     */
125 6
    protected function executePDO($query)
126
    {
127
        try {
128 6
            $this->pdo->exec($query);
129 6
        } catch (\Exception $e) {
130
            $msg = "PDOException : {$e->getMessage()} [$query]";
131
            throw new Exception\InvalidArgumentException($msg);
132
        }
133 6
    }
134
    
135
    
136
    /**
137
     *
138
     * @param string $query
139
     * @return ArrayObject
140
     */
141 25
    protected function queryPDO($query)
142
    {
143
        try {
144 25
            $stmt = $this->pdo->query($query, \PDO::FETCH_ASSOC);
145 25
            if (!$stmt) {
146
                throw new Exception\InvalidArgumentException("Query cannot be executed [$query].");
147
            }
148 25
            $results = new ArrayObject();
149 25
            foreach ($stmt as $row) {
150 25
                $results->append($row);
151 25
            }
152 25
        } catch (Exception\InvalidArgumentException $e) {
153
            throw $e;
154
        } catch (\Exception $e) {
155
            $msg = "PDOException : {$e->getMessage()} [$query]";
156
            throw new Exception\InvalidArgumentException($msg);
157
        }
158 25
        return $results;
159
    }
160
161
    /**
162
     *
163
     * @param string $query
164
     * @param boolean $throw_exception_if_empty if empty result (like set command...)
165
     * @return ArrayObject
166
     */
167 4
    protected function queryMysqli($query, $throw_exception_if_empty=true)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$throw_exception_if_empty" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$throw_exception_if_empty"; expected 1 but found 0
Loading history...
168
    {
169
        try {
170 4
            $r = $this->mysqli->query($query);
171
            
172 4
            $results = new ArrayObject();
173
            
174 4
            if (!$r) {
175
                throw new Exception\InvalidArgumentException("Query cannot be executed [$query].");
176 4
            } elseif ($throw_exception_if_empty && !$r instanceof \mysqli_result) {
177
                throw new Exception\InvalidArgumentException("Query didn't return any result [$query].");
178 4
            } elseif($r instanceof \mysqli_result)  {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ELSEIF keyword; 0 found
Loading history...
179 4
                while ($row = $r->fetch_assoc()) {
180 4
                    $results->append($row);
181 4
                }
182 4
            }
183
            
184 4
        } catch (Exception\InvalidArgumentException $e) {
185
            throw $e;
186
        } catch (\Exception $e) {
187
            $msg = "MysqliException: {$e->getMessage()} [$query]";
188
            throw new Exception\InvalidArgumentException($msg);
189
        }
190 4
        return $results;
191
    }
192
}
193