-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdo.php
104 lines (85 loc) · 3.55 KB
/
pdo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/*
Description: PDO PHP
Author: Viktor Kasko
github.com/Examplio
*/
class Db {
private static ?Db $instance = null;
private PDO $db;
private function __construct(string $host, string $dbName, string $user, string $password) {
try {
$dsn = "mysql:host=$host;dbname=$dbName;charset=utf8mb4;port=3306";
$this->db = new PDO($dsn, $user, $password, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
$this->db->query("SET lc_time_names = 'ru_RU'");
} catch (PDOException $e) {
die("Catched error with connection to db: " . $e->getMessage());
}
}
public static function getInstance(string $host = "", string $dbName = "", string $user = "", string $password = ""): Db {
if (self::$instance === null) {
self::$instance = new self($host, $dbName, $user, $password);
}
return self::$instance;
}
public function query(string $query, array $params = []): array|bool {
try {
$sth = $this->db->prepare($query);
$sth->execute($params);
return $sth->fetchAll();
} catch (PDOException $e) {
return false;
}
}
public function insert(string $table, array $data): bool {
$columns = implode(", ", array_keys($data));
$placeholders = implode(", ", array_fill(0, count($data), "?"));
$query = "INSERT INTO $table ($columns) VALUES ($placeholders)";
return $this->executeQuery($query, array_values($data));
}
public function select(string $table, array|string $columns = "*", array $condition = []): array|bool {
$columns = is_array($columns) ? implode(", ", $columns) : $columns;
$whereClause = $this->buildWhereClause($condition);
$query = "SELECT $columns FROM $table" . $whereClause['sql'];
return $this->query($query, $whereClause['params']);
}
public function update(string $table, array $data, array $condition): bool {
$setClause = implode(", ", array_map(fn($key) => "`$key` = ?", array_keys($data)));
$whereClause = $this->buildWhereClause($condition);
$query = "UPDATE $table SET $setClause" . $whereClause['sql'];
return $this->executeQuery($query, array_merge(array_values($data), $whereClause['params']));
}
public function delete(string $table, array $condition): bool {
$whereClause = $this->buildWhereClause($condition);
$query = "DELETE FROM $table" . $whereClause['sql'];
return $this->executeQuery($query, $whereClause['params']);
}
public function getLastId(): int {
return (int) $this->db->lastInsertId();
}
private function executeQuery(string $query, array $params): bool {
try {
$sth = $this->db->prepare($query);
return $sth->execute($params);
} catch (PDOException $e) {
return false;
}
}
private function buildWhereClause(array $condition): array {
if (empty($condition)) return ['sql' => '', 'params' => []];
$whereParts = array_map(fn($key) => "`$key` = ?", array_keys($condition));
return [
'sql' => " WHERE " . implode(" AND ", $whereParts),
'params' => array_values($condition)
];
}
public function __destruct() {
$this->db = null;
}
}
?>