-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
124 lines (111 loc) · 3.04 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
<?php
class DB {
private static $instance;
protected $sql;
/**
Usage: build the following file:
class API extends DB {
protected function performUpdates($current_version) {
if ($current_version < 2) {
$this->sql->query('whatever');
$current_version = 2;
}
}
public function exampleFn($x) {
$stmt = $this->sql->prepare("INSERT INTO foo(bar) VALUES (?)");
$stmt->bind_param("s", $x);
$stmt->execute();
$result = $stmt->insert_id;
$stmt->close();
return $result;
}
}
*/
private function get_config_value($name, $default = -1) {
$stmt = $this->sql->prepare("SELECT value FROM config WHERE `name`= ?");
if(!$stmt){
return $default;
}
$stmt->bind_param("s", $name);
$stmt->execute();
$stmt->bind_result($value);
if ($stmt->fetch()) {
$stmt->close();
return $value;
} else {
$stmt->close();
return $default;
}
}
private function set_config_value($name, $value) {
$stmt = $this->sql->prepare("SELECT count(*) FROM config WHERE `name` = ? LIMIT 1");
$stmt->bind_param("s", $name);
$stmt->execute();
$stmt->bind_result($found);
$stmt->fetch();
$stmt->close();
if ($found == '1') {
$stmt = $this->sql->prepare("UPDATE config SET `value` = ? WHERE `name` = ? LIMIT 1");
if(!$stmt) die($this->sql->error);
$stmt->bind_param("ss", $value, $name);
$stmt->execute();
$stmt->close();
} else {
$stmt = $this->sql->prepare("INSERT INTO config(`name`, `value`) VALUES(?, ?)");
$stmt->bind_param("ss", $name, $value);
$stmt->execute();
$stmt->close();
}
}
private function assertDatabase() {
$old_version = $this->get_config_value("database_version");
$current_version = $old_version;
if ($current_version == -1) {
// Create initial database
$sqldata = file_get_contents('schema.sql', false);
$this->sql->multi_query($sqldata);
header("Location: index.php");
die(_("Database updated")); // Automatischer Refresh folgt
}
$this->performUpdates($current_version);
if ($current_version != $old_version) {
$this->set_config_value("database_version", $current_version);
}
}
protected function performUpdates(&$current_version) {
// overload this and add your content like:
/*
if ($current_version < 2) {
$this->sql->query('whatever');
$current_version = 2;
}
*/
}
protected function __construct() {
if (!file_exists('conf.php')) {
die(_('Missing config. Please copy conf.php.dist to conf.php and edit it.'));
}
include 'conf.php';
$this->sql = new mysqli($host, $username, $password, $database);
$this->sql->report_mode = MYSQLI_REPORT_ALL;
$this->assertDatabase();
}
public static function getInstance() {
if (!self::$instance) {
self::$instance = new static();
}
return self::$instance;
}
// derive from this class, implement performUpdates() and add new functions
// that use $this->sql
/**
public function exampleFn($x) {
$stmt = $this->sql->prepare("INSERT INTO foo(bar) VALUES (?)");
$stmt->bind_param("s", $x);
$stmt->execute();
$result = $stmt->insert_id;
$stmt->close();
return $result;
}
*/
}