1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Repositories\Database; |
4
|
|
|
|
5
|
|
|
use App\Models\User\Admin; |
|
|
|
|
6
|
|
|
use PDO; |
7
|
|
|
use PDOException; |
8
|
|
|
use PDOStatement; |
9
|
|
|
use Win\Common\Traits\SingletonTrait; |
10
|
|
|
use Win\HttpException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Conexão com banco de dados |
14
|
|
|
*/ |
15
|
|
|
abstract class Connection |
16
|
|
|
{ |
17
|
|
|
/** @var PDO */ |
18
|
|
|
protected $pdo; |
19
|
|
|
|
20
|
|
|
/** @var PDOException */ |
21
|
|
|
public $pdoException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Cria e retorna conexão PDO |
25
|
|
|
* @param string[] $db Configuração de Conexão |
26
|
|
|
* @throws PDOException |
27
|
|
|
* @return PDO |
28
|
|
|
*/ |
29
|
|
|
abstract protected function createPdo(&$db); |
30
|
|
|
|
31
|
|
|
/** @return PDO */ |
32
|
|
|
public function getPdo() |
33
|
|
|
{ |
34
|
|
|
return $this->pdo; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Cria uma conexão com um banco de dados |
39
|
|
|
* @param string[] $dbConfig |
40
|
|
|
*/ |
41
|
|
|
public function __construct($dbConfig) |
42
|
|
|
{ |
43
|
|
|
try { |
44
|
|
|
$this->pdo = $this->createPdo($dbConfig); |
45
|
|
|
$this->pdo->exec('set names utf8'); |
46
|
|
|
$this->pdoException = null; |
47
|
|
|
} catch (\PDOException $e) { |
48
|
|
|
$message = 'Houve um erro ao conectar o banco de dados'; |
49
|
|
|
throw new HttpException($message, 503, $e); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Retorna TRUE caso a conexão tenha sido bem sucedida |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function isValid() |
58
|
|
|
{ |
59
|
|
|
return is_null($this->pdoException) && $this->pdo instanceof \PDO; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $query |
64
|
|
|
* @param mixed[] $values |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function execute($query, $values = []) |
68
|
|
|
{ |
69
|
|
|
try { |
70
|
|
|
$stmt = $this->pdo->prepare($query); |
71
|
|
|
|
72
|
|
|
return $stmt->execute($values); |
73
|
|
|
} catch (PDOException $e) { |
74
|
|
|
throw new DatabaseException($e); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $query |
80
|
|
|
* @param mixed[] $values |
81
|
|
|
* @return PDOStatement|bool |
82
|
|
|
*/ |
83
|
|
|
public function stmt($query, $values = []) |
84
|
|
|
{ |
85
|
|
|
try { |
86
|
|
|
$stmt = $this->pdo->prepare($query); |
87
|
|
|
$stmt->execute($values); |
88
|
|
|
|
89
|
|
|
return $stmt; |
90
|
|
|
} catch (PDOException $e) { |
91
|
|
|
throw new DatabaseException($e); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $query |
97
|
|
|
* @param mixed[] $values |
98
|
|
|
* @return mixed[] |
99
|
|
|
*/ |
100
|
|
|
public function fetchAll($query, $values = []) |
101
|
|
|
{ |
102
|
|
|
return $this->stmt($query, $values)->fetchAll(PDO::FETCH_ASSOC); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $query |
107
|
|
|
* @param mixed[] $values |
108
|
|
|
* @return mixed[] |
109
|
|
|
*/ |
110
|
|
|
public function fetch($query, $values) |
111
|
|
|
{ |
112
|
|
|
return $this->stmt($query, $values)->fetch(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $query |
117
|
|
|
* @param mixed[] $values |
118
|
|
|
* @return int |
119
|
|
|
*/ |
120
|
|
|
public function fetchCount($query, $values) |
121
|
|
|
{ |
122
|
|
|
return (int) $this->stmt($query, $values)->fetchColumn(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** @return int */ |
126
|
|
|
public function lastInsertId() |
127
|
|
|
{ |
128
|
|
|
return (int) $this->pdo->lastInsertId(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths