Skip to content

Commit

Permalink
Retab and fix indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanstowe committed Jan 28, 2017
1 parent 16d9a25 commit f4067d8
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 161 deletions.
22 changes: 10 additions & 12 deletions lib/DBDish.pm6
Expand Up @@ -12,11 +12,10 @@ role Driver does DBDish::ErrorHandling {
method connect(*%params --> DBDish::Connection) { ... };

method !conn-error(:$errstr!, :$code) is hidden-from-backtrace {
self!error-dispatch: X::DBDish::ConnectionFailed.new(
:$code, :native-message($errstr), :$.driver-name
);
self!error-dispatch: X::DBDish::ConnectionFailed.new(
:$code, :native-message($errstr), :$.driver-name
);
}
#method new() { nextsame; }
}

role TypeConverter does Associative {
Expand All @@ -25,20 +24,19 @@ role TypeConverter does Associative {
# The role implements the conversion
method convert (::?CLASS:D: Str $datum, Mu:U $type) {
with %!Conversions{$type} -> &converter {
&converter.signature.params.any ~~ .named
?? converter($datum, :$type)
!! converter($datum);
&converter.signature.params.any ~~ .named
?? converter($datum, :$type)
!! converter($datum);
} else { # Common case
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 }
}
for @(to_store) {
when Callable { %!Conversions{$_.signature.returns} = $_ }
when Pair { %!Conversions{::($_.key)} = $_.value }
}
}

}

=begin pod
Expand Down
26 changes: 13 additions & 13 deletions lib/DBDish/Connection.pm6
Expand Up @@ -48,29 +48,29 @@ method prepare(Str $statement, *%args) { ... }

method do(Str $statement, *@params, *%args) {
LEAVE {
with %!Statements{$!last-sth-id} {
warn "'do' should not be used for statements that return rows"
unless .Finished;
.dispose;
}
with %!Statements{$!last-sth-id} {
warn "'do' should not be used for statements that return rows"
unless .Finished;
.dispose;
}
}
if !@params && self.can('execute') {
self.execute($statement, |%args);
self.execute($statement, |%args);
} orwith self.prepare($statement, |%args) {
.execute(@params, |%args);
.execute(@params, |%args);
}
else {
.fail;
.fail;
}
}

method rows {
if $!last-sth-id {
with %!Statements{$!last-sth-id} {
.rows;
} else {
$!last-rows
}
with %!Statements{$!last-sth-id} {
.rows;
} else {
$!last-rows
}
}
}

