|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Core\Services\Database; |
|
4
|
|
|
|
|
5
|
|
|
use PDO; |
|
6
|
|
|
use PDOException; |
|
7
|
|
|
|
|
8
|
|
|
class MysqlDatabase |
|
9
|
|
|
{ |
|
10
|
|
|
private $db_name; |
|
11
|
|
|
private $db_user; |
|
12
|
|
|
private $db_pass; |
|
13
|
|
|
private $db_host; |
|
14
|
|
|
private $pdo; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Construct - set the environment variables |
|
19
|
|
|
* @param string $db_name |
|
20
|
|
|
* @param string $db_user |
|
21
|
|
|
* @param string $db_pass |
|
22
|
|
|
* @param string $db_host |
|
23
|
|
|
* @return void |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(string $db_name, string $db_user = "root", string $db_pass = "", string $db_host = "localhost") |
|
26
|
|
|
{ |
|
27
|
|
|
$this->db_name = $db_name; |
|
28
|
|
|
$this->db_user = $db_user; |
|
29
|
|
|
$this->db_pass = $db_pass; |
|
30
|
|
|
$this->db_host = $db_host; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get PDO - connect to the server |
|
35
|
|
|
* @return PDO |
|
36
|
|
|
*/ |
|
37
|
|
|
private function getPDO(): PDO |
|
38
|
|
|
{ |
|
39
|
|
|
if ($this->pdo === \null) { |
|
40
|
|
|
$pdo = new PDO("mysql:dbname={$this->db_name};host={$this->db_host}", $this->db_user, $this->db_pass); |
|
41
|
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
42
|
|
|
$this->pdo = $pdo; |
|
43
|
|
|
} |
|
44
|
|
|
return $this->pdo; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Query - auto manages the fetch mode & type |
|
49
|
|
|
* @param string $statement |
|
50
|
|
|
* @param null|string $class_name |
|
51
|
|
|
* @param null|bool $one |
|
52
|
|
|
* @return mixed |
|
53
|
|
|
*/ |
|
54
|
|
|
public function query(string $statement, ?string $class_name = \null, ?bool $one = \false) |
|
55
|
|
|
{ |
|
56
|
|
|
$req = $this->getPDO()->query($statement); |
|
57
|
|
|
|
|
58
|
|
|
if ( |
|
59
|
|
|
\strpos($statement, "UPDATE") === 0 || |
|
60
|
|
|
\strpos($statement, "INSERT") === 0 || |
|
61
|
|
|
\strpos($statement, "DELETE") === 0 |
|
62
|
|
|
) { |
|
63
|
|
|
return $req; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (\strpos($statement, 'COUNT')) { |
|
67
|
|
|
return $req->fetchColumn(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$class_name === \null ? $req->setFetchMode(PDO::FETCH_OBJ) : $req->setFetchMode(PDO::FETCH_CLASS, $class_name); |
|
71
|
|
|
|
|
72
|
|
|
$datas = ($one === \true) ? $req->fetch() : $req->fetchAll(); |
|
73
|
|
|
|
|
74
|
|
|
return $datas; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Prepare - auto manages the fetch mode & type |
|
79
|
|
|
* @param string $statement |
|
80
|
|
|
* @param array $attributes |
|
81
|
|
|
* @param null|string $class_name |
|
82
|
|
|
* @param null|bool $one |
|
83
|
|
|
* @return mixed |
|
84
|
|
|
* @throws PDOException |
|
85
|
|
|
*/ |
|
86
|
|
|
public function prepare(string $statement, array $attributes, ?string $class_name = \null, ?bool $one = \false) |
|
87
|
|
|
{ |
|
88
|
|
|
$req = $this->getPDO()->prepare($statement); |
|
89
|
|
|
foreach ($attributes as $key => $value) { |
|
90
|
|
|
if (is_int($value)) { |
|
91
|
|
|
$req->bindValue($key + 1, $value, PDO::PARAM_INT); |
|
92
|
|
|
} else { |
|
93
|
|
|
$req->bindValue($key + 1, $value, PDO::PARAM_STR); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
$res = $req->execute(); |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
if ( |
|
100
|
|
|
\strpos($statement, "UPDATE") === 0 || |
|
101
|
|
|
\strpos($statement, "INSERT") === 0 || |
|
102
|
|
|
\strpos($statement, "DELETE") === 0 |
|
103
|
|
|
) { |
|
104
|
|
|
return $res; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if (\strpos($statement, 'COUNT')) { |
|
108
|
|
|
return $req->fetchColumn(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if ($class_name === \null) { |
|
112
|
|
|
$req->setFetchMode(PDO::FETCH_OBJ); |
|
113
|
|
|
} else { |
|
114
|
|
|
$req->setFetchMode(PDO::FETCH_CLASS, $class_name); |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if ($one) { |
|
118
|
|
|
$datas = $req->fetch(); |
|
119
|
|
|
} else { |
|
120
|
|
|
$datas = $req->fetchAll(); |
|
121
|
|
|
} |
|
122
|
|
|
return $datas; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|