1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soluble\DbWrapper\Adapter; |
4
|
|
|
|
5
|
|
|
use Soluble\DbWrapper\Exception; |
6
|
|
|
use mysqli; |
7
|
|
|
use Soluble\DbWrapper\Result\Resultset; |
8
|
|
|
use Soluble\DbWrapper\Connection\MysqliConnection; |
9
|
|
|
|
10
|
|
|
class MysqliAdapter implements AdapterInterface |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
* @var \mysqli |
16
|
|
|
*/ |
17
|
|
|
protected $resource; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
* @var MysqliConnection |
22
|
|
|
*/ |
23
|
|
|
protected $connection; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor |
28
|
|
|
* |
29
|
|
|
* @param mysqli $resource |
30
|
|
|
*/ |
31
|
12 |
|
public function __construct(mysqli $resource) |
32
|
|
|
{ |
33
|
12 |
|
$this->resource = $resource; |
34
|
12 |
|
$this->connection = new MysqliConnection($this, $resource); |
35
|
12 |
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
2 |
|
public function quoteValue($value) |
42
|
|
|
{ |
43
|
2 |
|
return "'" . $this->resource->real_escape_string($value) . "'"; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
7 |
|
public function query($query) |
51
|
|
|
{ |
52
|
|
|
try { |
53
|
|
|
|
54
|
|
|
//$r = $this->resource->query($query, MYSQLI_STORE_RESULT); |
55
|
7 |
|
$r = $this->resource->query($query, MYSQLI_STORE_RESULT); |
56
|
|
|
|
57
|
7 |
|
$results = new Resultset(); |
58
|
|
|
|
59
|
7 |
|
if ($r === false) { |
60
|
4 |
|
throw new Exception\InvalidArgumentException("Query cannot be executed [$query]."); |
61
|
7 |
|
} elseif ($r !== true && !$r instanceof \mysqli_result) { |
62
|
|
|
throw new Exception\InvalidArgumentException("Query didn't return any result [$query]."); |
63
|
7 |
|
} elseif ($r instanceof \mysqli_result) { |
64
|
5 |
|
while ($row = $r->fetch_assoc()) { |
65
|
5 |
|
$results->append($row); |
66
|
5 |
|
} |
67
|
5 |
|
$r->close(); |
68
|
5 |
|
} |
69
|
7 |
|
} catch (Exception\InvalidArgumentException $e) { |
70
|
4 |
|
throw $e; |
71
|
|
|
} catch (\Exception $e) { |
72
|
|
|
$msg = "MysqliException: {$e->getMessage()} [$query]"; |
73
|
|
|
throw new Exception\InvalidArgumentException($msg); |
74
|
|
|
} |
75
|
7 |
|
return $results; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
* @return MysqlConnection |
81
|
|
|
*/ |
82
|
5 |
|
public function getConnection() |
83
|
|
|
{ |
84
|
5 |
|
return $this->connection; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|