-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathinit
executable file
·85 lines (77 loc) · 2.63 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/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';
require __DIR__ . '/swoole_pdo_pgsql/pdo_pgsql.inc';
Swoole\Coroutine\run(function () {
echo "[DB-init] initialization MySQL database...\n";
$mysql = new mysqli();
$connected = $mysql->connect(MYSQL_SERVER_HOST,
MYSQL_SERVER_USER,
MYSQL_SERVER_PWD,
MYSQL_SERVER_DB,
MYSQL_SERVER_PORT);
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] MySQL Done!\n";
echo "[DB-init] initialization ODBC...\n";
echo `set -ex`;
file_put_contents('/etc/odbcinst.ini', "[mysql]" . PHP_EOL
. "Driver=libmaodbc.so" . PHP_EOL
. "Description=MariaDB Connector/ODBC(Unicode)" . PHP_EOL
. "UsageCount=1" . PHP_EOL
);
echo `odbcinst -q -d -n "mysql"`;
file_put_contents('/etc/odbc.ini', "[mysql-test]" . PHP_EOL
. "Description = MySQL test database" . PHP_EOL
. "Trace = On" . PHP_EOL
. "TraceFile = stderr" . PHP_EOL
. "Driver = mysql" . PHP_EOL
. "SERVER = " . MYSQL_SERVER_HOST . PHP_EOL
. "USER = " . MYSQL_SERVER_USER . PHP_EOL
. "PASSWORD =" . MYSQL_SERVER_PWD . PHP_EOL
. "PORT = " . MYSQL_SERVER_PORT . PHP_EOL
. "DATABASE = " . MYSQL_SERVER_DB);
echo `odbcinst -i -d -f /etc/odbc.ini`;
echo "[DB-init] ODBC Done!\n";
});