Completed
Push — master ( 165c4c...1d39c6 )
by Sébastien
06:32
created

MysqlConnectionAdapter::query()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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