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

MysqlConnectionAdapter::executePDO()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.432

Importance

Changes 6
Bugs 2 Features 0
Metric Value
c 6
b 2
f 0
dl 0
loc 14
ccs 7
cts 10
cp 0.7
rs 9.2
cc 4
eloc 10
nc 5
nop 1
crap 4.432
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 33
    public function __construct($conn)
48
    {
49 33
        if ($conn instanceof \mysqli) {
50 8
            $this->mysqli = $conn;
51 8
            $this->type = self::DRIVER_TYPE_MYSQLI;
52 33
        } elseif ($conn instanceof \PDO && $conn->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'mysql') {
53 28
            $this->pdo = $conn;
54 28
            $this->type = self::DRIVER_TYPE_PDO;
55 28
        } else {
56 1
            $msg = "MysqlConnectionAdapter requires connection to be either 'pdo:mysql' or 'mysqli'";
57 1
            throw new Exception\InvalidArgumentException($msg);
58
        }
59 33
    }
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 || $results[0]['current_schema'] === null) {
70 2
            return false;
71
        }
72 26
        return $results[0]['current_schema'];
73
    }
74
75
    /**
76
     *
77
     * @param string $value
78
     * @return string
79
     */
80 8
    public function quoteValue($value)
81
    {
82 8
        if ($this->type == self::DRIVER_TYPE_MYSQLI) {
83 1
            $quoted = "'" . $this->mysqli->real_escape_string($value) . "'";
84 1
        } else {
85 7
            $quoted = $this->pdo->quote($value);
86
        }
87 8
        return $quoted;
88
    }
89
90
    /**
91
     * Execute query and return query as an ArrayObject
92
     *
93
     * @param string $query
94
     * @return ArrayObject
95
     */
96 28
    public function query($query)
97
    {
98 28
        if ($this->type == self::DRIVER_TYPE_MYSQLI) {
99 5
            $results = $this->queryMysqli($query);
100 5
        } else {
101 26
            $results = $this->queryPDO($query);
102
        }
103 28
        return $results;
104
    }
105
106
    /**
107
     * Execute special sql like set names...
108
     * @param string $query
109
     * @return void
110
     */
111 10
    public function execute($query)
112
    {
113 10
        if ($this->type == self::DRIVER_TYPE_MYSQLI) {
114 2
            $this->queryMysqli($query);
115 2
        } else {
116 8
            $this->executePDO($query);
117
        }
118 10
    }
119
120
    /**
121
     *
122
     * @param string $query
123
     * @return void
124
     */
125 8
    protected function executePDO($query)
126
    {
127
        try {
128 8
            $ret = $this->pdo->exec($query);
129 8
            if ($ret === false) {
130 1
                throw new Exception\InvalidArgumentException("Cannot execute [$query].");
131
            }
132 8
        } catch (Exception\InvalidArgumentException $e) {
133 1
            throw $e;
134
        } catch (\Exception $e) {
135
            $msg = "PDOException : {$e->getMessage()} [$query]";
136
            throw new Exception\InvalidArgumentException($msg);
137
        }
138 8
    }
139
140
141
    /**
142
     *
143
     * @param string $query
144
     * @return ArrayObject
145
     */
146 26
    protected function queryPDO($query)
147
    {
148
        try {
149 26
            $stmt = $this->pdo->query($query, \PDO::FETCH_ASSOC);
150 26
            if ($stmt === false) {
151 1
                throw new Exception\InvalidArgumentException("Query cannot be executed [$query].");
152
            }
153 26
            $results = new ArrayObject();
154 26
            foreach ($stmt as $row) {
155 26
                $results->append($row);
156 26
            }
157 26
        } catch (Exception\InvalidArgumentException $e) {
158 1
            throw $e;
159
        } catch (\Exception $e) {
160
            $msg = "PDOException : {$e->getMessage()} [$query]";
161
            throw new Exception\InvalidArgumentException($msg);
162
        }
163 26
        return $results;
164
    }
165
166
    /**
167
     *
168
     * @param string $query
169
     * @return ArrayObject
170
     */
171 6
    protected function queryMysqli($query)
172
    {
173
        try {
174 6
            $r = $this->mysqli->query($query);
175
176 6
            $results = new ArrayObject();
177
178 6
            if ($r === false) {
179 2
                throw new Exception\InvalidArgumentException("Query cannot be executed [$query].");
180 6
            } elseif ($r !== true && !$r instanceof \mysqli_result) {
181
                throw new Exception\InvalidArgumentException("Query didn't return any result [$query].");
182 6
            } elseif ($r instanceof \mysqli_result) {
183 5
                while ($row = $r->fetch_assoc()) {
184 5
                    $results->append($row);
185 5
                }
186 5
            }
187
188 6
        } catch (Exception\InvalidArgumentException $e) {
189 2
            throw $e;
190
        } catch (\Exception $e) {
191
            $msg = "MysqliException: {$e->getMessage()} [$query]";
192
            throw new Exception\InvalidArgumentException($msg);
193
        }
194 6
        return $results;
195
    }
196
}
197