This repository has been archived by the owner on Jan 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdatabase_wrapper.php
431 lines (346 loc) · 10.6 KB
/
database_wrapper.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?php
/*
* SIF Private server
* Provides database support. MySQL or SQLite3 can be used as the database backend.
*/
if(!defined("MAIN_INVOKED")) exit;
/** @@ Configuration @@ **/
// Use SQLite3 instead of MySQL? comment to use SQLite
//define("DBWRAPPER_USE_MYSQL", true);
// MySQL host
define("DBWRAPPER_MYSQL_HOSTNAME", "localhost");
// MySQL username
define("DBWRAPPER_MYSQL_USERNAME", "school_idol");
// MySQL password
define("DBWRAPPER_MYSQL_PASSWORD", "school_idol");
// MySQL port
define("DBWRAPPER_MYSQL_PORT", 3306);
// MySQL db. This is also serves as the SQLite3 database name (with db extension)
define("DBWRAPPER_MYSQL_DB", "school_idol");
// Automatically translate some MySQL-specific keyword to SQLite3 (and vice versa)? comment to disable
define("DBWRAPPER_MYSQL_SQLITE3_COMPAT", true);
/** !! Configuration !! **/ /* End configuration */
function common_initialize_environment(DatabaseWrapper $db, $direct_db = NULL)
{
$a = file_get_contents('data/initialize_environment.sql');
$b = file_get_contents('data/login_bonus_list.sql');
$query = <<<QUERY
BEGIN;
$a
$b
COMMIT
QUERY
;
if($db->execute_query($query) == false)
throw new Exception('Unable to initialize environment!');
}
$GLOBALS['common_sqlite3_concat_function'] = function(string ...$arg): string
{
return implode($arg);
};
/* This class must be inherited */
abstract class DatabaseWrapper
{
/* The db handle. Can be handle to MySQL connection or SQLite database file */
protected $db_handle;
protected $db_id;
abstract function __construct();
/* Initialize the database for the first time */
abstract public function initialize_environment();
/* Returns array if it returns table or "true" if it's not (but the query success) */
/* Returns false if the query is failed */
/* The additional argument is there to supply values. You must use ?, ?, ... in query */
/* to pass values to INSERT, UPDATE, ... */
/* values can be single value of array that contain everything or can be passed */
/* as function argument. If there's multiple SELECT, only the first result are returned */
abstract public function execute_query(string $query, string $types = NULL, ...$values);
/* Returns string representation of this database wrapper */
abstract public function __toString();
/* Create custom ordering SQL string */
public function custom_ordering(string $field_name, ...$order): string
{
if(is_array($order[0]))
$order = $order[0];
if(count($order) == 0)
return "";
$out = ["ORDER BY CASE `$field_name`"];
$max_len = count($order);
foreach($order as $key => $val)
{
if(is_string($val))
$out[] = "WHEN `$val` THEN $key";
else
$out[] = "WHEN $val THEN $key";
}
$out[] = "ELSE $max_len END";
return implode(' ', $out);
}
/* Closes the database handle */
function __destruct() {}
};
/*****************************************
** Database Wrapper: MySQL Wrapper **
*****************************************/
class MySQLDatabase extends DatabaseWrapper
{
function __construct()
{
$this->db_handle = new mysqli(DBWRAPPER_MYSQL_HOSTNAME, DBWRAPPER_MYSQL_USERNAME, DBWRAPPER_MYSQL_PASSWORD, DBWRAPPER_MYSQL_DB, DBWRAPPER_MYSQL_PORT);
if($this->db_handle->connect_error)
throw new Exception('Error ('.$mysqli->connect_errno.') '.$mysqli->connect_error);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
}
public function initialize_environment()
{
if(count($this->execute_query('SHOW TABLES LIKE "school_idol_initialized"')) > 0) return;
common_initialize_environment($this, $this->db_handle);
$this->db_handle->execute_query('ALTER TABLE `secretbox_list` AUTO_INCREMENT = 1'); // MySQL fix
// Add initialized flag
$this->execute_query('CREATE TABLE `school_idol_initialized` (unused INTEGER)');
}
public function execute_query(string $query, string $types = NULL, ...$values)
{
if(defined("DBWRAPPER_MYSQL_SQLITE3_COMPAT"))
$query = str_ireplace('INSERT OR IGNORE', 'INSERT IGNORE', preg_replace('/\?\d*/', "?", str_ireplace("RANDOM", "RAND", $query)));
if(isset($values[0]) && is_array($values[0]))
$values = $values[0];
if($types != NULL)
{
if($stmt = $this->db_handle->prepare($query))
{
$stmt->bind_param($types, ...$values);
if($stmt->execute())
{
$result = $stmt->get_result();
if($result)
{
/* Has result */
$out = $result->fetch_all(MYSQLI_BOTH);
$result->free();
return $out;
}
return true;
}
if($this->db_handle->error)
echo 'Error '.$this->db_handle->error, PHP_EOL;
return false;
}
if($this->db_handle->error)
echo 'Error '.$this->db_handle->error, PHP_EOL;
return false;
}
else
{
$result = $this->db_handle->multi_query($query);
if($result == false)
{
echo 'Error '.$this->db_handle->error, PHP_EOL;
return false;
}
if($result = $this->db_handle->use_result())
{
$fields = $result->fetch_fields();
$result_array = $result->fetch_all(MYSQLI_BOTH);
$result->free();
/* Convert the datatypes if possible */
if($fields)
{
/* Because associative array, also make fields as assoc */
foreach($fields as $x)
$fields[$x->name] = $x;
/* Enum */
foreach($result_array as &$values)
{
foreach($fields as $i => $types)
{
$target = &$values[$i];
switch($types->type)
{
case MYSQLI_TYPE_TINY:
case MYSQLI_TYPE_SHORT:
case MYSQLI_TYPE_LONG:
case MYSQLI_TYPE_INT24:
{
$target = intval($target);
break;
}
case MYSQLI_TYPE_LONGLONG:
{
if(PHP_INT_MAX > 2147483647)
// It's 64-bit. Convert it.
$target = intval($target);
break;
}
case MYSQLI_TYPE_DECIMAL:
case MYSQLI_TYPE_NEWDECIMAL:
case MYSQLI_TYPE_DOUBLE:
case MYSQLI_TYPE_FLOAT:
{
$target = floatval($target);
break;
}
default:
{
// Do mothing
break;
}
}
}
}
}
return $result_array;
}
if($this->db_handle->error)
{
echo 'Error '.$this->db_handle->error, PHP_EOL;
return false;
}
while($this->db_handle->more_results() && $this->db_handle->next_result()) {}
if($this->db_handle->error)
{
echo 'Error '.$this->db_handle->error, PHP_EOL;
return false;
}
/* No result. Return true */
return true;
}
}
public function __toString(): string
{
return 'DatabaseWrapper: Main MySQL';
}
public function __destruct()
{
$this->db_handle->close();
}
};
/*****************************************
** Database Wrapper: SQLite3 Wrapper **
*****************************************/
class SQLite3Database extends DatabaseWrapper
{
protected $custom_filename;
public function __construct(string $filename = NULL)
{
$custom_filename = false;
$dbname = DBWRAPPER_MYSQL_DB.'.db';
if($filename)
{
$dbname = $filename;
$this->custom_filename = true;
$this->db_id = random_int(0, 2147483647);
}
$this->db_handle = new SQLite3($dbname, $custom_filename ? SQLITE3_OPEN_READONLY : (SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE));
$this->db_handle->busyTimeout(5000); // timeout: 5 seconds
if(defined("DBWRAPPER_MYSQL_SQLITE3_COMPAT"))
$this->db_handle->createFunction('CONCAT', $GLOBALS['common_sqlite3_concat_function']);
}
public function initialize_environment()
{
if($this->custom_filename)
throw new Exception('Cannot initialize environment when opening another DB file');
{
$result = $this->db_handle->query('SELECT name FROM `sqlite_master` WHERE tbl_name = "initialized"');
$has_result = false;
while($row = $result->fetchArray())
$has_result = true;
if($has_result)
return;
}
fclose(fopen(DBWRAPPER_MYSQL_DB.'.db', 'w'));
if(!defined('DEBUG_ENVIRONMENT'))
if($this->db_handle->version()['versionNumber'] < 3007000)
throw new Exception('SQLite3 database wrapper requires SQLite v3.7.0 or later!');
else
$this->db_handle->exec("PRAGMA journal_mode=WAL"); // journal mode: WAL; production environment only
common_initialize_environment($this);
$this->db_handle->exec('CREATE TABLE `initialized` (a, b)');
}
public function execute_query(string $query, string $types = NULL, ...$values)
{
/* Try to convert the MySQL-specific keyword to SQLite */
if(defined("DBWRAPPER_MYSQL_SQLITE3_COMPAT"))
$query = str_ireplace("AUTO_INCREMENT", "AUTOINCREMENT", str_ireplace("LAST_INSERT_ID", "last_insert_rowid", $query));
if(isset($values[0]) && is_array($values[0]))
$values = $values[0];
if($types != NULL)
{
if($stmt = $this->db_handle->prepare($query))
{
foreach($values as $k => $v)
{
$datatype = SQLITE3_NULL;
switch($types[$k])
{
case "b":
{
$datatype = SQLITE3_BLOB;
break;
}
case "d":
{
$datatype = SQLITE3_FLOAT;
break;
}
case "i":
{
$datatype = SQLITE3_INTEGER;
break;
}
case "s":
{
$datatype = SQLITE3_TEXT;
break;
}
default:
{
unset($datatype);
break;
}
}
$stmt->bindValue($k + 1, $v, $datatype);
}
if($result = $stmt->execute())
{
if($result->numColumns())
{
/* There's result */
$out = [];
while($row = $result->fetchArray(SQLITE3_BOTH))
$out[] = $row;
return $out;
}
return true;
}
return false;
}
}
else
{
if(stripos($query, 'SELECT') === false)
return $this->db_handle->exec($query);
$result = $this->db_handle->query($query);
if($result == false)
return false;
$out = [];
while($row = $result->fetchArray(SQLITE3_BOTH))
$out[] = $row;
return $out;
}
}
public function __toString(): string
{
if($this->custom_filename)
return "DatabaseWrapper: SQLite3 {$this->db_id}";
else
return "DatabaseWrapper: Main SQLite3";
}
public function __destruct()
{
$this->db_handle->close();
}
};
if(defined("DBWRAPPER_USE_MYSQL"))
return new MySQLDatabase();
else
return new SQLite3Database();