/
Connection.pm6
59 lines (49 loc) · 1.39 KB
/
Connection.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
use v6;
need DBDish;
unit class DBDish::SQLite::Connection does DBDish::Role::Connection;
need DBDish::SQLite::StatementHandle;
use DBDish::SQLite::Native;
has SQLite $!conn;
has @!sths;
method BUILD(:$!conn) { }
method !handle-error(Int $status) {
return if $status == SQLITE_OK;
self!set_errstr(join ' ', SQLITE($status), sqlite3_errmsg($!conn));
}
method prepare(Str $statement, $attr?) {
my STMT $stmt .= new;
my $status = (sqlite3_libversion_number() >= 3003009)
?? sqlite3_prepare_v2($!conn, $statement, -1, $stmt, Null)
!! sqlite3_prepare($!conn, $statement, -1, $stmt, Null);
self!handle-error($status);
return Nil unless $status == SQLITE_OK;
my $sth = DBDish::SQLite::StatementHandle.new(
:$!conn,
:$statement,
:statement_handle($stmt),
:$.RaiseError,
:dbh(self),
);
@!sths.push: $sth;
$sth;
}
method _remove_sth($sth) {
@!sths .= grep(* !=== $sth);
}
method rows() {
die 'Cannot determine rows of closed connection' unless $!conn.DEFINITE;
my $rows = sqlite3_changes($!conn);
$rows == 0 ?? '0E0' !! $rows;
}
method do(Str $sql, *@args) {
my $sth = self.prepare($sql);
$sth.execute(@args);
my $res = $sth.rows || '0e0';
$sth.finish;
return $sth;
}
method disconnect() {
.free for @!sths;
self!handle-error(sqlite3_close($!conn));
return not self.errstr;
}