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
POC Dynamic types
- Loading branch information
Showing
7 changed files
with
126 additions
and
40 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,4 @@ | ||
| use v6; | ||
|
|
||
| need DBDish; | ||
| unit class DBDish::Pg::Types does DBDish::Type; |
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,22 @@ | ||
| use v6; | ||
| use Test; | ||
| use DBDish; | ||
| plan 9; | ||
|
|
||
| class type-test does DBDish::Type { | ||
| submethod BUILD { | ||
| %!Conversions{'Int'} = sub (Str $value) { Int($value) }; | ||
| self.set('Str', sub (Str $value) { $value }); | ||
| } | ||
| } | ||
|
|
||
| ok my $test = type-test.new; | ||
| ok my $sub = $test.get('Int'); | ||
| is $sub('123'), 123; | ||
| my $int = sub ($) {1}; | ||
| ok $test.set('Int', $int); | ||
| ok $sub = $test.get('Int'); | ||
| is $sub.WHAT, Sub; | ||
| is $sub('123'), 1; | ||
| ok $sub = $test.get('Str'); | ||
| is $sub('test'), 'test'; |
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.types.set('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'); |