-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathinit
executable file
·62 lines (58 loc) · 1.72 KB
/
init
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
#!/usr/bin/env php
<?php
function read_sql_file(string $file)
{
$comment_regex = '/(?<!:)\/\/.*|\/\\*(\s|.)*?\*\/|--[^\n]+/';
$lines = explode("\n", preg_replace($comment_regex, '', co::readFile($file)));
$init_sql = [];
$multi = false;
foreach ($lines as $index => $line) {
if (strlen($line) === 0) {
continue;
}
if (substr($line, -1, 1) !== ';') {
if (!$multi) {
$multi = true;
goto _new_line;
} else {
_append:
$end_line = &$init_sql[count($init_sql) - 1];
$end_line = $end_line . $line . "\n";
}
} else {
if ($multi) {
$multi = false;
goto _append;
} else {
$multi = false;
_new_line:
$init_sql[] = "{$line}";
}
}
}
return $init_sql;
}
require __DIR__ . '/include/config.php';
go(function () {
echo "[DB-init] initialization MySQL database...\n";
$mysql = new Swoole\Coroutine\MySQL();
$connected = $mysql->connect([
'host' => MYSQL_SERVER_HOST,
'port' => MYSQL_SERVER_PORT,
'user' => MYSQL_SERVER_USER,
'password' => MYSQL_SERVER_PWD,
'database' => MYSQL_SERVER_DB
]);
if (!$connected) {
echo "[DB-init] Connect failed! Error#{$mysql->connect_errno}: {$mysql->connect_error}\n";
exit(1);
}
$sql_file = read_sql_file(__DIR__ . '/test.sql');
foreach ($sql_file as $line) {
if (!$mysql->query($line)) {
echo "[DB-init] Failed! Error#{$mysql->errno}: {$mysql->error}\n";
exit(1);
}
}
echo "[DB-init] Done!\n";
});