Skip to content
Browse files

Update demo example to prompt for database and sql to execute

  • Loading branch information...
1 parent 6281f91 commit d4a546a97845c3d5f86c1484157b2d9bfe590818 @timbunce committed
Showing with 17 additions and 15 deletions.
  1. +6 −5 DBDI.pm6
  2. +11 −10 dbdi.pl6
View
11 DBDI.pm6
@@ -7,13 +7,14 @@ use DBDI_pg;
class DBDI does java::sql::DriverManager {
multi method getConnection (
- Str $v1, # java.lang.String
- Str $v2, # java.lang.String
- Str $v3, # java.lang.String
- --> java::sql::Connection # java.sql.Connection
+ Str $url,
+ Str $user,
+ Str $pass,
+ --> java::sql::Connection
) {
+ my $conninfo = "host=localhost $url user=$user password=$pass";
my %opt;
- my $con = DBDI_pg::Driver.connect($v1, %opt);
+ my $con = DBDI_pg::Driver.connect($conninfo, %opt);
return $con;
}
View
21 dbdi.pl6
@@ -2,19 +2,20 @@ use v6;
use DBDI;
-my $conninfo = "host=localhost user=testuser password=testpass dbname=zavolaj";
+my $dbname = prompt 'Database: ';
-my $con = DBDI.getConnection($conninfo, '', '');
+my $con = DBDI.getConnection("dbname=$dbname", 'testuser', 'testpass');
-my $stmt = $con.createStatement;
+while prompt 'SQL: ' -> $sql {
-my $result = $stmt.executeQuery('select * from pg_database');
-my $meta = $result.getMetaData;
+ my $result = $con.createStatement.executeQuery($sql);
+ my $meta = $result.getMetaData;
-my @names = map { $meta.getColumnLabel($_) }, 1..$meta.getColumnCount;
-say @names.join(", ");
+ my @names = map { $meta.getColumnLabel($_) }, 1..$meta.getColumnCount;
+ say @names.join(", ");
-while ( $result.next ) {
- my @row = map { $result.getString($_) }, 1..$meta.getColumnCount;
- say @row.join(", ");
+ while ( $result.next ) {
+ my @row = map { $result.getString($_) }, 1..@names.elems;
+ say @row.join(", ");
+ }
}

0 comments on commit d4a546a

Please sign in to comment.
Something went wrong with that request. Please try again.