Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #83 from kaare/master
Dynamic types handling
- Loading branch information
Showing
6 changed files
with
131 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| use v6; | ||
| use Test; | ||
| need DBDish; | ||
|
|
||
| plan 11; | ||
|
|
||
| class type-test { | ||
| has %.Converter is DBDish::TypeConverter; | ||
|
|
||
| method test-str(Str $value) { | ||
| $value.flip; | ||
| } | ||
|
|
||
| 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 $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 $sub = $test.Converter{Str}, 'get the Str method'; | ||
| is $test.$sub('test'), 'tset', 'and try it'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| use v6; | ||
| use Test; | ||
| use DBIish; | ||
|
|
||
| plan 10; | ||
|
|
||
| my %con-parms; | ||
| # If env var set, no parameter needed. | ||
| %con-parms<database> = 'dbdishtest' unless %*ENV<PGDATABASE>; | ||
| %con-parms<user> = 'postgres' unless %*ENV<PGUSER>; | ||
| my $dbh; | ||
|
|
||
| try { | ||
| $dbh = DBIish.connect('Pg', |%con-parms); | ||
| CATCH { | ||
| when X::DBIish::LibraryMissing | X::DBDish::ConnectionFailed { | ||
| diag "$_\nCan't continue."; | ||
| } | ||
| default { .throw; } | ||
| } | ||
| } | ||
| without $dbh { | ||
| skip-rest 'prerequisites failed'; | ||
| exit; | ||
| } | ||
|
|
||
| ok $dbh, 'Connected'; | ||
|
|
||
| # Be less verbose; | ||
| $dbh.do('SET client_min_messages TO WARNING'); | ||
| lives-ok { $dbh.do('DROP TABLE IF EXISTS test') }, 'Clean'; | ||
| lives-ok { | ||
| $dbh.do(q| | ||
| CREATE TABLE test ( | ||
| col1 text | ||
| )|) | ||
| }, 'Table created'; | ||
|
|
||
| my $sth = $dbh.prepare('INSERT INTO test (col1) VALUES(?)'); | ||
| lives-ok { | ||
| $sth.execute('test'); | ||
| }, 'Insert Perl6 values'; | ||
| $sth.dispose; | ||
| $sth = $dbh.prepare('SELECT col1 FROM test'); | ||
| my @coltype = $sth.column-types; | ||
| ok @coltype eqv [Str], 'Column-types'; | ||
|
|
||
| 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' }; | ||
| is $sth.execute, 1, '1 row'; | ||
| ($col1) = $sth.row; | ||
| is $col1, 'changed', 'Changed'; | ||
| $dbh.do('DROP TABLE IF EXISTS test'); |