Expand Down
22 changes: 11 additions & 11 deletions lib/DBDish/Pg.pm6
Expand Up @@ -67,10 +67,10 @@ method connect(:database(:$dbname), *%params) {

%params.push((:$dbname)) with $dbname;
my $pg_conn = PGconn.new(%(%params<
host hostaddr port dbname user password connect-timeout
client-encoding options application-name keepalives keepalives-idle
keepalives-interval sslmode requiressl sslcert sslkey sslrootcert
sslcrl requirepeer krbsrvname gsslib service>:p:delete));
host hostaddr port dbname user password connect-timeout
client-encoding options application-name keepalives keepalives-idle
keepalives-interval sslmode requiressl sslcert sslkey sslrootcert
sslcrl requirepeer krbsrvname gsslib service>:p:delete));
my $status = $pg_conn.PQstatus;
if $status == CONNECTION_OK {
DBDish::Pg::Connection.new(:$pg_conn, :parent(self), |%params);
Expand All @@ -89,13 +89,13 @@ method version {

method data-sources(*%params) {
with self.connect(:dbname<template1>, |%params) {
LEAVE { .dispose }
with .prepare(
'SELECT pg_catalog.quote_ident(datname) FROM pg_catalog.pg_database ORDER BY 1'
) {
.execute;
.allrows.map({ dbname => .[0] }) . eager; # 'cus this conn will be disposed
} else { .fail }
LEAVE { .dispose }
with .prepare(
'SELECT pg_catalog.quote_ident(datname) FROM pg_catalog.pg_database ORDER BY 1'
) {
.execute;
.allrows.map({ dbname => .[0] }) . eager; # 'cus this conn will be disposed
} else { .fail }
} else { .fail }
}

Expand Down
2 changes: 1 addition & 1 deletion lib/DBDish/Pg/StatementHandle.pm6
Expand Up @@ -94,7 +94,7 @@ method _row() {
$value = $!result.PQgetvalue($!current_row, $col);
if ct ~~ Array {
$value = _pg-to-array($value, ct.of);
} elsif (ct.^name ne 'Any') {
} elsif (ct.^name ne 'Any') {
$value = %Converter.convert($value, ct);
}
}
Expand Down
12 changes: 6 additions & 6 deletions lib/DBDish/SQLite.pm6
Expand Up @@ -12,8 +12,8 @@ has $.library-resolved = False;

method connect(:database(:$dbname)! is copy, *%params) {
die "Cannot locate native library '" ~
$*VM.platform-library-name('sqlite3'.IO, :version(v0)) ~ "'"
unless $!library-resolved;
$*VM.platform-library-name('sqlite3'.IO, :version(v0)) ~ "'"
unless $!library-resolved;

my SQLite $p .= new;
# Add the standard extension unless has one
Expand All @@ -31,10 +31,10 @@ method connect(:database(:$dbname)! is copy, *%params) {
my $wks = 'sqlite3_libversion'; # A well known symbol
method new() {
with (%*ENV<DBIISH_SQLITE_LIB> andthen NativeLibs::Searcher.try-versions($_, $wks))
// NativeLibs::Searcher.try-versions( 'sqlite3', $wks, 0) {
# Try to keep the library loaded.
%_<library> = NativeLibs::Loader.load($_);
%_<library-resolved> = True;
// NativeLibs::Searcher.try-versions( 'sqlite3', $wks, 0) {
# Try to keep the library loaded.
%_<library> = NativeLibs::Loader.load($_);
%_<library-resolved> = True;
}
self.bless(|%_);
}
Expand Down
34 changes: 17 additions & 17 deletions lib/DBDish/StatementHandle.pm6
Expand Up @@ -38,8 +38,8 @@ method !enter-execute(int $got = 0, int $expect = 0) {
$!affected_rows = Nil;
self!ftr;
self!set-err( -1,
"Wrong number of arguments to method execute: got $got, expected $expect"
).fail unless $got == $expect;
"Wrong number of arguments to method execute: got $got, expected $expect"
).fail unless $got == $expect;
}

method !done-execute(Int $rows, $fields) {
Expand All @@ -59,8 +59,8 @@ method dispose() {
self.finish unless $!Finished;
self._free;
with $.parent.Statements{self.WHICH}:delete {
$.parent.last-rows = self.rows;
True;
$.parent.last-rows = self.rows;
True;
} else { False };
}
#Avoid leaks if explicit dispose isn't used by the user.
Expand All @@ -75,9 +75,9 @@ method rows {
method row(:$hash) {
self!ftr;
if my \r = self._row {
$hash ?? (@!column-name Z=> @(r)).hash !! r.Array;
$hash ?? (@!column-name Z=> @(r)).hash !! r.Array;
} else {
$hash ?? % !! @;
$hash ?? % !! @;
}
}

Expand All @@ -91,34 +91,34 @@ method column-types {

multi method allrows(:$array-of-hash!) {
gather {
while self.row(:hash) -> %r {
take %r;
}
while self.row(:hash) -> %r {
take %r;
}
}
}

multi method allrows(:$hash-of-array!) {
my %rows = @!column-name Z=> [] xx *;
while self.row -> @a {
for @a Z @!column-name -> ($v, $n) {
%rows{$n}.push: $v;
}
for @a Z @!column-name -> ($v, $n) {
%rows{$n}.push: $v;
}
}
%rows;
}

multi method allrows() {
gather {
while self.row -> \r {
take r;
}
while self.row -> \r {
take r;
}
}
}

# Legacy
method fetchrow {
if my \r = self._row {
my @ = (r.map: { .defined ?? ~$_ !! Str });
my @ = (r.map: { .defined ?? ~$_ !! Str });
} else { @ };
}

Expand All @@ -130,7 +130,7 @@ method fetchrow_hashref { self.fetchrow-hash }
method fetchall_hashref(Str $key) {
my %results;
while self.fetch-hash -> \h {
%results{h{$key}} = h;
%results{h{$key}} = h;
}
%results;
}
Expand Down
110 changes: 55 additions & 55 deletions lib/DBIish.pm6
Expand Up @@ -5,19 +5,19 @@ unit class DBIish:auth<mberends>:ver<0.5.9>;
use DBDish;

package GLOBAL::X::DBIish {
class DriverNotFound is Exception {
has $.bogus;
method message { "DBIish: No DBDish driver found: $.bogus" };
}
class LibraryMissing is Exception {
has $.driver;
has $.library;
method message { "DBIish: DBDish::$.driver needs $.library, not found" }
}
class NotADBDishDriver is Exception {
has $.who;
method message { "$.who is not a DBDish::Driver" };
}
class DriverNotFound is Exception {
has $.bogus;
method message { "DBIish: No DBDish driver found: $.bogus" };
}
class LibraryMissing is Exception {
has $.driver;
has $.library;
method message { "DBIish: DBDish::$.driver needs $.library, not found" }
}
class NotADBDishDriver is Exception {
has $.who;
method message { "$.who is not a DBDish::Driver" };
}
}

my %installed;
Expand All @@ -27,54 +27,54 @@ unit class DBIish:auth<mberends>:ver<0.5.9>;
method errstr { $err-handler.errstr };

method connect( $driver ) {
# The first native call done by the driver can trigger an X::AdHoc
# to report missing libraries.
# I catch here to avoid the drivers the need of this logic.
CATCH {
when $_.message ~~ m/
^ "Cannot locate native library "
( "'" <-[ ' ]> * "'" )
/ {
X::DBIish::LibraryMissing.new(:library($/[0]), :$driver).fail;
}
default {
.throw;
};
}
my $d = self.install-driver( $driver, |%_ );
$d.connect(|%_);
# The first native call done by the driver can trigger an X::AdHoc
# to report missing libraries.
# I catch here to avoid the drivers the need of this logic.
CATCH {
when $_.message ~~ m/
^ "Cannot locate native library "
( "'" <-[ ' ]> * "'" )
/ {
X::DBIish::LibraryMissing.new(:library($/[0]), :$driver).fail;
}
default {
.throw;
};
}
my $d = self.install-driver( $driver, |%_ );
$d.connect(|%_);
}
method install-driver( $drivername ) {
my $d = %installed{$drivername} //= do {
CATCH {
when X::CompUnit::UnsatisfiedDependency {
X::DBIish::DriverNotFound.new(:bogus($drivername)).fail;
}
default {
.throw;
}
}
my $module = "DBDish::$drivername";
my \M = (require ::($module));
# The DBDish namespace isn't formally reserved for DBDish's drivers,
# and is a good place for related common code.
# An assurance at driver load time is in place,
unless M ~~ DBDish::Driver {
# This warn will be converted in a die after the Role is settled,
# it's an advice for authors for externally developed drivers
warn "$module dosn't DBDish::Driver role!";
}
M.new(:parent($err-handler), |%($*DBI-DEFS<ConnDefaults>), |%_);
}
without $d { .throw; };
$d;
my $d = %installed{$drivername} //= do {
CATCH {
when X::CompUnit::UnsatisfiedDependency {
X::DBIish::DriverNotFound.new(:bogus($drivername)).fail;
}
default {
.throw;
}
}
my $module = "DBDish::$drivername";
my \M = (require ::($module));
# The DBDish namespace isn't formally reserved for DBDish's drivers,
# and is a good place for related common code.
# An assurance at driver load time is in place,
unless M ~~ DBDish::Driver {
# This warn will be converted in a die after the Role is settled,
# it's an advice for authors for externally developed drivers
warn "$module dosn't DBDish::Driver role!";
}
M.new(:parent($err-handler), |%($*DBI-DEFS<ConnDefaults>), |%_);
}
without $d { .throw; };
$d;
}
method install_driver($drivername) is hidden-from-backtrace {
warn "DBIish::install_driver is DEPRECATED, please use install-driver";
self.install-driver($drivername)
warn "DBIish::install_driver is DEPRECATED, please use install-driver";
self.install-driver($drivername)
}
method installed-drivers {
%installed.pairs.cache;
%installed.pairs.cache;
}


Expand Down
2 changes: 1 addition & 1 deletion lib/DBIish/Common.pm6
Expand Up @@ -5,7 +5,7 @@ unit package DBIish;

Rakudo::Internals.REGISTER-DYNAMIC: '$*DBI-DEFS', {
PROCESS::<$DBI-DEFS> = {
ConnDefaults => (:RaiseError, :!PrintError, :AutoCommit)
ConnDefaults => (:RaiseError, :!PrintError, :AutoCommit)
}
}

Expand Down

0 comments on commit f4067d8

Please sign in to comment.