1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Database\Connection; |
3
|
|
|
|
4
|
|
|
use Exception; |
5
|
|
|
use PDO; |
6
|
|
|
use PDOStatement; |
7
|
|
|
use Throwable; |
8
|
|
|
use Wandu\Database\Contracts\Connection; |
9
|
|
|
use Wandu\Database\Events\ExecuteQuery; |
10
|
|
|
use Wandu\Event\Contracts\EventEmitter; |
11
|
|
|
|
12
|
|
|
class MysqlConnection implements Connection |
13
|
|
|
{ |
14
|
|
|
/** @var \PDO */ |
15
|
|
|
protected $pdo; |
16
|
|
|
|
17
|
|
|
/** @var \Wandu\Event\Contracts\EventEmitter */ |
18
|
|
|
protected $emitter; |
19
|
|
|
|
20
|
18 |
|
public function __construct(PDO $pdo) |
21
|
|
|
{ |
22
|
18 |
|
$this->pdo = $pdo; |
23
|
18 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
17 |
|
public function setEventEmitter(EventEmitter $emitter) |
29
|
|
|
{ |
30
|
17 |
|
$this->emitter = $emitter; |
31
|
17 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
8 |
|
public function fetch(string $query, array $bindings = []) |
37
|
|
|
{ |
38
|
8 |
|
$statement = $this->execute($query, $bindings); |
39
|
8 |
|
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) { |
40
|
8 |
|
yield $row; |
41
|
|
|
} |
42
|
8 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
11 |
|
public function first(string $query, array $bindings = []) |
48
|
|
|
{ |
49
|
11 |
|
$attributes = $this->execute($query, $bindings)->fetch(PDO::FETCH_ASSOC); |
50
|
11 |
|
return $attributes ?: null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
2 |
|
public function query(string $query, array $bindings = []) |
57
|
|
|
{ |
58
|
2 |
|
return $this->execute($query, $bindings)->rowCount(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
1 |
|
public function getLastInsertId() |
65
|
|
|
{ |
66
|
1 |
|
return $this->pdo->lastInsertId(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function transaction(callable $handler) |
73
|
|
|
{ |
74
|
|
|
$this->pdo->beginTransaction(); |
75
|
|
|
try { |
76
|
|
|
$result = call_user_func($handler, $this); |
77
|
|
|
} catch (Exception $e) { |
78
|
|
|
$this->pdo->rollBack(); |
79
|
|
|
throw $e; |
80
|
|
|
} catch (Throwable $e) { |
81
|
|
|
$this->pdo->rollBack(); |
82
|
|
|
throw $e; |
83
|
|
|
} |
84
|
|
|
$this->pdo->commit(); |
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $query |
90
|
|
|
* @param array $bindings |
91
|
|
|
* @return \PDOStatement |
92
|
|
|
*/ |
93
|
17 |
|
protected function execute(string $query, array $bindings = []) |
94
|
|
|
{ |
95
|
17 |
|
$statement = $this->pdo->prepare($query); |
96
|
17 |
|
$this->bindValues($statement, $bindings); |
97
|
17 |
|
$statement->execute(); |
98
|
17 |
|
if ($this->emitter) { |
99
|
17 |
|
$this->emitter->trigger(new ExecuteQuery($statement->queryString, $bindings)); |
100
|
|
|
} |
101
|
17 |
|
return $statement; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param \PDOStatement $statement |
106
|
|
|
* @param array $bindings |
107
|
|
|
*/ |
108
|
17 |
|
protected function bindValues(PDOStatement $statement, array $bindings = []) |
109
|
|
|
{ |
110
|
17 |
|
foreach ($bindings as $key => $value) { |
111
|
17 |
|
if (is_int($value)) { |
112
|
10 |
|
$dataType = PDO::PARAM_INT; |
113
|
12 |
|
} elseif (is_bool($value)) { |
114
|
|
|
$dataType = PDO::PARAM_BOOL; |
115
|
12 |
|
} elseif (is_null($value)) { |
116
|
|
|
$dataType = PDO::PARAM_NULL; |
117
|
|
|
} else { |
118
|
12 |
|
$dataType = PDO::PARAM_STR; |
119
|
|
|
} |
120
|
17 |
|
$statement->bindValue( |
121
|
17 |
|
is_int($key) ? $key + 1 : $key, |
122
|
17 |
|
$value, |
123
|
17 |
|
$dataType |
124
|
|
|
); |
125
|
|
|
} |
126
|
17 |
|
} |
127
|
|
|
} |
128
|
|
|
|