Commit 84623b60 authored by Jason Cooper's avatar Jason Cooper

Fixed attribute aliasing to work with driver DSNs.

parent bf4a484f
Pipeline #90853285 passed with stage
in 2 minutes and 46 seconds
......@@ -775,6 +775,10 @@ All functionality listed here is highly experimental and should be used with gre
DBI->connect('dbi:Mock:MySQL', '', '');
Or, by using the database name if using driver DSNs:
DBI->connect('dbi:Mock:host=localhost;port=3306;database=MySQL', '', '');
The 'MySQL' in the DSN will be picked up and the MySQL specific attribute aliasing will be used.
Right now there is only minimal support for MySQL and MariaDB:
......
......@@ -963,6 +963,10 @@ Once this is turned on, you will need to choose a database specific attribute al
DBI->connect('dbi:Mock:MySQL', '', '');
Or, by using the database name if using driver DSNs:
DBI->connect('dbi:Mock:host=localhost;port=3306;database=MySQL', '', '');
The 'MySQL' in the DSN will be picked up and the MySQL specific attribute aliasing will be used.
Right now there is only minimal support for MySQL and MariaDB:
......
......@@ -3,6 +3,8 @@ package DBD::Mock::dr;
use strict;
use warnings;
use List::Util qw(reduce);
our $imp_data_size = 0;
my @connect_callbacks;
......@@ -16,12 +18,14 @@ sub connect {
}
$attributes ||= {};
my %driverParameters = _parse_driver_dsn( $dbname );
if ( $dbname && $DBD::Mock::AttributeAliasing ) {
# this is the DB we are mocking
$attributes->{mock_attribute_aliases} =
DBD::Mock::_get_mock_attribute_aliases($dbname);
$attributes->{mock_database_name} = $dbname;
DBD::Mock::_get_mock_attribute_aliases($driverParameters{database});
$attributes->{mock_database_name} = $driverParameters{database};
}
# holds statement parsing coderefs/objects
......@@ -117,4 +121,22 @@ sub add_connect_callbacks {
push @connect_callbacks, map { die "connect callbacks needs to be a reference to a function " unless ref $_ eq "CODE"; $_ } @_;
}
sub _parse_driver_dsn {
my ( $driverDsn ) = @_;
$driverDsn = $driverDsn ? $driverDsn : '';
my %driverParameters;
foreach my $parameter ( split /;/, $driverDsn ) {
if ( my ( $key, $value ) = $parameter =~ m/^(.*?)=(.*)$/ ) {
$driverParameters{ $key } = $value;
}
}
$driverParameters{database} = $driverDsn unless %driverParameters;
return %driverParameters;
}
1;
......@@ -136,4 +136,50 @@ like($@, qr/Attribute aliases not available for \'Fail\'/, '... got the error we
}
# test the MariaDB mock db
{
my $dbh;
eval {
$dbh = DBI->connect('dbi:Mock:host=localhost;port=3306;database=mariadb', '', '');
};
ok(!$@, '... got our mock DB successfully');
isa_ok($dbh, 'DBI::db');
is($dbh->{mock_database_name}, 'mariadb', '... and its the name we expected');
ok(defined($dbh->{mock_attribute_aliases}), '... got something here');
is(ref($dbh->{mock_attribute_aliases}), 'HASH', '... and its the hash we expected');
my $sth = $dbh->prepare('INSERT INTO Foo (bar) VALUES(NULL)');
isa_ok($sth, 'DBI::st');
$sth->execute();
is($dbh->{mariadb_insertid}, 1, '... our alias works');
}
# and test it with the lowercasing
{
my $dbh;
eval {
$dbh = DBI->connect('dbi:Mock:host=;database=MariaDB;port=', '', '');
};
ok(!$@, '... got our mock DB successfully');
isa_ok($dbh, 'DBI::db');
is($dbh->{mock_database_name}, 'MariaDB', '... and its the name we expected');
ok(defined($dbh->{mock_attribute_aliases}), '... got something here');
is(ref($dbh->{mock_attribute_aliases}), 'HASH', '... and its the hash we expected');
my $sth = $dbh->prepare('INSERT INTO Foo (bar) VALUES(NULL)');
isa_ok($sth, 'DBI::st');
$sth->execute();
is($dbh->{mariadb_insertid}, 1, '... our alias works');
}
done_testing();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment