|
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
|
|
|
* @var \mysqli |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $resource; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var MysqliConnection |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $connection; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param mysqli $resource |
|
26
|
|
|
*/ |
|
27
|
13 |
|
public function __construct(mysqli $resource) |
|
28
|
|
|
{ |
|
29
|
13 |
|
$this->resource = $resource; |
|
30
|
13 |
|
$this->connection = new MysqliConnection($this, $resource); |
|
31
|
13 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
2 |
|
public function quoteValue($value) |
|
37
|
|
|
{ |
|
38
|
2 |
|
return "'" . $this->resource->real_escape_string($value) . "'"; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
8 |
|
public function query($query, $resultsetType = Resultset::TYPE_ARRAY) |
|
45
|
|
|
{ |
|
46
|
|
|
try { |
|
47
|
|
|
//$r = $this->resource->query($query, MYSQLI_STORE_RESULT); |
|
48
|
8 |
|
$r = $this->resource->query($query, MYSQLI_STORE_RESULT); |
|
49
|
|
|
|
|
50
|
8 |
|
$results = new Resultset($resultsetType); |
|
51
|
|
|
|
|
52
|
8 |
|
if ($r === false) { |
|
53
|
4 |
|
throw new Exception\InvalidArgumentException("Query cannot be executed [$query]."); |
|
54
|
8 |
|
} elseif ($r instanceof \mysqli_result) { |
|
55
|
6 |
|
while ($row = $r->fetch_assoc()) { |
|
56
|
6 |
|
$results->append($row); |
|
57
|
|
|
} |
|
58
|
8 |
|
$r->close(); |
|
59
|
|
|
} |
|
60
|
4 |
|
} catch (Exception\InvalidArgumentException $e) { |
|
61
|
4 |
|
throw $e; |
|
62
|
|
|
} catch (\Exception $e) { |
|
63
|
|
|
$msg = "MysqliException: {$e->getMessage()} [$query]"; |
|
64
|
|
|
throw new Exception\InvalidArgumentException($msg); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
8 |
|
return $results; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
* |
|
73
|
|
|
* @return MysqlConnection |
|
74
|
|
|
*/ |
|
75
|
5 |
|
public function getConnection() |
|
76
|
|
|
{ |
|
77
|
5 |
|
return $this->connection; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|