-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseSchemeUpdater.php
More file actions
124 lines (95 loc) · 3.54 KB
/
DatabaseSchemeUpdater.php
File metadata and controls
124 lines (95 loc) · 3.54 KB
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 DatabaseSchemeUpdater {
private $wpdb;
private $tablePrefix = "";
private $tableName = "dbversion";
public function __construct($tablePrefix = "") {
global $wpdb;
$this->tablePrefix = $tablePrefix;
$this->wpdb = $wpdb;
$this->update();
}
private function update() {
if (!$this->getIsDbSchemaTableExists()) {
$this->createDbVersionSchemeTable();
$this->createScheme();
}
$this->updateScheme();
}
private function getIsDbSchemaTableExists() {
$sql = "SELECT 1 FROM ".$this->getTableName()." LIMIT 1";
return $this->wpdb->query($sql);
}
private function createDbVersionSchemeTable() {
$sql = "CREATE TABLE IF NOT EXISTS `".$this->getTableName()."` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`version` int(11) NOT NULL,
PRIMARY KEY (id)
)";
if (!$this->wpdb->query($sql)) {
die ("WPCity: Db version scheme was not created.");
}
if (!$this->wpdb->insert($this->getTableName(), array ('version' => 0), array ("%d"))) {
die ("WPCity: Db version scheme was not created correctly.");
}
}
private function createScheme() {
$filename = $this->getSQLScriptsDirectory()."scheme.sql";
$this->executeScript($filename);
}
private function executeScript($filename) {
$handle = fopen($filename, "r");
if ($handle) {
$sql = "";
while (($line = fgets($handle)) !== false) {
$line = trim($line);
$lastChar = mb_substr($line, mb_strlen($line) - 1, 1);
$sql .= " ".$line;
if ($lastChar == ";") {
$sql = $this->replacePrefix($sql);
if (!$this->wpdb->query($sql)) {
die("WPCity: Database scheme was not created.");
}
$sql = "";
}
}
fclose($handle);
} else {
die("WPCity: Cannot load scheme.sql.");
}
}
private function updateScheme() {
$revision = $this->getDbRevision();
$revision++;
$filename = $this->getSQLScriptsDirectory().$revision.".sql";
while (file_exists($filename)) {
$this->executeScript($filename);
$this->setDbRevision($revision);
$revision++;
$filename = $this->getSQLScriptsDirectory().$revision.".sql";
}
}
private function getDbRevision() {
$sql = "SELECT * FROM ".$this->getTableName()." LIMIT 1";
$rows = $this->wpdb->get_results($sql);
if (!$rows) {
die("WPCity: Cannot get DB revision.");
}
$row = $rows[0];
return $row->version;
}
private function setDbRevision($revision) {
$values = array ("version" => $revision);
$types = array ('%d');
return $this->wpdb->update($this->getTableName(), $values, array("id" => 1), $types);
}
private function replacePrefix($sql) {
return str_replace("{{PREFIX}}", $this->tablePrefix, $sql);
}
private function getTableName() {
return $this->tablePrefix.$this->tableName;
}
private function getSQLScriptsDirectory() {
return dirname(__FILE__)."/sql/";
}
}