Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Change the type identifier to be a type object instead of a string.
  • Loading branch information
kaare committed Jan 5, 2017
1 parent 416b4db commit 6223169
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 24 deletions.
8 changes: 4 additions & 4 deletions lib/DBDish.pm6
Expand Up @@ -20,13 +20,13 @@ role Driver does DBDish::ErrorHandling {
}

role Type {
has Callable %!Conversions{Str};
has Callable %!Conversions{Mu:U};

method set(Str $name, &convert) {
method set(Mu:U $name, &convert) {
%!Conversions{$name} = &convert;
}
method get(Str $name) {
%!Conversions{$name} || sub (Str :$str, Str :$type-name) { $type-name($str) };
method get(Mu:U $type) {
%!Conversions{$type} || sub (Str :$str, Mu:U :$type-name) { $type-name($str) };
}
}

Expand Down
14 changes: 6 additions & 8 deletions lib/DBDish/Pg/Connection.pm6
Expand Up @@ -18,14 +18,12 @@ has $.types;

submethod BUILD(:$!pg_conn, :$!parent!, :$!AutoCommit) {
$!types = DBDish::Pg::Types.new;
$!types.set('Int', sub (Str :$str, Str :$type-name) { Int($str) });
$!types.set('Rat', sub (Str :$str, Str :$type-name) { Rat($str) });
$!types.set('Str', sub (Str :$str, Str :$type-name) { $str });
$!types.set('Date', sub (Str :$str, Str :$type-name) { Date.new($str) });
$!types.set('DateTime', sub (Str :$str, Str :$type-name) { DateTime.new($str.split(' ').join('T')) });
$!types.set('Array', sub (Str :$str, Str :$type-name) { $str });
$!types.set('Bool', sub (Str :$str, Str :$type-name) { $str eq 't' });
$!types.set('Buf', &str-to-blob);
$!types.set(Str, sub (Str :$str, Mu:U :$type-name) { $str });
$!types.set(Date, sub (Str :$str, Mu:U :$type-name) { Date.new($str) });
$!types.set(DateTime, sub (Str :$str, Mu:U :$type-name) { DateTime.new($str.split(' ').join('T')) });
$!types.set(Array, sub (Str :$str, Mu:U :$type-name) { $str });
$!types.set(Bool, sub (Str :$str, Mu:U :$type-name) { $str eq 't' });
$!types.set(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 :str($value), Str :$type-name) is export {
sub str-to-blob(Str :str($value), Mu:U :$type-name) is export {
my \ptr = PQunescapeBytea($value, my size_t $elems);
LEAVE { PQfreemem(ptr) if ptr }
with ptr {
blob-from-pointer(ptr, :$elems, :type(Buf))
blob-from-pointer(ptr, :$elems, :type($type-name))
} else { die "Can't allocate memory!" };
}

Expand Down
4 changes: 2 additions & 2 deletions lib/DBDish/Pg/StatementHandle.pm6
Expand Up @@ -96,8 +96,8 @@ method _row() {
if ct ~~ Array {
$value = _pg-to-array($str, ct.of);
} else {
my $sub = $types.get(ct.^name);
$value = &$sub(:$str, :type-name(ct.^name));
my $sub = $types.get(ct);
$value = &$sub(:$str, :type-name(ct));
}
}
$col++;
Expand Down
13 changes: 6 additions & 7 deletions t/06-types.t
Expand Up @@ -9,19 +9,18 @@ class type-test does DBDish::Type {
}

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

ok my $test = type-test.new;
ok my $sub = $test.get('Int');
ok my $sub = $test.get(Int);
is $sub('123'), 123;
my $int = sub ($) {1};
ok $test.set('Int', $int);
ok $sub = $test.get('Int');
ok $test.set(Int, $int);
ok $sub = $test.get(Int);
is $sub.WHAT, Sub;
is $sub('123'), 1;
ok $sub = $test.get('Str');
$sub.gist.say;
ok $sub = $test.get(Str);
is $test.$sub('test'), 'tset';
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.types.set('Str', sub (Str :$str, Str :$type-name) { 'changed' });
$dbh.types.set(Str, sub (Str :$str, Str :$type-name) { 'changed' });
is $sth.execute, 1, '1 row';
($col1) = $sth.row;
is $col1, 'changed', 'Changed';
Expand Down

0 comments on commit 6223169

Please sign in to comment.