1: <?php
2:
3: namespace ngatngay\database;
4:
5: use PDO;
6:
7: class database
8: {
9: private PDO $pdo;
10:
11: public function __construct(?PDO $pdo = null)
12: {
13: $this->pdo = $pdo;
14: $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_CLASS);
15: $this->pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, [statement::class]);
16: }
17:
18: public function query(string $sql, ?array $params = null): mixed
19: {
20: $stmt = $this->pdo->prepare($sql);
21: $stmt->execute($params);
22:
23: return $stmt;
24: }
25:
26: public function insert(string $table, array $params): mixed
27: {
28: $sql = 'insert into "' . $table . '"'
29: . ' (' . implode(',', $this->buildName(array_keys($params))) . ')'
30: . ' values (' . implode(',', array_fill(0, count($params), '?')) . ')';
31:
32: $this->query($sql, array_values($params));
33:
34: return $this->pdo->lastInsertId();
35: }
36:
37:
38: public function update_or_insert(string $table, array $con, array $arr): void
39: {
40: $whereConditions = [];
41: $whereParams = [];
42:
43: foreach ($con as $column => $value) {
44: $whereConditions[] = sprintf('"%s" = ?', $column);
45: $whereParams[] = $value;
46: }
47:
48: $whereClause = implode(' AND ', $whereConditions);
49: $checkSql = sprintf('SELECT COUNT(*) FROM "%s" WHERE %s', $table, $whereClause);
50: $count = $this->fetch_column($checkSql, $whereParams);
51:
52: if ($count > 0) {
53: $updateParts = [];
54: $updateParams = [];
55:
56: foreach ($arr as $column => $value) {
57: $updateParts[] = sprintf('"%s" = ?', $column);
58: $updateParams[] = $value;
59: }
60:
61: $updateClause = implode(', ', $updateParts);
62: $updateSql = sprintf('UPDATE "%s" SET %s WHERE %s', $table, $updateClause, $whereClause);
63:
64: $this->query($updateSql, array_merge($updateParams, $whereParams));
65: } else {
66: $this->insert($table, array_merge($con, $arr));
67: }
68: }
69:
70: public function update(string $sql, ?array $params = null): mixed
71: {
72: $stmt = $this->query($sql, $params);
73:
74: return $stmt->rowCount();
75: }
76:
77: public function fetch(string $sql, ?array $params = null): mixed
78: {
79: return $this->query($sql, $params)
80: ->fetch();
81: }
82:
83: public function fetchAll(string $sql, ?array $params = null): mixed
84: {
85: return $this->query($sql, $params)
86: ->fetchAll();
87: }
88: public function fetch_all(string $sql, ?array $params = null): mixed
89: {
90: return $this->query($sql, $params)
91: ->fetchAll();
92: }
93:
94: public function fetchColumn(string $sql, ?array $params = null, int $column = 0): mixed
95: {
96: $stmt = $this->query($sql, $params);
97: return $stmt->fetchColumn($column);
98: }
99: public function fetch_column(string $sql, ?array $params = null, int $column = 0): mixed
100: {
101: $stmt = $this->query($sql, $params);
102: return $stmt->fetchColumn($column);
103: }
104:
105: public function getOffset(int $page, int $perPage): int
106: {
107: return $page * $perPage - $perPage;
108: }
109: public function get_offset(int $page, int $perPage): int
110: {
111: return $page * $perPage - $perPage;
112: }
113:
114: public function quote(string $str): string
115: {
116: return $this->pdo->quote($str);
117: }
118:
119: public function buildName(array $arr): array
120: {
121: return array_map(function ($item) {
122: return '"' . $item . '"';
123: }, $arr);
124: }
125: public function build_name(array $arr): array
126: {
127: return array_map(function ($item) {
128: return '"' . $item . '"';
129: }, $arr);
130: }
131: }
132: