-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdb.php
118 lines (99 loc) · 3.17 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
<?php
abstract class Database {
protected $conn;
public function connect() {
global $CONFIG;
if (is_null($this->conn)) {
try {
$this->conn = new PDO($CONFIG['db']['connection_string'], $CONFIG['db']['user'], $CONFIG['db']['password']);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die('database connection failed');
}
}
}
public function query_params($query, $params=null, $get_id=null) {
if (is_null($params)) {
$params = array();
}
try {
$query = $this->preprocess($query, $get_id);
$stmt = $this->conn->prepare($query);
foreach ($params as $key => $param) {
if ($key == "data") {
$stmt->bindValue(':' . $key, $param, PDO::PARAM_LOB);
} else if ($key == "limit") {
$stmt->bindValue(':' . $key, (int)trim($param), PDO::PARAM_INT);
} else {
$stmt->bindValue(':' . $key, $param);
}
}
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
if (is_null($get_id)) {
return $stmt;
} else {
return $this->get_last_id($stmt, $get_id);
}
} catch (PDOException $e) {
die('database exception');
}
}
protected abstract function preprocess($query, $get_id);
protected abstract function get_last_id($stmt, $get_id);
public abstract function random();
public abstract function interval($s);
public abstract function get_datetime($s);
public abstract function decode_data($s);
public abstract function encode_bool($s);
}
class MySqlDatabase extends Database {
protected function preprocess($query, $get_id) {
return $query;
}
protected function get_last_id($stmt, $get_id) {
return $this->conn->lastInsertId($get_id);
}
public function random() {
return 'RAND()';
}
public function interval($s) {
return sprintf("INTERVAL %s", $s);
}
public function get_datetime($s) {
return DateTime::createFromFormat('Y-m-d H:i:s', $s);
}
public function decode_data($s) {
return $s;
}
public function encode_bool($s) {
return $s ? 1 : 0;
}
}
class PostgreSqlDatabase extends Database {
protected function preprocess($query, $get_id) {
if (!is_null($get_id)) {
return $query . ' RETURNING ' . $get_id;
}
return $query;
}
protected function get_last_id($stmt, $get_id) {
$row = $stmt->fetch();
return $row[$get_id];
}
public function random() {
return 'RANDOM()';
}
public function interval($s) {
return sprintf("INTERVAL '%s'", $s);
}
public function get_datetime($s) {
return DateTime::createFromFormat('Y-m-d H:i:s.u', $s);
}
public function decode_data($s) {
return stream_get_contents($s);
}
public function encode_bool($s) {
return $s ? 't' : 'f';
}
}