Skip to content

Commit

Permalink
Pg: TypeConverter post-merge cleanup
Browse files Browse the repository at this point in the history
kaare++ the work and original itch
  • Loading branch information
salortiz committed Jan 11, 2017
1 parent 92abd9b commit cbdf5b7
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 28 deletions.
19 changes: 14 additions & 5 deletions lib/DBDish.pm6
Expand Up @@ -23,13 +23,22 @@ role TypeConverter does Associative {
has Callable %!Conversions{Mu:U} handles <AT-KEY EXISTS-KEY>;

# The role implements the conversion
method convert (::?CLASS:D: Str $datum, Mu:U $typ) {
with %!Conversions{$typ} -> &converter {
converter($datum, $typ);
method convert (::?CLASS:D: Str $datum, Mu:U $type) {
with %!Conversions{$type} -> &converter {
&converter.signature.params.any ~~ .named
?? converter($datum, :$type)
!! converter($datum);
} else { # Common case
$typ($datum);
Str.can($type.^name) ?? $type($datum) !! $type.new($datum);
}
}
method STORE(::?CLASS:D: \to_store) {
for @(to_store) {
when Callable { %!Conversions{$_.signature.returns} = $_ }
when Pair { %!Conversions{::($_.key)} = $_.value }
}
}

}

=begin pod
Expand Down Expand Up @@ -72,7 +81,7 @@ The minimal declaration of a driver Foo typically start like:
- See L<DBDish::StatementHandle>
=head2 DBDish::Type
=head2 DBDish::TypeConverter
This role defines the API for dynamic handling of the types of a DB system
Expand Down
2 changes: 1 addition & 1 deletion lib/DBDish/Pg.pm6
@@ -1,7 +1,7 @@
use v6;
need DBDish;

unit class DBDish::Pg:auth<mberends>:ver<0.1.6> does DBDish::Driver;
unit class DBDish::Pg:auth<mberends>:ver<0.1.7> does DBDish::Driver;
use DBDish::Pg::Native;
need DBDish::Pg::Connection;

Expand Down
9 changes: 4 additions & 5 deletions lib/DBDish/Pg/Connection.pm6
Expand Up @@ -17,11 +17,10 @@ has %.Converter is DBDish::TypeConverter;
has %.dynamic-types = %oid-to-type;

submethod BUILD(:$!pg_conn, :$!parent!, :$!AutoCommit) {
%!Converter{Str} = sub (Str $str, Mu:U $type-name) { $str };
%!Converter{Date} = sub (Str $str, Mu:U $type-name) { Date.new($str) };
%!Converter{DateTime} = sub (Str $str, Mu:U $type-name) { DateTime.new($str.split(' ').join('T')) };
%!Converter{Bool} = sub (Str $str, Mu:U $type-name) { $str eq 't' };
%!Converter{Buf} = &str-to-blob;
%!Converter =
method (--> Bool) { self eq 't' },
method (--> DateTime) { DateTime.new(self.split(' ').join('T')) },
:Buf(&str-to-blob);
}

method prepare(Str $statement, *%args) {
Expand Down
4 changes: 2 additions & 2 deletions lib/DBDish/Pg/Native.pm6
Expand Up @@ -15,11 +15,11 @@ sub PQlibVersion(-->uint32) is native(LIB) is export { * }
sub PQfreemem(Pointer) is native(LIB) { * }
sub PQunescapeBytea(str, size_t is rw --> Pointer) is native(LIB) { * }

sub str-to-blob(Str $value, Mu:U $type-name) is export {
sub str-to-blob(Str $value, Mu:U :$type) is export {
my \ptr = PQunescapeBytea($value, my size_t $elems);
LEAVE { PQfreemem(ptr) if ptr }
with ptr {
blob-from-pointer(ptr, :$elems, :type($type-name))
blob-from-pointer(ptr, :$elems, :$type)
} else { die "Can't allocate memory!" };
}

Expand Down
6 changes: 3 additions & 3 deletions lib/DBDish/Pg/StatementHandle.pm6
Expand Up @@ -91,11 +91,11 @@ method _row() {
$l = do for @!column-type -> \ct {
my $value = ct;
unless $!result.PQgetisnull($!current_row, $col) {
my $str = $!result.PQgetvalue($!current_row, $col);
$value = $!result.PQgetvalue($!current_row, $col);
if ct ~~ Array {
$value = _pg-to-array($str, ct.of);
$value = _pg-to-array($value, ct.of);
} else {
$value = %Converter.convert($str, ct);
$value = %Converter.convert($value, ct);
}
}
$col++;
Expand Down
22 changes: 11 additions & 11 deletions t/06-types.t
Expand Up @@ -2,7 +2,7 @@ use v6;
use Test;
need DBDish;

plan 11;
plan 12;

class type-test {
has %.Converter is DBDish::TypeConverter;
Expand All @@ -12,22 +12,22 @@ class type-test {
}

submethod BUILD {
%!Converter{Int} = sub (Str $value, $typ) { Int($value) };
%!Converter{Str} = self.^find_method('test-str');
}
}

ok my $test = type-test.new;
ok my $res = $test.Converter.convert('123', Int), 'Get the result (Int)';
is $res, 123, 'Check it';
ok my $test = type-test.new, 'Converter created';
nok $test.Converter{Int}:exists, 'Int is builtin';
ok not $test.Converter{Int}.defined, 'So not defined';
ok my $res = $test.Converter.convert('123', Int), 'But can be used';
ok $res ~~ Int, 'Correct type';
is $res, 123, 'Check it';

ok my $sub = $test.Converter{Int}, 'Get the converter sub (Int)';
is $sub('123', Int), 123, 'and then convert';
my $int = sub ($) {1};
ok ($test.Converter{Int} = $int), 'Change the Int converter';
ok $sub = $test.Converter{Int}, 'Get it back';
is $sub.WHAT, Sub, 'Is it a sub?';
is $sub('123'), 1, 'Does it do its job?';
ok ($test.Converter{Int} = $int), 'Change the Int converter';
ok my $sub = $test.Converter{Int}, 'Get it back';
ok $sub === $int, 'The same sub';
is $test.Converter.convert('123', Int), 1, 'Does it do its job?';

ok $sub = $test.Converter{Str}, 'get the Str method';
is $test.$sub('test'), 'tset', 'and try it';
2 changes: 1 addition & 1 deletion t/34-pg-types.t
Expand Up @@ -49,7 +49,7 @@ is $sth.execute, 1, '1 row';
my ($col1) = $sth.row;
isa-ok $col1, Str;
is $col1, 'test', 'Test';
$dbh.Converter{Str} = sub (Str $str, Str $type-name) { 'changed' };
$dbh.Converter{Str} = sub ($) { 'changed' };
is $sth.execute, 1, '1 row';
($col1) = $sth.row;
is $col1, 'changed', 'Changed';
Expand Down

0 comments on commit cbdf5b7

Please sign in to comment.