Skip to content

Commit

Permalink
SQLite: Now with typed Pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
salortiz committed Feb 26, 2016
1 parent a823fb2 commit bb1682c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 77 deletions.
10 changes: 4 additions & 6 deletions lib/DBDish/SQLite.pm6
@@ -1,7 +1,6 @@
unit class DBDish::SQLite:auth<mberends>:ver<0.0.1>;
use DBDish::SQLite::Native;
need DBDish::SQLite::Connection;
use NativeCall;

has $.Version = 0.01;
has $.errstr;
Expand All @@ -10,12 +9,11 @@ method connect(:$RaiseError, *%params) {
my $dbname = %params<dbname> // %params<database>;
die 'No "dbname" or "database" given' unless defined $dbname;

my @conn := CArray[OpaquePointer].new;
@conn[0] = OpaquePointer;
my $status = sqlite3_open($dbname, @conn);
my SQLite $p .= new;
my $status = sqlite3_open($dbname, $p);
if $status == SQLITE_OK {
return DBDish::SQLite::Connection.bless(
:conn(@conn[0]),
return DBDish::SQLite::Connection.new(
:conn($p),
:$RaiseError,
);
}
Expand Down
33 changes: 8 additions & 25 deletions lib/DBDish/SQLite/Connection.pm6
Expand Up @@ -5,9 +5,8 @@ need DBDish;
unit class DBDish::SQLite::Connection does DBDish::Role::Connection;
need DBDish::SQLite::StatementHandle;
use DBDish::SQLite::Native;
use NativeCall;

has $!conn;
has SQLite $!conn;
has @!sths;

method BUILD(:$!conn) { }
Expand All @@ -18,32 +17,16 @@ method !handle-error(Int $status) {
}

method prepare(Str $statement, $attr?) {
my @stmt := CArray[OpaquePointer].new;
@stmt[0] = OpaquePointer;
my $status;
if sqlite3_libversion_number() >= 3003009 {
$status = sqlite3_prepare_v2(
$!conn,
$statement,
-1,
@stmt,
CArray[OpaquePointer]
);
} else {
$status = sqlite3_prepare(
$!conn,
$statement,
-1,
@stmt,
CArray[OpaquePointer])
}
my $statement_handle = @stmt[0];
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.bless(
my $sth = DBDish::SQLite::StatementHandle.new(
:$!conn,
:$statement,
:$statement_handle,
:statement_handle($stmt),
:$.RaiseError,
:dbh(self),
);
Expand All @@ -52,7 +35,7 @@ method prepare(Str $statement, $attr?) {
}

method _remove_sth($sth) {
@!sths.=grep(* !=== $sth);
@!sths .= grep(* !=== $sth);
}

method rows() {
Expand Down
91 changes: 50 additions & 41 deletions lib/DBDish/SQLite/Native.pm6
Expand Up @@ -50,79 +50,88 @@ sub MyLibName {
}
constant LIB = &MyLibName;

sub sqlite3_errmsg(OpaquePointer $handle)
constant Null is export = Pointer;
class SQLite is export is repr('CPointer') { };
class STMT is export is repr('CPointer') { };
constant SQLITE_TRANSIENT = Pointer.new(-1);

sub sqlite3_errmsg(SQLite $handle)
returns Str
is native(LIB)
is export
{ ... }

sub sqlite3_open(Str $filename, CArray[OpaquePointer] $handle)
sub sqlite3_open(Str $filename, SQLite $handle is rw)
returns int32
is native(LIB)
is export
{ ... }

sub sqlite3_close(OpaquePointer)
sub sqlite3_close(SQLite)
returns int32
is native(LIB)
is export
{ ... }


sub sqlite3_prepare_v2 (
OpaquePointer $handle,
Str $statement,
int32 $statement_length,
CArray[OpaquePointer] $statement_handle,
CArray[OpaquePointer] $pz_tail
SQLite,
Str $statement is encoded('utf8'),
int32 $statement_length,
STMT $statement_handle is rw,
Pointer
)
returns int32
is native(LIB)
is export
{ ... }

sub sqlite3_prepare (
OpaquePointer $handle,
Str $statement,
int32 $statement_length,
CArray[OpaquePointer] $statement_handle,
CArray[OpaquePointer] $pz_tail
SQLite,
Str $statement is encoded('utf8'),
int32 $statement_length,
STMT $statement_handle is rw,
Pointer
)
returns int32
is native(LIB)
is export
{ ... }
sub sqlite3_step(OpaquePointer $statement_handle)

sub sqlite3_step(STMT $statement_handle)
returns int32
is native(LIB)
is export
{ ... }


sub sqlite3_libversion_number() returns int32 is native(LIB) is export { ... };
sub sqlite3_bind_blob(OpaquePointer $stmt, int32, OpaquePointer, int32, OpaquePointer) returns int32 is native(LIB) is export { ... };
sub sqlite3_bind_double(OpaquePointer $stmt, int32, num64) returns int32 is native(LIB) is export { ... };
sub sqlite3_bind_int64(OpaquePointer $stmt, int32, int64) returns int32 is native(LIB) is export { ... };
sub sqlite3_bind_null(OpaquePointer $stmt, int32) returns int32 is native(LIB) is export { ... };
sub sqlite3_bind_text(OpaquePointer $stmt, int32, Str, int32, OpaquePointer) returns int32 is native(LIB) is export { ... };

sub sqlite3_changes(OpaquePointer $handle) returns int32 is native(LIB) is export { ... };

proto sub sqlite3_bind($, $, $) {*}
multi sub sqlite3_bind($stmt, Int $n, Buf:D $b) is export { sqlite3_bind_blob($stmt, $n, $b, $b.bytes, OpaquePointer) }
multi sub sqlite3_bind($stmt, Int $n, Real:D $d) is export { sqlite3_bind_double($stmt, $n, $d.Num) }
multi sub sqlite3_bind($stmt, Int $n, Int:D $i) is export { sqlite3_bind_int64($stmt, $n, $i) }
multi sub sqlite3_bind($stmt, Int $n, Any:U) is export { sqlite3_bind_null($stmt, $n) }
multi sub sqlite3_bind($stmt, Int $n, Str:D $d) is export { sqlite3_bind_text($stmt, $n, $d, -1, OpaquePointer) }

sub sqlite3_reset(OpaquePointer) returns int32 is native(LIB) is export { ... }

sub sqlite3_column_text(OpaquePointer, int32) returns Str is native(LIB) is export { ... }
sub sqlite3_column_double(OpaquePointer, int32) returns num64 is native(LIB) is export { ... }
sub sqlite3_column_int64(OpaquePointer, int32) returns int64 is native(LIB) is export { ... }

sub sqlite3_finalize(OpaquePointer) returns int32 is native(LIB) is export { ... }
sub sqlite3_column_count(OpaquePointer) returns int32 is native(LIB) is export { ... }
sub sqlite3_column_name(OpaquePointer, int32) returns Str is native(LIB) is export { ... }
sub sqlite3_column_type(OpaquePointer, int32) returns int32 is native(LIB) is export { ... }
sub sqlite3_bind_blob(STMT, int32, OpaquePointer, int32, OpaquePointer) returns int32 is native(LIB) is export { ... };
sub sqlite3_bind_double(STMT, int32, num64) returns int32 is native(LIB) is export { ... };
sub sqlite3_bind_int64(STMT, int32, int64) returns int32 is native(LIB) is export { ... };
sub sqlite3_bind_null(STMT, int32) returns int32 is native(LIB) is export { ... };
sub sqlite3_bind_text(STMT, int32, Str is encoded('utf8'), int32, Pointer) returns int32 is native(LIB) is export { ... };

sub sqlite3_changes(SQLite) returns int32 is native(LIB) is export { ... };

proto sub sqlite3_bind(STMT, $, $) {*}
multi sub sqlite3_bind(STMT $stmt, Int $n, Buf:D $b) is export { sqlite3_bind_blob($stmt, $n, $b, $b.bytes, OpaquePointer) }
multi sub sqlite3_bind(STMT $stmt, Int $n, Real:D $d) is export { sqlite3_bind_double($stmt, $n, $d.Num) }
multi sub sqlite3_bind(STMT $stmt, Int $n, Int:D $i) is export { sqlite3_bind_int64($stmt, $n, $i) }
multi sub sqlite3_bind(STMT $stmt, Int $n, Any:U) is export { sqlite3_bind_null($stmt, $n) }
multi sub sqlite3_bind(STMT $stmt, Int $n, Str:D $d) is export {
sqlite3_bind_text($stmt, $n, $d, -1, SQLITE_TRANSIENT)
}

sub sqlite3_reset(STMT) returns int32 is native(LIB) is export { ... }

sub sqlite3_column_text(STMT, int32) returns Str is native(LIB) is export { ... }
sub sqlite3_column_double(STMT, int32) returns num64 is native(LIB) is export { ... }
sub sqlite3_column_int64(STMT, int32) returns int64 is native(LIB) is export { ... }
sub sqlite3_column_blob(STMT, int32) returns Pointer is native(LIB) is export { ... }
sub sqlite3_column_bytes(STMT, int32) returns int32 is native(LIB) is export { ... }

sub sqlite3_finalize(STMT) returns int32 is native(LIB) is export { ... }
sub sqlite3_column_count(STMT) returns int32 is native(LIB) is export { ... }
sub sqlite3_column_name(STMT, int32) returns Str is native(LIB) is export { ... }
sub sqlite3_column_type(STMT, int32) returns int32 is native(LIB) is export { ... }
18 changes: 13 additions & 5 deletions lib/DBDish/SQLite/StatementHandle.pm6
Expand Up @@ -6,7 +6,7 @@ unit class DBDish::SQLite::StatementHandle does DBDish::Role::StatementHandle;
use DBDish::SQLite::Native;
use NativeCall;

has $!conn;
has SQLite $!conn;
has $.statement;
has $!statement_handle;
has $.dbh;
Expand All @@ -27,7 +27,6 @@ method execute(*@params) {
@!mem_rows = ();
for @params.kv -> $idx, $v {
if $v ~~ Str {
explicitly-manage($v);
@!mem_rows.push: $v;
}
self!handle-error(sqlite3_bind($!statement_handle, $idx + 1, $v));
Expand All @@ -40,7 +39,7 @@ method execute(*@params) {
}

method rows {
die 'Cannot determine rows of closed connection' unless $!conn.DEFINITE;
die 'Cannot determine rows of closed connection' unless $!conn;
my $rows = sqlite3_changes($!conn);
$rows == 0 ?? '0E0' !! $rows;
}
Expand Down Expand Up @@ -69,9 +68,18 @@ method _row (:$hash) {
}
when SQLITE_FLOAT {
$value = sqlite3_column_double($!statement_handle, $col);
$value = $value.Rat;
$value = $value.Rat; # FIXME
}
default {
when SQLITE_BLOB {
... # TODO WIP
$value = sqlite3_column_blob($!statement_handle, $col);
}
when SQLITE_NULL {
# SQLite can't determine the type of NULL column, so instead
# of lyng, prefer an explicit Nil.
$value = Nil;
}
default {
$value = sqlite3_column_text($!statement_handle, $col);
}
}
Expand Down

0 comments on commit bb1682c

Please sign in to comment.