getAggregateState(sqlite3_context cx, T initialValue){
+ return map.getAggregateState(cx, initialValue);
+ }
+
+ /**
+ To be called from the implementation's xFinal() method to fetch
+ the final state of the UDF and remove its mapping.
+
+ see SQLFunction.PerContextState#takeAggregateState
+ */
+ protected final T takeAggregateState(sqlite3_context cx){
+ return map.takeAggregateState(cx);
+ }
+}
diff --git a/ext/jni/src/org/sqlite/jni/capi/AuthorizerCallback.java b/ext/jni/src/org/sqlite/jni/capi/AuthorizerCallback.java
new file mode 100644
index 0000000000..ce7c6fca6d
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/AuthorizerCallback.java
@@ -0,0 +1,28 @@
+/*
+** 2023-08-25
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+import org.sqlite.jni.annotation.*;
+
+/**
+ Callback for use with {@link CApi#sqlite3_set_authorizer}.
+*/
+public interface AuthorizerCallback extends CallbackProxy {
+ /**
+ Must function as described for the C-level
+ sqlite3_set_authorizer() callback.
+ */
+ int call(int opId, @Nullable String s1, @Nullable String s2,
+ @Nullable String s3, @Nullable String s4);
+
+}
diff --git a/ext/jni/src/org/sqlite/jni/capi/AutoExtensionCallback.java b/ext/jni/src/org/sqlite/jni/capi/AutoExtensionCallback.java
new file mode 100644
index 0000000000..7a54132d29
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/AutoExtensionCallback.java
@@ -0,0 +1,40 @@
+/*
+** 2023-08-25
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+
+/**
+ Callback for use with the {@link CApi#sqlite3_auto_extension}
+ family of APIs.
+*/
+public interface AutoExtensionCallback extends CallbackProxy {
+ /**
+ Must function as described for a C-level
+ sqlite3_auto_extension() callback.
+
+ This callback may throw and the exception's error message will
+ be set as the db's error string.
+
+
Tips for implementations:
+
+
- Opening a database from an auto-extension handler will lead to
+ an endless recursion of the auto-handler triggering itself
+ indirectly for each newly-opened database.
+
+
- If this routine is stateful, it may be useful to make the
+ overridden method synchronized.
+
+
- Results are undefined if the given db is closed by an auto-extension.
+ */
+ int call(sqlite3 db);
+}
diff --git a/ext/jni/src/org/sqlite/jni/capi/BusyHandlerCallback.java b/ext/jni/src/org/sqlite/jni/capi/BusyHandlerCallback.java
new file mode 100644
index 0000000000..00223f0b66
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/BusyHandlerCallback.java
@@ -0,0 +1,26 @@
+/*
+** 2023-08-25
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+
+/**
+ Callback for use with {@link CApi#sqlite3_busy_handler}.
+*/
+public interface BusyHandlerCallback extends CallbackProxy {
+ /**
+ Must function as documented for the C-level
+ sqlite3_busy_handler() callback argument, minus the (void*)
+ argument the C-level function requires.
+ */
+ int call(int n);
+}
diff --git a/ext/jni/src/org/sqlite/jni/capi/CApi.java b/ext/jni/src/org/sqlite/jni/capi/CApi.java
new file mode 100644
index 0000000000..302cdb760e
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/CApi.java
@@ -0,0 +1,2449 @@
+/*
+** 2023-07-21
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file declares JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+import java.nio.charset.StandardCharsets;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import org.sqlite.jni.annotation.*;
+import java.util.Arrays;
+
+/**
+ This class contains the entire C-style sqlite3 JNI API binding,
+ minus a few bits and pieces declared in other files. For client-side
+ use, a static import is recommended:
+
+
{@code
+ import static org.sqlite.jni.capi.CApi.*;
+ }
+
+ The C-side part can be found in sqlite3-jni.c.
+
+
This class is package-private in order to keep Java clients from
+ having direct access to the low-level C-style APIs, a design
+ decision made by Java developers based on the C-style API being
+ riddled with opportunities for Java developers to proverbially shoot
+ themselves in the foot with. Third-party copies of this code may
+ eliminate that guard by simply changing this class from
+ package-private to public. Its methods which are intended to be
+ exposed that way are all public.
+
+
Only functions which materially differ from their C counterparts
+ are documented here, and only those material differences are
+ documented. The C documentation is otherwise applicable for these
+ APIs:
+
+
https://sqlite.org/c3ref/intro.html
+
+
A handful of Java-specific APIs have been added which are
+ documented here. A number of convenience overloads are provided
+ which are not documented but whose semantics map 1-to-1 in an
+ intuitive manner. e.g. {@link
+ #sqlite3_result_set(sqlite3_context,int)} is equivalent to {@link
+ #sqlite3_result_int}, and sqlite3_result_set() has many
+ type-specific overloads.
+
+
Notes regarding Java's Modified UTF-8 vs standard UTF-8:
+
+
SQLite internally uses UTF-8 encoding, whereas Java natively uses
+ UTF-16. Java JNI has routines for converting to and from UTF-8,
+ but JNI uses what its docs call modified UTF-8 (see links below)
+ Care must be taken when converting Java strings to or from standard
+ UTF-8 to ensure that the proper conversion is performed. In short,
+ Java's {@code String.getBytes(StandardCharsets.UTF_8)} performs the proper
+ conversion in Java, and there are no JNI C APIs for that conversion
+ (JNI's {@code NewStringUTF()} requires its input to be in MUTF-8).
+
+
The known consequences and limitations this discrepancy places on
+ the SQLite3 JNI binding include:
+
+
+
+ - C functions which take C-style strings without a length argument
+ require special care when taking input from Java. In particular,
+ Java strings converted to byte arrays for encoding purposes are not
+ NUL-terminated, and conversion to a Java byte array must sometimes
+ be careful to add one. Functions which take a length do not require
+ this so long as the length is provided. Search the CApi class
+ for "\0" for many examples.
+
+
+
+ Further reading:
+
+
https://stackoverflow.com/questions/57419723
+
https://stackoverflow.com/questions/7921016
+
https://itecnote.com/tecnote/java-getting-true-utf-8-characters-in-java-jni/
+
https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#unicode
+
https://docs.oracle.com/javase/8/docs/api/java/io/DataInput.html#modified-utf-8
+
+*/
+public final class CApi {
+ static {
+ System.loadLibrary("sqlite3-jni");
+ }
+ //! Not used
+ private CApi(){}
+ //! Called from static init code.
+ private static native void init();
+
+ /**
+ Returns a nul-terminated copy of s as a UTF-8-encoded byte array,
+ or null if s is null.
+ */
+ private static byte[] nulTerminateUtf8(String s){
+ return null==s ? null : (s+"\0").getBytes(StandardCharsets.UTF_8);
+ }
+
+ /**
+ Each thread which uses the SQLite3 JNI APIs should call
+ sqlite3_jni_uncache_thread() when it is done with the library -
+ either right before it terminates or when it finishes using the
+ SQLite API. This will clean up any cached per-thread info.
+
+
This process does not close any databases or finalize
+ any prepared statements because their ownership does not depend on
+ a given thread. For proper library behavior, and to
+ avoid C-side leaks, be sure to finalize all statements and close
+ all databases before calling this function.
+
+
Calling this from the main application thread is not strictly
+ required. Additional threads must call this before ending or they
+ will leak cache entries in the C heap, which in turn may keep
+ numerous Java-side global references active.
+
+
This routine returns false without side effects if the current
+ JNIEnv is not cached, else returns true, but this information is
+ primarily for testing of the JNI bindings and is not information
+ which client-level code can use to make any informed decisions.
+ */
+ public static native boolean sqlite3_java_uncache_thread();
+
+ //////////////////////////////////////////////////////////////////////
+ // Maintenance reminder: please keep the sqlite3_.... functions
+ // alphabetized. The SQLITE_... values. on the other hand, are
+ // grouped by category.
+
+ /**
+ Functions exactly like the native form except that (A) the 2nd
+ argument is a boolean instead of an int and (B) the returned
+ value is not a pointer address and is only intended for use as a
+ per-UDF-call lookup key in a higher-level data structure.
+
+
Passing a true second argument is analogous to passing some
+ unspecified small, non-0 positive value to the C API and passing
+ false is equivalent to passing 0 to the C API.
+
+
Like the C API, it returns 0 if allocation fails or if
+ initialize is false and no prior aggregate context was allocated
+ for cx. If initialize is true then it returns 0 only on
+ allocation error. In all casses, 0 is considered the sentinel
+ "not a key" value.
+ */
+ public static native long sqlite3_aggregate_context(sqlite3_context cx, boolean initialize);
+
+ /**
+ Functions almost as documented for the C API, with these
+ exceptions:
+
+
- The callback interface is shorter because of
+ cross-language differences. Specifically, 3rd argument to the C
+ auto-extension callback interface is unnecessary here.
+
+
The C API docs do not specifically say so, but if the list of
+ auto-extensions is manipulated from an auto-extension, it is
+ undefined which, if any, auto-extensions will subsequently
+ execute for the current database. That is, doing so will result
+ in unpredictable, but not undefined, behavior.
+
+
See the AutoExtension class docs for more information.
+ */
+ public static native int sqlite3_auto_extension(@NotNull AutoExtensionCallback callback);
+
+ static native int sqlite3_backup_finish(@NotNull long ptrToBackup);
+
+ public static int sqlite3_backup_finish(@NotNull sqlite3_backup b){
+ return sqlite3_backup_finish(b.clearNativePointer());
+ }
+
+ static native sqlite3_backup sqlite3_backup_init(
+ @NotNull long ptrToDbDest, @NotNull String destTableName,
+ @NotNull long ptrToDbSrc, @NotNull String srcTableName
+ );
+
+ public static sqlite3_backup sqlite3_backup_init(
+ @NotNull sqlite3 dbDest, @NotNull String destTableName,
+ @NotNull sqlite3 dbSrc, @NotNull String srcTableName
+ ){
+ return sqlite3_backup_init( dbDest.getNativePointer(), destTableName,
+ dbSrc.getNativePointer(), srcTableName );
+ }
+
+ static native int sqlite3_backup_pagecount(@NotNull long ptrToBackup);
+
+ public static int sqlite3_backup_pagecount(@NotNull sqlite3_backup b){
+ return sqlite3_backup_pagecount(b.getNativePointer());
+ }
+
+ static native int sqlite3_backup_remaining(@NotNull long ptrToBackup);
+
+ public static int sqlite3_backup_remaining(@NotNull sqlite3_backup b){
+ return sqlite3_backup_remaining(b.getNativePointer());
+ }
+
+ static native int sqlite3_backup_step(@NotNull long ptrToBackup, int nPage);
+
+ public static int sqlite3_backup_step(@NotNull sqlite3_backup b, int nPage){
+ return sqlite3_backup_step(b.getNativePointer(), nPage);
+ }
+
+ static native int sqlite3_bind_blob(
+ @NotNull long ptrToStmt, int ndx, @Nullable byte[] data, int n
+ );
+
+ /**
+ If n is negative, SQLITE_MISUSE is returned. If n>data.length
+ then n is silently truncated to data.length.
+ */
+ static int sqlite3_bind_blob(
+ @NotNull sqlite3_stmt stmt, int ndx, @Nullable byte[] data, int n
+ ){
+ return sqlite3_bind_blob(stmt.getNativePointer(), ndx, data, n);
+ }
+
+ public static int sqlite3_bind_blob(
+ @NotNull sqlite3_stmt stmt, int ndx, @Nullable byte[] data
+ ){
+ return (null==data)
+ ? sqlite3_bind_null(stmt.getNativePointer(), ndx)
+ : sqlite3_bind_blob(stmt.getNativePointer(), ndx, data, data.length);
+ }
+
+ static native int sqlite3_bind_double(
+ @NotNull long ptrToStmt, int ndx, double v
+ );
+
+ public static int sqlite3_bind_double(
+ @NotNull sqlite3_stmt stmt, int ndx, double v
+ ){
+ return sqlite3_bind_double(stmt.getNativePointer(), ndx, v);
+ }
+
+ static native int sqlite3_bind_int(
+ @NotNull long ptrToStmt, int ndx, int v
+ );
+
+ public static int sqlite3_bind_int(
+ @NotNull sqlite3_stmt stmt, int ndx, int v
+ ){
+ return sqlite3_bind_int(stmt.getNativePointer(), ndx, v);
+ }
+
+ static native int sqlite3_bind_int64(
+ @NotNull long ptrToStmt, int ndx, long v
+ );
+
+ public static int sqlite3_bind_int64(@NotNull sqlite3_stmt stmt, int ndx, long v){
+ return sqlite3_bind_int64( stmt.getNativePointer(), ndx, v );
+ }
+
+ static native int sqlite3_bind_java_object(
+ @NotNull long ptrToStmt, int ndx, @Nullable Object o
+ );
+
+ /**
+ Binds the given object at the given index. If o is null then this behaves like
+ sqlite3_bind_null().
+
+ @see #sqlite3_result_java_object
+ */
+ public static int sqlite3_bind_java_object(
+ @NotNull sqlite3_stmt stmt, int ndx, @Nullable Object o
+ ){
+ return sqlite3_bind_java_object(stmt.getNativePointer(), ndx, o);
+ }
+
+ static native int sqlite3_bind_null(@NotNull long ptrToStmt, int ndx);
+
+ public static int sqlite3_bind_null(@NotNull sqlite3_stmt stmt, int ndx){
+ return sqlite3_bind_null(stmt.getNativePointer(), ndx);
+ }
+
+ static native int sqlite3_bind_parameter_count(@NotNull long ptrToStmt);
+
+ public static int sqlite3_bind_parameter_count(@NotNull sqlite3_stmt stmt){
+ return sqlite3_bind_parameter_count(stmt.getNativePointer());
+ }
+
+ /**
+ Requires that paramName be a NUL-terminated UTF-8 string.
+
+ This overload is private because: (A) to keep users from
+ inadvertently passing non-NUL-terminated byte arrays (an easy
+ thing to do). (B) it is cheaper to NUL-terminate the
+ String-to-byte-array conversion in the public-facing Java-side
+ overload than to do that in C, so that signature is the
+ public-facing one.
+ */
+ private static native int sqlite3_bind_parameter_index(
+ @NotNull long ptrToStmt, @NotNull byte[] paramName
+ );
+
+ public static int sqlite3_bind_parameter_index(
+ @NotNull sqlite3_stmt stmt, @NotNull String paramName
+ ){
+ final byte[] utf8 = nulTerminateUtf8(paramName);
+ return null==utf8 ? 0 : sqlite3_bind_parameter_index(stmt.getNativePointer(), utf8);
+ }
+
+ static native String sqlite3_bind_parameter_name(
+ @NotNull long ptrToStmt, int index
+ );
+
+ public static String sqlite3_bind_parameter_name(@NotNull sqlite3_stmt stmt, int index){
+ return sqlite3_bind_parameter_name(stmt.getNativePointer(), index);
+ }
+
+ static native int sqlite3_bind_text(
+ @NotNull long ptrToStmt, int ndx, @Nullable byte[] utf8, int maxBytes
+ );
+
+ /**
+ Works like the C-level sqlite3_bind_text() but assumes
+ SQLITE_TRANSIENT for the final C API parameter. The byte array is
+ assumed to be in UTF-8 encoding.
+
+
If data is not null and maxBytes>utf8.length then maxBytes is
+ silently truncated to utf8.length. If maxBytes is negative then
+ results are undefined if data is not null and does not contain a
+ NUL byte.
+ */
+ static int sqlite3_bind_text(
+ @NotNull sqlite3_stmt stmt, int ndx, @Nullable byte[] utf8, int maxBytes
+ ){
+ return sqlite3_bind_text(stmt.getNativePointer(), ndx, utf8, maxBytes);
+ }
+
+ /**
+ Converts data, if not null, to a UTF-8-encoded byte array and
+ binds it as such, returning the result of the C-level
+ sqlite3_bind_null() or sqlite3_bind_text().
+ */
+ public static int sqlite3_bind_text(
+ @NotNull sqlite3_stmt stmt, int ndx, @Nullable String data
+ ){
+ if( null==data ) return sqlite3_bind_null(stmt.getNativePointer(), ndx);
+ final byte[] utf8 = data.getBytes(StandardCharsets.UTF_8);
+ return sqlite3_bind_text(stmt.getNativePointer(), ndx, utf8, utf8.length);
+ }
+
+ /**
+ Requires that utf8 be null or in UTF-8 encoding.
+ */
+ public static int sqlite3_bind_text(
+ @NotNull sqlite3_stmt stmt, int ndx, @Nullable byte[] utf8
+ ){
+ return ( null==utf8 )
+ ? sqlite3_bind_null(stmt.getNativePointer(), ndx)
+ : sqlite3_bind_text(stmt.getNativePointer(), ndx, utf8, utf8.length);
+ }
+
+ static native int sqlite3_bind_text16(
+ @NotNull long ptrToStmt, int ndx, @Nullable byte[] data, int maxBytes
+ );
+
+ /**
+ Identical to the sqlite3_bind_text() overload with the same
+ signature but requires that its input be encoded in UTF-16 in
+ platform byte order.
+ */
+ static int sqlite3_bind_text16(
+ @NotNull sqlite3_stmt stmt, int ndx, @Nullable byte[] data, int maxBytes
+ ){
+ return sqlite3_bind_text16(stmt.getNativePointer(), ndx, data, maxBytes);
+ }
+
+ /**
+ Converts its string argument to UTF-16 and binds it as such, returning
+ the result of the C-side function of the same name. The 3rd argument
+ may be null.
+ */
+ public static int sqlite3_bind_text16(
+ @NotNull sqlite3_stmt stmt, int ndx, @Nullable String data
+ ){
+ if(null == data) return sqlite3_bind_null(stmt, ndx);
+ final byte[] bytes = data.getBytes(StandardCharsets.UTF_16);
+ return sqlite3_bind_text16(stmt.getNativePointer(), ndx, bytes, bytes.length);
+ }
+
+ /**
+ Requires that data be null or in UTF-16 encoding in platform byte
+ order. Returns the result of the C-level sqlite3_bind_null() or
+ sqlite3_bind_text16().
+ */
+ public static int sqlite3_bind_text16(
+ @NotNull sqlite3_stmt stmt, int ndx, @Nullable byte[] data
+ ){
+ return (null == data)
+ ? sqlite3_bind_null(stmt.getNativePointer(), ndx)
+ : sqlite3_bind_text16(stmt.getNativePointer(), ndx, data, data.length);
+ }
+
+ static native int sqlite3_bind_value(@NotNull long ptrToStmt, int ndx, long ptrToValue);
+
+ /**
+ Functions like the C-level sqlite3_bind_value(), or
+ sqlite3_bind_null() if val is null.
+ */
+ public static int sqlite3_bind_value(@NotNull sqlite3_stmt stmt, int ndx, sqlite3_value val){
+ return sqlite3_bind_value(stmt.getNativePointer(), ndx,
+ null==val ? 0L : val.getNativePointer());
+ }
+
+ static native int sqlite3_bind_zeroblob(@NotNull long ptrToStmt, int ndx, int n);
+
+ public static int sqlite3_bind_zeroblob(@NotNull sqlite3_stmt stmt, int ndx, int n){
+ return sqlite3_bind_zeroblob(stmt.getNativePointer(), ndx, n);
+ }
+
+ static native int sqlite3_bind_zeroblob64(
+ @NotNull long ptrToStmt, int ndx, long n
+ );
+
+ public static int sqlite3_bind_zeroblob64(@NotNull sqlite3_stmt stmt, int ndx, long n){
+ return sqlite3_bind_zeroblob64(stmt.getNativePointer(), ndx, n);
+ }
+
+ static native int sqlite3_blob_bytes(@NotNull long ptrToBlob);
+
+ public static int sqlite3_blob_bytes(@NotNull sqlite3_blob blob){
+ return sqlite3_blob_bytes(blob.getNativePointer());
+ }
+
+ static native int sqlite3_blob_close(@Nullable long ptrToBlob);
+
+ public static int sqlite3_blob_close(@Nullable sqlite3_blob blob){
+ return sqlite3_blob_close(blob.clearNativePointer());
+ }
+
+ static native int sqlite3_blob_open(
+ @NotNull long ptrToDb, @NotNull String dbName,
+ @NotNull String tableName, @NotNull String columnName,
+ long iRow, int flags, @NotNull OutputPointer.sqlite3_blob out
+ );
+
+ public static int sqlite3_blob_open(
+ @NotNull sqlite3 db, @NotNull String dbName,
+ @NotNull String tableName, @NotNull String columnName,
+ long iRow, int flags, @NotNull OutputPointer.sqlite3_blob out
+ ){
+ return sqlite3_blob_open(db.getNativePointer(), dbName, tableName,
+ columnName, iRow, flags, out);
+ }
+
+ /**
+ Convenience overload.
+ */
+ public static sqlite3_blob sqlite3_blob_open(
+ @NotNull sqlite3 db, @NotNull String dbName,
+ @NotNull String tableName, @NotNull String columnName,
+ long iRow, int flags ){
+ final OutputPointer.sqlite3_blob out = new OutputPointer.sqlite3_blob();
+ sqlite3_blob_open(db.getNativePointer(), dbName, tableName, columnName,
+ iRow, flags, out);
+ return out.take();
+ };
+
+ static native int sqlite3_blob_read(
+ @NotNull long ptrToBlob, @NotNull byte[] target, int iOffset
+ );
+
+ public static int sqlite3_blob_read(
+ @NotNull sqlite3_blob b, @NotNull byte[] target, int iOffset
+ ){
+ return sqlite3_blob_read(b.getNativePointer(), target, iOffset);
+ }
+
+ static native int sqlite3_blob_reopen(
+ @NotNull long ptrToBlob, long newRowId
+ );
+
+ public static int sqlite3_blob_reopen(@NotNull sqlite3_blob b, long newRowId){
+ return sqlite3_blob_reopen(b.getNativePointer(), newRowId);
+ }
+
+ static native int sqlite3_blob_write(
+ @NotNull long ptrToBlob, @NotNull byte[] bytes, int iOffset
+ );
+
+ public static int sqlite3_blob_write(
+ @NotNull sqlite3_blob b, @NotNull byte[] bytes, int iOffset
+ ){
+ return sqlite3_blob_write(b.getNativePointer(), bytes, iOffset);
+ }
+
+ static native int sqlite3_busy_handler(
+ @NotNull long ptrToDb, @Nullable BusyHandlerCallback handler
+ );
+
+ /**
+ As for the C-level function of the same name, with a
+ BusyHandlerCallback instance in place of a callback
+ function. Pass it a null handler to clear the busy handler.
+ */
+ public static int sqlite3_busy_handler(
+ @NotNull sqlite3 db, @Nullable BusyHandlerCallback handler
+ ){
+ return sqlite3_busy_handler(db.getNativePointer(), handler);
+ }
+
+ static native int sqlite3_busy_timeout(@NotNull long ptrToDb, int ms);
+
+ public static int sqlite3_busy_timeout(@NotNull sqlite3 db, int ms){
+ return sqlite3_busy_timeout(db.getNativePointer(), ms);
+ }
+
+ public static native boolean sqlite3_cancel_auto_extension(
+ @NotNull AutoExtensionCallback ax
+ );
+
+ static native int sqlite3_changes(@NotNull long ptrToDb);
+
+ public static int sqlite3_changes(@NotNull sqlite3 db){
+ return sqlite3_changes(db.getNativePointer());
+ }
+
+ static native long sqlite3_changes64(@NotNull long ptrToDb);
+
+ public static long sqlite3_changes64(@NotNull sqlite3 db){
+ return sqlite3_changes64(db.getNativePointer());
+ }
+
+ static native int sqlite3_clear_bindings(@NotNull long ptrToStmt);
+
+ public static int sqlite3_clear_bindings(@NotNull sqlite3_stmt stmt){
+ return sqlite3_clear_bindings(stmt.getNativePointer());
+ }
+
+ static native int sqlite3_close(@Nullable long ptrToDb);
+
+ public static int sqlite3_close(@Nullable sqlite3 db){
+ int rc = 0;
+ if( null!=db ){
+ rc = sqlite3_close(db.getNativePointer());
+ if( 0==rc ) db.clearNativePointer();
+ }
+ return rc;
+ }
+
+ static native int sqlite3_close_v2(@Nullable long ptrToDb);
+
+ public static int sqlite3_close_v2(@Nullable sqlite3 db){
+ return db==null ? 0 : sqlite3_close_v2(db.clearNativePointer());
+ }
+
+ public static native byte[] sqlite3_column_blob(
+ @NotNull sqlite3_stmt stmt, int ndx
+ );
+
+ static native int sqlite3_column_bytes(@NotNull long ptrToStmt, int ndx);
+
+ public static int sqlite3_column_bytes(@NotNull sqlite3_stmt stmt, int ndx){
+ return sqlite3_column_bytes(stmt.getNativePointer(), ndx);
+ }
+
+ static native int sqlite3_column_bytes16(@NotNull long ptrToStmt, int ndx);
+
+ public static int sqlite3_column_bytes16(@NotNull sqlite3_stmt stmt, int ndx){
+ return sqlite3_column_bytes16(stmt.getNativePointer(), ndx);
+ }
+
+ static native int sqlite3_column_count(@NotNull long ptrToStmt);
+
+ public static int sqlite3_column_count(@NotNull sqlite3_stmt stmt){
+ return sqlite3_column_count(stmt.getNativePointer());
+ }
+
+ static native String sqlite3_column_decltype(@NotNull long ptrToStmt, int ndx);
+
+ public static String sqlite3_column_decltype(@NotNull sqlite3_stmt stmt, int ndx){
+ return sqlite3_column_decltype(stmt.getNativePointer(), ndx);
+ }
+
+ public static native double sqlite3_column_double(
+ @NotNull sqlite3_stmt stmt, int ndx
+ );
+
+ public static native int sqlite3_column_int(
+ @NotNull sqlite3_stmt stmt, int ndx
+ );
+
+ public static native long sqlite3_column_int64(
+ @NotNull sqlite3_stmt stmt, int ndx
+ );
+
+ static native String sqlite3_column_name(@NotNull long ptrToStmt, int ndx);
+
+ public static String sqlite3_column_name(@NotNull sqlite3_stmt stmt, int ndx){
+ return sqlite3_column_name(stmt.getNativePointer(), ndx);
+ }
+
+ static native String sqlite3_column_database_name(@NotNull long ptrToStmt, int ndx);
+
+ public static String sqlite3_column_database_name(@NotNull sqlite3_stmt stmt, int ndx){
+ return sqlite3_column_database_name(stmt.getNativePointer(), ndx);
+ }
+
+ static native String sqlite3_column_origin_name(@NotNull long ptrToStmt, int ndx);
+
+ public static String sqlite3_column_origin_name(@NotNull sqlite3_stmt stmt, int ndx){
+ return sqlite3_column_origin_name(stmt.getNativePointer(), ndx);
+ }
+
+ static native String sqlite3_column_table_name(@NotNull long ptrToStmt, int ndx);
+
+ public static String sqlite3_column_table_name(@NotNull sqlite3_stmt stmt, int ndx){
+ return sqlite3_column_table_name(stmt.getNativePointer(), ndx);
+ }
+
+ /**
+ Functions identially to the C API, and this note is just to
+ stress that the returned bytes are encoded as UTF-8. It returns
+ null if the underlying C-level sqlite3_column_text() returns NULL
+ or on allocation error.
+
+ @see #sqlite3_column_text16(sqlite3_stmt,int)
+ */
+ public static native byte[] sqlite3_column_text(
+ @NotNull sqlite3_stmt stmt, int ndx
+ );
+
+ public static native String sqlite3_column_text16(
+ @NotNull sqlite3_stmt stmt, int ndx
+ );
+
+ // The real utility of this function is questionable.
+ // /**
+ // Returns a Java value representation based on the value of
+ // sqlite_value_type(). For integer types it returns either Integer
+ // or Long, depending on whether the value will fit in an
+ // Integer. For floating-point values it always returns type Double.
+
+ // If the column was bound using sqlite3_result_java_object() then
+ // that value, as an Object, is returned.
+ // */
+ // public static Object sqlite3_column_to_java(@NotNull sqlite3_stmt stmt,
+ // int ndx){
+ // sqlite3_value v = sqlite3_column_value(stmt, ndx);
+ // Object rv = null;
+ // if(null == v) return v;
+ // v = sqlite3_value_dup(v)/*need a protected value*/;
+ // if(null == v) return v /* OOM error in C */;
+ // if(112/* 'p' */ == sqlite3_value_subtype(v)){
+ // rv = sqlite3_value_java_object(v);
+ // }else{
+ // switch(sqlite3_value_type(v)){
+ // case SQLITE_INTEGER: {
+ // final long i = sqlite3_value_int64(v);
+ // rv = (i<=0x7fffffff && i>=-0x7fffffff-1)
+ // ? new Integer((int)i) : new Long(i);
+ // break;
+ // }
+ // case SQLITE_FLOAT: rv = new Double(sqlite3_value_double(v)); break;
+ // case SQLITE_BLOB: rv = sqlite3_value_blob(v); break;
+ // case SQLITE_TEXT: rv = sqlite3_value_text16(v); break;
+ // default: break;
+ // }
+ // }
+ // sqlite3_value_free(v);
+ // return rv;
+ // }
+
+ static native int sqlite3_column_type(@NotNull long ptrToStmt, int ndx);
+
+ public static int sqlite3_column_type(@NotNull sqlite3_stmt stmt, int ndx){
+ return sqlite3_column_type(stmt.getNativePointer(), ndx);
+ }
+
+ public static native sqlite3_value sqlite3_column_value(
+ @NotNull sqlite3_stmt stmt, int ndx
+ );
+
+ static native int sqlite3_collation_needed(
+ @NotNull long ptrToDb, @Nullable CollationNeededCallback callback
+ );
+
+ /**
+ This functions like C's sqlite3_collation_needed16() because
+ Java's string type is inherently compatible with that interface.
+ */
+ public static int sqlite3_collation_needed(
+ @NotNull sqlite3 db, @Nullable CollationNeededCallback callback
+ ){
+ return sqlite3_collation_needed(db.getNativePointer(), callback);
+ }
+
+ static native CommitHookCallback sqlite3_commit_hook(
+ @NotNull long ptrToDb, @Nullable CommitHookCallback hook
+ );
+
+ public static CommitHookCallback sqlite3_commit_hook(
+ @NotNull sqlite3 db, @Nullable CommitHookCallback hook
+ ){
+ return sqlite3_commit_hook(db.getNativePointer(), hook);
+ }
+
+ public static native String sqlite3_compileoption_get(int n);
+
+ public static native boolean sqlite3_compileoption_used(String optName);
+
+ /**
+ This implementation is private because it's too easy to pass it
+ non-NUL-terminated byte arrays from client code.
+ */
+ private static native int sqlite3_complete(
+ @NotNull byte[] nulTerminatedUtf8Sql
+ );
+
+ /**
+ Unlike the C API, this returns SQLITE_MISUSE if its argument is
+ null (as opposed to invoking UB).
+ */
+ public static int sqlite3_complete(@NotNull String sql){
+ return sqlite3_complete( nulTerminateUtf8(sql) );
+ }
+
+
+ /**
+
Works like in the C API with the exception that it only supports
+ the following subset of configution flags:
+
+
SQLITE_CONFIG_SINGLETHREAD
+ SQLITE_CONFIG_MULTITHREAD
+ SQLITE_CONFIG_SERIALIZED
+
+
Others may be added in the future. It returns SQLITE_MISUSE if
+ given an argument it does not handle.
+
+
Note that sqlite3_config() is not threadsafe with regards to
+ the rest of the library. This must not be called when any other
+ library APIs are being called.
+ */
+ public static native int sqlite3_config(int op);
+
+ /**
+ If the native library was built with SQLITE_ENABLE_SQLLOG defined
+ then this acts as a proxy for C's
+ sqlite3_config(SQLITE_ENABLE_SQLLOG,...). This sets or clears the
+ logger. If installation of a logger fails, any previous logger is
+ retained.
+
+
If not built with SQLITE_ENABLE_SQLLOG defined, this returns
+ SQLITE_MISUSE.
+
+
Note that sqlite3_config() is not threadsafe with regards to
+ the rest of the library. This must not be called when any other
+ library APIs are being called.
+ */
+ public static native int sqlite3_config( @Nullable ConfigSqllogCallback logger );
+
+ /**
+ The sqlite3_config() overload for handling the SQLITE_CONFIG_LOG
+ option.
+ */
+ public static native int sqlite3_config( @Nullable ConfigLogCallback logger );
+
+ /**
+ Unlike the C API, this returns null if its argument is
+ null (as opposed to invoking UB).
+ */
+ public static native sqlite3 sqlite3_context_db_handle(
+ @NotNull sqlite3_context cx
+ );
+
+ public static native int sqlite3_create_collation(
+ @NotNull sqlite3 db, @NotNull String name, int eTextRep,
+ @NotNull CollationCallback col
+ );
+
+ /**
+ The Java counterpart to the C-native sqlite3_create_function(),
+ sqlite3_create_function_v2(), and
+ sqlite3_create_window_function(). Which one it behaves like
+ depends on which methods the final argument implements. See
+ SQLFunction's subclasses (ScalarFunction, AggregateFunction,
+ and WindowFunction) for details.
+
+ Unlike the C API, this returns SQLITE_MISUSE null if its db or
+ functionName arguments are null (as opposed to invoking UB).
+ */
+ public static native int sqlite3_create_function(
+ @NotNull sqlite3 db, @NotNull String functionName,
+ int nArg, int eTextRep, @NotNull SQLFunction func
+ );
+
+ static native int sqlite3_data_count(@NotNull long ptrToStmt);
+
+ public static int sqlite3_data_count(@NotNull sqlite3_stmt stmt){
+ return sqlite3_data_count(stmt.getNativePointer());
+ }
+
+ /**
+ Overload for sqlite3_db_config() calls which take (int,int*)
+ variadic arguments. Returns SQLITE_MISUSE if op is not one of the
+ SQLITE_DBCONFIG_... options which uses this call form.
+
+
Unlike the C API, this returns SQLITE_MISUSE if its db argument
+ are null (as opposed to invoking UB).
+ */
+ public static native int sqlite3_db_config(
+ @NotNull sqlite3 db, int op, int onOff, @Nullable OutputPointer.Int32 out
+ );
+
+ /**
+ Overload for sqlite3_db_config() calls which take a (const char*)
+ variadic argument. As of SQLite3 v3.43 the only such option is
+ SQLITE_DBCONFIG_MAINDBNAME. Returns SQLITE_MISUSE if op is not
+ SQLITE_DBCONFIG_MAINDBNAME, but that set of options may be
+ extended in future versions.
+ */
+ public static native int sqlite3_db_config(
+ @NotNull sqlite3 db, int op, @NotNull String val
+ );
+
+ private static native String sqlite3_db_name(@NotNull long ptrToDb, int ndx);
+
+ public static String sqlite3_db_name(@NotNull sqlite3 db, int ndx){
+ return null==db ? null : sqlite3_db_name(db.getNativePointer(), ndx);
+ }
+
+
+ public static native String sqlite3_db_filename(
+ @NotNull sqlite3 db, @NotNull String dbName
+ );
+
+ public static native sqlite3 sqlite3_db_handle(@NotNull sqlite3_stmt stmt);
+
+ public static native int sqlite3_db_readonly(@NotNull sqlite3 db, String dbName);
+
+ public static native int sqlite3_db_release_memory(sqlite3 db);
+
+ public static native int sqlite3_db_status(
+ @NotNull sqlite3 db, int op, @NotNull OutputPointer.Int32 pCurrent,
+ @NotNull OutputPointer.Int32 pHighwater, boolean reset
+ );
+
+ public static native int sqlite3_errcode(@NotNull sqlite3 db);
+
+ public static native String sqlite3_errmsg(@NotNull sqlite3 db);
+
+ static native int sqlite3_error_offset(@NotNull long ptrToDb);
+
+ /**
+ Note that the returned byte offset values assume UTF-8-encoded
+ inputs, so won't always match character offsets in Java Strings.
+ */
+ public static int sqlite3_error_offset(@NotNull sqlite3 db){
+ return sqlite3_error_offset(db.getNativePointer());
+ }
+
+ public static native String sqlite3_errstr(int resultCode);
+
+ public static native String sqlite3_expanded_sql(@NotNull sqlite3_stmt stmt);
+
+ static native int sqlite3_extended_errcode(@NotNull long ptrToDb);
+
+ public static int sqlite3_extended_errcode(@NotNull sqlite3 db){
+ return sqlite3_extended_errcode(db.getNativePointer());
+ }
+
+ public static native boolean sqlite3_extended_result_codes(
+ @NotNull sqlite3 db, boolean onoff
+ );
+
+ static native boolean sqlite3_get_autocommit(@NotNull long ptrToDb);
+
+ public static boolean sqlite3_get_autocommit(@NotNull sqlite3 db){
+ return sqlite3_get_autocommit(db.getNativePointer());
+ }
+
+ public static native Object sqlite3_get_auxdata(
+ @NotNull sqlite3_context cx, int n
+ );
+
+ static native int sqlite3_finalize(long ptrToStmt);
+
+ public static int sqlite3_finalize(@NotNull sqlite3_stmt stmt){
+ return null==stmt ? 0 : sqlite3_finalize(stmt.clearNativePointer());
+ }
+
+ public static native int sqlite3_initialize();
+
+ public static native void sqlite3_interrupt(@NotNull sqlite3 db);
+
+ public static native boolean sqlite3_is_interrupted(@NotNull sqlite3 db);
+
+ public static native boolean sqlite3_keyword_check(@NotNull String word);
+
+ public static native int sqlite3_keyword_count();
+
+ public static native String sqlite3_keyword_name(int index);
+
+
+ public static native long sqlite3_last_insert_rowid(@NotNull sqlite3 db);
+
+ public static native String sqlite3_libversion();
+
+ public static native int sqlite3_libversion_number();
+
+ public static native int sqlite3_limit(@NotNull sqlite3 db, int id, int newVal);
+
+ /**
+ Only available if built with SQLITE_ENABLE_NORMALIZE. If not, it always
+ returns null.
+ */
+ public static native String sqlite3_normalized_sql(@NotNull sqlite3_stmt stmt);
+
+ /**
+ Works like its C counterpart and makes the native pointer of the
+ underling (sqlite3*) object available via
+ ppDb.getNativePointer(). That pointer is necessary for looking up
+ the JNI-side native, but clients need not pay it any
+ heed. Passing the object to sqlite3_close() or sqlite3_close_v2()
+ will clear that pointer mapping.
+
+
Recall that even if opening fails, the output pointer might be
+ non-null. Any error message about the failure will be in that
+ object and it is up to the caller to sqlite3_close() that
+ db handle.
+ */
+ public static native int sqlite3_open(
+ @Nullable String filename, @NotNull OutputPointer.sqlite3 ppDb
+ );
+
+ /**
+ Convenience overload which returns its db handle directly. The returned
+ object might not have been successfully opened: use sqlite3_errcode() to
+ check whether it is in an error state.
+
+
Ownership of the returned value is passed to the caller, who must eventually
+ pass it to sqlite3_close() or sqlite3_close_v2().
+ */
+ public static sqlite3 sqlite3_open(@Nullable String filename){
+ final OutputPointer.sqlite3 out = new OutputPointer.sqlite3();
+ sqlite3_open(filename, out);
+ return out.take();
+ };
+
+ public static native int sqlite3_open_v2(
+ @Nullable String filename, @NotNull OutputPointer.sqlite3 ppDb,
+ int flags, @Nullable String zVfs
+ );
+
+ /**
+ Has the same semantics as the sqlite3-returning sqlite3_open()
+ but uses sqlite3_open_v2() instead of sqlite3_open().
+ */
+ public static sqlite3 sqlite3_open_v2(@Nullable String filename, int flags,
+ @Nullable String zVfs){
+ final OutputPointer.sqlite3 out = new OutputPointer.sqlite3();
+ sqlite3_open_v2(filename, out, flags, zVfs);
+ return out.take();
+ };
+
+ /**
+ The sqlite3_prepare() family of functions require slightly
+ different signatures than their native counterparts, but (A) they
+ retain functionally equivalent semantics and (B) overloading
+ allows us to install several convenience forms.
+
+
All of them which take their SQL in the form of a byte[] require
+ that it be in UTF-8 encoding unless explicitly noted otherwise.
+
+
The forms which take a "tail" output pointer return (via that
+ output object) the index into their SQL byte array at which the
+ end of the first SQL statement processed by the call was
+ found. That's fundamentally how the C APIs work but making use of
+ that value requires more copying of the input SQL into
+ consecutively smaller arrays in order to consume all of
+ it. (There is an example of doing that in this project's Tester1
+ class.) For that vast majority of uses, that capability is not
+ necessary, however, and overloads are provided which gloss over
+ that.
+
+
Results are undefined if maxBytes>sqlUtf8.length.
+
+
This routine is private because its maxBytes value is not
+ strictly necessary in the Java interface, as sqlUtf8.length tells
+ us the information we need. Making this public would give clients
+ more ways to shoot themselves in the foot without providing any
+ real utility.
+ */
+ private static native int sqlite3_prepare(
+ @NotNull long ptrToDb, @NotNull byte[] sqlUtf8, int maxBytes,
+ @NotNull OutputPointer.sqlite3_stmt outStmt,
+ @Nullable OutputPointer.Int32 pTailOffset
+ );
+
+ /**
+ Works like the canonical sqlite3_prepare() but its "tail" output
+ argument is returned as the index offset into the given
+ UTF-8-encoded byte array at which SQL parsing stopped. The
+ semantics are otherwise identical to the C API counterpart.
+
+
Several overloads provided simplified call signatures.
+ */
+ public static int sqlite3_prepare(
+ @NotNull sqlite3 db, @NotNull byte[] sqlUtf8,
+ @NotNull OutputPointer.sqlite3_stmt outStmt,
+ @Nullable OutputPointer.Int32 pTailOffset
+ ){
+ return sqlite3_prepare(db.getNativePointer(), sqlUtf8, sqlUtf8.length,
+ outStmt, pTailOffset);
+ }
+
+ public static int sqlite3_prepare(
+ @NotNull sqlite3 db, @NotNull byte[] sqlUtf8,
+ @NotNull OutputPointer.sqlite3_stmt outStmt
+ ){
+ return sqlite3_prepare(db.getNativePointer(), sqlUtf8, sqlUtf8.length,
+ outStmt, null);
+ }
+
+ public static int sqlite3_prepare(
+ @NotNull sqlite3 db, @NotNull String sql,
+ @NotNull OutputPointer.sqlite3_stmt outStmt
+ ){
+ final byte[] utf8 = sql.getBytes(StandardCharsets.UTF_8);
+ return sqlite3_prepare(db.getNativePointer(), utf8, utf8.length,
+ outStmt, null);
+ }
+
+ /**
+ Convenience overload which returns its statement handle directly,
+ or null on error or when reading only whitespace or
+ comments. sqlite3_errcode() can be used to determine whether
+ there was an error or the input was empty. Ownership of the
+ returned object is passed to the caller, who must eventually pass
+ it to sqlite3_finalize().
+ */
+ public static sqlite3_stmt sqlite3_prepare(
+ @NotNull sqlite3 db, @NotNull String sql
+ ){
+ final OutputPointer.sqlite3_stmt out = new OutputPointer.sqlite3_stmt();
+ sqlite3_prepare(db, sql, out);
+ return out.take();
+ }
+ /**
+ @see #sqlite3_prepare
+ */
+ private static native int sqlite3_prepare_v2(
+ @NotNull long ptrToDb, @NotNull byte[] sqlUtf8, int maxBytes,
+ @NotNull OutputPointer.sqlite3_stmt outStmt,
+ @Nullable OutputPointer.Int32 pTailOffset
+ );
+
+ /**
+ Works like the canonical sqlite3_prepare_v2() but its "tail"
+ output paramter is returned as the index offset into the given
+ byte array at which SQL parsing stopped.
+ */
+ public static int sqlite3_prepare_v2(
+ @NotNull sqlite3 db, @NotNull byte[] sqlUtf8,
+ @NotNull OutputPointer.sqlite3_stmt outStmt,
+ @Nullable OutputPointer.Int32 pTailOffset
+ ){
+ return sqlite3_prepare_v2(db.getNativePointer(), sqlUtf8, sqlUtf8.length,
+ outStmt, pTailOffset);
+ }
+
+ public static int sqlite3_prepare_v2(
+ @NotNull sqlite3 db, @NotNull byte[] sqlUtf8,
+ @NotNull OutputPointer.sqlite3_stmt outStmt
+ ){
+ return sqlite3_prepare_v2(db.getNativePointer(), sqlUtf8, sqlUtf8.length,
+ outStmt, null);
+ }
+
+ public static int sqlite3_prepare_v2(
+ @NotNull sqlite3 db, @NotNull String sql,
+ @NotNull OutputPointer.sqlite3_stmt outStmt
+ ){
+ final byte[] utf8 = sql.getBytes(StandardCharsets.UTF_8);
+ return sqlite3_prepare_v2(db.getNativePointer(), utf8, utf8.length,
+ outStmt, null);
+ }
+
+ /**
+ Works identically to the sqlite3_stmt-returning sqlite3_prepare()
+ but uses sqlite3_prepare_v2().
+ */
+ public static sqlite3_stmt sqlite3_prepare_v2(
+ @NotNull sqlite3 db, @NotNull String sql
+ ){
+ final OutputPointer.sqlite3_stmt out = new OutputPointer.sqlite3_stmt();
+ sqlite3_prepare_v2(db, sql, out);
+ return out.take();
+ }
+
+ /**
+ @see #sqlite3_prepare
+ */
+ private static native int sqlite3_prepare_v3(
+ @NotNull long ptrToDb, @NotNull byte[] sqlUtf8, int maxBytes,
+ int prepFlags, @NotNull OutputPointer.sqlite3_stmt outStmt,
+ @Nullable OutputPointer.Int32 pTailOffset
+ );
+
+ /**
+ Works like the canonical sqlite3_prepare_v2() but its "tail"
+ output paramter is returned as the index offset into the given
+ byte array at which SQL parsing stopped.
+ */
+ public static int sqlite3_prepare_v3(
+ @NotNull sqlite3 db, @NotNull byte[] sqlUtf8, int prepFlags,
+ @NotNull OutputPointer.sqlite3_stmt outStmt,
+ @Nullable OutputPointer.Int32 pTailOffset
+ ){
+ return sqlite3_prepare_v3(db.getNativePointer(), sqlUtf8, sqlUtf8.length,
+ prepFlags, outStmt, pTailOffset);
+ }
+
+ /**
+ Convenience overload which elides the seldom-used pTailOffset
+ parameter.
+ */
+ public static int sqlite3_prepare_v3(
+ @NotNull sqlite3 db, @NotNull byte[] sqlUtf8, int prepFlags,
+ @NotNull OutputPointer.sqlite3_stmt outStmt
+ ){
+ return sqlite3_prepare_v3(db.getNativePointer(), sqlUtf8, sqlUtf8.length,
+ prepFlags, outStmt, null);
+ }
+
+ /**
+ Convenience overload which elides the seldom-used pTailOffset
+ parameter and converts the given string to UTF-8 before passing
+ it on.
+ */
+ public static int sqlite3_prepare_v3(
+ @NotNull sqlite3 db, @NotNull String sql, int prepFlags,
+ @NotNull OutputPointer.sqlite3_stmt outStmt
+ ){
+ final byte[] utf8 = sql.getBytes(StandardCharsets.UTF_8);
+ return sqlite3_prepare_v3(db.getNativePointer(), utf8, utf8.length,
+ prepFlags, outStmt, null);
+ }
+
+ /**
+ Works identically to the sqlite3_stmt-returning sqlite3_prepare()
+ but uses sqlite3_prepare_v3().
+ */
+ public static sqlite3_stmt sqlite3_prepare_v3(
+ @NotNull sqlite3 db, @NotNull String sql, int prepFlags
+ ){
+ final OutputPointer.sqlite3_stmt out = new OutputPointer.sqlite3_stmt();
+ sqlite3_prepare_v3(db, sql, prepFlags, out);
+ return out.take();
+ }
+
+ /**
+ A convenience wrapper around sqlite3_prepare_v3() which accepts
+ an arbitrary amount of input provided as a UTF-8-encoded byte
+ array. It loops over the input bytes looking for
+ statements. Each one it finds is passed to p.call(), passing
+ ownership of it to that function. If p.call() returns 0, looping
+ continues, else the loop stops.
+
+
If p.call() throws, the exception is propagated.
+
+
How each statement is handled, including whether it is finalized
+ or not, is up to the callback object. e.g. the callback might
+ collect them for later use. If it does not collect them then it
+ must finalize them. See PrepareMultiCallback.Finalize for a
+ simple proxy which does that.
+ */
+ public static int sqlite3_prepare_multi(
+ @NotNull sqlite3 db, @NotNull byte[] sqlUtf8,
+ int preFlags,
+ @NotNull PrepareMultiCallback p){
+ final OutputPointer.Int32 oTail = new OutputPointer.Int32();
+ int pos = 0, n = 1;
+ byte[] sqlChunk = sqlUtf8;
+ int rc = 0;
+ final OutputPointer.sqlite3_stmt outStmt = new OutputPointer.sqlite3_stmt();
+ while(0==rc && pos 0){
+ sqlChunk = Arrays.copyOfRange(sqlChunk, pos,
+ sqlChunk.length);
+ }
+ if( 0==sqlChunk.length ) break;
+ rc = sqlite3_prepare_v3(db, sqlChunk, preFlags, outStmt, oTail);
+ if( 0!=rc ) break;
+ pos = oTail.value;
+ stmt = outStmt.take();
+ if( null == stmt ){
+ // empty statement was parsed.
+ continue;
+ }
+ rc = p.call(stmt);
+ }
+ return rc;
+ }
+
+ /**
+ Convenience overload which accepts its SQL as a String and uses
+ no statement-preparation flags.
+ */
+ public static int sqlite3_prepare_multi(
+ @NotNull sqlite3 db, @NotNull byte[] sqlUtf8,
+ @NotNull PrepareMultiCallback p){
+ return sqlite3_prepare_multi(db, sqlUtf8, 0, p);
+ }
+
+ /**
+ Convenience overload which accepts its SQL as a String.
+ */
+ public static int sqlite3_prepare_multi(
+ @NotNull sqlite3 db, @NotNull String sql, int prepFlags,
+ @NotNull PrepareMultiCallback p){
+ return sqlite3_prepare_multi(
+ db, sql.getBytes(StandardCharsets.UTF_8), prepFlags, p
+ );
+ }
+
+ /**
+ Convenience overload which accepts its SQL as a String and uses
+ no statement-preparation flags.
+ */
+ public static int sqlite3_prepare_multi(
+ @NotNull sqlite3 db, @NotNull String sql,
+ @NotNull PrepareMultiCallback p){
+ return sqlite3_prepare_multi(db, sql, 0, p);
+ }
+
+ /**
+ Convenience overload which accepts its SQL as a String
+ array. They will be concatenated together as-is, with no
+ separator, and passed on to one of the other overloads.
+ */
+ public static int sqlite3_prepare_multi(
+ @NotNull sqlite3 db, @NotNull String[] sql, int prepFlags,
+ @NotNull PrepareMultiCallback p){
+ return sqlite3_prepare_multi(db, String.join("",sql), prepFlags, p);
+ }
+
+ /**
+ Convenience overload which uses no statement-preparation flags.
+ */
+ public static int sqlite3_prepare_multi(
+ @NotNull sqlite3 db, @NotNull String[] sql,
+ @NotNull PrepareMultiCallback p){
+ return sqlite3_prepare_multi(db, sql, 0, p);
+ }
+
+ static native int sqlite3_preupdate_blobwrite(@NotNull long ptrToDb);
+
+ /**
+ If the C API was built with SQLITE_ENABLE_PREUPDATE_HOOK defined, this
+ acts as a proxy for C's sqlite3_preupdate_blobwrite(), else it returns
+ SQLITE_MISUSE with no side effects.
+ */
+ public static int sqlite3_preupdate_blobwrite(@NotNull sqlite3 db){
+ return sqlite3_preupdate_blobwrite(db.getNativePointer());
+ }
+
+ static native int sqlite3_preupdate_count(@NotNull long ptrToDb);
+
+ /**
+ If the C API was built with SQLITE_ENABLE_PREUPDATE_HOOK defined, this
+ acts as a proxy for C's sqlite3_preupdate_count(), else it returns
+ SQLITE_MISUSE with no side effects.
+ */
+ public static int sqlite3_preupdate_count(@NotNull sqlite3 db){
+ return sqlite3_preupdate_count(db.getNativePointer());
+ }
+
+ static native int sqlite3_preupdate_depth(@NotNull long ptrToDb);
+
+ /**
+ If the C API was built with SQLITE_ENABLE_PREUPDATE_HOOK defined, this
+ acts as a proxy for C's sqlite3_preupdate_depth(), else it returns
+ SQLITE_MISUSE with no side effects.
+ */
+ public static int sqlite3_preupdate_depth(@NotNull sqlite3 db){
+ return sqlite3_preupdate_depth(db.getNativePointer());
+ }
+
+ static native PreupdateHookCallback sqlite3_preupdate_hook(
+ @NotNull long ptrToDb, @Nullable PreupdateHookCallback hook
+ );
+
+ /**
+ If the C API was built with SQLITE_ENABLE_PREUPDATE_HOOK defined, this
+ acts as a proxy for C's sqlite3_preupdate_hook(), else it returns null
+ with no side effects.
+ */
+ public static PreupdateHookCallback sqlite3_preupdate_hook(
+ @NotNull sqlite3 db, @Nullable PreupdateHookCallback hook
+ ){
+ return sqlite3_preupdate_hook(db.getNativePointer(), hook);
+ }
+
+ static native int sqlite3_preupdate_new(@NotNull long ptrToDb, int col,
+ @NotNull OutputPointer.sqlite3_value out);
+
+ /**
+ If the C API was built with SQLITE_ENABLE_PREUPDATE_HOOK defined,
+ this acts as a proxy for C's sqlite3_preupdate_new(), else it
+ returns SQLITE_MISUSE with no side effects.
+ */
+ public static int sqlite3_preupdate_new(@NotNull sqlite3 db, int col,
+ @NotNull OutputPointer.sqlite3_value out){
+ return sqlite3_preupdate_new(db.getNativePointer(), col, out);
+ }
+
+ /**
+ Convenience wrapper for the 3-arg sqlite3_preupdate_new() which returns
+ null on error.
+ */
+ public static sqlite3_value sqlite3_preupdate_new(@NotNull sqlite3 db, int col){
+ final OutputPointer.sqlite3_value out = new OutputPointer.sqlite3_value();
+ sqlite3_preupdate_new(db.getNativePointer(), col, out);
+ return out.take();
+ }
+
+ static native int sqlite3_preupdate_old(@NotNull long ptrToDb, int col,
+ @NotNull OutputPointer.sqlite3_value out);
+
+ /**
+ If the C API was built with SQLITE_ENABLE_PREUPDATE_HOOK defined,
+ this acts as a proxy for C's sqlite3_preupdate_old(), else it
+ returns SQLITE_MISUSE with no side effects.
+ */
+ public static int sqlite3_preupdate_old(@NotNull sqlite3 db, int col,
+ @NotNull OutputPointer.sqlite3_value out){
+ return sqlite3_preupdate_old(db.getNativePointer(), col, out);
+ }
+
+ /**
+ Convenience wrapper for the 3-arg sqlite3_preupdate_old() which returns
+ null on error.
+ */
+ public static sqlite3_value sqlite3_preupdate_old(@NotNull sqlite3 db, int col){
+ final OutputPointer.sqlite3_value out = new OutputPointer.sqlite3_value();
+ sqlite3_preupdate_old(db.getNativePointer(), col, out);
+ return out.take();
+ }
+
+ public static native void sqlite3_progress_handler(
+ @NotNull sqlite3 db, int n, @Nullable ProgressHandlerCallback h
+ );
+
+ public static native void sqlite3_randomness(byte[] target);
+
+ public static native int sqlite3_release_memory(int n);
+
+ public static native int sqlite3_reset(@NotNull sqlite3_stmt stmt);
+
+ /**
+ Works like the C API except that it has no side effects if auto
+ extensions are currently running. (The JNI-level list of
+ extensions cannot be manipulated while it is being traversed.)
+ */
+ public static native void sqlite3_reset_auto_extension();
+
+ public static native void sqlite3_result_double(
+ @NotNull sqlite3_context cx, double v
+ );
+
+ /**
+ The main sqlite3_result_error() impl of which all others are
+ proxies. eTextRep must be one of SQLITE_UTF8 or SQLITE_UTF16 and
+ msg must be encoded correspondingly. Any other eTextRep value
+ results in the C-level sqlite3_result_error() being called with a
+ complaint about the invalid argument.
+ */
+ static native void sqlite3_result_error(
+ @NotNull sqlite3_context cx, @NotNull byte[] msg, int eTextRep
+ );
+
+ public static void sqlite3_result_error(
+ @NotNull sqlite3_context cx, @NotNull byte[] utf8
+ ){
+ sqlite3_result_error(cx, utf8, SQLITE_UTF8);
+ }
+
+ public static void sqlite3_result_error(
+ @NotNull sqlite3_context cx, @NotNull String msg
+ ){
+ final byte[] utf8 = msg.getBytes(StandardCharsets.UTF_8);
+ sqlite3_result_error(cx, utf8, SQLITE_UTF8);
+ }
+
+ public static void sqlite3_result_error16(
+ @NotNull sqlite3_context cx, @NotNull byte[] utf16
+ ){
+ sqlite3_result_error(cx, utf16, SQLITE_UTF16);
+ }
+
+ public static void sqlite3_result_error16(
+ @NotNull sqlite3_context cx, @NotNull String msg
+ ){
+ final byte[] utf16 = msg.getBytes(StandardCharsets.UTF_16);
+ sqlite3_result_error(cx, utf16, SQLITE_UTF16);
+ }
+
+ /**
+ Equivalent to passing e.toString() to {@link
+ #sqlite3_result_error(sqlite3_context,String)}. Note that
+ toString() is used instead of getMessage() because the former
+ prepends the exception type name to the message.
+ */
+ public static void sqlite3_result_error(
+ @NotNull sqlite3_context cx, @NotNull Exception e
+ ){
+ sqlite3_result_error(cx, e.toString());
+ }
+
+ public static native void sqlite3_result_error_toobig(
+ @NotNull sqlite3_context cx
+ );
+
+ public static native void sqlite3_result_error_nomem(
+ @NotNull sqlite3_context cx
+ );
+
+ public static native void sqlite3_result_error_code(
+ @NotNull sqlite3_context cx, int c
+ );
+
+ public static native void sqlite3_result_null(
+ @NotNull sqlite3_context cx
+ );
+
+ public static native void sqlite3_result_int(
+ @NotNull sqlite3_context cx, int v
+ );
+
+ public static native void sqlite3_result_int64(
+ @NotNull sqlite3_context cx, long v
+ );
+
+ /**
+ Binds the SQL result to the given object, or {@link
+ #sqlite3_result_null} if {@code o} is null. Use {@link
+ #sqlite3_value_java_object} to fetch it.
+
+ This is implemented in terms of C's sqlite3_result_pointer(),
+ but that function is not exposed to JNI because (A)
+ cross-language semantic mismatch and (B) Java doesn't need that
+ argument for its intended purpose (type safety).
+
+
Note that there is no sqlite3_column_java_object(), as the
+ C-level API has no sqlite3_column_pointer() to proxy.
+
+ @see #sqlite3_value_java_object
+ @see #sqlite3_bind_java_object
+ */
+ public static native void sqlite3_result_java_object(
+ @NotNull sqlite3_context cx, @NotNull Object o
+ );
+
+ public static void sqlite3_result_set(
+ @NotNull sqlite3_context cx, @NotNull Boolean v
+ ){
+ sqlite3_result_int(cx, v ? 1 : 0);
+ }
+
+ public static void sqlite3_result_set(
+ @NotNull sqlite3_context cx, boolean v
+ ){
+ sqlite3_result_int(cx, v ? 1 : 0);
+ }
+
+ public static void sqlite3_result_set(
+ @NotNull sqlite3_context cx, @NotNull Double v
+ ){
+ sqlite3_result_double(cx, v);
+ }
+
+ public static void sqlite3_result_set(
+ @NotNull sqlite3_context cx, double v
+ ){
+ sqlite3_result_double(cx, v);
+ }
+
+ public static void sqlite3_result_set(
+ @NotNull sqlite3_context cx, @NotNull Integer v
+ ){
+ sqlite3_result_int(cx, v);
+ }
+
+ public static void sqlite3_result_set(@NotNull sqlite3_context cx, int v){
+ sqlite3_result_int(cx, v);
+ }
+
+ public static void sqlite3_result_set(
+ @NotNull sqlite3_context cx, @NotNull Long v
+ ){
+ sqlite3_result_int64(cx, v);
+ }
+
+ public static void sqlite3_result_set(
+ @NotNull sqlite3_context cx, long v
+ ){
+ sqlite3_result_int64(cx, v);
+ }
+
+ public static void sqlite3_result_set(
+ @NotNull sqlite3_context cx, @Nullable String v
+ ){
+ if( null==v ) sqlite3_result_null(cx);
+ else sqlite3_result_text(cx, v);
+ }
+
+ public static void sqlite3_result_set(
+ @NotNull sqlite3_context cx, @Nullable byte[] blob
+ ){
+ if( null==blob ) sqlite3_result_null(cx);
+ else sqlite3_result_blob(cx, blob, blob.length);
+ }
+
+ public static native void sqlite3_result_value(
+ @NotNull sqlite3_context cx, @NotNull sqlite3_value v
+ );
+
+ public static native void sqlite3_result_zeroblob(
+ @NotNull sqlite3_context cx, int n
+ );
+
+ public static native int sqlite3_result_zeroblob64(
+ @NotNull sqlite3_context cx, long n
+ );
+
+ /**
+ This overload is private because its final parameter is arguably
+ unnecessary in Java.
+ */
+ private static native void sqlite3_result_blob(
+ @NotNull sqlite3_context cx, @Nullable byte[] blob, int maxLen
+ );
+
+ public static void sqlite3_result_blob(
+ @NotNull sqlite3_context cx, @Nullable byte[] blob
+ ){
+ sqlite3_result_blob(cx, blob, (int)(null==blob ? 0 : blob.length));
+ }
+
+ /**
+ Binds the given text using C's sqlite3_result_blob64() unless:
+
+
+
+ - @param blob is null: translates to sqlite3_result_null()
+
+ - @param blob is too large: translates to
+ sqlite3_result_error_toobig()
+
+
+
+ If @param maxLen is larger than blob.length, it is truncated
+ to that value. If it is negative, results are undefined.
+
+ This overload is private because its final parameter is
+ arguably unnecessary in Java.
+ */
+ private static native void sqlite3_result_blob64(
+ @NotNull sqlite3_context cx, @Nullable byte[] blob, long maxLen
+ );
+
+ public static void sqlite3_result_blob64(
+ @NotNull sqlite3_context cx, @Nullable byte[] blob
+ ){
+ sqlite3_result_blob64(cx, blob, (long)(null==blob ? 0 : blob.length));
+ }
+
+ /**
+ This overload is private because its final parameter is
+ arguably unnecessary in Java.
+ */
+ private static native void sqlite3_result_text(
+ @NotNull sqlite3_context cx, @Nullable byte[] utf8, int maxLen
+ );
+
+ public static void sqlite3_result_text(
+ @NotNull sqlite3_context cx, @Nullable byte[] utf8
+ ){
+ sqlite3_result_text(cx, utf8, null==utf8 ? 0 : utf8.length);
+ }
+
+ public static void sqlite3_result_text(
+ @NotNull sqlite3_context cx, @Nullable String text
+ ){
+ if(null == text) sqlite3_result_null(cx);
+ else{
+ final byte[] utf8 = text.getBytes(StandardCharsets.UTF_8);
+ sqlite3_result_text(cx, utf8, utf8.length);
+ }
+ }
+
+ /**
+ Binds the given text using C's sqlite3_result_text64() unless:
+
+
+
+ - text is null: translates to a call to sqlite3_result_null()
+
+ - text is too large: translates to a call to
+ {@link #sqlite3_result_error_toobig}
+
+ - The @param encoding argument has an invalid value: translates to
+ {@link sqlite3_result_error_code} with code SQLITE_FORMAT.
+
+
+
+ If maxLength (in bytes, not characters) is larger than
+ text.length, it is silently truncated to text.length. If it is
+ negative, results are undefined. If text is null, the subsequent
+ arguments are ignored.
+
+ This overload is private because its maxLength parameter is
+ arguably unnecessary in Java.
+ */
+ private static native void sqlite3_result_text64(
+ @NotNull sqlite3_context cx, @Nullable byte[] text,
+ long maxLength, int encoding
+ );
+
+ /**
+ Sets the current UDF result to the given bytes, which are assumed
+ be encoded in UTF-16 using the platform's byte order.
+ */
+ public static void sqlite3_result_text16(
+ @NotNull sqlite3_context cx, @Nullable byte[] utf16
+ ){
+ if(null == utf16) sqlite3_result_null(cx);
+ else sqlite3_result_text64(cx, utf16, utf16.length, SQLITE_UTF16);
+ }
+
+ public static void sqlite3_result_text16(
+ @NotNull sqlite3_context cx, @Nullable String text
+ ){
+ if(null == text) sqlite3_result_null(cx);
+ else{
+ final byte[] b = text.getBytes(StandardCharsets.UTF_16);
+ sqlite3_result_text64(cx, b, b.length, SQLITE_UTF16);
+ }
+ }
+
+ static native RollbackHookCallback sqlite3_rollback_hook(
+ @NotNull long ptrToDb, @Nullable RollbackHookCallback hook
+ );
+
+ public static RollbackHookCallback sqlite3_rollback_hook(
+ @NotNull sqlite3 db, @Nullable RollbackHookCallback hook
+ ){
+ return sqlite3_rollback_hook(db.getNativePointer(), hook);
+ }
+
+ public static native int sqlite3_set_authorizer(
+ @NotNull sqlite3 db, @Nullable AuthorizerCallback auth
+ );
+
+ public static native void sqlite3_set_auxdata(
+ @NotNull sqlite3_context cx, int n, @Nullable Object data
+ );
+
+ public static native void sqlite3_set_last_insert_rowid(
+ @NotNull sqlite3 db, long rowid
+ );
+
+
+ /**
+ In addition to calling the C-level sqlite3_shutdown(), the JNI
+ binding also cleans up all stale per-thread state managed by the
+ library, as well as any registered auto-extensions, and frees up
+ various bits of memory. Calling this while database handles or
+ prepared statements are still active will leak resources. Trying
+ to use those objects after this routine is called invoked
+ undefined behavior.
+ */
+ public static synchronized native int sqlite3_shutdown();
+
+ public static native int sqlite3_sleep(int ms);
+
+ public static native String sqlite3_sourceid();
+
+ public static native String sqlite3_sql(@NotNull sqlite3_stmt stmt);
+
+ //! Consider removing this. We can use sqlite3_status64() instead,
+ // or use that one's impl with this one's name.
+ public static native int sqlite3_status(
+ int op, @NotNull OutputPointer.Int32 pCurrent,
+ @NotNull OutputPointer.Int32 pHighwater, boolean reset
+ );
+
+ public static native int sqlite3_status64(
+ int op, @NotNull OutputPointer.Int64 pCurrent,
+ @NotNull OutputPointer.Int64 pHighwater, boolean reset
+ );
+
+ public static native int sqlite3_step(@NotNull sqlite3_stmt stmt);
+
+ public static native boolean sqlite3_stmt_busy(@NotNull sqlite3_stmt stmt);
+
+ static native int sqlite3_stmt_explain(@NotNull long ptrToStmt, int op);
+
+ public static int sqlite3_stmt_explain(@NotNull sqlite3_stmt stmt, int op){
+ return sqlite3_stmt_explain(stmt.getNativePointer(), op);
+ }
+
+ static native int sqlite3_stmt_isexplain(@NotNull long ptrToStmt);
+
+ public static int sqlite3_stmt_isexplain(@NotNull sqlite3_stmt stmt){
+ return sqlite3_stmt_isexplain(stmt.getNativePointer());
+ }
+
+ public static native boolean sqlite3_stmt_readonly(@NotNull sqlite3_stmt stmt);
+
+ public static native int sqlite3_stmt_status(
+ @NotNull sqlite3_stmt stmt, int op, boolean reset
+ );
+
+ /**
+ Internal impl of the public sqlite3_strglob() method. Neither
+ argument may be null and both must be NUL-terminated UTF-8.
+
+ This overload is private because: (A) to keep users from
+ inadvertently passing non-NUL-terminated byte arrays (an easy
+ thing to do). (B) it is cheaper to NUL-terminate the
+ String-to-byte-array conversion in the Java implementation
+ (sqlite3_strglob(String,String)) than to do that in C, so that
+ signature is the public-facing one.
+ */
+ private static native int sqlite3_strglob(
+ @NotNull byte[] glob, @NotNull byte[] nullTerminatedUtf8
+ );
+
+ public static int sqlite3_strglob(
+ @NotNull String glob, @NotNull String txt
+ ){
+ return sqlite3_strglob(nulTerminateUtf8(glob),
+ nulTerminateUtf8(txt));
+ }
+
+ /**
+ The LIKE counterpart of the private sqlite3_strglob() method.
+ */
+ private static native int sqlite3_strlike(
+ @NotNull byte[] glob, @NotNull byte[] nullTerminatedUtf8,
+ int escChar
+ );
+
+ public static int sqlite3_strlike(
+ @NotNull String glob, @NotNull String txt, char escChar
+ ){
+ return sqlite3_strlike(nulTerminateUtf8(glob),
+ nulTerminateUtf8(txt),
+ (int)escChar);
+ }
+
+ static native int sqlite3_system_errno(@NotNull long ptrToDb);
+
+ public static int sqlite3_system_errno(@NotNull sqlite3 db){
+ return sqlite3_system_errno(db.getNativePointer());
+ }
+
+ public static native int sqlite3_table_column_metadata(
+ @NotNull sqlite3 db, @NotNull String zDbName,
+ @NotNull String zTableName, @NotNull String zColumnName,
+ @Nullable OutputPointer.String pzDataType,
+ @Nullable OutputPointer.String pzCollSeq,
+ @Nullable OutputPointer.Bool pNotNull,
+ @Nullable OutputPointer.Bool pPrimaryKey,
+ @Nullable OutputPointer.Bool pAutoinc
+ );
+
+ /**
+ Convenience overload which returns its results via a single
+ output object. If this function returns non-0 (error), the the
+ contents of the output object are not modified.
+ */
+ public static int sqlite3_table_column_metadata(
+ @NotNull sqlite3 db, @NotNull String zDbName,
+ @NotNull String zTableName, @NotNull String zColumnName,
+ @NotNull TableColumnMetadata out){
+ return sqlite3_table_column_metadata(
+ db, zDbName, zTableName, zColumnName,
+ out.pzDataType, out.pzCollSeq, out.pNotNull,
+ out.pPrimaryKey, out.pAutoinc);
+ }
+
+ /**
+ Convenience overload which returns the column metadata object on
+ success and null on error.
+ */
+ public static TableColumnMetadata sqlite3_table_column_metadata(
+ @NotNull sqlite3 db, @NotNull String zDbName,
+ @NotNull String zTableName, @NotNull String zColumnName){
+ final TableColumnMetadata out = new TableColumnMetadata();
+ return 0==sqlite3_table_column_metadata(
+ db, zDbName, zTableName, zColumnName, out
+ ) ? out : null;
+ }
+
+ public static native int sqlite3_threadsafe();
+
+ static native int sqlite3_total_changes(@NotNull long ptrToDb);
+
+ public static int sqlite3_total_changes(@NotNull sqlite3 db){
+ return sqlite3_total_changes(db.getNativePointer());
+ }
+
+ static native long sqlite3_total_changes64(@NotNull long ptrToDb);
+
+ public static long sqlite3_total_changes64(@NotNull sqlite3 db){
+ return sqlite3_total_changes64(db.getNativePointer());
+ }
+
+ /**
+ Works like C's sqlite3_trace_v2() except that the 3rd argument to that
+ function is elided here because the roles of that functions' 3rd and 4th
+ arguments are encapsulated in the final argument to this function.
+
+ Unlike the C API, which is documented as always returning 0,
+ this implementation returns non-0 if initialization of the tracer
+ mapping state fails (e.g. on OOM).
+ */
+ public static native int sqlite3_trace_v2(
+ @NotNull sqlite3 db, int traceMask, @Nullable TraceV2Callback tracer
+ );
+
+ public static native int sqlite3_txn_state(
+ @NotNull sqlite3 db, @Nullable String zSchema
+ );
+
+ static native UpdateHookCallback sqlite3_update_hook(
+ @NotNull long ptrToDb, @Nullable UpdateHookCallback hook
+ );
+
+ public static UpdateHookCallback sqlite3_update_hook(
+ @NotNull sqlite3 db, @Nullable UpdateHookCallback hook
+ ){
+ return sqlite3_update_hook(db.getNativePointer(), hook);
+ }
+
+ /*
+ Note that:
+
+ void * sqlite3_user_data(sqlite3_context*)
+
+ Is not relevant in the JNI binding, as its feature is replaced by
+ the ability to pass an object, including any relevant state, to
+ sqlite3_create_function().
+ */
+
+ static native byte[] sqlite3_value_blob(@NotNull long ptrToValue);
+
+ public static byte[] sqlite3_value_blob(@NotNull sqlite3_value v){
+ return sqlite3_value_blob(v.getNativePointer());
+ }
+
+ static native int sqlite3_value_bytes(@NotNull long ptrToValue);
+
+ public static int sqlite3_value_bytes(@NotNull sqlite3_value v){
+ return sqlite3_value_bytes(v.getNativePointer());
+ }
+
+ static native int sqlite3_value_bytes16(@NotNull long ptrToValue);
+
+ public static int sqlite3_value_bytes16(@NotNull sqlite3_value v){
+ return sqlite3_value_bytes16(v.getNativePointer());
+ }
+
+ static native double sqlite3_value_double(@NotNull long ptrToValue);
+
+ public static double sqlite3_value_double(@NotNull sqlite3_value v){
+ return sqlite3_value_double(v.getNativePointer());
+ }
+
+ static native sqlite3_value sqlite3_value_dup(@NotNull long ptrToValue);
+
+ public static sqlite3_value sqlite3_value_dup(@NotNull sqlite3_value v){
+ return sqlite3_value_dup(v.getNativePointer());
+ }
+
+ static native int sqlite3_value_encoding(@NotNull long ptrToValue);
+
+ public static int sqlite3_value_encoding(@NotNull sqlite3_value v){
+ return sqlite3_value_encoding(v.getNativePointer());
+ }
+
+ static native void sqlite3_value_free(@Nullable long ptrToValue);
+
+ public static void sqlite3_value_free(@Nullable sqlite3_value v){
+ sqlite3_value_free(v.getNativePointer());
+ }
+
+ static native boolean sqlite3_value_frombind(@NotNull long ptrToValue);
+
+ public static boolean sqlite3_value_frombind(@NotNull sqlite3_value v){
+ return sqlite3_value_frombind(v.getNativePointer());
+ }
+
+ static native int sqlite3_value_int(@NotNull long ptrToValue);
+
+ public static int sqlite3_value_int(@NotNull sqlite3_value v){
+ return sqlite3_value_int(v.getNativePointer());
+ }
+
+ static native long sqlite3_value_int64(@NotNull long ptrToValue);
+
+ public static long sqlite3_value_int64(@NotNull sqlite3_value v){
+ return sqlite3_value_int64(v.getNativePointer());
+ }
+
+ static native Object sqlite3_value_java_object(@NotNull long ptrToValue);
+
+ /**
+ If the given value was set using {@link
+ #sqlite3_result_java_object} then this function returns that
+ object, else it returns null.
+
+
It is up to the caller to inspect the object to determine its
+ type, and cast it if necessary.
+ */
+ public static Object sqlite3_value_java_object(@NotNull sqlite3_value v){
+ return sqlite3_value_java_object(v.getNativePointer());
+ }
+
+ /**
+ A variant of sqlite3_value_java_object() which returns the
+ fetched object cast to T if the object is an instance of the
+ given Class, else it returns null.
+ */
+ @SuppressWarnings("unchecked")
+ public static T sqlite3_value_java_casted(@NotNull sqlite3_value v,
+ @NotNull Class type){
+ final Object o = sqlite3_value_java_object(v);
+ return type.isInstance(o) ? (T)o : null;
+ }
+
+ static native int sqlite3_value_nochange(@NotNull long ptrToValue);
+
+ public static int sqlite3_value_nochange(@NotNull sqlite3_value v){
+ return sqlite3_value_nochange(v.getNativePointer());
+ }
+
+ static native int sqlite3_value_numeric_type(@NotNull long ptrToValue);
+
+ public static int sqlite3_value_numeric_type(@NotNull sqlite3_value v){
+ return sqlite3_value_numeric_type(v.getNativePointer());
+ }
+
+ static native int sqlite3_value_subtype(@NotNull long ptrToValue);
+
+ public static int sqlite3_value_subtype(@NotNull sqlite3_value v){
+ return sqlite3_value_subtype(v.getNativePointer());
+ }
+
+ static native byte[] sqlite3_value_text(@NotNull long ptrToValue);
+
+ /**
+ Functions identially to the C API, and this note is just to
+ stress that the returned bytes are encoded as UTF-8. It returns
+ null if the underlying C-level sqlite3_value_text() returns NULL
+ or on allocation error.
+ */
+ public static byte[] sqlite3_value_text(@NotNull sqlite3_value v){
+ return sqlite3_value_text(v.getNativePointer());
+ }
+
+ static native String sqlite3_value_text16(@NotNull long ptrToValue);
+
+ public static String sqlite3_value_text16(@NotNull sqlite3_value v){
+ return sqlite3_value_text16(v.getNativePointer());
+ }
+
+ static native int sqlite3_value_type(@NotNull long ptrToValue);
+
+ public static int sqlite3_value_type(@NotNull sqlite3_value v){
+ return sqlite3_value_type(v.getNativePointer());
+ }
+
+ /**
+ This is NOT part of the public API. It exists solely as a place
+ for this code's developers to collect internal metrics and such.
+ It has no stable interface. It may go way or change behavior at
+ any time.
+ */
+ public static native void sqlite3_jni_internal_details();
+
+ //////////////////////////////////////////////////////////////////////
+ // SQLITE_... constants follow...
+
+ // version info
+ public static final int SQLITE_VERSION_NUMBER = sqlite3_libversion_number();
+ public static final String SQLITE_VERSION = sqlite3_libversion();
+ public static final String SQLITE_SOURCE_ID = sqlite3_sourceid();
+
+ // access
+ public static final int SQLITE_ACCESS_EXISTS = 0;
+ public static final int SQLITE_ACCESS_READWRITE = 1;
+ public static final int SQLITE_ACCESS_READ = 2;
+
+ // authorizer
+ public static final int SQLITE_DENY = 1;
+ public static final int SQLITE_IGNORE = 2;
+ public static final int SQLITE_CREATE_INDEX = 1;
+ public static final int SQLITE_CREATE_TABLE = 2;
+ public static final int SQLITE_CREATE_TEMP_INDEX = 3;
+ public static final int SQLITE_CREATE_TEMP_TABLE = 4;
+ public static final int SQLITE_CREATE_TEMP_TRIGGER = 5;
+ public static final int SQLITE_CREATE_TEMP_VIEW = 6;
+ public static final int SQLITE_CREATE_TRIGGER = 7;
+ public static final int SQLITE_CREATE_VIEW = 8;
+ public static final int SQLITE_DELETE = 9;
+ public static final int SQLITE_DROP_INDEX = 10;
+ public static final int SQLITE_DROP_TABLE = 11;
+ public static final int SQLITE_DROP_TEMP_INDEX = 12;
+ public static final int SQLITE_DROP_TEMP_TABLE = 13;
+ public static final int SQLITE_DROP_TEMP_TRIGGER = 14;
+ public static final int SQLITE_DROP_TEMP_VIEW = 15;
+ public static final int SQLITE_DROP_TRIGGER = 16;
+ public static final int SQLITE_DROP_VIEW = 17;
+ public static final int SQLITE_INSERT = 18;
+ public static final int SQLITE_PRAGMA = 19;
+ public static final int SQLITE_READ = 20;
+ public static final int SQLITE_SELECT = 21;
+ public static final int SQLITE_TRANSACTION = 22;
+ public static final int SQLITE_UPDATE = 23;
+ public static final int SQLITE_ATTACH = 24;
+ public static final int SQLITE_DETACH = 25;
+ public static final int SQLITE_ALTER_TABLE = 26;
+ public static final int SQLITE_REINDEX = 27;
+ public static final int SQLITE_ANALYZE = 28;
+ public static final int SQLITE_CREATE_VTABLE = 29;
+ public static final int SQLITE_DROP_VTABLE = 30;
+ public static final int SQLITE_FUNCTION = 31;
+ public static final int SQLITE_SAVEPOINT = 32;
+ public static final int SQLITE_RECURSIVE = 33;
+
+ // blob finalizers: these should, because they are treated as
+ // special pointer values in C, ideally have the same sizeof() as
+ // the platform's (void*), but we can't know that size from here.
+ public static final long SQLITE_STATIC = 0;
+ public static final long SQLITE_TRANSIENT = -1;
+
+ // changeset
+ public static final int SQLITE_CHANGESETSTART_INVERT = 2;
+ public static final int SQLITE_CHANGESETAPPLY_NOSAVEPOINT = 1;
+ public static final int SQLITE_CHANGESETAPPLY_INVERT = 2;
+ public static final int SQLITE_CHANGESETAPPLY_IGNORENOOP = 4;
+ public static final int SQLITE_CHANGESET_DATA = 1;
+ public static final int SQLITE_CHANGESET_NOTFOUND = 2;
+ public static final int SQLITE_CHANGESET_CONFLICT = 3;
+ public static final int SQLITE_CHANGESET_CONSTRAINT = 4;
+ public static final int SQLITE_CHANGESET_FOREIGN_KEY = 5;
+ public static final int SQLITE_CHANGESET_OMIT = 0;
+ public static final int SQLITE_CHANGESET_REPLACE = 1;
+ public static final int SQLITE_CHANGESET_ABORT = 2;
+
+ // config
+ public static final int SQLITE_CONFIG_SINGLETHREAD = 1;
+ public static final int SQLITE_CONFIG_MULTITHREAD = 2;
+ public static final int SQLITE_CONFIG_SERIALIZED = 3;
+ public static final int SQLITE_CONFIG_MALLOC = 4;
+ public static final int SQLITE_CONFIG_GETMALLOC = 5;
+ public static final int SQLITE_CONFIG_SCRATCH = 6;
+ public static final int SQLITE_CONFIG_PAGECACHE = 7;
+ public static final int SQLITE_CONFIG_HEAP = 8;
+ public static final int SQLITE_CONFIG_MEMSTATUS = 9;
+ public static final int SQLITE_CONFIG_MUTEX = 10;
+ public static final int SQLITE_CONFIG_GETMUTEX = 11;
+ public static final int SQLITE_CONFIG_LOOKASIDE = 13;
+ public static final int SQLITE_CONFIG_PCACHE = 14;
+ public static final int SQLITE_CONFIG_GETPCACHE = 15;
+ public static final int SQLITE_CONFIG_LOG = 16;
+ public static final int SQLITE_CONFIG_URI = 17;
+ public static final int SQLITE_CONFIG_PCACHE2 = 18;
+ public static final int SQLITE_CONFIG_GETPCACHE2 = 19;
+ public static final int SQLITE_CONFIG_COVERING_INDEX_SCAN = 20;
+ public static final int SQLITE_CONFIG_SQLLOG = 21;
+ public static final int SQLITE_CONFIG_MMAP_SIZE = 22;
+ public static final int SQLITE_CONFIG_WIN32_HEAPSIZE = 23;
+ public static final int SQLITE_CONFIG_PCACHE_HDRSZ = 24;
+ public static final int SQLITE_CONFIG_PMASZ = 25;
+ public static final int SQLITE_CONFIG_STMTJRNL_SPILL = 26;
+ public static final int SQLITE_CONFIG_SMALL_MALLOC = 27;
+ public static final int SQLITE_CONFIG_SORTERREF_SIZE = 28;
+ public static final int SQLITE_CONFIG_MEMDB_MAXSIZE = 29;
+
+ // data types
+ public static final int SQLITE_INTEGER = 1;
+ public static final int SQLITE_FLOAT = 2;
+ public static final int SQLITE_TEXT = 3;
+ public static final int SQLITE_BLOB = 4;
+ public static final int SQLITE_NULL = 5;
+
+ // db config
+ public static final int SQLITE_DBCONFIG_MAINDBNAME = 1000;
+ public static final int SQLITE_DBCONFIG_LOOKASIDE = 1001;
+ public static final int SQLITE_DBCONFIG_ENABLE_FKEY = 1002;
+ public static final int SQLITE_DBCONFIG_ENABLE_TRIGGER = 1003;
+ public static final int SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER = 1004;
+ public static final int SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION = 1005;
+ public static final int SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE = 1006;
+ public static final int SQLITE_DBCONFIG_ENABLE_QPSG = 1007;
+ public static final int SQLITE_DBCONFIG_TRIGGER_EQP = 1008;
+ public static final int SQLITE_DBCONFIG_RESET_DATABASE = 1009;
+ public static final int SQLITE_DBCONFIG_DEFENSIVE = 1010;
+ public static final int SQLITE_DBCONFIG_WRITABLE_SCHEMA = 1011;
+ public static final int SQLITE_DBCONFIG_LEGACY_ALTER_TABLE = 1012;
+ public static final int SQLITE_DBCONFIG_DQS_DML = 1013;
+ public static final int SQLITE_DBCONFIG_DQS_DDL = 1014;
+ public static final int SQLITE_DBCONFIG_ENABLE_VIEW = 1015;
+ public static final int SQLITE_DBCONFIG_LEGACY_FILE_FORMAT = 1016;
+ public static final int SQLITE_DBCONFIG_TRUSTED_SCHEMA = 1017;
+ public static final int SQLITE_DBCONFIG_STMT_SCANSTATUS = 1018;
+ public static final int SQLITE_DBCONFIG_REVERSE_SCANORDER = 1019;
+ public static final int SQLITE_DBCONFIG_MAX = 1019;
+
+ // db status
+ public static final int SQLITE_DBSTATUS_LOOKASIDE_USED = 0;
+ public static final int SQLITE_DBSTATUS_CACHE_USED = 1;
+ public static final int SQLITE_DBSTATUS_SCHEMA_USED = 2;
+ public static final int SQLITE_DBSTATUS_STMT_USED = 3;
+ public static final int SQLITE_DBSTATUS_LOOKASIDE_HIT = 4;
+ public static final int SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE = 5;
+ public static final int SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL = 6;
+ public static final int SQLITE_DBSTATUS_CACHE_HIT = 7;
+ public static final int SQLITE_DBSTATUS_CACHE_MISS = 8;
+ public static final int SQLITE_DBSTATUS_CACHE_WRITE = 9;
+ public static final int SQLITE_DBSTATUS_DEFERRED_FKS = 10;
+ public static final int SQLITE_DBSTATUS_CACHE_USED_SHARED = 11;
+ public static final int SQLITE_DBSTATUS_CACHE_SPILL = 12;
+ public static final int SQLITE_DBSTATUS_MAX = 12;
+
+ // encodings
+ public static final int SQLITE_UTF8 = 1;
+ public static final int SQLITE_UTF16LE = 2;
+ public static final int SQLITE_UTF16BE = 3;
+ public static final int SQLITE_UTF16 = 4;
+ public static final int SQLITE_UTF16_ALIGNED = 8;
+
+ // fcntl
+ public static final int SQLITE_FCNTL_LOCKSTATE = 1;
+ public static final int SQLITE_FCNTL_GET_LOCKPROXYFILE = 2;
+ public static final int SQLITE_FCNTL_SET_LOCKPROXYFILE = 3;
+ public static final int SQLITE_FCNTL_LAST_ERRNO = 4;
+ public static final int SQLITE_FCNTL_SIZE_HINT = 5;
+ public static final int SQLITE_FCNTL_CHUNK_SIZE = 6;
+ public static final int SQLITE_FCNTL_FILE_POINTER = 7;
+ public static final int SQLITE_FCNTL_SYNC_OMITTED = 8;
+ public static final int SQLITE_FCNTL_WIN32_AV_RETRY = 9;
+ public static final int SQLITE_FCNTL_PERSIST_WAL = 10;
+ public static final int SQLITE_FCNTL_OVERWRITE = 11;
+ public static final int SQLITE_FCNTL_VFSNAME = 12;
+ public static final int SQLITE_FCNTL_POWERSAFE_OVERWRITE = 13;
+ public static final int SQLITE_FCNTL_PRAGMA = 14;
+ public static final int SQLITE_FCNTL_BUSYHANDLER = 15;
+ public static final int SQLITE_FCNTL_TEMPFILENAME = 16;
+ public static final int SQLITE_FCNTL_MMAP_SIZE = 18;
+ public static final int SQLITE_FCNTL_TRACE = 19;
+ public static final int SQLITE_FCNTL_HAS_MOVED = 20;
+ public static final int SQLITE_FCNTL_SYNC = 21;
+ public static final int SQLITE_FCNTL_COMMIT_PHASETWO = 22;
+ public static final int SQLITE_FCNTL_WIN32_SET_HANDLE = 23;
+ public static final int SQLITE_FCNTL_WAL_BLOCK = 24;
+ public static final int SQLITE_FCNTL_ZIPVFS = 25;
+ public static final int SQLITE_FCNTL_RBU = 26;
+ public static final int SQLITE_FCNTL_VFS_POINTER = 27;
+ public static final int SQLITE_FCNTL_JOURNAL_POINTER = 28;
+ public static final int SQLITE_FCNTL_WIN32_GET_HANDLE = 29;
+ public static final int SQLITE_FCNTL_PDB = 30;
+ public static final int SQLITE_FCNTL_BEGIN_ATOMIC_WRITE = 31;
+ public static final int SQLITE_FCNTL_COMMIT_ATOMIC_WRITE = 32;
+ public static final int SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE = 33;
+ public static final int SQLITE_FCNTL_LOCK_TIMEOUT = 34;
+ public static final int SQLITE_FCNTL_DATA_VERSION = 35;
+ public static final int SQLITE_FCNTL_SIZE_LIMIT = 36;
+ public static final int SQLITE_FCNTL_CKPT_DONE = 37;
+ public static final int SQLITE_FCNTL_RESERVE_BYTES = 38;
+ public static final int SQLITE_FCNTL_CKPT_START = 39;
+ public static final int SQLITE_FCNTL_EXTERNAL_READER = 40;
+ public static final int SQLITE_FCNTL_CKSM_FILE = 41;
+ public static final int SQLITE_FCNTL_RESET_CACHE = 42;
+
+ // flock
+ public static final int SQLITE_LOCK_NONE = 0;
+ public static final int SQLITE_LOCK_SHARED = 1;
+ public static final int SQLITE_LOCK_RESERVED = 2;
+ public static final int SQLITE_LOCK_PENDING = 3;
+ public static final int SQLITE_LOCK_EXCLUSIVE = 4;
+
+ // iocap
+ public static final int SQLITE_IOCAP_ATOMIC = 1;
+ public static final int SQLITE_IOCAP_ATOMIC512 = 2;
+ public static final int SQLITE_IOCAP_ATOMIC1K = 4;
+ public static final int SQLITE_IOCAP_ATOMIC2K = 8;
+ public static final int SQLITE_IOCAP_ATOMIC4K = 16;
+ public static final int SQLITE_IOCAP_ATOMIC8K = 32;
+ public static final int SQLITE_IOCAP_ATOMIC16K = 64;
+ public static final int SQLITE_IOCAP_ATOMIC32K = 128;
+ public static final int SQLITE_IOCAP_ATOMIC64K = 256;
+ public static final int SQLITE_IOCAP_SAFE_APPEND = 512;
+ public static final int SQLITE_IOCAP_SEQUENTIAL = 1024;
+ public static final int SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN = 2048;
+ public static final int SQLITE_IOCAP_POWERSAFE_OVERWRITE = 4096;
+ public static final int SQLITE_IOCAP_IMMUTABLE = 8192;
+ public static final int SQLITE_IOCAP_BATCH_ATOMIC = 16384;
+
+ // limits
+ public static final int SQLITE_LIMIT_LENGTH = 0;
+ public static final int SQLITE_LIMIT_SQL_LENGTH = 1;
+ public static final int SQLITE_LIMIT_COLUMN = 2;
+ public static final int SQLITE_LIMIT_EXPR_DEPTH = 3;
+ public static final int SQLITE_LIMIT_COMPOUND_SELECT = 4;
+ public static final int SQLITE_LIMIT_VDBE_OP = 5;
+ public static final int SQLITE_LIMIT_FUNCTION_ARG = 6;
+ public static final int SQLITE_LIMIT_ATTACHED = 7;
+ public static final int SQLITE_LIMIT_LIKE_PATTERN_LENGTH = 8;
+ public static final int SQLITE_LIMIT_VARIABLE_NUMBER = 9;
+ public static final int SQLITE_LIMIT_TRIGGER_DEPTH = 10;
+ public static final int SQLITE_LIMIT_WORKER_THREADS = 11;
+
+ // open flags
+
+ public static final int SQLITE_OPEN_READONLY = 0x00000001 /* Ok for sqlite3_open_v2() */;
+ public static final int SQLITE_OPEN_READWRITE = 0x00000002 /* Ok for sqlite3_open_v2() */;
+ public static final int SQLITE_OPEN_CREATE = 0x00000004 /* Ok for sqlite3_open_v2() */;
+ //public static final int SQLITE_OPEN_DELETEONCLOSE = 0x00000008 /* VFS only */;
+ //public static final int SQLITE_OPEN_EXCLUSIVE = 0x00000010 /* VFS only */;
+ //public static final int SQLITE_OPEN_AUTOPROXY = 0x00000020 /* VFS only */;
+ public static final int SQLITE_OPEN_URI = 0x00000040 /* Ok for sqlite3_open_v2() */;
+ public static final int SQLITE_OPEN_MEMORY = 0x00000080 /* Ok for sqlite3_open_v2() */;
+ //public static final int SQLITE_OPEN_MAIN_DB = 0x00000100 /* VFS only */;
+ //public static final int SQLITE_OPEN_TEMP_DB = 0x00000200 /* VFS only */;
+ //public static final int SQLITE_OPEN_TRANSIENT_DB = 0x00000400 /* VFS only */;
+ //public static final int SQLITE_OPEN_MAIN_JOURNAL = 0x00000800 /* VFS only */;
+ //public static final int SQLITE_OPEN_TEMP_JOURNAL = 0x00001000 /* VFS only */;
+ //public static final int SQLITE_OPEN_SUBJOURNAL = 0x00002000 /* VFS only */;
+ //public static final int SQLITE_OPEN_SUPER_JOURNAL = 0x00004000 /* VFS only */;
+ public static final int SQLITE_OPEN_NOMUTEX = 0x00008000 /* Ok for sqlite3_open_v2() */;
+ public static final int SQLITE_OPEN_FULLMUTEX = 0x00010000 /* Ok for sqlite3_open_v2() */;
+ public static final int SQLITE_OPEN_SHAREDCACHE = 0x00020000 /* Ok for sqlite3_open_v2() */;
+ public static final int SQLITE_OPEN_PRIVATECACHE = 0x00040000 /* Ok for sqlite3_open_v2() */;
+ //public static final int SQLITE_OPEN_WAL = 0x00080000 /* VFS only */;
+ public static final int SQLITE_OPEN_NOFOLLOW = 0x01000000 /* Ok for sqlite3_open_v2() */;
+ public static final int SQLITE_OPEN_EXRESCODE = 0x02000000 /* Extended result codes */;
+
+ // prepare flags
+ public static final int SQLITE_PREPARE_PERSISTENT = 1;
+ public static final int SQLITE_PREPARE_NORMALIZE = 2;
+ public static final int SQLITE_PREPARE_NO_VTAB = 4;
+
+ // result codes
+ public static final int SQLITE_OK = 0;
+ public static final int SQLITE_ERROR = 1;
+ public static final int SQLITE_INTERNAL = 2;
+ public static final int SQLITE_PERM = 3;
+ public static final int SQLITE_ABORT = 4;
+ public static final int SQLITE_BUSY = 5;
+ public static final int SQLITE_LOCKED = 6;
+ public static final int SQLITE_NOMEM = 7;
+ public static final int SQLITE_READONLY = 8;
+ public static final int SQLITE_INTERRUPT = 9;
+ public static final int SQLITE_IOERR = 10;
+ public static final int SQLITE_CORRUPT = 11;
+ public static final int SQLITE_NOTFOUND = 12;
+ public static final int SQLITE_FULL = 13;
+ public static final int SQLITE_CANTOPEN = 14;
+ public static final int SQLITE_PROTOCOL = 15;
+ public static final int SQLITE_EMPTY = 16;
+ public static final int SQLITE_SCHEMA = 17;
+ public static final int SQLITE_TOOBIG = 18;
+ public static final int SQLITE_CONSTRAINT = 19;
+ public static final int SQLITE_MISMATCH = 20;
+ public static final int SQLITE_MISUSE = 21;
+ public static final int SQLITE_NOLFS = 22;
+ public static final int SQLITE_AUTH = 23;
+ public static final int SQLITE_FORMAT = 24;
+ public static final int SQLITE_RANGE = 25;
+ public static final int SQLITE_NOTADB = 26;
+ public static final int SQLITE_NOTICE = 27;
+ public static final int SQLITE_WARNING = 28;
+ public static final int SQLITE_ROW = 100;
+ public static final int SQLITE_DONE = 101;
+ public static final int SQLITE_ERROR_MISSING_COLLSEQ = 257;
+ public static final int SQLITE_ERROR_RETRY = 513;
+ public static final int SQLITE_ERROR_SNAPSHOT = 769;
+ public static final int SQLITE_IOERR_READ = 266;
+ public static final int SQLITE_IOERR_SHORT_READ = 522;
+ public static final int SQLITE_IOERR_WRITE = 778;
+ public static final int SQLITE_IOERR_FSYNC = 1034;
+ public static final int SQLITE_IOERR_DIR_FSYNC = 1290;
+ public static final int SQLITE_IOERR_TRUNCATE = 1546;
+ public static final int SQLITE_IOERR_FSTAT = 1802;
+ public static final int SQLITE_IOERR_UNLOCK = 2058;
+ public static final int SQLITE_IOERR_RDLOCK = 2314;
+ public static final int SQLITE_IOERR_DELETE = 2570;
+ public static final int SQLITE_IOERR_BLOCKED = 2826;
+ public static final int SQLITE_IOERR_NOMEM = 3082;
+ public static final int SQLITE_IOERR_ACCESS = 3338;
+ public static final int SQLITE_IOERR_CHECKRESERVEDLOCK = 3594;
+ public static final int SQLITE_IOERR_LOCK = 3850;
+ public static final int SQLITE_IOERR_CLOSE = 4106;
+ public static final int SQLITE_IOERR_DIR_CLOSE = 4362;
+ public static final int SQLITE_IOERR_SHMOPEN = 4618;
+ public static final int SQLITE_IOERR_SHMSIZE = 4874;
+ public static final int SQLITE_IOERR_SHMLOCK = 5130;
+ public static final int SQLITE_IOERR_SHMMAP = 5386;
+ public static final int SQLITE_IOERR_SEEK = 5642;
+ public static final int SQLITE_IOERR_DELETE_NOENT = 5898;
+ public static final int SQLITE_IOERR_MMAP = 6154;
+ public static final int SQLITE_IOERR_GETTEMPPATH = 6410;
+ public static final int SQLITE_IOERR_CONVPATH = 6666;
+ public static final int SQLITE_IOERR_VNODE = 6922;
+ public static final int SQLITE_IOERR_AUTH = 7178;
+ public static final int SQLITE_IOERR_BEGIN_ATOMIC = 7434;
+ public static final int SQLITE_IOERR_COMMIT_ATOMIC = 7690;
+ public static final int SQLITE_IOERR_ROLLBACK_ATOMIC = 7946;
+ public static final int SQLITE_IOERR_DATA = 8202;
+ public static final int SQLITE_IOERR_CORRUPTFS = 8458;
+ public static final int SQLITE_LOCKED_SHAREDCACHE = 262;
+ public static final int SQLITE_LOCKED_VTAB = 518;
+ public static final int SQLITE_BUSY_RECOVERY = 261;
+ public static final int SQLITE_BUSY_SNAPSHOT = 517;
+ public static final int SQLITE_BUSY_TIMEOUT = 773;
+ public static final int SQLITE_CANTOPEN_NOTEMPDIR = 270;
+ public static final int SQLITE_CANTOPEN_ISDIR = 526;
+ public static final int SQLITE_CANTOPEN_FULLPATH = 782;
+ public static final int SQLITE_CANTOPEN_CONVPATH = 1038;
+ public static final int SQLITE_CANTOPEN_SYMLINK = 1550;
+ public static final int SQLITE_CORRUPT_VTAB = 267;
+ public static final int SQLITE_CORRUPT_SEQUENCE = 523;
+ public static final int SQLITE_CORRUPT_INDEX = 779;
+ public static final int SQLITE_READONLY_RECOVERY = 264;
+ public static final int SQLITE_READONLY_CANTLOCK = 520;
+ public static final int SQLITE_READONLY_ROLLBACK = 776;
+ public static final int SQLITE_READONLY_DBMOVED = 1032;
+ public static final int SQLITE_READONLY_CANTINIT = 1288;
+ public static final int SQLITE_READONLY_DIRECTORY = 1544;
+ public static final int SQLITE_ABORT_ROLLBACK = 516;
+ public static final int SQLITE_CONSTRAINT_CHECK = 275;
+ public static final int SQLITE_CONSTRAINT_COMMITHOOK = 531;
+ public static final int SQLITE_CONSTRAINT_FOREIGNKEY = 787;
+ public static final int SQLITE_CONSTRAINT_FUNCTION = 1043;
+ public static final int SQLITE_CONSTRAINT_NOTNULL = 1299;
+ public static final int SQLITE_CONSTRAINT_PRIMARYKEY = 1555;
+ public static final int SQLITE_CONSTRAINT_TRIGGER = 1811;
+ public static final int SQLITE_CONSTRAINT_UNIQUE = 2067;
+ public static final int SQLITE_CONSTRAINT_VTAB = 2323;
+ public static final int SQLITE_CONSTRAINT_ROWID = 2579;
+ public static final int SQLITE_CONSTRAINT_PINNED = 2835;
+ public static final int SQLITE_CONSTRAINT_DATATYPE = 3091;
+ public static final int SQLITE_NOTICE_RECOVER_WAL = 283;
+ public static final int SQLITE_NOTICE_RECOVER_ROLLBACK = 539;
+ public static final int SQLITE_WARNING_AUTOINDEX = 284;
+ public static final int SQLITE_AUTH_USER = 279;
+ public static final int SQLITE_OK_LOAD_PERMANENTLY = 256;
+
+ // serialize
+ public static final int SQLITE_SERIALIZE_NOCOPY = 1;
+ public static final int SQLITE_DESERIALIZE_FREEONCLOSE = 1;
+ public static final int SQLITE_DESERIALIZE_READONLY = 4;
+ public static final int SQLITE_DESERIALIZE_RESIZEABLE = 2;
+
+ // session
+ public static final int SQLITE_SESSION_CONFIG_STRMSIZE = 1;
+ public static final int SQLITE_SESSION_OBJCONFIG_SIZE = 1;
+
+ // sqlite3 status
+ public static final int SQLITE_STATUS_MEMORY_USED = 0;
+ public static final int SQLITE_STATUS_PAGECACHE_USED = 1;
+ public static final int SQLITE_STATUS_PAGECACHE_OVERFLOW = 2;
+ public static final int SQLITE_STATUS_MALLOC_SIZE = 5;
+ public static final int SQLITE_STATUS_PARSER_STACK = 6;
+ public static final int SQLITE_STATUS_PAGECACHE_SIZE = 7;
+ public static final int SQLITE_STATUS_MALLOC_COUNT = 9;
+
+ // stmt status
+ public static final int SQLITE_STMTSTATUS_FULLSCAN_STEP = 1;
+ public static final int SQLITE_STMTSTATUS_SORT = 2;
+ public static final int SQLITE_STMTSTATUS_AUTOINDEX = 3;
+ public static final int SQLITE_STMTSTATUS_VM_STEP = 4;
+ public static final int SQLITE_STMTSTATUS_REPREPARE = 5;
+ public static final int SQLITE_STMTSTATUS_RUN = 6;
+ public static final int SQLITE_STMTSTATUS_FILTER_MISS = 7;
+ public static final int SQLITE_STMTSTATUS_FILTER_HIT = 8;
+ public static final int SQLITE_STMTSTATUS_MEMUSED = 99;
+
+ // sync flags
+ public static final int SQLITE_SYNC_NORMAL = 2;
+ public static final int SQLITE_SYNC_FULL = 3;
+ public static final int SQLITE_SYNC_DATAONLY = 16;
+
+ // tracing flags
+ public static final int SQLITE_TRACE_STMT = 1;
+ public static final int SQLITE_TRACE_PROFILE = 2;
+ public static final int SQLITE_TRACE_ROW = 4;
+ public static final int SQLITE_TRACE_CLOSE = 8;
+
+ // transaction state
+ public static final int SQLITE_TXN_NONE = 0;
+ public static final int SQLITE_TXN_READ = 1;
+ public static final int SQLITE_TXN_WRITE = 2;
+
+ // udf flags
+ public static final int SQLITE_DETERMINISTIC = 0x000000800;
+ public static final int SQLITE_DIRECTONLY = 0x000080000;
+ public static final int SQLITE_INNOCUOUS = 0x000200000;
+
+ // virtual tables
+ public static final int SQLITE_INDEX_SCAN_UNIQUE = 1;
+ public static final int SQLITE_INDEX_CONSTRAINT_EQ = 2;
+ public static final int SQLITE_INDEX_CONSTRAINT_GT = 4;
+ public static final int SQLITE_INDEX_CONSTRAINT_LE = 8;
+ public static final int SQLITE_INDEX_CONSTRAINT_LT = 16;
+ public static final int SQLITE_INDEX_CONSTRAINT_GE = 32;
+ public static final int SQLITE_INDEX_CONSTRAINT_MATCH = 64;
+ public static final int SQLITE_INDEX_CONSTRAINT_LIKE = 65;
+ public static final int SQLITE_INDEX_CONSTRAINT_GLOB = 66;
+ public static final int SQLITE_INDEX_CONSTRAINT_REGEXP = 67;
+ public static final int SQLITE_INDEX_CONSTRAINT_NE = 68;
+ public static final int SQLITE_INDEX_CONSTRAINT_ISNOT = 69;
+ public static final int SQLITE_INDEX_CONSTRAINT_ISNOTNULL = 70;
+ public static final int SQLITE_INDEX_CONSTRAINT_ISNULL = 71;
+ public static final int SQLITE_INDEX_CONSTRAINT_IS = 72;
+ public static final int SQLITE_INDEX_CONSTRAINT_LIMIT = 73;
+ public static final int SQLITE_INDEX_CONSTRAINT_OFFSET = 74;
+ public static final int SQLITE_INDEX_CONSTRAINT_FUNCTION = 150;
+ public static final int SQLITE_VTAB_CONSTRAINT_SUPPORT = 1;
+ public static final int SQLITE_VTAB_INNOCUOUS = 2;
+ public static final int SQLITE_VTAB_DIRECTONLY = 3;
+ public static final int SQLITE_VTAB_USES_ALL_SCHEMAS = 4;
+ public static final int SQLITE_ROLLBACK = 1;
+ public static final int SQLITE_FAIL = 3;
+ public static final int SQLITE_REPLACE = 5;
+ static {
+ // This MUST come after the SQLITE_MAX_... values or else
+ // attempting to modify them silently fails.
+ init();
+ }
+}
diff --git a/ext/jni/src/org/sqlite/jni/capi/CallbackProxy.java b/ext/jni/src/org/sqlite/jni/capi/CallbackProxy.java
new file mode 100644
index 0000000000..0495702561
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/CallbackProxy.java
@@ -0,0 +1,44 @@
+/*
+** 2023-08-25
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+/**
+ This marker interface exists soley for use as a documentation and
+ class-grouping tool. It should be applied to interfaces or
+ classes which have a call() method implementing some specific
+ callback interface on behalf of the C library.
+
+ Unless very explicitely documented otherwise, callbacks must
+ never throw. Any which do throw but should not might trigger debug
+ output regarding the error, but the exception will not be
+ propagated. For callback interfaces which support returning error
+ info to the core, the JNI binding will convert any exceptions to
+ C-level error information. For callback interfaces which do not
+ support, all exceptions will necessarily be suppressed in order to
+ retain the C-style no-throw semantics.
+
+
Callbacks of this style follow a common naming convention:
+
+
1) They use the UpperCamelCase form of the C function they're
+ proxying for, minus the {@code sqlite3_} prefix, plus a {@code
+ Callback} suffix. e.g. {@code sqlite3_busy_handler()}'s callback is
+ named {@code BusyHandlerCallback}. Exceptions are made where that
+ would potentially be ambiguous, e.g. {@link ConfigSqllogCallback}
+ instead of {@code ConfigCallback} because the {@code
+ sqlite3_config()} interface may need to support more callback types
+ in the future.
+
+
2) They all have a {@code call()} method but its signature is
+ callback-specific.
+*/
+public interface CallbackProxy {}
diff --git a/ext/jni/src/org/sqlite/jni/capi/CollationCallback.java b/ext/jni/src/org/sqlite/jni/capi/CollationCallback.java
new file mode 100644
index 0000000000..ed8bd09475
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/CollationCallback.java
@@ -0,0 +1,35 @@
+/*
+** 2023-08-25
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+import org.sqlite.jni.annotation.NotNull;
+
+/**
+ Callback for use with {@link CApi#sqlite3_create_collation}.
+
+ @see AbstractCollationCallback
+*/
+public interface CollationCallback
+ extends CallbackProxy, XDestroyCallback {
+ /**
+ Must compare the given byte arrays and return the result using
+ {@code memcmp()} semantics.
+ */
+ int call(@NotNull byte[] lhs, @NotNull byte[] rhs);
+
+ /**
+ Called by SQLite when the collation is destroyed. If a collation
+ requires custom cleanup, override this method.
+ */
+ void xDestroy();
+}
diff --git a/ext/jni/src/org/sqlite/jni/CollationNeeded.java b/ext/jni/src/org/sqlite/jni/capi/CollationNeededCallback.java
similarity index 64%
rename from ext/jni/src/org/sqlite/jni/CollationNeeded.java
rename to ext/jni/src/org/sqlite/jni/capi/CollationNeededCallback.java
index 85214a1d27..fe61fe5065 100644
--- a/ext/jni/src/org/sqlite/jni/CollationNeeded.java
+++ b/ext/jni/src/org/sqlite/jni/capi/CollationNeededCallback.java
@@ -1,5 +1,5 @@
/*
-** 2023-07-30
+** 2023-08-25
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
@@ -11,18 +11,18 @@
*************************************************************************
** This file is part of the JNI bindings for the sqlite3 C API.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.capi;
/**
- Callback proxy for use with sqlite3_collation_needed().
+ Callback for use with {@link CApi#sqlite3_collation_needed}.
*/
-public interface CollationNeeded {
+public interface CollationNeededCallback extends CallbackProxy {
/**
Has the same semantics as the C-level sqlite3_create_collation()
callback.
- If it throws, the exception message is passed on to the db and
+
If it throws, the exception message is passed on to the db and
the exception is suppressed.
*/
- int xCollationNeeded(sqlite3 db, int eTextRep, String collationName);
+ int call(sqlite3 db, int eTextRep, String collationName);
}
diff --git a/ext/jni/src/org/sqlite/jni/UpdateHook.java b/ext/jni/src/org/sqlite/jni/capi/CommitHookCallback.java
similarity index 60%
rename from ext/jni/src/org/sqlite/jni/UpdateHook.java
rename to ext/jni/src/org/sqlite/jni/capi/CommitHookCallback.java
index 171e2bdb41..24373bdf2b 100644
--- a/ext/jni/src/org/sqlite/jni/UpdateHook.java
+++ b/ext/jni/src/org/sqlite/jni/capi/CommitHookCallback.java
@@ -1,5 +1,5 @@
/*
-** 2023-07-22
+** 2023-08-25
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
@@ -11,15 +11,15 @@
*************************************************************************
** This file is part of the JNI bindings for the sqlite3 C API.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.capi;
/**
- Callback proxy for use with sqlite3_update_hook().
+ Callback for use with {@link CApi#sqlite3_commit_hook}.
*/
-public interface UpdateHook {
+public interface CommitHookCallback extends CallbackProxy {
/**
- Works as documented for the sqlite3_update_hook() callback.
- Must not throw.
+ Works as documented for the C-level sqlite3_commit_hook()
+ callback. Must not throw.
*/
- void xUpdateHook(int opId, String dbName, String tableName, long rowId);
+ int call();
}
diff --git a/ext/jni/src/org/sqlite/jni/capi/ConfigLogCallback.java b/ext/jni/src/org/sqlite/jni/capi/ConfigLogCallback.java
new file mode 100644
index 0000000000..6513b0730d
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/ConfigLogCallback.java
@@ -0,0 +1,25 @@
+/*
+** 2023-08-23
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+
+/**
+ A callback for use with sqlite3_config().
+*/
+public interface ConfigLogCallback {
+ /**
+ Must function as described for a C-level callback for
+ {@link CApi#sqlite3_config(ConfigLogCallback)}, with the slight signature change.
+ */
+ void call(int errCode, String msg);
+}
diff --git a/ext/jni/src/org/sqlite/jni/capi/ConfigSqllogCallback.java b/ext/jni/src/org/sqlite/jni/capi/ConfigSqllogCallback.java
new file mode 100644
index 0000000000..df753e6513
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/ConfigSqllogCallback.java
@@ -0,0 +1,25 @@
+/*
+** 2023-08-23
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+
+/**
+ A callback for use with sqlite3_config().
+*/
+public interface ConfigSqllogCallback {
+ /**
+ Must function as described for a C-level callback for
+ {@link CApi#sqlite3_config(ConfigSqllogCallback)}, with the slight signature change.
+ */
+ void call(sqlite3 db, String msg, int msgType );
+}
diff --git a/ext/jni/src/org/sqlite/jni/NativePointerHolder.java b/ext/jni/src/org/sqlite/jni/capi/NativePointerHolder.java
similarity index 68%
rename from ext/jni/src/org/sqlite/jni/NativePointerHolder.java
rename to ext/jni/src/org/sqlite/jni/capi/NativePointerHolder.java
index afe2618a00..e82909e424 100644
--- a/ext/jni/src/org/sqlite/jni/NativePointerHolder.java
+++ b/ext/jni/src/org/sqlite/jni/capi/NativePointerHolder.java
@@ -11,7 +11,7 @@
*************************************************************************
** This file is part of the JNI bindings for the sqlite3 C API.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.capi;
/**
A helper for passing pointers between JNI C code and Java, in
@@ -23,11 +23,24 @@ package org.sqlite.jni;
NativePointerHolder is not inadvertently passed to an incompatible
function signature.
- These objects do not _own_ the pointer they refer to. They are
+ These objects do not own the pointer they refer to. They are
intended simply to communicate that pointer between C and Java.
*/
public class NativePointerHolder {
//! Only set from JNI, where access permissions don't matter.
- private long nativePointer = 0;
+ private volatile long nativePointer = 0;
+ /**
+ For use ONLY by package-level APIs which act as proxies for
+ close/finalize operations. Such ops must call this to zero out
+ the pointer so that this object is not carrying a stale
+ pointer. This function returns the prior value of the pointer and
+ sets it to 0.
+ */
+ final long clearNativePointer() {
+ final long rv = nativePointer;
+ nativePointer= 0;
+ return rv;
+ }
+
public final long getNativePointer(){ return nativePointer; }
}
diff --git a/ext/jni/src/org/sqlite/jni/capi/OutputPointer.java b/ext/jni/src/org/sqlite/jni/capi/OutputPointer.java
new file mode 100644
index 0000000000..60b9025386
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/OutputPointer.java
@@ -0,0 +1,231 @@
+/*
+** 2023-07-21
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+
+/**
+ Helper classes for handling JNI output pointers.
+
+ We do not use a generic OutputPointer because working with those
+ from the native JNI code is unduly quirky due to a lack of
+ autoboxing at that level.
+
+ The usage is similar for all of thes types:
+
+
{@code
+ OutputPointer.sqlite3 out = new OutputPointer.sqlite3();
+ assert( null==out.get() );
+ int rc = sqlite3_open(":memory:", out);
+ if( 0!=rc ) ... error;
+ assert( null!=out.get() );
+ sqlite3 db = out.take();
+ assert( null==out.get() );
+ }
+
+ With the minor exception that the primitive types permit direct
+ access to the object's value via the `value` property, whereas the
+ JNI-level opaque types do not permit client-level code to set that
+ property.
+
+
Warning: do not share instances of these classes across
+ threads. Doing so may lead to corrupting sqlite3-internal state.
+*/
+public final class OutputPointer {
+
+ /**
+ Output pointer for use with routines, such as sqlite3_open(),
+ which return a database handle via an output pointer. These
+ pointers can only be set by the JNI layer, not by client-level
+ code.
+ */
+ public static final class sqlite3 {
+ private org.sqlite.jni.capi.sqlite3 value;
+ /** Initializes with a null value. */
+ public sqlite3(){value = null;}
+ /** Sets the current value to null. */
+ public void clear(){value = null;}
+ /** Returns the current value. */
+ public final org.sqlite.jni.capi.sqlite3 get(){return value;}
+ /** Equivalent to calling get() then clear(). */
+ public final org.sqlite.jni.capi.sqlite3 take(){
+ final org.sqlite.jni.capi.sqlite3 v = value;
+ value = null;
+ return v;
+ }
+ }
+
+ /**
+ Output pointer for sqlite3_blob_open(). These
+ pointers can only be set by the JNI layer, not by client-level
+ code.
+ */
+ public static final class sqlite3_blob {
+ private org.sqlite.jni.capi.sqlite3_blob value;
+ /** Initializes with a null value. */
+ public sqlite3_blob(){value = null;}
+ /** Sets the current value to null. */
+ public void clear(){value = null;}
+ /** Returns the current value. */
+ public final org.sqlite.jni.capi.sqlite3_blob get(){return value;}
+ /** Equivalent to calling get() then clear(). */
+ public final org.sqlite.jni.capi.sqlite3_blob take(){
+ final org.sqlite.jni.capi.sqlite3_blob v = value;
+ value = null;
+ return v;
+ }
+ }
+
+ /**
+ Output pointer for use with routines, such as sqlite3_prepare(),
+ which return a statement handle via an output pointer. These
+ pointers can only be set by the JNI layer, not by client-level
+ code.
+ */
+ public static final class sqlite3_stmt {
+ private org.sqlite.jni.capi.sqlite3_stmt value;
+ /** Initializes with a null value. */
+ public sqlite3_stmt(){value = null;}
+ /** Sets the current value to null. */
+ public void clear(){value = null;}
+ /** Returns the current value. */
+ public final org.sqlite.jni.capi.sqlite3_stmt get(){return value;}
+ /** Equivalent to calling get() then clear(). */
+ public final org.sqlite.jni.capi.sqlite3_stmt take(){
+ final org.sqlite.jni.capi.sqlite3_stmt v = value;
+ value = null;
+ return v;
+ }
+ }
+
+ /**
+ Output pointer for use with routines, such as sqlite3_prepupdate_new(),
+ which return a sqlite3_value handle via an output pointer. These
+ pointers can only be set by the JNI layer, not by client-level
+ code.
+ */
+ public static final class sqlite3_value {
+ private org.sqlite.jni.capi.sqlite3_value value;
+ /** Initializes with a null value. */
+ public sqlite3_value(){value = null;}
+ /** Sets the current value to null. */
+ public void clear(){value = null;}
+ /** Returns the current value. */
+ public final org.sqlite.jni.capi.sqlite3_value get(){return value;}
+ /** Equivalent to calling get() then clear(). */
+ public final org.sqlite.jni.capi.sqlite3_value take(){
+ final org.sqlite.jni.capi.sqlite3_value v = value;
+ value = null;
+ return v;
+ }
+ }
+
+ /**
+ Output pointer for use with native routines which return booleans
+ via integer output pointers.
+ */
+ public static final class Bool {
+ /**
+ This is public for ease of use. Accessors are provided for
+ consistency with the higher-level types.
+ */
+ public boolean value;
+ /** Initializes with the value 0. */
+ public Bool(){this(false);}
+ /** Initializes with the value v. */
+ public Bool(boolean v){value = v;}
+ /** Returns the current value. */
+ public final boolean get(){return value;}
+ /** Sets the current value to v. */
+ public final void set(boolean v){value = v;}
+ }
+
+ /**
+ Output pointer for use with native routines which return integers via
+ output pointers.
+ */
+ public static final class Int32 {
+ /**
+ This is public for ease of use. Accessors are provided for
+ consistency with the higher-level types.
+ */
+ public int value;
+ /** Initializes with the value 0. */
+ public Int32(){this(0);}
+ /** Initializes with the value v. */
+ public Int32(int v){value = v;}
+ /** Returns the current value. */
+ public final int get(){return value;}
+ /** Sets the current value to v. */
+ public final void set(int v){value = v;}
+ }
+
+ /**
+ Output pointer for use with native routines which return 64-bit integers
+ via output pointers.
+ */
+ public static final class Int64 {
+ /**
+ This is public for ease of use. Accessors are provided for
+ consistency with the higher-level types.
+ */
+ public long value;
+ /** Initializes with the value 0. */
+ public Int64(){this(0);}
+ /** Initializes with the value v. */
+ public Int64(long v){value = v;}
+ /** Returns the current value. */
+ public final long get(){return value;}
+ /** Sets the current value. */
+ public final void set(long v){value = v;}
+ }
+
+ /**
+ Output pointer for use with native routines which return strings via
+ output pointers.
+ */
+ public static final class String {
+ /**
+ This is public for ease of use. Accessors are provided for
+ consistency with the higher-level types.
+ */
+ public java.lang.String value;
+ /** Initializes with a null value. */
+ public String(){this(null);}
+ /** Initializes with the value v. */
+ public String(java.lang.String v){value = v;}
+ /** Returns the current value. */
+ public final java.lang.String get(){return value;}
+ /** Sets the current value. */
+ public final void set(java.lang.String v){value = v;}
+ }
+
+ /**
+ Output pointer for use with native routines which return byte
+ arrays via output pointers.
+ */
+ public static final class ByteArray {
+ /**
+ This is public for ease of use. Accessors are provided for
+ consistency with the higher-level types.
+ */
+ public byte[] value;
+ /** Initializes with the value null. */
+ public ByteArray(){this(null);}
+ /** Initializes with the value v. */
+ public ByteArray(byte[] v){value = v;}
+ /** Returns the current value. */
+ public final byte[] get(){return value;}
+ /** Sets the current value. */
+ public final void set(byte[] v){value = v;}
+ }
+}
diff --git a/ext/jni/src/org/sqlite/jni/capi/PrepareMultiCallback.java b/ext/jni/src/org/sqlite/jni/capi/PrepareMultiCallback.java
new file mode 100644
index 0000000000..1c805a9b16
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/PrepareMultiCallback.java
@@ -0,0 +1,78 @@
+/*
+** 2023-09-13
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+
+/**
+ Callback for use with {@link CApi#sqlite3_prepare_multi}.
+*/
+public interface PrepareMultiCallback extends CallbackProxy {
+
+ /**
+ Gets passed a sqlite3_stmt which it may handle in arbitrary ways,
+ transfering ownership of it to this function.
+
+ sqlite3_prepare_multi() will _not_ finalize st - it is up
+ to the call() implementation how st is handled.
+
+ Must return 0 on success or an SQLITE_... code on error.
+
+ See the {@link Finalize} class for a wrapper which finalizes the
+ statement after calling a proxy PrepareMultiCallback.
+ */
+ int call(sqlite3_stmt st);
+
+ /**
+ A PrepareMultiCallback impl which wraps a separate impl and finalizes
+ any sqlite3_stmt passed to its callback.
+ */
+ public static final class Finalize implements PrepareMultiCallback {
+ private PrepareMultiCallback p;
+ /**
+ p is the proxy to call() when this.call() is called.
+ */
+ public Finalize( PrepareMultiCallback p ){
+ this.p = p;
+ }
+ /**
+ Calls the call() method of the proxied callback and either returns its
+ result or propagates an exception. Either way, it passes its argument to
+ sqlite3_finalize() before returning.
+ */
+ @Override public int call(sqlite3_stmt st){
+ try {
+ return this.p.call(st);
+ }finally{
+ CApi.sqlite3_finalize(st);
+ }
+ }
+ }
+
+ /**
+ A PrepareMultiCallback impl which steps entirely through a result set,
+ ignoring all non-error results.
+ */
+ public static final class StepAll implements PrepareMultiCallback {
+ public StepAll(){}
+ /**
+ Calls sqlite3_step() on st until it returns something other than
+ SQLITE_ROW. If the final result is SQLITE_DONE then 0 is returned,
+ else the result of the final step is returned.
+ */
+ @Override public int call(sqlite3_stmt st){
+ int rc = CApi.SQLITE_DONE;
+ while( CApi.SQLITE_ROW == (rc = CApi.sqlite3_step(st)) ){}
+ return CApi.SQLITE_DONE==rc ? 0 : rc;
+ }
+ }
+}
diff --git a/ext/jni/src/org/sqlite/jni/Collation.java b/ext/jni/src/org/sqlite/jni/capi/PreupdateHookCallback.java
similarity index 55%
rename from ext/jni/src/org/sqlite/jni/Collation.java
rename to ext/jni/src/org/sqlite/jni/capi/PreupdateHookCallback.java
index a05b8ef9ef..99d3fb0351 100644
--- a/ext/jni/src/org/sqlite/jni/Collation.java
+++ b/ext/jni/src/org/sqlite/jni/capi/PreupdateHookCallback.java
@@ -1,5 +1,5 @@
/*
-** 2023-07-22
+** 2023-08-25
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
@@ -11,18 +11,16 @@
*************************************************************************
** This file is part of the JNI bindings for the sqlite3 C API.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.capi;
/**
+ Callback for use with {@link CApi#sqlite3_preupdate_hook}.
*/
-public abstract class Collation {
+public interface PreupdateHookCallback extends CallbackProxy {
/**
- Must compare the given byte arrays using memcmp() semantics.
+ Must function as described for the C-level sqlite3_preupdate_hook()
+ callback.
*/
- public abstract int xCompare(byte[] lhs, byte[] rhs);
- /**
- Called by SQLite when the collation is destroyed. If a Collation
- requires custom cleanup, override this method.
- */
- public void xDestroy() {}
+ void call(sqlite3 db, int op, String dbName, String dbTable,
+ long iKey1, long iKey2 );
}
diff --git a/ext/jni/src/org/sqlite/jni/ProgressHandler.java b/ext/jni/src/org/sqlite/jni/capi/ProgressHandlerCallback.java
similarity index 58%
rename from ext/jni/src/org/sqlite/jni/ProgressHandler.java
rename to ext/jni/src/org/sqlite/jni/capi/ProgressHandlerCallback.java
index c806eebca0..464baa2e3d 100644
--- a/ext/jni/src/org/sqlite/jni/ProgressHandler.java
+++ b/ext/jni/src/org/sqlite/jni/capi/ProgressHandlerCallback.java
@@ -1,5 +1,5 @@
/*
-** 2023-07-22
+** 2023-08-25
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
@@ -11,17 +11,17 @@
*************************************************************************
** This file is part of the JNI bindings for the sqlite3 C API.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.capi;
/**
- Callback proxy for use with sqlite3_progress_handler().
+ Callback for use with {@link CApi#sqlite3_progress_handler}.
*/
-public interface ProgressHandler {
+public interface ProgressHandlerCallback extends CallbackProxy {
/**
- Works as documented for the sqlite3_progress_handler() callback.
+ Works as documented for the C-level sqlite3_progress_handler() callback.
- If it throws, the exception message is passed on to the db and
+
If it throws, the exception message is passed on to the db and
the exception is suppressed.
*/
- int xCallback();
+ int call();
}
diff --git a/ext/jni/src/org/sqlite/jni/capi/ResultCode.java b/ext/jni/src/org/sqlite/jni/capi/ResultCode.java
new file mode 100644
index 0000000000..5a8b2e6a18
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/ResultCode.java
@@ -0,0 +1,155 @@
+/*
+** 2023-07-21
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+
+/**
+ This enum contains all of the core and "extended" result codes used
+ by the sqlite3 library. It is provided not for use with the C-style
+ API (with which it won't work) but for higher-level code which may
+ find it useful to map SQLite result codes to human-readable names.
+*/
+public enum ResultCode {
+ SQLITE_OK(CApi.SQLITE_OK),
+ SQLITE_ERROR(CApi.SQLITE_ERROR),
+ SQLITE_INTERNAL(CApi.SQLITE_INTERNAL),
+ SQLITE_PERM(CApi.SQLITE_PERM),
+ SQLITE_ABORT(CApi.SQLITE_ABORT),
+ SQLITE_BUSY(CApi.SQLITE_BUSY),
+ SQLITE_LOCKED(CApi.SQLITE_LOCKED),
+ SQLITE_NOMEM(CApi.SQLITE_NOMEM),
+ SQLITE_READONLY(CApi.SQLITE_READONLY),
+ SQLITE_INTERRUPT(CApi.SQLITE_INTERRUPT),
+ SQLITE_IOERR(CApi.SQLITE_IOERR),
+ SQLITE_CORRUPT(CApi.SQLITE_CORRUPT),
+ SQLITE_NOTFOUND(CApi.SQLITE_NOTFOUND),
+ SQLITE_FULL(CApi.SQLITE_FULL),
+ SQLITE_CANTOPEN(CApi.SQLITE_CANTOPEN),
+ SQLITE_PROTOCOL(CApi.SQLITE_PROTOCOL),
+ SQLITE_EMPTY(CApi.SQLITE_EMPTY),
+ SQLITE_SCHEMA(CApi.SQLITE_SCHEMA),
+ SQLITE_TOOBIG(CApi.SQLITE_TOOBIG),
+ SQLITE_CONSTRAINT(CApi.SQLITE_CONSTRAINT),
+ SQLITE_MISMATCH(CApi.SQLITE_MISMATCH),
+ SQLITE_MISUSE(CApi.SQLITE_MISUSE),
+ SQLITE_NOLFS(CApi.SQLITE_NOLFS),
+ SQLITE_AUTH(CApi.SQLITE_AUTH),
+ SQLITE_FORMAT(CApi.SQLITE_FORMAT),
+ SQLITE_RANGE(CApi.SQLITE_RANGE),
+ SQLITE_NOTADB(CApi.SQLITE_NOTADB),
+ SQLITE_NOTICE(CApi.SQLITE_NOTICE),
+ SQLITE_WARNING(CApi.SQLITE_WARNING),
+ SQLITE_ROW(CApi.SQLITE_ROW),
+ SQLITE_DONE(CApi.SQLITE_DONE),
+ SQLITE_ERROR_MISSING_COLLSEQ(CApi.SQLITE_ERROR_MISSING_COLLSEQ),
+ SQLITE_ERROR_RETRY(CApi.SQLITE_ERROR_RETRY),
+ SQLITE_ERROR_SNAPSHOT(CApi.SQLITE_ERROR_SNAPSHOT),
+ SQLITE_IOERR_READ(CApi.SQLITE_IOERR_READ),
+ SQLITE_IOERR_SHORT_READ(CApi.SQLITE_IOERR_SHORT_READ),
+ SQLITE_IOERR_WRITE(CApi.SQLITE_IOERR_WRITE),
+ SQLITE_IOERR_FSYNC(CApi.SQLITE_IOERR_FSYNC),
+ SQLITE_IOERR_DIR_FSYNC(CApi.SQLITE_IOERR_DIR_FSYNC),
+ SQLITE_IOERR_TRUNCATE(CApi.SQLITE_IOERR_TRUNCATE),
+ SQLITE_IOERR_FSTAT(CApi.SQLITE_IOERR_FSTAT),
+ SQLITE_IOERR_UNLOCK(CApi.SQLITE_IOERR_UNLOCK),
+ SQLITE_IOERR_RDLOCK(CApi.SQLITE_IOERR_RDLOCK),
+ SQLITE_IOERR_DELETE(CApi.SQLITE_IOERR_DELETE),
+ SQLITE_IOERR_BLOCKED(CApi.SQLITE_IOERR_BLOCKED),
+ SQLITE_IOERR_NOMEM(CApi.SQLITE_IOERR_NOMEM),
+ SQLITE_IOERR_ACCESS(CApi.SQLITE_IOERR_ACCESS),
+ SQLITE_IOERR_CHECKRESERVEDLOCK(CApi.SQLITE_IOERR_CHECKRESERVEDLOCK),
+ SQLITE_IOERR_LOCK(CApi.SQLITE_IOERR_LOCK),
+ SQLITE_IOERR_CLOSE(CApi.SQLITE_IOERR_CLOSE),
+ SQLITE_IOERR_DIR_CLOSE(CApi.SQLITE_IOERR_DIR_CLOSE),
+ SQLITE_IOERR_SHMOPEN(CApi.SQLITE_IOERR_SHMOPEN),
+ SQLITE_IOERR_SHMSIZE(CApi.SQLITE_IOERR_SHMSIZE),
+ SQLITE_IOERR_SHMLOCK(CApi.SQLITE_IOERR_SHMLOCK),
+ SQLITE_IOERR_SHMMAP(CApi.SQLITE_IOERR_SHMMAP),
+ SQLITE_IOERR_SEEK(CApi.SQLITE_IOERR_SEEK),
+ SQLITE_IOERR_DELETE_NOENT(CApi.SQLITE_IOERR_DELETE_NOENT),
+ SQLITE_IOERR_MMAP(CApi.SQLITE_IOERR_MMAP),
+ SQLITE_IOERR_GETTEMPPATH(CApi.SQLITE_IOERR_GETTEMPPATH),
+ SQLITE_IOERR_CONVPATH(CApi.SQLITE_IOERR_CONVPATH),
+ SQLITE_IOERR_VNODE(CApi.SQLITE_IOERR_VNODE),
+ SQLITE_IOERR_AUTH(CApi.SQLITE_IOERR_AUTH),
+ SQLITE_IOERR_BEGIN_ATOMIC(CApi.SQLITE_IOERR_BEGIN_ATOMIC),
+ SQLITE_IOERR_COMMIT_ATOMIC(CApi.SQLITE_IOERR_COMMIT_ATOMIC),
+ SQLITE_IOERR_ROLLBACK_ATOMIC(CApi.SQLITE_IOERR_ROLLBACK_ATOMIC),
+ SQLITE_IOERR_DATA(CApi.SQLITE_IOERR_DATA),
+ SQLITE_IOERR_CORRUPTFS(CApi.SQLITE_IOERR_CORRUPTFS),
+ SQLITE_LOCKED_SHAREDCACHE(CApi.SQLITE_LOCKED_SHAREDCACHE),
+ SQLITE_LOCKED_VTAB(CApi.SQLITE_LOCKED_VTAB),
+ SQLITE_BUSY_RECOVERY(CApi.SQLITE_BUSY_RECOVERY),
+ SQLITE_BUSY_SNAPSHOT(CApi.SQLITE_BUSY_SNAPSHOT),
+ SQLITE_BUSY_TIMEOUT(CApi.SQLITE_BUSY_TIMEOUT),
+ SQLITE_CANTOPEN_NOTEMPDIR(CApi.SQLITE_CANTOPEN_NOTEMPDIR),
+ SQLITE_CANTOPEN_ISDIR(CApi.SQLITE_CANTOPEN_ISDIR),
+ SQLITE_CANTOPEN_FULLPATH(CApi.SQLITE_CANTOPEN_FULLPATH),
+ SQLITE_CANTOPEN_CONVPATH(CApi.SQLITE_CANTOPEN_CONVPATH),
+ SQLITE_CANTOPEN_SYMLINK(CApi.SQLITE_CANTOPEN_SYMLINK),
+ SQLITE_CORRUPT_VTAB(CApi.SQLITE_CORRUPT_VTAB),
+ SQLITE_CORRUPT_SEQUENCE(CApi.SQLITE_CORRUPT_SEQUENCE),
+ SQLITE_CORRUPT_INDEX(CApi.SQLITE_CORRUPT_INDEX),
+ SQLITE_READONLY_RECOVERY(CApi.SQLITE_READONLY_RECOVERY),
+ SQLITE_READONLY_CANTLOCK(CApi.SQLITE_READONLY_CANTLOCK),
+ SQLITE_READONLY_ROLLBACK(CApi.SQLITE_READONLY_ROLLBACK),
+ SQLITE_READONLY_DBMOVED(CApi.SQLITE_READONLY_DBMOVED),
+ SQLITE_READONLY_CANTINIT(CApi.SQLITE_READONLY_CANTINIT),
+ SQLITE_READONLY_DIRECTORY(CApi.SQLITE_READONLY_DIRECTORY),
+ SQLITE_ABORT_ROLLBACK(CApi.SQLITE_ABORT_ROLLBACK),
+ SQLITE_CONSTRAINT_CHECK(CApi.SQLITE_CONSTRAINT_CHECK),
+ SQLITE_CONSTRAINT_COMMITHOOK(CApi.SQLITE_CONSTRAINT_COMMITHOOK),
+ SQLITE_CONSTRAINT_FOREIGNKEY(CApi.SQLITE_CONSTRAINT_FOREIGNKEY),
+ SQLITE_CONSTRAINT_FUNCTION(CApi.SQLITE_CONSTRAINT_FUNCTION),
+ SQLITE_CONSTRAINT_NOTNULL(CApi.SQLITE_CONSTRAINT_NOTNULL),
+ SQLITE_CONSTRAINT_PRIMARYKEY(CApi.SQLITE_CONSTRAINT_PRIMARYKEY),
+ SQLITE_CONSTRAINT_TRIGGER(CApi.SQLITE_CONSTRAINT_TRIGGER),
+ SQLITE_CONSTRAINT_UNIQUE(CApi.SQLITE_CONSTRAINT_UNIQUE),
+ SQLITE_CONSTRAINT_VTAB(CApi.SQLITE_CONSTRAINT_VTAB),
+ SQLITE_CONSTRAINT_ROWID(CApi.SQLITE_CONSTRAINT_ROWID),
+ SQLITE_CONSTRAINT_PINNED(CApi.SQLITE_CONSTRAINT_PINNED),
+ SQLITE_CONSTRAINT_DATATYPE(CApi.SQLITE_CONSTRAINT_DATATYPE),
+ SQLITE_NOTICE_RECOVER_WAL(CApi.SQLITE_NOTICE_RECOVER_WAL),
+ SQLITE_NOTICE_RECOVER_ROLLBACK(CApi.SQLITE_NOTICE_RECOVER_ROLLBACK),
+ SQLITE_WARNING_AUTOINDEX(CApi.SQLITE_WARNING_AUTOINDEX),
+ SQLITE_AUTH_USER(CApi.SQLITE_AUTH_USER),
+ SQLITE_OK_LOAD_PERMANENTLY(CApi.SQLITE_OK_LOAD_PERMANENTLY);
+
+ public final int value;
+
+ ResultCode(int rc){
+ value = rc;
+ ResultCodeMap.set(rc, this);
+ }
+
+ /**
+ Returns the entry from this enum for the given result code, or
+ null if no match is found.
+ */
+ public static ResultCode getEntryForInt(int rc){
+ return ResultCodeMap.get(rc);
+ }
+
+ /**
+ Internal level of indirection required because we cannot initialize
+ static enum members in an enum before the enum constructor is
+ invoked.
+ */
+ private static final class ResultCodeMap {
+ private static final java.util.Map i2e
+ = new java.util.HashMap<>();
+ private static void set(int rc, ResultCode e){ i2e.put(rc, e); }
+ private static ResultCode get(int rc){ return i2e.get(rc); }
+ }
+
+}
diff --git a/ext/jni/src/org/sqlite/jni/RollbackHook.java b/ext/jni/src/org/sqlite/jni/capi/RollbackHookCallback.java
similarity index 62%
rename from ext/jni/src/org/sqlite/jni/RollbackHook.java
rename to ext/jni/src/org/sqlite/jni/capi/RollbackHookCallback.java
index 4ce3cb93e3..5ce17e718a 100644
--- a/ext/jni/src/org/sqlite/jni/RollbackHook.java
+++ b/ext/jni/src/org/sqlite/jni/capi/RollbackHookCallback.java
@@ -1,5 +1,5 @@
/*
-** 2023-07-22
+** 2023-08-25
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
@@ -11,15 +11,15 @@
*************************************************************************
** This file is part of the JNI bindings for the sqlite3 C API.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.capi;
/**
- Callback proxy for use with sqlite3_rollback_hook().
+ Callback for use with {@link CApi#sqlite3_rollback_hook}.
*/
-public interface RollbackHook {
+public interface RollbackHookCallback extends CallbackProxy {
/**
- Works as documented for the sqlite3_rollback_hook() callback.
- Must not throw.
+ Works as documented for the C-level sqlite3_rollback_hook()
+ callback.
*/
- void xRollbackHook();
+ void call();
}
diff --git a/ext/jni/src/org/sqlite/jni/capi/SQLFunction.java b/ext/jni/src/org/sqlite/jni/capi/SQLFunction.java
new file mode 100644
index 0000000000..4806e2fc0c
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/SQLFunction.java
@@ -0,0 +1,103 @@
+/*
+** 2023-07-22
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+
+/**
+ SQLFunction is used in conjunction with the
+ sqlite3_create_function() JNI-bound API to give that native code
+ access to the callback functions needed in order to implement SQL
+ functions in Java.
+
+
+
+ This class is not used by itself, but is a marker base class. The
+ three UDF types are modelled by the inner classes Scalar,
+ Aggregate, and Window. Most simply, clients may subclass
+ those, or create anonymous classes from them, to implement
+ UDFs. Clients are free to create their own classes for use with
+ UDFs, so long as they conform to the public interfaces defined by
+ those three classes. The JNI layer only actively relies on the
+ SQLFunction base class and the method names and signatures used by
+ the UDF callback interfaces.
+*/
+public interface SQLFunction {
+
+ /**
+ PerContextState assists aggregate and window functions in
+ managing their accumulator state across calls to the UDF's
+ callbacks.
+
+ T must be of a type which can be legally stored as a value in
+ java.util.HashMap.
+
+ If a given aggregate or window function is called multiple times
+ in a single SQL statement, e.g. SELECT MYFUNC(A), MYFUNC(B)...,
+ then the clients need some way of knowing which call is which so
+ that they can map their state between their various UDF callbacks
+ and reset it via xFinal(). This class takes care of such
+ mappings.
+
+
This class works by mapping
+ sqlite3_context.getAggregateContext() to a single piece of
+ state, of a client-defined type (the T part of this class), which
+ persists across a "matching set" of the UDF's callbacks.
+
+
This class is a helper providing commonly-needed functionality
+ - it is not required for use with aggregate or window functions.
+ Client UDFs are free to perform such mappings using custom
+ approaches. The provided {@link AggregateFunction} and {@link
+ WindowFunction} classes use this.
+ */
+ public static final class PerContextState {
+ private final java.util.Map> map
+ = new java.util.HashMap<>();
+
+ /**
+ Should be called from a UDF's xStep(), xValue(), and xInverse()
+ methods, passing it that method's first argument and an initial
+ value for the persistent state. If there is currently no
+ mapping for the given context within the map, one is created
+ using the given initial value, else the existing one is used
+ and the 2nd argument is ignored. It returns a ValueHolder
+ which can be used to modify that state directly without
+ requiring that the client update the underlying map's entry.
+
+ The caller is obligated to eventually call
+ takeAggregateState() to clear the mapping.
+ */
+ public ValueHolder getAggregateState(sqlite3_context cx, T initialValue){
+ final Long key = cx.getAggregateContext(true);
+ ValueHolder rc = null==key ? null : map.get(key);
+ if( null==rc ){
+ map.put(key, rc = new ValueHolder<>(initialValue));
+ }
+ return rc;
+ }
+
+ /**
+ Should be called from a UDF's xFinal() method and passed that
+ method's first argument. This function removes the value
+ associated with cx.getAggregateContext() from the map and
+ returns it, returning null if no other UDF method has been
+ called to set up such a mapping. The latter condition will be
+ the case if a UDF is used in a statement which has no result
+ rows.
+ */
+ public T takeAggregateState(sqlite3_context cx){
+ final ValueHolder h = map.remove(cx.getAggregateContext(false));
+ return null==h ? null : h.value;
+ }
+ }
+
+}
diff --git a/ext/jni/src/org/sqlite/jni/tester/SQLTester.java b/ext/jni/src/org/sqlite/jni/capi/SQLTester.java
similarity index 93%
rename from ext/jni/src/org/sqlite/jni/tester/SQLTester.java
rename to ext/jni/src/org/sqlite/jni/capi/SQLTester.java
index ffdb867d9b..81d6106be7 100644
--- a/ext/jni/src/org/sqlite/jni/tester/SQLTester.java
+++ b/ext/jni/src/org/sqlite/jni/capi/SQLTester.java
@@ -12,16 +12,13 @@
** This file contains the main application entry pointer for the
** SQLTester framework.
*/
-package org.sqlite.jni.tester;
+package org.sqlite.jni.capi;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.nio.charset.StandardCharsets;
import java.util.regex.*;
-import org.sqlite.jni.*;
-import static org.sqlite.jni.SQLite3Jni.*;
-import org.sqlite.jni.sqlite3;
-
+import static org.sqlite.jni.capi.CApi.*;
/**
Modes for how to escape (or not) column values and names from
@@ -150,14 +147,17 @@ class Outer {
}
/**
- This class provides an application which aims to implement the
+ This class provides an application which aims to implement the
rudimentary SQL-driven test tool described in the accompanying
- test-script-interpreter.md.
+ {@code test-script-interpreter.md}.
- This is a work in progress.
+
This class is an internal testing tool, not part of the public
+ interface but is (A) in the same package as the library because
+ access permissions require it to be so and (B) the JDK8 javadoc
+ offers no way to filter individual classes out of the doc
+ generation process (it can only exclude packages, but see (A)).
-
- An instance of this application provides a core set of services
+
An instance of this application provides a core set of services
which TestScript instances use for processing testing logic.
TestScripts, in turn, delegate the concrete test work to Command
objects, which the TestScript parses on their behalf.
@@ -181,7 +181,7 @@ public class SQLTester {
private int nTestFile = 0;
//! Number of scripts which were aborted.
private int nAbortedScript = 0;
- //! Per-script test counter.
+ //! Incremented by test case handlers
private int nTest = 0;
//! True to enable column name output from execSql()
private boolean emitColNames;
@@ -250,14 +250,14 @@ public class SQLTester {
}
public void runTests() throws Exception {
- final long tStart = System.nanoTime();
+ final long tStart = System.currentTimeMillis();
for(String f : listInFiles){
reset();
++nTestFile;
final TestScript ts = new TestScript(f);
outln(nextStartEmoji(), " starting [",f,"]");
boolean threw = false;
- final long timeStart = System.nanoTime();
+ final long timeStart = System.currentTimeMillis();
try{
ts.run(this);
}catch(SQLTesterException e){
@@ -267,14 +267,13 @@ public class SQLTester {
if( keepGoing ) outln("Continuing anyway becaure of the keep-going option.");
else if( e.isFatal() ) throw e;
}finally{
- final long timeEnd = System.nanoTime();
+ final long timeEnd = System.currentTimeMillis();
outln("🏁",(threw ? "❌" : "✅")," ",nTest," test(s) in ",
- ((timeEnd-timeStart)/1000000.0),"ms.");
- //ts.getFilename());
+ (timeEnd-timeStart),"ms.");
}
}
- final long tEnd = System.nanoTime();
- outln("Total run-time: ",((tEnd-tStart)/1000000.0),"ms");
+ final long tEnd = System.currentTimeMillis();
+ outln("Total run-time: ",(tEnd-tStart),"ms");
Util.unlink(initialDbName);
}
@@ -336,7 +335,9 @@ public class SQLTester {
}
sqlite3 setCurrentDb(int n) throws Exception{
- return affirmDbId(n).aDb[n];
+ affirmDbId(n);
+ iCurrentDb = n;
+ return this.aDb[n];
}
sqlite3 getCurrentDb(){ return aDb[iCurrentDb]; }
@@ -399,7 +400,7 @@ public class SQLTester {
nullView = "nil";
emitColNames = false;
iCurrentDb = 0;
- dbInitSql.append("SELECT 1;");
+ //dbInitSql.append("SELECT 1;");
}
void setNullValue(String v){nullView = v;}
@@ -456,7 +457,7 @@ public class SQLTester {
}
private void appendDbErr(sqlite3 db, StringBuilder sb, int rc){
- sb.append(org.sqlite.jni.ResultCode.getEntryForInt(rc)).append(' ');
+ sb.append(org.sqlite.jni.capi.ResultCode.getEntryForInt(rc)).append(' ');
final String msg = escapeSqlValue(sqlite3_errmsg(db));
if( '{' == msg.charAt(0) ){
sb.append(msg);
@@ -474,12 +475,12 @@ public class SQLTester {
the db's result code.
appendMode specifies how/whether to append results to the result
- buffer. lineMode specifies whether to output all results in a
+ buffer. rowMode specifies whether to output all results in a
single line or one line per row. If appendMode is
- ResultBufferMode.NONE then lineMode is ignored and may be null.
+ ResultBufferMode.NONE then rowMode is ignored and may be null.
*/
public int execSql(sqlite3 db, boolean throwOnError,
- ResultBufferMode appendMode, ResultRowMode lineMode,
+ ResultBufferMode appendMode, ResultRowMode rowMode,
String sql) throws SQLTesterException {
if( null==db && null==aDb[0] ){
// Delay opening of the initial db to enable tests to change its
@@ -561,7 +562,7 @@ public class SQLTester {
throw new SQLTesterException("Unhandled ResultBufferMode: "+appendMode);
}
}
- if( ResultRowMode.NEWLINE == lineMode ){
+ if( ResultRowMode.NEWLINE == rowMode ){
spacing = 0;
sb.append('\n');
}
@@ -580,6 +581,10 @@ public class SQLTester {
}
}
}finally{
+ sqlite3_reset(stmt
+ /* In order to trigger an exception in the
+ INSERT...RETURNING locking scenario:
+ https://sqlite.org/forum/forumpost/36f7a2e7494897df */);
sqlite3_finalize(stmt);
}
if( 0!=rc && throwOnError ){
@@ -609,9 +614,9 @@ public class SQLTester {
}
t.addTestScript(a);
}
- final AutoExtension ax = new AutoExtension() {
+ final AutoExtensionCallback ax = new AutoExtensionCallback() {
private final SQLTester tester = t;
- public int xEntryPoint(sqlite3 db){
+ @Override public int call(sqlite3 db){
final String init = tester.getDbInitSql();
if( !init.isEmpty() ){
tester.execSql(db, true, ResultBufferMode.NONE, null, init);
@@ -629,7 +634,7 @@ public class SQLTester {
t.outln("Aborted ",t.nAbortedScript," script(s).");
}
if( dumpInternals ){
- sqlite3_do_something_for_developer();
+ sqlite3_jni_internal_details();
}
}
}
@@ -663,7 +668,7 @@ public class SQLTester {
static {
System.loadLibrary("sqlite3-jni")
/* Interestingly, when SQLTester is the main app, we have to
- load that lib from here. The same load from SQLite3Jni does
+ load that lib from here. The same load from CApi does
not happen early enough. Without this,
installCustomExtensions() is an unresolved symbol. */;
}
@@ -924,8 +929,8 @@ class RunCommand extends Command {
final sqlite3 db = (1==argv.length)
? t.getCurrentDb() : t.getDbById( Integer.parseInt(argv[1]) );
final String sql = t.takeInputBuffer();
- int rc = t.execSql(db, false, ResultBufferMode.NONE,
- ResultRowMode.ONELINE, sql);
+ final int rc = t.execSql(db, false, ResultBufferMode.NONE,
+ ResultRowMode.ONELINE, sql);
if( 0!=rc && t.isVerbose() ){
String msg = sqlite3_errmsg(db);
ts.verbose1(argv[0]," non-fatal command error #",rc,": ",
@@ -948,8 +953,7 @@ class TableResultCommand extends Command {
if( !body.endsWith("\n--end") ){
ts.toss(argv[0], " must be terminated with --end.");
}else{
- int n = body.length();
- body = body.substring(0, n-6);
+ body = body.substring(0, body.length()-6);
}
final String[] globs = body.split("\\s*\\n\\s*");
if( globs.length < 1 ){
@@ -1124,8 +1128,9 @@ class TestScript {
}
public String getOutputPrefix(){
- String rc = "["+(moduleName==null ? filename : moduleName)+"]";
+ String rc = "["+(moduleName==null ? "" : moduleName)+"]";
if( null!=testCaseName ) rc += "["+testCaseName+"]";
+ if( null!=filename ) rc += "["+filename+"]";
return rc + " line "+ cur.lineNo;
}
@@ -1238,14 +1243,15 @@ class TestScript {
final int oldPB = cur.putbackPos;
final int oldPBL = cur.putbackLineNo;
final int oldLine = cur.lineNo;
- final String rc = getLine();
- cur.peekedPos = cur.pos;
- cur.peekedLineNo = cur.lineNo;
- cur.pos = oldPos;
- cur.lineNo = oldLine;
- cur.putbackPos = oldPB;
- cur.putbackLineNo = oldPBL;
- return rc;
+ try{ return getLine(); }
+ finally{
+ cur.peekedPos = cur.pos;
+ cur.peekedLineNo = cur.lineNo;
+ cur.pos = oldPos;
+ cur.lineNo = oldLine;
+ cur.putbackPos = oldPB;
+ cur.putbackLineNo = oldPBL;
+ }
}
/**
@@ -1268,6 +1274,7 @@ class TestScript {
}
private boolean checkRequiredProperties(SQLTester t, String[] props) throws SQLTesterException{
+ if( true ) return false;
int nOk = 0;
for(String rp : props){
verbose1("REQUIRED_PROPERTIES: ",rp);
@@ -1288,6 +1295,12 @@ class TestScript {
t.appendDbInitSql("pragma temp_store=0;");
++nOk;
break;
+ case "AUTOVACUUM":
+ t.appendDbInitSql("pragma auto_vacuum=full;");
+ ++nOk;
+ case "INCRVACUUM":
+ t.appendDbInitSql("pragma auto_vacuum=incremental;");
+ ++nOk;
default:
break;
}
@@ -1326,9 +1339,9 @@ class TestScript {
m = patternRequiredProperties.matcher(line);
if( m.find() ){
final String rp = m.group(1);
- //if( ! checkRequiredProperties( tester, rp.split("\\s+") ) ){
- throw new IncompatibleDirective(this, "REQUIRED_PROPERTIES: "+rp);
- //}
+ if( ! checkRequiredProperties( tester, rp.split("\\s+") ) ){
+ throw new IncompatibleDirective(this, "REQUIRED_PROPERTIES: "+rp);
+ }
}
m = patternMixedModuleName.matcher(line);
if( m.find() ){
@@ -1372,11 +1385,10 @@ class TestScript {
String line;
while( (null != (line = peekLine())) ){
checkForDirective(tester, line);
- if( !isCommandLine(line, true) ){
+ if( isCommandLine(line, true) ) break;
+ else {
sb.append(line).append("\n");
consumePeeked();
- }else{
- break;
}
}
line = sb.toString();
diff --git a/ext/jni/src/org/sqlite/jni/capi/ScalarFunction.java b/ext/jni/src/org/sqlite/jni/capi/ScalarFunction.java
new file mode 100644
index 0000000000..95541bdcba
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/ScalarFunction.java
@@ -0,0 +1,33 @@
+/*
+** 2023-08-25
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+
+
+/**
+ A SQLFunction implementation for scalar functions.
+*/
+public abstract class ScalarFunction implements SQLFunction {
+ /**
+ As for the xFunc() argument of the C API's
+ sqlite3_create_function(). If this function throws, it is
+ translated into an sqlite3_result_error().
+ */
+ public abstract void xFunc(sqlite3_context cx, sqlite3_value[] args);
+
+ /**
+ Optionally override to be notified when the UDF is finalized by
+ SQLite. This default implementation does nothing.
+ */
+ public void xDestroy() {}
+}
diff --git a/ext/jni/src/org/sqlite/jni/capi/TableColumnMetadata.java b/ext/jni/src/org/sqlite/jni/capi/TableColumnMetadata.java
new file mode 100644
index 0000000000..d8b6226ac9
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/TableColumnMetadata.java
@@ -0,0 +1,35 @@
+/*
+** 2023-07-21
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+
+/**
+ A wrapper object for use with sqlite3_table_column_metadata().
+ They are populated only via that interface.
+*/
+public final class TableColumnMetadata {
+ OutputPointer.Bool pNotNull = new OutputPointer.Bool();
+ OutputPointer.Bool pPrimaryKey = new OutputPointer.Bool();
+ OutputPointer.Bool pAutoinc = new OutputPointer.Bool();
+ OutputPointer.String pzCollSeq = new OutputPointer.String();
+ OutputPointer.String pzDataType = new OutputPointer.String();
+
+ public TableColumnMetadata(){
+ }
+
+ public String getDataType(){ return pzDataType.value; }
+ public String getCollation(){ return pzCollSeq.value; }
+ public boolean isNotNull(){ return pNotNull.value; }
+ public boolean isPrimaryKey(){ return pPrimaryKey.value; }
+ public boolean isAutoincrement(){ return pAutoinc.value; }
+}
diff --git a/ext/jni/src/org/sqlite/jni/capi/Tester1.java b/ext/jni/src/org/sqlite/jni/capi/Tester1.java
new file mode 100644
index 0000000000..6fb28e65b9
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/Tester1.java
@@ -0,0 +1,1976 @@
+/*
+** 2023-07-21
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file contains a set of tests for the sqlite3 JNI bindings.
+*/
+package org.sqlite.jni.capi;
+import static org.sqlite.jni.capi.CApi.*;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+/**
+ An annotation for Tester1 tests which we do not want to run in
+ reflection-driven test mode because either they are not suitable
+ for multi-threaded threaded mode or we have to control their execution
+ order.
+*/
+@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
+@java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD})
+@interface ManualTest{}
+/**
+ Annotation for Tester1 tests which mark those which must be skipped
+ in multi-threaded mode.
+*/
+@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
+@java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD})
+@interface SingleThreadOnly{}
+
+public class Tester1 implements Runnable {
+ //! True when running in multi-threaded mode.
+ private static boolean mtMode = false;
+ //! True to sleep briefly between tests.
+ private static boolean takeNaps = false;
+ //! True to shuffle the order of the tests.
+ private static boolean shuffle = false;
+ //! True to dump the list of to-run tests to stdout.
+ private static boolean listRunTests = false;
+ //! True to squelch all out() and outln() output.
+ private static boolean quietMode = false;
+ //! Total number of runTests() calls.
+ private static int nTestRuns = 0;
+ //! List of test*() methods to run.
+ private static List testMethods = null;
+ //! List of exceptions collected by run()
+ private static List listErrors = new ArrayList<>();
+ private static final class Metrics {
+ //! Number of times createNewDb() (or equivalent) is invoked.
+ volatile int dbOpen = 0;
+ }
+
+ private Integer tId;
+
+ Tester1(Integer id){
+ tId = id;
+ }
+
+ static final Metrics metrics = new Metrics();
+
+ public static synchronized void outln(){
+ if( !quietMode ){
+ System.out.println("");
+ }
+ }
+
+ public static synchronized void outPrefix(){
+ if( !quietMode ){
+ System.out.print(Thread.currentThread().getName()+": ");
+ }
+ }
+
+ public static synchronized void outln(Object val){
+ if( !quietMode ){
+ outPrefix();
+ System.out.println(val);
+ }
+ }
+
+ public static synchronized void out(Object val){
+ if( !quietMode ){
+ System.out.print(val);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public static synchronized void out(Object... vals){
+ if( !quietMode ){
+ outPrefix();
+ for(Object v : vals) out(v);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public static synchronized void outln(Object... vals){
+ if( !quietMode ){
+ out(vals); out("\n");
+ }
+ }
+
+ static volatile int affirmCount = 0;
+ public static synchronized int affirm(Boolean v, String comment){
+ ++affirmCount;
+ if( false ) assert( v /* prefer assert over exception if it's enabled because
+ the JNI layer sometimes has to suppress exceptions,
+ so they might be squelched on their way back to the
+ top. */);
+ if( !v ) throw new RuntimeException(comment);
+ return affirmCount;
+ }
+
+ public static void affirm(Boolean v){
+ affirm(v, "Affirmation failed.");
+ }
+
+ @SingleThreadOnly /* because it's thread-agnostic */
+ private void test1(){
+ affirm(sqlite3_libversion_number() == SQLITE_VERSION_NUMBER);
+ }
+
+ public static sqlite3 createNewDb(){
+ final OutputPointer.sqlite3 out = new OutputPointer.sqlite3();
+ int rc = sqlite3_open(":memory:", out);
+ ++metrics.dbOpen;
+ sqlite3 db = out.take();
+ if( 0!=rc ){
+ final String msg =
+ null==db ? sqlite3_errstr(rc) : sqlite3_errmsg(db);
+ sqlite3_close(db);
+ throw new RuntimeException("Opening db failed: "+msg);
+ }
+ affirm( null == out.get() );
+ affirm( 0 != db.getNativePointer() );
+ rc = sqlite3_busy_timeout(db, 2000);
+ affirm( 0 == rc );
+ return db;
+ }
+
+ public static void execSql(sqlite3 db, String[] sql){
+ execSql(db, String.join("", sql));
+ }
+
+ public static int execSql(sqlite3 db, boolean throwOnError, String sql){
+ OutputPointer.Int32 oTail = new OutputPointer.Int32();
+ final byte[] sqlUtf8 = sql.getBytes(StandardCharsets.UTF_8);
+ int pos = 0, n = 1;
+ byte[] sqlChunk = sqlUtf8;
+ int rc = 0;
+ sqlite3_stmt stmt = null;
+ final OutputPointer.sqlite3_stmt outStmt = new OutputPointer.sqlite3_stmt();
+ while(pos < sqlChunk.length){
+ if(pos > 0){
+ sqlChunk = Arrays.copyOfRange(sqlChunk, pos,
+ sqlChunk.length);
+ }
+ if( 0==sqlChunk.length ) break;
+ rc = sqlite3_prepare_v2(db, sqlChunk, outStmt, oTail);
+ if(throwOnError) affirm(0 == rc);
+ else if( 0!=rc ) break;
+ pos = oTail.value;
+ stmt = outStmt.take();
+ if( null == stmt ){
+ // empty statement was parsed.
+ continue;
+ }
+ affirm(0 != stmt.getNativePointer());
+ while( SQLITE_ROW == (rc = sqlite3_step(stmt)) ){
+ }
+ sqlite3_finalize(stmt);
+ affirm(0 == stmt.getNativePointer());
+ if(0!=rc && SQLITE_ROW!=rc && SQLITE_DONE!=rc){
+ break;
+ }
+ }
+ sqlite3_finalize(stmt);
+ if(SQLITE_ROW==rc || SQLITE_DONE==rc) rc = 0;
+ if( 0!=rc && throwOnError){
+ throw new RuntimeException("db op failed with rc="
+ +rc+": "+sqlite3_errmsg(db));
+ }
+ return rc;
+ }
+
+ public static void execSql(sqlite3 db, String sql){
+ execSql(db, true, sql);
+ }
+
+ public static sqlite3_stmt prepare(sqlite3 db, boolean throwOnError, String sql){
+ final OutputPointer.sqlite3_stmt outStmt = new OutputPointer.sqlite3_stmt();
+ int rc = sqlite3_prepare_v2(db, sql, outStmt);
+ if( throwOnError ){
+ affirm( 0 == rc );
+ }
+ final sqlite3_stmt rv = outStmt.take();
+ affirm( null == outStmt.get() );
+ if( throwOnError ){
+ affirm( 0 != rv.getNativePointer() );
+ }
+ return rv;
+ }
+
+ public static sqlite3_stmt prepare(sqlite3 db, String sql){
+ return prepare(db, true, sql);
+ }
+
+ private void showCompileOption(){
+ int i = 0;
+ String optName;
+ outln("compile options:");
+ for( ; null != (optName = sqlite3_compileoption_get(i)); ++i){
+ outln("\t"+optName+"\t (used="+
+ sqlite3_compileoption_used(optName)+")");
+ }
+ }
+
+ private void testCompileOption(){
+ int i = 0;
+ String optName;
+ for( ; null != (optName = sqlite3_compileoption_get(i)); ++i){
+ }
+ affirm( i > 10 );
+ affirm( null==sqlite3_compileoption_get(-1) );
+ }
+
+ private void testOpenDb1(){
+ final OutputPointer.sqlite3 out = new OutputPointer.sqlite3();
+ int rc = sqlite3_open(":memory:", out);
+ ++metrics.dbOpen;
+ sqlite3 db = out.get();
+ affirm(0 == rc);
+ affirm(db.getNativePointer()!=0);
+ sqlite3_db_config(db, SQLITE_DBCONFIG_DEFENSIVE, 1, null)
+ /* This function has different mangled names in jdk8 vs jdk19,
+ and this call is here to ensure that the build fails
+ if it cannot find both names. */;
+
+ affirm( 0==sqlite3_db_readonly(db,"main") );
+ affirm( 0==sqlite3_db_readonly(db,null) );
+ affirm( 0>sqlite3_db_readonly(db,"nope") );
+ affirm( 0>sqlite3_db_readonly(null,null) );
+ affirm( 0==sqlite3_last_insert_rowid(null) );
+
+ // These interrupt checks are only to make sure that the JNI binding
+ // has the proper exported symbol names. They don't actually test
+ // anything useful.
+ affirm( !sqlite3_is_interrupted(db) );
+ sqlite3_interrupt(db);
+ affirm( sqlite3_is_interrupted(db) );
+ sqlite3_close_v2(db);
+ affirm(0 == db.getNativePointer());
+ }
+
+ private void testOpenDb2(){
+ final OutputPointer.sqlite3 out = new OutputPointer.sqlite3();
+ int rc = sqlite3_open_v2(":memory:", out,
+ SQLITE_OPEN_READWRITE
+ | SQLITE_OPEN_CREATE, null);
+ ++metrics.dbOpen;
+ affirm(0 == rc);
+ sqlite3 db = out.get();
+ affirm(0 != db.getNativePointer());
+ sqlite3_close_v2(db);
+ affirm(0 == db.getNativePointer());
+ }
+
+ private void testPrepare123(){
+ sqlite3 db = createNewDb();
+ int rc;
+ final OutputPointer.sqlite3_stmt outStmt = new OutputPointer.sqlite3_stmt();
+ rc = sqlite3_prepare(db, "CREATE TABLE t1(a);", outStmt);
+ affirm(0 == rc);
+ sqlite3_stmt stmt = outStmt.take();
+ affirm(0 != stmt.getNativePointer());
+ affirm( !sqlite3_stmt_readonly(stmt) );
+ affirm( db == sqlite3_db_handle(stmt) );
+ rc = sqlite3_step(stmt);
+ affirm(SQLITE_DONE == rc);
+ sqlite3_finalize(stmt);
+ affirm( null == sqlite3_db_handle(stmt) );
+ affirm(0 == stmt.getNativePointer());
+
+ { /* Demonstrate how to use the "zTail" option of
+ sqlite3_prepare() family of functions. */
+ OutputPointer.Int32 oTail = new OutputPointer.Int32();
+ final byte[] sqlUtf8 =
+ "CREATE TABLE t2(a); INSERT INTO t2(a) VALUES(1),(2),(3)"
+ .getBytes(StandardCharsets.UTF_8);
+ int pos = 0, n = 1;
+ byte[] sqlChunk = sqlUtf8;
+ while(pos < sqlChunk.length){
+ if(pos > 0){
+ sqlChunk = Arrays.copyOfRange(sqlChunk, pos, sqlChunk.length);
+ }
+ //outln("SQL chunk #"+n+" length = "+sqlChunk.length+", pos = "+pos);
+ if( 0==sqlChunk.length ) break;
+ rc = sqlite3_prepare_v2(db, sqlChunk, outStmt, oTail);
+ affirm(0 == rc);
+ stmt = outStmt.get();
+ pos = oTail.value;
+ /*outln("SQL tail pos = "+pos+". Chunk = "+
+ (new String(Arrays.copyOfRange(sqlChunk,0,pos),
+ StandardCharsets.UTF_8)));*/
+ switch(n){
+ case 1: affirm(19 == pos); break;
+ case 2: affirm(36 == pos); break;
+ default: affirm( false /* can't happen */ );
+
+ }
+ ++n;
+ affirm(0 != stmt.getNativePointer());
+ rc = sqlite3_step(stmt);
+ affirm(SQLITE_DONE == rc);
+ sqlite3_finalize(stmt);
+ affirm(0 == stmt.getNativePointer());
+ }
+ }
+
+
+ rc = sqlite3_prepare_v3(db, "INSERT INTO t2(a) VALUES(1),(2),(3)",
+ SQLITE_PREPARE_NORMALIZE, outStmt);
+ affirm(0 == rc);
+ stmt = outStmt.get();
+ affirm(0 != stmt.getNativePointer());
+ sqlite3_finalize(stmt);
+ affirm(0 == stmt.getNativePointer() );
+
+ affirm( 0==sqlite3_errcode(db) );
+ stmt = sqlite3_prepare(db, "intentional error");
+ affirm( null==stmt );
+ affirm( 0!=sqlite3_errcode(db) );
+ affirm( 0==sqlite3_errmsg(db).indexOf("near \"intentional\"") );
+ sqlite3_finalize(stmt);
+ stmt = sqlite3_prepare(db, "/* empty input*/\n-- comments only");
+ affirm( null==stmt );
+ affirm( 0==sqlite3_errcode(db) );
+ sqlite3_close_v2(db);
+ }
+
+ private void testBindFetchInt(){
+ sqlite3 db = createNewDb();
+ execSql(db, "CREATE TABLE t(a)");
+
+ sqlite3_stmt stmt = prepare(db, "INSERT INTO t(a) VALUES(:a);");
+ affirm(1 == sqlite3_bind_parameter_count(stmt));
+ final int paramNdx = sqlite3_bind_parameter_index(stmt, ":a");
+ affirm(1 == paramNdx);
+ affirm( ":a".equals(sqlite3_bind_parameter_name(stmt, paramNdx)));
+ int total1 = 0;
+ long rowid = -1;
+ int changes = sqlite3_changes(db);
+ int changesT = sqlite3_total_changes(db);
+ long changes64 = sqlite3_changes64(db);
+ long changesT64 = sqlite3_total_changes64(db);
+ int rc;
+ for(int i = 99; i < 102; ++i ){
+ total1 += i;
+ rc = sqlite3_bind_int(stmt, paramNdx, i);
+ affirm(0 == rc);
+ rc = sqlite3_step(stmt);
+ sqlite3_reset(stmt);
+ affirm(SQLITE_DONE == rc);
+ long x = sqlite3_last_insert_rowid(db);
+ affirm(x > rowid);
+ rowid = x;
+ }
+ sqlite3_finalize(stmt);
+ affirm(300 == total1);
+ affirm(sqlite3_changes(db) > changes);
+ affirm(sqlite3_total_changes(db) > changesT);
+ affirm(sqlite3_changes64(db) > changes64);
+ affirm(sqlite3_total_changes64(db) > changesT64);
+ stmt = prepare(db, "SELECT a FROM t ORDER BY a DESC;");
+ affirm( sqlite3_stmt_readonly(stmt) );
+ affirm( !sqlite3_stmt_busy(stmt) );
+ int total2 = 0;
+ while( SQLITE_ROW == sqlite3_step(stmt) ){
+ affirm( sqlite3_stmt_busy(stmt) );
+ total2 += sqlite3_column_int(stmt, 0);
+ sqlite3_value sv = sqlite3_column_value(stmt, 0);
+ affirm( null != sv );
+ affirm( 0 != sv.getNativePointer() );
+ affirm( SQLITE_INTEGER == sqlite3_value_type(sv) );
+ }
+ affirm( !sqlite3_stmt_busy(stmt) );
+ sqlite3_finalize(stmt);
+ affirm(total1 == total2);
+
+ // sqlite3_value_frombind() checks...
+ stmt = prepare(db, "SELECT 1, ?");
+ sqlite3_bind_int(stmt, 1, 2);
+ rc = sqlite3_step(stmt);
+ affirm( SQLITE_ROW==rc );
+ affirm( !sqlite3_value_frombind(sqlite3_column_value(stmt, 0)) );
+ affirm( sqlite3_value_frombind(sqlite3_column_value(stmt, 1)) );
+ sqlite3_finalize(stmt);
+
+ sqlite3_close_v2(db);
+ affirm(0 == db.getNativePointer());
+ }
+
+ private void testBindFetchInt64(){
+ try (sqlite3 db = createNewDb()){
+ execSql(db, "CREATE TABLE t(a)");
+ sqlite3_stmt stmt = prepare(db, "INSERT INTO t(a) VALUES(?);");
+ long total1 = 0;
+ for(long i = 0xffffffff; i < 0xffffffff + 3; ++i ){
+ total1 += i;
+ sqlite3_bind_int64(stmt, 1, i);
+ sqlite3_step(stmt);
+ sqlite3_reset(stmt);
+ }
+ sqlite3_finalize(stmt);
+ stmt = prepare(db, "SELECT a FROM t ORDER BY a DESC;");
+ long total2 = 0;
+ while( SQLITE_ROW == sqlite3_step(stmt) ){
+ total2 += sqlite3_column_int64(stmt, 0);
+ }
+ sqlite3_finalize(stmt);
+ affirm(total1 == total2);
+ //sqlite3_close_v2(db);
+ }
+ }
+
+ private void testBindFetchDouble(){
+ try (sqlite3 db = createNewDb()){
+ execSql(db, "CREATE TABLE t(a)");
+ sqlite3_stmt stmt = prepare(db, "INSERT INTO t(a) VALUES(?);");
+ double total1 = 0;
+ for(double i = 1.5; i < 5.0; i = i + 1.0 ){
+ total1 += i;
+ sqlite3_bind_double(stmt, 1, i);
+ sqlite3_step(stmt);
+ sqlite3_reset(stmt);
+ }
+ sqlite3_finalize(stmt);
+ stmt = prepare(db, "SELECT a FROM t ORDER BY a DESC;");
+ double total2 = 0;
+ int counter = 0;
+ while( SQLITE_ROW == sqlite3_step(stmt) ){
+ ++counter;
+ total2 += sqlite3_column_double(stmt, 0);
+ }
+ affirm(4 == counter);
+ sqlite3_finalize(stmt);
+ affirm(total2<=total1+0.01 && total2>=total1-0.01);
+ //sqlite3_close_v2(db);
+ }
+ }
+
+ private void testBindFetchText(){
+ sqlite3 db = createNewDb();
+ execSql(db, "CREATE TABLE t(a)");
+ sqlite3_stmt stmt = prepare(db, "INSERT INTO t(a) VALUES(?);");
+ String[] list1 = { "hell🤩", "w😃rld", "!🤩" };
+ int rc;
+ int n = 0;
+ for( String e : list1 ){
+ rc = (0==n)
+ ? sqlite3_bind_text(stmt, 1, e)
+ : sqlite3_bind_text16(stmt, 1, e);
+ affirm(0 == rc);
+ rc = sqlite3_step(stmt);
+ affirm(SQLITE_DONE==rc);
+ sqlite3_reset(stmt);
+ }
+ sqlite3_finalize(stmt);
+ stmt = prepare(db, "SELECT a FROM t ORDER BY a DESC;");
+ StringBuilder sbuf = new StringBuilder();
+ n = 0;
+ while( SQLITE_ROW == sqlite3_step(stmt) ){
+ final sqlite3_value sv = sqlite3_value_dup(sqlite3_column_value(stmt,0));
+ final String txt = sqlite3_column_text16(stmt, 0);
+ sbuf.append( txt );
+ affirm( txt.equals(new String(
+ sqlite3_column_text(stmt, 0),
+ StandardCharsets.UTF_8
+ )) );
+ affirm( txt.length() < sqlite3_value_bytes(sv) );
+ affirm( txt.equals(new String(
+ sqlite3_value_text(sv),
+ StandardCharsets.UTF_8)) );
+ affirm( txt.length() == sqlite3_value_bytes16(sv)/2 );
+ affirm( txt.equals(sqlite3_value_text16(sv)) );
+ sqlite3_value_free(sv);
+ ++n;
+ }
+ sqlite3_finalize(stmt);
+ affirm(3 == n);
+ affirm("w😃rldhell🤩!🤩".equals(sbuf.toString()));
+
+ try( sqlite3_stmt stmt2 = prepare(db, "SELECT ?, ?") ){
+ rc = sqlite3_bind_text(stmt2, 1, "");
+ affirm( 0==rc );
+ rc = sqlite3_bind_text(stmt2, 2, (String)null);
+ affirm( 0==rc );
+ rc = sqlite3_step(stmt2);
+ affirm( SQLITE_ROW==rc );
+ byte[] colBa = sqlite3_column_text(stmt2, 0);
+ affirm( 0==colBa.length );
+ colBa = sqlite3_column_text(stmt2, 1);
+ affirm( null==colBa );
+ //sqlite3_finalize(stmt);
+ }
+
+ if(true){
+ sqlite3_close_v2(db);
+ }else{
+ // Let the Object.finalize() override deal with it.
+ }
+ }
+
+ private void testBindFetchBlob(){
+ sqlite3 db = createNewDb();
+ execSql(db, "CREATE TABLE t(a)");
+ sqlite3_stmt stmt = prepare(db, "INSERT INTO t(a) VALUES(?);");
+ byte[] list1 = { 0x32, 0x33, 0x34 };
+ int rc = sqlite3_bind_blob(stmt, 1, list1);
+ affirm( 0==rc );
+ rc = sqlite3_step(stmt);
+ affirm(SQLITE_DONE == rc);
+ sqlite3_finalize(stmt);
+ stmt = prepare(db, "SELECT a FROM t ORDER BY a DESC;");
+ int n = 0;
+ int total = 0;
+ while( SQLITE_ROW == sqlite3_step(stmt) ){
+ byte[] blob = sqlite3_column_blob(stmt, 0);
+ affirm(3 == blob.length);
+ int i = 0;
+ for(byte b : blob){
+ affirm(b == list1[i++]);
+ total += b;
+ }
+ ++n;
+ }
+ sqlite3_finalize(stmt);
+ affirm(1 == n);
+ affirm(total == 0x32 + 0x33 + 0x34);
+ sqlite3_close_v2(db);
+ }
+
+ private void testSql(){
+ sqlite3 db = createNewDb();
+ sqlite3_stmt stmt = prepare(db, "SELECT 1");
+ affirm( "SELECT 1".equals(sqlite3_sql(stmt)) );
+ sqlite3_finalize(stmt);
+ stmt = prepare(db, "SELECT ?");
+ sqlite3_bind_text(stmt, 1, "hell😃");
+ final String expect = "SELECT 'hell😃'";
+ affirm( expect.equals(sqlite3_expanded_sql(stmt)) );
+ String n = sqlite3_normalized_sql(stmt);
+ affirm( null==n || "SELECT?;".equals(n) );
+ sqlite3_finalize(stmt);
+ sqlite3_close(db);
+ }
+
+ private void testCollation(){
+ final sqlite3 db = createNewDb();
+ execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES('a'),('b'),('c')");
+ final ValueHolder xDestroyCalled = new ValueHolder<>(0);
+ final CollationCallback myCollation = new CollationCallback() {
+ private String myState =
+ "this is local state. There is much like it, but this is mine.";
+ @Override
+ // Reverse-sorts its inputs...
+ public int call(byte[] lhs, byte[] rhs){
+ int len = lhs.length > rhs.length ? rhs.length : lhs.length;
+ int c = 0, i = 0;
+ for(i = 0; i < len; ++i){
+ c = lhs[i] - rhs[i];
+ if(0 != c) break;
+ }
+ if(0==c){
+ if(i < lhs.length) c = 1;
+ else if(i < rhs.length) c = -1;
+ }
+ return -c;
+ }
+ @Override
+ public void xDestroy() {
+ // Just demonstrates that xDestroy is called.
+ ++xDestroyCalled.value;
+ }
+ };
+ final CollationNeededCallback collLoader = new CollationNeededCallback(){
+ @Override
+ public int call(sqlite3 dbArg, int eTextRep, String collationName){
+ affirm(dbArg == db/* as opposed to a temporary object*/);
+ return sqlite3_create_collation(dbArg, "reversi", eTextRep, myCollation);
+ }
+ };
+ int rc = sqlite3_collation_needed(db, collLoader);
+ affirm( 0 == rc );
+ rc = sqlite3_collation_needed(db, collLoader);
+ affirm( 0 == rc /* Installing the same object again is a no-op */);
+ sqlite3_stmt stmt = prepare(db, "SELECT a FROM t ORDER BY a COLLATE reversi");
+ int counter = 0;
+ while( SQLITE_ROW == sqlite3_step(stmt) ){
+ final String val = sqlite3_column_text16(stmt, 0);
+ ++counter;
+ //outln("REVERSI'd row#"+counter+": "+val);
+ switch(counter){
+ case 1: affirm("c".equals(val)); break;
+ case 2: affirm("b".equals(val)); break;
+ case 3: affirm("a".equals(val)); break;
+ }
+ }
+ affirm(3 == counter);
+ sqlite3_finalize(stmt);
+ stmt = prepare(db, "SELECT a FROM t ORDER BY a");
+ counter = 0;
+ while( SQLITE_ROW == sqlite3_step(stmt) ){
+ final String val = sqlite3_column_text16(stmt, 0);
+ ++counter;
+ //outln("Non-REVERSI'd row#"+counter+": "+val);
+ switch(counter){
+ case 3: affirm("c".equals(val)); break;
+ case 2: affirm("b".equals(val)); break;
+ case 1: affirm("a".equals(val)); break;
+ }
+ }
+ affirm(3 == counter);
+ sqlite3_finalize(stmt);
+ affirm( 0 == xDestroyCalled.value );
+ rc = sqlite3_collation_needed(db, null);
+ affirm( 0 == rc );
+ sqlite3_close_v2(db);
+ affirm( 0 == db.getNativePointer() );
+ affirm( 1 == xDestroyCalled.value );
+ }
+
+ @SingleThreadOnly /* because it's thread-agnostic */
+ private void testToUtf8(){
+ /**
+ https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html
+
+ Let's ensure that we can convert to standard UTF-8 in Java code
+ (noting that the JNI native API has no way to do this).
+ */
+ final byte[] ba = "a \0 b".getBytes(StandardCharsets.UTF_8);
+ affirm( 5 == ba.length /* as opposed to 6 in modified utf-8 */);
+ }
+
+ private void testStatus(){
+ final OutputPointer.Int64 cur64 = new OutputPointer.Int64();
+ final OutputPointer.Int64 high64 = new OutputPointer.Int64();
+ final OutputPointer.Int32 cur32 = new OutputPointer.Int32();
+ final OutputPointer.Int32 high32 = new OutputPointer.Int32();
+ final sqlite3 db = createNewDb();
+ execSql(db, "create table t(a); insert into t values(1),(2),(3)");
+
+ int rc = sqlite3_status(SQLITE_STATUS_MEMORY_USED, cur32, high32, false);
+ affirm( 0 == rc );
+ affirm( cur32.value > 0 );
+ affirm( high32.value >= cur32.value );
+
+ rc = sqlite3_status64(SQLITE_STATUS_MEMORY_USED, cur64, high64, false);
+ affirm( 0 == rc );
+ affirm( cur64.value > 0 );
+ affirm( high64.value >= cur64.value );
+
+ cur32.value = 0;
+ high32.value = 1;
+ rc = sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, cur32, high32, false);
+ affirm( 0 == rc );
+ affirm( cur32.value > 0 );
+ affirm( high32.value == 0 /* always 0 for SCHEMA_USED */ );
+
+ sqlite3_close_v2(db);
+ }
+
+ private void testUdf1(){
+ final sqlite3 db = createNewDb();
+ // These ValueHolders are just to confirm that the func did what we want...
+ final ValueHolder xDestroyCalled = new ValueHolder<>(false);
+ final ValueHolder xFuncAccum = new ValueHolder<>(0);
+ final ValueHolder neverEverDoThisInClientCode = new ValueHolder<>(null);
+ final ValueHolder neverEverDoThisInClientCode2 = new ValueHolder<>(null);
+
+ // Create an SQLFunction instance using one of its 3 subclasses:
+ // Scalar, Aggregate, or Window:
+ SQLFunction func =
+ // Each of the 3 subclasses requires a different set of
+ // functions, all of which must be implemented. Anonymous
+ // classes are a convenient way to implement these.
+ new ScalarFunction(){
+ public void xFunc(sqlite3_context cx, sqlite3_value[] args){
+ affirm(db == sqlite3_context_db_handle(cx));
+ if( null==neverEverDoThisInClientCode.value ){
+ /* !!!NEVER!!! hold a reference to an sqlite3_value or
+ sqlite3_context object like this in client code! They
+ are ONLY legal for the duration of their single
+ call. We do it here ONLY to test that the defenses
+ against clients doing this are working. */
+ neverEverDoThisInClientCode2.value = cx;
+ neverEverDoThisInClientCode.value = args;
+ }
+ int result = 0;
+ for( sqlite3_value v : args ) result += sqlite3_value_int(v);
+ xFuncAccum.value += result;// just for post-run testing
+ sqlite3_result_int(cx, result);
+ }
+ /* OPTIONALLY override xDestroy... */
+ public void xDestroy(){
+ xDestroyCalled.value = true;
+ }
+ };
+
+ // Register and use the function...
+ int rc = sqlite3_create_function(db, "myfunc", -1, SQLITE_UTF8, func);
+ affirm(0 == rc);
+ affirm(0 == xFuncAccum.value);
+ final sqlite3_stmt stmt = prepare(db, "SELECT myfunc(1,2,3)");
+ int n = 0;
+ while( SQLITE_ROW == sqlite3_step(stmt) ){
+ affirm( 6 == sqlite3_column_int(stmt, 0) );
+ ++n;
+ }
+ sqlite3_finalize(stmt);
+ affirm(1 == n);
+ affirm(6 == xFuncAccum.value);
+ affirm( !xDestroyCalled.value );
+ affirm( null!=neverEverDoThisInClientCode.value );
+ affirm( null!=neverEverDoThisInClientCode2.value );
+ affirm( 0 xFuncAccum = new ValueHolder<>(0);
+
+ SQLFunction funcAgg = new AggregateFunction(){
+ @Override public void xStep(sqlite3_context cx, sqlite3_value[] args){
+ /** Throwing from here should emit loud noise on stdout or stderr
+ but the exception is supressed because we have no way to inform
+ sqlite about it from these callbacks. */
+ //throw new RuntimeException("Throwing from an xStep");
+ }
+ @Override public void xFinal(sqlite3_context cx){
+ throw new RuntimeException("Throwing from an xFinal");
+ }
+ };
+ int rc = sqlite3_create_function(db, "myagg", 1, SQLITE_UTF8, funcAgg);
+ affirm(0 == rc);
+ affirm(0 == xFuncAccum.value);
+ sqlite3_stmt stmt = prepare(db, "SELECT myagg(1)");
+ rc = sqlite3_step(stmt);
+ sqlite3_finalize(stmt);
+ affirm( 0 != rc );
+ affirm( sqlite3_errmsg(db).indexOf("an xFinal") > 0 );
+
+ SQLFunction funcSc = new ScalarFunction(){
+ @Override public void xFunc(sqlite3_context cx, sqlite3_value[] args){
+ throw new RuntimeException("Throwing from an xFunc");
+ }
+ };
+ rc = sqlite3_create_function(db, "mysca", 0, SQLITE_UTF8, funcSc);
+ affirm(0 == rc);
+ affirm(0 == xFuncAccum.value);
+ stmt = prepare(db, "SELECT mysca()");
+ rc = sqlite3_step(stmt);
+ sqlite3_finalize(stmt);
+ affirm( 0 != rc );
+ affirm( sqlite3_errmsg(db).indexOf("an xFunc") > 0 );
+ rc = sqlite3_create_function(db, "mysca", 1, -1, funcSc);
+ affirm( SQLITE_FORMAT==rc, "invalid encoding value." );
+ sqlite3_close_v2(db);
+ }
+
+ @SingleThreadOnly
+ private void testUdfJavaObject(){
+ affirm( !mtMode );
+ final sqlite3 db = createNewDb();
+ final ValueHolder testResult = new ValueHolder<>(db);
+ final ValueHolder boundObj = new ValueHolder<>(42);
+ final SQLFunction func = new ScalarFunction(){
+ public void xFunc(sqlite3_context cx, sqlite3_value args[]){
+ sqlite3_result_java_object(cx, testResult.value);
+ affirm( sqlite3_value_java_object(args[0]) == boundObj );
+ }
+ };
+ int rc = sqlite3_create_function(db, "myfunc", -1, SQLITE_UTF8, func);
+ affirm(0 == rc);
+ sqlite3_stmt stmt = prepare(db, "select myfunc(?)");
+ affirm( 0 != stmt.getNativePointer() );
+ affirm( testResult.value == db );
+ rc = sqlite3_bind_java_object(stmt, 1, boundObj);
+ affirm( 0==rc );
+ int n = 0;
+ if( SQLITE_ROW == sqlite3_step(stmt) ){
+ final sqlite3_value v = sqlite3_column_value(stmt, 0);
+ affirm( testResult.value == sqlite3_value_java_object(v) );
+ affirm( testResult.value == sqlite3_value_java_casted(v, sqlite3.class) );
+ affirm( testResult.value ==
+ sqlite3_value_java_casted(v, testResult.value.getClass()) );
+ affirm( testResult.value == sqlite3_value_java_casted(v, Object.class) );
+ affirm( null == sqlite3_value_java_casted(v, String.class) );
+ ++n;
+ }
+ sqlite3_finalize(stmt);
+ affirm( 1 == n );
+ affirm( 0==sqlite3_db_release_memory(db) );
+ sqlite3_close_v2(db);
+ }
+
+ private void testUdfAggregate(){
+ final sqlite3 db = createNewDb();
+ final ValueHolder xFinalNull =
+ // To confirm that xFinal() is called with no aggregate state
+ // when the corresponding result set is empty.
+ new ValueHolder<>(false);
+ SQLFunction func = new AggregateFunction(){
+ @Override
+ public void xStep(sqlite3_context cx, sqlite3_value[] args){
+ final ValueHolder agg = this.getAggregateState(cx, 0);
+ agg.value += sqlite3_value_int(args[0]);
+ affirm( agg == this.getAggregateState(cx, 0) );
+ }
+ @Override
+ public void xFinal(sqlite3_context cx){
+ final Integer v = this.takeAggregateState(cx);
+ if(null == v){
+ xFinalNull.value = true;
+ sqlite3_result_null(cx);
+ }else{
+ sqlite3_result_int(cx, v);
+ }
+ }
+ };
+ execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES(1),(2),(3)");
+ int rc = sqlite3_create_function(db, "myfunc", 1, SQLITE_UTF8, func);
+ affirm(0 == rc);
+ sqlite3_stmt stmt = prepare(db, "select myfunc(a), myfunc(a+10) from t");
+ affirm( 0==sqlite3_stmt_status(stmt, SQLITE_STMTSTATUS_RUN, false) );
+ int n = 0;
+ if( SQLITE_ROW == sqlite3_step(stmt) ){
+ int v = sqlite3_column_int(stmt, 0);
+ affirm( 6 == v );
+ int v2 = sqlite3_column_int(stmt, 1);
+ affirm( 30+v == v2 );
+ ++n;
+ }
+ affirm( 1==n );
+ affirm(!xFinalNull.value);
+ sqlite3_reset(stmt);
+ affirm( 1==sqlite3_stmt_status(stmt, SQLITE_STMTSTATUS_RUN, false) );
+ // Ensure that the accumulator is reset on subsequent calls...
+ n = 0;
+ if( SQLITE_ROW == sqlite3_step(stmt) ){
+ final int v = sqlite3_column_int(stmt, 0);
+ affirm( 6 == v );
+ ++n;
+ }
+ sqlite3_finalize(stmt);
+ affirm( 1==n );
+
+ stmt = prepare(db, "select myfunc(a), myfunc(a+a) from t order by a");
+ n = 0;
+ while( SQLITE_ROW == sqlite3_step(stmt) ){
+ final int c0 = sqlite3_column_int(stmt, 0);
+ final int c1 = sqlite3_column_int(stmt, 1);
+ ++n;
+ affirm( 6 == c0 );
+ affirm( 12 == c1 );
+ }
+ sqlite3_finalize(stmt);
+ affirm( 1 == n );
+ affirm(!xFinalNull.value);
+
+ execSql(db, "SELECT myfunc(1) WHERE 0");
+ affirm(xFinalNull.value);
+ sqlite3_close_v2(db);
+ }
+
+ private void testUdfWindow(){
+ final sqlite3 db = createNewDb();
+ /* Example window function, table, and results taken from:
+ https://sqlite.org/windowfunctions.html#udfwinfunc */
+ final SQLFunction func = new WindowFunction(){
+
+ private void xStepInverse(sqlite3_context cx, int v){
+ this.getAggregateState(cx,0).value += v;
+ }
+ @Override public void xStep(sqlite3_context cx, sqlite3_value[] args){
+ this.xStepInverse(cx, sqlite3_value_int(args[0]));
+ }
+ @Override public void xInverse(sqlite3_context cx, sqlite3_value[] args){
+ this.xStepInverse(cx, -sqlite3_value_int(args[0]));
+ }
+
+ private void xFinalValue(sqlite3_context cx, Integer v){
+ if(null == v) sqlite3_result_null(cx);
+ else sqlite3_result_int(cx, v);
+ }
+ @Override public void xFinal(sqlite3_context cx){
+ xFinalValue(cx, this.takeAggregateState(cx));
+ }
+ @Override public void xValue(sqlite3_context cx){
+ xFinalValue(cx, this.getAggregateState(cx,null).value);
+ }
+ };
+ int rc = sqlite3_create_function(db, "winsumint", 1, SQLITE_UTF8, func);
+ affirm( 0 == rc );
+ execSql(db, new String[] {
+ "CREATE TEMP TABLE twin(x, y); INSERT INTO twin VALUES",
+ "('a', 4),('b', 5),('c', 3),('d', 8),('e', 1)"
+ });
+ final sqlite3_stmt stmt = prepare(db,
+ "SELECT x, winsumint(y) OVER ("+
+ "ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING"+
+ ") AS sum_y "+
+ "FROM twin ORDER BY x;");
+ affirm( 0 == rc );
+ int n = 0;
+ while( SQLITE_ROW == sqlite3_step(stmt) ){
+ final String s = sqlite3_column_text16(stmt, 0);
+ final int i = sqlite3_column_int(stmt, 1);
+ switch(++n){
+ case 1: affirm( "a".equals(s) && 9==i ); break;
+ case 2: affirm( "b".equals(s) && 12==i ); break;
+ case 3: affirm( "c".equals(s) && 16==i ); break;
+ case 4: affirm( "d".equals(s) && 12==i ); break;
+ case 5: affirm( "e".equals(s) && 9==i ); break;
+ default: affirm( false /* cannot happen */ );
+ }
+ }
+ sqlite3_finalize(stmt);
+ affirm( 5 == n );
+ sqlite3_close_v2(db);
+ }
+
+ private void listBoundMethods(){
+ if(false){
+ final java.lang.reflect.Field[] declaredFields =
+ CApi.class.getDeclaredFields();
+ outln("Bound constants:\n");
+ for(java.lang.reflect.Field field : declaredFields) {
+ if(java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
+ outln("\t",field.getName());
+ }
+ }
+ }
+ final java.lang.reflect.Method[] declaredMethods =
+ CApi.class.getDeclaredMethods();
+ final java.util.List funcList = new java.util.ArrayList<>();
+ for(java.lang.reflect.Method m : declaredMethods){
+ if((m.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0){
+ final String name = m.getName();
+ if(name.startsWith("sqlite3_")){
+ funcList.add(name);
+ }
+ }
+ }
+ int count = 0;
+ java.util.Collections.sort(funcList);
+ for(String n : funcList){
+ ++count;
+ outln("\t",n,"()");
+ }
+ outln(count," functions named sqlite3_*.");
+ }
+
+ private void testTrace(){
+ final sqlite3 db = createNewDb();
+ final ValueHolder counter = new ValueHolder<>(0);
+ /* Ensure that characters outside of the UTF BMP survive the trip
+ from Java to sqlite3 and back to Java. (At no small efficiency
+ penalty.) */
+ final String nonBmpChar = "😃";
+ int rc = sqlite3_trace_v2(
+ db, SQLITE_TRACE_STMT | SQLITE_TRACE_PROFILE
+ | SQLITE_TRACE_ROW | SQLITE_TRACE_CLOSE,
+ new TraceV2Callback(){
+ @Override public int call(int traceFlag, Object pNative, Object x){
+ ++counter.value;
+ //outln("TRACE "+traceFlag+" pNative = "+pNative.getClass().getName());
+ switch(traceFlag){
+ case SQLITE_TRACE_STMT:
+ affirm(pNative instanceof sqlite3_stmt);
+ //outln("TRACE_STMT sql = "+x);
+ affirm(x instanceof String);
+ affirm( ((String)x).indexOf(nonBmpChar) > 0 );
+ break;
+ case SQLITE_TRACE_PROFILE:
+ affirm(pNative instanceof sqlite3_stmt);
+ affirm(x instanceof Long);
+ //outln("TRACE_PROFILE time = "+x);
+ break;
+ case SQLITE_TRACE_ROW:
+ affirm(pNative instanceof sqlite3_stmt);
+ affirm(null == x);
+ //outln("TRACE_ROW = "+sqlite3_column_text16((sqlite3_stmt)pNative, 0));
+ break;
+ case SQLITE_TRACE_CLOSE:
+ affirm(pNative instanceof sqlite3);
+ affirm(null == x);
+ break;
+ default:
+ affirm(false /*cannot happen*/);
+ break;
+ }
+ return 0;
+ }
+ });
+ affirm( 0==rc );
+ execSql(db, "SELECT coalesce(null,null,'"+nonBmpChar+"'); "+
+ "SELECT 'w"+nonBmpChar+"orld'");
+ affirm( 6 == counter.value );
+ sqlite3_close_v2(db);
+ affirm( 7 == counter.value );
+ }
+
+ @SingleThreadOnly /* because threads inherently break this test */
+ private static void testBusy(){
+ final String dbName = "_busy-handler.db";
+ final OutputPointer.sqlite3 outDb = new OutputPointer.sqlite3();
+ final OutputPointer.sqlite3_stmt outStmt = new OutputPointer.sqlite3_stmt();
+
+ int rc = sqlite3_open(dbName, outDb);
+ ++metrics.dbOpen;
+ affirm( 0 == rc );
+ final sqlite3 db1 = outDb.get();
+ execSql(db1, "CREATE TABLE IF NOT EXISTS t(a)");
+ rc = sqlite3_open(dbName, outDb);
+ ++metrics.dbOpen;
+ affirm( 0 == rc );
+ affirm( outDb.get() != db1 );
+ final sqlite3 db2 = outDb.get();
+
+ affirm( "main".equals( sqlite3_db_name(db1, 0) ) );
+ rc = sqlite3_db_config(db1, SQLITE_DBCONFIG_MAINDBNAME, "foo");
+ affirm( sqlite3_db_filename(db1, "foo").endsWith(dbName) );
+ affirm( "foo".equals( sqlite3_db_name(db1, 0) ) );
+
+ final ValueHolder xBusyCalled = new ValueHolder<>(0);
+ BusyHandlerCallback handler = new BusyHandlerCallback(){
+ @Override public int call(int n){
+ //outln("busy handler #"+n);
+ return n > 2 ? 0 : ++xBusyCalled.value;
+ }
+ };
+ rc = sqlite3_busy_handler(db2, handler);
+ affirm(0 == rc);
+
+ // Force a locked condition...
+ execSql(db1, "BEGIN EXCLUSIVE");
+ rc = sqlite3_prepare_v2(db2, "SELECT * from t", outStmt);
+ affirm( SQLITE_BUSY == rc);
+ affirm( null == outStmt.get() );
+ affirm( 3 == xBusyCalled.value );
+ sqlite3_close_v2(db1);
+ sqlite3_close_v2(db2);
+ try{
+ final java.io.File f = new java.io.File(dbName);
+ f.delete();
+ }catch(Exception e){
+ /* ignore */
+ }
+ }
+
+ private void testProgress(){
+ final sqlite3 db = createNewDb();
+ final ValueHolder counter = new ValueHolder<>(0);
+ sqlite3_progress_handler(db, 1, new ProgressHandlerCallback(){
+ @Override public int call(){
+ ++counter.value;
+ return 0;
+ }
+ });
+ execSql(db, "SELECT 1; SELECT 2;");
+ affirm( counter.value > 0 );
+ int nOld = counter.value;
+ sqlite3_progress_handler(db, 0, null);
+ execSql(db, "SELECT 1; SELECT 2;");
+ affirm( nOld == counter.value );
+ sqlite3_close_v2(db);
+ }
+
+ private void testCommitHook(){
+ final sqlite3 db = createNewDb();
+ final ValueHolder counter = new ValueHolder<>(0);
+ final ValueHolder hookResult = new ValueHolder<>(0);
+ final CommitHookCallback theHook = new CommitHookCallback(){
+ @Override public int call(){
+ ++counter.value;
+ return hookResult.value;
+ }
+ };
+ CommitHookCallback oldHook = sqlite3_commit_hook(db, theHook);
+ affirm( null == oldHook );
+ execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES('a'),('b'),('c')");
+ affirm( 2 == counter.value );
+ execSql(db, "BEGIN; SELECT 1; SELECT 2; COMMIT;");
+ affirm( 2 == counter.value /* NOT invoked if no changes are made */ );
+ execSql(db, "BEGIN; update t set a='d' where a='c'; COMMIT;");
+ affirm( 3 == counter.value );
+ oldHook = sqlite3_commit_hook(db, theHook);
+ affirm( theHook == oldHook );
+ execSql(db, "BEGIN; update t set a='e' where a='d'; COMMIT;");
+ affirm( 4 == counter.value );
+ oldHook = sqlite3_commit_hook(db, null);
+ affirm( theHook == oldHook );
+ execSql(db, "BEGIN; update t set a='f' where a='e'; COMMIT;");
+ affirm( 4 == counter.value );
+ oldHook = sqlite3_commit_hook(db, null);
+ affirm( null == oldHook );
+ execSql(db, "BEGIN; update t set a='g' where a='f'; COMMIT;");
+ affirm( 4 == counter.value );
+
+ final CommitHookCallback newHook = new CommitHookCallback(){
+ @Override public int call(){return 0;}
+ };
+ oldHook = sqlite3_commit_hook(db, newHook);
+ affirm( null == oldHook );
+ execSql(db, "BEGIN; update t set a='h' where a='g'; COMMIT;");
+ affirm( 4 == counter.value );
+ oldHook = sqlite3_commit_hook(db, theHook);
+ affirm( newHook == oldHook );
+ execSql(db, "BEGIN; update t set a='i' where a='h'; COMMIT;");
+ affirm( 5 == counter.value );
+ hookResult.value = SQLITE_ERROR;
+ int rc = execSql(db, false, "BEGIN; update t set a='j' where a='i'; COMMIT;");
+ affirm( SQLITE_CONSTRAINT == rc );
+ affirm( 6 == counter.value );
+ sqlite3_close_v2(db);
+ }
+
+ private void testUpdateHook(){
+ final sqlite3 db = createNewDb();
+ final ValueHolder counter = new ValueHolder<>(0);
+ final ValueHolder expectedOp = new ValueHolder<>(0);
+ final UpdateHookCallback theHook = new UpdateHookCallback(){
+ @Override
+ public void call(int opId, String dbName, String tableName, long rowId){
+ ++counter.value;
+ if( 0!=expectedOp.value ){
+ affirm( expectedOp.value == opId );
+ }
+ }
+ };
+ UpdateHookCallback oldHook = sqlite3_update_hook(db, theHook);
+ affirm( null == oldHook );
+ expectedOp.value = SQLITE_INSERT;
+ execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES('a'),('b'),('c')");
+ affirm( 3 == counter.value );
+ expectedOp.value = SQLITE_UPDATE;
+ execSql(db, "update t set a='d' where a='c';");
+ affirm( 4 == counter.value );
+ oldHook = sqlite3_update_hook(db, theHook);
+ affirm( theHook == oldHook );
+ expectedOp.value = SQLITE_DELETE;
+ execSql(db, "DELETE FROM t where a='d'");
+ affirm( 5 == counter.value );
+ oldHook = sqlite3_update_hook(db, null);
+ affirm( theHook == oldHook );
+ execSql(db, "update t set a='e' where a='b';");
+ affirm( 5 == counter.value );
+ oldHook = sqlite3_update_hook(db, null);
+ affirm( null == oldHook );
+
+ final UpdateHookCallback newHook = new UpdateHookCallback(){
+ @Override public void call(int opId, String dbName, String tableName, long rowId){
+ }
+ };
+ oldHook = sqlite3_update_hook(db, newHook);
+ affirm( null == oldHook );
+ execSql(db, "update t set a='h' where a='a'");
+ affirm( 5 == counter.value );
+ oldHook = sqlite3_update_hook(db, theHook);
+ affirm( newHook == oldHook );
+ expectedOp.value = SQLITE_UPDATE;
+ execSql(db, "update t set a='i' where a='h'");
+ affirm( 6 == counter.value );
+ sqlite3_close_v2(db);
+ }
+
+ /**
+ This test is functionally identical to testUpdateHook(), only with a
+ different callback type.
+ */
+ private void testPreUpdateHook(){
+ if( !sqlite3_compileoption_used("ENABLE_PREUPDATE_HOOK") ){
+ //outln("Skipping testPreUpdateHook(): no pre-update hook support.");
+ return;
+ }
+ final sqlite3 db = createNewDb();
+ final ValueHolder counter = new ValueHolder<>(0);
+ final ValueHolder expectedOp = new ValueHolder<>(0);
+ final PreupdateHookCallback theHook = new PreupdateHookCallback(){
+ @Override
+ public void call(sqlite3 db, int opId, String dbName, String dbTable,
+ long iKey1, long iKey2 ){
+ ++counter.value;
+ switch( opId ){
+ case SQLITE_UPDATE:
+ affirm( 0 < sqlite3_preupdate_count(db) );
+ affirm( null != sqlite3_preupdate_new(db, 0) );
+ affirm( null != sqlite3_preupdate_old(db, 0) );
+ break;
+ case SQLITE_INSERT:
+ affirm( null != sqlite3_preupdate_new(db, 0) );
+ break;
+ case SQLITE_DELETE:
+ affirm( null != sqlite3_preupdate_old(db, 0) );
+ break;
+ default:
+ break;
+ }
+ if( 0!=expectedOp.value ){
+ affirm( expectedOp.value == opId );
+ }
+ }
+ };
+ PreupdateHookCallback oldHook = sqlite3_preupdate_hook(db, theHook);
+ affirm( null == oldHook );
+ expectedOp.value = SQLITE_INSERT;
+ execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES('a'),('b'),('c')");
+ affirm( 3 == counter.value );
+ expectedOp.value = SQLITE_UPDATE;
+ execSql(db, "update t set a='d' where a='c';");
+ affirm( 4 == counter.value );
+ oldHook = sqlite3_preupdate_hook(db, theHook);
+ affirm( theHook == oldHook );
+ expectedOp.value = SQLITE_DELETE;
+ execSql(db, "DELETE FROM t where a='d'");
+ affirm( 5 == counter.value );
+ oldHook = sqlite3_preupdate_hook(db, null);
+ affirm( theHook == oldHook );
+ execSql(db, "update t set a='e' where a='b';");
+ affirm( 5 == counter.value );
+ oldHook = sqlite3_preupdate_hook(db, null);
+ affirm( null == oldHook );
+
+ final PreupdateHookCallback newHook = new PreupdateHookCallback(){
+ @Override
+ public void call(sqlite3 db, int opId, String dbName,
+ String tableName, long iKey1, long iKey2){
+ }
+ };
+ oldHook = sqlite3_preupdate_hook(db, newHook);
+ affirm( null == oldHook );
+ execSql(db, "update t set a='h' where a='a'");
+ affirm( 5 == counter.value );
+ oldHook = sqlite3_preupdate_hook(db, theHook);
+ affirm( newHook == oldHook );
+ expectedOp.value = SQLITE_UPDATE;
+ execSql(db, "update t set a='i' where a='h'");
+ affirm( 6 == counter.value );
+
+ sqlite3_close_v2(db);
+ }
+
+ private void testRollbackHook(){
+ final sqlite3 db = createNewDb();
+ final ValueHolder counter = new ValueHolder<>(0);
+ final RollbackHookCallback theHook = new RollbackHookCallback(){
+ @Override public void call(){
+ ++counter.value;
+ }
+ };
+ RollbackHookCallback oldHook = sqlite3_rollback_hook(db, theHook);
+ affirm( null == oldHook );
+ execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES('a'),('b'),('c')");
+ affirm( 0 == counter.value );
+ execSql(db, false, "BEGIN; SELECT 1; SELECT 2; ROLLBACK;");
+ affirm( 1 == counter.value /* contra to commit hook, is invoked if no changes are made */ );
+
+ final RollbackHookCallback newHook = new RollbackHookCallback(){
+ @Override public void call(){return;}
+ };
+ oldHook = sqlite3_rollback_hook(db, newHook);
+ affirm( theHook == oldHook );
+ execSql(db, false, "BEGIN; SELECT 1; ROLLBACK;");
+ affirm( 1 == counter.value );
+ oldHook = sqlite3_rollback_hook(db, theHook);
+ affirm( newHook == oldHook );
+ execSql(db, false, "BEGIN; SELECT 1; ROLLBACK;");
+ affirm( 2 == counter.value );
+ int rc = execSql(db, false, "BEGIN; SELECT 1; ROLLBACK;");
+ affirm( 0 == rc );
+ affirm( 3 == counter.value );
+ sqlite3_close_v2(db);
+ }
+
+ /**
+ If FTS5 is available, runs FTS5 tests, else returns with no side
+ effects. If it is available but loading of the FTS5 bits fails,
+ it throws.
+ */
+ @SuppressWarnings("unchecked")
+ @SingleThreadOnly /* because the Fts5 parts are not yet known to be
+ thread-safe */
+ private void testFts5() throws Exception {
+ if( !sqlite3_compileoption_used("ENABLE_FTS5") ){
+ //outln("SQLITE_ENABLE_FTS5 is not set. Skipping FTS5 tests.");
+ return;
+ }
+ Exception err = null;
+ try {
+ Class t = Class.forName("org.sqlite.jni.fts5.TesterFts5");
+ java.lang.reflect.Constructor ctor = t.getConstructor();
+ ctor.setAccessible(true);
+ final long timeStart = System.currentTimeMillis();
+ ctor.newInstance() /* will run all tests */;
+ final long timeEnd = System.currentTimeMillis();
+ outln("FTS5 Tests done in ",(timeEnd - timeStart),"ms");
+ }catch(ClassNotFoundException e){
+ outln("FTS5 classes not loaded.");
+ err = e;
+ }catch(NoSuchMethodException e){
+ outln("FTS5 tester ctor not found.");
+ err = e;
+ }catch(Exception e){
+ outln("Instantiation of FTS5 tester threw.");
+ err = e;
+ }
+ if( null != err ){
+ outln("Exception: "+err);
+ err.printStackTrace();
+ throw err;
+ }
+ }
+
+ private void testAuthorizer(){
+ final sqlite3 db = createNewDb();
+ final ValueHolder counter = new ValueHolder<>(0);
+ final ValueHolder authRc = new ValueHolder<>(0);
+ final AuthorizerCallback auth = new AuthorizerCallback(){
+ public int call(int op, String s0, String s1, String s2, String s3){
+ ++counter.value;
+ //outln("xAuth(): "+s0+" "+s1+" "+s2+" "+s3);
+ return authRc.value;
+ }
+ };
+ execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES('a'),('b'),('c')");
+ sqlite3_set_authorizer(db, auth);
+ execSql(db, "UPDATE t SET a=1");
+ affirm( 1 == counter.value );
+ authRc.value = SQLITE_DENY;
+ int rc = execSql(db, false, "UPDATE t SET a=2");
+ affirm( SQLITE_AUTH==rc );
+ // TODO: expand these tests considerably
+ sqlite3_close(db);
+ }
+
+ @SingleThreadOnly /* because multiple threads legitimately make these
+ results unpredictable */
+ private synchronized void testAutoExtension(){
+ final ValueHolder val = new ValueHolder<>(0);
+ final ValueHolder toss = new ValueHolder<>(null);
+ final AutoExtensionCallback ax = new AutoExtensionCallback(){
+ @Override public int call(sqlite3 db){
+ ++val.value;
+ if( null!=toss.value ){
+ throw new RuntimeException(toss.value);
+ }
+ return 0;
+ }
+ };
+ int rc = sqlite3_auto_extension( ax );
+ affirm( 0==rc );
+ sqlite3_close(createNewDb());
+ affirm( 1==val.value );
+ sqlite3_close(createNewDb());
+ affirm( 2==val.value );
+ sqlite3_reset_auto_extension();
+ sqlite3_close(createNewDb());
+ affirm( 2==val.value );
+ rc = sqlite3_auto_extension( ax );
+ affirm( 0==rc );
+ // Must not add a new entry
+ rc = sqlite3_auto_extension( ax );
+ affirm( 0==rc );
+ sqlite3_close( createNewDb() );
+ affirm( 3==val.value );
+
+ sqlite3 db = createNewDb();
+ affirm( 4==val.value );
+ execSql(db, "ATTACH ':memory:' as foo");
+ affirm( 4==val.value, "ATTACH uses the same connection, not sub-connections." );
+ sqlite3_close(db);
+ db = null;
+
+ affirm( sqlite3_cancel_auto_extension(ax) );
+ affirm( !sqlite3_cancel_auto_extension(ax) );
+ sqlite3_close(createNewDb());
+ affirm( 4==val.value );
+ rc = sqlite3_auto_extension( ax );
+ affirm( 0==rc );
+ Exception err = null;
+ toss.value = "Throwing from auto_extension.";
+ try{
+ sqlite3_close(createNewDb());
+ }catch(Exception e){
+ err = e;
+ }
+ affirm( err!=null );
+ affirm( err.getMessage().indexOf(toss.value)>0 );
+ toss.value = null;
+
+ val.value = 0;
+ final AutoExtensionCallback ax2 = new AutoExtensionCallback(){
+ @Override public synchronized int call(sqlite3 db){
+ ++val.value;
+ return 0;
+ }
+ };
+ rc = sqlite3_auto_extension( ax2 );
+ affirm( 0 == rc );
+ sqlite3_close(createNewDb());
+ affirm( 2 == val.value );
+ affirm( sqlite3_cancel_auto_extension(ax) );
+ affirm( !sqlite3_cancel_auto_extension(ax) );
+ sqlite3_close(createNewDb());
+ affirm( 3 == val.value );
+ rc = sqlite3_auto_extension( ax );
+ affirm( 0 == rc );
+ sqlite3_close(createNewDb());
+ affirm( 5 == val.value );
+ affirm( sqlite3_cancel_auto_extension(ax2) );
+ affirm( !sqlite3_cancel_auto_extension(ax2) );
+ sqlite3_close(createNewDb());
+ affirm( 6 == val.value );
+ rc = sqlite3_auto_extension( ax2 );
+ affirm( 0 == rc );
+ sqlite3_close(createNewDb());
+ affirm( 8 == val.value );
+
+ sqlite3_reset_auto_extension();
+ sqlite3_close(createNewDb());
+ affirm( 8 == val.value );
+ affirm( !sqlite3_cancel_auto_extension(ax) );
+ affirm( !sqlite3_cancel_auto_extension(ax2) );
+ sqlite3_close(createNewDb());
+ affirm( 8 == val.value );
+ }
+
+
+ private void testColumnMetadata(){
+ final sqlite3 db = createNewDb();
+ execSql(db, new String[] {
+ "CREATE TABLE t(a duck primary key not null collate noCase); ",
+ "INSERT INTO t(a) VALUES(1),(2),(3);"
+ });
+ OutputPointer.Bool bNotNull = new OutputPointer.Bool();
+ OutputPointer.Bool bPrimaryKey = new OutputPointer.Bool();
+ OutputPointer.Bool bAutoinc = new OutputPointer.Bool();
+ OutputPointer.String zCollSeq = new OutputPointer.String();
+ OutputPointer.String zDataType = new OutputPointer.String();
+ int rc = sqlite3_table_column_metadata(
+ db, "main", "t", "a", zDataType, zCollSeq,
+ bNotNull, bPrimaryKey, bAutoinc);
+ affirm( 0==rc );
+ affirm( bPrimaryKey.value );
+ affirm( !bAutoinc.value );
+ affirm( bNotNull.value );
+ affirm( "noCase".equals(zCollSeq.value) );
+ affirm( "duck".equals(zDataType.value) );
+
+ TableColumnMetadata m =
+ sqlite3_table_column_metadata(db, "main", "t", "a");
+ affirm( null != m );
+ affirm( bPrimaryKey.value == m.isPrimaryKey() );
+ affirm( bAutoinc.value == m.isAutoincrement() );
+ affirm( bNotNull.value == m.isNotNull() );
+ affirm( zCollSeq.value.equals(m.getCollation()) );
+ affirm( zDataType.value.equals(m.getDataType()) );
+
+ affirm( null == sqlite3_table_column_metadata(db, "nope", "t", "a") );
+ affirm( null == sqlite3_table_column_metadata(db, "main", "nope", "a") );
+
+ m = sqlite3_table_column_metadata(db, "main", "t", null)
+ /* Check only for existence of table */;
+ affirm( null != m );
+ affirm( m.isPrimaryKey() );
+ affirm( !m.isAutoincrement() );
+ affirm( !m.isNotNull() );
+ affirm( "BINARY".equalsIgnoreCase(m.getCollation()) );
+ affirm( "INTEGER".equalsIgnoreCase(m.getDataType()) );
+
+ sqlite3_close_v2(db);
+ }
+
+ private void testTxnState(){
+ final sqlite3 db = createNewDb();
+ affirm( SQLITE_TXN_NONE == sqlite3_txn_state(db, null) );
+ affirm( sqlite3_get_autocommit(db) );
+ execSql(db, "BEGIN;");
+ affirm( !sqlite3_get_autocommit(db) );
+ affirm( SQLITE_TXN_NONE == sqlite3_txn_state(db, null) );
+ execSql(db, "SELECT * FROM sqlite_schema;");
+ affirm( SQLITE_TXN_READ == sqlite3_txn_state(db, "main") );
+ execSql(db, "CREATE TABLE t(a);");
+ affirm( SQLITE_TXN_WRITE == sqlite3_txn_state(db, null) );
+ execSql(db, "ROLLBACK;");
+ affirm( SQLITE_TXN_NONE == sqlite3_txn_state(db, null) );
+ sqlite3_close_v2(db);
+ }
+
+
+ private void testExplain(){
+ final sqlite3 db = createNewDb();
+ sqlite3_stmt stmt = prepare(db,"SELECT 1");
+
+ affirm( 0 == sqlite3_stmt_isexplain(stmt) );
+ int rc = sqlite3_stmt_explain(stmt, 1);
+ affirm( 1 == sqlite3_stmt_isexplain(stmt) );
+ rc = sqlite3_stmt_explain(stmt, 2);
+ affirm( 2 == sqlite3_stmt_isexplain(stmt) );
+ sqlite3_finalize(stmt);
+ sqlite3_close_v2(db);
+ }
+
+ private void testLimit(){
+ final sqlite3 db = createNewDb();
+ int v;
+
+ v = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
+ affirm( v > 0 );
+ affirm( v == sqlite3_limit(db, SQLITE_LIMIT_LENGTH, v-1) );
+ affirm( v-1 == sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1) );
+ sqlite3_close_v2(db);
+ }
+
+ private void testComplete(){
+ affirm( 0==sqlite3_complete("select 1") );
+ affirm( 0!=sqlite3_complete("select 1;") );
+ affirm( 0!=sqlite3_complete("nope 'nope' 'nope' 1;"), "Yup" );
+ }
+
+ private void testKeyword(){
+ final int n = sqlite3_keyword_count();
+ affirm( n>0 );
+ affirm( !sqlite3_keyword_check("_nope_") );
+ affirm( sqlite3_keyword_check("seLect") );
+ affirm( null!=sqlite3_keyword_name(0) );
+ affirm( null!=sqlite3_keyword_name(n-1) );
+ affirm( null==sqlite3_keyword_name(n) );
+ }
+
+ private void testBackup(){
+ final sqlite3 dbDest = createNewDb();
+
+ try (sqlite3 dbSrc = createNewDb()) {
+ execSql(dbSrc, new String[]{
+ "pragma page_size=512; VACUUM;",
+ "create table t(a);",
+ "insert into t(a) values(1),(2),(3);"
+ });
+ affirm( null==sqlite3_backup_init(dbSrc,"main",dbSrc,"main") );
+ try (sqlite3_backup b = sqlite3_backup_init(dbDest,"main",dbSrc,"main")) {
+ affirm( null!=b );
+ affirm( b.getNativePointer()!=0 );
+ int rc;
+ while( SQLITE_DONE!=(rc = sqlite3_backup_step(b, 1)) ){
+ affirm( 0==rc );
+ }
+ affirm( sqlite3_backup_pagecount(b) > 0 );
+ rc = sqlite3_backup_finish(b);
+ affirm( 0==rc );
+ affirm( b.getNativePointer()==0 );
+ }
+ }
+
+ try (sqlite3_stmt stmt = prepare(dbDest,"SELECT sum(a) from t")) {
+ sqlite3_step(stmt);
+ affirm( sqlite3_column_int(stmt,0) == 6 );
+ }
+ sqlite3_close_v2(dbDest);
+ }
+
+ private void testRandomness(){
+ byte[] foo = new byte[20];
+ int i = 0;
+ for( byte b : foo ){
+ i += b;
+ }
+ affirm( i==0 );
+ sqlite3_randomness(foo);
+ for( byte b : foo ){
+ if(b!=0) ++i;
+ }
+ affirm( i!=0, "There's a very slight chance that 0 is actually correct." );
+ }
+
+ private void testBlobOpen(){
+ final sqlite3 db = createNewDb();
+
+ execSql(db, "CREATE TABLE T(a BLOB);"
+ +"INSERT INTO t(rowid,a) VALUES(1, 'def'),(2, 'XYZ');"
+ );
+ final OutputPointer.sqlite3_blob pOut = new OutputPointer.sqlite3_blob();
+ int rc = sqlite3_blob_open(db, "main", "t", "a",
+ sqlite3_last_insert_rowid(db), 1, pOut);
+ affirm( 0==rc );
+ sqlite3_blob b = pOut.take();
+ affirm( null!=b );
+ affirm( 0!=b.getNativePointer() );
+ affirm( 3==sqlite3_blob_bytes(b) );
+ rc = sqlite3_blob_write( b, new byte[] {100, 101, 102 /*"DEF"*/}, 0);
+ affirm( 0==rc );
+ rc = sqlite3_blob_close(b);
+ affirm( 0==rc );
+ rc = sqlite3_blob_close(b);
+ affirm( 0!=rc );
+ affirm( 0==b.getNativePointer() );
+ sqlite3_stmt stmt = prepare(db,"SELECT length(a), a FROM t ORDER BY a");
+ affirm( SQLITE_ROW == sqlite3_step(stmt) );
+ affirm( 3 == sqlite3_column_int(stmt,0) );
+ affirm( "def".equals(sqlite3_column_text16(stmt,1)) );
+ sqlite3_finalize(stmt);
+
+ b = sqlite3_blob_open(db, "main", "t", "a",
+ sqlite3_last_insert_rowid(db), 1);
+ affirm( null!=b );
+ rc = sqlite3_blob_reopen(b, 2);
+ affirm( 0==rc );
+ final byte[] tgt = new byte[3];
+ rc = sqlite3_blob_read(b, tgt, 0);
+ affirm( 0==rc );
+ affirm( 100==tgt[0] && 101==tgt[1] && 102==tgt[2], "DEF" );
+ rc = sqlite3_blob_close(b);
+ affirm( 0==rc );
+ sqlite3_close_v2(db);
+ }
+
+ private void testPrepareMulti(){
+ final sqlite3 db = createNewDb();
+ final String[] sql = {
+ "create table t(","a)",
+ "; insert into t(a) values(1),(2),(3);",
+ "select a from t;"
+ };
+ final List liStmt = new ArrayList();
+ final PrepareMultiCallback proxy = new PrepareMultiCallback.StepAll();
+ PrepareMultiCallback m = new PrepareMultiCallback() {
+ @Override public int call(sqlite3_stmt st){
+ liStmt.add(st);
+ return proxy.call(st);
+ }
+ };
+ int rc = sqlite3_prepare_multi(db, sql, m);
+ affirm( 0==rc );
+ affirm( liStmt.size() == 3 );
+ for( sqlite3_stmt st : liStmt ){
+ sqlite3_finalize(st);
+ }
+ sqlite3_close_v2(db);
+ }
+
+ /* Copy/paste/rename this to add new tests. */
+ private void _testTemplate(){
+ final sqlite3 db = createNewDb();
+ sqlite3_stmt stmt = prepare(db,"SELECT 1");
+ sqlite3_finalize(stmt);
+ sqlite3_close_v2(db);
+ }
+
+
+ @ManualTest /* we really only want to run this test manually */
+ private void testSleep(){
+ out("Sleeping briefly... ");
+ sqlite3_sleep(600);
+ outln("Woke up.");
+ }
+
+ private void nap() throws InterruptedException {
+ if( takeNaps ){
+ Thread.sleep(java.util.concurrent.ThreadLocalRandom.current().nextInt(3, 17), 0);
+ }
+ }
+
+ @ManualTest /* because we only want to run this test on demand */
+ private void testFail(){
+ affirm( false, "Intentional failure." );
+ }
+
+ private void runTests(boolean fromThread) throws Exception {
+ if(false) showCompileOption();
+ List mlist = testMethods;
+ affirm( null!=mlist );
+ if( shuffle ){
+ mlist = new ArrayList<>( testMethods.subList(0, testMethods.size()) );
+ java.util.Collections.shuffle(mlist);
+ }
+ if( listRunTests ){
+ synchronized(this.getClass()){
+ if( !fromThread ){
+ out("Initial test"," list: ");
+ for(java.lang.reflect.Method m : testMethods){
+ out(m.getName()+" ");
+ }
+ outln();
+ outln("(That list excludes some which are hard-coded to run.)");
+ }
+ out("Running"," tests: ");
+ for(java.lang.reflect.Method m : mlist){
+ out(m.getName()+" ");
+ }
+ outln();
+ }
+ }
+ for(java.lang.reflect.Method m : mlist){
+ nap();
+ try{
+ m.invoke(this);
+ }catch(java.lang.reflect.InvocationTargetException e){
+ outln("FAILURE: ",m.getName(),"(): ", e.getCause());
+ throw e;
+ }
+ }
+ synchronized( this.getClass() ){
+ ++nTestRuns;
+ }
+ }
+
+ public void run() {
+ try {
+ runTests(0!=this.tId);
+ }catch(Exception e){
+ synchronized( listErrors ){
+ listErrors.add(e);
+ }
+ }finally{
+ affirm( sqlite3_java_uncache_thread() );
+ affirm( !sqlite3_java_uncache_thread() );
+ }
+ }
+
+ /**
+ Runs the basic sqlite3 JNI binding sanity-check suite.
+
+ CLI flags:
+
+ -q|-quiet: disables most test output.
+
+ -t|-thread N: runs the tests in N threads
+ concurrently. Default=1.
+
+ -r|-repeat N: repeats the tests in a loop N times, each one
+ consisting of the -thread value's threads.
+
+ -shuffle: randomizes the order of most of the test functions.
+
+ -naps: sleep small random intervals between tests in order to add
+ some chaos for cross-thread contention.
+
+ -list-tests: outputs the list of tests being run, minus some
+ which are hard-coded. This is noisy in multi-threaded mode.
+
+ -fail: forces an exception to be thrown during the test run. Use
+ with -shuffle to make its appearance unpredictable.
+
+ -v: emit some developer-mode info at the end.
+ */
+ public static void main(String[] args) throws Exception {
+ Integer nThread = 1;
+ boolean doSomethingForDev = false;
+ Integer nRepeat = 1;
+ boolean forceFail = false;
+ boolean sqlLog = false;
+ boolean configLog = false;
+ boolean squelchTestOutput = false;
+ for( int i = 0; i < args.length; ){
+ String arg = args[i++];
+ if(arg.startsWith("-")){
+ arg = arg.replaceFirst("-+","");
+ if(arg.equals("v")){
+ doSomethingForDev = true;
+ //listBoundMethods();
+ }else if(arg.equals("t") || arg.equals("thread")){
+ nThread = Integer.parseInt(args[i++]);
+ }else if(arg.equals("r") || arg.equals("repeat")){
+ nRepeat = Integer.parseInt(args[i++]);
+ }else if(arg.equals("shuffle")){
+ shuffle = true;
+ }else if(arg.equals("list-tests")){
+ listRunTests = true;
+ }else if(arg.equals("fail")){
+ forceFail = true;
+ }else if(arg.equals("sqllog")){
+ sqlLog = true;
+ }else if(arg.equals("configlog")){
+ configLog = true;
+ }else if(arg.equals("naps")){
+ takeNaps = true;
+ }else if(arg.equals("q") || arg.equals("quiet")){
+ squelchTestOutput = true;
+ }else{
+ throw new IllegalArgumentException("Unhandled flag:"+arg);
+ }
+ }
+ }
+
+ if( sqlLog ){
+ if( sqlite3_compileoption_used("ENABLE_SQLLOG") ){
+ final ConfigSqllogCallback log = new ConfigSqllogCallback() {
+ @Override public void call(sqlite3 db, String msg, int op){
+ switch(op){
+ case 0: outln("Opening db: ",db); break;
+ case 1: outln("SQL ",db,": ",msg); break;
+ case 2: outln("Closing db: ",db); break;
+ }
+ }
+ };
+ int rc = sqlite3_config( log );
+ affirm( 0==rc );
+ rc = sqlite3_config( (ConfigSqllogCallback)null );
+ affirm( 0==rc );
+ rc = sqlite3_config( log );
+ affirm( 0==rc );
+ }else{
+ outln("WARNING: -sqllog is not active because library was built ",
+ "without SQLITE_ENABLE_SQLLOG.");
+ }
+ }
+ if( configLog ){
+ final ConfigLogCallback log = new ConfigLogCallback() {
+ @Override public void call(int code, String msg){
+ outln("ConfigLogCallback: ",ResultCode.getEntryForInt(code),": ", msg);
+ };
+ };
+ int rc = sqlite3_config( log );
+ affirm( 0==rc );
+ rc = sqlite3_config( (ConfigLogCallback)null );
+ affirm( 0==rc );
+ rc = sqlite3_config( log );
+ affirm( 0==rc );
+ }
+
+ quietMode = squelchTestOutput;
+ outln("If you just saw warning messages regarding CallStaticObjectMethod, ",
+ "you are very likely seeing the side effects of a known openjdk8 ",
+ "bug. It is unsightly but does not affect the library.");
+
+ {
+ // Build list of tests to run from the methods named test*().
+ testMethods = new ArrayList<>();
+ int nSkipped = 0;
+ for(final java.lang.reflect.Method m : Tester1.class.getDeclaredMethods()){
+ final String name = m.getName();
+ if( name.equals("testFail") ){
+ if( forceFail ){
+ testMethods.add(m);
+ }
+ }else if( !m.isAnnotationPresent( ManualTest.class ) ){
+ if( nThread>1 && m.isAnnotationPresent( SingleThreadOnly.class ) ){
+ if( 0==nSkipped++ ){
+ out("Skipping tests in multi-thread mode:");
+ }
+ out(" "+name+"()");
+ }else if( name.startsWith("test") ){
+ testMethods.add(m);
+ }
+ }
+ }
+ if( nSkipped>0 ) out("\n");
+ }
+
+ final long timeStart = System.currentTimeMillis();
+ int nLoop = 0;
+ switch( sqlite3_threadsafe() ){ /* Sanity checking */
+ case 0:
+ affirm( SQLITE_ERROR==sqlite3_config( SQLITE_CONFIG_SINGLETHREAD ),
+ "Could not switch to single-thread mode." );
+ affirm( SQLITE_ERROR==sqlite3_config( SQLITE_CONFIG_MULTITHREAD ),
+ "Could switch to multithread mode." );
+ affirm( SQLITE_ERROR==sqlite3_config( SQLITE_CONFIG_SERIALIZED ),
+ "Could not switch to serialized threading mode." );
+ outln("This is a single-threaded build. Not using threads.");
+ nThread = 1;
+ break;
+ case 1:
+ case 2:
+ affirm( 0==sqlite3_config( SQLITE_CONFIG_SINGLETHREAD ),
+ "Could not switch to single-thread mode." );
+ affirm( 0==sqlite3_config( SQLITE_CONFIG_MULTITHREAD ),
+ "Could not switch to multithread mode." );
+ affirm( 0==sqlite3_config( SQLITE_CONFIG_SERIALIZED ),
+ "Could not switch to serialized threading mode." );
+ break;
+ default:
+ affirm( false, "Unhandled SQLITE_THREADSAFE value." );
+ }
+ outln("libversion_number: ",
+ sqlite3_libversion_number(),"\n",
+ sqlite3_libversion(),"\n",SQLITE_SOURCE_ID,"\n",
+ "SQLITE_THREADSAFE=",sqlite3_threadsafe());
+ final boolean showLoopCount = (nRepeat>1 && nThread>1);
+ if( showLoopCount ){
+ outln("Running ",nRepeat," loop(s) with ",nThread," thread(s) each.");
+ }
+ if( takeNaps ) outln("Napping between tests is enabled.");
+ for( int n = 0; n < nRepeat; ++n ){
+ ++nLoop;
+ if( showLoopCount ) out((1==nLoop ? "" : " ")+nLoop);
+ if( nThread<=1 ){
+ new Tester1(0).runTests(false);
+ continue;
+ }
+ Tester1.mtMode = true;
+ final ExecutorService ex = Executors.newFixedThreadPool( nThread );
+ for( int i = 0; i < nThread; ++i ){
+ ex.submit( new Tester1(i), i );
+ }
+ ex.shutdown();
+ try{
+ ex.awaitTermination(nThread*200, java.util.concurrent.TimeUnit.MILLISECONDS);
+ ex.shutdownNow();
+ }catch (InterruptedException ie){
+ ex.shutdownNow();
+ Thread.currentThread().interrupt();
+ }
+ if( !listErrors.isEmpty() ){
+ quietMode = false;
+ outln("TEST ERRORS:");
+ Exception err = null;
+ for( Exception e : listErrors ){
+ e.printStackTrace();
+ if( null==err ) err = e;
+ }
+ if( null!=err ) throw err;
+ }
+ }
+ if( showLoopCount ) outln();
+ quietMode = false;
+
+ final long timeEnd = System.currentTimeMillis();
+ outln("Tests done. Metrics across ",nTestRuns," total iteration(s):");
+ outln("\tAssertions checked: ",affirmCount);
+ outln("\tDatabases opened: ",metrics.dbOpen);
+ if( doSomethingForDev ){
+ sqlite3_jni_internal_details();
+ }
+ affirm( 0==sqlite3_release_memory(1) );
+ sqlite3_shutdown();
+ int nMethods = 0;
+ int nNatives = 0;
+ final java.lang.reflect.Method[] declaredMethods =
+ CApi.class.getDeclaredMethods();
+ for(java.lang.reflect.Method m : declaredMethods){
+ final int mod = m.getModifiers();
+ if( 0!=(mod & java.lang.reflect.Modifier.STATIC) ){
+ final String name = m.getName();
+ if(name.startsWith("sqlite3_")){
+ ++nMethods;
+ if( 0!=(mod & java.lang.reflect.Modifier.NATIVE) ){
+ ++nNatives;
+ }
+ }
+ }
+ }
+ outln("\tCApi.sqlite3_*() methods: "+
+ nMethods+" total, with "+
+ nNatives+" native, "+
+ (nMethods - nNatives)+" Java"
+ );
+ outln("\tTotal test time = "
+ +(timeEnd - timeStart)+"ms");
+ }
+}
diff --git a/ext/jni/src/org/sqlite/jni/capi/TraceV2Callback.java b/ext/jni/src/org/sqlite/jni/capi/TraceV2Callback.java
new file mode 100644
index 0000000000..56465a2c0a
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/TraceV2Callback.java
@@ -0,0 +1,50 @@
+/*
+** 2023-08-25
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+import org.sqlite.jni.annotation.Nullable;
+
+/**
+ Callback for use with {@link CApi#sqlite3_trace_v2}.
+*/
+public interface TraceV2Callback extends CallbackProxy {
+ /**
+ Called by sqlite3 for various tracing operations, as per
+ sqlite3_trace_v2(). Note that this interface elides the 2nd
+ argument to the native trace callback, as that role is better
+ filled by instance-local state.
+
+ These callbacks may throw, in which case their exceptions are
+ converted to C-level error information.
+
+
The 2nd argument to this function, if non-null, will be a an
+ sqlite3 or sqlite3_stmt object, depending on the first argument
+ (see below).
+
+
The final argument to this function is the "X" argument
+ documented for sqlite3_trace() and sqlite3_trace_v2(). Its type
+ depends on value of the first argument:
+
+
- SQLITE_TRACE_STMT: pNative is a sqlite3_stmt. pX is a String
+ containing the prepared SQL.
+
+
- SQLITE_TRACE_PROFILE: pNative is a sqlite3_stmt. pX is a Long
+ holding an approximate number of nanoseconds the statement took
+ to run.
+
+
- SQLITE_TRACE_ROW: pNative is a sqlite3_stmt. pX is null.
+
+
- SQLITE_TRACE_CLOSE: pNative is a sqlite3. pX is null.
+ */
+ int call(int traceFlag, Object pNative, @Nullable Object pX);
+}
diff --git a/ext/jni/src/org/sqlite/jni/capi/UpdateHookCallback.java b/ext/jni/src/org/sqlite/jni/capi/UpdateHookCallback.java
new file mode 100644
index 0000000000..33d72a5dd2
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/UpdateHookCallback.java
@@ -0,0 +1,25 @@
+/*
+** 2023-08-25
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+
+/**
+ Callback for use with {@link CApi#sqlite3_update_hook}.
+*/
+public interface UpdateHookCallback extends CallbackProxy {
+ /**
+ Must function as described for the C-level sqlite3_update_hook()
+ callback.
+ */
+ void call(int opId, String dbName, String tableName, long rowId);
+}
diff --git a/ext/jni/src/org/sqlite/jni/ValueHolder.java b/ext/jni/src/org/sqlite/jni/capi/ValueHolder.java
similarity index 77%
rename from ext/jni/src/org/sqlite/jni/ValueHolder.java
rename to ext/jni/src/org/sqlite/jni/capi/ValueHolder.java
index 7f6a463ba5..b3f03ac867 100644
--- a/ext/jni/src/org/sqlite/jni/ValueHolder.java
+++ b/ext/jni/src/org/sqlite/jni/capi/ValueHolder.java
@@ -1,5 +1,5 @@
/*
-** 2023-07-21
+** 2023-10-16
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
@@ -9,12 +9,12 @@
** May you share freely, never taking more than you give.
**
*************************************************************************
-** This file is part of the JNI bindings for the sqlite3 C API.
+** This file contains a set of tests for the sqlite3 JNI bindings.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.capi;
/**
- A helper class which simply holds a single value. Its current use
+ A helper class which simply holds a single value. Its primary use
is for communicating values out of anonymous classes, as doing so
requires a "final" reference.
*/
diff --git a/ext/jni/src/org/sqlite/jni/capi/WindowFunction.java b/ext/jni/src/org/sqlite/jni/capi/WindowFunction.java
new file mode 100644
index 0000000000..eaf1bb9a35
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/WindowFunction.java
@@ -0,0 +1,39 @@
+/*
+** 2023-08-25
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+
+
+/**
+ A SQLFunction implementation for window functions. Note that
+ WindowFunction inherits from {@link AggregateFunction} and each
+ instance is required to implement the inherited abstract methods
+ from that class. See {@link AggregateFunction} for information on
+ managing the UDF's invocation-specific state.
+*/
+public abstract class WindowFunction extends AggregateFunction {
+
+ /**
+ As for the xInverse() argument of the C API's
+ sqlite3_create_window_function(). If this function throws, the
+ exception is not propagated and a warning might be emitted
+ to a debugging channel.
+ */
+ public abstract void xInverse(sqlite3_context cx, sqlite3_value[] args);
+
+ /**
+ As for the xValue() argument of the C API's sqlite3_create_window_function().
+ See xInverse() for the fate of any exceptions this throws.
+ */
+ public abstract void xValue(sqlite3_context cx);
+}
diff --git a/ext/jni/src/org/sqlite/jni/capi/XDestroyCallback.java b/ext/jni/src/org/sqlite/jni/capi/XDestroyCallback.java
new file mode 100644
index 0000000000..372e4ec8d0
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/XDestroyCallback.java
@@ -0,0 +1,37 @@
+/*
+** 2023-07-21
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file declares JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+
+/**
+ Callback for a hook called by SQLite when certain client-provided
+ state are destroyed. It gets its name from the pervasive use of
+ the symbol name xDestroy() for this purpose in the C API
+ documentation.
+*/
+public interface XDestroyCallback {
+ /**
+ Must perform any cleanup required by this object. Must not
+ throw. Must not call back into the sqlite3 API, else it might
+ invoke a deadlock.
+
+ WARNING: as a rule, it is never safe to register individual
+ instances with this interface multiple times in the
+ library. e.g., do not register the same CollationCallback with
+ multiple arities or names using sqlite3_create_collation(). If
+ this rule is violated, the library will eventually try to free
+ each individual reference, leading to memory corruption or a
+ crash via duplicate free().
+ */
+ public void xDestroy();
+}
diff --git a/ext/jni/src/org/sqlite/jni/capi/package-info.java b/ext/jni/src/org/sqlite/jni/capi/package-info.java
new file mode 100644
index 0000000000..127f380675
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/package-info.java
@@ -0,0 +1,89 @@
+/**
+ This package houses a JNI binding to the SQLite3 C API.
+
+ The primary interfaces are in {@link
+ org.sqlite.jni.capi.CApi}.
+
+ API Goals and Requirements
+
+
+
+ - A 1-to-1(-ish) mapping of the C API to Java via JNI, insofar
+ as cross-language semantics allow for. A closely-related goal is
+ that the C
+ documentation should be usable as-is, insofar as possible,
+ for most of the JNI binding. As a rule, undocumented symbols in
+ the Java interface behave as documented for their C API
+ counterpart. Only semantic differences and Java-specific features
+ are documented here.
+
+ - Support Java as far back as version 8 (2014).
+
+ - Environment-independent. Should work everywhere both Java and
+ SQLite3 do.
+
+ - No 3rd-party dependencies beyond the JDK. That includes no
+ build-level dependencies for specific IDEs and toolchains. We
+ welcome the addition of build files for arbitrary environments
+ insofar as they neither interfere with each other nor become a
+ maintenance burden for the sqlite developers.
+
+
+
+ Non-Goals
+
+
+
+ - Creation of high-level OO wrapper APIs. Clients are free to
+ create them off of the C-style API.
+
+ - Support for mixed-mode operation, where client code accesses
+ SQLite both via the Java-side API and the C API via their own
+ native code. In such cases, proxy functionalities (primarily
+ callback handler wrappers of all sorts) may fail because the
+ C-side use of the SQLite APIs will bypass those proxies.
+
+
+
+ State of this API
+
+ As of version 3.43, this software is in "tech preview" form. We
+ tentatively plan to stamp it as stable with the 3.44 release.
+
+ Threading Considerations
+
+ This API is, if built with SQLITE_THREADSAFE set to 1 or 2,
+ thread-safe, insofar as the C API guarantees, with some addenda:
+
+
+
+ - It is not legal to use Java-facing SQLite3 resource handles
+ (sqlite3, sqlite3_stmt, etc) from multiple threads concurrently,
+ nor to use any database-specific resources concurrently in a
+ thread separate from the one the database is currently in use
+ in. i.e. do not use a sqlite3_stmt in thread #2 when thread #1 is
+ using the database which prepared that handle.
+
+
Violating this will eventually corrupt the JNI-level bindings
+ between Java's and C's view of the database. This is a limitation
+ of the JNI bindings, not the lower-level library.
+
+
+ - It is legal to use a given handle, and database-specific
+ resources, across threads, so long as no two threads pass
+ resources owned by the same database into the library
+ concurrently.
+
+
+
+
+ Any number of threads may, of course, create and use any number
+ of database handles they wish. Care only needs to be taken when
+ those handles or their associated resources cross threads, or...
+
+ When built with SQLITE_THREADSAFE=0 then no threading guarantees
+ are provided and multi-threaded use of the library will provoke
+ undefined behavior.
+
+*/
+package org.sqlite.jni.capi;
diff --git a/ext/jni/src/org/sqlite/jni/sqlite3.java b/ext/jni/src/org/sqlite/jni/capi/sqlite3.java
similarity index 75%
rename from ext/jni/src/org/sqlite/jni/sqlite3.java
rename to ext/jni/src/org/sqlite/jni/capi/sqlite3.java
index cfc6c08d47..901317f0ef 100644
--- a/ext/jni/src/org/sqlite/jni/sqlite3.java
+++ b/ext/jni/src/org/sqlite/jni/capi/sqlite3.java
@@ -11,7 +11,7 @@
*************************************************************************
** This file is part of the JNI bindings for the sqlite3 C API.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.capi;
/**
A wrapper for communicating C-level (sqlite3*) instances with
@@ -19,19 +19,25 @@ package org.sqlite.jni;
simply provide a type-safe way to communicate it between Java
and C via JNI.
*/
-public final class sqlite3 extends NativePointerHolder {
+public final class sqlite3 extends NativePointerHolder
+ implements AutoCloseable {
+
// Only invoked from JNI
private sqlite3(){}
public String toString(){
- long ptr = getNativePointer();
+ final long ptr = getNativePointer();
if( 0==ptr ){
return sqlite3.class.getSimpleName()+"@null";
}
- String fn = SQLite3Jni.sqlite3_db_filename(this, "main");
+ final String fn = CApi.sqlite3_db_filename(this, "main");
return sqlite3.class.getSimpleName()
+"@"+String.format("0x%08x",ptr)
+"["+((null == fn) ? "" : fn)+"]"
;
}
+
+ @Override public void close(){
+ CApi.sqlite3_close_v2(this.clearNativePointer());
+ }
}
diff --git a/ext/jni/src/org/sqlite/jni/capi/sqlite3_backup.java b/ext/jni/src/org/sqlite/jni/capi/sqlite3_backup.java
new file mode 100644
index 0000000000..0ef75c17eb
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/sqlite3_backup.java
@@ -0,0 +1,31 @@
+/*
+** 2023-09-03
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+
+/**
+ A wrapper for passing C-level (sqlite3_backup*) instances around in
+ Java. These wrappers do not own their associated pointer, they
+ simply provide a type-safe way to communicate it between Java and C
+ via JNI.
+*/
+public final class sqlite3_backup extends NativePointerHolder
+ implements AutoCloseable {
+ // Only invoked from JNI.
+ private sqlite3_backup(){}
+
+ @Override public void close(){
+ CApi.sqlite3_backup_finish(this);
+ }
+
+}
diff --git a/ext/jni/src/org/sqlite/jni/capi/sqlite3_blob.java b/ext/jni/src/org/sqlite/jni/capi/sqlite3_blob.java
new file mode 100644
index 0000000000..1b96c18b06
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/sqlite3_blob.java
@@ -0,0 +1,31 @@
+/*
+** 2023-09-03
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+
+/**
+ A wrapper for passing C-level (sqlite3_blob*) instances around in
+ Java. These wrappers do not own their associated pointer, they
+ simply provide a type-safe way to communicate it between Java and C
+ via JNI.
+*/
+public final class sqlite3_blob extends NativePointerHolder
+ implements AutoCloseable {
+ // Only invoked from JNI.
+ private sqlite3_blob(){}
+
+ @Override public void close(){
+ CApi.sqlite3_blob_close(this.clearNativePointer());
+ }
+
+}
diff --git a/ext/jni/src/org/sqlite/jni/capi/sqlite3_context.java b/ext/jni/src/org/sqlite/jni/capi/sqlite3_context.java
new file mode 100644
index 0000000000..82ec49af16
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/capi/sqlite3_context.java
@@ -0,0 +1,79 @@
+/*
+** 2023-07-21
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the JNI bindings for the sqlite3 C API.
+*/
+package org.sqlite.jni.capi;
+
+/**
+ sqlite3_context instances are used in conjunction with user-defined
+ SQL functions (a.k.a. UDFs).
+*/
+public final class sqlite3_context extends NativePointerHolder {
+ private Long aggregateContext = null;
+
+ /**
+ getAggregateContext() corresponds to C's
+ sqlite3_aggregate_context(), with a slightly different interface
+ to account for cross-language differences. It serves the same
+ purposes in a slightly different way: it provides a key which is
+ stable across invocations of a UDF's callbacks, such that all
+ calls into those callbacks can determine which "set" of those
+ calls they belong to.
+
+ Note that use of this method is not a requirement for proper use
+ of this class. sqlite3_aggregate_context() can also be used.
+
+
If the argument is true and the aggregate context has not yet
+ been set up, it will be initialized and fetched on demand, else it
+ won't. The intent is that xStep(), xValue(), and xInverse()
+ methods pass true and xFinal() methods pass false.
+
+
This function treats numeric 0 as null, always returning null instead
+ of 0.
+
+
If this object is being used in the context of an aggregate or
+ window UDF, this function returns a non-0 value which is distinct
+ for each set of UDF callbacks from a single invocation of the
+ UDF, otherwise it returns 0. The returned value is only only
+ valid within the context of execution of a single SQL statement,
+ and must not be re-used by future invocations of the UDF in
+ different SQL statements.
+
+
Consider this SQL, where MYFUNC is a user-defined aggregate function:
+
+
{@code
+ SELECT MYFUNC(A), MYFUNC(B) FROM T;
+ }
+
+ The xStep() and xFinal() methods of the callback need to be able
+ to differentiate between those two invocations in order to
+ perform their work properly. The value returned by
+ getAggregateContext() will be distinct for each of those
+ invocations of MYFUNC() and is intended to be used as a lookup
+ key for mapping callback invocations to whatever client-defined
+ state is needed by the UDF.
+
+
There is one case where this will return null in the context
+ of an aggregate or window function: if the result set has no
+ rows, the UDF's xFinal() will be called without any other x...()
+ members having been called. In that one case, no aggregate
+ context key will have been generated. xFinal() implementations
+ need to be prepared to accept that condition as legal.
+ */
+ public synchronized Long getAggregateContext(boolean initIfNeeded){
+ if( aggregateContext==null ){
+ aggregateContext = CApi.sqlite3_aggregate_context(this, initIfNeeded);
+ if( !initIfNeeded && null==aggregateContext ) aggregateContext = 0L;
+ }
+ return (null==aggregateContext || 0!=aggregateContext) ? aggregateContext : null;
+ }
+}
diff --git a/ext/jni/src/org/sqlite/jni/sqlite3_stmt.java b/ext/jni/src/org/sqlite/jni/capi/sqlite3_stmt.java
similarity index 83%
rename from ext/jni/src/org/sqlite/jni/sqlite3_stmt.java
rename to ext/jni/src/org/sqlite/jni/capi/sqlite3_stmt.java
index d672301378..3b8b71f8a5 100644
--- a/ext/jni/src/org/sqlite/jni/sqlite3_stmt.java
+++ b/ext/jni/src/org/sqlite/jni/capi/sqlite3_stmt.java
@@ -11,7 +11,7 @@
*************************************************************************
** This file is part of the JNI bindings for the sqlite3 C API.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.capi;
/**
A wrapper for communicating C-level (sqlite3_stmt*) instances with
@@ -19,7 +19,12 @@ package org.sqlite.jni;
simply provide a type-safe way to communicate it between Java and C
via JNI.
*/
-public final class sqlite3_stmt extends NativePointerHolder {
+public final class sqlite3_stmt extends NativePointerHolder
+ implements AutoCloseable {
// Only invoked from JNI.
private sqlite3_stmt(){}
+
+ @Override public void close(){
+ CApi.sqlite3_finalize(this.clearNativePointer());
+ }
}
diff --git a/ext/jni/src/org/sqlite/jni/sqlite3_value.java b/ext/jni/src/org/sqlite/jni/capi/sqlite3_value.java
similarity index 95%
rename from ext/jni/src/org/sqlite/jni/sqlite3_value.java
rename to ext/jni/src/org/sqlite/jni/capi/sqlite3_value.java
index 2cfb32ff1a..a4772f0f63 100644
--- a/ext/jni/src/org/sqlite/jni/sqlite3_value.java
+++ b/ext/jni/src/org/sqlite/jni/capi/sqlite3_value.java
@@ -11,7 +11,7 @@
*************************************************************************
** This file is part of the JNI bindings for the sqlite3 C API.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.capi;
public final class sqlite3_value extends NativePointerHolder {
//! Invoked only from JNI.
diff --git a/ext/jni/src/org/sqlite/jni/Fts5.java b/ext/jni/src/org/sqlite/jni/fts5/Fts5.java
similarity index 67%
rename from ext/jni/src/org/sqlite/jni/Fts5.java
rename to ext/jni/src/org/sqlite/jni/fts5/Fts5.java
index 102cf575a8..0dceeafd2e 100644
--- a/ext/jni/src/org/sqlite/jni/Fts5.java
+++ b/ext/jni/src/org/sqlite/jni/fts5/Fts5.java
@@ -11,24 +11,18 @@
*************************************************************************
** This file is part of the JNI bindings for the sqlite3 C API.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.fts5;
/**
INCOMPLETE AND COMPLETELY UNTESTED.
- A wrapper for communicating C-level (fts5_api*) instances with
- Java. These wrappers do not own their associated pointer, they
- simply provide a type-safe way to communicate it between Java and C
- via JNI.
+ A utility object for holding FTS5-specific types and constants
+ which are used by multiple FTS5 classes.
*/
public final class Fts5 {
/* Not used */
private Fts5(){}
- //! Callback type for use with xTokenize() variants
- public static interface xTokenizeCallback {
- int xToken(int tFlags, byte txt[], int iStart, int iEnd);
- }
public static final int FTS5_TOKENIZE_QUERY = 0x0001;
public static final int FTS5_TOKENIZE_PREFIX = 0x0002;
diff --git a/ext/jni/src/org/sqlite/jni/Fts5Context.java b/ext/jni/src/org/sqlite/jni/fts5/Fts5Context.java
similarity index 92%
rename from ext/jni/src/org/sqlite/jni/Fts5Context.java
rename to ext/jni/src/org/sqlite/jni/fts5/Fts5Context.java
index e78f67d556..439b477910 100644
--- a/ext/jni/src/org/sqlite/jni/Fts5Context.java
+++ b/ext/jni/src/org/sqlite/jni/fts5/Fts5Context.java
@@ -11,7 +11,8 @@
*************************************************************************
** This file is part of the JNI bindings for the sqlite3 C API.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.fts5;
+import org.sqlite.jni.capi.*;
/**
A wrapper for communicating C-level (Fts5Context*) instances with
diff --git a/ext/jni/src/org/sqlite/jni/Fts5ExtensionApi.java b/ext/jni/src/org/sqlite/jni/fts5/Fts5ExtensionApi.java
similarity index 54%
rename from ext/jni/src/org/sqlite/jni/Fts5ExtensionApi.java
rename to ext/jni/src/org/sqlite/jni/fts5/Fts5ExtensionApi.java
index ac041e3001..594f3eaad6 100644
--- a/ext/jni/src/org/sqlite/jni/Fts5ExtensionApi.java
+++ b/ext/jni/src/org/sqlite/jni/fts5/Fts5ExtensionApi.java
@@ -11,76 +11,87 @@
*************************************************************************
** This file is part of the JNI bindings for the sqlite3 C API.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.fts5;
import java.nio.charset.StandardCharsets;
+import org.sqlite.jni.capi.*;
+import org.sqlite.jni.annotation.*;
/**
- ALMOST COMPLETELY UNTESTED.
-
- FAR FROM COMPLETE and the feasibility of binding this to Java
- is still undetermined. This might be removed.
*/
public final class Fts5ExtensionApi extends NativePointerHolder {
//! Only called from JNI
private Fts5ExtensionApi(){}
- private int iVersion = 2;
+ private final int iVersion = 2;
/* Callback type for used by xQueryPhrase(). */
- public static interface xQueryPhraseCallback {
- int xCallback(Fts5ExtensionApi fapi, Fts5Context cx);
+ public static interface XQueryPhraseCallback {
+ int call(Fts5ExtensionApi fapi, Fts5Context cx);
}
/**
Returns the singleton instance of this class.
*/
- public static synchronized native Fts5ExtensionApi getInstance();
+ public static native Fts5ExtensionApi getInstance();
- public synchronized native int xColumnCount(@NotNull Fts5Context fcx);
- public synchronized native int xColumnSize(@NotNull Fts5Context cx, int iCol,
+ public native int xColumnCount(@NotNull Fts5Context fcx);
+
+ public native int xColumnSize(@NotNull Fts5Context cx, int iCol,
@NotNull OutputPointer.Int32 pnToken);
- public synchronized native int xColumnText(@NotNull Fts5Context cx, int iCol,
+
+ public native int xColumnText(@NotNull Fts5Context cx, int iCol,
@NotNull OutputPointer.String txt);
- public synchronized native int xColumnTotalSize(@NotNull Fts5Context fcx, int iCol,
+
+ public native int xColumnTotalSize(@NotNull Fts5Context fcx, int iCol,
@NotNull OutputPointer.Int64 pnToken);
- public synchronized native Object xGetAuxdata(@NotNull Fts5Context cx, boolean clearIt);
- public synchronized native int xInst(@NotNull Fts5Context cx, int iIdx,
+
+ public native Object xGetAuxdata(@NotNull Fts5Context cx, boolean clearIt);
+
+ public native int xInst(@NotNull Fts5Context cx, int iIdx,
@NotNull OutputPointer.Int32 piPhrase,
@NotNull OutputPointer.Int32 piCol,
@NotNull OutputPointer.Int32 piOff);
- public synchronized native int xInstCount(@NotNull Fts5Context fcx,
+
+ public native int xInstCount(@NotNull Fts5Context fcx,
@NotNull OutputPointer.Int32 pnInst);
- public synchronized native int xPhraseCount(@NotNull Fts5Context fcx);
- public synchronized native int xPhraseFirst(@NotNull Fts5Context cx, int iPhrase,
+
+ public native int xPhraseCount(@NotNull Fts5Context fcx);
+
+ public native int xPhraseFirst(@NotNull Fts5Context cx, int iPhrase,
@NotNull Fts5PhraseIter iter,
@NotNull OutputPointer.Int32 iCol,
@NotNull OutputPointer.Int32 iOff);
- public synchronized native int xPhraseFirstColumn(@NotNull Fts5Context cx, int iPhrase,
+
+ public native int xPhraseFirstColumn(@NotNull Fts5Context cx, int iPhrase,
@NotNull Fts5PhraseIter iter,
@NotNull OutputPointer.Int32 iCol);
- public synchronized native void xPhraseNext(@NotNull Fts5Context cx,
+ public native void xPhraseNext(@NotNull Fts5Context cx,
@NotNull Fts5PhraseIter iter,
@NotNull OutputPointer.Int32 iCol,
@NotNull OutputPointer.Int32 iOff);
- public synchronized native void xPhraseNextColumn(@NotNull Fts5Context cx,
+ public native void xPhraseNextColumn(@NotNull Fts5Context cx,
@NotNull Fts5PhraseIter iter,
@NotNull OutputPointer.Int32 iCol);
- public synchronized native int xPhraseSize(@NotNull Fts5Context fcx, int iPhrase);
- public synchronized native int xQueryPhrase(@NotNull Fts5Context cx, int iPhrase,
- @NotNull xQueryPhraseCallback callback);
- public synchronized native int xRowCount(@NotNull Fts5Context fcx,
+ public native int xPhraseSize(@NotNull Fts5Context fcx, int iPhrase);
+
+ public native int xQueryPhrase(@NotNull Fts5Context cx, int iPhrase,
+ @NotNull XQueryPhraseCallback callback);
+ public native int xRowCount(@NotNull Fts5Context fcx,
@NotNull OutputPointer.Int64 nRow);
- public synchronized native long xRowid(@NotNull Fts5Context cx);
+
+ public native long xRowid(@NotNull Fts5Context cx);
/* Note that the JNI binding lacks the C version's xDelete()
callback argument. Instead, if pAux has an xDestroy() method, it
is called if the FTS5 API finalizes the aux state (including if
allocation of storage for the auxdata fails). Any reference to
pAux held by the JNI layer will be relinquished regardless of
whether pAux has an xDestroy() method. */
- public synchronized native int xSetAuxdata(@NotNull Fts5Context cx, @Nullable Object pAux);
- public synchronized native int xTokenize(@NotNull Fts5Context cx, @NotNull byte pText[],
- @NotNull Fts5.xTokenizeCallback callback);
- public synchronized native Object xUserData(Fts5Context cx);
+ public native int xSetAuxdata(@NotNull Fts5Context cx, @Nullable Object pAux);
+
+ public native int xTokenize(@NotNull Fts5Context cx, @NotNull byte[] pText,
+ @NotNull XTokenizeCallback callback);
+
+ public native Object xUserData(Fts5Context cx);
//^^^ returns the pointer passed as the 3rd arg to the C-level
- // fts5_api::xCreateFunction.
+ // fts5_api::xCreateFunction().
}
diff --git a/ext/jni/src/org/sqlite/jni/Fts5PhraseIter.java b/ext/jni/src/org/sqlite/jni/fts5/Fts5PhraseIter.java
similarity index 90%
rename from ext/jni/src/org/sqlite/jni/Fts5PhraseIter.java
rename to ext/jni/src/org/sqlite/jni/fts5/Fts5PhraseIter.java
index eb4e05fdf8..5774eb5936 100644
--- a/ext/jni/src/org/sqlite/jni/Fts5PhraseIter.java
+++ b/ext/jni/src/org/sqlite/jni/fts5/Fts5PhraseIter.java
@@ -11,7 +11,8 @@
*************************************************************************
** This file is part of the JNI bindings for the sqlite3 C API.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.fts5;
+import org.sqlite.jni.capi.NativePointerHolder;
/**
A wrapper for C-level Fts5PhraseIter. They are only modified and
diff --git a/ext/jni/src/org/sqlite/jni/Fts5Tokenizer.java b/ext/jni/src/org/sqlite/jni/fts5/Fts5Tokenizer.java
similarity index 92%
rename from ext/jni/src/org/sqlite/jni/Fts5Tokenizer.java
rename to ext/jni/src/org/sqlite/jni/fts5/Fts5Tokenizer.java
index 0d266a13d8..b72e5d0fc0 100644
--- a/ext/jni/src/org/sqlite/jni/Fts5Tokenizer.java
+++ b/ext/jni/src/org/sqlite/jni/fts5/Fts5Tokenizer.java
@@ -11,7 +11,8 @@
*************************************************************************
** This file is part of the JNI bindings for the sqlite3 C API.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.fts5;
+import org.sqlite.jni.capi.NativePointerHolder;
/**
INCOMPLETE AND COMPLETELY UNTESTED.
diff --git a/ext/jni/src/org/sqlite/jni/fts5/TesterFts5.java b/ext/jni/src/org/sqlite/jni/fts5/TesterFts5.java
new file mode 100644
index 0000000000..c4264c5417
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/fts5/TesterFts5.java
@@ -0,0 +1,832 @@
+/*
+** 2023-08-04
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file contains a set of tests for the sqlite3 JNI bindings.
+*/
+package org.sqlite.jni.fts5;
+import static org.sqlite.jni.capi.CApi.*;
+import static org.sqlite.jni.capi.Tester1.*;
+import org.sqlite.jni.capi.*;
+import org.sqlite.jni.fts5.*;
+
+import java.util.*;
+
+public class TesterFts5 {
+
+ private static void test1(){
+ final Fts5ExtensionApi fea = Fts5ExtensionApi.getInstance();
+ affirm( null != fea );
+ affirm( fea.getNativePointer() != 0 );
+ affirm( fea == Fts5ExtensionApi.getInstance() )/*singleton*/;
+
+ sqlite3 db = createNewDb();
+ fts5_api fApi = fts5_api.getInstanceForDb(db);
+ affirm( fApi != null );
+ affirm( fApi == fts5_api.getInstanceForDb(db) /* singleton per db */ );
+
+ execSql(db, new String[] {
+ "CREATE VIRTUAL TABLE ft USING fts5(a, b);",
+ "INSERT INTO ft(rowid, a, b) VALUES(1, 'X Y', 'Y Z');",
+ "INSERT INTO ft(rowid, a, b) VALUES(2, 'A Z', 'Y Y');"
+ });
+
+ final String pUserData = "This is pUserData";
+ final int outputs[] = {0, 0};
+ final fts5_extension_function func = new fts5_extension_function(){
+ @Override public void call(Fts5ExtensionApi ext, Fts5Context fCx,
+ sqlite3_context pCx, sqlite3_value argv[]){
+ final int nCols = ext.xColumnCount(fCx);
+ affirm( 2 == nCols );
+ affirm( nCols == argv.length );
+ affirm( ext.xUserData(fCx) == pUserData );
+ final OutputPointer.String op = new OutputPointer.String();
+ final OutputPointer.Int32 colsz = new OutputPointer.Int32();
+ final OutputPointer.Int64 colTotalSz = new OutputPointer.Int64();
+ for(int i = 0; i < nCols; ++i ){
+ int rc = ext.xColumnText(fCx, i, op);
+ affirm( 0 == rc );
+ final String val = op.value;
+ affirm( val.equals(sqlite3_value_text16(argv[i])) );
+ rc = ext.xColumnSize(fCx, i, colsz);
+ affirm( 0==rc );
+ affirm( 3==sqlite3_value_bytes(argv[i]) );
+ rc = ext.xColumnTotalSize(fCx, i, colTotalSz);
+ affirm( 0==rc );
+ }
+ ++outputs[0];
+ }
+ public void xDestroy(){
+ outputs[1] = 1;
+ }
+ };
+
+ int rc = fApi.xCreateFunction("myaux", pUserData, func);
+ affirm( 0==rc );
+
+ affirm( 0==outputs[0] );
+ execSql(db, "select myaux(ft,a,b) from ft;");
+ affirm( 2==outputs[0] );
+ affirm( 0==outputs[1] );
+ sqlite3_close_v2(db);
+ affirm( 1==outputs[1] );
+ }
+
+ /*
+ ** Argument sql is a string containing one or more SQL statements
+ ** separated by ";" characters. This function executes each of these
+ ** statements against the database passed as the first argument. If
+ ** no error occurs, the results of the SQL script are returned as
+ ** an array of strings. If an error does occur, a RuntimeException is
+ ** thrown.
+ */
+ private static String[] sqlite3_exec(sqlite3 db, String sql) {
+ List aOut = new ArrayList();
+
+ /* Iterate through the list of SQL statements. For each, step through
+ ** it and add any results to the aOut[] array. */
+ int rc = sqlite3_prepare_multi(db, sql, new PrepareMultiCallback() {
+ @Override public int call(sqlite3_stmt pStmt){
+ while( SQLITE_ROW==sqlite3_step(pStmt) ){
+ int ii;
+ for(ii=0; ii, );
+ */
+ class fts5_aux implements fts5_extension_function {
+ @Override public void call(
+ Fts5ExtensionApi ext,
+ Fts5Context fCx,
+ sqlite3_context pCx,
+ sqlite3_value argv[]
+ ){
+ if( argv.length>1 ){
+ throw new RuntimeException("fts5_aux: wrong number of args");
+ }
+
+ boolean bClear = (argv.length==1);
+ Object obj = ext.xGetAuxdata(fCx, bClear);
+ if( obj instanceof String ){
+ sqlite3_result_text16(pCx, (String)obj);
+ }
+
+ if( argv.length==1 ){
+ String val = sqlite3_value_text16(argv[0]);
+ if( !val.equals("") ){
+ ext.xSetAuxdata(fCx, val);
+ }
+ }
+ }
+ public void xDestroy(){ }
+ };
+
+ /*
+ ** fts5_inst();
+ **
+ ** This is used to test the xInstCount() and xInst() APIs. It returns a
+ ** text value containing a Tcl list with xInstCount() elements. Each
+ ** element is itself a list of 3 integers - the phrase number, column
+ ** number and token offset returned by each call to xInst().
+ */
+ fts5_extension_function fts5_inst = new fts5_extension_function(){
+ @Override public void call(
+ Fts5ExtensionApi ext,
+ Fts5Context fCx,
+ sqlite3_context pCx,
+ sqlite3_value argv[]
+ ){
+ if( argv.length!=0 ){
+ throw new RuntimeException("fts5_inst: wrong number of args");
+ }
+
+ OutputPointer.Int32 pnInst = new OutputPointer.Int32();
+ OutputPointer.Int32 piPhrase = new OutputPointer.Int32();
+ OutputPointer.Int32 piCol = new OutputPointer.Int32();
+ OutputPointer.Int32 piOff = new OutputPointer.Int32();
+ String ret = new String();
+
+ int rc = ext.xInstCount(fCx, pnInst);
+ int nInst = pnInst.get();
+ int ii;
+
+ for(ii=0; rc==SQLITE_OK && ii0 ) ret += " ";
+ ret += "{"+piPhrase.get()+" "+piCol.get()+" "+piOff.get()+"}";
+ }
+
+ sqlite3_result_text(pCx, ret);
+ }
+ public void xDestroy(){ }
+ };
+
+ /*
+ ** fts5_pinst();
+ **
+ ** Like SQL function fts5_inst(), except using the following
+ **
+ ** xPhraseCount
+ ** xPhraseFirst
+ ** xPhraseNext
+ */
+ fts5_extension_function fts5_pinst = new fts5_extension_function(){
+ @Override public void call(
+ Fts5ExtensionApi ext,
+ Fts5Context fCx,
+ sqlite3_context pCx,
+ sqlite3_value argv[]
+ ){
+ if( argv.length!=0 ){
+ throw new RuntimeException("fts5_pinst: wrong number of args");
+ }
+
+ OutputPointer.Int32 piCol = new OutputPointer.Int32();
+ OutputPointer.Int32 piOff = new OutputPointer.Int32();
+ String ret = new String();
+ int rc = SQLITE_OK;
+
+ int nPhrase = ext.xPhraseCount(fCx);
+ int ii;
+
+ for(ii=0; rc==SQLITE_OK && ii=0;
+ ext.xPhraseNext(fCx, pIter, piCol, piOff)
+ ){
+ if( !ret.equals("") ) ret += " ";
+ ret += "{"+ii+" "+piCol.get()+" "+piOff.get()+"}";
+ }
+ }
+
+ if( rc!=SQLITE_OK ){
+ throw new RuntimeException("fts5_pinst: rc=" + rc);
+ }else{
+ sqlite3_result_text(pCx, ret);
+ }
+ }
+ public void xDestroy(){ }
+ };
+
+ /*
+ ** fts5_pcolinst();
+ **
+ ** Like SQL function fts5_pinst(), except using the following
+ **
+ ** xPhraseFirstColumn
+ ** xPhraseNextColumn
+ */
+ fts5_extension_function fts5_pcolinst = new fts5_extension_function(){
+ @Override public void call(
+ Fts5ExtensionApi ext,
+ Fts5Context fCx,
+ sqlite3_context pCx,
+ sqlite3_value argv[]
+ ){
+ if( argv.length!=0 ){
+ throw new RuntimeException("fts5_pcolinst: wrong number of args");
+ }
+
+ OutputPointer.Int32 piCol = new OutputPointer.Int32();
+ String ret = new String();
+ int rc = SQLITE_OK;
+
+ int nPhrase = ext.xPhraseCount(fCx);
+ int ii;
+
+ for(ii=0; rc==SQLITE_OK && ii=0;
+ ext.xPhraseNextColumn(fCx, pIter, piCol)
+ ){
+ if( !ret.equals("") ) ret += " ";
+ ret += "{"+ii+" "+piCol.get()+"}";
+ }
+ }
+
+ if( rc!=SQLITE_OK ){
+ throw new RuntimeException("fts5_pcolinst: rc=" + rc);
+ }else{
+ sqlite3_result_text(pCx, ret);
+ }
+ }
+ public void xDestroy(){ }
+ };
+
+ /*
+ ** fts5_rowcount();
+ */
+ fts5_extension_function fts5_rowcount = new fts5_extension_function(){
+ @Override public void call(
+ Fts5ExtensionApi ext,
+ Fts5Context fCx,
+ sqlite3_context pCx,
+ sqlite3_value argv[]
+ ){
+ if( argv.length!=0 ){
+ throw new RuntimeException("fts5_rowcount: wrong number of args");
+ }
+ OutputPointer.Int64 pnRow = new OutputPointer.Int64();
+
+ int rc = ext.xRowCount(fCx, pnRow);
+ if( rc==SQLITE_OK ){
+ sqlite3_result_int64(pCx, pnRow.get());
+ }else{
+ throw new RuntimeException("fts5_rowcount: rc=" + rc);
+ }
+ }
+ public void xDestroy(){ }
+ };
+
+ /*
+ ** fts5_phrasesize();
+ */
+ fts5_extension_function fts5_phrasesize = new fts5_extension_function(){
+ @Override public void call(
+ Fts5ExtensionApi ext,
+ Fts5Context fCx,
+ sqlite3_context pCx,
+ sqlite3_value argv[]
+ ){
+ if( argv.length!=1 ){
+ throw new RuntimeException("fts5_phrasesize: wrong number of args");
+ }
+ int iPhrase = sqlite3_value_int(argv[0]);
+
+ int sz = ext.xPhraseSize(fCx, iPhrase);
+ sqlite3_result_int(pCx, sz);
+ }
+ public void xDestroy(){ }
+ };
+
+ /*
+ ** fts5_phrasehits(, );
+ **
+ ** Use the xQueryPhrase() API to determine how many hits, in total,
+ ** there are for phrase in the database.
+ */
+ fts5_extension_function fts5_phrasehits = new fts5_extension_function(){
+ @Override public void call(
+ Fts5ExtensionApi ext,
+ Fts5Context fCx,
+ sqlite3_context pCx,
+ sqlite3_value argv[]
+ ){
+ if( argv.length!=1 ){
+ throw new RuntimeException("fts5_phrasesize: wrong number of args");
+ }
+ int iPhrase = sqlite3_value_int(argv[0]);
+ int rc = SQLITE_OK;
+
+ class MyCallback implements Fts5ExtensionApi.XQueryPhraseCallback {
+ public int nRet = 0;
+ public int getRet() { return nRet; }
+
+ @Override
+ public int call(Fts5ExtensionApi fapi, Fts5Context cx){
+ OutputPointer.Int32 pnInst = new OutputPointer.Int32();
+ int rc = fapi.xInstCount(cx, pnInst);
+ nRet += pnInst.get();
+ return rc;
+ }
+ };
+
+ MyCallback xCall = new MyCallback();
+ rc = ext.xQueryPhrase(fCx, iPhrase, xCall);
+ if( rc!=SQLITE_OK ){
+ throw new RuntimeException("fts5_phrasehits: rc=" + rc);
+ }
+ sqlite3_result_int(pCx, xCall.getRet());
+ }
+ public void xDestroy(){ }
+ };
+
+ /*
+ ** fts5_tokenize(, )
+ */
+ fts5_extension_function fts5_tokenize = new fts5_extension_function(){
+ @Override public void call(
+ Fts5ExtensionApi ext,
+ Fts5Context fCx,
+ sqlite3_context pCx,
+ sqlite3_value argv[]
+ ){
+ if( argv.length!=1 ){
+ throw new RuntimeException("fts5_tokenize: wrong number of args");
+ }
+ byte[] utf8 = sqlite3_value_text(argv[0]);
+ int rc = SQLITE_OK;
+
+ class MyCallback implements XTokenizeCallback {
+ private List myList = new ArrayList();
+
+ public String getval() {
+ return String.join("+", myList);
+ }
+
+ @Override
+ public int call(int tFlags, byte[] txt, int iStart, int iEnd){
+ try {
+ String str = new String(txt, "UTF-8");
+ myList.add(str);
+ } catch (Exception e) {
+ }
+ return SQLITE_OK;
+ }
+ };
+
+ MyCallback xCall = new MyCallback();
+ ext.xTokenize(fCx, utf8, xCall);
+ sqlite3_result_text16(pCx, xCall.getval());
+
+ if( rc!=SQLITE_OK ){
+ throw new RuntimeException("fts5_tokenize: rc=" + rc);
+ }
+ }
+ public void xDestroy(){ }
+ };
+
+ fts5_api api = fts5_api.getInstanceForDb(db);
+ api.xCreateFunction("fts5_rowid", fts5_rowid);
+ api.xCreateFunction("fts5_columncount", fts5_columncount);
+ api.xCreateFunction("fts5_columnsize", fts5_columnsize);
+ api.xCreateFunction("fts5_columntext", fts5_columntext);
+ api.xCreateFunction("fts5_columntotalsize", fts5_columntsize);
+
+ api.xCreateFunction("fts5_aux1", new fts5_aux());
+ api.xCreateFunction("fts5_aux2", new fts5_aux());
+
+ api.xCreateFunction("fts5_inst", fts5_inst);
+ api.xCreateFunction("fts5_pinst", fts5_pinst);
+ api.xCreateFunction("fts5_pcolinst", fts5_pcolinst);
+ api.xCreateFunction("fts5_rowcount", fts5_rowcount);
+ api.xCreateFunction("fts5_phrasesize", fts5_phrasesize);
+ api.xCreateFunction("fts5_phrasehits", fts5_phrasehits);
+ api.xCreateFunction("fts5_tokenize", fts5_tokenize);
+ }
+ /*
+ ** Test of various Fts5ExtensionApi methods
+ */
+ private static void test2(){
+
+ /* Open db and populate an fts5 table */
+ sqlite3 db = createNewDb();
+ do_execsql_test(db,
+ "CREATE VIRTUAL TABLE ft USING fts5(a, b);" +
+ "INSERT INTO ft(rowid, a, b) VALUES(-9223372036854775808, 'x', 'x');" +
+ "INSERT INTO ft(rowid, a, b) VALUES(0, 'x', 'x');" +
+ "INSERT INTO ft(rowid, a, b) VALUES(1, 'x y z', 'x y z');" +
+ "INSERT INTO ft(rowid, a, b) VALUES(2, 'x y z', 'x z');" +
+ "INSERT INTO ft(rowid, a, b) VALUES(3, 'x y z', 'x y z');" +
+ "INSERT INTO ft(rowid, a, b) VALUES(9223372036854775807, 'x', 'x');"
+ );
+
+ create_test_functions(db);
+
+ /* Test that fts5_rowid() seems to work */
+ do_execsql_test(db,
+ "SELECT rowid==fts5_rowid(ft) FROM ft('x')",
+ "[1, 1, 1, 1, 1, 1]"
+ );
+
+ /* Test fts5_columncount() */
+ do_execsql_test(db,
+ "SELECT fts5_columncount(ft) FROM ft('x')",
+ "[2, 2, 2, 2, 2, 2]"
+ );
+
+ /* Test fts5_columnsize() */
+ do_execsql_test(db,
+ "SELECT fts5_columnsize(ft, 0) FROM ft('x') ORDER BY rowid",
+ "[1, 1, 3, 3, 3, 1]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_columnsize(ft, 1) FROM ft('x') ORDER BY rowid",
+ "[1, 1, 3, 2, 3, 1]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_columnsize(ft, -1) FROM ft('x') ORDER BY rowid",
+ "[2, 2, 6, 5, 6, 2]"
+ );
+
+ /* Test that xColumnSize() returns SQLITE_RANGE if the column number
+ ** is out-of range */
+ try {
+ do_execsql_test(db,
+ "SELECT fts5_columnsize(ft, 2) FROM ft('x') ORDER BY rowid"
+ );
+ } catch( RuntimeException e ){
+ affirm( e.getMessage().matches(".*column index out of range") );
+ }
+
+ /* Test fts5_columntext() */
+ do_execsql_test(db,
+ "SELECT fts5_columntext(ft, 0) FROM ft('x') ORDER BY rowid",
+ "[x, x, x y z, x y z, x y z, x]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_columntext(ft, 1) FROM ft('x') ORDER BY rowid",
+ "[x, x, x y z, x z, x y z, x]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_columntext(ft, 2) FROM ft('x') ORDER BY rowid",
+ "[null, null, null, null, null, null]"
+ );
+
+ /* Test fts5_columntotalsize() */
+ do_execsql_test(db,
+ "SELECT fts5_columntotalsize(ft, 0) FROM ft('x') ORDER BY rowid",
+ "[12, 12, 12, 12, 12, 12]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_columntotalsize(ft, 1) FROM ft('x') ORDER BY rowid",
+ "[11, 11, 11, 11, 11, 11]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_columntotalsize(ft, -1) FROM ft('x') ORDER BY rowid",
+ "[23, 23, 23, 23, 23, 23]"
+ );
+
+ /* Test that xColumnTotalSize() returns SQLITE_RANGE if the column
+ ** number is out-of range */
+ try {
+ do_execsql_test(db,
+ "SELECT fts5_columntotalsize(ft, 2) FROM ft('x') ORDER BY rowid"
+ );
+ } catch( RuntimeException e ){
+ affirm( e.getMessage().matches(".*column index out of range") );
+ }
+
+ do_execsql_test(db,
+ "SELECT rowid, fts5_rowcount(ft) FROM ft('z')",
+ "[1, 6, 2, 6, 3, 6]"
+ );
+
+ sqlite3_close_v2(db);
+ }
+
+ /*
+ ** Test of various Fts5ExtensionApi methods
+ */
+ private static void test3(){
+
+ /* Open db and populate an fts5 table */
+ sqlite3 db = createNewDb();
+ do_execsql_test(db,
+ "CREATE VIRTUAL TABLE ft USING fts5(a, b);" +
+ "INSERT INTO ft(a, b) VALUES('the one', 1);" +
+ "INSERT INTO ft(a, b) VALUES('the two', 2);" +
+ "INSERT INTO ft(a, b) VALUES('the three', 3);" +
+ "INSERT INTO ft(a, b) VALUES('the four', '');"
+ );
+ create_test_functions(db);
+
+ /* Test fts5_aux1() + fts5_aux2() - users of xGetAuxdata and xSetAuxdata */
+ do_execsql_test(db,
+ "SELECT fts5_aux1(ft, a) FROM ft('the')",
+ "[null, the one, the two, the three]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_aux2(ft, b) FROM ft('the')",
+ "[null, 1, 2, 3]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_aux1(ft, a), fts5_aux2(ft, b) FROM ft('the')",
+ "[null, null, the one, 1, the two, 2, the three, 3]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_aux1(ft, b), fts5_aux1(ft) FROM ft('the')",
+ "[null, 1, 1, 2, 2, 3, 3, null]"
+ );
+ }
+
+ /*
+ ** Test of various Fts5ExtensionApi methods
+ */
+ private static void test4(){
+
+ /* Open db and populate an fts5 table */
+ sqlite3 db = createNewDb();
+ create_test_functions(db);
+ do_execsql_test(db,
+ "CREATE VIRTUAL TABLE ft USING fts5(a, b);" +
+ "INSERT INTO ft(a, b) VALUES('one two three', 'two three four');" +
+ "INSERT INTO ft(a, b) VALUES('two three four', 'three four five');" +
+ "INSERT INTO ft(a, b) VALUES('three four five', 'four five six');"
+ );
+
+
+ do_execsql_test(db,
+ "SELECT fts5_inst(ft) FROM ft('two')",
+ "[{0 0 1} {0 1 0}, {0 0 0}]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_inst(ft) FROM ft('four')",
+ "[{0 1 2}, {0 0 2} {0 1 1}, {0 0 1} {0 1 0}]"
+ );
+
+ do_execsql_test(db,
+ "SELECT fts5_inst(ft) FROM ft('a OR b OR four')",
+ "[{2 1 2}, {2 0 2} {2 1 1}, {2 0 1} {2 1 0}]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_inst(ft) FROM ft('two four')",
+ "[{0 0 1} {0 1 0} {1 1 2}, {0 0 0} {1 0 2} {1 1 1}]"
+ );
+
+ do_execsql_test(db,
+ "SELECT fts5_pinst(ft) FROM ft('two')",
+ "[{0 0 1} {0 1 0}, {0 0 0}]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_pinst(ft) FROM ft('four')",
+ "[{0 1 2}, {0 0 2} {0 1 1}, {0 0 1} {0 1 0}]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_pinst(ft) FROM ft('a OR b OR four')",
+ "[{2 1 2}, {2 0 2} {2 1 1}, {2 0 1} {2 1 0}]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_pinst(ft) FROM ft('two four')",
+ "[{0 0 1} {0 1 0} {1 1 2}, {0 0 0} {1 0 2} {1 1 1}]"
+ );
+
+ do_execsql_test(db,
+ "SELECT fts5_pcolinst(ft) FROM ft('two')",
+ "[{0 0} {0 1}, {0 0}]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_pcolinst(ft) FROM ft('four')",
+ "[{0 1}, {0 0} {0 1}, {0 0} {0 1}]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_pcolinst(ft) FROM ft('a OR b OR four')",
+ "[{2 1}, {2 0} {2 1}, {2 0} {2 1}]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_pcolinst(ft) FROM ft('two four')",
+ "[{0 0} {0 1} {1 1}, {0 0} {1 0} {1 1}]"
+ );
+
+ do_execsql_test(db,
+ "SELECT fts5_phrasesize(ft, 0) FROM ft('four five six') LIMIT 1;",
+ "[1]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_phrasesize(ft, 0) FROM ft('four + five + six') LIMIT 1;",
+ "[3]"
+ );
+
+
+ sqlite3_close_v2(db);
+ }
+
+ private static void test5(){
+ /* Open db and populate an fts5 table */
+ sqlite3 db = createNewDb();
+ create_test_functions(db);
+ do_execsql_test(db,
+ "CREATE VIRTUAL TABLE ft USING fts5(x, b);" +
+ "INSERT INTO ft(x) VALUES('one two three four five six seven eight');" +
+ "INSERT INTO ft(x) VALUES('one two one four one six one eight');" +
+ "INSERT INTO ft(x) VALUES('one two three four five six seven eight');"
+ );
+
+ do_execsql_test(db,
+ "SELECT fts5_phrasehits(ft, 0) FROM ft('one') LIMIT 1",
+ "[6]"
+ );
+
+ sqlite3_close_v2(db);
+ }
+
+ private static void test6(){
+ sqlite3 db = createNewDb();
+ create_test_functions(db);
+ do_execsql_test(db,
+ "CREATE VIRTUAL TABLE ft USING fts5(x, b);" +
+ "INSERT INTO ft(x) VALUES('one two three four five six seven eight');"
+ );
+
+ do_execsql_test(db,
+ "SELECT fts5_tokenize(ft, 'abc def ghi') FROM ft('one')",
+ "[abc+def+ghi]"
+ );
+ do_execsql_test(db,
+ "SELECT fts5_tokenize(ft, 'it''s BEEN a...') FROM ft('one')",
+ "[it+s+been+a]"
+ );
+
+ sqlite3_close_v2(db);
+ }
+
+ private static synchronized void runTests(){
+ test1();
+ test2();
+ test3();
+ test4();
+ test5();
+ test6();
+ }
+
+ public TesterFts5(){
+ runTests();
+ }
+}
diff --git a/ext/jni/src/org/sqlite/jni/CommitHook.java b/ext/jni/src/org/sqlite/jni/fts5/XTokenizeCallback.java
similarity index 64%
rename from ext/jni/src/org/sqlite/jni/CommitHook.java
rename to ext/jni/src/org/sqlite/jni/fts5/XTokenizeCallback.java
index eaa75a0040..3aa514f314 100644
--- a/ext/jni/src/org/sqlite/jni/CommitHook.java
+++ b/ext/jni/src/org/sqlite/jni/fts5/XTokenizeCallback.java
@@ -1,5 +1,5 @@
/*
-** 2023-07-22
+** 2023-08-04
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
@@ -11,15 +11,12 @@
*************************************************************************
** This file is part of the JNI bindings for the sqlite3 C API.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.fts5;
+
/**
- Callback proxy for use with sqlite3_commit_hook().
+ Callback type for use with xTokenize() variants.
*/
-public interface CommitHook {
- /**
- Works as documented for the sqlite3_commit_hook() callback.
- Must not throw.
- */
- int xCommitHook();
+public interface XTokenizeCallback {
+ int call(int tFlags, byte[] txt, int iStart, int iEnd);
}
diff --git a/ext/jni/src/org/sqlite/jni/fts5_api.java b/ext/jni/src/org/sqlite/jni/fts5/fts5_api.java
similarity index 80%
rename from ext/jni/src/org/sqlite/jni/fts5_api.java
rename to ext/jni/src/org/sqlite/jni/fts5/fts5_api.java
index 43b3d62ded..d7d2da430d 100644
--- a/ext/jni/src/org/sqlite/jni/fts5_api.java
+++ b/ext/jni/src/org/sqlite/jni/fts5/fts5_api.java
@@ -11,11 +11,11 @@
*************************************************************************
** This file is part of the JNI bindings for the sqlite3 C API.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.fts5;
+import org.sqlite.jni.annotation.*;
+import org.sqlite.jni.capi.*;
/**
- INCOMPLETE AND COMPLETELY UNTESTED.
-
A wrapper for communicating C-level (fts5_api*) instances with
Java. These wrappers do not own their associated pointer, they
simply provide a type-safe way to communicate it between Java and C
@@ -24,7 +24,8 @@ package org.sqlite.jni;
public final class fts5_api extends NativePointerHolder {
/* Only invoked from JNI */
private fts5_api(){}
- public final int iVersion = 2;
+
+ public static final int iVersion = 2;
/**
Returns the fts5_api instance associated with the given db, or
@@ -32,6 +33,30 @@ public final class fts5_api extends NativePointerHolder {
*/
public static synchronized native fts5_api getInstanceForDb(@NotNull sqlite3 db);
+ public synchronized native int xCreateFunction(@NotNull String name,
+ @Nullable Object userData,
+ @NotNull fts5_extension_function xFunction);
+
+ /**
+ Convenience overload which passes null as the 2nd argument to the
+ 3-parameter form.
+ */
+ public int xCreateFunction(@NotNull String name,
+ @NotNull fts5_extension_function xFunction){
+ return xCreateFunction(name, null, xFunction);
+ }
+
+ // /* Create a new auxiliary function */
+ // int (*xCreateFunction)(
+ // fts5_api *pApi,
+ // const char *zName,
+ // void *pContext,
+ // fts5_extension_function xFunction,
+ // void (*xDestroy)(void*)
+ // );
+
+ // Still potentially todo:
+
// int (*xCreateTokenizer)(
// fts5_api *pApi,
// const char *zName,
@@ -48,22 +73,4 @@ public final class fts5_api extends NativePointerHolder {
// fts5_tokenizer *pTokenizer
// );
- // /* Create a new auxiliary function */
- // int (*xCreateFunction)(
- // fts5_api *pApi,
- // const char *zName,
- // void *pContext,
- // fts5_extension_function xFunction,
- // void (*xDestroy)(void*)
- // );
-
- public synchronized native int xCreateFunction(@NotNull String name,
- @Nullable Object userData,
- @NotNull fts5_extension_function xFunction);
-
- public int xCreateFunction(@NotNull String name,
- @NotNull fts5_extension_function xFunction){
- return xCreateFunction(name, null, xFunction);
- }
-
}
diff --git a/ext/jni/src/org/sqlite/jni/fts5_extension_function.java b/ext/jni/src/org/sqlite/jni/fts5/fts5_extension_function.java
similarity index 61%
rename from ext/jni/src/org/sqlite/jni/fts5_extension_function.java
rename to ext/jni/src/org/sqlite/jni/fts5/fts5_extension_function.java
index 0e273119f5..5e47633baa 100644
--- a/ext/jni/src/org/sqlite/jni/fts5_extension_function.java
+++ b/ext/jni/src/org/sqlite/jni/fts5/fts5_extension_function.java
@@ -11,13 +11,14 @@
*************************************************************************
** This file is part of the JNI bindings for the sqlite3 C API.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.fts5;
+import org.sqlite.jni.capi.sqlite3_context;
+import org.sqlite.jni.capi.sqlite3_value;
/**
JNI-level wrapper for C's fts5_extension_function type.
-
*/
-public abstract class fts5_extension_function {
+public interface fts5_extension_function {
// typedef void (*fts5_extension_function)(
// const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
// Fts5Context *pFts, /* First arg to pass to pApi functions */
@@ -30,8 +31,17 @@ public abstract class fts5_extension_function {
The callback implementation, corresponding to the xFunction
argument of C's fts5_api::xCreateFunction().
*/
- public abstract void xFunction(Fts5ExtensionApi ext, Fts5Context fCx,
- sqlite3_context pCx, sqlite3_value argv[]);
- //! Optionally override
- public void xDestroy(){}
+ void call(Fts5ExtensionApi ext, Fts5Context fCx,
+ sqlite3_context pCx, sqlite3_value argv[]);
+ /**
+ Is called when this function is destroyed by sqlite3. Typically
+ this function will be empty.
+ */
+ void xDestroy();
+
+ public static abstract class Abstract implements fts5_extension_function {
+ @Override public abstract void call(Fts5ExtensionApi ext, Fts5Context fCx,
+ sqlite3_context pCx, sqlite3_value argv[]);
+ @Override public void xDestroy(){}
+ }
}
diff --git a/ext/jni/src/org/sqlite/jni/fts5_tokenizer.java b/ext/jni/src/org/sqlite/jni/fts5/fts5_tokenizer.java
similarity index 90%
rename from ext/jni/src/org/sqlite/jni/fts5_tokenizer.java
rename to ext/jni/src/org/sqlite/jni/fts5/fts5_tokenizer.java
index 097a0cc055..f4ada4dc30 100644
--- a/ext/jni/src/org/sqlite/jni/fts5_tokenizer.java
+++ b/ext/jni/src/org/sqlite/jni/fts5/fts5_tokenizer.java
@@ -11,11 +11,11 @@
*************************************************************************
** This file is part of the JNI bindings for the sqlite3 C API.
*/
-package org.sqlite.jni;
+package org.sqlite.jni.fts5;
+import org.sqlite.jni.capi.NativePointerHolder;
+import org.sqlite.jni.annotation.NotNull;
/**
- INCOMPLETE AND COMPLETELY UNTESTED.
-
A wrapper for communicating C-level (fts5_tokenizer*) instances with
Java. These wrappers do not own their associated pointer, they
simply provide a type-safe way to communicate it between Java and C
@@ -30,7 +30,7 @@ public final class fts5_tokenizer extends NativePointerHolder {
public native int xTokenize(@NotNull Fts5Tokenizer t, int tokFlags,
@NotNull byte pText[],
- @NotNull Fts5.xTokenizeCallback callback);
+ @NotNull XTokenizeCallback callback);
// int (*xTokenize)(Fts5Tokenizer*,
diff --git a/ext/jni/src/org/sqlite/jni/sqlite3_context.java b/ext/jni/src/org/sqlite/jni/sqlite3_context.java
deleted file mode 100644
index a61ff21c7e..0000000000
--- a/ext/jni/src/org/sqlite/jni/sqlite3_context.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
-** 2023-07-21
-**
-** The author disclaims copyright to this source code. In place of
-** a legal notice, here is a blessing:
-**
-** May you do good and not evil.
-** May you find forgiveness for yourself and forgive others.
-** May you share freely, never taking more than you give.
-**
-*************************************************************************
-** This file is part of the JNI bindings for the sqlite3 C API.
-*/
-package org.sqlite.jni;
-
-/**
- sqlite3_context instances are used in conjunction with user-defined
- SQL functions (a.k.a. UDFs).
-*/
-public final class sqlite3_context extends NativePointerHolder {
- /**
- For use only by the JNI layer. It's permitted to set this even
- though it's private.
- */
- private long aggregateContext = 0;
-
- /**
- getAggregateContext() corresponds to C's
- sqlite3_aggregate_context(), with a slightly different interface
- to account for cross-language differences. It serves the same
- purposes in a slightly different way: it provides a key which is
- stable across invocations of "matching sets" of a UDF's callbacks,
- such that all calls into those callbacks can determine which "set"
- of those calls they belong to.
-
- If this object is being used in the context of an aggregate or
- window UDF, this function returns a non-0 value which is distinct
- for each set of UDF callbacks from a single invocation of the
- UDF, otherwise it returns 0. The returned value is only only
- valid within the context of execution of a single SQL statement,
- and may be re-used by future invocations of the UDF in different
- SQL statements.
-
- Consider this SQL, where MYFUNC is a user-defined aggregate function:
-
- SELECT MYFUNC(A), MYFUNC(B) FROM T;
-
- The xStep() and xFinal() methods of the callback need to be able
- to differentiate between those two invocations in order to
- perform their work properly. The value returned by
- getAggregateContext() will be distinct for each of those
- invocations of MYFUNC() and is intended to be used as a lookup
- key for mapping callback invocations to whatever client-defined
- state is needed by the UDF.
-
- There is one case where this will return 0 in the context of an
- aggregate or window function: if the result set has no rows,
- the UDF's xFinal() will be called without any other x...() members
- having been called. In that one case, no aggregate context key will
- have been generated. xFinal() implementations need to be prepared to
- accept that condition as legal.
- */
- public long getAggregateContext(){
- return aggregateContext;
- }
-}
diff --git a/ext/jni/src/org/sqlite/jni/tester/test-script-interpreter.md b/ext/jni/src/org/sqlite/jni/test-script-interpreter.md
similarity index 100%
rename from ext/jni/src/org/sqlite/jni/tester/test-script-interpreter.md
rename to ext/jni/src/org/sqlite/jni/test-script-interpreter.md
diff --git a/ext/jni/src/org/sqlite/jni/wrapper1/AggregateFunction.java b/ext/jni/src/org/sqlite/jni/wrapper1/AggregateFunction.java
new file mode 100644
index 0000000000..173d775e62
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/wrapper1/AggregateFunction.java
@@ -0,0 +1,82 @@
+/*
+** 2023-10-16
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the wrapper1 interface for sqlite3.
+*/
+package org.sqlite.jni.wrapper1;
+import org.sqlite.jni.capi.CApi;
+import org.sqlite.jni.annotation.*;
+import org.sqlite.jni.capi.sqlite3_context;
+import org.sqlite.jni.capi.sqlite3_value;
+
+/**
+ EXPERIMENTAL/INCOMPLETE/UNTESTED
+
+ A SqlFunction implementation for aggregate functions. The T type
+ represents the type of data accumulated by this aggregate while it
+ works. e.g. a SUM()-like UDF might use Integer or Long and a
+ CONCAT()-like UDF might use a StringBuilder or a List.
+*/
+public abstract class AggregateFunction implements SqlFunction {
+
+ /**
+ As for the xStep() argument of the C API's
+ sqlite3_create_function(). If this function throws, the
+ exception is reported via sqlite3_result_error().
+ */
+ public abstract void xStep(SqlFunction.Arguments args);
+
+ /**
+ As for the xFinal() argument of the C API's
+ sqlite3_create_function(). If this function throws, it is
+ translated into sqlite3_result_error().
+
+ Note that the passed-in object will not actually contain any
+ arguments for xFinal() but will contain the context object needed
+ for setting the call's result or error state.
+ */
+ public abstract void xFinal(SqlFunction.Arguments args);
+
+ /**
+ Optionally override to be notified when the UDF is finalized by
+ SQLite.
+ */
+ public void xDestroy() {}
+
+ /** Per-invocation state for the UDF. */
+ private final SqlFunction.PerContextState map =
+ new SqlFunction.PerContextState<>();
+
+ /**
+ To be called from the implementation's xStep() method, as well
+ as the xValue() and xInverse() methods of the {@link WindowFunction}
+ subclass, to fetch the current per-call UDF state. On the
+ first call to this method for any given sqlite3_context
+ argument, the context is set to the given initial value. On all other
+ calls, the 2nd argument is ignored.
+
+ @see SQLFunction.PerContextState#getAggregateState
+ */
+ protected final ValueHolder getAggregateState(SqlFunction.Arguments args, T initialValue){
+ return map.getAggregateState(args, initialValue);
+ }
+
+ /**
+ To be called from the implementation's xFinal() method to fetch
+ the final state of the UDF and remove its mapping.
+
+ see SQLFunction.PerContextState#takeAggregateState
+ */
+ protected final T takeAggregateState(SqlFunction.Arguments args){
+ return map.takeAggregateState(args);
+ }
+
+}
diff --git a/ext/jni/src/org/sqlite/jni/wrapper1/ScalarFunction.java b/ext/jni/src/org/sqlite/jni/wrapper1/ScalarFunction.java
new file mode 100644
index 0000000000..067a6983eb
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/wrapper1/ScalarFunction.java
@@ -0,0 +1,37 @@
+/*
+** 2023-10-16
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the wrapper1 interface for sqlite3.
+*/
+package org.sqlite.jni.wrapper1;
+import org.sqlite.jni.capi.CApi;
+import org.sqlite.jni.annotation.*;
+import org.sqlite.jni.capi.sqlite3_context;
+import org.sqlite.jni.capi.sqlite3_value;
+
+/**
+ The SqlFunction type for scalar SQL functions.
+*/
+public abstract class ScalarFunction implements SqlFunction {
+ /**
+ As for the xFunc() argument of the C API's
+ sqlite3_create_function(). If this function throws, it is
+ translated into an sqlite3_result_error().
+ */
+ public abstract void xFunc(SqlFunction.Arguments args);
+
+ /**
+ Optionally override to be notified when the UDF is finalized by
+ SQLite. This default implementation does nothing.
+ */
+ public void xDestroy() {}
+
+}
diff --git a/ext/jni/src/org/sqlite/jni/wrapper1/SqlFunction.java b/ext/jni/src/org/sqlite/jni/wrapper1/SqlFunction.java
new file mode 100644
index 0000000000..d6acda5aa5
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/wrapper1/SqlFunction.java
@@ -0,0 +1,301 @@
+/*
+** 2023-10-16
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the wrapper1 interface for sqlite3.
+*/
+package org.sqlite.jni.wrapper1;
+import org.sqlite.jni.capi.CApi;
+import org.sqlite.jni.capi.sqlite3_context;
+import org.sqlite.jni.capi.sqlite3_value;
+
+/**
+ Base marker interface for SQLite's three types of User-Defined SQL
+ Functions (UDFs): Scalar, Aggregate, and Window functions.
+*/
+public interface SqlFunction {
+
+ /**
+ The Arguments type is an abstraction on top of the lower-level
+ UDF function argument types. It provides _most_ of the functionality
+ of the lower-level interface, insofar as possible without "leaking"
+ those types into this API.
+ */
+ public final static class Arguments implements Iterable{
+ private final sqlite3_context cx;
+ private final sqlite3_value args[];
+ public final int length;
+
+ /**
+ Must be passed the context and arguments for the UDF call this
+ object is wrapping. Intended to be used by internal proxy
+ classes which "convert" the lower-level interface into this
+ package's higher-level interface, e.g. ScalarAdapter and
+ AggregateAdapter.
+
+ Passing null for the args is equivalent to passing a length-0
+ array.
+ */
+ Arguments(sqlite3_context cx, sqlite3_value args[]){
+ this.cx = cx;
+ this.args = args==null ? new sqlite3_value[0] : args;;
+ this.length = this.args.length;
+ }
+
+ /**
+ Wrapper for a single SqlFunction argument. Primarily intended
+ for use with the Arguments class's Iterable interface.
+ */
+ public final static class Arg {
+ private final Arguments a;
+ private final int ndx;
+ /* Only for use by the Arguments class. */
+ private Arg(Arguments a, int ndx){
+ this.a = a;
+ this.ndx = ndx;
+ }
+ /** Returns this argument's index in its parent argument list. */
+ public int getIndex(){return ndx;}
+ public int getInt(){return a.getInt(ndx);}
+ public long getInt64(){return a.getInt64(ndx);}
+ public double getDouble(){return a.getDouble(ndx);}
+ public byte[] getBlob(){return a.getBlob(ndx);}
+ public byte[] getText(){return a.getText(ndx);}
+ public String getText16(){return a.getText16(ndx);}
+ public int getBytes(){return a.getBytes(ndx);}
+ public int getBytes16(){return a.getBytes16(ndx);}
+ public Object getObject(){return a.getObject(ndx);}
+ public T getObjectCasted(Class type){ return a.getObjectCasted(ndx, type); }
+ public int getType(){return a.getType(ndx);}
+ public Object getAuxData(){return a.getAuxData(ndx);}
+ public void setAuxData(Object o){a.setAuxData(ndx, o);}
+ }
+
+ @Override
+ public java.util.Iterator iterator(){
+ final Arg[] proxies = new Arg[args.length];
+ for( int i = 0; i < args.length; ++i ){
+ proxies[i] = new Arg(this, i);
+ }
+ return java.util.Arrays.stream(proxies).iterator();
+ }
+
+ /**
+ Returns the sqlite3_value at the given argument index or throws
+ an IllegalArgumentException exception if ndx is out of range.
+ */
+ private sqlite3_value valueAt(int ndx){
+ if(ndx<0 || ndx>=args.length){
+ throw new IllegalArgumentException(
+ "SQL function argument index "+ndx+" is out of range."
+ );
+ }
+ return args[ndx];
+ }
+
+ sqlite3_context getContext(){return cx;}
+
+ public int getArgCount(){ return args.length; }
+
+ public int getInt(int arg){return CApi.sqlite3_value_int(valueAt(arg));}
+ public long getInt64(int arg){return CApi.sqlite3_value_int64(valueAt(arg));}
+ public double getDouble(int arg){return CApi.sqlite3_value_double(valueAt(arg));}
+ public byte[] getBlob(int arg){return CApi.sqlite3_value_blob(valueAt(arg));}
+ public byte[] getText(int arg){return CApi.sqlite3_value_text(valueAt(arg));}
+ public String getText16(int arg){return CApi.sqlite3_value_text16(valueAt(arg));}
+ public int getBytes(int arg){return CApi.sqlite3_value_bytes(valueAt(arg));}
+ public int getBytes16(int arg){return CApi.sqlite3_value_bytes16(valueAt(arg));}
+ public Object getObject(int arg){return CApi.sqlite3_value_java_object(valueAt(arg));}
+ public T getObjectCasted(int arg, Class type){
+ return CApi.sqlite3_value_java_casted(valueAt(arg), type);
+ }
+
+ public int getType(int arg){return CApi.sqlite3_value_type(valueAt(arg));}
+ public int getSubtype(int arg){return CApi.sqlite3_value_subtype(valueAt(arg));}
+ public int getNumericType(int arg){return CApi.sqlite3_value_numeric_type(valueAt(arg));}
+ public int getNoChange(int arg){return CApi.sqlite3_value_nochange(valueAt(arg));}
+ public boolean getFromBind(int arg){return CApi.sqlite3_value_frombind(valueAt(arg));}
+ public int getEncoding(int arg){return CApi.sqlite3_value_encoding(valueAt(arg));}
+
+ public void resultInt(int v){ CApi.sqlite3_result_int(cx, v); }
+ public void resultInt64(long v){ CApi.sqlite3_result_int64(cx, v); }
+ public void resultDouble(double v){ CApi.sqlite3_result_double(cx, v); }
+ public void resultError(String msg){CApi.sqlite3_result_error(cx, msg);}
+ public void resultError(Exception e){CApi.sqlite3_result_error(cx, e);}
+ public void resultErrorTooBig(){CApi.sqlite3_result_error_toobig(cx);}
+ public void resultErrorCode(int rc){CApi.sqlite3_result_error_code(cx, rc);}
+ public void resultObject(Object o){CApi.sqlite3_result_java_object(cx, o);}
+ public void resultNull(){CApi.sqlite3_result_null(cx);}
+ public void resultArg(int argNdx){CApi.sqlite3_result_value(cx, valueAt(argNdx));}
+ public void resultZeroBlob(long n){
+ // Throw on error? If n is too big,
+ // sqlite3_result_error_toobig() is automatically called.
+ CApi.sqlite3_result_zeroblob64(cx, n);
+ }
+
+ public void resultBlob(byte[] blob){CApi.sqlite3_result_blob(cx, blob);}
+ public void resultText(byte[] utf8){CApi.sqlite3_result_text(cx, utf8);}
+ public void resultText(String txt){CApi.sqlite3_result_text(cx, txt);}
+ public void resultText16(byte[] utf16){CApi.sqlite3_result_text16(cx, utf16);}
+ public void resultText16(String txt){CApi.sqlite3_result_text16(cx, txt);}
+
+ public void setAuxData(int arg, Object o){
+ /* From the API docs: https://www.sqlite.org/c3ref/get_auxdata.html
+
+ The value of the N parameter to these interfaces should be
+ non-negative. Future enhancements may make use of negative N
+ values to define new kinds of function caching behavior.
+ */
+ valueAt(arg);
+ CApi.sqlite3_set_auxdata(cx, arg, o);
+ }
+
+ public Object getAuxData(int arg){
+ valueAt(arg);
+ return CApi.sqlite3_get_auxdata(cx, arg);
+ }
+ }
+
+ /**
+ PerContextState assists aggregate and window functions in
+ managing their accumulator state across calls to the UDF's
+ callbacks.
+
+ T must be of a type which can be legally stored as a value in
+ java.util.HashMap.
+
+ If a given aggregate or window function is called multiple times
+ in a single SQL statement, e.g. SELECT MYFUNC(A), MYFUNC(B)...,
+ then the clients need some way of knowing which call is which so
+ that they can map their state between their various UDF callbacks
+ and reset it via xFinal(). This class takes care of such
+ mappings.
+
+
This class works by mapping
+ sqlite3_context.getAggregateContext() to a single piece of
+ state, of a client-defined type (the T part of this class), which
+ persists across a "matching set" of the UDF's callbacks.
+
+
This class is a helper providing commonly-needed functionality
+ - it is not required for use with aggregate or window functions.
+ Client UDFs are free to perform such mappings using custom
+ approaches. The provided {@link AggregateFunction} and {@link
+ WindowFunction} classes use this.
+ */
+ public static final class PerContextState {
+ private final java.util.Map> map
+ = new java.util.HashMap<>();
+
+ /**
+ Should be called from a UDF's xStep(), xValue(), and xInverse()
+ methods, passing it that method's first argument and an initial
+ value for the persistent state. If there is currently no
+ mapping for the given context within the map, one is created
+ using the given initial value, else the existing one is used
+ and the 2nd argument is ignored. It returns a ValueHolder
+ which can be used to modify that state directly without
+ requiring that the client update the underlying map's entry.
+
+ The caller is obligated to eventually call
+ takeAggregateState() to clear the mapping.
+ */
+ public ValueHolder getAggregateState(SqlFunction.Arguments args, T initialValue){
+ final Long key = args.getContext().getAggregateContext(true);
+ ValueHolder rc = null==key ? null : map.get(key);
+ if( null==rc ){
+ map.put(key, rc = new ValueHolder<>(initialValue));
+ }
+ return rc;
+ }
+
+ /**
+ Should be called from a UDF's xFinal() method and passed that
+ method's first argument. This function removes the value
+ associated with with the arguments' aggregate context from the
+ map and returns it, returning null if no other UDF method has
+ been called to set up such a mapping. The latter condition will
+ be the case if a UDF is used in a statement which has no result
+ rows.
+ */
+ public T takeAggregateState(SqlFunction.Arguments args){
+ final ValueHolder h = map.remove(args.getContext().getAggregateContext(false));
+ return null==h ? null : h.value;
+ }
+ }
+
+ /**
+ Internal-use adapter for wrapping this package's ScalarFunction
+ for use with the org.sqlite.jni.capi.ScalarFunction interface.
+ */
+ static final class ScalarAdapter extends org.sqlite.jni.capi.ScalarFunction {
+ final ScalarFunction impl;
+ ScalarAdapter(ScalarFunction impl){
+ this.impl = impl;
+ }
+ /**
+ Proxies this.impl.xFunc(), adapting the call arguments to that
+ function's signature. If the proxy throws, it's translated to
+ sqlite_result_error() with the exception's message.
+ */
+ public void xFunc(sqlite3_context cx, sqlite3_value[] args){
+ try{
+ impl.xFunc( new SqlFunction.Arguments(cx, args) );
+ }catch(Exception e){
+ CApi.sqlite3_result_error(cx, e);
+ }
+ }
+
+ public void xDestroy(){
+ impl.xDestroy();
+ }
+ }
+
+ /**
+ Internal-use adapter for wrapping this package's AggregateFunction
+ for use with the org.sqlite.jni.capi.AggregateFunction interface.
+ */
+ static final class AggregateAdapter extends org.sqlite.jni.capi.AggregateFunction {
+ final AggregateFunction impl;
+ AggregateAdapter(AggregateFunction impl){
+ this.impl = impl;
+ }
+
+ /**
+ Proxies this.impl.xStep(), adapting the call arguments to that
+ function's signature. If the proxied function throws, it is
+ translated to sqlite_result_error() with the exception's
+ message.
+ */
+ public void xStep(sqlite3_context cx, sqlite3_value[] args){
+ try{
+ impl.xStep( new SqlFunction.Arguments(cx, args) );
+ }catch(Exception e){
+ CApi.sqlite3_result_error(cx, e);
+ }
+ }
+
+ /**
+ As for the xFinal() argument of the C API's sqlite3_create_function().
+ If the proxied function throws, it is translated into a sqlite3_result_error().
+ */
+ public void xFinal(sqlite3_context cx){
+ try{
+ impl.xFinal( new SqlFunction.Arguments(cx, null) );
+ }catch(Exception e){
+ CApi.sqlite3_result_error(cx, e);
+ }
+ }
+
+ public void xDestroy(){
+ impl.xDestroy();
+ }
+ }
+
+}
diff --git a/ext/jni/src/org/sqlite/jni/wrapper1/Sqlite.java b/ext/jni/src/org/sqlite/jni/wrapper1/Sqlite.java
new file mode 100644
index 0000000000..bcf97b2394
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/wrapper1/Sqlite.java
@@ -0,0 +1,218 @@
+/*
+** 2023-10-09
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the wrapper1 interface for sqlite3.
+*/
+package org.sqlite.jni.wrapper1;
+import java.nio.charset.StandardCharsets;
+import static org.sqlite.jni.capi.CApi.*;
+import org.sqlite.jni.capi.CApi;
+import org.sqlite.jni.capi.sqlite3;
+import org.sqlite.jni.capi.sqlite3_stmt;
+import org.sqlite.jni.capi.OutputPointer;
+
+/**
+ This class represents a database connection, analog to the C-side
+ sqlite3 class but with added argument validation, exceptions, and
+ similar "smoothing of sharp edges" to make the API safe to use from
+ Java. It also acts as a namespace for other types for which
+ individual instances are tied to a specific database connection.
+*/
+public final class Sqlite implements AutoCloseable {
+ private sqlite3 db;
+
+ //! Used only by the open() factory functions.
+ private Sqlite(sqlite3 db){
+ this.db = db;
+ }
+
+ /**
+ Returns a newly-opened db connection or throws SqliteException if
+ opening fails. All arguments are as documented for
+ sqlite3_open_v2().
+
+ Design question: do we want static factory functions or should
+ this be reformulated as a constructor?
+ */
+ public static Sqlite open(String filename, int flags, String vfsName){
+ final OutputPointer.sqlite3 out = new OutputPointer.sqlite3();
+ final int rc = sqlite3_open_v2(filename, out, flags, vfsName);
+ final sqlite3 n = out.take();
+ if( 0!=rc ){
+ if( null==n ) throw new SqliteException(rc);
+ final SqliteException ex = new SqliteException(n);
+ n.close();
+ throw ex;
+ }
+ return new Sqlite(n);
+ }
+
+ public static Sqlite open(String filename, int flags){
+ return open(filename, flags, null);
+ }
+
+ public static Sqlite open(String filename){
+ return open(filename, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, null);
+ }
+
+ @Override public void close(){
+ if(null!=this.db){
+ this.db.close();
+ this.db = null;
+ }
+ }
+
+ /**
+ Returns this object's underlying native db handle, or null if
+ this instance has been closed. This is very specifically not
+ public.
+ */
+ sqlite3 nativeHandle(){ return this.db; }
+
+ private sqlite3 affirmOpen(){
+ if( null==db || 0==db.getNativePointer() ){
+ throw new IllegalArgumentException("This database instance is closed.");
+ }
+ return this.db;
+ }
+
+ // private byte[] stringToUtf8(String s){
+ // return s==null ? null : s.getBytes(StandardCharsets.UTF_8);
+ // }
+
+ private void affirmRcOk(int rc){
+ if( 0!=rc ){
+ throw new SqliteException(db);
+ }
+ }
+
+ /**
+ Corresponds to the sqlite3_stmt class. Use Sqlite.prepare() to
+ create new instances.
+ */
+ public final class Stmt implements AutoCloseable {
+ private Sqlite _db = null;
+ private sqlite3_stmt stmt = null;
+ /** Only called by the prepare() factory functions. */
+ Stmt(Sqlite db, sqlite3_stmt stmt){
+ this._db = db;
+ this.stmt = stmt;
+ }
+
+ sqlite3_stmt nativeHandle(){
+ return stmt;
+ }
+
+ private sqlite3_stmt affirmOpen(){
+ if( null==stmt || 0==stmt.getNativePointer() ){
+ throw new IllegalArgumentException("This Stmt has been finalized.");
+ }
+ return stmt;
+ }
+
+ /**
+ Corresponds to sqlite3_finalize(), but we cannot override the
+ name finalize() here because this one requires a different
+ signature. It does not throw on error here because "destructors
+ do not throw." If it returns non-0, the object is still
+ finalized.
+ */
+ public int finalizeStmt(){
+ int rc = 0;
+ if( null!=stmt ){
+ sqlite3_finalize(stmt);
+ stmt = null;
+ }
+ return rc;
+ }
+
+ @Override public void close(){
+ finalizeStmt();
+ }
+
+ /**
+ Throws if rc is any value other than 0, SQLITE_ROW, or
+ SQLITE_DONE, else returns rc.
+ */
+ private int checkRc(int rc){
+ switch(rc){
+ case 0:
+ case SQLITE_ROW:
+ case SQLITE_DONE: return rc;
+ default:
+ throw new SqliteException(this);
+ }
+ }
+
+ /**
+ Works like sqlite3_step() but throws SqliteException for any
+ result other than 0, SQLITE_ROW, or SQLITE_DONE.
+ */
+ public int step(){
+ return checkRc(sqlite3_step(affirmOpen()));
+ }
+
+ public Sqlite db(){ return this._db; }
+
+ /**
+ Works like sqlite3_reset() but throws on error.
+ */
+ public void reset(){
+ checkRc(sqlite3_reset(affirmOpen()));
+ }
+
+ public void clearBindings(){
+ sqlite3_clear_bindings( affirmOpen() );
+ }
+ }
+
+
+ /**
+ prepare() TODOs include:
+
+ - overloads taking byte[] and ByteBuffer.
+
+ - multi-statement processing, like CApi.sqlite3_prepare_multi()
+ but using a callback specific to the higher-level Stmt class
+ rather than the sqlite3_stmt class.
+ */
+ public Stmt prepare(String sql, int prepFlags){
+ final OutputPointer.sqlite3_stmt out = new OutputPointer.sqlite3_stmt();
+ final int rc = sqlite3_prepare_v3(affirmOpen(), sql, prepFlags, out);
+ affirmRcOk(rc);
+ return new Stmt(this, out.take());
+ }
+
+ public Stmt prepare(String sql){
+ return prepare(sql, 0);
+ }
+
+ public void createFunction(String name, int nArg, int eTextRep, ScalarFunction f ){
+ int rc = CApi.sqlite3_create_function(affirmOpen(), name, nArg, eTextRep,
+ new SqlFunction.ScalarAdapter(f));
+ if( 0!=rc ) throw new SqliteException(db);
+ }
+
+ public void createFunction(String name, int nArg, ScalarFunction f){
+ this.createFunction(name, nArg, CApi.SQLITE_UTF8, f);
+ }
+
+ public void createFunction(String name, int nArg, int eTextRep, AggregateFunction f ){
+ int rc = CApi.sqlite3_create_function(affirmOpen(), name, nArg, eTextRep,
+ new SqlFunction.AggregateAdapter(f));
+ if( 0!=rc ) throw new SqliteException(db);
+ }
+
+ public void createFunction(String name, int nArg, AggregateFunction f){
+ this.createFunction(name, nArg, CApi.SQLITE_UTF8, f);
+ }
+
+}
diff --git a/ext/jni/src/org/sqlite/jni/wrapper1/SqliteException.java b/ext/jni/src/org/sqlite/jni/wrapper1/SqliteException.java
new file mode 100644
index 0000000000..111f004db4
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/wrapper1/SqliteException.java
@@ -0,0 +1,82 @@
+/*
+** 2023-10-09
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file is part of the wrapper1 interface for sqlite3.
+*/
+package org.sqlite.jni.wrapper1;
+import static org.sqlite.jni.capi.CApi.*;
+import org.sqlite.jni.capi.sqlite3;
+
+/**
+ A wrapper for communicating C-level (sqlite3*) instances with
+ Java. These wrappers do not own their associated pointer, they
+ simply provide a type-safe way to communicate it between Java
+ and C via JNI.
+*/
+public final class SqliteException extends java.lang.RuntimeException {
+ int errCode = SQLITE_ERROR;
+ int xerrCode = SQLITE_ERROR;
+ int errOffset = -1;
+ int sysErrno = 0;
+
+ /**
+ Records the given error string and uses SQLITE_ERROR for both the
+ error code and extended error code.
+ */
+ public SqliteException(String msg){
+ super(msg);
+ }
+
+ /**
+ Uses sqlite3_errstr(sqlite3ResultCode) for the error string and
+ sets both the error code and extended error code to the given
+ value.
+ */
+ public SqliteException(int sqlite3ResultCode){
+ super(sqlite3_errstr(sqlite3ResultCode));
+ errCode = xerrCode = sqlite3ResultCode;
+ }
+
+ /**
+ Records the current error state of db (which must not be null and
+ must refer to an opened db object). Note that this does NOT close
+ the db.
+
+ Design note: closing the db on error is likely only useful during
+ a failed db-open operation, and the place(s) where that can
+ happen are inside this library, not client-level code.
+ */
+ SqliteException(sqlite3 db){
+ super(sqlite3_errmsg(db));
+ errCode = sqlite3_errcode(db);
+ xerrCode = sqlite3_extended_errcode(db);
+ errOffset = sqlite3_error_offset(db);
+ sysErrno = sqlite3_system_errno(db);
+ }
+
+ /**
+ Records the current error state of db (which must not be null and
+ must refer to an open database).
+ */
+ public SqliteException(Sqlite db){
+ this(db.nativeHandle());
+ }
+
+ public SqliteException(Sqlite.Stmt stmt){
+ this( stmt.db() );
+ }
+
+ public int errcode(){ return errCode; }
+ public int extendedErrcode(){ return xerrCode; }
+ public int errorOffset(){ return errOffset; }
+ public int systemErrno(){ return sysErrno; }
+
+}
diff --git a/ext/jni/src/org/sqlite/jni/wrapper1/Tester2.java b/ext/jni/src/org/sqlite/jni/wrapper1/Tester2.java
new file mode 100644
index 0000000000..f5fd5f84e6
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/wrapper1/Tester2.java
@@ -0,0 +1,584 @@
+/*
+** 2023-10-09
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file contains a set of tests for the sqlite3 JNI bindings.
+*/
+package org.sqlite.jni.wrapper1;
+//import static org.sqlite.jni.capi.CApi.*;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import org.sqlite.jni.capi.*;
+
+/**
+ An annotation for Tester2 tests which we do not want to run in
+ reflection-driven test mode because either they are not suitable
+ for multi-threaded threaded mode or we have to control their execution
+ order.
+*/
+@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
+@java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD})
+@interface ManualTest{}
+/**
+ Annotation for Tester2 tests which mark those which must be skipped
+ in multi-threaded mode.
+*/
+@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
+@java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD})
+@interface SingleThreadOnly{}
+
+public class Tester2 implements Runnable {
+ //! True when running in multi-threaded mode.
+ private static boolean mtMode = false;
+ //! True to sleep briefly between tests.
+ private static boolean takeNaps = false;
+ //! True to shuffle the order of the tests.
+ private static boolean shuffle = false;
+ //! True to dump the list of to-run tests to stdout.
+ private static boolean listRunTests = false;
+ //! True to squelch all out() and outln() output.
+ private static boolean quietMode = false;
+ //! Total number of runTests() calls.
+ private static int nTestRuns = 0;
+ //! List of test*() methods to run.
+ private static List testMethods = null;
+ //! List of exceptions collected by run()
+ private static List listErrors = new ArrayList<>();
+ private static final class Metrics {
+ //! Number of times createNewDb() (or equivalent) is invoked.
+ volatile int dbOpen = 0;
+ }
+
+ //! Instance ID.
+ private Integer tId;
+
+ Tester2(Integer id){
+ tId = id;
+ }
+
+ static final Metrics metrics = new Metrics();
+
+ public static synchronized void outln(){
+ if( !quietMode ){
+ System.out.println("");
+ }
+ }
+
+ public static synchronized void outPrefix(){
+ if( !quietMode ){
+ System.out.print(Thread.currentThread().getName()+": ");
+ }
+ }
+
+ public static synchronized void outln(Object val){
+ if( !quietMode ){
+ outPrefix();
+ System.out.println(val);
+ }
+ }
+
+ public static synchronized void out(Object val){
+ if( !quietMode ){
+ System.out.print(val);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public static synchronized void out(Object... vals){
+ if( !quietMode ){
+ outPrefix();
+ for(Object v : vals) out(v);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public static synchronized void outln(Object... vals){
+ if( !quietMode ){
+ out(vals); out("\n");
+ }
+ }
+
+ static volatile int affirmCount = 0;
+ public static synchronized int affirm(Boolean v, String comment){
+ ++affirmCount;
+ if( false ) assert( v /* prefer assert over exception if it's enabled because
+ the JNI layer sometimes has to suppress exceptions,
+ so they might be squelched on their way back to the
+ top. */);
+ if( !v ) throw new RuntimeException(comment);
+ return affirmCount;
+ }
+
+ public static void affirm(Boolean v){
+ affirm(v, "Affirmation failed.");
+ }
+
+
+ public static void execSql(Sqlite db, String[] sql){
+ execSql(db, String.join("", sql));
+ }
+
+ public static int execSql(Sqlite dbw, boolean throwOnError, String sql){
+ final sqlite3 db = dbw.nativeHandle();
+ OutputPointer.Int32 oTail = new OutputPointer.Int32();
+ final byte[] sqlUtf8 = sql.getBytes(StandardCharsets.UTF_8);
+ int pos = 0, n = 1;
+ byte[] sqlChunk = sqlUtf8;
+ int rc = 0;
+ sqlite3_stmt stmt = null;
+ final OutputPointer.sqlite3_stmt outStmt = new OutputPointer.sqlite3_stmt();
+ while(pos < sqlChunk.length){
+ if(pos > 0){
+ sqlChunk = Arrays.copyOfRange(sqlChunk, pos,
+ sqlChunk.length);
+ }
+ if( 0==sqlChunk.length ) break;
+ rc = CApi.sqlite3_prepare_v2(db, sqlChunk, outStmt, oTail);
+ if(throwOnError) affirm(0 == rc);
+ else if( 0!=rc ) break;
+ pos = oTail.value;
+ stmt = outStmt.take();
+ if( null == stmt ){
+ // empty statement was parsed.
+ continue;
+ }
+ affirm(0 != stmt.getNativePointer());
+ while( CApi.SQLITE_ROW == (rc = CApi.sqlite3_step(stmt)) ){
+ }
+ CApi.sqlite3_finalize(stmt);
+ affirm(0 == stmt.getNativePointer());
+ if(0!=rc && CApi.SQLITE_ROW!=rc && CApi.SQLITE_DONE!=rc){
+ break;
+ }
+ }
+ CApi.sqlite3_finalize(stmt);
+ if(CApi.SQLITE_ROW==rc || CApi.SQLITE_DONE==rc) rc = 0;
+ if( 0!=rc && throwOnError){
+ throw new SqliteException(db);
+ }
+ return rc;
+ }
+
+ static void execSql(Sqlite db, String sql){
+ execSql(db, true, sql);
+ }
+
+ @SingleThreadOnly /* because it's thread-agnostic */
+ private void test1(){
+ affirm(CApi.sqlite3_libversion_number() == CApi.SQLITE_VERSION_NUMBER);
+ }
+
+ /* Copy/paste/rename this to add new tests. */
+ private void _testTemplate(){
+ //final sqlite3 db = createNewDb();
+ //sqlite3_stmt stmt = prepare(db,"SELECT 1");
+ //sqlite3_finalize(stmt);
+ //sqlite3_close_v2(db);
+ }
+
+ private void nap() throws InterruptedException {
+ if( takeNaps ){
+ Thread.sleep(java.util.concurrent.ThreadLocalRandom.current().nextInt(3, 17), 0);
+ }
+ }
+
+ Sqlite openDb(String name){
+ final Sqlite db = Sqlite.open(name, CApi.SQLITE_OPEN_READWRITE|
+ CApi.SQLITE_OPEN_CREATE|
+ CApi.SQLITE_OPEN_EXRESCODE);
+ ++metrics.dbOpen;
+ return db;
+ }
+
+ Sqlite openDb(){ return openDb(":memory:"); }
+
+ void testOpenDb1(){
+ Sqlite db = openDb();
+ affirm( 0!=db.nativeHandle().getNativePointer() );
+ db.close();
+ affirm( null==db.nativeHandle() );
+
+ SqliteException ex = null;
+ try {
+ db = openDb("/no/such/dir/.../probably");
+ }catch(SqliteException e){
+ ex = e;
+ }
+ affirm( ex!=null );
+ affirm( ex.errcode() != 0 );
+ affirm( ex.extendedErrcode() != 0 );
+ affirm( ex.errorOffset() < 0 );
+ // there's no reliable way to predict what ex.systemErrno() might be
+ }
+
+ void testPrepare1(){
+ try (Sqlite db = openDb()) {
+ Sqlite.Stmt stmt = db.prepare("SELECT 1");
+ affirm( null!=stmt.nativeHandle() );
+ affirm( CApi.SQLITE_ROW == stmt.step() );
+ affirm( CApi.SQLITE_DONE == stmt.step() );
+ stmt.reset();
+ affirm( CApi.SQLITE_ROW == stmt.step() );
+ affirm( CApi.SQLITE_DONE == stmt.step() );
+ affirm( 0 == stmt.finalizeStmt() );
+ affirm( null==stmt.nativeHandle() );
+
+ stmt = db.prepare("SELECT 1");
+ affirm( CApi.SQLITE_ROW == stmt.step() );
+ affirm( 0 == stmt.finalizeStmt() )
+ /* getting a non-0 out of sqlite3_finalize() is tricky */;
+ affirm( null==stmt.nativeHandle() );
+ }
+ }
+
+ void testUdfScalar(){
+ final ValueHolder xDestroyCalled = new ValueHolder<>(0);
+ try (Sqlite db = openDb()) {
+ execSql(db, "create table t(a); insert into t(a) values(1),(2),(3)");
+ final ValueHolder vh = new ValueHolder<>(0);
+ final ScalarFunction f = new ScalarFunction(){
+ public void xFunc(SqlFunction.Arguments args){
+ for( SqlFunction.Arguments.Arg arg : args ){
+ vh.value += arg.getInt();
+ }
+ }
+ public void xDestroy(){
+ ++xDestroyCalled.value;
+ }
+ };
+ db.createFunction("myfunc", -1, f);
+ execSql(db, "select myfunc(1,2,3)");
+ affirm( 6 == vh.value );
+ vh.value = 0;
+ execSql(db, "select myfunc(-1,-2,-3)");
+ affirm( -6 == vh.value );
+ affirm( 0 == xDestroyCalled.value );
+ }
+ affirm( 1 == xDestroyCalled.value );
+ }
+
+ void testUdfAggregate(){
+ final ValueHolder xDestroyCalled = new ValueHolder<>(0);
+ final ValueHolder vh = new ValueHolder<>(0);
+ try (Sqlite db = openDb()) {
+ execSql(db, "create table t(a); insert into t(a) values(1),(2),(3)");
+ final AggregateFunction f = new AggregateFunction(){
+ public void xStep(SqlFunction.Arguments args){
+ final ValueHolder agg = this.getAggregateState(args, 0);
+ for( SqlFunction.Arguments.Arg arg : args ){
+ agg.value += arg.getInt();
+ }
+ }
+ public void xFinal(SqlFunction.Arguments args){
+ final Integer v = this.takeAggregateState(args);
+ if( null==v ) args.resultNull();
+ else args.resultInt(v);
+ vh.value = v;
+ }
+ public void xDestroy(){
+ ++xDestroyCalled.value;
+ }
+ };
+ db.createFunction("myagg", -1, f);
+ execSql(db, "select myagg(a) from t");
+ affirm( 6 == vh.value );
+ affirm( 0 == xDestroyCalled.value );
+ }
+ affirm( 1 == xDestroyCalled.value );
+ }
+
+ private void runTests(boolean fromThread) throws Exception {
+ List mlist = testMethods;
+ affirm( null!=mlist );
+ if( shuffle ){
+ mlist = new ArrayList<>( testMethods.subList(0, testMethods.size()) );
+ java.util.Collections.shuffle(mlist);
+ }
+ if( listRunTests ){
+ synchronized(this.getClass()){
+ if( !fromThread ){
+ out("Initial test"," list: ");
+ for(java.lang.reflect.Method m : testMethods){
+ out(m.getName()+" ");
+ }
+ outln();
+ outln("(That list excludes some which are hard-coded to run.)");
+ }
+ out("Running"," tests: ");
+ for(java.lang.reflect.Method m : mlist){
+ out(m.getName()+" ");
+ }
+ outln();
+ }
+ }
+ for(java.lang.reflect.Method m : mlist){
+ nap();
+ try{
+ m.invoke(this);
+ }catch(java.lang.reflect.InvocationTargetException e){
+ outln("FAILURE: ",m.getName(),"(): ", e.getCause());
+ throw e;
+ }
+ }
+ synchronized( this.getClass() ){
+ ++nTestRuns;
+ }
+ }
+
+ public void run() {
+ try {
+ runTests(0!=this.tId);
+ }catch(Exception e){
+ synchronized( listErrors ){
+ listErrors.add(e);
+ }
+ }finally{
+ affirm( CApi.sqlite3_java_uncache_thread() );
+ affirm( !CApi.sqlite3_java_uncache_thread() );
+ }
+ }
+
+ /**
+ Runs the basic sqlite3 JNI binding sanity-check suite.
+
+ CLI flags:
+
+ -q|-quiet: disables most test output.
+
+ -t|-thread N: runs the tests in N threads
+ concurrently. Default=1.
+
+ -r|-repeat N: repeats the tests in a loop N times, each one
+ consisting of the -thread value's threads.
+
+ -shuffle: randomizes the order of most of the test functions.
+
+ -naps: sleep small random intervals between tests in order to add
+ some chaos for cross-thread contention.
+
+ -list-tests: outputs the list of tests being run, minus some
+ which are hard-coded. This is noisy in multi-threaded mode.
+
+ -fail: forces an exception to be thrown during the test run. Use
+ with -shuffle to make its appearance unpredictable.
+
+ -v: emit some developer-mode info at the end.
+ */
+ public static void main(String[] args) throws Exception {
+ Integer nThread = 1;
+ boolean doSomethingForDev = false;
+ Integer nRepeat = 1;
+ boolean forceFail = false;
+ boolean sqlLog = false;
+ boolean configLog = false;
+ boolean squelchTestOutput = false;
+ for( int i = 0; i < args.length; ){
+ String arg = args[i++];
+ if(arg.startsWith("-")){
+ arg = arg.replaceFirst("-+","");
+ if(arg.equals("v")){
+ doSomethingForDev = true;
+ //listBoundMethods();
+ }else if(arg.equals("t") || arg.equals("thread")){
+ nThread = Integer.parseInt(args[i++]);
+ }else if(arg.equals("r") || arg.equals("repeat")){
+ nRepeat = Integer.parseInt(args[i++]);
+ }else if(arg.equals("shuffle")){
+ shuffle = true;
+ }else if(arg.equals("list-tests")){
+ listRunTests = true;
+ }else if(arg.equals("fail")){
+ forceFail = true;
+ }else if(arg.equals("sqllog")){
+ sqlLog = true;
+ }else if(arg.equals("configlog")){
+ configLog = true;
+ }else if(arg.equals("naps")){
+ takeNaps = true;
+ }else if(arg.equals("q") || arg.equals("quiet")){
+ squelchTestOutput = true;
+ }else{
+ throw new IllegalArgumentException("Unhandled flag:"+arg);
+ }
+ }
+ }
+
+ if( sqlLog ){
+ if( CApi.sqlite3_compileoption_used("ENABLE_SQLLOG") ){
+ final ConfigSqllogCallback log = new ConfigSqllogCallback() {
+ @Override public void call(sqlite3 db, String msg, int op){
+ switch(op){
+ case 0: outln("Opening db: ",db); break;
+ case 1: outln("SQL ",db,": ",msg); break;
+ case 2: outln("Closing db: ",db); break;
+ }
+ }
+ };
+ int rc = CApi.sqlite3_config( log );
+ affirm( 0==rc );
+ rc = CApi.sqlite3_config( (ConfigSqllogCallback)null );
+ affirm( 0==rc );
+ rc = CApi.sqlite3_config( log );
+ affirm( 0==rc );
+ }else{
+ outln("WARNING: -sqllog is not active because library was built ",
+ "without SQLITE_ENABLE_SQLLOG.");
+ }
+ }
+ if( configLog ){
+ final ConfigLogCallback log = new ConfigLogCallback() {
+ @Override public void call(int code, String msg){
+ outln("ConfigLogCallback: ",ResultCode.getEntryForInt(code),": ", msg);
+ };
+ };
+ int rc = CApi.sqlite3_config( log );
+ affirm( 0==rc );
+ rc = CApi.sqlite3_config( (ConfigLogCallback)null );
+ affirm( 0==rc );
+ rc = CApi.sqlite3_config( log );
+ affirm( 0==rc );
+ }
+
+ quietMode = squelchTestOutput;
+ outln("If you just saw warning messages regarding CallStaticObjectMethod, ",
+ "you are very likely seeing the side effects of a known openjdk8 ",
+ "bug. It is unsightly but does not affect the library.");
+
+ {
+ // Build list of tests to run from the methods named test*().
+ testMethods = new ArrayList<>();
+ int nSkipped = 0;
+ for(final java.lang.reflect.Method m : Tester2.class.getDeclaredMethods()){
+ final String name = m.getName();
+ if( name.equals("testFail") ){
+ if( forceFail ){
+ testMethods.add(m);
+ }
+ }else if( !m.isAnnotationPresent( ManualTest.class ) ){
+ if( nThread>1 && m.isAnnotationPresent( SingleThreadOnly.class ) ){
+ if( 0==nSkipped++ ){
+ out("Skipping tests in multi-thread mode:");
+ }
+ out(" "+name+"()");
+ }else if( name.startsWith("test") ){
+ testMethods.add(m);
+ }
+ }
+ }
+ if( nSkipped>0 ) out("\n");
+ }
+
+ final long timeStart = System.currentTimeMillis();
+ int nLoop = 0;
+ switch( CApi.sqlite3_threadsafe() ){ /* Sanity checking */
+ case 0:
+ affirm( CApi.SQLITE_ERROR==CApi.sqlite3_config( CApi.SQLITE_CONFIG_SINGLETHREAD ),
+ "Could not switch to single-thread mode." );
+ affirm( CApi.SQLITE_ERROR==CApi.sqlite3_config( CApi.SQLITE_CONFIG_MULTITHREAD ),
+ "Could switch to multithread mode." );
+ affirm( CApi.SQLITE_ERROR==CApi.sqlite3_config( CApi.SQLITE_CONFIG_SERIALIZED ),
+ "Could not switch to serialized threading mode." );
+ outln("This is a single-threaded build. Not using threads.");
+ nThread = 1;
+ break;
+ case 1:
+ case 2:
+ affirm( 0==CApi.sqlite3_config( CApi.SQLITE_CONFIG_SINGLETHREAD ),
+ "Could not switch to single-thread mode." );
+ affirm( 0==CApi.sqlite3_config( CApi.SQLITE_CONFIG_MULTITHREAD ),
+ "Could not switch to multithread mode." );
+ affirm( 0==CApi.sqlite3_config( CApi.SQLITE_CONFIG_SERIALIZED ),
+ "Could not switch to serialized threading mode." );
+ break;
+ default:
+ affirm( false, "Unhandled SQLITE_THREADSAFE value." );
+ }
+ outln("libversion_number: ",
+ CApi.sqlite3_libversion_number(),"\n",
+ CApi.sqlite3_libversion(),"\n",CApi.SQLITE_SOURCE_ID,"\n",
+ "SQLITE_THREADSAFE=",CApi.sqlite3_threadsafe());
+ final boolean showLoopCount = (nRepeat>1 && nThread>1);
+ if( showLoopCount ){
+ outln("Running ",nRepeat," loop(s) with ",nThread," thread(s) each.");
+ }
+ if( takeNaps ) outln("Napping between tests is enabled.");
+ for( int n = 0; n < nRepeat; ++n ){
+ ++nLoop;
+ if( showLoopCount ) out((1==nLoop ? "" : " ")+nLoop);
+ if( nThread<=1 ){
+ new Tester2(0).runTests(false);
+ continue;
+ }
+ Tester2.mtMode = true;
+ final ExecutorService ex = Executors.newFixedThreadPool( nThread );
+ for( int i = 0; i < nThread; ++i ){
+ ex.submit( new Tester2(i), i );
+ }
+ ex.shutdown();
+ try{
+ ex.awaitTermination(nThread*200, java.util.concurrent.TimeUnit.MILLISECONDS);
+ ex.shutdownNow();
+ }catch (InterruptedException ie){
+ ex.shutdownNow();
+ Thread.currentThread().interrupt();
+ }
+ if( !listErrors.isEmpty() ){
+ quietMode = false;
+ outln("TEST ERRORS:");
+ Exception err = null;
+ for( Exception e : listErrors ){
+ e.printStackTrace();
+ if( null==err ) err = e;
+ }
+ if( null!=err ) throw err;
+ }
+ }
+ if( showLoopCount ) outln();
+ quietMode = false;
+
+ final long timeEnd = System.currentTimeMillis();
+ outln("Tests done. Metrics across ",nTestRuns," total iteration(s):");
+ outln("\tAssertions checked: ",affirmCount);
+ outln("\tDatabases opened: ",metrics.dbOpen);
+ if( doSomethingForDev ){
+ CApi.sqlite3_jni_internal_details();
+ }
+ affirm( 0==CApi.sqlite3_release_memory(1) );
+ CApi.sqlite3_shutdown();
+ int nMethods = 0;
+ int nNatives = 0;
+ int nCanonical = 0;
+ final java.lang.reflect.Method[] declaredMethods =
+ CApi.class.getDeclaredMethods();
+ for(java.lang.reflect.Method m : declaredMethods){
+ final int mod = m.getModifiers();
+ if( 0!=(mod & java.lang.reflect.Modifier.STATIC) ){
+ final String name = m.getName();
+ if(name.startsWith("sqlite3_")){
+ ++nMethods;
+ if( 0!=(mod & java.lang.reflect.Modifier.NATIVE) ){
+ ++nNatives;
+ }
+ }
+ }
+ }
+ outln("\tCApi.sqlite3_*() methods: "+
+ nMethods+" total, with "+
+ nNatives+" native, "+
+ (nMethods - nNatives)+" Java"
+ );
+ outln("\tTotal test time = "
+ +(timeEnd - timeStart)+"ms");
+ }
+}
diff --git a/ext/jni/src/org/sqlite/jni/wrapper1/ValueHolder.java b/ext/jni/src/org/sqlite/jni/wrapper1/ValueHolder.java
new file mode 100644
index 0000000000..009936a43e
--- /dev/null
+++ b/ext/jni/src/org/sqlite/jni/wrapper1/ValueHolder.java
@@ -0,0 +1,25 @@
+/*
+** 2023-10-16
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file contains a set of tests for the sqlite3 JNI bindings.
+*/
+package org.sqlite.jni.wrapper1;
+
+/**
+ A helper class which simply holds a single value. Its primary use
+ is for communicating values out of anonymous classes, as doing so
+ requires a "final" reference.
+*/
+public class ValueHolder {
+ public T value;
+ public ValueHolder(){}
+ public ValueHolder(T v){value = v;}
+}
diff --git a/ext/jni/src/tests/000-000-sanity.test b/ext/jni/src/tests/000-000-sanity.test
index 2bfacb1cec..4ccbece31c 100644
--- a/ext/jni/src/tests/000-000-sanity.test
+++ b/ext/jni/src/tests/000-000-sanity.test
@@ -6,7 +6,8 @@
** xMODULE_NAME: module-name
** xREQUIRED_PROPERTIES: small fast reliable
** xREQUIRED_PROPERTIES: RECURSIVE_TRIGGERS
-** xREQUIRED_PROPERTIES: TEMPSTORE_MEM TEMPSTORE_FILE
+** xREQUIRED_PROPERTIES: TEMPSTORE_FILE TEMPSTORE_MEM
+** xREQUIRED_PROPERTIES: AUTOVACUUM INCRVACUUM
**
*/
--print starting up 😃
diff --git a/ext/jni/src/tests/900-001-fts.test b/ext/jni/src/tests/900-001-fts.test
new file mode 100644
index 0000000000..65285e86b0
--- /dev/null
+++ b/ext/jni/src/tests/900-001-fts.test
@@ -0,0 +1,12 @@
+/*
+** SCRIPT_MODULE_NAME: fts5-sanity-checks
+** xREQUIRED_PROPERTIES: FTS5
+**
+*/
+
+--testcase 1.0
+CREATE VIRTUAL TABLE email USING fts5(sender, title, body);
+insert into email values('fred','Help!','Dear Sir...');
+insert into email values('barney','Assistance','Dear Madam...');
+select * from email where email match 'assistance';
+--result barney Assistance {Dear Madam...}
diff --git a/ext/lsm1/lsm_vtab.c b/ext/lsm1/lsm_vtab.c
index bb1460297d..8c21923e1a 100644
--- a/ext/lsm1/lsm_vtab.c
+++ b/ext/lsm1/lsm_vtab.c
@@ -1061,6 +1061,11 @@ static sqlite3_module lsm1Module = {
lsm1Rollback, /* xRollback */
0, /* xFindMethod */
0, /* xRename */
+ 0, /* xSavepoint */
+ 0, /* xRelease */
+ 0, /* xRollbackTo */
+ 0, /* xShadowName */
+ 0 /* xIntegrity */
};
diff --git a/ext/misc/amatch.c b/ext/misc/amatch.c
index bafa43283a..dd9bee53d7 100644
--- a/ext/misc/amatch.c
+++ b/ext/misc/amatch.c
@@ -1475,7 +1475,8 @@ static sqlite3_module amatchModule = {
0, /* xSavepoint */
0, /* xRelease */
0, /* xRollbackTo */
- 0 /* xShadowName */
+ 0, /* xShadowName */
+ 0 /* xIntegrity */
};
#endif /* SQLITE_OMIT_VIRTUALTABLE */
diff --git a/ext/misc/btreeinfo.c b/ext/misc/btreeinfo.c
index 22f8268139..02f8c0319c 100644
--- a/ext/misc/btreeinfo.c
+++ b/ext/misc/btreeinfo.c
@@ -411,7 +411,8 @@ int sqlite3BinfoRegister(sqlite3 *db){
0, /* xSavepoint */
0, /* xRelease */
0, /* xRollbackTo */
- 0 /* xShadowName */
+ 0, /* xShadowName */
+ 0 /* xIntegrity */
};
return sqlite3_create_module(db, "sqlite_btreeinfo", &binfo_module, 0);
}
diff --git a/ext/misc/carray.c b/ext/misc/carray.c
index 709c894f27..b1caa98c3f 100644
--- a/ext/misc/carray.c
+++ b/ext/misc/carray.c
@@ -409,6 +409,11 @@ static sqlite3_module carrayModule = {
0, /* xRollback */
0, /* xFindMethod */
0, /* xRename */
+ 0, /* xSavepoint */
+ 0, /* xRelease */
+ 0, /* xRollbackTo */
+ 0, /* xShadow */
+ 0 /* xIntegrity */
};
/*
diff --git a/ext/misc/closure.c b/ext/misc/closure.c
index db9b2b7394..79a5a21d1e 100644
--- a/ext/misc/closure.c
+++ b/ext/misc/closure.c
@@ -939,7 +939,8 @@ static sqlite3_module closureModule = {
0, /* xSavepoint */
0, /* xRelease */
0, /* xRollbackTo */
- 0 /* xShadowName */
+ 0, /* xShadowName */
+ 0 /* xIntegrity */
};
#endif /* SQLITE_OMIT_VIRTUALTABLE */
diff --git a/ext/misc/completion.c b/ext/misc/completion.c
index d9e7b85972..987595a3d6 100644
--- a/ext/misc/completion.c
+++ b/ext/misc/completion.c
@@ -470,7 +470,8 @@ static sqlite3_module completionModule = {
0, /* xSavepoint */
0, /* xRelease */
0, /* xRollbackTo */
- 0 /* xShadowName */
+ 0, /* xShadowName */
+ 0 /* xIntegrity */
};
#endif /* SQLITE_OMIT_VIRTUALTABLE */
diff --git a/ext/misc/csv.c b/ext/misc/csv.c
index 870a0cf60e..b38500f4b9 100644
--- a/ext/misc/csv.c
+++ b/ext/misc/csv.c
@@ -897,6 +897,11 @@ static sqlite3_module CsvModule = {
0, /* xRollback */
0, /* xFindMethod */
0, /* xRename */
+ 0, /* xSavepoint */
+ 0, /* xRelease */
+ 0, /* xRollbackTo */
+ 0, /* xShadowName */
+ 0 /* xIntegrity */
};
#ifdef SQLITE_TEST
@@ -929,6 +934,11 @@ static sqlite3_module CsvModuleFauxWrite = {
0, /* xRollback */
0, /* xFindMethod */
0, /* xRename */
+ 0, /* xSavepoint */
+ 0, /* xRelease */
+ 0, /* xRollbackTo */
+ 0, /* xShadowName */
+ 0 /* xIntegrity */
};
#endif /* SQLITE_TEST */
diff --git a/ext/misc/explain.c b/ext/misc/explain.c
index 0095194570..726af76b96 100644
--- a/ext/misc/explain.c
+++ b/ext/misc/explain.c
@@ -293,6 +293,7 @@ static sqlite3_module explainModule = {
0, /* xRelease */
0, /* xRollbackTo */
0, /* xShadowName */
+ 0 /* xIntegrity */
};
#endif /* SQLITE_OMIT_VIRTUALTABLE */
diff --git a/ext/misc/fileio.c b/ext/misc/fileio.c
index 7cdbd5968f..70546adfca 100644
--- a/ext/misc/fileio.c
+++ b/ext/misc/fileio.c
@@ -983,6 +983,7 @@ static int fsdirRegister(sqlite3 *db){
0, /* xRelease */
0, /* xRollbackTo */
0, /* xShadowName */
+ 0 /* xIntegrity */
};
int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
diff --git a/ext/misc/fossildelta.c b/ext/misc/fossildelta.c
index 6a597e0d7d..e638737d2b 100644
--- a/ext/misc/fossildelta.c
+++ b/ext/misc/fossildelta.c
@@ -1058,7 +1058,8 @@ static sqlite3_module deltaparsevtabModule = {
/* xSavepoint */ 0,
/* xRelease */ 0,
/* xRollbackTo */ 0,
- /* xShadowName */ 0
+ /* xShadowName */ 0,
+ /* xIntegrity */ 0
};
diff --git a/ext/misc/fuzzer.c b/ext/misc/fuzzer.c
index 65d9d8df69..92b7c0dae0 100644
--- a/ext/misc/fuzzer.c
+++ b/ext/misc/fuzzer.c
@@ -1165,6 +1165,11 @@ static sqlite3_module fuzzerModule = {
0, /* xRollback */
0, /* xFindMethod */
0, /* xRename */
+ 0, /* xSavepoint */
+ 0, /* xRelease */
+ 0, /* xRollbackTo */
+ 0, /* xShadowName */
+ 0 /* xIntegrity */
};
#endif /* SQLITE_OMIT_VIRTUALTABLE */
diff --git a/ext/misc/memstat.c b/ext/misc/memstat.c
index 800a86e7a4..c56af9f297 100644
--- a/ext/misc/memstat.c
+++ b/ext/misc/memstat.c
@@ -396,6 +396,7 @@ static sqlite3_module memstatModule = {
0, /* xRelease */
0, /* xRollbackTo */
0, /* xShadowName */
+ 0 /* xIntegrity */
};
#endif /* SQLITE_OMIT_VIRTUALTABLE */
diff --git a/ext/misc/prefixes.c b/ext/misc/prefixes.c
index 3f053b7f1c..e6517e7195 100644
--- a/ext/misc/prefixes.c
+++ b/ext/misc/prefixes.c
@@ -248,7 +248,8 @@ static sqlite3_module prefixesModule = {
/* xSavepoint */ 0,
/* xRelease */ 0,
/* xRollbackTo */ 0,
- /* xShadowName */ 0
+ /* xShadowName */ 0,
+ /* xIntegrity */ 0
};
/*
diff --git a/ext/misc/qpvtab.c b/ext/misc/qpvtab.c
index fb0c155a27..b7c2a05126 100644
--- a/ext/misc/qpvtab.c
+++ b/ext/misc/qpvtab.c
@@ -439,7 +439,8 @@ static sqlite3_module qpvtabModule = {
/* xSavepoint */ 0,
/* xRelease */ 0,
/* xRollbackTo */ 0,
- /* xShadowName */ 0
+ /* xShadowName */ 0,
+ /* xIntegrity */ 0
};
#endif /* SQLITE_OMIT_VIRTUALTABLE */
diff --git a/ext/misc/series.c b/ext/misc/series.c
index 3bd567b2ef..abd6af7ad6 100644
--- a/ext/misc/series.c
+++ b/ext/misc/series.c
@@ -557,7 +557,8 @@ static sqlite3_module seriesModule = {
0, /* xSavepoint */
0, /* xRelease */
0, /* xRollbackTo */
- 0 /* xShadowName */
+ 0, /* xShadowName */
+ 0 /* xIntegrity */
};
#endif /* SQLITE_OMIT_VIRTUALTABLE */
diff --git a/ext/misc/spellfix.c b/ext/misc/spellfix.c
index 3b78d9f07d..a0c5aafd10 100644
--- a/ext/misc/spellfix.c
+++ b/ext/misc/spellfix.c
@@ -3009,6 +3009,11 @@ static sqlite3_module spellfix1Module = {
0, /* xRollback */
0, /* xFindMethod */
spellfix1Rename, /* xRename */
+ 0, /* xSavepoint */
+ 0, /* xRelease */
+ 0, /* xRollbackTo */
+ 0, /* xShadowName */
+ 0 /* xIntegrity */
};
/*
diff --git a/ext/misc/stmt.c b/ext/misc/stmt.c
index 4819cbe673..cc1aaa8493 100644
--- a/ext/misc/stmt.c
+++ b/ext/misc/stmt.c
@@ -314,6 +314,7 @@ static sqlite3_module stmtModule = {
0, /* xRelease */
0, /* xRollbackTo */
0, /* xShadowName */
+ 0 /* xIntegrity */
};
#endif /* SQLITE_OMIT_VIRTUALTABLE */
diff --git a/ext/misc/templatevtab.c b/ext/misc/templatevtab.c
index d7efa2b40e..5865f5214b 100644
--- a/ext/misc/templatevtab.c
+++ b/ext/misc/templatevtab.c
@@ -249,7 +249,8 @@ static sqlite3_module templatevtabModule = {
/* xSavepoint */ 0,
/* xRelease */ 0,
/* xRollbackTo */ 0,
- /* xShadowName */ 0
+ /* xShadowName */ 0,
+ /* xIntegrity */ 0
};
diff --git a/ext/misc/unionvtab.c b/ext/misc/unionvtab.c
index 6ac7ca6e95..506ad5ddba 100644
--- a/ext/misc/unionvtab.c
+++ b/ext/misc/unionvtab.c
@@ -1351,7 +1351,8 @@ static int createUnionVtab(sqlite3 *db){
0, /* xSavepoint */
0, /* xRelease */
0, /* xRollbackTo */
- 0 /* xShadowName */
+ 0, /* xShadowName */
+ 0 /* xIntegrity */
};
int rc;
diff --git a/ext/misc/vfsstat.c b/ext/misc/vfsstat.c
index 83a7a3df75..ba22115ab8 100644
--- a/ext/misc/vfsstat.c
+++ b/ext/misc/vfsstat.c
@@ -775,6 +775,11 @@ static sqlite3_module VfsStatModule = {
0, /* xRollback */
0, /* xFindMethod */
0, /* xRename */
+ 0, /* xSavepoint */
+ 0, /* xRelease */
+ 0, /* xRollbackTo */
+ 0, /* xShadowName */
+ 0 /* xIntegrity */
};
/*
diff --git a/ext/misc/vtablog.c b/ext/misc/vtablog.c
index 424b3457ff..e414e7afa7 100644
--- a/ext/misc/vtablog.c
+++ b/ext/misc/vtablog.c
@@ -493,6 +493,7 @@ static sqlite3_module vtablogModule = {
0, /* xRelease */
0, /* xRollbackTo */
0, /* xShadowName */
+ 0 /* xIntegrity */
};
#ifdef _WIN32
diff --git a/ext/misc/wholenumber.c b/ext/misc/wholenumber.c
index 03d6e6902e..4c955925da 100644
--- a/ext/misc/wholenumber.c
+++ b/ext/misc/wholenumber.c
@@ -254,6 +254,11 @@ static sqlite3_module wholenumberModule = {
0, /* xRollback */
0, /* xFindMethod */
0, /* xRename */
+ 0, /* xSavepoint */
+ 0, /* xRelease */
+ 0, /* xRollbackTo */
+ 0, /* xShadowName */
+ 0 /* xIntegrity */
};
#endif /* SQLITE_OMIT_VIRTUALTABLE */
diff --git a/ext/misc/zipfile.c b/ext/misc/zipfile.c
index 4dbf80b197..416a49443b 100644
--- a/ext/misc/zipfile.c
+++ b/ext/misc/zipfile.c
@@ -2200,7 +2200,8 @@ static int zipfileRegister(sqlite3 *db){
0, /* xSavepoint */
0, /* xRelease */
0, /* xRollback */
- 0 /* xShadowName */
+ 0, /* xShadowName */
+ 0 /* xIntegrity */
};
int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0);
diff --git a/ext/recover/dbdata.c b/ext/recover/dbdata.c
index e3bec33d8d..25a6e9fd6a 100644
--- a/ext/recover/dbdata.c
+++ b/ext/recover/dbdata.c
@@ -933,7 +933,8 @@ static int sqlite3DbdataRegister(sqlite3 *db){
0, /* xSavepoint */
0, /* xRelease */
0, /* xRollbackTo */
- 0 /* xShadowName */
+ 0, /* xShadowName */
+ 0 /* xIntegrity */
};
int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
diff --git a/ext/repair/checkindex.c b/ext/repair/checkindex.c
index 080a51530d..5f6e646e44 100644
--- a/ext/repair/checkindex.c
+++ b/ext/repair/checkindex.c
@@ -907,6 +907,8 @@ static int ciInit(sqlite3 *db){
0, /* xSavepoint */
0, /* xRelease */
0, /* xRollbackTo */
+ 0, /* xShadowName */
+ 0 /* xIntegrity */
};
return sqlite3_create_module(db, "incremental_index_check", &cidx_module, 0);
}
diff --git a/ext/rtree/geopoly.c b/ext/rtree/geopoly.c
index 640cb86b20..3e9c2a2713 100644
--- a/ext/rtree/geopoly.c
+++ b/ext/rtree/geopoly.c
@@ -1252,24 +1252,28 @@ static int geopolyInit(
(void)pAux;
sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
+ sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
/* Allocate the sqlite3_vtab structure */
nDb = strlen(argv[1]);
nName = strlen(argv[2]);
- pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName+2);
+ pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName*2+8);
if( !pRtree ){
return SQLITE_NOMEM;
}
- memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
+ memset(pRtree, 0, sizeof(Rtree)+nDb+nName*2+8);
pRtree->nBusy = 1;
pRtree->base.pModule = &rtreeModule;
pRtree->zDb = (char *)&pRtree[1];
pRtree->zName = &pRtree->zDb[nDb+1];
+ pRtree->zNodeName = &pRtree->zName[nName+1];
pRtree->eCoordType = RTREE_COORD_REAL32;
pRtree->nDim = 2;
pRtree->nDim2 = 4;
memcpy(pRtree->zDb, argv[1], nDb);
memcpy(pRtree->zName, argv[2], nName);
+ memcpy(pRtree->zNodeName, argv[2], nName);
+ memcpy(&pRtree->zNodeName[nName], "_node", 6);
/* Create/Connect to the underlying relational database schema. If
@@ -1683,7 +1687,6 @@ static int geopolyUpdate(
}
if( rc==SQLITE_OK ){
int rc2;
- pRtree->iReinsertHeight = -1;
rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
rc2 = nodeRelease(pRtree, pLeaf);
if( rc==SQLITE_OK ){
@@ -1780,7 +1783,8 @@ static sqlite3_module geopolyModule = {
rtreeSavepoint, /* xSavepoint */
0, /* xRelease */
0, /* xRollbackTo */
- rtreeShadowName /* xShadowName */
+ rtreeShadowName, /* xShadowName */
+ rtreeIntegrity /* xIntegrity */
};
static int sqlite3_geopoly_init(sqlite3 *db){
diff --git a/ext/rtree/rtree.c b/ext/rtree/rtree.c
index 4e85cc8aec..682c052c56 100644
--- a/ext/rtree/rtree.c
+++ b/ext/rtree/rtree.c
@@ -166,6 +166,7 @@ struct Rtree {
int iDepth; /* Current depth of the r-tree structure */
char *zDb; /* Name of database containing r-tree table */
char *zName; /* Name of r-tree table */
+ char *zNodeName; /* Name of the %_node table */
u32 nBusy; /* Current number of users of this structure */
i64 nRowEst; /* Estimated number of rows in this table */
u32 nCursor; /* Number of open cursors */
@@ -178,7 +179,6 @@ struct Rtree {
** headed by the node (leaf nodes have RtreeNode.iNode==0).
*/
RtreeNode *pDeleted;
- int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */
/* Blob I/O on xxx_node */
sqlite3_blob *pNodeBlob;
@@ -200,7 +200,7 @@ struct Rtree {
/* Statement for writing to the "aux:" fields, if there are any */
sqlite3_stmt *pWriteAux;
- RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
+ RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
};
/* Possible values for Rtree.eCoordType: */
@@ -475,15 +475,20 @@ struct RtreeMatchArg {
** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
** at run-time.
*/
-#ifndef SQLITE_BYTEORDER
-# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
+#ifndef SQLITE_BYTEORDER /* Replicate changes at tag-20230904a */
+# if defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
+# define SQLITE_BYTEORDER 4321
+# elif defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
+# define SQLITE_BYTEORDER 1234
+# elif defined(__BIG_ENDIAN__) && __BIG_ENDIAN__==1
+# define SQLITE_BYTEORDER 4321
+# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64)
-# define SQLITE_BYTEORDER 1234
-# elif defined(sparc) || defined(__ppc__) || \
- defined(__ARMEB__) || defined(__AARCH64EB__)
-# define SQLITE_BYTEORDER 4321
+# define SQLITE_BYTEORDER 1234
+# elif defined(sparc) || defined(__ARMEB__) || defined(__AARCH64EB__)
+# define SQLITE_BYTEORDER 4321
# else
# define SQLITE_BYTEORDER 0
# endif
@@ -732,11 +737,9 @@ static int nodeAcquire(
}
}
if( pRtree->pNodeBlob==0 ){
- char *zTab = sqlite3_mprintf("%s_node", pRtree->zName);
- if( zTab==0 ) return SQLITE_NOMEM;
- rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0,
+ rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, pRtree->zNodeName,
+ "data", iNode, 0,
&pRtree->pNodeBlob);
- sqlite3_free(zTab);
}
if( rc ){
nodeBlobReset(pRtree);
@@ -2077,8 +2080,12 @@ static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
pIdxInfo->idxNum = 2;
pIdxInfo->needToFreeIdxStr = 1;
- if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
- return SQLITE_NOMEM;
+ if( iIdx>0 ){
+ pIdxInfo->idxStr = sqlite3_malloc( iIdx+1 );
+ if( pIdxInfo->idxStr==0 ){
+ return SQLITE_NOMEM;
+ }
+ memcpy(pIdxInfo->idxStr, zIdxStr, iIdx+1);
}
nRow = pRtree->nRowEst >> (iIdx/2);
@@ -2157,31 +2164,22 @@ static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
*/
static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
int ii;
- int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
- for(ii=0; iinDim2; ii+=2){
- RtreeCoord *a1 = &p1->aCoord[ii];
- RtreeCoord *a2 = &p2->aCoord[ii];
- if( (!isInt && (a2[0].fa1[1].f))
- || ( isInt && (a2[0].ia1[1].i))
- ){
- return 0;
+ if( pRtree->eCoordType==RTREE_COORD_INT32 ){
+ for(ii=0; iinDim2; ii+=2){
+ RtreeCoord *a1 = &p1->aCoord[ii];
+ RtreeCoord *a2 = &p2->aCoord[ii];
+ if( a2[0].ia1[1].i ) return 0;
+ }
+ }else{
+ for(ii=0; iinDim2; ii+=2){
+ RtreeCoord *a1 = &p1->aCoord[ii];
+ RtreeCoord *a2 = &p2->aCoord[ii];
+ if( a2[0].fa1[1].f ) return 0;
}
}
return 1;
}
-/*
-** Return the amount cell p would grow by if it were unioned with pCell.
-*/
-static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
- RtreeDValue area;
- RtreeCell cell;
- memcpy(&cell, p, sizeof(RtreeCell));
- area = cellArea(pRtree, &cell);
- cellUnion(pRtree, &cell, pCell);
- return (cellArea(pRtree, &cell)-area);
-}
-
static RtreeDValue cellOverlap(
Rtree *pRtree,
RtreeCell *p,
@@ -2228,38 +2226,52 @@ static int ChooseLeaf(
for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
int iCell;
sqlite3_int64 iBest = 0;
-
+ int bFound = 0;
RtreeDValue fMinGrowth = RTREE_ZERO;
RtreeDValue fMinArea = RTREE_ZERO;
-
int nCell = NCELL(pNode);
- RtreeCell cell;
RtreeNode *pChild = 0;
- RtreeCell *aCell = 0;
-
- /* Select the child node which will be enlarged the least if pCell
- ** is inserted into it. Resolve ties by choosing the entry with
- ** the smallest area.
+ /* First check to see if there is are any cells in pNode that completely
+ ** contains pCell. If two or more cells in pNode completely contain pCell
+ ** then pick the smallest.
*/
for(iCell=0; iCell1 ){
- int iLeft = 0;
- int iRight = 0;
-
- int nLeft = nIdx/2;
- int nRight = nIdx-nLeft;
- int *aLeft = aIdx;
- int *aRight = &aIdx[nLeft];
-
- SortByDistance(aLeft, nLeft, aDistance, aSpare);
- SortByDistance(aRight, nRight, aDistance, aSpare);
-
- memcpy(aSpare, aLeft, sizeof(int)*nLeft);
- aLeft = aSpare;
-
- while( iLeftnDim; iDim++){
- aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
- aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
- }
- }
- for(iDim=0; iDimnDim; iDim++){
- aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
- }
-
- for(ii=0; iinDim; iDim++){
- RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) -
- DCOORD(aCell[ii].aCoord[iDim*2]));
- aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
- }
- }
-
- SortByDistance(aOrder, nCell, aDistance, aSpare);
- nodeZero(pRtree, pNode);
-
- for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
- RtreeCell *p = &aCell[aOrder[ii]];
- nodeInsertCell(pRtree, pNode, p);
- if( p->iRowid==pCell->iRowid ){
- if( iHeight==0 ){
- rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
- }else{
- rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
- }
- }
- }
- if( rc==SQLITE_OK ){
- rc = fixBoundingBox(pRtree, pNode);
- }
- for(; rc==SQLITE_OK && iiiNode currently contains
- ** the height of the sub-tree headed by the cell.
- */
- RtreeNode *pInsert;
- RtreeCell *p = &aCell[aOrder[ii]];
- rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
- if( rc==SQLITE_OK ){
- int rc2;
- rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
- rc2 = nodeRelease(pRtree, pInsert);
- if( rc==SQLITE_OK ){
- rc = rc2;
- }
- }
- }
-
- sqlite3_free(aCell);
- return rc;
-}
-
+
/*
** Insert cell pCell into node pNode. Node pNode is the head of a
** subtree iHeight high (leaf nodes have iHeight==0).
@@ -3008,12 +2848,7 @@ static int rtreeInsertCell(
}
}
if( nodeInsertCell(pRtree, pNode, pCell) ){
- if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
- rc = SplitNode(pRtree, pNode, pCell, iHeight);
- }else{
- pRtree->iReinsertHeight = iHeight;
- rc = Reinsert(pRtree, pNode, pCell, iHeight);
- }
+ rc = SplitNode(pRtree, pNode, pCell, iHeight);
}else{
rc = AdjustTree(pRtree, pNode, pCell);
if( ALWAYS(rc==SQLITE_OK) ){
@@ -3356,7 +3191,6 @@ static int rtreeUpdate(
}
if( rc==SQLITE_OK ){
int rc2;
- pRtree->iReinsertHeight = -1;
rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
rc2 = nodeRelease(pRtree, pLeaf);
if( rc==SQLITE_OK ){
@@ -3497,8 +3331,11 @@ static int rtreeShadowName(const char *zName){
return 0;
}
+/* Forward declaration */
+static int rtreeIntegrity(sqlite3_vtab*, char**);
+
static sqlite3_module rtreeModule = {
- 3, /* iVersion */
+ 4, /* iVersion */
rtreeCreate, /* xCreate - create a table */
rtreeConnect, /* xConnect - connect to an existing table */
rtreeBestIndex, /* xBestIndex - Determine search strategy */
@@ -3521,7 +3358,8 @@ static sqlite3_module rtreeModule = {
rtreeSavepoint, /* xSavepoint */
0, /* xRelease */
0, /* xRollbackTo */
- rtreeShadowName /* xShadowName */
+ rtreeShadowName, /* xShadowName */
+ rtreeIntegrity /* xIntegrity */
};
static int rtreeSqlInit(
@@ -3777,22 +3615,27 @@ static int rtreeInit(
}
sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
+ sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
+
/* Allocate the sqlite3_vtab structure */
nDb = (int)strlen(argv[1]);
nName = (int)strlen(argv[2]);
- pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName+2);
+ pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName*2+8);
if( !pRtree ){
return SQLITE_NOMEM;
}
- memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
+ memset(pRtree, 0, sizeof(Rtree)+nDb+nName*2+8);
pRtree->nBusy = 1;
pRtree->base.pModule = &rtreeModule;
pRtree->zDb = (char *)&pRtree[1];
pRtree->zName = &pRtree->zDb[nDb+1];
+ pRtree->zNodeName = &pRtree->zName[nName+1];
pRtree->eCoordType = (u8)eCoordType;
memcpy(pRtree->zDb, argv[1], nDb);
memcpy(pRtree->zName, argv[2], nName);
+ memcpy(pRtree->zNodeName, argv[2], nName);
+ memcpy(&pRtree->zNodeName[nName], "_node", 6);
/* Create/Connect to the underlying relational database schema. If
@@ -4289,7 +4132,6 @@ static int rtreeCheckTable(
){
RtreeCheck check; /* Common context for various routines */
sqlite3_stmt *pStmt = 0; /* Used to find column count of rtree table */
- int bEnd = 0; /* True if transaction should be closed */
int nAux = 0; /* Number of extra columns. */
/* Initialize the context object */
@@ -4298,14 +4140,6 @@ static int rtreeCheckTable(
check.zDb = zDb;
check.zTab = zTab;
- /* If there is not already an open transaction, open one now. This is
- ** to ensure that the queries run as part of this integrity-check operate
- ** on a consistent snapshot. */
- if( sqlite3_get_autocommit(db) ){
- check.rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
- bEnd = 1;
- }
-
/* Find the number of auxiliary columns */
if( check.rc==SQLITE_OK ){
pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.'%q_rowid'", zDb, zTab);
@@ -4346,15 +4180,24 @@ static int rtreeCheckTable(
sqlite3_finalize(check.aCheckMapping[0]);
sqlite3_finalize(check.aCheckMapping[1]);
- /* If one was opened, close the transaction */
- if( bEnd ){
- int rc = sqlite3_exec(db, "END", 0, 0, 0);
- if( check.rc==SQLITE_OK ) check.rc = rc;
- }
*pzReport = check.zReport;
return check.rc;
}
+/*
+** Implementation of the xIntegrity method for Rtree.
+*/
+static int rtreeIntegrity(sqlite3_vtab *pVtab, char **pzErr){
+ Rtree *pRtree = (Rtree*)pVtab;
+ int rc;
+ rc = rtreeCheckTable(pRtree->db, pRtree->zDb, pRtree->zName, pzErr);
+ if( rc==SQLITE_OK && *pzErr ){
+ *pzErr = sqlite3_mprintf("In RTree %s.%s:\n%z",
+ pRtree->zDb, pRtree->zName, *pzErr);
+ }
+ return rc;
+}
+
/*
** Usage:
**
diff --git a/ext/rtree/rtree1.test b/ext/rtree/rtree1.test
index 633d0a5d5f..2d8458a538 100644
--- a/ext/rtree/rtree1.test
+++ b/ext/rtree/rtree1.test
@@ -784,4 +784,15 @@ do_execsql_test 22.1 {
SELECT id, x0 > 9223372036854775807 AS 'a0' FROM t1;
} {123 1}
+# 2023-10-14 dbsqlfuzz --sql-fuzz find. rtreecheck() should not call
+# BEGIN/COMMIT because that causes problems with statement transactions,
+# and it is unnecessary.
+#
+reset_db
+do_test 23.0 {
+ db eval {CREATE TABLE t1(a,b,c);}
+ catch {db eval {CREATE TABLE t2 AS SELECT rtreecheck('t1') AS y;}}
+ db eval {PRAGMA integrity_check;}
+} {ok}
+
finish_test
diff --git a/ext/rtree/rtree8.test b/ext/rtree/rtree8.test
index 12e75a6850..51bd404164 100644
--- a/ext/rtree/rtree8.test
+++ b/ext/rtree/rtree8.test
@@ -75,7 +75,7 @@ do_test rtree8-1.2.2 { nested_select 1 } {51}
#
populate_t1 1500
do_rtree_integrity_test rtree8-1.3.0 t1
-do_execsql_test rtree8-1.3.1 { SELECT max(nodeno) FROM t1_node } {164}
+do_execsql_test rtree8-1.3.1 { SELECT max(nodeno) FROM t1_node } {183}
do_test rtree8-1.3.2 {
set rowids [execsql {SELECT min(rowid) FROM t1_rowid GROUP BY nodeno}]
set stmt_list [list]
diff --git a/ext/rtree/rtreeA.test b/ext/rtree/rtreeA.test
index 301cd4fc62..0b52070c42 100644
--- a/ext/rtree/rtreeA.test
+++ b/ext/rtree/rtreeA.test
@@ -113,7 +113,7 @@ do_execsql_test rtreeA-1.1.1 {
SELECT rtreecheck('main', 't1')
} {{Node 1 missing from database
Wrong number of entries in %_rowid table - expected 0, actual 500
-Wrong number of entries in %_parent table - expected 0, actual 23}}
+Wrong number of entries in %_parent table - expected 0, actual 25}}
do_execsql_test rtreeA-1.2.0 { DROP TABLE t1_node } {}
do_corruption_tests rtreeA-1.2 -error "database disk image is malformed" {
@@ -191,7 +191,7 @@ do_execsql_test rtreeA-3.3.3.4 {
SELECT rtreecheck('main', 't1')
} {{Rtree depth out of range (65535)
Wrong number of entries in %_rowid table - expected 0, actual 499
-Wrong number of entries in %_parent table - expected 0, actual 23}}
+Wrong number of entries in %_parent table - expected 0, actual 25}}
#-------------------------------------------------------------------------
# Set the "number of entries" field on some nodes incorrectly.
diff --git a/ext/rtree/rtree_util.tcl b/ext/rtree/rtree_util.tcl
index afa588e4e9..5640baf4e0 100644
--- a/ext/rtree/rtree_util.tcl
+++ b/ext/rtree/rtree_util.tcl
@@ -192,6 +192,6 @@ proc rtree_treedump {db zTab} {
}
proc do_rtree_integrity_test {tn tbl} {
- uplevel [list do_execsql_test $tn "SELECT rtreecheck('$tbl')" ok]
+ uplevel [list do_execsql_test $tn.1 "SELECT rtreecheck('$tbl')" ok]
+ uplevel [list do_execsql_test $tn.2 "PRAGMA integrity_check" ok]
}
-
diff --git a/ext/rtree/rtreecheck.test b/ext/rtree/rtreecheck.test
index ff5397e1e1..7a98f9bf4e 100644
--- a/ext/rtree/rtreecheck.test
+++ b/ext/rtree/rtreecheck.test
@@ -78,6 +78,11 @@ do_execsql_test 2.3 {
SELECT rtreecheck('r1')
} {{Dimension 0 of cell 0 on node 1 is corrupt
Dimension 1 of cell 3 on node 1 is corrupt}}
+do_execsql_test 2.3b {
+ PRAGMA integrity_check;
+} {{In RTree main.r1:
+Dimension 0 of cell 0 on node 1 is corrupt
+Dimension 1 of cell 3 on node 1 is corrupt}}
setup_simple_db
do_execsql_test 2.4 {
@@ -85,12 +90,21 @@ do_execsql_test 2.4 {
SELECT rtreecheck('r1')
} {{Mapping (3 -> 1) missing from %_rowid table
Wrong number of entries in %_rowid table - expected 5, actual 4}}
+do_execsql_test 2.4b {
+ PRAGMA integrity_check
+} {{In RTree main.r1:
+Mapping (3 -> 1) missing from %_rowid table
+Wrong number of entries in %_rowid table - expected 5, actual 4}}
setup_simple_db
do_execsql_test 2.5 {
UPDATE r1_rowid SET nodeno=2 WHERE rowid=3;
SELECT rtreecheck('r1')
} {{Found (3 -> 2) in %_rowid table, expected (3 -> 1)}}
+do_execsql_test 2.5b {
+ PRAGMA integrity_check
+} {{In RTree main.r1:
+Found (3 -> 2) in %_rowid table, expected (3 -> 1)}}
reset_db
do_execsql_test 3.0 {
@@ -104,14 +118,16 @@ do_execsql_test 3.0 {
INSERT INTO r1 VALUES(7, 5, 0x00000080);
INSERT INTO r1 VALUES(8, 5, 0x40490fdb);
INSERT INTO r1 VALUES(9, 0x7f800000, 0x7f900000);
- SELECT rtreecheck('r1')
-} {ok}
+ SELECT rtreecheck('r1');
+ PRAGMA integrity_check;
+} {ok ok}
do_execsql_test 3.1 {
CREATE VIRTUAL TABLE r2 USING rtree_i32(id, x1, x2);
INSERT INTO r2 VALUES(2, -1*(1<<31), -1*(1<<31)+5);
- SELECT rtreecheck('r2')
-} {ok}
+ SELECT rtreecheck('r2');
+ PRAGMA integrity_check;
+} {ok ok}
sqlite3_db_config db DEFENSIVE 0
do_execsql_test 3.2 {
@@ -125,6 +141,11 @@ do_execsql_test 3.3 {
UPDATE r2_node SET data = X'00001234';
SELECT rtreecheck('r2')!='ok';
} {1}
+do_execsql_test 3.4 {
+ PRAGMA integrity_check;
+} {{In RTree main.r2:
+Node 1 is too small for cell count of 4660 (4 bytes)
+Wrong number of entries in %_rowid table - expected 0, actual 1}}
do_execsql_test 4.0 {
CREATE TABLE notanrtree(i);
@@ -181,4 +202,3 @@ if {[permutation]=="inmemory_journal"} {
}
finish_test
-
diff --git a/ext/rtree/rtreedoc.test b/ext/rtree/rtreedoc.test
index b64faa2e99..4e610db8a2 100644
--- a/ext/rtree/rtreedoc.test
+++ b/ext/rtree/rtreedoc.test
@@ -601,11 +601,21 @@ do_execsql_test 2.5.2 {
SELECT A.id FROM demo_index AS A, demo_index AS B
WHERE A.maxX>=B.minX AND A.minX<=B.maxX
AND A.maxY>=B.minY AND A.minY<=B.maxY
- AND B.id=28269;
+ AND B.id=28269 ORDER BY +A.id;
} {
- 28293 28216 28322 28286 28269
- 28215 28336 28262 28291 28320
- 28313 28298 28287
+ 28215
+ 28216
+ 28262
+ 28269
+ 28286
+ 28287
+ 28291
+ 28293
+ 28298
+ 28313
+ 28320
+ 28322
+ 28336
}
# EVIDENCE-OF: R-02723-34107 Note that it is not necessary for all
@@ -1575,7 +1585,7 @@ execsql BEGIN
do_test 3.6 {
execsql { INSERT INTO rt2_parent VALUES(1000, 1000) }
execsql { SELECT rtreecheck('rt2') }
-} {{Wrong number of entries in %_parent table - expected 9, actual 10}}
+} {{Wrong number of entries in %_parent table - expected 10, actual 11}}
execsql ROLLBACK
diff --git a/ext/rtree/rtreefuzz001.test b/ext/rtree/rtreefuzz001.test
index 58fd179ab9..c81c41da30 100644
--- a/ext/rtree/rtreefuzz001.test
+++ b/ext/rtree/rtreefuzz001.test
@@ -1043,7 +1043,7 @@ do_test rtreefuzz001-500 {
| end crash-2e81f5dce5cbd4.db}]
execsql { PRAGMA writable_schema = 1;}
catchsql {UPDATE t1 SET ex= ex ISNULL}
-} {1 {database disk image is malformed}}
+} {0 {}}
do_test rtreefuzz001-600 {
sqlite3 db {}
diff --git a/ext/session/session3.test b/ext/session/session3.test
index ba316348ef..ee955f1376 100644
--- a/ext/session/session3.test
+++ b/ext/session/session3.test
@@ -135,8 +135,8 @@ do_test 2.2.2 {
DROP TABLE t2;
CREATE TABLE t2(a, b PRIMARY KEY, c, d);
}
- list [catch { S changeset } msg] $msg
-} {1 SQLITE_SCHEMA}
+ catch { S changeset }
+} {0}
do_test 2.2.3 {
S delete
sqlite3session S db main
@@ -167,8 +167,8 @@ do_test 2.2.4 {
CREATE TABLE t2(a, b PRIMARY KEY, c, d);
INSERT INTO t2 VALUES(4, 5, 6, 7);
}
- list [catch { S changeset } msg] $msg
-} {1 SQLITE_SCHEMA}
+ catch { S changeset }
+} {0}
do_test 2.3 {
S delete
diff --git a/ext/session/sessionalter.test b/ext/session/sessionalter.test
new file mode 100644
index 0000000000..34424cf275
--- /dev/null
+++ b/ext/session/sessionalter.test
@@ -0,0 +1,238 @@
+# 2023 October 02
+#
+# The author disclaims copyright to this source code. In place of
+# a legal notice, here is a blessing:
+#
+# May you do good and not evil.
+# May you find forgiveness for yourself and forgive others.
+# May you share freely, never taking more than you give.
+#
+#***********************************************************************
+# This file implements that the sessions module interacts well with
+# the ALTER TABLE ADD COLUMN command.
+#
+
+if {![info exists testdir]} {
+ set testdir [file join [file dirname [info script]] .. .. test]
+}
+source [file join [file dirname [info script]] session_common.tcl]
+source $testdir/tester.tcl
+
+ifcapable !session {finish_test; return}
+set testprefix sessionalter
+
+
+forcedelete test.db2
+sqlite3 db2 test.db2
+
+do_execsql_test 1.0 {
+ CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
+}
+
+do_execsql_test -db db2 1.1 {
+ CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c DEFAULT 1234);
+}
+
+do_then_apply_sql {
+ INSERT INTO t1 VALUES(1, 'one');
+ INSERT INTO t1 VALUES(2, 'two');
+}
+
+do_execsql_test -db db2 1.2 {
+ SELECT * FROM t1
+} {
+ 1 one 1234
+ 2 two 1234
+}
+
+do_then_apply_sql {
+ UPDATE t1 SET b='four' WHERE a=2;
+}
+
+do_execsql_test -db db2 1.3 {
+ SELECT * FROM t1
+} {
+ 1 one 1234
+ 2 four 1234
+}
+
+do_then_apply_sql {
+ DELETE FROM t1 WHERE a=1;
+}
+
+do_execsql_test -db db2 1.4 {
+ SELECT * FROM t1
+} {
+ 2 four 1234
+}
+
+db2 close
+
+#--------------------------------------------------------------------------
+reset_db
+
+do_execsql_test 2.0 {
+ CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
+}
+
+do_test 2.1 {
+ sqlite3session S db main
+ S attach t1
+ set {} {}
+} {}
+do_execsql_test 2.2 {
+ INSERT INTO t1 VALUES(1, 2);
+ ALTER TABLE t1 ADD COLUMN c DEFAULT 'abcd';
+ INSERT INTO t1 VALUES(2, 3, 4);
+}
+do_changeset_test 2.3 S {
+ {INSERT t1 0 X.. {} {i 1 i 2 t abcd}}
+ {INSERT t1 0 X.. {} {i 2 i 3 i 4}}
+}
+
+do_iterator_test 2.4 {} {
+ DELETE FROM t1 WHERE a=2;
+ ALTER TABLE t1 ADD COLUMN d DEFAULT 'abcd';
+ ALTER TABLE t1 ADD COLUMN e DEFAULT 5;
+ ALTER TABLE t1 ADD COLUMN f DEFAULT 7.2;
+ -- INSERT INTO t1 VALUES(9, 9, 9, 9);
+} {
+ {DELETE t1 0 X..... {i 2 i 3 i 4 t abcd i 5 f 7.2} {}}
+}
+
+#-------------------------------------------------------------------------
+# Tests of the sqlite3changegroup_xxx() APIs.
+#
+reset_db
+do_execsql_test 3.0 {
+ CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
+ CREATE TABLE t2(x PRIMARY KEY, y);
+ CREATE TABLE t3(x, y);
+ CREATE TABLE t4(y PRIMARY KEY, x) WITHOUT ROWID;
+
+ INSERT INTO t1 VALUES(1, 2), (3, 4), (5, 6);
+ INSERT INTO t2 VALUES('one', 'two'), ('three', 'four'), ('five', 'six');
+ INSERT INTO t3 VALUES(1, 2), (3, 4), (5, 6);
+
+ INSERT INTO t4(x, y) VALUES(1, 2), (3, 4), (5, 6);
+}
+
+db_save_and_close
+foreach {tn sql1 at sql2} {
+ 1 {
+ INSERT INTO t1(x, y) VALUES(7, 8);
+ } {
+ ALTER TABLE t1 ADD COLUMN z DEFAULT 10;
+ } {
+ UPDATE t1 SET y=11 WHERE x=7;
+ }
+
+ 2 {
+ UPDATE t2 SET y='two.two' WHERE x='one';
+ DELETE FROM t2 WHERE x='five';
+ INSERT INTO t2(x, y) VALUES('seven', 'eight');
+ } {
+ ALTER TABLE t2 ADD COLUMN z;
+ ALTER TABLE t2 ADD COLUMN zz;
+ } {
+ }
+
+ 3 {
+ DELETE FROM t2 WHERE x='five';
+ } {
+ ALTER TABLE t2 ADD COLUMN z DEFAULT 'xyz';
+ } {
+ }
+
+ 4 {
+ UPDATE t2 SET y='two.two' WHERE x='three';
+ } {
+ ALTER TABLE t2 ADD COLUMN z;
+ } {
+ UPDATE t2 SET z='abc' WHERE x='one';
+ }
+
+ 5* {
+ UPDATE t2 SET y='two.two' WHERE x='three';
+ } {
+ ALTER TABLE t2 ADD COLUMN z DEFAULT 'defu1';
+ } {
+ }
+
+ 6* {
+ INSERT INTO t2(x, y) VALUES('nine', 'ten');
+ } {
+ ALTER TABLE t2 ADD COLUMN z;
+ ALTER TABLE t2 ADD COLUMN a DEFAULT 'eelve';
+ ALTER TABLE t2 ADD COLUMN b DEFAULT x'1234abcd';
+ ALTER TABLE t2 ADD COLUMN c DEFAULT 4.2;
+ ALTER TABLE t2 ADD COLUMN d DEFAULT NULL;
+ } {
+ }
+
+ 7 {
+ INSERT INTO t3(x, y) VALUES(7, 8);
+ UPDATE t3 SET y='fourteen' WHERE x=1;
+ DELETE FROM t3 WHERE x=3;
+ } {
+ ALTER TABLE t3 ADD COLUMN c;
+ } {
+ INSERT INTO t3(x, y, c) VALUES(9, 10, 11);
+ }
+
+ 8 {
+ INSERT INTO t4(x, y) VALUES(7, 8);
+ UPDATE t4 SET y='fourteen' WHERE x=1;
+ DELETE FROM t4 WHERE x=3;
+ } {
+ ALTER TABLE t4 ADD COLUMN c;
+ } {
+ INSERT INTO t4(x, y, c) VALUES(9, 10, 11);
+ }
+} {
+ foreach {tn2 cmd} {
+ 1 changeset_from_sql
+ 2 patchset_from_sql
+ } {
+ db_restore_and_reopen
+
+ set C1 [$cmd $sql1]
+ execsql $at
+ set C2 [$cmd $sql2]
+
+ sqlite3changegroup grp
+ grp schema db main
+ grp add $C1
+ grp add $C2
+ set T1 [grp output]
+ grp delete
+
+ db_restore_and_reopen
+ execsql $at
+ set T2 [$cmd "$sql1 ; $sql2"]
+
+ if {[string range $tn end end]!="*"} {
+ do_test 3.1.$tn.$tn2.1 { changeset_to_list $T1 } [changeset_to_list $T2]
+ set testname "$tn.$tn2"
+ } else {
+ set testname "[string range $tn 0 end-1].$tn2"
+ }
+
+ db_restore_and_reopen
+ proc xConflict {args} { return "REPLACE" }
+ sqlite3changeset_apply_v2 db $T1 xConflict
+ set S1 [scksum db main]
+
+ db_restore_and_reopen
+ sqlite3changeset_apply_v2 db $T2 xConflict
+ set S2 [scksum db main]
+
+ # if { $tn==7 } { puts [changeset_to_list $T1] }
+
+ do_test 3.1.$tn.2 { set S1 } $S2
+ }
+}
+
+
+finish_test
+
diff --git a/ext/session/sessionfault3.test b/ext/session/sessionfault3.test
new file mode 100644
index 0000000000..af5a4cdb43
--- /dev/null
+++ b/ext/session/sessionfault3.test
@@ -0,0 +1,59 @@
+# 2016 October 6
+#
+# The author disclaims copyright to this source code. In place of
+# a legal notice, here is a blessing:
+#
+# May you do good and not evil.
+# May you find forgiveness for yourself and forgive others.
+# May you share freely, never taking more than you give.
+#
+#***********************************************************************
+#
+# The focus of this file is testing the session module.
+#
+
+if {![info exists testdir]} {
+ set testdir [file join [file dirname [info script]] .. .. test]
+}
+source [file join [file dirname [info script]] session_common.tcl]
+source $testdir/tester.tcl
+ifcapable !session {finish_test; return}
+set testprefix sessionfault3
+
+do_execsql_test 1.0 {
+ CREATE TABLE t1(a, b, PRIMARY KEY(a));
+ INSERT INTO t1 VALUES(1, 2);
+ INSERT INTO t1 VALUES(3, 4);
+ INSERT INTO t1 VALUES('five', 'six');
+}
+
+set C1 [changeset_from_sql {
+ INSERT INTO t1 VALUES('seven', 'eight');
+ UPDATE t1 SET b=6 WHERE a='five';
+ DELETE FROM t1 WHERE a=1;
+}]
+
+do_execsql_test 1.1 {
+ ALTER TABLE t1 ADD COLUMN d DEFAULT 123;
+ ALTER TABLE t1 ADD COLUMN e DEFAULT 'string';
+}
+
+set C2 [changeset_from_sql {
+ UPDATE t1 SET e='new value' WHERE a='seven';
+ INSERT INTO t1 VALUES(0, 0, 0, 0);
+}]
+
+do_faultsim_test 1 -faults oom* -prep {
+ sqlite3changegroup G
+} -body {
+ G schema db main
+ G add $::C1
+ G add $::C2
+ G output
+ set {} {}
+} -test {
+ catch { G delete }
+ faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
+}
+
+finish_test
diff --git a/ext/session/sessionnoact.test b/ext/session/sessionnoact.test
new file mode 100644
index 0000000000..1274ecb146
--- /dev/null
+++ b/ext/session/sessionnoact.test
@@ -0,0 +1,110 @@
+# 2023 October 20
+#
+# The author disclaims copyright to this source code. In place of
+# a legal notice, here is a blessing:
+#
+# May you do good and not evil.
+# May you find forgiveness for yourself and forgive others.
+# May you share freely, never taking more than you give.
+#
+#***********************************************************************
+# This file implements regression tests for SQLite library.
+#
+
+if {![info exists testdir]} {
+ set testdir [file join [file dirname [info script]] .. .. test]
+}
+source [file join [file dirname [info script]] session_common.tcl]
+source $testdir/tester.tcl
+ifcapable !session {finish_test; return}
+
+set testprefix sessionnoact
+
+do_execsql_test 1.0 {
+ CREATE TABLE p1(a INTEGER PRIMARY KEY, b, c UNIQUE);
+ INSERT INTO p1 VALUES(1, 1, 'one');
+ INSERT INTO p1 VALUES(2, 2, 'two');
+ INSERT INTO p1 VALUES(3, 3, 'three');
+ INSERT INTO p1 VALUES(4, 4, 'four');
+}
+
+db_save
+
+set C [changeset_from_sql {
+ DELETE FROM p1 WHERE a=2;
+ UPDATE p1 SET c='six' WHERE a=3;
+ INSERT INTO p1 VALUES(5, 5, 'two');
+ INSERT INTO p1 VALUES(6, 6, 'three');
+}]
+
+db_restore_and_reopen
+
+do_execsql_test 1.1 {
+ CREATE TABLE c1(x INTEGER PRIMARY KEY, y,
+ FOREIGN KEY(y) REFERENCES p1(c) ON DELETE CASCADE ON UPDATE SET NULL
+ );
+
+ INSERT INTO c1 VALUES(10, 'one');
+ INSERT INTO c1 VALUES(20, 'two');
+ INSERT INTO c1 VALUES(30, 'three');
+ INSERT INTO c1 VALUES(40, 'four');
+}
+
+db_save
+
+do_execsql_test 1.2 {
+ PRAGMA foreign_keys = 1;
+}
+
+set ::nConflict 0
+proc conflict {args} {
+ incr ::nConflict
+ return "OMIT"
+}
+
+sqlite3changeset_apply_v2 db $C conflict
+
+do_execsql_test 1.3 {
+ SELECT * FROM c1
+} {
+ 10 one
+ 30 {}
+ 40 four
+}
+
+db_restore_and_reopen
+
+do_execsql_test 1.4 {
+ PRAGMA foreign_keys = 1;
+}
+
+do_execsql_test 1.5 {
+ UPDATE p1 SET c=12345 WHERE a = 45;
+}
+
+sqlite3changeset_apply_v2 -noaction db $C conflict
+do_execsql_test 1.6 {
+ SELECT * FROM c1
+} {
+ 10 one
+ 20 two
+ 30 three
+ 40 four
+}
+
+do_execsql_test 1.7 {
+ PRAGMA foreign_keys = 1;
+ UPDATE p1 SET c = 'ten' WHERE c='two';
+ SELECT * FROM c1;
+} {
+ 10 one
+ 20 {}
+ 30 three
+ 40 four
+}
+
+do_execsql_test 1.8 {
+ PRAGMA foreign_key_check
+}
+
+finish_test
diff --git a/ext/session/sqlite3session.c b/ext/session/sqlite3session.c
index a048472333..1e8eb664f7 100644
--- a/ext/session/sqlite3session.c
+++ b/ext/session/sqlite3session.c
@@ -127,6 +127,18 @@ struct sqlite3_changeset_iter {
** The data associated with each hash-table entry is a structure containing
** a subset of the initial values that the modified row contained at the
** start of the session. Or no initial values if the row was inserted.
+**
+** pDfltStmt:
+** This is only used by the sqlite3changegroup_xxx() APIs, not by
+** regular sqlite3_session objects. It is a SELECT statement that
+** selects the default value for each table column. For example,
+** if the table is
+**
+** CREATE TABLE xx(a DEFAULT 1, b, c DEFAULT 'abc')
+**
+** then this variable is the compiled version of:
+**
+** SELECT 1, NULL, 'abc'
*/
struct SessionTable {
SessionTable *pNext;
@@ -135,10 +147,12 @@ struct SessionTable {
int bStat1; /* True if this is sqlite_stat1 */
int bRowid; /* True if this table uses rowid for PK */
const char **azCol; /* Column names */
+ const char **azDflt; /* Default value expressions */
u8 *abPK; /* Array of primary key flags */
int nEntry; /* Total number of entries in hash table */
int nChange; /* Size of apChange[] array */
SessionChange **apChange; /* Hash table buckets */
+ sqlite3_stmt *pDfltStmt;
};
/*
@@ -307,6 +321,7 @@ struct SessionTable {
struct SessionChange {
u8 op; /* One of UPDATE, DELETE, INSERT */
u8 bIndirect; /* True if this change is "indirect" */
+ u16 nRecordField; /* Number of fields in aRecord[] */
int nMaxSize; /* Max size of eventual changeset record */
int nRecord; /* Number of bytes in buffer aRecord[] */
u8 *aRecord; /* Buffer containing old.* record */
@@ -332,7 +347,7 @@ static int sessionVarintLen(int iVal){
** Read a varint value from aBuf[] into *piVal. Return the number of
** bytes read.
*/
-static int sessionVarintGet(u8 *aBuf, int *piVal){
+static int sessionVarintGet(const u8 *aBuf, int *piVal){
return getVarint32(aBuf, *piVal);
}
@@ -595,7 +610,7 @@ static int sessionPreupdateHash(
** Return the number of bytes of space occupied by the value (including
** the type byte).
*/
-static int sessionSerialLen(u8 *a){
+static int sessionSerialLen(const u8 *a){
int e = *a;
int n;
if( e==0 || e==0xFF ) return 1;
@@ -1002,13 +1017,14 @@ static int sessionGrowHash(
**
** For example, if the table is declared as:
**
-** CREATE TABLE tbl1(w, x, y, z, PRIMARY KEY(w, z));
+** CREATE TABLE tbl1(w, x DEFAULT 'abc', y, z, PRIMARY KEY(w, z));
**
-** Then the four output variables are populated as follows:
+** Then the five output variables are populated as follows:
**
** *pnCol = 4
** *pzTab = "tbl1"
** *pazCol = {"w", "x", "y", "z"}
+** *pazDflt = {NULL, 'abc', NULL, NULL}
** *pabPK = {1, 0, 0, 1}
**
** All returned buffers are part of the same single allocation, which must
@@ -1022,6 +1038,7 @@ static int sessionTableInfo(
int *pnCol, /* OUT: number of columns */
const char **pzTab, /* OUT: Copy of zThis */
const char ***pazCol, /* OUT: Array of column names for table */
+ const char ***pazDflt, /* OUT: Array of default value expressions */
u8 **pabPK, /* OUT: Array of booleans - true for PK col */
int *pbRowid /* OUT: True if only PK is a rowid */
){
@@ -1034,11 +1051,18 @@ static int sessionTableInfo(
int i;
u8 *pAlloc = 0;
char **azCol = 0;
+ char **azDflt = 0;
u8 *abPK = 0;
int bRowid = 0; /* Set to true to use rowid as PK */
assert( pazCol && pabPK );
+ *pazCol = 0;
+ *pabPK = 0;
+ *pnCol = 0;
+ if( pzTab ) *pzTab = 0;
+ if( pazDflt ) *pazDflt = 0;
+
nThis = sqlite3Strlen30(zThis);
if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){
rc = sqlite3_table_column_metadata(db, zDb, zThis, 0, 0, 0, 0, 0, 0);
@@ -1052,39 +1076,28 @@ static int sessionTableInfo(
}else if( rc==SQLITE_ERROR ){
zPragma = sqlite3_mprintf("");
}else{
- *pazCol = 0;
- *pabPK = 0;
- *pnCol = 0;
- if( pzTab ) *pzTab = 0;
return rc;
}
}else{
zPragma = sqlite3_mprintf("PRAGMA '%q'.table_info('%q')", zDb, zThis);
}
if( !zPragma ){
- *pazCol = 0;
- *pabPK = 0;
- *pnCol = 0;
- if( pzTab ) *pzTab = 0;
return SQLITE_NOMEM;
}
rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
sqlite3_free(zPragma);
if( rc!=SQLITE_OK ){
- *pazCol = 0;
- *pabPK = 0;
- *pnCol = 0;
- if( pzTab ) *pzTab = 0;
return rc;
}
nByte = nThis + 1;
bRowid = (pbRowid!=0);
while( SQLITE_ROW==sqlite3_step(pStmt) ){
- nByte += sqlite3_column_bytes(pStmt, 1);
+ nByte += sqlite3_column_bytes(pStmt, 1); /* name */
+ nByte += sqlite3_column_bytes(pStmt, 4); /* dflt_value */
nDbCol++;
- if( sqlite3_column_int(pStmt, 5) ) bRowid = 0;
+ if( sqlite3_column_int(pStmt, 5) ) bRowid = 0; /* pk */
}
if( nDbCol==0 ) bRowid = 0;
nDbCol += bRowid;
@@ -1092,15 +1105,18 @@ static int sessionTableInfo(
rc = sqlite3_reset(pStmt);
if( rc==SQLITE_OK ){
- nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1);
+ nByte += nDbCol * (sizeof(const char *)*2 + sizeof(u8) + 1 + 1);
pAlloc = sessionMalloc64(pSession, nByte);
if( pAlloc==0 ){
rc = SQLITE_NOMEM;
+ }else{
+ memset(pAlloc, 0, nByte);
}
}
if( rc==SQLITE_OK ){
azCol = (char **)pAlloc;
- pAlloc = (u8 *)&azCol[nDbCol];
+ azDflt = (char**)&azCol[nDbCol];
+ pAlloc = (u8 *)&azDflt[nDbCol];
abPK = (u8 *)pAlloc;
pAlloc = &abPK[nDbCol];
if( pzTab ){
@@ -1120,11 +1136,21 @@ static int sessionTableInfo(
}
while( SQLITE_ROW==sqlite3_step(pStmt) ){
int nName = sqlite3_column_bytes(pStmt, 1);
+ int nDflt = sqlite3_column_bytes(pStmt, 4);
const unsigned char *zName = sqlite3_column_text(pStmt, 1);
+ const unsigned char *zDflt = sqlite3_column_text(pStmt, 4);
+
if( zName==0 ) break;
memcpy(pAlloc, zName, nName+1);
azCol[i] = (char *)pAlloc;
pAlloc += nName+1;
+ if( zDflt ){
+ memcpy(pAlloc, zDflt, nDflt+1);
+ azDflt[i] = (char *)pAlloc;
+ pAlloc += nDflt+1;
+ }else{
+ azDflt[i] = 0;
+ }
abPK[i] = sqlite3_column_int(pStmt, 5);
i++;
}
@@ -1135,14 +1161,11 @@ static int sessionTableInfo(
** free any allocation made. An error code will be returned in this case.
*/
if( rc==SQLITE_OK ){
- *pazCol = (const char **)azCol;
+ *pazCol = (const char**)azCol;
+ if( pazDflt ) *pazDflt = (const char**)azDflt;
*pabPK = abPK;
*pnCol = nDbCol;
}else{
- *pazCol = 0;
- *pabPK = 0;
- *pnCol = 0;
- if( pzTab ) *pzTab = 0;
sessionFree(pSession, azCol);
}
if( pbRowid ) *pbRowid = bRowid;
@@ -1151,10 +1174,9 @@ static int sessionTableInfo(
}
/*
-** This function is only called from within a pre-update handler for a
-** write to table pTab, part of session pSession. If this is the first
-** write to this table, initalize the SessionTable.nCol, azCol[] and
-** abPK[] arrays accordingly.
+** This function is called to initialize the SessionTable.nCol, azCol[]
+** abPK[] and azDflt[] members of SessionTable object pTab. If these
+** fields are already initilialized, this function is a no-op.
**
** If an error occurs, an error code is stored in sqlite3_session.rc and
** non-zero returned. Or, if no error occurs but the table has no primary
@@ -1162,15 +1184,22 @@ static int sessionTableInfo(
** indicate that updates on this table should be ignored. SessionTable.abPK
** is set to NULL in this case.
*/
-static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
+static int sessionInitTable(
+ sqlite3_session *pSession, /* Optional session handle */
+ SessionTable *pTab, /* Table object to initialize */
+ sqlite3 *db, /* Database handle to read schema from */
+ const char *zDb /* Name of db - "main", "temp" etc. */
+){
+ int rc = SQLITE_OK;
+
if( pTab->nCol==0 ){
u8 *abPK;
assert( pTab->azCol==0 || pTab->abPK==0 );
- pSession->rc = sessionTableInfo(pSession, pSession->db, pSession->zDb,
- pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK,
- (pSession->bImplicitPK ? &pTab->bRowid : 0)
+ rc = sessionTableInfo(pSession, db, zDb,
+ pTab->zName, &pTab->nCol, 0, &pTab->azCol, &pTab->azDflt, &abPK,
+ ((pSession==0 || pSession->bImplicitPK) ? &pTab->bRowid : 0)
);
- if( pSession->rc==SQLITE_OK ){
+ if( rc==SQLITE_OK ){
int i;
for(i=0; inCol; i++){
if( abPK[i] ){
@@ -1182,14 +1211,321 @@ static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
pTab->bStat1 = 1;
}
- if( pSession->bEnableSize ){
+ if( pSession && pSession->bEnableSize ){
pSession->nMaxChangesetSize += (
1 + sessionVarintLen(pTab->nCol) + pTab->nCol + strlen(pTab->zName)+1
);
}
}
}
- return (pSession->rc || pTab->abPK==0);
+
+ if( pSession ){
+ pSession->rc = rc;
+ return (rc || pTab->abPK==0);
+ }
+ return rc;
+}
+
+/*
+** Re-initialize table object pTab.
+*/
+static int sessionReinitTable(sqlite3_session *pSession, SessionTable *pTab){
+ int nCol = 0;
+ const char **azCol = 0;
+ const char **azDflt = 0;
+ u8 *abPK = 0;
+ int bRowid = 0;
+
+ assert( pSession->rc==SQLITE_OK );
+
+ pSession->rc = sessionTableInfo(pSession, pSession->db, pSession->zDb,
+ pTab->zName, &nCol, 0, &azCol, &azDflt, &abPK,
+ (pSession->bImplicitPK ? &bRowid : 0)
+ );
+ if( pSession->rc==SQLITE_OK ){
+ if( pTab->nCol>nCol || pTab->bRowid!=bRowid ){
+ pSession->rc = SQLITE_SCHEMA;
+ }else{
+ int ii;
+ int nOldCol = pTab->nCol;
+ for(ii=0; iinCol ){
+ if( pTab->abPK[ii]!=abPK[ii] ){
+ pSession->rc = SQLITE_SCHEMA;
+ }
+ }else if( abPK[ii] ){
+ pSession->rc = SQLITE_SCHEMA;
+ }
+ }
+
+ if( pSession->rc==SQLITE_OK ){
+ const char **a = pTab->azCol;
+ pTab->azCol = azCol;
+ pTab->nCol = nCol;
+ pTab->azDflt = azDflt;
+ pTab->abPK = abPK;
+ azCol = a;
+ }
+ if( pSession->bEnableSize ){
+ pSession->nMaxChangesetSize += (nCol - nOldCol);
+ pSession->nMaxChangesetSize += sessionVarintLen(nCol);
+ pSession->nMaxChangesetSize -= sessionVarintLen(nOldCol);
+ }
+ }
+ }
+
+ sqlite3_free(azCol);
+ return pSession->rc;
+}
+
+/*
+** Session-change object (*pp) contains an old.* record with fewer than
+** nCol fields. This function updates it with the default values for
+** the missing fields.
+*/
+static void sessionUpdateOneChange(
+ sqlite3_session *pSession, /* For memory accounting */
+ int *pRc, /* IN/OUT: Error code */
+ SessionChange **pp, /* IN/OUT: Change object to update */
+ int nCol, /* Number of columns now in table */
+ sqlite3_stmt *pDflt /* SELECT */
+){
+ SessionChange *pOld = *pp;
+
+ while( pOld->nRecordFieldnRecordField;
+ int eType = sqlite3_column_type(pDflt, iField);
+ switch( eType ){
+ case SQLITE_NULL:
+ nIncr = 1;
+ break;
+ case SQLITE_INTEGER:
+ case SQLITE_FLOAT:
+ nIncr = 9;
+ break;
+ default: {
+ int n = sqlite3_column_bytes(pDflt, iField);
+ nIncr = 1 + sessionVarintLen(n) + n;
+ assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
+ break;
+ }
+ }
+
+ nByte = nIncr + (sizeof(SessionChange) + pOld->nRecord);
+ pNew = sessionMalloc64(pSession, nByte);
+ if( pNew==0 ){
+ *pRc = SQLITE_NOMEM;
+ return;
+ }else{
+ memcpy(pNew, pOld, sizeof(SessionChange));
+ pNew->aRecord = (u8*)&pNew[1];
+ memcpy(pNew->aRecord, pOld->aRecord, pOld->nRecord);
+ pNew->aRecord[pNew->nRecord++] = (u8)eType;
+ switch( eType ){
+ case SQLITE_INTEGER: {
+ i64 iVal = sqlite3_column_int64(pDflt, iField);
+ sessionPutI64(&pNew->aRecord[pNew->nRecord], iVal);
+ pNew->nRecord += 8;
+ break;
+ }
+
+ case SQLITE_FLOAT: {
+ double rVal = sqlite3_column_double(pDflt, iField);
+ i64 iVal = 0;
+ memcpy(&iVal, &rVal, sizeof(rVal));
+ sessionPutI64(&pNew->aRecord[pNew->nRecord], iVal);
+ pNew->nRecord += 8;
+ break;
+ }
+
+ case SQLITE_TEXT: {
+ int n = sqlite3_column_bytes(pDflt, iField);
+ const char *z = (const char*)sqlite3_column_text(pDflt, iField);
+ pNew->nRecord += sessionVarintPut(&pNew->aRecord[pNew->nRecord], n);
+ memcpy(&pNew->aRecord[pNew->nRecord], z, n);
+ pNew->nRecord += n;
+ break;
+ }
+
+ case SQLITE_BLOB: {
+ int n = sqlite3_column_bytes(pDflt, iField);
+ const u8 *z = (const u8*)sqlite3_column_blob(pDflt, iField);
+ pNew->nRecord += sessionVarintPut(&pNew->aRecord[pNew->nRecord], n);
+ memcpy(&pNew->aRecord[pNew->nRecord], z, n);
+ pNew->nRecord += n;
+ break;
+ }
+
+ default:
+ assert( eType==SQLITE_NULL );
+ break;
+ }
+
+ sessionFree(pSession, pOld);
+ *pp = pOld = pNew;
+ pNew->nRecordField++;
+ pNew->nMaxSize += nIncr;
+ if( pSession ){
+ pSession->nMaxChangesetSize += nIncr;
+ }
+ }
+ }
+}
+
+/*
+** Ensure that there is room in the buffer to append nByte bytes of data.
+** If not, use sqlite3_realloc() to grow the buffer so that there is.
+**
+** If successful, return zero. Otherwise, if an OOM condition is encountered,
+** set *pRc to SQLITE_NOMEM and return non-zero.
+*/
+static int sessionBufferGrow(SessionBuffer *p, i64 nByte, int *pRc){
+#define SESSION_MAX_BUFFER_SZ (0x7FFFFF00 - 1)
+ i64 nReq = p->nBuf + nByte;
+ if( *pRc==SQLITE_OK && nReq>p->nAlloc ){
+ u8 *aNew;
+ i64 nNew = p->nAlloc ? p->nAlloc : 128;
+
+ do {
+ nNew = nNew*2;
+ }while( nNewSESSION_MAX_BUFFER_SZ ){
+ nNew = SESSION_MAX_BUFFER_SZ;
+ if( nNewaBuf, nNew);
+ if( 0==aNew ){
+ *pRc = SQLITE_NOMEM;
+ }else{
+ p->aBuf = aNew;
+ p->nAlloc = nNew;
+ }
+ }
+ return (*pRc!=SQLITE_OK);
+}
+
+
+/*
+** This function is a no-op if *pRc is other than SQLITE_OK when it is
+** called. Otherwise, append a string to the buffer. All bytes in the string
+** up to (but not including) the nul-terminator are written to the buffer.
+**
+** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
+** returning.
+*/
+static void sessionAppendStr(
+ SessionBuffer *p,
+ const char *zStr,
+ int *pRc
+){
+ int nStr = sqlite3Strlen30(zStr);
+ if( 0==sessionBufferGrow(p, nStr+1, pRc) ){
+ memcpy(&p->aBuf[p->nBuf], zStr, nStr);
+ p->nBuf += nStr;
+ p->aBuf[p->nBuf] = 0x00;
+ }
+}
+
+/*
+** Format a string using printf() style formatting and then append it to the
+** buffer using sessionAppendString().
+*/
+static void sessionAppendPrintf(
+ SessionBuffer *p, /* Buffer to append to */
+ int *pRc,
+ const char *zFmt,
+ ...
+){
+ if( *pRc==SQLITE_OK ){
+ char *zApp = 0;
+ va_list ap;
+ va_start(ap, zFmt);
+ zApp = sqlite3_vmprintf(zFmt, ap);
+ if( zApp==0 ){
+ *pRc = SQLITE_NOMEM;
+ }else{
+ sessionAppendStr(p, zApp, pRc);
+ }
+ va_end(ap);
+ sqlite3_free(zApp);
+ }
+}
+
+/*
+** Prepare a statement against database handle db that SELECTs a single
+** row containing the default values for each column in table pTab. For
+** example, if pTab is declared as:
+**
+** CREATE TABLE pTab(a PRIMARY KEY, b DEFAULT 123, c DEFAULT 'abcd');
+**
+** Then this function prepares and returns the SQL statement:
+**
+** SELECT NULL, 123, 'abcd';
+*/
+static int sessionPrepareDfltStmt(
+ sqlite3 *db, /* Database handle */
+ SessionTable *pTab, /* Table to prepare statement for */
+ sqlite3_stmt **ppStmt /* OUT: Statement handle */
+){
+ SessionBuffer sql = {0,0,0};
+ int rc = SQLITE_OK;
+ const char *zSep = " ";
+ int ii = 0;
+
+ *ppStmt = 0;
+ sessionAppendPrintf(&sql, &rc, "SELECT");
+ for(ii=0; iinCol; ii++){
+ const char *zDflt = pTab->azDflt[ii] ? pTab->azDflt[ii] : "NULL";
+ sessionAppendPrintf(&sql, &rc, "%s%s", zSep, zDflt);
+ zSep = ", ";
+ }
+ if( rc==SQLITE_OK ){
+ rc = sqlite3_prepare_v2(db, (const char*)sql.aBuf, -1, ppStmt, 0);
+ }
+ sqlite3_free(sql.aBuf);
+
+ return rc;
+}
+
+/*
+** Table pTab has one or more existing change-records with old.* records
+** with fewer than pTab->nCol columns. This function updates all such
+** change-records with the default values for the missing columns.
+*/
+static int sessionUpdateChanges(sqlite3_session *pSession, SessionTable *pTab){
+ sqlite3_stmt *pStmt = 0;
+ int rc = pSession->rc;
+
+ rc = sessionPrepareDfltStmt(pSession->db, pTab, &pStmt);
+ if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
+ int ii = 0;
+ SessionChange **pp = 0;
+ for(ii=0; iinChange; ii++){
+ for(pp=&pTab->apChange[ii]; *pp; pp=&((*pp)->pNext)){
+ if( (*pp)->nRecordField!=pTab->nCol ){
+ sessionUpdateOneChange(pSession, &rc, pp, pTab->nCol, pStmt);
+ }
+ }
+ }
+ }
+
+ pSession->rc = rc;
+ rc = sqlite3_finalize(pStmt);
+ if( pSession->rc==SQLITE_OK ) pSession->rc = rc;
+ return pSession->rc;
}
/*
@@ -1352,16 +1688,22 @@ static void sessionPreupdateOneChange(
int iHash;
int bNull = 0;
int rc = SQLITE_OK;
+ int nExpect = 0;
SessionStat1Ctx stat1 = {{0,0,0,0,0},0};
if( pSession->rc ) return;
/* Load table details if required */
- if( sessionInitTable(pSession, pTab) ) return;
+ if( sessionInitTable(pSession, pTab, pSession->db, pSession->zDb) ) return;
/* Check the number of columns in this xPreUpdate call matches the
** number of columns in the table. */
- if( (pTab->nCol-pTab->bRowid)!=pSession->hook.xCount(pSession->hook.pCtx) ){
+ nExpect = pSession->hook.xCount(pSession->hook.pCtx);
+ if( (pTab->nCol-pTab->bRowid)nCol-pTab->bRowid)!=nExpect ){
pSession->rc = SQLITE_SCHEMA;
return;
}
@@ -1438,7 +1780,7 @@ static void sessionPreupdateOneChange(
}
/* Allocate the change object */
- pC = (SessionChange *)sessionMalloc64(pSession, nByte);
+ pC = (SessionChange*)sessionMalloc64(pSession, nByte);
if( !pC ){
rc = SQLITE_NOMEM;
goto error_out;
@@ -1471,6 +1813,7 @@ static void sessionPreupdateOneChange(
if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
pC->bIndirect = 1;
}
+ pC->nRecordField = pTab->nCol;
pC->nRecord = nByte;
pC->op = op;
pC->pNext = pTab->apChange[iHash];
@@ -1850,7 +2193,7 @@ int sqlite3session_diff(
/* Locate and if necessary initialize the target table object */
rc = sessionFindTable(pSession, zTbl, &pTo);
if( pTo==0 ) goto diff_out;
- if( sessionInitTable(pSession, pTo) ){
+ if( sessionInitTable(pSession, pTo, pSession->db, pSession->zDb) ){
rc = pSession->rc;
goto diff_out;
}
@@ -1863,7 +2206,7 @@ int sqlite3session_diff(
int bRowid = 0;
u8 *abPK;
const char **azCol = 0;
- rc = sessionTableInfo(0, db, zFrom, zTbl, &nCol, 0, &azCol, &abPK,
+ rc = sessionTableInfo(0, db, zFrom, zTbl, &nCol, 0, &azCol, 0, &abPK,
pSession->bImplicitPK ? &bRowid : 0
);
if( rc==SQLITE_OK ){
@@ -1978,6 +2321,7 @@ static void sessionDeleteTable(sqlite3_session *pSession, SessionTable *pList){
sessionFree(pSession, p);
}
}
+ sqlite3_finalize(pTab->pDfltStmt);
sessionFree(pSession, (char*)pTab->azCol); /* cast works around VC++ bug */
sessionFree(pSession, pTab->apChange);
sessionFree(pSession, pTab);
@@ -2012,7 +2356,7 @@ void sqlite3session_delete(sqlite3_session *pSession){
/* Assert that all allocations have been freed and then free the
** session object itself. */
- assert( pSession->nMalloc==0 );
+ // assert( pSession->nMalloc==0 );
sqlite3_free(pSession);
}
@@ -2083,48 +2427,6 @@ int sqlite3session_attach(
return rc;
}
-/*
-** Ensure that there is room in the buffer to append nByte bytes of data.
-** If not, use sqlite3_realloc() to grow the buffer so that there is.
-**
-** If successful, return zero. Otherwise, if an OOM condition is encountered,
-** set *pRc to SQLITE_NOMEM and return non-zero.
-*/
-static int sessionBufferGrow(SessionBuffer *p, i64 nByte, int *pRc){
-#define SESSION_MAX_BUFFER_SZ (0x7FFFFF00 - 1)
- i64 nReq = p->nBuf + nByte;
- if( *pRc==SQLITE_OK && nReq>p->nAlloc ){
- u8 *aNew;
- i64 nNew = p->nAlloc ? p->nAlloc : 128;
-
- do {
- nNew = nNew*2;
- }while( nNewSESSION_MAX_BUFFER_SZ ){
- nNew = SESSION_MAX_BUFFER_SZ;
- if( nNewaBuf, nNew);
- if( 0==aNew ){
- *pRc = SQLITE_NOMEM;
- }else{
- p->aBuf = aNew;
- p->nAlloc = nNew;
- }
- }
- return (*pRc!=SQLITE_OK);
-}
-
/*
** Append the value passed as the second argument to the buffer passed
** as the first.
@@ -2193,27 +2495,6 @@ static void sessionAppendBlob(
}
}
-/*
-** This function is a no-op if *pRc is other than SQLITE_OK when it is
-** called. Otherwise, append a string to the buffer. All bytes in the string
-** up to (but not including) the nul-terminator are written to the buffer.
-**
-** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
-** returning.
-*/
-static void sessionAppendStr(
- SessionBuffer *p,
- const char *zStr,
- int *pRc
-){
- int nStr = sqlite3Strlen30(zStr);
- if( 0==sessionBufferGrow(p, nStr+1, pRc) ){
- memcpy(&p->aBuf[p->nBuf], zStr, nStr);
- p->nBuf += nStr;
- p->aBuf[p->nBuf] = 0x00;
- }
-}
-
/*
** This function is a no-op if *pRc is other than SQLITE_OK when it is
** called. Otherwise, append the string representation of integer iVal
@@ -2232,27 +2513,6 @@ static void sessionAppendInteger(
sessionAppendStr(p, aBuf, pRc);
}
-static void sessionAppendPrintf(
- SessionBuffer *p, /* Buffer to append to */
- int *pRc,
- const char *zFmt,
- ...
-){
- if( *pRc==SQLITE_OK ){
- char *zApp = 0;
- va_list ap;
- va_start(ap, zFmt);
- zApp = sqlite3_vmprintf(zFmt, ap);
- if( zApp==0 ){
- *pRc = SQLITE_NOMEM;
- }else{
- sessionAppendStr(p, zApp, pRc);
- }
- va_end(ap);
- sqlite3_free(zApp);
- }
-}
-
/*
** This function is a no-op if *pRc is other than SQLITE_OK when it is
** called. Otherwise, append the string zStr enclosed in quotes (") and
@@ -2743,26 +3003,16 @@ static int sessionGenerateChangeset(
for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
if( pTab->nEntry ){
const char *zName = pTab->zName;
- int nCol = 0; /* Number of columns in table */
- u8 *abPK = 0; /* Primary key array */
- const char **azCol = 0; /* Table columns */
int i; /* Used to iterate through hash buckets */
sqlite3_stmt *pSel = 0; /* SELECT statement to query table pTab */
int nRewind = buf.nBuf; /* Initial size of write buffer */
int nNoop; /* Size of buffer after writing tbl header */
- int bRowid = 0;
+ int nOldCol = pTab->nCol;
/* Check the table schema is still Ok. */
- rc = sessionTableInfo(
- 0, db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK,
- (pSession->bImplicitPK ? &bRowid : 0)
- );
- if( rc==SQLITE_OK && (
- pTab->nCol!=nCol
- || pTab->bRowid!=bRowid
- || memcmp(abPK, pTab->abPK, nCol)
- )){
- rc = SQLITE_SCHEMA;
+ rc = sessionReinitTable(pSession, pTab);
+ if( rc==SQLITE_OK && pTab->nCol!=nOldCol ){
+ rc = sessionUpdateChanges(pSession, pTab);
}
/* Write a table header */
@@ -2770,8 +3020,8 @@ static int sessionGenerateChangeset(
/* Build and compile a statement to execute: */
if( rc==SQLITE_OK ){
- rc = sessionSelectStmt(
- db, 0, pSession->zDb, zName, bRowid, nCol, azCol, abPK, &pSel
+ rc = sessionSelectStmt(db, 0, pSession->zDb,
+ zName, pTab->bRowid, pTab->nCol, pTab->azCol, pTab->abPK, &pSel
);
}
@@ -2780,22 +3030,22 @@ static int sessionGenerateChangeset(
SessionChange *p; /* Used to iterate through changes */
for(p=pTab->apChange[i]; rc==SQLITE_OK && p; p=p->pNext){
- rc = sessionSelectBind(pSel, nCol, abPK, p);
+ rc = sessionSelectBind(pSel, pTab->nCol, pTab->abPK, p);
if( rc!=SQLITE_OK ) continue;
if( sqlite3_step(pSel)==SQLITE_ROW ){
if( p->op==SQLITE_INSERT ){
int iCol;
sessionAppendByte(&buf, SQLITE_INSERT, &rc);
sessionAppendByte(&buf, p->bIndirect, &rc);
- for(iCol=0; iColnCol; iCol++){
sessionAppendCol(&buf, pSel, iCol, &rc);
}
}else{
- assert( abPK!=0 ); /* Because sessionSelectStmt() returned ok */
- rc = sessionAppendUpdate(&buf, ePatchset, pSel, p, abPK);
+ assert( pTab->abPK!=0 );
+ rc = sessionAppendUpdate(&buf, ePatchset, pSel, p, pTab->abPK);
}
}else if( p->op!=SQLITE_INSERT ){
- rc = sessionAppendDelete(&buf, ePatchset, p, nCol, abPK);
+ rc = sessionAppendDelete(&buf, ePatchset, p, pTab->nCol,pTab->abPK);
}
if( rc==SQLITE_OK ){
rc = sqlite3_reset(pSel);
@@ -2820,7 +3070,6 @@ static int sessionGenerateChangeset(
if( buf.nBuf==nNoop ){
buf.nBuf = nRewind;
}
- sqlite3_free((char*)azCol); /* cast works around VC++ bug */
}
}
@@ -3263,15 +3512,19 @@ static int sessionReadRecord(
}
}
if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
- sqlite3_int64 v = sessionGetI64(aVal);
- if( eType==SQLITE_INTEGER ){
- sqlite3VdbeMemSetInt64(apOut[i], v);
+ if( (pIn->nData-pIn->iNext)<8 ){
+ rc = SQLITE_CORRUPT_BKPT;
}else{
- double d;
- memcpy(&d, &v, 8);
- sqlite3VdbeMemSetDouble(apOut[i], d);
+ sqlite3_int64 v = sessionGetI64(aVal);
+ if( eType==SQLITE_INTEGER ){
+ sqlite3VdbeMemSetInt64(apOut[i], v);
+ }else{
+ double d;
+ memcpy(&d, &v, 8);
+ sqlite3VdbeMemSetDouble(apOut[i], d);
+ }
+ pIn->iNext += 8;
}
- pIn->iNext += 8;
}
}
}
@@ -4968,7 +5221,7 @@ static int sessionChangesetApply(
sqlite3changeset_pk(pIter, &abPK, 0);
rc = sessionTableInfo(0, db, "main", zNew,
- &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK, &sApply.bRowid
+ &sApply.nCol, &zTab, &sApply.azCol, 0, &sApply.abPK, &sApply.bRowid
);
if( rc!=SQLITE_OK ) break;
for(i=0; iflags & SQLITE_FkNoAction;
+
+ if( flags & SQLITE_CHANGESETAPPLY_FKNOACTION ){
+ db->flags |= ((u64)SQLITE_FkNoAction);
+ db->aDb[0].pSchema->schema_cookie -= 32;
+ }
+
if( rc==SQLITE_OK ){
rc = sessionChangesetApply(
db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
);
}
+
+ if( (flags & SQLITE_CHANGESETAPPLY_FKNOACTION) && savedFlag==0 ){
+ assert( db->flags & SQLITE_FkNoAction );
+ db->flags &= ~((u64)SQLITE_FkNoAction);
+ db->aDb[0].pSchema->schema_cookie -= 32;
+ }
return rc;
}
@@ -5192,6 +5458,9 @@ struct sqlite3_changegroup {
int rc; /* Error code */
int bPatch; /* True to accumulate patchsets */
SessionTable *pList; /* List of tables in current patch */
+
+ sqlite3 *db; /* Configured by changegroup_schema() */
+ char *zDb; /* Configured by changegroup_schema() */
};
/*
@@ -5377,6 +5646,114 @@ static int sessionChangeMerge(
return rc;
}
+/*
+** Check if a changeset entry with nCol columns and the PK array passed
+** as the final argument to this function is compatible with SessionTable
+** pTab. If so, return 1. Otherwise, if they are incompatible in some way,
+** return 0.
+*/
+static int sessionChangesetCheckCompat(
+ SessionTable *pTab,
+ int nCol,
+ u8 *abPK
+){
+ if( pTab->azCol && nColnCol ){
+ int ii;
+ for(ii=0; iinCol; ii++){
+ u8 bPK = (ii < nCol) ? abPK[ii] : 0;
+ if( pTab->abPK[ii]!=bPK ) return 0;
+ }
+ return 1;
+ }
+ return (pTab->nCol==nCol && 0==memcmp(abPK, pTab->abPK, nCol));
+}
+
+static int sessionChangesetExtendRecord(
+ sqlite3_changegroup *pGrp,
+ SessionTable *pTab,
+ int nCol,
+ int op,
+ const u8 *aRec,
+ int nRec,
+ SessionBuffer *pOut
+){
+ int rc = SQLITE_OK;
+ int ii = 0;
+
+ assert( pTab->azCol );
+ assert( nColnCol );
+
+ pOut->nBuf = 0;
+ if( op==SQLITE_INSERT || (op==SQLITE_DELETE && pGrp->bPatch==0) ){
+ /* Append the missing default column values to the record. */
+ sessionAppendBlob(pOut, aRec, nRec, &rc);
+ if( rc==SQLITE_OK && pTab->pDfltStmt==0 ){
+ rc = sessionPrepareDfltStmt(pGrp->db, pTab, &pTab->pDfltStmt);
+ }
+ for(ii=nCol; rc==SQLITE_OK && iinCol; ii++){
+ int eType = sqlite3_column_type(pTab->pDfltStmt, ii);
+ sessionAppendByte(pOut, eType, &rc);
+ switch( eType ){
+ case SQLITE_FLOAT:
+ case SQLITE_INTEGER: {
+ i64 iVal;
+ if( eType==SQLITE_INTEGER ){
+ iVal = sqlite3_column_int64(pTab->pDfltStmt, ii);
+ }else{
+ double rVal = sqlite3_column_int64(pTab->pDfltStmt, ii);
+ memcpy(&iVal, &rVal, sizeof(i64));
+ }
+ if( SQLITE_OK==sessionBufferGrow(pOut, 8, &rc) ){
+ sessionPutI64(&pOut->aBuf[pOut->nBuf], iVal);
+ }
+ break;
+ }
+
+ case SQLITE_BLOB:
+ case SQLITE_TEXT: {
+ int n = sqlite3_column_bytes(pTab->pDfltStmt, ii);
+ sessionAppendVarint(pOut, n, &rc);
+ if( eType==SQLITE_TEXT ){
+ const u8 *z = (const u8*)sqlite3_column_text(pTab->pDfltStmt, ii);
+ sessionAppendBlob(pOut, z, n, &rc);
+ }else{
+ const u8 *z = (const u8*)sqlite3_column_blob(pTab->pDfltStmt, ii);
+ sessionAppendBlob(pOut, z, n, &rc);
+ }
+ break;
+ }
+
+ default:
+ assert( eType==SQLITE_NULL );
+ break;
+ }
+ }
+ }else if( op==SQLITE_UPDATE ){
+ /* Append missing "undefined" entries to the old.* record. And, if this
+ ** is an UPDATE, to the new.* record as well. */
+ int iOff = 0;
+ if( pGrp->bPatch==0 ){
+ for(ii=0; iinCol-nCol); ii++){
+ sessionAppendByte(pOut, 0x00, &rc);
+ }
+ }
+
+ sessionAppendBlob(pOut, &aRec[iOff], nRec-iOff, &rc);
+ for(ii=0; ii<(pTab->nCol-nCol); ii++){
+ sessionAppendByte(pOut, 0x00, &rc);
+ }
+ }else{
+ assert( op==SQLITE_DELETE && pGrp->bPatch );
+ sessionAppendBlob(pOut, aRec, nRec, &rc);
+ }
+
+ return rc;
+}
+
/*
** Add all changes in the changeset traversed by the iterator passed as
** the first argument to the changegroup hash tables.
@@ -5390,6 +5767,7 @@ static int sessionChangesetToHash(
int nRec;
int rc = SQLITE_OK;
SessionTable *pTab = 0;
+ SessionBuffer rec = {0, 0, 0};
while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
const char *zNew;
@@ -5401,6 +5779,9 @@ static int sessionChangesetToHash(
SessionChange *pExist = 0;
SessionChange **pp;
+ /* Ensure that only changesets, or only patchsets, but not a mixture
+ ** of both, are being combined. It is an error to try to combine a
+ ** changeset and a patchset. */
if( pGrp->pList==0 ){
pGrp->bPatch = pIter->bPatchset;
}else if( pIter->bPatchset!=pGrp->bPatch ){
@@ -5433,18 +5814,38 @@ static int sessionChangesetToHash(
pTab->zName = (char*)&pTab->abPK[nCol];
memcpy(pTab->zName, zNew, nNew+1);
+ if( pGrp->db ){
+ pTab->nCol = 0;
+ rc = sessionInitTable(0, pTab, pGrp->db, pGrp->zDb);
+ if( rc ){
+ assert( pTab->azCol==0 );
+ sqlite3_free(pTab);
+ break;
+ }
+ }
+
/* The new object must be linked on to the end of the list, not
** simply added to the start of it. This is to ensure that the
** tables within the output of sqlite3changegroup_output() are in
** the right order. */
for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
*ppTab = pTab;
- }else if( pTab->nCol!=nCol || memcmp(pTab->abPK, abPK, nCol) ){
+ }
+
+ if( !sessionChangesetCheckCompat(pTab, nCol, abPK) ){
rc = SQLITE_SCHEMA;
break;
}
}
+ if( nColnCol ){
+ assert( pGrp->db );
+ rc = sessionChangesetExtendRecord(pGrp, pTab, nCol, op, aRec, nRec, &rec);
+ if( rc ) break;
+ aRec = rec.aBuf;
+ nRec = rec.nBuf;
+ }
+
if( sessionGrowHash(0, pIter->bPatchset, pTab) ){
rc = SQLITE_NOMEM;
break;
@@ -5482,6 +5883,7 @@ static int sessionChangesetToHash(
}
}
+ sqlite3_free(rec.aBuf);
if( rc==SQLITE_OK ) rc = pIter->rc;
return rc;
}
@@ -5569,6 +5971,31 @@ int sqlite3changegroup_new(sqlite3_changegroup **pp){
return rc;
}
+/*
+** Provide a database schema to the changegroup object.
+*/
+int sqlite3changegroup_schema(
+ sqlite3_changegroup *pGrp,
+ sqlite3 *db,
+ const char *zDb
+){
+ int rc = SQLITE_OK;
+
+ if( pGrp->pList || pGrp->db ){
+ /* Cannot add a schema after one or more calls to sqlite3changegroup_add(),
+ ** or after sqlite3changegroup_schema() has already been called. */
+ rc = SQLITE_MISUSE;
+ }else{
+ pGrp->zDb = sqlite3_mprintf("%s", zDb);
+ if( pGrp->zDb==0 ){
+ rc = SQLITE_NOMEM;
+ }else{
+ pGrp->db = db;
+ }
+ }
+ return rc;
+}
+
/*
** Add the changeset currently stored in buffer pData, size nData bytes,
** to changeset-group p.
@@ -5632,6 +6059,7 @@ int sqlite3changegroup_output_strm(
*/
void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
if( pGrp ){
+ sqlite3_free(pGrp->zDb);
sessionDeleteTable(0, pGrp->pList);
sqlite3_free(pGrp);
}
diff --git a/ext/session/sqlite3session.h b/ext/session/sqlite3session.h
index c5bc545fe7..d0d83a3c8c 100644
--- a/ext/session/sqlite3session.h
+++ b/ext/session/sqlite3session.h
@@ -914,6 +914,18 @@ int sqlite3changeset_concat(
);
+/*
+** CAPI3REF: Upgrade the Schema of a Changeset/Patchset
+*/
+int sqlite3changeset_upgrade(
+ sqlite3 *db,
+ const char *zDb,
+ int nIn, const void *pIn, /* Input changeset */
+ int *pnOut, void **ppOut /* OUT: Inverse of input */
+);
+
+
+
/*
** CAPI3REF: Changegroup Handle
**
@@ -960,6 +972,38 @@ typedef struct sqlite3_changegroup sqlite3_changegroup;
*/
int sqlite3changegroup_new(sqlite3_changegroup **pp);
+/*
+** CAPI3REF: Add a Schema to a Changegroup
+** METHOD: sqlite3_changegroup_schema
+**
+** This method may be used to optionally enforce the rule that the changesets
+** added to the changegroup handle must match the schema of database zDb
+** ("main", "temp", or the name of an attached database). If
+** sqlite3changegroup_add() is called to add a changeset that is not compatible
+** with the configured schema, SQLITE_SCHEMA is returned and the changegroup
+** object is left in an undefined state.
+**
+** A changeset schema is considered compatible with the database schema in
+** the same way as for sqlite3changeset_apply(). Specifically, for each
+** table in the changeset, there exists a database table with:
+**
+**
+** - The name identified by the changeset, and
+**
- at least as many columns as recorded in the changeset, and
+**
- the primary key columns in the same position as recorded in
+** the changeset.
+**
+**
+** The output of the changegroup object always has the same schema as the
+** database nominated using this function. In cases where changesets passed
+** to sqlite3changegroup_add() have fewer columns than the corresponding table
+** in the database schema, these are filled in using the default column
+** values from the database schema. This makes it possible to combined
+** changesets that have different numbers of columns for a single table
+** within a changegroup, provided that they are otherwise compatible.
+*/
+int sqlite3changegroup_schema(sqlite3_changegroup*, sqlite3*, const char *zDb);
+
/*
** CAPI3REF: Add A Changeset To A Changegroup
** METHOD: sqlite3_changegroup
@@ -1028,13 +1072,18 @@ int sqlite3changegroup_new(sqlite3_changegroup **pp);
** If the new changeset contains changes to a table that is already present
** in the changegroup, then the number of columns and the position of the
** primary key columns for the table must be consistent. If this is not the
-** case, this function fails with SQLITE_SCHEMA. If the input changeset
-** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
-** returned. Or, if an out-of-memory condition occurs during processing, this
-** function returns SQLITE_NOMEM. In all cases, if an error occurs the state
-** of the final contents of the changegroup is undefined.
+** case, this function fails with SQLITE_SCHEMA. Except, if the changegroup
+** object has been configured with a database schema using the
+** sqlite3changegroup_schema() API, then it is possible to combine changesets
+** with different numbers of columns for a single table, provided that
+** they are otherwise compatible.
**
-** If no error occurs, SQLITE_OK is returned.
+** If the input changeset appears to be corrupt and the corruption is
+** detected, SQLITE_CORRUPT is returned. Or, if an out-of-memory condition
+** occurs during processing, this function returns SQLITE_NOMEM.
+**
+** In all cases, if an error occurs the state of the final contents of the
+** changegroup is undefined. If no error occurs, SQLITE_OK is returned.
*/
int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
@@ -1299,10 +1348,17 @@ int sqlite3changeset_apply_v2(
** an insert change if all fields of the conflicting row match
** the row being inserted.
**
+**
+** SQLITE_CHANGESETAPPLY_FKNOACTION
+** If this flag it set, then all foreign key constraints in the target
+** database behave as if they were declared with "ON UPDATE NO ACTION ON
+** DELETE NO ACTION", even if they are actually CASCADE, RESTRICT, SET NULL
+** or SET DEFAULT.
*/
#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT 0x0001
#define SQLITE_CHANGESETAPPLY_INVERT 0x0002
#define SQLITE_CHANGESETAPPLY_IGNORENOOP 0x0004
+#define SQLITE_CHANGESETAPPLY_FKNOACTION 0x0008
/*
** CAPI3REF: Constants Passed To The Conflict Handler
diff --git a/ext/session/test_session.c b/ext/session/test_session.c
index d21decf1c3..3e45aa7e86 100644
--- a/ext/session/test_session.c
+++ b/ext/session/test_session.c
@@ -817,9 +817,12 @@ static int SQLITE_TCLAPI testSqlite3changesetApply(
while( objc>1 ){
const char *z1 = Tcl_GetString(objv[1]);
int n = strlen(z1);
- if( n>1 && n<=12 && 0==sqlite3_strnicmp("-nosavepoint", z1, n) ){
+ if( n>3 && n<=12 && 0==sqlite3_strnicmp("-nosavepoint", z1, n) ){
flags |= SQLITE_CHANGESETAPPLY_NOSAVEPOINT;
}
+ else if( n>3 && n<=9 && 0==sqlite3_strnicmp("-noaction", z1, n) ){
+ flags |= SQLITE_CHANGESETAPPLY_FKNOACTION;
+ }
else if( n>2 && n<=7 && 0==sqlite3_strnicmp("-invert", z1, n) ){
flags |= SQLITE_CHANGESETAPPLY_INVERT;
}
@@ -1578,12 +1581,144 @@ static int SQLITE_TCLAPI test_sqlite3session_config(
return TCL_OK;
}
+typedef struct TestChangegroup TestChangegroup;
+struct TestChangegroup {
+ sqlite3_changegroup *pGrp;
+};
+
+/*
+** Destructor for Tcl changegroup command object.
+*/
+static void test_changegroup_del(void *clientData){
+ TestChangegroup *pGrp = (TestChangegroup*)clientData;
+ sqlite3changegroup_delete(pGrp->pGrp);
+ ckfree(pGrp);
+}
+
+/*
+** Tclcmd: $changegroup schema DB DBNAME
+** Tclcmd: $changegroup add CHANGESET
+** Tclcmd: $changegroup output
+** Tclcmd: $changegroup delete
+*/
+static int SQLITE_TCLAPI test_changegroup_cmd(
+ void * clientData,
+ Tcl_Interp *interp,
+ int objc,
+ Tcl_Obj *CONST objv[]
+){
+ TestChangegroup *p = (TestChangegroup*)clientData;
+ static struct ChangegroupCmd {
+ const char *zSub;
+ int nArg;
+ const char *zMsg;
+ int iSub;
+ } aSub[] = {
+ { "schema", 2, "DB DBNAME", }, /* 0 */
+ { "add", 1, "CHANGESET", }, /* 1 */
+ { "output", 0, "", }, /* 2 */
+ { "delete", 0, "", }, /* 3 */
+ { 0 }
+ };
+ int rc = TCL_OK;
+ int iSub = 0;
+
+ if( objc<2 ){
+ Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
+ return TCL_ERROR;
+ }
+ rc = Tcl_GetIndexFromObjStruct(interp,
+ objv[1], aSub, sizeof(aSub[0]), "sub-command", 0, &iSub
+ );
+ if( rc!=TCL_OK ) return rc;
+ if( objc!=2+aSub[iSub].nArg ){
+ Tcl_WrongNumArgs(interp, 2, objv, aSub[iSub].zMsg);
+ return TCL_ERROR;
+ }
+
+ switch( iSub ){
+ case 0: { /* schema */
+ sqlite3 *db = 0;
+ const char *zDb = Tcl_GetString(objv[3]);
+ if( dbHandleFromObj(interp, objv[2], &db) ){
+ return TCL_ERROR;
+ }
+ rc = sqlite3changegroup_schema(p->pGrp, db, zDb);
+ if( rc!=SQLITE_OK ) rc = test_session_error(interp, rc, 0);
+ break;
+ };
+
+ case 1: { /* add */
+ int nByte = 0;
+ const u8 *aByte = Tcl_GetByteArrayFromObj(objv[2], &nByte);
+ rc = sqlite3changegroup_add(p->pGrp, nByte, (void*)aByte);
+ if( rc!=SQLITE_OK ) rc = test_session_error(interp, rc, 0);
+ break;
+ };
+
+ case 2: { /* output */
+ int nByte = 0;
+ u8 *aByte = 0;
+ rc = sqlite3changegroup_output(p->pGrp, &nByte, (void**)&aByte);
+ if( rc!=SQLITE_OK ){
+ rc = test_session_error(interp, rc, 0);
+ }else{
+ Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(aByte, nByte));
+ }
+ sqlite3_free(aByte);
+ break;
+ };
+
+ default: { /* delete */
+ assert( iSub==3 );
+ Tcl_DeleteCommand(interp, Tcl_GetString(objv[0]));
+ break;
+ }
+ }
+
+ return rc;
+}
+
+/*
+** Tclcmd: sqlite3changegroup CMD
+*/
+static int SQLITE_TCLAPI test_sqlite3changegroup(
+ void * clientData,
+ Tcl_Interp *interp,
+ int objc,
+ Tcl_Obj *CONST objv[]
+){
+ int rc; /* sqlite3changegroup_new() return code */
+ TestChangegroup *p; /* New wrapper object */
+
+ if( objc!=2 ){
+ Tcl_WrongNumArgs(interp, 1, objv, "CMD");
+ return TCL_ERROR;
+ }
+
+ p = (TestChangegroup*)ckalloc(sizeof(TestChangegroup));
+ memset(p, 0, sizeof(TestChangegroup));
+ rc = sqlite3changegroup_new(&p->pGrp);
+ if( rc!=SQLITE_OK ){
+ ckfree((char*)p);
+ return test_session_error(interp, rc, 0);
+ }
+
+ Tcl_CreateObjCommand(
+ interp, Tcl_GetString(objv[1]), test_changegroup_cmd, (ClientData)p,
+ test_changegroup_del
+ );
+ Tcl_SetObjResult(interp, objv[1]);
+ return TCL_OK;
+}
+
int TestSession_Init(Tcl_Interp *interp){
struct Cmd {
const char *zCmd;
Tcl_ObjCmdProc *xProc;
} aCmd[] = {
{ "sqlite3session", test_sqlite3session },
+ { "sqlite3changegroup", test_sqlite3changegroup },
{ "sqlite3session_foreach", test_sqlite3session_foreach },
{ "sqlite3changeset_invert", test_sqlite3changeset_invert },
{ "sqlite3changeset_concat", test_sqlite3changeset_concat },
diff --git a/ext/wasm/GNUmakefile b/ext/wasm/GNUmakefile
index 080c427045..c0cab212d8 100644
--- a/ext/wasm/GNUmakefile
+++ b/ext/wasm/GNUmakefile
@@ -456,13 +456,6 @@ emcc.exportedRuntimeMethods := \
emcc.jsflags += $(emcc.exportedRuntimeMethods)
emcc.jsflags += -sUSE_CLOSURE_COMPILER=0
emcc.jsflags += -sIMPORTED_MEMORY
-#emcc.jsflags += -sASYNCIFY=2
-# ^^^ ASYNCIFY=2 is for experimental JSPI support
-# (https://v8.dev/blog/jspi), but enabling it causes the lib-level
-# init code to throw inexplicable complaints about C-level function
-# signatures not matching what we expect them to be. JSPI requires, as of
-# this writing, requires an experimental Chrome flag:
-# chrome://flags/#enable-experimental-webassembly-stack-switching
emcc.jsflags += -sSTRICT_JS=0
# STRICT_JS disabled due to:
# https://github.com/emscripten-core/emscripten/issues/18610
@@ -845,11 +838,11 @@ sqlite3-worker1-bundler-friendly.js := $(dir.dout)/sqlite3-worker1-bundler-frien
sqlite3-worker1-promiser-bundler-friendly.js := $(dir.dout)/sqlite3-worker1-promiser-bundler-friendly.js
$(eval $(call C-PP.FILTER,$(sqlite3-worker1.js.in),$(sqlite3-worker1.js)))
$(eval $(call C-PP.FILTER,$(sqlite3-worker1.js.in),$(sqlite3-worker1-bundler-friendly.js),\
- $(c-pp.D.bundler-friendly)))
+ $(c-pp.D.sqlite3-bundler-friendly)))
$(eval $(call C-PP.FILTER,$(sqlite3-worker1-promiser.js.in),$(sqlite3-worker1-promiser.js)))
$(eval $(call C-PP.FILTER,$(sqlite3-worker1-promiser.js.in),\
$(sqlite3-worker1-promiser-bundler-friendly.js),\
- $(c-pp.D.bundler-friendly)))
+ $(c-pp.D.sqlite3-bundler-friendly)))
$(sqlite3-bundler-friendly.mjs): $(sqlite3-worker1-bundler-friendly.js) \
$(sqlite3-worker1-promiser-bundler-friendly.js)
$(sqlite3.js) $(sqlite3.mjs): $(sqlite3-worker1.js) $(sqlite3-worker1-promiser.js)
diff --git a/ext/wasm/SQLTester/GNUmakefile b/ext/wasm/SQLTester/GNUmakefile
new file mode 100644
index 0000000000..8fa1247138
--- /dev/null
+++ b/ext/wasm/SQLTester/GNUmakefile
@@ -0,0 +1,55 @@
+#!/this/is/make
+#
+# This makefile compiles SQLTester test files into something
+# we can readily import into JavaScript.
+all:
+
+SHELL := $(shell which bash 2>/dev/null)
+MAKEFILE := $(lastword $(MAKEFILE_LIST))
+CLEAN_FILES :=
+DISTCLEAN_FILES := ./--dummy-- *~
+
+test-list.mjs := test-list.mjs
+test-list.mjs.gz := $(test-list.mjs).gz
+CLEAN_FILES += $(test-list.mjs)
+
+tests.dir := $(firstword $(wildcard tests ../../jni/src/tests))
+$(info test script dir=$(tests.dir))
+
+tests.all := $(wildcard $(tests.dir)/*.test)
+
+bin.touint8array := ./touint8array
+$(bin.touint8array): $(bin.touint8array).c $(MAKEFILE)
+ $(CC) -o $@ $<
+CLEAN_FILES += $(bin.touint8array)
+
+ifneq (,$(tests.all))
+$(test-list.mjs): $(bin.touint8array) $(tests.all) $(MAKEFILE)
+ @{\
+ echo 'export default ['; \
+ sep=''; \
+ for f in $(sort $(tests.all)); do \
+ echo -en $$sep'{"name": "'$${f##*/}'", "content":'; \
+ $(bin.touint8array) < $$f; \
+ echo -n '}'; \
+ sep=',\n'; \
+ done; \
+ echo '];'; \
+ } > $@
+ @echo "Created $@"
+$(test-list.mjs.gz): $(test-list.mjs)
+ gzip -c $< > $@
+CLEAN_FILES += $(test-list.mjs.gz)
+all: $(test-list.mjs.gz)
+else
+ @echo "Cannot build $(test-list.mjs) for lack of input test files."; \
+ echo "Symlink ./tests to a directory containing SQLTester-format "; \
+ echo "test scripts named *.test, then try again"; \
+ exit 1
+endif
+
+.PHONY: clean distclean
+clean:
+ -rm -f $(CLEAN_FILES)
+distclean: clean
+ -rm -f $(DISTCLEAN_FILES)
diff --git a/ext/wasm/SQLTester/SQLTester.mjs b/ext/wasm/SQLTester/SQLTester.mjs
new file mode 100644
index 0000000000..a72399aefc
--- /dev/null
+++ b/ext/wasm/SQLTester/SQLTester.mjs
@@ -0,0 +1,1339 @@
+/*
+** 2023-08-29
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file contains the main application entry pointer for the JS
+** implementation of the SQLTester framework.
+**
+** This version is not well-documented because it's a direct port of
+** the Java immplementation, which is documented: in the main SQLite3
+** source tree, see ext/jni/src/org/sqlite/jni/tester/SQLite3Tester.java.
+*/
+
+import sqlite3ApiInit from '/jswasm/sqlite3.mjs';
+
+const sqlite3 = await sqlite3ApiInit();
+
+const log = (...args)=>{
+ console.log('SQLTester:',...args);
+};
+
+/**
+ Try to install vfsName as the new default VFS. Once this succeeds
+ (returns true) then it becomes a no-op on future calls. Throws if
+ vfs registration as the default VFS fails but has no side effects
+ if vfsName is not currently registered.
+*/
+const tryInstallVfs = function f(vfsName){
+ if(f.vfsName) return false;
+ const pVfs = sqlite3.capi.sqlite3_vfs_find(vfsName);
+ if(pVfs){
+ log("Installing",'"'+vfsName+'"',"as default VFS.");
+ const rc = sqlite3.capi.sqlite3_vfs_register(pVfs, 1);
+ if(rc){
+ sqlite3.SQLite3Error.toss(rc,"While trying to register",vfsName,"vfs.");
+ }
+ f.vfsName = vfsName;
+ }
+ return !!pVfs;
+};
+tryInstallVfs.vfsName = undefined;
+
+if( 0 && globalThis.WorkerGlobalScope ){
+ // Try OPFS storage, if available...
+ if( 0 && sqlite3.oo1.OpfsDb ){
+ /* Really slow with these tests */
+ tryInstallVfs("opfs");
+ }
+ if( sqlite3.installOpfsSAHPoolVfs ){
+ await sqlite3.installOpfsSAHPoolVfs({
+ clearOnInit: true,
+ initialCapacity: 15,
+ name: 'opfs-SQLTester'
+ }).then(pool=>{
+ tryInstallVfs(pool.vfsName);
+ }).catch(e=>{
+ log("OpfsSAHPool could not load:",e);
+ });
+ }
+}
+
+const wPost = (function(){
+ return (('undefined'===typeof WorkerGlobalScope)
+ ? ()=>{}
+ : (type, payload)=>{
+ postMessage({type, payload});
+ });
+})();
+//log("WorkerGlobalScope",globalThis.WorkerGlobalScope);
+
+// Return a new enum entry value
+const newE = ()=>Object.create(null);
+
+const newObj = (props)=>Object.assign(newE(), props);
+
+/**
+ Modes for how to escape (or not) column values and names from
+ SQLTester.execSql() to the result buffer output.
+*/
+const ResultBufferMode = Object.assign(Object.create(null),{
+ //! Do not append to result buffer
+ NONE: newE(),
+ //! Append output escaped.
+ ESCAPED: newE(),
+ //! Append output as-is
+ ASIS: newE()
+});
+
+/**
+ Modes to specify how to emit multi-row output from
+ SQLTester.execSql() to the result buffer.
+*/
+const ResultRowMode = newObj({
+ //! Keep all result rows on one line, space-separated.
+ ONLINE: newE(),
+ //! Add a newline between each result row.
+ NEWLINE: newE()
+});
+
+class SQLTesterException extends globalThis.Error {
+ constructor(testScript, ...args){
+ if(testScript){
+ super( [testScript.getOutputPrefix()+": ", ...args].join('') );
+ }else{
+ super( args.join('') );
+ }
+ this.name = 'SQLTesterException';
+ }
+ isFatal() { return false; }
+}
+
+SQLTesterException.toss = (...args)=>{
+ throw new SQLTesterException(...args);
+}
+
+class DbException extends SQLTesterException {
+ constructor(testScript, pDb, rc, closeDb=false){
+ super(testScript, "DB error #"+rc+": "+sqlite3.capi.sqlite3_errmsg(pDb));
+ this.name = 'DbException';
+ if( closeDb ) sqlite3.capi.sqlite3_close_v2(pDb);
+ }
+ isFatal() { return true; }
+}
+
+class TestScriptFailed extends SQLTesterException {
+ constructor(testScript, ...args){
+ super(testScript,...args);
+ this.name = 'TestScriptFailed';
+ }
+ isFatal() { return true; }
+}
+
+class UnknownCommand extends SQLTesterException {
+ constructor(testScript, cmdName){
+ super(testScript, cmdName);
+ this.name = 'UnknownCommand';
+ }
+ isFatal() { return true; }
+}
+
+class IncompatibleDirective extends SQLTesterException {
+ constructor(testScript, ...args){
+ super(testScript,...args);
+ this.name = 'IncompatibleDirective';
+ }
+}
+
+//! For throwing where an expression is required.
+const toss = (errType, ...args)=>{
+ throw new errType(...args);
+};
+
+const __utf8Decoder = new TextDecoder();
+const __utf8Encoder = new TextEncoder('utf-8');
+//! Workaround for Util.utf8Decode()
+const __SAB = ('undefined'===typeof globalThis.SharedArrayBuffer)
+ ? function(){} : globalThis.SharedArrayBuffer;
+
+
+/* Frequently-reused regexes. */
+const Rx = newObj({
+ requiredProperties: / REQUIRED_PROPERTIES:[ \t]*(\S.*)\s*$/,
+ scriptModuleName: / SCRIPT_MODULE_NAME:[ \t]*(\S+)\s*$/,
+ mixedModuleName: / ((MIXED_)?MODULE_NAME):[ \t]*(\S+)\s*$/,
+ command: /^--(([a-z-]+)( .*)?)$/,
+ //! "Special" characters - we have to escape output if it contains any.
+ special: /[\x00-\x20\x22\x5c\x7b\x7d]/,
+ squiggly: /[{}]/
+});
+
+const Util = newObj({
+ toss,
+
+ unlink: function(fn){
+ return 0==sqlite3.wasm.sqlite3_wasm_vfs_unlink(0,fn);
+ },
+
+ argvToString: (list)=>{
+ const m = [...list];
+ m.shift() /* strip command name */;
+ return m.join(" ")
+ },
+
+ utf8Decode: function(arrayBuffer, begin, end){
+ return __utf8Decoder.decode(
+ (arrayBuffer.buffer instanceof __SAB)
+ ? arrayBuffer.slice(begin, end)
+ : arrayBuffer.subarray(begin, end)
+ );
+ },
+
+ utf8Encode: (str)=>__utf8Encoder.encode(str),
+
+ strglob: sqlite3.wasm.xWrap('sqlite3_wasm_SQLTester_strglob','int',
+ ['string','string'])
+})/*Util*/;
+
+class Outer {
+ #lnBuf = [];
+ #verbosity = 0;
+ #logger = console.log.bind(console);
+
+ constructor(func){
+ if(func) this.setFunc(func);
+ }
+
+ logger(...args){
+ if(args.length){
+ this.#logger = args[0];
+ return this;
+ }
+ return this.#logger;
+ }
+
+ out(...args){
+ if( this.getOutputPrefix && !this.#lnBuf.length ){
+ this.#lnBuf.push(this.getOutputPrefix());
+ }
+ this.#lnBuf.push(...args);
+ return this;
+ }
+
+ #outlnImpl(vLevel, ...args){
+ if( this.getOutputPrefix && !this.#lnBuf.length ){
+ this.#lnBuf.push(this.getOutputPrefix());
+ }
+ this.#lnBuf.push(...args,'\n');
+ const msg = this.#lnBuf.join('');
+ this.#lnBuf.length = 0;
+ this.#logger(msg);
+ return this;
+ }
+
+ outln(...args){
+ return this.#outlnImpl(0,...args);
+ }
+
+ outputPrefix(){
+ if( 0==arguments.length ){
+ return (this.getOutputPrefix
+ ? (this.getOutputPrefix() ?? '') : '');
+ }else{
+ this.getOutputPrefix = arguments[0];
+ return this;
+ }
+ }
+
+ static #verboseLabel = ["🔈",/*"🔉",*/"🔊","📢"];
+ verboseN(lvl, args){
+ if( this.#verbosity>=lvl ){
+ this.#outlnImpl(lvl, Outer.#verboseLabel[lvl-1],': ',...args);
+ }
+ }
+ verbose1(...args){ return this.verboseN(1,args); }
+ verbose2(...args){ return this.verboseN(2,args); }
+ verbose3(...args){ return this.verboseN(3,args); }
+
+ verbosity(){
+ const rc = this.#verbosity;
+ if(arguments.length) this.#verbosity = +arguments[0];
+ return rc;
+ }
+
+}/*Outer*/
+
+class SQLTester {
+
+ //! Console output utility.
+ #outer = new Outer().outputPrefix( ()=>'SQLTester: ' );
+ //! List of input scripts.
+ #aScripts = [];
+ //! Test input buffer.
+ #inputBuffer = [];
+ //! Test result buffer.
+ #resultBuffer = [];
+ //! Output representation of SQL NULL.
+ #nullView;
+ metrics = newObj({
+ //! Total tests run
+ nTotalTest: 0,
+ //! Total test script files run
+ nTestFile: 0,
+ //! Test-case count for to the current TestScript
+ nTest: 0,
+ //! Names of scripts which were aborted.
+ failedScripts: []
+ });
+ #emitColNames = false;
+ //! True to keep going regardless of how a test fails.
+ #keepGoing = false;
+ #db = newObj({
+ //! The list of available db handles.
+ list: new Array(7),
+ //! Index into this.list of the current db.
+ iCurrentDb: 0,
+ //! Name of the default db, re-created for each script.
+ initialDbName: "test.db",
+ //! Buffer for REQUIRED_PROPERTIES pragmas.
+ initSql: ['select 1;'],
+ //! (sqlite3*) to the current db.
+ currentDb: function(){
+ return this.list[this.iCurrentDb];
+ }
+ });
+
+ constructor(){
+ this.reset();
+ }
+
+ outln(...args){ return this.#outer.outln(...args); }
+ out(...args){ return this.#outer.out(...args); }
+ outer(...args){
+ if(args.length){
+ this.#outer = args[0];
+ return this;
+ }
+ return this.#outer;
+ }
+ verbose1(...args){ return this.#outer.verboseN(1,args); }
+ verbose2(...args){ return this.#outer.verboseN(2,args); }
+ verbose3(...args){ return this.#outer.verboseN(3,args); }
+ verbosity(...args){
+ const rc = this.#outer.verbosity(...args);
+ return args.length ? this : rc;
+ }
+ setLogger(func){
+ this.#outer.logger(func);
+ return this;
+ }
+
+ incrementTestCounter(){
+ ++this.metrics.nTotalTest;
+ ++this.metrics.nTest;
+ }
+
+ reset(){
+ this.clearInputBuffer();
+ this.clearResultBuffer();
+ this.#clearBuffer(this.#db.initSql);
+ this.closeAllDbs();
+ this.metrics.nTest = 0;
+ this.#nullView = "nil";
+ this.emitColNames = false;
+ this.#db.iCurrentDb = 0;
+ //this.#db.initSql.push("SELECT 1;");
+ }
+
+ appendInput(line, addNL){
+ this.#inputBuffer.push(line);
+ if( addNL ) this.#inputBuffer.push('\n');
+ }
+ appendResult(line, addNL){
+ this.#resultBuffer.push(line);
+ if( addNL ) this.#resultBuffer.push('\n');
+ }
+ appendDbInitSql(sql){
+ this.#db.initSql.push(sql);
+ if( this.currentDb() ){
+ this.execSql(null, true, ResultBufferMode.NONE, null, sql);
+ }
+ }
+
+ #runInitSql(pDb){
+ let rc = 0;
+ for(const sql of this.#db.initSql){
+ this.#outer.verbose2("RUNNING DB INIT CODE: ",sql);
+ rc = this.execSql(pDb, false, ResultBufferMode.NONE, null, sql);
+ if( rc ) break;
+ }
+ return rc;
+ }
+
+#clearBuffer(buffer){
+ buffer.length = 0;
+ return buffer;
+ }
+
+ clearInputBuffer(){ return this.#clearBuffer(this.#inputBuffer); }
+ clearResultBuffer(){return this.#clearBuffer(this.#resultBuffer); }
+
+ getInputText(){ return this.#inputBuffer.join(''); }
+ getResultText(){ return this.#resultBuffer.join(''); }
+
+ #takeBuffer(buffer){
+ const s = buffer.join('');
+ buffer.length = 0;
+ return s;
+ }
+
+ takeInputBuffer(){
+ return this.#takeBuffer(this.#inputBuffer);
+ }
+ takeResultBuffer(){
+ return this.#takeBuffer(this.#resultBuffer);
+ }
+
+ nullValue(){
+ return (0==arguments.length)
+ ? this.#nullView
+ : (this.#nullView = ''+arguments[0]);
+ }
+
+ outputColumnNames(){
+ return (0==arguments.length)
+ ? this.#emitColNames
+ : (this.#emitColNames = !!arguments[0]);
+ }
+
+ currentDbId(){
+ return (0==arguments.length)
+ ? this.#db.iCurrentDb
+ : (this.#affirmDbId(arguments[0]).#db.iCurrentDb = arguments[0]);
+ }
+
+ #affirmDbId(id){
+ if(id<0 || id>=this.#db.list.length){
+ toss(SQLTesterException, "Database index ",id," is out of range.");
+ }
+ return this;
+ }
+
+ currentDb(...args){
+ if( 0!=args.length ){
+ this.#affirmDbId(id).#db.iCurrentDb = id;
+ }
+ return this.#db.currentDb();
+ }
+
+ getDbById(id){
+ return this.#affirmDbId(id).#db.list[id];
+ }
+
+ getCurrentDb(){ return this.#db.list[this.#db.iCurrentDb]; }
+
+
+ closeDb(id) {
+ if( 0==arguments.length ){
+ id = this.#db.iCurrentDb;
+ }
+ const pDb = this.#affirmDbId(id).#db.list[id];
+ if( pDb ){
+ sqlite3.capi.sqlite3_close_v2(pDb);
+ this.#db.list[id] = null;
+ }
+ }
+
+ closeAllDbs(){
+ for(let i = 0; i 0){
+ rc = this.#runInitSql(pDb);
+ }
+ if( 0!=rc ){
+ sqlite3.SQLite3Error.toss(
+ rc,
+ "sqlite3 result code",rc+":",
+ (pDb ? sqlite3.capi.sqlite3_errmsg(pDb)
+ : sqlite3.capi.sqlite3_errstr(rc))
+ );
+ }
+ return this.#db.list[this.#db.iCurrentDb] = pDb;
+ }catch(e){
+ sqlite3.capi.sqlite3_close_v2(pDb);
+ throw e;
+ }
+ }
+
+ addTestScript(ts){
+ if( 2===arguments.length ){
+ ts = new TestScript(arguments[0], arguments[1]);
+ }else if(ts instanceof Uint8Array){
+ ts = new TestScript('