Skip to content

Commit

Permalink
switch MiniDBI.pm6 and mysql.pm6 to passing connection paramters as n…
Browse files Browse the repository at this point in the history
…amed arguments
  • Loading branch information
moritz committed Apr 23, 2012
1 parent 7f80cb4 commit 5b5859f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 36 deletions.
8 changes: 1 addition & 7 deletions lib/MiniDBD/mysql.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -326,19 +326,13 @@ class MiniDBD::mysql:auth<mberends>:ver<0.0.1> {
has $.Version = 0.01;

#------------------ methods to be called from MiniDBI ------------------
method connect( Str $user, Str $password, Str $params, $RaiseError ) {
method connect(Str :$user, Str :$password, :$RaiseError, *%params ) {
# warn "in MiniDBD::mysql.connect('$user',*,'$params')";
my ( $mysql_client, $mysql_error );
unless defined $mysql_client {
$mysql_client = mysql_init( OpaquePointer );
$mysql_error = mysql_error( $mysql_client );
}
my @params = $params.split(';');
my %params;
for @params -> $p {
my ( $key, $value ) = $p.split('=');
%params{$key} = $value;
}
my $host = %params<host> // 'localhost';
my $port = (%params<port> // 0).Int;
my $database = %params<database> // 'mysql';
Expand Down
12 changes: 4 additions & 8 deletions lib/MiniDBI.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@
class MiniDBI:auth<mberends>:ver<0.1.0> {
has $!err;
has $!errstr;
method connect( $dsn, $username, $password, :$RaiseError=0, :$PrintError=0, :$AutoCommit=1 ) {
# warn "in MiniDBI.connect('$dsn')";
# Divide $dsn up into its separate fields.
my ( $prefix, $drivername, $params ) = $dsn.split(':');
my $driver = self.install_driver( $drivername );
method connect($driver, :$RaiseError=1, :$PrintError=0, :$AutoCommit=1, *%opts ) {
my $d = self.install_driver( $driver );
# warn "calling MiniDBD::" ~ $drivername ~ ".connect($username,*,$params)";
my $connection = $driver.connect( $username, $password, $params, $RaiseError );
my $connection = $d.connect(:$RaiseError, :$PrintError, :$AutoCommit, |%opts );
return $connection;
}
method install_driver( $drivername ) {
my $driver;
given $drivername {
when 'CSV' { eval 'use MiniDBD::CSV; $driver = MiniDBD::CSV.new()' }
when 'mysql' { eval 'use MiniDBD::mysql; $driver = MiniDBD::mysql.new()' }
when 'PgPir' { eval 'use MiniDBD::PgPir; $driver = MiniDBD::PgPir.new()' }
when 'Pg' { eval 'use MiniDBD::Pg; $driver = MiniDBD::Pg.new()' }
when 'SQLite' { eval 'use MiniDBD::SQLite;$driver = MiniDBD::SQLite.new()' }
default { die "driver name '$drivername' is not known"; }
Expand Down Expand Up @@ -105,7 +101,7 @@ our sub SQL_INTERVAL_MINUTE_TO_SECOND { 113 }
# TODO: %drivers = DBI.installed_drivers;
# TODO: @data_sources = DBI.data_sources($driver_name, \%attr);
$dbh = MiniDBI.connect($data_source, $username, $auth, \%attr);
$dbh = MiniDBI.connect($driver, :$username, :$password, |%options);
$rv = $dbh.do($statement);
# TODO: $rv = $dbh.do($statement, \%attr);
Expand Down
11 changes: 5 additions & 6 deletions t/10-mysql.t
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ use MiniDBI;
# The file 'lib.pl' customizes the testing environment per DBD, but all
# this test script currently needs is the variables listed here.
my $mdriver = 'mysql';
my $hostname = 'localhost';
my $host = 'localhost';
my $port = 3306;
my $database = 'zavolaj';
my $test_user = 'testuser';
my $test_password = 'testpass';
my $test_dsn = "MiniDBI:$mdriver" ~ ":database=$database;" ~
"host=$hostname;port=$port";
my $table = 't1';

#-----------------------------------------------------------------------
Expand Down Expand Up @@ -77,7 +75,8 @@ ok $drh_version > 0, "MiniDBD::mysql version $drh_version"; # test 2
#ok defined $dbh, "Connected to database";
#ok $dbh->disconnect();
#
my $dbh = MiniDBI.connect( $test_dsn, $test_user, $test_password,
my $dbh = MiniDBI.connect($mdriver, :user($test_user), :password($test_password),
:$host, :$port, :$database,
RaiseError => 1, PrintError => 1, AutoCommit => 0
);
# die "ERROR: {MiniDBI.errstr}. Can't continue test" if $!.defined;
Expand All @@ -95,7 +94,7 @@ ok $result, 'disconnect returned true'; # test 4
#$dbh->disconnect();

try {
$dbh = MiniDBI.connect( $test_dsn, $test_user, $test_password,
$dbh = MiniDBI.connect( $mdriver, :user($test_user), :password($test_password), :$host, :$port,
RaiseError => 1, PrintError => 1, AutoCommit => 0 );
CATCH { die "ERROR: {MiniDBI.errstr}. Can't continue test\n"; }
}
Expand Down Expand Up @@ -240,7 +239,7 @@ ok $dbh.do("DROP TABLE $table"),"drop table $table"; # test 38
# Because the drop table might fail, disconnect and reconnect
$dbh.disconnect();
try {
$dbh = MiniDBI.connect( $test_dsn, $test_user, $test_password,
$dbh = MiniDBI.connect( $mdriver, :user($test_user), :password($test_password), :$host, :$port,
RaiseError => 1, PrintError => 1, AutoCommit => 0 );
CATCH { die "ERROR: {MiniDBI.errstr}. Can't continue test\n"; }
}
Expand Down
20 changes: 7 additions & 13 deletions t/25-mysql-common.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@ use Test;
use MiniDBI;

# Define the only database specific values used by the common tests.
my ( $*mdriver, $*test_dsn, $*test_user, $*test_password );
{
# Define values that are relevant only to MySQL
my $hostname = 'localhost';
my $port = 3306;
my $database = 'zavolaj';
# Set up the common variables with the MySQL specific values
$*mdriver = 'mysql';
$*test_dsn = "MiniDBI:$*mdriver" ~ ":database=$database;"
~ "host=$hostname;port=$port";
$*test_user = 'testuser';
$*test_password = 'testpass';
}
my $*mdriver = 'mysql';
my %*opts = ().hash;
%*opts<host> = 'localhost';
%*opts<port> = 3306;
%*opts<database> = 'zavolaj';
%*opts<user> = 'testuser';
%*opts<password> = 'testpass';

# Detect and report possible errors from eval of the common test script
warn $! if "ok 99-common.pl6" ne eval slurp 't/99-common.pl6';
4 changes: 2 additions & 2 deletions t/99-common.pl6
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ $drh_version = $drh.Version;
ok $drh_version > 0, "MiniDBD::$*mdriver version $drh_version"; # test 2

# Connect to the data sourcequantity*price AS amount FROM nom
my $dbh = MiniDBI.connect( $*test_dsn, $*test_user, $*test_password, :RaiseError<1> );
ok $dbh, "connect to $*test_dsn"; # test 3
my $dbh = MiniDBI.connect( $*mdriver, |%*opts, :RaiseError<1> );
ok $dbh, "connect to %*opts<database>"; # test 3

try eval '$*post_connect_cb.($dbh)';

Expand Down

0 comments on commit 5b5859f

Please sign in to comment.