-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-live-test.php
101 lines (75 loc) · 2.18 KB
/
db-live-test.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
#!/usr/bin/env php
<?php
/*
by xxxxxliil@github
db-live-test.php released under MIT License.
*/
$longopts = array(
"dsn:",
"help",
"password:",
"user:",
"version",
);
$optind = null;
$shortopts = "";
$shortopts .= "d:";
$shortopts .= "h";
$shortopts .= "p:";
$shortopts .= "u:";
$shortopts .= "V";
$cmd_options = getopt($shortopts, $longopts, $optind);
$pos_args = array_slice($argv, $optind);
function check_repeated_option($option1, $option2) {
if (isset($option1) && isset($option2)) {
echo("You can't use long option and short option at the same time.\n");
exit(2);
} elseif (isset($option1)){
return $option1;
} elseif (isset($option2)){
return $option2;
} else {
return null;
}
}
if (isset($cmd_options["h"]) || isset($cmd_options["help"]) || count($cmd_options) == 0) {
echo <<<EOF
db-online-test: test your database is online
usage: db-online-test [options]
-h, --help display this help
-d, --dsn set the Data Source Name info
-p, --password database password
-u, --user database user
-V, --version display version
EOF;
if (count($cmd_options) != 0) {
exit(0);
} else {
//EX_USAGE
exit(64);
}
}
if (isset($cmd_options["V"]) || isset($cmd_options["version"])) {
exit("db-live-test 0.0.1". "\n");
}
$db_options = array(
"dsn" => check_repeated_option($cmd_options["d"] ?? null, $cmd_options["dsn"] ?? null),
"password" => check_repeated_option($cmd_options["p"] ?? null, $cmd_options["password"] ?? null),
"user" => check_repeated_option($cmd_options["u"] ?? null, $cmd_options["user"] ?? null),
);
if (! isset($db_options["dsn"])) {
echo("Error: dsn is not set\n");
exit(64);
} elseif (! isset($db_options["user"])) {
echo("Error:user is not set\n");
exit(64);
} elseif (! isset($db_options["password"])) {
echo("Warning: password is not set\n");
}
try {
$dbh = new PDO($db_options["dsn"], $db_options["user"], $db_options["password"]);
} catch (PDOException $error) {
echo('Connection failed: ' . $error->getMessage() . "\n");
exit(75);
}
?>