-
Notifications
You must be signed in to change notification settings - Fork 33
/
DBIish.pm6
executable file
·151 lines (125 loc) · 4.3 KB
/
DBIish.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use v6;
# DBIish.pm6
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" };
}
}
my %installed;
my $err-handler = DBDish::ErrorHandling.new(:parent(Nil));
method err { $err-handler.err };
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(|%_);
}
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 doesn't do DBDish::Driver role!";
}
M.new(:parent($err-handler), |%($*DBI-DEFS<ConnDefaults>), |%_);
}
without $d { .throw; };
$d;
}
method install_driver($drivername)
is hidden-from-backtrace
is DEPRECATED("install-driver")
{
self.install-driver($drivername)
}
method installed-drivers {
%installed.pairs.cache;
}
=begin pod
=head1 SYNOPSIS
use v6;
use DBIish;
my $dbh = DBIish.connect("SQLite", :database<example-db.sqlite3>);
my $sth = $dbh.do(q:to/STATEMENT/);
DROP TABLE nom
STATEMENT
$sth = $dbh.do(q:to/STATEMENT/);
CREATE TABLE nom (
name varchar(4),
description varchar(30),
quantity int,
price numeric(5,2)
)
STATEMENT
$sth = $dbh.do(q:to/STATEMENT/);
INSERT INTO nom (name, description, quantity, price)
VALUES ( 'BUBH', 'Hot beef burrito', 1, 4.95 )
STATEMENT
$sth = $dbh.prepare(q:to/STATEMENT/);
INSERT INTO nom (name, description, quantity, price)
VALUES ( ?, ?, ?, ? )
STATEMENT
$sth.execute('TAFM', 'Mild fish taco', 1, 4.85);
$sth.execute('BEOM', 'Medium size orange juice', 2, 1.20);
$sth = $dbh.prepare(q:to/STATEMENT/);
SELECT name, description, quantity, price, quantity*price AS amount
FROM nom
STATEMENT
$sth.execute();
my $arrayref = $sth.fetchall_arrayref();
say $arrayref.elems; # 3
$sth.finish;
$dbh.dispose;
See also F<README.pod> for more documentation.
=head1 DESCRIPTION
The name C<DBIish> has two meanings. In lowercase it indicates the
github project being used for development. In mixed case it is the
module name and class name that database client applications should use.
=head1 DBIish CLASSES and ROLES
=head2 DBIish
The C<DBIish> class exists mainly to provide the F<connect> method,
which acts as a constructor for database connections.
=head2 DBDish
The C<DBDish> role should only be used with 'does' to provide standard
members for DBDish classes.
=head1 SEE ALSO
L<http://dbi.perl.org>
=end pod