Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Simplified changes to NativeCall.pm6 so that mysql works again.

Get sqlite working via inline UnManagedStruct creation. Need to find a better way.
  • Loading branch information...
commit 76f5c3297b9e7914c06aaffc993bd72be8b9db85 1 parent d165fe8
@donaldh donaldh authored
Showing with 81 additions and 27 deletions.
  1. +79 −13 examples/sqlite3.p6
  2. +2 −14 lib/NativeCall.pm6
View
92 examples/sqlite3.p6
@@ -73,11 +73,22 @@ sub sqlite3_table_column_metadata(OpaquePointer $ppDB, Str $zDbName, Str $zTable
is native('libsqlite3')
{ ... }
+sub sqlite3_errmsg(OpaquePointer $ppDB)
+ returns Str
+ is native('libsqlite3')
+ { ... }
+
# ----------------------- main example program -------------------------
-my OpaquePointer $db = OpaquePointer.new;
-my OpaquePointer $stmt = OpaquePointer.new;
-my OpaquePointer $pzTail = OpaquePointer.new;
+sub report_status(Int $status, Str $op) {
+ say "$op status: $status";
+ say sqlite3_errmsg($db) if $status;
+}
+
+
+my $db = pir::new__Ps('UnManagedStruct');
+my $stmt = pir::new__Ps('UnManagedStruct');
+my $pzTail = pir::new__Ps('UnManagedStruct');
my Positional of Str $pzDataType;
my Positional of Str $pzCollSeq;
my Positional of Int $pNotNull;
@@ -85,19 +96,74 @@ my Positional of Int $pPrimaryKey;
my Positional of Int $pAutoinc;
my $status = sqlite3_open( "test.db", $db );
-say "open status $status";
+report_status($status, 'open');
$status = sqlite3_exec( $db, "CREATE TABLE a ( b INT );", pir::null__P(), pir::null__P(), pir::null__P() );
-say "exec status $status";
-$status = sqlite3_prepare_v2( $db, "CREATE TABLE a ( b INT );", 0, $stmt, $pzTail );
-say "prepare status $status";
-$status = sqlite3_table_column_metadata($db,"","","",$pzDataType,$pzCollSeq,$pNotNull,$pPrimaryKey,$pAutoinc);
-say "table_column_metadata status $status";
+report_status($status, 'exec');
+
+$status = sqlite3_prepare_v2( $db, "CREATE TABLE a ( b INT );", -1, $stmt, $pzTail );
+report_status($status, 'prepare');
+
+$status = sqlite3_table_column_metadata($db,
+ pir::null__P(),"a","b",
+ $pzDataType,$pzCollSeq,$pNotNull,$pPrimaryKey,$pAutoinc);
+report_status($status, 'table_column_metadata');
+
$status = sqlite3_column_text( $stmt, 1 );
-say "step status $status";
+say "column text returns: $status";
+
$status = sqlite3_step( $stmt );
-say "step status $status";
+report_status($status, 'step');
+
$status = sqlite3_finalize( $stmt );
-say "finalize status $status";
+report_status($status, 'finalize');
+
+$status = sqlite3_exec( $db, "
+ CREATE TABLE nom (
+ name char(4),
+ description char(30),
+ quantity int,
+ price numeric(5,2)
+ )
+", pir::null__P(), pir::null__P(), pir::null__P());
+report_status($status, 'exec');
+
+$status = sqlite3_exec( $db, "
+ INSERT INTO nom (name, description, quantity, price)
+ VALUES ( 'BUBH', 'Hot beef burrito', 1, 4.95 )
+", pir::null__P(), pir::null__P(), pir::null__P());
+report_status($status, 'insert');
+
+$status = sqlite3_exec( $db, "
+ INSERT INTO nom (name, description, quantity, price)
+ VALUES ( 'TAFM', 'Mild fish taco', 1, 4.85 )
+", pir::null__P(), pir::null__P(), pir::null__P());
+report_status($status, 'insert');
+
+$status = sqlite3_exec( $db, "
+ INSERT INTO nom (name, description, quantity, price)
+ VALUES ( 'BEOM', 'Medium size orange juice', 2, 1.20 )
+", pir::null__P(), pir::null__P(), pir::null__P());
+report_status($status, 'insert');
+
+$status = sqlite3_prepare_v2( $db, "SELECT * FROM nom;", -1, $stmt, $pzTail );
+report_status($status, 'prepare');
+
+$status = sqlite3_step($stmt);
+report_status($status, 'step');
+while $status == 100 {
+ my $columns = sqlite3_column_count($stmt);
+ for 0 .. $columns - 1 -> $col {
+ my $text = sqlite3_column_text($stmt, $col);
+ print "\t", $text;
+ }
+ say '';
+ $status = sqlite3_step($stmt);
+ report_status($status, 'step');
+};
+$status = sqlite3_finalize($stmt);
+report_status($status, 'finalize');
+
$status = sqlite3_close( $db );
-say "close status $status";
+report_status($status, 'close');
+
View
16 lib/NativeCall.pm6
@@ -1,10 +1,4 @@
-class OpaquePointer {
- has $!unmanaged = pir::new__Ps('UnManagedStruct');
-
- method ref() {
- $!unmanaged;
- }
-}
+class OpaquePointer { }
class NativeArray {
has $!unmanaged;
@@ -112,19 +106,13 @@ our multi trait_mod:<is>(Routine $r, $libname?, :$native!) {
}
}
pir::setattribute__vPsP($r, '$!do', -> |$c {
- my $backend-capture = |$c.map({
- given $_ {
- when OpaquePointer { pir::descalarref__PP($_.ref) }
- default { $_ }
- }
- });
$return-mapper(
pir::descalarref__PP( (pir::dlfunc__PPss(
($lib ?? pir::descalarref__PP($lib) !! pir::null__P()),
$entry-point,
$call-sig
) // die("Could not locate symbol '$entry-point' in native library '{$libname || q<(resident)>}'")
- ).(|$backend-capture) )
+ ).(|$c) )
)
});
}
Please sign in to comment.
Something went wrong with that request. Please try again.