/
PgPir.pm6
67 lines (49 loc) · 1.54 KB
/
PgPir.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
pir::load_bytecode("Pg.pir");
class FakeDBD::PgPir::StatementHandle does FakeDBD::StatementHandle {
has $!name;
has $!RaiseError;
}
class FakeDBD::PgPir::Connection does FakeDBD::Connection {
has $!pg_conn;
has $!statement_name = 'a';
has $!RaiseError;
method prepare(Str $statement) {
my $name = $!statement_name++;
# the third argument to .prepare() is the number of
# bind where we want to explicitly specify the type
my $handle = $!pg_conn.prepare($name, $statement, 0);
}
method status {
my $c = $!pg_conn;
! Q:PIR {
$P0 = find_lex '$c'
$I0 = $P0.'status'()
%r = box $I0
}
}
method Bool { $.status };
}
class FakeDBD::PgPir:auth<moritz> {
sub pg_escape($x) {
q[']
~ $x.subst(rx/\\|\'/, -> $m { '\\' ~ $m }, :g)
~ q['];
}
method connect(Str $user, Str $password, Str $params, $RaiseError) {
my $pg = pir::new__pS('Pg');
my %params = $params.split(';').map({ .split(rx{\s*\=\s*}, 2)}).flat;
my %opt =
user => pg_escape($user),
password => pg_escape($password),
%params;
%opt<application_name> //= 'Perl6FakeDBD';
say "Options: %opt.perl()";
# nearly scary how concise this is in Perl 6 :-)
my $connection_string = %opt.fmt('%s=%s', ';');
my $con = $pg.connectdb($connection_string);
}
method finish() {
$!pg_conn.finish() if $.Bool;
}
}
# vim: ft=perl6