|
1
|
|
|
<?php |
|
2
|
|
|
namespace Wandu\Database\Connection; |
|
3
|
|
|
|
|
4
|
|
|
use Exception; |
|
5
|
|
|
use PDO; |
|
6
|
|
|
use PDOStatement; |
|
7
|
|
|
use Throwable; |
|
8
|
|
|
use Wandu\Collection\ArrayList; |
|
9
|
|
|
use Wandu\Database\Configuration; |
|
10
|
|
|
use Wandu\Database\Contracts\ConnectionInterface; |
|
11
|
|
|
use Wandu\Database\Contracts\QueryInterface; |
|
12
|
|
|
|
|
13
|
|
|
class MysqlConnection implements ConnectionInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var \PDO */ |
|
16
|
|
|
protected $pdo; |
|
17
|
|
|
|
|
18
|
|
|
/** @var \Wandu\Database\Configuration */ |
|
19
|
|
|
protected $config; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param \Wandu\Database\Configuration $config |
|
23
|
|
|
*/ |
|
24
|
16 |
|
public function __construct(Configuration $config) |
|
25
|
|
|
{ |
|
26
|
16 |
|
$this->config = $config; |
|
27
|
16 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
16 |
|
public function connect() |
|
33
|
|
|
{ |
|
34
|
16 |
|
if (!$this->pdo) { |
|
35
|
16 |
|
$this->pdo = $this->config->createPdo(); |
|
36
|
|
|
} |
|
37
|
16 |
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// /** |
|
|
|
|
|
|
41
|
|
|
// * {@inheritdoc} |
|
42
|
|
|
// */ |
|
43
|
|
|
// public function createQueryBuilder($table) |
|
44
|
|
|
// { |
|
45
|
|
|
// return new QueryBuilder($this->getPrefix() . $table); |
|
46
|
|
|
// } |
|
47
|
|
|
// |
|
48
|
|
|
// /** |
|
49
|
|
|
// * {@inheritdoc} |
|
50
|
|
|
// */ |
|
51
|
|
|
// public function createRepository($className) |
|
52
|
|
|
// { |
|
53
|
|
|
// if (!$this->container || !$this->container[Reader::class]) { |
|
54
|
|
|
// throw new ClassNotFoundException(Reader::class); |
|
55
|
|
|
// } |
|
56
|
|
|
// return new Repository($this, RepositorySettings::fromAnnotation($className, $this->container[Reader::class])); |
|
57
|
|
|
// } |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
6 |
|
public function fetch($query, array $bindings = []) |
|
63
|
|
|
{ |
|
64
|
6 |
|
$statement = $this->prepare($query, $bindings); |
|
65
|
6 |
|
$statement->execute(); |
|
66
|
6 |
|
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) { |
|
67
|
6 |
|
yield $row; |
|
68
|
|
|
} |
|
69
|
6 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
public function all($query, array $bindings = []) |
|
75
|
|
|
{ |
|
76
|
|
|
return new ArrayList($this->fetch($query, $bindings)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
8 |
|
public function first($query, array $bindings = []) |
|
83
|
|
|
{ |
|
84
|
8 |
|
$statement = $this->prepare($query, $bindings); |
|
85
|
8 |
|
$statement->execute(); |
|
86
|
8 |
|
$attributes = $statement->fetch(PDO::FETCH_ASSOC); |
|
87
|
8 |
|
return $attributes ?: null; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
2 |
|
public function query($query, array $bindings = []) |
|
94
|
|
|
{ |
|
95
|
2 |
|
$statement = $this->prepare($query, $bindings); |
|
96
|
2 |
|
$statement->execute(); |
|
97
|
2 |
|
return $statement->rowCount(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function getLastInsertId() |
|
104
|
|
|
{ |
|
105
|
1 |
|
return $this->pdo->lastInsertId(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function transaction(callable $handler) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->pdo->beginTransaction(); |
|
114
|
|
|
try { |
|
115
|
|
|
call_user_func($handler, $this); |
|
116
|
|
|
} catch (Exception $e) { |
|
117
|
|
|
$this->pdo->rollBack(); |
|
118
|
|
|
throw $e; |
|
119
|
|
|
} catch (Throwable $e) { |
|
120
|
|
|
$this->pdo->rollBack(); |
|
121
|
|
|
throw $e; |
|
122
|
|
|
} |
|
123
|
|
|
$this->pdo->commit(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
|
128
|
|
|
* @param array $bindings |
|
129
|
|
|
* @return \PDOStatement |
|
130
|
|
|
*/ |
|
131
|
14 |
|
protected function prepare($query, array $bindings = []) |
|
132
|
|
|
{ |
|
133
|
14 |
|
while (is_callable($query)) { |
|
134
|
2 |
|
$query = call_user_func($query); |
|
135
|
|
|
} |
|
136
|
14 |
|
if ($query instanceof QueryInterface) { |
|
137
|
8 |
|
$bindings = $query->getBindings(); |
|
138
|
8 |
|
$query = $query->toSql(); |
|
139
|
|
|
} |
|
140
|
14 |
|
$statement = $this->pdo->prepare($query); |
|
141
|
14 |
|
$this->bindValues($statement, $bindings); |
|
142
|
14 |
|
return $statement; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param \PDOStatement $statement |
|
147
|
|
|
* @param array $bindings |
|
148
|
|
|
*/ |
|
149
|
14 |
|
protected function bindValues(PDOStatement $statement, array $bindings = []) |
|
150
|
|
|
{ |
|
151
|
14 |
|
foreach ($bindings as $key => $value) { |
|
152
|
14 |
|
if (is_int($value)) { |
|
153
|
7 |
|
$dataType = PDO::PARAM_INT; |
|
154
|
12 |
|
} elseif (is_bool($value)) { |
|
155
|
|
|
$dataType = PDO::PARAM_BOOL; |
|
156
|
12 |
|
} elseif (is_null($value)) { |
|
157
|
|
|
$dataType = PDO::PARAM_NULL; |
|
158
|
|
|
} else { |
|
159
|
12 |
|
$dataType = PDO::PARAM_STR; |
|
160
|
|
|
} |
|
161
|
14 |
|
$statement->bindValue( |
|
162
|
14 |
|
is_int($key) ? $key + 1 : $key, |
|
163
|
|
|
$value, |
|
164
|
|
|
$dataType |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
14 |
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.