This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
155 lines (133 loc) · 4.79 KB
/
db.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
require_once('table.php');
require_once('dialect.php');
class DB {
public static function open($url, ...$options) {
$dialect = null;
if (preg_match("/^pgsql:/", $url)) {
$dialect = new PostgreSQLDialect();
} elseif (preg_match("/^mysql:/", $url)) {
$dialect = new MySQLDialect();
} else {
throw new Exception("Unsupported Database Type");
}
$base = new PDO($url, ...$options);
$base->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return new DB($base, $dialect);
}
private $base;
private $dialect;
private $name;
private $columns;
private $relations;
private $hooks;
private $cached;
private $transaction_level;
function __construct($base, $dialect) {
$this->base = $base;
$this->dialect = $dialect;
$this->name = $this->value($dialect->database());
$this->columns = [];
$this->relations = [];
$this->hooks = [];
$this->cached = [];
$this->transaction_level = 0;
}
public function prepare($sql) {
return $this->base->prepare($sql);
}
public function execute($sql, ...$parameters) {
$call = $this->base->prepare($sql);
return $call->execute($parameters);
}
public function lastInsertId($table_name) {
return $this->base->lastInsertId($this->dialect->sequence($table_name));
}
public function query($sql, ...$parameters) {
$call = $this->base->prepare($sql);
$call->execute($parameters);
return $call->fetchAll();
}
public function one($sql, ...$parameters) {
$rows = $this->query($sql, ...$parameters);
return empty($rows)? null: $rows[0];
}
public function value($sql, ...$parameters) {
$row = $this->one($sql, ...$parameters);
return empty($row)? null: $row[0];
}
public function createTable($table_name, ...$columns) {
$sql = sprintf($this->dialect->create_table(), $table_name, implode(", ", $columns));
$this->execute($sql);
unset($this->cached[$table_name]);
unset($this->columns[$table_name]);
return $this->__get($table_name);
}
public function dropTable($table_name) {
return $this->execute("drop table if exists {$table_name}");
}
public function alterTable($table_name, $statement) {
return $this->execute("alter table {$table_name} {$statement}");
}
public function createIndex($index_name, $table_name, ...$column_names) {
$template = "create index %s on %s(%s)";
$sql = sprintf($template, $index_name, $table_name, implode(", ", $column_names));
return $this->execute($sql);
}
public function dropIndex($index_name) {
return $this->execute("drop index {$index_name}");
}
public function getTableNames() {
return array_map(function($row) {
return $row[0];
}, $this->query($this->dialect->tables(), $this->name));
}
public function getTables() {
return array_map(function($table_name) {
return $this->$table_name;
}, $this->getTableNames());
}
public function getColumns($table_name) {
if (!isset($this->columns[$table_name])) {
$columns = array_map(function($row) {
return $row[0];
}, $this->query($this->dialect->columns(), $this->name, $table_name));
$this->columns[$table_name] = array_filter($columns, function($column_name) {
return !in_array($column_name, ['id', 'created_at', 'updated_at']);
});
}
return $this->columns[$table_name];
}
public function __get($table_name) {
if (isset($this->cached[$table_name])) {
return $this->cached[$table_name];
}
$table_name = $this->dialect->convert($table_name);
if (!isset($this->relations[$table_name])) {
$this->relations[$table_name] = [];
}
if (!isset($this->hooks[$table_name])) {
$this->hooks[$table_name] = [];
}
$cached[$table_name] = new Table($this, $table_name, $this->getColumns($table_name), $this->relations[$table_name], $this->hooks[$table_name]);
return $cached[$table_name];
}
public function tx($fn) {
if ($this->transaction_level === 0) {
$this->base->beginTransaction();
}
$this->transaction_level++;
try {
$result = $fn();
$this->transaction_level--;
if ($this->transaction_level === 0) {
$this->base->commit();
}
return $result;
} catch (Exception $e) {
$this->transaction_level = 0;
$this->base->rollBack();
throw $e;
}
}
}