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
|
# vim:sw=2:ai
package hstest;
use DBI;
use Net::HandlerSocket;
our %conf = ();
sub get_conf_env {
my ($key, $defval) = @_;
return $ENV{$key} || $defval;
}
sub init_conf {
$conf{host} = get_conf_env("MYHOST", "localhost");
$conf{myport} = get_conf_env("MYPORT", 3306);
$conf{dbname} = get_conf_env("MYDBNAME", "hstestdb");
$conf{ssps} = get_conf_env("MYSSPS");
$conf{user} = get_conf_env("MYSQLUSER", "root");
$conf{pass} = get_conf_env("MYSQLPASS", "");
$conf{hsport} = get_conf_env("HSPORT", 9998);
$conf{hspass} = get_conf_env("HSPASS", undef);
}
sub get_dbi_connection {
my ($dbname, $host, $myport, $ssps, $user, $pass)
= ($conf{dbname}, $conf{host}, $conf{myport}, $conf{ssps},
$conf{user}, $conf{pass});
my $mycnf = "binary_my.cnf";
my $dsn = "DBI:mysql:database=;host=$host;port=$myport"
. ";mysql_server_prepare=$ssps"
. ";mysql_read_default_group=perl"
. ";mysql_read_default_file=../common/$mycnf";
my $dbh = DBI->connect($dsn, $user, $pass, { RaiseError => 1 });
return $dbh;
}
sub init_testdb {
my $charset = $_[0] || "binary";
my $dbh = get_dbi_connection();
my $dbname = $conf{dbname};
$dbh->do("drop database if exists $dbname");
$dbh->do("create database $dbname default character set $charset");
$dbh->do("use $dbname");
return $dbh;
}
sub get_hs_connection {
my ($host, $port) = @_;
$host ||= $conf{host};
$port ||= $conf{hsport};
my $hsargs = { 'host' => $host, 'port' => $port };
my $conn = new Net::HandlerSocket($hsargs);
if (defined($conn) && defined($conf{hspass})) {
$conn->auth($conf{hspass});
}
return $conn;
}
init_conf();
1;
|