forked from mberends/MiniDBI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_mysql.p6
79 lines (54 loc) · 1.7 KB
/
check_mysql.p6
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
use v6;
use MiniDBI;
sub MAIN($sleep) {
say "** in MAIN";
unless ($sleep) {
$sleep = 1;
}
say "** check mysql connection p6....";
my $conn = getMysqlConnection('zavolaj');
say "- alive after connect";
selectRows($conn, 'foo');
say " - alive after select";
# say "sleeping for $sleep seconds, shutdown mysql...";
# sleep($sleep);
say '** (1) Mysql ping in client: "'~ $conn.ping() ~'"';
say "sleeping for $sleep seconds, shutdown mysql...";
sleep($sleep);
say '** (2) Mysql ping in client: "'~ $conn.ping() ~'"';
say '** Calling disconnect in client';
my $disconnectResult = $conn.disconnect();
say ' - result: '~ $disconnectResult;
if ($disconnectResult eq "True") {
say ' - Disconnect OK';
} else {
say ' - Disconnect NOT OK';
}
say '** (3) Mysql ping in client: "'~ $conn.ping() ~'"';
}
sub selectRows {
my ($dbh, $table) = @_;
my $sth = $dbh.prepare('SELECT * FROM '~ $table);
$sth.execute();
# print "field count: "~ $sth.mysql_field_count;
my $result = $sth.fetchrow_hashref();
print "Value returned: $result\n";
}
sub getMysqlConnection {
my ($database) = @_;
my $mdriver = 'mysql';
my $hostname = 'localhost';
my $port = 3306;
my $test_user = 'testuser';
my $test_password = 'testpass';
######
my $test_dsn = "MiniDBI:$mdriver" ~ ":database=$database;" ~
"host=$hostname;port=$port";
warn("** Installing driver... "~ $mdriver);
# my $drh = MiniDBI.install_driver($mdriver);
warn("** Trying connecting to "~ $test_dsn);
my $dbh = MiniDBI.connect( $test_dsn, $test_user, $test_password,
RaiseError => 1, PrintError => 1, AutoCommit => 0 );
CATCH { die "ERROR. Unable to connect to server\n"; }
return $dbh;
}