1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Database; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
use PDOException; |
7
|
|
|
use PDOStatement; |
8
|
|
|
use Win\Mvc\Application; |
9
|
|
|
use Win\Singleton\SingletonTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Conexão com banco de dados |
13
|
|
|
*/ |
14
|
|
|
abstract class Connection |
15
|
|
|
{ |
16
|
|
|
use SingletonTrait; |
17
|
|
|
|
18
|
|
|
/** @var PDO */ |
19
|
|
|
protected $pdo; |
20
|
|
|
|
21
|
|
|
/** @var PDOException|null */ |
22
|
|
|
protected $pdoException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Cria e retorna conexão PDO |
26
|
|
|
* @param string[] $dbConfig |
27
|
|
|
* @throws PDOException |
28
|
|
|
* @return PDO |
29
|
|
|
*/ |
30
|
|
|
abstract protected function createPdo(&$dbConfig); |
31
|
|
|
|
32
|
|
|
/** @return PDO */ |
33
|
|
|
public function getPdo() |
34
|
|
|
{ |
35
|
|
|
return $this->pdo; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Cria uma conexão com um banco de dados |
40
|
|
|
* @param string[] $dbConfig |
41
|
|
|
*/ |
42
|
|
|
public function connect($dbConfig) |
43
|
|
|
{ |
44
|
|
|
try { |
45
|
|
|
$this->pdo = $this->createPdo($dbConfig); |
46
|
|
|
$this->pdo->exec('set names utf8'); |
47
|
|
|
$this->pdoException = null; |
48
|
|
|
Orm::setConnection($this); |
49
|
|
|
} catch (PDOException $ex) { |
50
|
|
|
$this->pdoException = $ex; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Retorna TRUE caso a conexão tenha sido bem sucedida |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function isValid() |
59
|
|
|
{ |
60
|
|
|
return is_null($this->pdoException) && $this->pdo instanceof \PDO; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Redireciona para 503 caso a conexão tenha falhado |
65
|
|
|
*/ |
66
|
|
|
public function validate() |
67
|
|
|
{ |
68
|
|
|
if (!is_null($this->pdoException)) { |
69
|
|
|
Application::app()->errorPage(503, $this->pdoException->getMessage()); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $query |
75
|
|
|
* @param string $values |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function query($query, $values = []) |
79
|
|
|
{ |
80
|
|
|
$stmt = $this->pdo->prepare($query); |
81
|
|
|
|
82
|
|
|
return $stmt->execute($values); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $query |
87
|
|
|
* @param string $values |
88
|
|
|
* @return PDOStatement|null |
89
|
|
|
*/ |
90
|
|
|
public function stmt($query, $values = []) |
91
|
|
|
{ |
92
|
|
|
$stmt = $this->pdo->prepare($query); |
93
|
|
|
$stmt->execute($values); |
|
|
|
|
94
|
|
|
|
95
|
|
|
return $stmt; |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $query |
100
|
|
|
* @param string $values |
101
|
|
|
* @return mixed[] |
102
|
|
|
*/ |
103
|
|
|
public function fetchAll($query, $values = []) |
104
|
|
|
{ |
105
|
|
|
$stmt = $this->stmt($query, $values); |
|
|
|
|
106
|
|
|
|
107
|
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $query |
112
|
|
|
* @param mixed[] $values |
113
|
|
|
* @return int |
114
|
|
|
*/ |
115
|
|
|
public function fetchCount($query, $values) |
116
|
|
|
{ |
117
|
|
|
$stmt = $this->stmt($query, $values); |
|
|
|
|
118
|
|
|
|
119
|
|
|
return $stmt->fetchColumn(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** @return string */ |
123
|
|
|
public function getLastInsertId() |
124
|
|
|
{ |
125
|
|
|
return $this->pdo->lastInsertId(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|