diff --git a/ext/jni/GNUmakefile b/ext/jni/GNUmakefile index 44a0d74011..7c89e987ff 100644 --- a/ext/jni/GNUmakefile +++ b/ext/jni/GNUmakefile @@ -27,10 +27,18 @@ dir.bld := $(dir.jni)/bld dir.bld.c := $(dir.bld) dir.src.jni := $(dir.src)/org/sqlite/jni dir.src.jni.tester := $(dir.src.jni)/tester -mkdir := mkdir -p +dir.src.fts5 := $(dir.src.jni)/fts5 +mkdir ?= mkdir -p $(dir.bld.c): $(mkdir) $@ +javac.flags ?= -Xlint:unchecked -Xlint:deprecation +java.flags ?= +jnicheck ?= 1 +ifeq (1,$(jnicheck)) + java.flags += -Xcheck:jni +endif + classpath := $(dir.src) CLEAN_FILES := $(package.jar) DISTCLEAN_FILES := $(dir.jni)/*~ $(dir.src.c)/*~ $(dir.src.jni)/*~ @@ -44,10 +52,17 @@ SQLTester.class := $(SQLTester.java:.java=.class) ######################################################################## # The future of FTS5 customization in this API is as yet unclear. -# It would be a real doozy to bind to JNI. -enable.fts5 ?= 1 +# The pieces are all in place, and are all thin proxies so not much +# complexity, but some semantic changes were required in porting +# which are largely untested. +# +# Reminder: this flag influences the contents of $(sqlite3-jni.h), +# which is checked in. Please do not check in changes to that file in +# which the fts5 APIs have been stripped unless that feature is +# intended to be stripped for good. +enable.fts5 ?= 0 # If enable.tester is 0, the org/sqlite/jni/tester/* bits are elided. -enable.tester ?= 1 +enable.tester ?= $(if $(wildcard $(dir.src.jni.tester)/SQLTester.java),1,0) # bin.version-info = binary to output various sqlite3 version info # building the distribution zip file. @@ -62,7 +77,6 @@ JAVA_FILES.main := $(patsubst %,$(dir.src.jni)/%,\ annotation/Canonical.java \ annotation/NotNull.java \ annotation/Nullable.java \ - annotation/package-info.java \ AbstractCollationCallback.java \ AggregateFunction.java \ AuthorizerCallback.java \ @@ -71,6 +85,7 @@ JAVA_FILES.main := $(patsubst %,$(dir.src.jni)/%,\ CollationCallback.java \ CollationNeededCallback.java \ CommitHookCallback.java \ + ConfigLogCallback.java \ ConfigSqllogCallback.java \ NativePointerHolder.java \ OutputPointer.java \ @@ -80,60 +95,59 @@ JAVA_FILES.main := $(patsubst %,$(dir.src.jni)/%,\ RollbackHookCallback.java \ ScalarFunction.java \ SQLFunction.java \ + CallbackProxy.java \ SQLite3Jni.java \ - Tester1.java \ + TableColumnMetadata.java \ TraceV2Callback.java \ UpdateHookCallback.java \ - ValueHolder.java \ WindowFunction.java \ XDestroyCallback.java \ - package-info.java \ sqlite3.java \ sqlite3_context.java \ sqlite3_stmt.java \ sqlite3_value.java \ ) +JAVA_FILES.unittest := $(patsubst %,$(dir.src.jni)/%,\ + Tester1.java \ +) ifeq (1,$(enable.fts5)) - JAVA_FILES.main += $(patsubst %,$(dir.src.jni)/%,\ + JAVA_FILES.main += $(patsubst %,$(dir.src.fts5)/%,\ fts5_api.java \ fts5_extension_function.java \ fts5_tokenizer.java \ Fts5.java \ Fts5Context.java \ Fts5ExtensionApi.java \ - Fts5Function.java \ Fts5PhraseIter.java \ Fts5Tokenizer.java \ TesterFts5.java \ ) endif JAVA_FILES.tester := $(dir.src.jni.tester)/SQLTester.java +JAVA_FILES.package.info := \ + $(dir.src.jni)/package-info.java \ + $(dir.src.jni)/annotation/package-info.java CLASS_FILES.main := $(JAVA_FILES.main:.java=.class) +CLASS_FILES.unittest := $(JAVA_FILES.unittest:.java=.class) CLASS_FILES.tester := $(JAVA_FILES.tester:.java=.class) -JAVA_FILES += $(JAVA_FILES.main) +JAVA_FILES += $(JAVA_FILES.main) $(JAVA_FILES.unittest) ifeq (1,$(enable.tester)) JAVA_FILES += $(JAVA_FILES.tester) endif CLASS_FILES := -define DOTCLASS_DEPS -$(1).class: $(1).java $(MAKEFILE) +define CLASSFILE_DEPS all: $(1).class CLASS_FILES += $(1).class endef -$(foreach B,$(basename $(JAVA_FILES)),$(eval $(call DOTCLASS_DEPS,$(B)))) -$(CLASS_FILES.tester): $(CLASS_FILES.main) -javac.flags ?= -Xlint:unchecked -Xlint:deprecation -java.flags ?= -jnicheck ?= 1 -ifeq (1,$(jnicheck)) - java.flags += -Xcheck:jni -endif -$(SQLite3Jni.class): $(JAVA_FILES) +$(foreach B,$(basename \ + $(JAVA_FILES.main) $(JAVA_FILES.unittest) $(JAVA_FILES.tester)),\ + $(eval $(call CLASSFILE_DEPS,$(B)))) +$(CLASS_FILES): $(JAVA_FILES) $(MAKEFILE) $(bin.javac) $(javac.flags) -h $(dir.bld.c) -cp $(classpath) $(JAVA_FILES) -all: $(SQLite3Jni.class) + #.PHONY: classfiles ######################################################################## @@ -168,7 +182,9 @@ $(sqlite3.h): $(sqlite3.c): $(sqlite3.h) opt.threadsafe ?= 1 -opt.oom ?= 0 +opt.fatal-oom ?= 1 +opt.debug ?= 1 +opt.metrics ?= 1 SQLITE_OPT = \ -DSQLITE_ENABLE_RTREE \ -DSQLITE_ENABLE_EXPLAIN_COMMENTS \ @@ -186,10 +202,14 @@ SQLITE_OPT = \ -DSQLITE_TEMP_STORE=2 \ -DSQLITE_USE_URI=1 \ -DSQLITE_C=$(sqlite3.c) \ - -DSQLITE_JNI_FATAL_OOM=$(opt.oom) \ - -DSQLITE_DEBUG + -DSQLITE_JNI_FATAL_OOM=$(opt.fatal-oom) \ + -DSQLITE_JNI_ENABLE_METRICS=$(opt.metrics) -SQLITE_OPT += -g -DDEBUG -UNDEBUG +ifeq (1,$(opt.debug)) + SQLITE_OPT += -DSQLITE_DEBUG -g -DDEBUG -UNDEBUG +else + SQLITE_OPT += -O2 +endif ifeq (1,$(enable.fts5)) SQLITE_OPT += -DSQLITE_ENABLE_FTS5 @@ -198,25 +218,31 @@ endif sqlite3-jni.c := $(dir.src.c)/sqlite3-jni.c sqlite3-jni.o := $(dir.bld.c)/sqlite3-jni.o sqlite3-jni.h := $(dir.src.c)/sqlite3-jni.h -sqlite3-jni.dll := $(dir.bld.c)/libsqlite3-jni.so +package.dll := $(dir.bld.c)/libsqlite3-jni.so # All javac-generated .h files must be listed in $(sqlite3-jni.h.in): sqlite3-jni.h.in := +# $(java.with.jni) lists all Java files which contain JNI decls: +java.with.jni := define ADD_JNI_H -sqlite3-jni.h.in += $$(dir.bld.c)/org_sqlite_jni_$(1).h -$$(dir.bld.c)/org_sqlite_jni_$(1).h: $$(dir.src.jni)/$(1).java +sqlite3-jni.h.in += $$(dir.bld.c)/org_sqlite_jni$(3)_$(2).h +java.with.jni += $(1)/$(2).java +$$(dir.bld.c)/org_sqlite_jni$(3)_$(2).h: $(1)/$(2).java endef -$(eval $(call ADD_JNI_H,SQLite3Jni)) +# Invoke ADD_JNI_H once for each Java file which includes JNI +# declarations: +$(eval $(call ADD_JNI_H,$(dir.src.jni),SQLite3Jni,)) ifeq (1,$(enable.fts5)) - $(eval $(call ADD_JNI_H,Fts5ExtensionApi)) - $(eval $(call ADD_JNI_H,fts5_api)) - $(eval $(call ADD_JNI_H,fts5_tokenizer)) + $(eval $(call ADD_JNI_H,$(dir.src.fts5),Fts5ExtensionApi,_fts5)) + $(eval $(call ADD_JNI_H,$(dir.src.fts5),fts5_api,_fts5)) + $(eval $(call ADD_JNI_H,$(dir.src.fts5),fts5_tokenizer,_fts5)) endif ifeq (1,$(enable.tester)) - sqlite3-jni.h.in += $(dir.bld.c)/org_sqlite_jni_tester_SQLTester.h - $(dir.bld.c)/org_sqlite_jni_tester_SQLTester.h: $(dir.src.jni.tester)/SQLTester.java + $(eval $(call ADD_JNI_H,$(dir.src.jni.tester),SQLTester,_tester)) endif -#sqlite3-jni.dll.cfiles := $(dir.src.c) -sqlite3-jni.dll.cflags = \ +$(sqlite3-jni.h.in): $(dir.bld.c) + +#package.dll.cfiles := +package.dll.cflags = \ -fPIC \ -I. \ -I$(dir $(sqlite3.h)) \ @@ -233,22 +259,34 @@ sqlite3-jni.dll.cflags = \ # include path for client-level code. ######################################################################## ifeq (1,$(enable.tester)) - sqlite3-jni.dll.cflags += -DSQLITE_JNI_ENABLE_SQLTester + package.dll.cflags += -DSQLITE_JNI_ENABLE_SQLTester endif $(sqlite3-jni.h): $(sqlite3-jni.h.in) $(MAKEFILE) - cat $(sqlite3-jni.h.in) > $@ -$(sqlite3-jni.dll): $(sqlite3-jni.h) $(sqlite3.c) $(sqlite3.h) -$(sqlite3-jni.dll): $(dir.bld.c) $(sqlite3-jni.c) $(SQLite3Jni.java) $(MAKEFILE) - $(CC) $(sqlite3-jni.dll.cflags) $(SQLITE_OPT) \ + @cat $(sqlite3-jni.h.in) > $@.tmp + @if cmp $@ $@.tmp >/dev/null; then \ + rm -f $@.tmp; \ + echo "$@ not modified"; \ + else \ + mv $@.tmp $@; \ + echo "Updated $@"; \ + fi + @if [ x1 != x$(enable.fts5) ]; then \ + echo "*** REMINDER:"; \ + echo "*** enable.fts5=0, so please do not check in changes to $@."; \ + fi + +$(package.dll): $(sqlite3-jni.h) $(sqlite3.c) $(sqlite3.h) +$(package.dll): $(sqlite3-jni.c) $(MAKEFILE) + $(CC) $(package.dll.cflags) $(SQLITE_OPT) \ $(sqlite3-jni.c) -shared -o $@ -all: $(sqlite3-jni.dll) +all: $(package.dll) .PHONY: test test-one test.flags ?= test.main.flags = -ea -Djava.library.path=$(dir.bld.c) \ $(java.flags) -cp $(classpath) \ org.sqlite.jni.Tester1 -test.deps := $(SQLite3Jni.class) $(sqlite3-jni.dll) +test.deps := $(CLASS_FILES) $(package.dll) test-one: $(test.deps) $(bin.java) $(test.main.flags) $(test.flags) test-sqllog: $(test.deps) @@ -256,7 +294,7 @@ test-sqllog: $(test.deps) $(bin.java) $(test.main.flags) -sqllog test-mt: $(test.deps) @echo "Testing in multi-threaded mode:"; - $(bin.java) $(test.main.flags) -t 11 -r 50 -shuffle $(test.flags) + $(bin.java) $(test.main.flags) -t 7 -r 50 -shuffle $(test.flags) test: test-one test-mt tests: test test-sqllog @@ -265,7 +303,7 @@ tester.scripts := $(sort $(wildcard $(dir.src)/tests/*.test)) tester.flags ?= # --verbose .PHONY: tester tester-local tester-ext ifeq (1,$(enable.tester)) -tester-local: $(CLASS_FILES.tester) $(sqlite3-jni.dll) +tester-local: $(CLASS_FILES.tester) $(package.dll) $(bin.java) -ea -Djava.library.path=$(dir.bld.c) \ $(java.flags) -cp $(classpath) \ org.sqlite.jni.tester.SQLTester $(tester.flags) $(tester.scripts) @@ -296,41 +334,59 @@ endif tester-ext: tester-local tester: tester-ext tests: tester - ######################################################################## # Build each SQLITE_THREADMODE variant and run all tests against them. multitest: clean - $(MAKE) opt.threadsafe=0 opt.oom=1 tests clean - $(MAKE) opt.threadsafe=0 opt.oom=0 tests clean - $(MAKE) opt.threadsafe=1 opt.oom=1 tests clean - $(MAKE) opt.threadsafe=1 opt.oom=0 tests clean - $(MAKE) opt.threadsafe=2 opt.oom=1 tests clean - $(MAKE) opt.threadsafe=2 opt.oom=0 tests clean +define MULTIOPT +multitest: multitest-$(1) +multitest-$(1): + $$(MAKE) opt.debug=$$(opt.debug) $(patsubst %,opt.%,$(2)) \ + tests clean enable.fts5=1 +endef + +$(eval $(call MULTIOPT,01,threadsafe=0 oom=1)) +$(eval $(call MULTIOPT,00,threadsafe=0 oom=0)) +$(eval $(call MULTIOPT,11,threadsafe=1 oom=1)) +$(eval $(call MULTIOPT,10,threadsafe=1 oom=0)) +$(eval $(call MULTIOPT,21,threadsafe=2 oom=1)) +$(eval $(call MULTIOPT,20,threadsafe=2 oom=0)) ######################################################################## # jar bundle... package.jar.in := $(abspath $(dir.src)/jar.in) CLEAN_FILES += $(package.jar.in) -$(package.jar.in): $(MAKEFILE) $(CLASS_FILES.main) - cd $(dir.src); ls -1 org/sqlite/jni/*.java org/sqlite/jni/*.class > $@ - @echo "To use this jar you will need the -Djava.library.path=DIR/CONTAINING/libsqlite3-jni.so flag." - @echo "e.g. java -jar $@ -Djava.library.path=bld" +JAVA_FILES.jar := $(JAVA_FILES.main) $(JAVA_FILES.unittest) $(JAVA_FILES.package.info) +CLASS_FILES.jar := $(filter-out %/package-info.class,$(JAVA_FILES.jar:.java=.class)) +$(package.jar.in): $(package.dll) $(MAKEFILE) + ls -1 \ + $(dir.src.jni)/*.java $(dir.src.jni)/*.class \ + $(dir.src.jni)/annotation/*.java $(dir.src.jni)/annotation/*.class \ + | sed -e 's,^$(dir.src)/,,' | sort > $@ -$(package.jar): $(CLASS_FILES) $(MAKEFILE) $(package.jar.in) - rm -f $(dir.src)/c/*~ $(dir.src.jni)/*~ +$(package.jar): $(CLASS_FILES.jar) $(MAKEFILE) $(package.jar.in) + @rm -f $(dir.src)/c/*~ $(dir.src.jni)/*~ cd $(dir.src); $(bin.jar) -cfe ../$@ org.sqlite.jni.Tester1 @$(package.jar.in) + @ls -la $@ + @echo "To use this jar you will need the -Djava.library.path=DIR/CONTAINING/libsqlite3-jni.so flag." + @echo "e.g. java -Djava.library.path=bld -jar $@" jar: $(package.jar) +run-jar: $(package.jar) $(package.dll) + $(bin.java) -Djava.library.path=$(dir.bld) -jar $(package.jar) $(run-jar.flags) ######################################################################## # javadoc... dir.doc := $(dir.jni)/javadoc doc.index := $(dir.doc)/index.html +javadoc.exclude := -exclude org.sqlite.jni.tester +ifneq (1,$(enable.fts5)) + javadoc.exclude += -exclude org.sqlite.jni.fts5 +endif $(doc.index): $(JAVA_FILES.main) $(MAKEFILE) @if [ -d $(dir.doc) ]; then rm -fr $(dir.doc)/*; fi $(bin.javadoc) -cp $(classpath) -d $(dir.doc) -quiet \ - -subpackages org.sqlite.jni -exclude org.sqlite.jni.tester + -subpackages org.sqlite.jni $(javadoc.exclude) @echo "javadoc output is in $@" .PHONY: doc javadoc docserve @@ -348,7 +404,7 @@ docserve: $(doc.index) CLEAN_FILES += $(dir.bld.c)/* \ $(dir.src.jni)/*.class \ $(dir.src.jni.tester)/*.class \ - $(sqlite3-jni.dll) \ + $(package.dll) \ hs_err_pid*.log .PHONY: clean distclean diff --git a/ext/jni/src/c/sqlite3-jni.c b/ext/jni/src/c/sqlite3-jni.c index 20bc5cc986..0de280ac2a 100644 --- a/ext/jni/src/c/sqlite3-jni.c +++ b/ext/jni/src/c/sqlite3-jni.c @@ -113,14 +113,6 @@ # define SQLITE_THREADSAFE 1 #endif -/* -** 2023-08-25: initial attempts at running with SQLITE_THREADSAFE=0 -** lead to as-yet-uninvestigated bad reference errors from JNI. -*/ -#if 0 && SQLITE_THREADSAFE==0 -# error "This code currently requires SQLITE_THREADSAFE!=0." -#endif - /**********************************************************************/ /* SQLITE_USE_... */ #ifndef SQLITE_USE_URI @@ -132,9 +124,11 @@ ** Which sqlite3.c we're using needs to be configurable to enable ** building against a custom copy, e.g. the SEE variant. We have to ** include sqlite3.c, as opposed to sqlite3.h, in order to get access -** to SQLITE_MAX_... and friends. This increases the rebuild time -** considerably but we need this in order to keep the exported values -** of SQLITE_MAX_... and SQLITE_LIMIT_... in sync with the C build. +** to some interal details like SQLITE_MAX_... and friends. This +** increases the rebuild time considerably but we need this in order +** to access some internal functionality and keep the to-Java-exported +** values of SQLITE_MAX_... and SQLITE_LIMIT_... in sync with the C +** build. */ #ifndef SQLITE_C # define SQLITE_C sqlite3.c @@ -176,7 +170,8 @@ ** S3JniApi's intent is that CFunc be the C API func(s) the ** being-declared JNI function is wrapping, making it easier to find ** that function's JNI-side entry point. The other args are for JniDecl. - */ +** See the many examples in this file. +*/ #define S3JniApi(CFunc,ReturnType,Suffix) JniDecl(ReturnType,Suffix) /* @@ -217,7 +212,8 @@ /* ** Declares local var env = s3jni_env(). All JNI calls involve a ** JNIEnv somewhere, always named env, and many of our macros assume -** env is in scope. +** env is in scope. Where it's not, but should be, use this to make it +** so. */ #define S3JniDeclLocal_env JNIEnv * const env = s3jni_env() @@ -271,6 +267,9 @@ static void * s3jni_realloc_or_die(JNIEnv * const env, void * p, size_t n){ #else #define s3jni_oom_check(EXPR) #endif +//#define S3JniDb_oom(pDb,EXPR) ((EXPR) ? sqlite3OomFault(pDb) : 0) + +#define s3jni_db_oom(pDb) (void)((pDb) ? ((pDb)->mallocFailed=1) : 0) /* Helpers for Java value reference management. */ static jobject s3jni_ref_global(JNIEnv * const env, jobject const v){ @@ -295,42 +294,69 @@ static inline void s3jni_unref_local(JNIEnv * const env, jobject const v){ #define S3JniUnrefLocal(VAR) s3jni_unref_local(env, (VAR)) /* -** Key type for use with S3JniGlobal_nph(). +** Lookup key type for use with s3jni_nphop() and a cache of a +** frequently-needed Java-side class reference and one or two Java +** class member IDs. */ -typedef struct S3JniNphRef S3JniNphRef; -struct S3JniNphRef { +typedef struct S3JniNphOp S3JniNphOp; +struct S3JniNphOp { const int index /* index into S3JniGlobal.nph[] */; const char * const zName /* Full Java name of the class */; const char * const zMember /* Name of member property */; const char * const zTypeSig /* JNI type signature of zMember */; + /* + ** klazz is a global ref to the class represented by pRef. + ** + ** According to: + ** + ** https://developer.ibm.com/articles/j-jni/ + ** + ** > ... the IDs returned for a given class don't change for the + ** lifetime of the JVM process. But the call to get the field or + ** method can require significant work in the JVM, because fields + ** and methods might have been inherited from superclasses, making + ** the JVM walk up the class hierarchy to find them. Because the + ** IDs are the same for a given class, you should look them up + ** once and then reuse them. Similarly, looking up class objects + ** can be expensive, so they should be cached as well. + */ + jclass klazz; + volatile jfieldID fidValue /* NativePointerHolder.nativePointer or + ** OutputPointer.T.value */; + volatile jmethodID midCtor /* klazz's no-arg constructor. Used by + ** NativePointerHolder_new(). */; }; /* -** Cache keys for each concrete NativePointerHolder subclass and -** OutputPointer.T type. The members are to be used with -** S3JniGlobal_nph() and friends, and each one's member->index -** corresponds to its index in the S3JniGlobal.nph[] array. +** Cache keys for each concrete NativePointerHolder subclasses and +** OutputPointer.T types. The members are to be used with s3jni_nphop() +** and friends, and each one's member->index corresponds to its index +** in the S3JniGlobal.nph[] array. */ static const struct { - const S3JniNphRef sqlite3; - const S3JniNphRef sqlite3_stmt; - const S3JniNphRef sqlite3_context; - const S3JniNphRef sqlite3_value; - const S3JniNphRef OutputPointer_Int32; - const S3JniNphRef OutputPointer_Int64; - const S3JniNphRef OutputPointer_sqlite3; - const S3JniNphRef OutputPointer_sqlite3_stmt; - const S3JniNphRef OutputPointer_sqlite3_value; + const S3JniNphOp sqlite3; + const S3JniNphOp sqlite3_backup; + const S3JniNphOp sqlite3_blob; + const S3JniNphOp sqlite3_context; + const S3JniNphOp sqlite3_stmt; + const S3JniNphOp sqlite3_value; + const S3JniNphOp OutputPointer_Bool; + const S3JniNphOp OutputPointer_Int32; + const S3JniNphOp OutputPointer_Int64; + const S3JniNphOp OutputPointer_sqlite3; + const S3JniNphOp OutputPointer_sqlite3_blob; + const S3JniNphOp OutputPointer_sqlite3_stmt; + const S3JniNphOp OutputPointer_sqlite3_value; + const S3JniNphOp OutputPointer_String; #ifdef SQLITE_ENABLE_FTS5 - const S3JniNphRef OutputPointer_String; - const S3JniNphRef OutputPointer_ByteArray; - const S3JniNphRef Fts5Context; - const S3JniNphRef Fts5ExtensionApi; - const S3JniNphRef fts5_api; - const S3JniNphRef fts5_tokenizer; - const S3JniNphRef Fts5Tokenizer; + const S3JniNphOp OutputPointer_ByteArray; + const S3JniNphOp Fts5Context; + const S3JniNphOp Fts5ExtensionApi; + const S3JniNphOp fts5_api; + const S3JniNphOp fts5_tokenizer; + const S3JniNphOp Fts5Tokenizer; #endif -} S3JniNphRefs = { +} S3JniNphOps = { #define MkRef(INDEX, KLAZZ, MEMBER, SIG) \ { INDEX, "org/sqlite/jni/" KLAZZ, MEMBER, SIG } /* NativePointerHolder ref */ @@ -338,60 +364,47 @@ static const struct { /* OutputPointer.T ref */ #define RefO(INDEX, KLAZZ, SIG) MkRef(INDEX, KLAZZ, "value", SIG) RefN(0, "sqlite3"), - RefN(1, "sqlite3_stmt"), - RefN(2, "sqlite3_context"), - RefN(3, "sqlite3_value"), - RefO(4, "OutputPointer$Int32", "I"), - RefO(5, "OutputPointer$Int64", "J"), - RefO(6, "OutputPointer$sqlite3", + RefN(1, "sqlite3_backup"), + RefN(2, "sqlite3_blob"), + RefN(3, "sqlite3_context"), + RefN(4, "sqlite3_stmt"), + RefN(5, "sqlite3_value"), + RefO(6, "OutputPointer$Bool", "Z"), + RefO(7, "OutputPointer$Int32", "I"), + RefO(8, "OutputPointer$Int64", "J"), + RefO(9, "OutputPointer$sqlite3", "Lorg/sqlite/jni/sqlite3;"), - RefO(7, "OutputPointer$sqlite3_stmt", + RefO(10, "OutputPointer$sqlite3_blob", + "Lorg/sqlite/jni/sqlite3_blob;"), + RefO(11, "OutputPointer$sqlite3_stmt", "Lorg/sqlite/jni/sqlite3_stmt;"), - RefO(8, "OutputPointer$sqlite3_value", + RefO(12, "OutputPointer$sqlite3_value", "Lorg/sqlite/jni/sqlite3_value;"), + RefO(13, "OutputPointer$String", "Ljava/lang/String;"), #ifdef SQLITE_ENABLE_FTS5 - RefO(9, "OutputPointer$String", "Ljava/lang/String;"), - RefO(10, "OutputPointer$ByteArray", "[B"), - RefN(11, "Fts5Context"), - RefN(12, "Fts5ExtensionApi"), - RefN(13, "fts5_api"), - RefN(14, "fts5_tokenizer"), - RefN(15, "Fts5Tokenizer") + RefO(14, "OutputPointer$ByteArray", "[B"), + RefN(15, "fts5/Fts5Context"), + RefN(16, "fts5/Fts5ExtensionApi"), + RefN(17, "fts5/fts5_api"), + RefN(18, "fts5/fts5_tokenizer"), + RefN(19, "fts5/Fts5Tokenizer") #endif #undef MkRef #undef RefN #undef RefO }; +#define S3JniNph(T) &S3JniNphOps.T + enum { /* ** Size of the NativePointerHolder cache. Need enough space for ** (only) the library's NativePointerHolder and OutputPointer types, ** a fixed count known at build-time. This value needs to be - ** exactly the number of S3JniNphRef entries in the S3JniNphRefs + ** exactly the number of S3JniNphOp entries in the S3JniNphOps ** object. */ - S3Jni_NphCache_size = sizeof(S3JniNphRefs) / sizeof(S3JniNphRef) -}; - -/* -** Cache entry for NativePointerHolder subclasses and OutputPointer -** types. The pRef and klazz fields are set up the first time the -** entry is fetched using S3JniGlobal_nph(). The other fields are -** populated as needed by the routines which use them. -*/ -typedef struct S3JniNphClass S3JniNphClass; -struct S3JniNphClass { - volatile const S3JniNphRef * pRef /* Entry from S3JniNphRefs. */; - jclass klazz /* global ref to the concrete - ** NativePointerHolder subclass - ** represented by zClassName */; - volatile jmethodID midCtor /* klazz's no-arg constructor. Used by - ** new_NativePointerHolder_object(). */; - volatile jfieldID fidValue /* NativePointerHolder.nativePointer or - ** OutputPointer.T.value */; - volatile jfieldID fidAggCtx /* sqlite3_context.aggregateContext, used only - ** by the sqlite3_context binding. */; + S3Jni_NphCache_size = sizeof(S3JniNphOps) / sizeof(S3JniNphOp) }; /* @@ -403,10 +416,12 @@ struct S3JniHook{ jmethodID midCallback /* callback method. Signature depends on ** jObj's type */; /* We lookup the jObj.xDestroy() method as-needed for contexts which - ** have custom finalizers. */ + ** support custom finalizers. Fundamentally we can support them for + ** any Java type, but we only want to expose support for them where + ** the C API does. */ jobject jExtra /* Global ref to a per-hook-type value */; - int doXDestroy /* If true call jObj->xDestroy() when - this object is S3JniHook_unref()'d. */; + int doXDestroy /* If true then S3JniHook_unref() will call + jObj->xDestroy() if it's available. */; S3JniHook * pNext /* Next entry in S3Global.hooks.aFree */; }; /* For clean bitwise-copy init of local instances. */ @@ -416,6 +431,10 @@ static const S3JniHook S3JniHook_empty = {0,0,0,0,0}; ** Per-(sqlite3*) state for various JNI bindings. This state is ** allocated as needed, cleaned up in sqlite3_close(_v2)(), and ** recycled when possible. +** +** Trivia: vars and parameters of this type are often named "ps" +** because this class used to have a name for which that abbreviation +** made sense. */ typedef struct S3JniDb S3JniDb; struct S3JniDb { @@ -445,17 +464,28 @@ struct S3JniDb { #endif } hooks; #ifdef SQLITE_ENABLE_FTS5 - jobject jFtsApi /* global ref to s3jni_fts5_api_from_db() */; + /* FTS5-specific state */ + struct { + jobject jApi /* global ref to s3jni_fts5_api_from_db() */; + } fts; #endif S3JniDb * pNext /* Next entry in SJG.perDb.aFree */; }; +static const char * const S3JniDb_clientdata_key = "S3JniDb"; +#define S3JniDb_from_clientdata(pDb) \ + (pDb ? sqlite3_get_clientdata(pDb, S3JniDb_clientdata_key) : 0) + /* ** Cache for per-JNIEnv (i.e. per-thread) data. +** +** Trivia: vars and parameters of this type are often named "jc" +** because this class used to have a name for which that abbreviation +** made sense. */ typedef struct S3JniEnv S3JniEnv; struct S3JniEnv { - JNIEnv *env /* env in which this cache entry was created */; + JNIEnv *env /* JNIEnv in which this cache entry was created */; /* ** pdbOpening is used to coordinate the Java/DB connection of a ** being-open()'d db in the face of auto-extensions. @@ -516,10 +546,8 @@ struct S3JniUdf { S3JniUdf * pNext /* Next entry in SJG.udf.aFree. */; }; -#if !defined(SQLITE_JNI_OMIT_METRICS) && !defined(SQLITE_JNI_ENABLE_METRICS) -# ifdef SQLITE_DEBUG -# define SQLITE_JNI_ENABLE_METRICS -# endif +#if defined(SQLITE_JNI_ENABLE_METRICS) && 0==SQLITE_JNI_ENABLE_METRICS +# undef SQLITE_JNI_ENABLE_METRICS #endif /* @@ -551,27 +579,33 @@ struct S3JniGlobalType { ** threads. Caching a copy of the JavaVM object enables any thread ** with access to the cached object to get access to its own ** JNIEnv when necessary. - ** */ JavaVM * jvm; - /* Global mutex. */ + /* + ** Global mutex. It must not be used for anything which might call + ** back into the JNI layer. + */ sqlite3_mutex * mutex; /* - ** Cache of Java refs and method IDs for NativePointerHolder - ** subclasses and OutputPointer.T types. + ** Cache of references to Java classes and method IDs for + ** NativePointerHolder subclasses and OutputPointer.T types. */ - S3JniNphClass nph[S3Jni_NphCache_size]; + struct { + S3JniNphOp list[S3Jni_NphCache_size]; + sqlite3_mutex * mutex; /* mutex for this->list */ + volatile void const * locker; /* sanity-checking-only context object + for this->mutex */ + } nph; /* ** Cache of per-thread state. */ struct { S3JniEnv * aHead /* Linked list of in-use instances */; S3JniEnv * aFree /* Linked list of free instances */; - sqlite3_mutex * mutex /* mutex for aHead and aFree, first-time - inits of nph[] entries, and - NativePointerHolder_get/set(). */; - void const * locker /* env mutex is held on this object's behalf. - Used only for sanity checking. */; + sqlite3_mutex * mutex /* mutex for aHead and aFree. */; + volatile void const * locker /* env mutex is held on this + object's behalf. Used only for + sanity checking. */; } envCache; /* ** Per-db state. This can move into the core library once we can tie @@ -580,8 +614,10 @@ struct S3JniGlobalType { struct { S3JniDb * aFree /* Linked list of free instances */; sqlite3_mutex * mutex /* mutex for aHead and aFree */; - void const * locker /* perDb mutex is held on this object's - behalf. Used only for sanity checking. */; + volatile void const * locker + /* perDb mutex is held on this object's behalf. Used only for + sanity checking. Note that the mutex is at the class level, not + instance level. */; } perDb; struct { S3JniUdf * aFree /* Head of the free-item list. Guarded by global @@ -614,26 +650,31 @@ struct S3JniGlobalType { int nExt /* number of active entries in aExt, all in the first nExt'th array elements. */; sqlite3_mutex * mutex /* mutex for manipulation/traversal of aExt */; - const void * locker /* object on whose behalf the mutex is held. - Only for sanity checking in debug builds. */; + volatile const void * locker /* object on whose behalf the mutex + is held. Only for sanity checking + in debug builds. */; } autoExt; #ifdef SQLITE_ENABLE_FTS5 struct { - volatile jobject jFtsExt /* Global ref to Java singleton for the - Fts5ExtensionApi instance. */; + volatile jobject jExt /* Global ref to Java singleton for the + Fts5ExtensionApi instance. */; struct { - jfieldID fidA /* Fts5Phrase::a member */; - jfieldID fidB /* Fts5Phrase::b member */; + jfieldID fidA /* Fts5Phrase::a member */; + jfieldID fidB /* Fts5Phrase::b member */; } jPhraseIter; } fts5; #endif -#ifdef SQLITE_ENABLE_SQLLOG struct { - S3JniHook sqllog /* sqlite3_config(SQLITE_CONFIG_SQLLOG) callback */; - S3JniHook * aFree /* free-item list, for recycling. Guarded by - the global mutex. */; - } hooks; +#ifdef SQLITE_ENABLE_SQLLOG + S3JniHook sqllog /* sqlite3_config(SQLITE_CONFIG_SQLLOG) callback */; #endif + S3JniHook configlog /* sqlite3_config(SQLITE_CONFIG_LOG) callback */; + S3JniHook * aFree /* free-item list, for recycling. */; + sqlite3_mutex * mutex /* mutex for aFree */; + volatile const void * locker /* object on whose behalf the mutex + is held. Only for sanity checking + in debug builds. */; + } hook; #ifdef SQLITE_JNI_ENABLE_METRICS /* Internal metrics. */ struct { @@ -642,7 +683,8 @@ struct S3JniGlobalType { volatile unsigned nEnvAlloc; volatile unsigned nMutexEnv /* number of times envCache.mutex was entered for a S3JniEnv operation. */; - volatile unsigned nMutexEnv2 /* number of times envCache.mutex was entered */; + volatile unsigned nMutexNph /* number of times SJG.mutex was entered */; + volatile unsigned nMutexHook /* number of times SJG.mutex hooks.was entered */; volatile unsigned nMutexPerDb /* number of times perDb.mutex was entered */; volatile unsigned nMutexAutoExt /* number of times autoExt.mutex was entered */; volatile unsigned nMutexGlobal /* number of times global mutex was entered. */; @@ -690,77 +732,80 @@ static void s3jni_incr( volatile unsigned int * const p ){ /* Helpers for working with specific mutexes. */ #if SQLITE_THREADSAFE -#define S3JniMutex_Env_assertLocked \ +#define s3jni_mutex_enter2(M, Metric) \ + sqlite3_mutex_enter( M ); \ + s3jni_incr( &SJG.metrics.Metric ) +#define s3jni_mutex_leave2(M) \ + sqlite3_mutex_leave( M ) + +#define s3jni_mutex_enter(M, L, Metric) \ + assert( (void*)env != (void*)L && "Invalid use of " #L); \ + s3jni_mutex_enter2( M, Metric ); \ + L = env +#define s3jni_mutex_leave(M, L) \ + assert( (void*)env == (void*)L && "Invalid use of " #L); \ + L = 0; \ + s3jni_mutex_leave2( M ) + +#define S3JniEnv_mutex_assertLocked \ assert( 0 != SJG.envCache.locker && "Misuse of S3JniGlobal.envCache.mutex" ) -#define S3JniMutex_Env_assertLocker \ +#define S3JniEnv_mutex_assertLocker \ assert( (env) == SJG.envCache.locker && "Misuse of S3JniGlobal.envCache.mutex" ) -#define S3JniMutex_Env_assertNotLocker \ +#define S3JniEnv_mutex_assertNotLocker \ assert( (env) != SJG.envCache.locker && "Misuse of S3JniGlobal.envCache.mutex" ) -#define S3JniMutex_Env_enter \ - S3JniMutex_Env_assertNotLocker; \ - sqlite3_mutex_enter( SJG.envCache.mutex ); \ - s3jni_incr(&SJG.metrics.nMutexEnv); \ - SJG.envCache.locker = env -#define S3JniMutex_Env_leave \ - S3JniMutex_Env_assertLocker; \ - SJG.envCache.locker = 0; \ - sqlite3_mutex_leave( SJG.envCache.mutex ) +#define S3JniEnv_mutex_enter \ + s3jni_mutex_enter( SJG.envCache.mutex, SJG.envCache.locker, nMutexEnv ) +#define S3JniEnv_mutex_leave \ + s3jni_mutex_leave( SJG.envCache.mutex, SJG.envCache.locker ) -#define S3JniMutex_Ext_enter \ - sqlite3_mutex_enter( SJG.autoExt.mutex ); \ - SJG.autoExt.locker = env; \ - s3jni_incr( &SJG.metrics.nMutexAutoExt ) -#define S3JniMutex_Ext_leave \ - assert( env == SJG.autoExt.locker && "Misuse of S3JniGlobal.autoExt.mutex" ); \ - sqlite3_mutex_leave( SJG.autoExt.mutex ) -#define S3JniMutex_Ext_assertLocker \ +#define S3JniAutoExt_mutex_enter \ + s3jni_mutex_enter( SJG.autoExt.mutex, SJG.autoExt.locker, nMutexAutoExt ) +#define S3JniAutoExt_mutex_leave \ + s3jni_mutex_leave( SJG.autoExt.mutex, SJG.autoExt.locker ) +#define S3JniAutoExt_mutex_assertLocker \ assert( env == SJG.autoExt.locker && "Misuse of S3JniGlobal.autoExt.mutex" ) -#define S3JniMutex_Global_enter \ - sqlite3_mutex_enter( SJG.mutex ); \ - s3jni_incr(&SJG.metrics.nMutexGlobal); -#define S3JniMutex_Global_leave \ - sqlite3_mutex_leave( SJG.mutex ) +#define S3JniGlobal_mutex_enter \ + s3jni_mutex_enter2( SJG.mutex, nMutexGlobal ) +#define S3JniGlobal_mutex_leave \ + s3jni_mutex_leave2( SJG.mutex ) -#define S3JniMutex_Nph_enter \ - S3JniMutex_Env_assertNotLocker; \ - sqlite3_mutex_enter( SJG.envCache.mutex ); \ - s3jni_incr( &SJG.metrics.nMutexEnv2 ); \ - SJG.envCache.locker = env -#define S3JniMutex_Nph_leave \ - S3JniMutex_Env_assertLocker; \ - SJG.envCache.locker = 0; \ - sqlite3_mutex_leave( SJG.envCache.mutex ) +#define S3JniHook_mutex_enter \ + s3jni_mutex_enter( SJG.hook.mutex, SJG.hook.locker, nMutexHook ) +#define S3JniHook_mutex_leave \ + s3jni_mutex_leave( SJG.hook.mutex, SJG.hook.locker ) -#define S3JniMutex_S3JniDb_assertLocker \ +#define S3JniNph_mutex_enter \ + s3jni_mutex_enter( SJG.nph.mutex, SJG.nph.locker, nMutexNph ) +#define S3JniNph_mutex_leave \ + s3jni_mutex_leave( SJG.nph.mutex, SJG.nph.locker ) + +#define S3JniDb_mutex_assertLocker \ assert( (env) == SJG.perDb.locker && "Misuse of S3JniGlobal.perDb.mutex" ) -#define S3JniMutex_S3JniDb_enter \ - sqlite3_mutex_enter( SJG.perDb.mutex ); \ - assert( 0==SJG.perDb.locker && "Misuse of S3JniGlobal.perDb.mutex" ); \ - s3jni_incr( &SJG.metrics.nMutexPerDb ); \ - SJG.perDb.locker = env; -#define S3JniMutex_S3JniDb_leave \ - assert( env == SJG.perDb.locker && "Misuse of S3JniGlobal.perDb.mutex" ); \ - SJG.perDb.locker = 0; \ - sqlite3_mutex_leave( SJG.perDb.mutex ) +#define S3JniDb_mutex_enter \ + s3jni_mutex_enter( SJG.perDb.mutex, SJG.perDb.locker, nMutexPerDb ) +#define S3JniDb_mutex_leave \ + s3jni_mutex_leave( SJG.perDb.mutex, SJG.perDb.locker ) #else /* SQLITE_THREADSAFE==0 */ -#define S3JniMutex_Env_assertLocked -#define S3JniMutex_Env_assertLocker -#define S3JniMutex_Env_assertNotLocker -#define S3JniMutex_Env_enter -#define S3JniMutex_Env_leave -#define S3JniMutex_Ext_assertLocker -#define S3JniMutex_Ext_enter -#define S3JniMutex_Ext_leave -#define S3JniMutex_Global_enter -#define S3JniMutex_Global_leave -#define S3JniMutex_Nph_enter -#define S3JniMutex_Nph_leave -#define S3JniMutex_S3JniDb_assertLocker -#define S3JniMutex_S3JniDb_enter -#define S3JniMutex_S3JniDb_leave +#define S3JniAutoExt_mutex_assertLocker +#define S3JniAutoExt_mutex_enter +#define S3JniAutoExt_mutex_leave +#define S3JniDb_mutex_assertLocker +#define S3JniDb_mutex_enter +#define S3JniDb_mutex_leave +#define S3JniEnv_mutex_assertLocked +#define S3JniEnv_mutex_assertLocker +#define S3JniEnv_mutex_assertNotLocker +#define S3JniEnv_mutex_enter +#define S3JniEnv_mutex_leave +#define S3JniGlobal_mutex_enter +#define S3JniGlobal_mutex_leave +#define S3JniHook_mutex_enter +#define S3JniHook_mutex_leave +#define S3JniNph_mutex_enter +#define S3JniNph_mutex_leave #endif /* Helpers for jstring and jbyteArray. */ @@ -773,15 +818,17 @@ static const char * s3jni__jstring_to_mutf8_bytes(JNIEnv * const env, jstring v #define s3jni_jstring_to_mutf8(ARG) s3jni__jstring_to_mutf8_bytes(env, (ARG)) #define s3jni_mutf8_release(ARG,VAR) if( VAR ) (*env)->ReleaseStringUTFChars(env, ARG, VAR) -static jbyte * s3jni__jbytearray_bytes(JNIEnv * const env, jbyteArray jBA ){ +static jbyte * s3jni__jbyteArray_bytes(JNIEnv * const env, jbyteArray jBA ){ jbyte * const rv = jBA ? (*env)->GetByteArrayElements(env, jBA, NULL) : 0; s3jni_oom_check( jBA ? !!rv : 1 ); return rv; } -#define s3jni_jbytearray_bytes(jByteArray) s3jni__jbytearray_bytes(env, (jByteArray)) -#define s3jni_jbytearray_release(jByteArray,jBytes) \ +#define s3jni_jbyteArray_bytes(jByteArray) s3jni__jbyteArray_bytes(env, (jByteArray)) +#define s3jni_jbyteArray_release(jByteArray,jBytes) \ if( jBytes ) (*env)->ReleaseByteArrayElements(env, jByteArray, jBytes, JNI_ABORT) +#define s3jni_jbyteArray_commit(jByteArray,jBytes) \ + if( jBytes ) (*env)->ReleaseByteArrayElements(env, jByteArray, jBytes, JNI_COMMIT) /* ** Returns the current JNIEnv object. Fails fatally if it cannot find @@ -805,12 +852,12 @@ static JNIEnv * s3jni_env(void){ */ static S3JniEnv * S3JniEnv__get(JNIEnv * const env){ struct S3JniEnv * row; - S3JniMutex_Env_enter; + S3JniEnv_mutex_enter; row = SJG.envCache.aHead; for( ; row; row = row->pNext ){ if( row->env == env ){ s3jni_incr( &SJG.metrics.nEnvHit ); - S3JniMutex_Env_leave; + S3JniEnv_mutex_leave; return row; } } @@ -827,7 +874,7 @@ static S3JniEnv * S3JniEnv__get(JNIEnv * const env){ SJG.envCache.aHead = row; row->env = env; - S3JniMutex_Env_leave; + S3JniEnv_mutex_leave; return row; } @@ -1030,7 +1077,7 @@ static char * s3jni_exception_error_msg(JNIEnv * const env, jthrowable jx){ ** Returns errCode unless it is 0, in which case SQLITE_ERROR is ** returned. */ -static int s3jni__db_exception(JNIEnv * const env, S3JniDb * const ps, +static int s3jni__db_exception(JNIEnv * const env, sqlite3 * const pDb, int errCode, const char *zDfltMsg){ jthrowable const ex = (*env)->ExceptionOccurred(env); @@ -1038,17 +1085,17 @@ static int s3jni__db_exception(JNIEnv * const env, S3JniDb * const ps, if( ex ){ char * zMsg; S3JniExceptionClear; - S3JniMutex_S3JniDb_enter; zMsg = s3jni_exception_error_msg(env, ex); - s3jni_db_error(ps->pDb, errCode, zMsg ? zMsg : zDfltMsg); + s3jni_db_error(pDb, errCode, zMsg ? zMsg : zDfltMsg); sqlite3_free(zMsg); S3JniUnrefLocal(ex); - S3JniMutex_S3JniDb_leave; + }else if( zDfltMsg ){ + s3jni_db_error(pDb, errCode, zDfltMsg); } return errCode; } -#define s3jni_db_exception(JniDb,ERRCODE,DFLTMSG) \ - s3jni__db_exception(env, (JniDb), (ERRCODE), (DFLTMSG) ) +#define s3jni_db_exception(pDb,ERRCODE,DFLTMSG) \ + s3jni__db_exception(env, (pDb), (ERRCODE), (DFLTMSG) ) /* ** Extracts the (void xDestroy()) method from jObj and applies it to @@ -1080,10 +1127,10 @@ static void s3jni__call_xDestroy(JNIEnv * const env, jobject jObj){ /* ** Internal helper for many hook callback impls. Locks the S3JniDb -** mutex, makes a copy of src into dest, with a some differences: (1) if -** src->jObj or src->jExtra are not NULL then dest will be a new LOCAL -** ref to it instead of a copy of the prior GLOBAL ref. (2) dest->doXDestroy -** is always false. +** mutex, makes a copy of src into dest, with a some differences: (1) +** if src->jObj or src->jExtra are not NULL then dest will be a new +** LOCAL ref to it instead of a copy of the prior GLOBAL ref. (2) +** dest->doXDestroy is always false. ** ** If dest->jObj is not NULL when this returns then the caller is ** obligated to eventually free the new ref by passing *dest to @@ -1097,12 +1144,12 @@ static void s3jni__call_xDestroy(JNIEnv * const env, jobject jObj){ */ static void S3JniHook__localdup( JNIEnv * const env, S3JniHook const * const src, S3JniHook * const dest ){ - S3JniMutex_S3JniDb_enter; + S3JniHook_mutex_enter; *dest = *src; if(src->jObj) dest->jObj = S3JniRefLocal(src->jObj); if(src->jExtra) dest->jExtra = S3JniRefLocal(src->jExtra); dest->doXDestroy = 0; - S3JniMutex_S3JniDb_leave; + S3JniHook_mutex_leave; } #define S3JniHook_localdup(src,dest) S3JniHook__localdup(env,src,dest) @@ -1130,8 +1177,7 @@ static void S3JniHook__unref(JNIEnv * const env, S3JniHook * const s){ } *s = S3JniHook_empty; } -#define S3JniHook_unref(hook) \ - S3JniHook__unref(env, (hook)) +#define S3JniHook_unref(hook) S3JniHook__unref(env, (hook)) /* ** Allocates one blank S3JniHook object from the recycling bin, if @@ -1140,14 +1186,14 @@ static void S3JniHook__unref(JNIEnv * const env, S3JniHook * const s){ */ static S3JniHook *S3JniHook__alloc(JNIEnv * const env){ S3JniHook * p = 0; - S3JniMutex_Global_enter; - if( SJG.hooks.aFree ){ - p = SJG.hooks.aFree; - SJG.hooks.aFree = p->pNext; + S3JniHook_mutex_enter; + if( SJG.hook.aFree ){ + p = SJG.hook.aFree; + SJG.hook.aFree = p->pNext; p->pNext = 0; s3jni_incr(&SJG.metrics.nHookRecycled); } - S3JniMutex_Global_leave; + S3JniHook_mutex_leave; if( 0==p ){ p = s3jni_malloc(sizeof(S3JniHook)); if( p ){ @@ -1162,17 +1208,17 @@ static S3JniHook *S3JniHook__alloc(JNIEnv * const env){ #define S3JniHook_alloc() S3JniHook__alloc(env) /* -** The rightful fate of all results from S3JniHook_alloc(). doXDestroy -** is passed on as-is to S3JniHook_unref(). Locks the global mutex. +** The rightful fate of all results from S3JniHook_alloc(). Locks on +** SJG>hooks.mutex. */ static void S3JniHook__free(JNIEnv * const env, S3JniHook * const p){ if(p){ assert( !p->pNext ); S3JniHook_unref(p); - S3JniMutex_Global_enter; - p->pNext = SJG.hooks.aFree; - SJG.hooks.aFree = p; - S3JniMutex_Global_leave; + S3JniHook_mutex_enter; + p->pNext = SJG.hook.aFree; + SJG.hook.aFree = p; + S3JniHook_mutex_leave; } } #define S3JniHook_free(hook) S3JniHook__free(env, hook) @@ -1182,10 +1228,10 @@ static void S3JniHook__free(JNIEnv * const env, S3JniHook * const p){ static void S3JniHook__free_unlocked(JNIEnv * const env, S3JniHook * const p){ if(p){ assert( !p->pNext ); - assert( p->pNext != SJG.hooks.aFree ); + assert( p->pNext != SJG.hook.aFree ); S3JniHook_unref(p); - p->pNext = SJG.hooks.aFree; - SJG.hooks.aFree = p; + p->pNext = SJG.hook.aFree; + SJG.hook.aFree = p; } } #define S3JniHook_free_unlocked(hook) S3JniHook__free_unlocked(env, hook) @@ -1197,7 +1243,7 @@ static void S3JniHook__free_unlocked(JNIEnv * const env, S3JniHook * const p){ ** s->pNext and s->pPrev before calling this, as this clears them. */ static void S3JniDb_clear(JNIEnv * const env, S3JniDb * const s){ - S3JniMutex_S3JniDb_assertLocker; + S3JniDb_mutex_assertLocker; sqlite3_free( s->zMainDbName ); #define UNHOOK(MEMBER) \ S3JniHook_unref(&s->hooks.MEMBER) @@ -1223,7 +1269,7 @@ static void S3JniDb_clear(JNIEnv * const env, S3JniDb * const s){ */ static void S3JniDb__set_aside_unlocked(JNIEnv * const env, S3JniDb * const s){ assert( s ); - S3JniMutex_S3JniDb_assertLocker; + S3JniDb_mutex_assertLocker; if( s ){ S3JniDb_clear(env, s); s->pNext = SJG.perDb.aFree; @@ -1233,9 +1279,9 @@ static void S3JniDb__set_aside_unlocked(JNIEnv * const env, S3JniDb * const s){ #define S3JniDb_set_aside_unlocked(JniDb) S3JniDb__set_aside_unlocked(env, JniDb) static void S3JniDb__set_aside(JNIEnv * const env, S3JniDb * const s){ - S3JniMutex_S3JniDb_enter; + S3JniDb_mutex_enter; S3JniDb_set_aside_unlocked(s); - S3JniMutex_S3JniDb_leave; + S3JniDb_mutex_leave; } #define S3JniDb_set_aside(JNIDB) S3JniDb__set_aside(env, JNIDB) @@ -1250,7 +1296,8 @@ static void S3JniDb__set_aside(JNIEnv * const env, S3JniDb * const s){ static int S3JniEnv_uncache(JNIEnv * const env){ struct S3JniEnv * row; struct S3JniEnv * pPrev = 0; - S3JniMutex_Env_assertLocked; + + S3JniEnv_mutex_assertLocked; row = SJG.envCache.aHead; for( ; row; pPrev = row, row = row->pNext ){ if( row->env == env ){ @@ -1272,113 +1319,96 @@ static int S3JniEnv_uncache(JNIEnv * const env){ } /* -** Searches the NativePointerHolder cache for the given combination of -** args. It returns a cache entry with its klazz member set. This is -** an O(1) operation except on the first call for a given pRef, during -** which pRef->klazz and pRef->pRef are initialized thread-safely. In -** the latter case it's still effectively O(1), but with a much longer -** 1. +** Fetches the given nph-ref from cache the cache and returns the +** object with its klazz member set. This is an O(1) operation except +** on the first call for a given pRef, during which pRef->klazz and +** pRef->pRef are initialized thread-safely. In the latter case it's +** still effectively O(1), but with a much longer 1. ** ** It is up to the caller to populate the other members of the ** returned object if needed, taking care to lock the modification -** with S3JniMutex_Nph_enter/leave. +** with S3JniNph_mutex_enter/leave. ** ** This simple cache catches >99% of searches in the current ** (2023-07-31) tests. */ -static S3JniNphClass * S3JniGlobal__nph(JNIEnv * const env, S3JniNphRef const* pRef){ - /** - According to: +static S3JniNphOp * s3jni__nphop(JNIEnv * const env, S3JniNphOp const* pRef){ + S3JniNphOp * const pNC = &SJG.nph.list[pRef->index]; - https://developer.ibm.com/articles/j-jni/ - - > ... the IDs returned for a given class don't change for the - lifetime of the JVM process. But the call to get the field or - method can require significant work in the JVM, because - fields and methods might have been inherited from - superclasses, making the JVM walk up the class hierarchy to - find them. Because the IDs are the same for a given class, - you should look them up once and then reuse them. Similarly, - looking up class objects can be expensive, so they should be - cached as well. - */ - S3JniNphClass * const pNC = &SJG.nph[pRef->index]; - assert( (void*)pRef>=(void*)&S3JniNphRefs && (void*)pRef<(void*)(&S3JniNphRefs + 1) - && "pRef is out of range." ); + assert( (void*)pRef>=(void*)&S3JniNphOps && (void*)pRef<(void*)(&S3JniNphOps + 1) + && "pRef is out of range" ); assert( pRef->index>=0 - && (pRef->index < (sizeof(S3JniNphRefs) / sizeof(S3JniNphRef))) ); - if( !pNC->pRef ){ - S3JniMutex_Nph_enter; - if( !pNC->pRef ){ + && (pRef->index < (sizeof(S3JniNphOps) / sizeof(S3JniNphOp))) + && "pRef->index is out of range" ); + if( !pNC->klazz ){ + S3JniNph_mutex_enter; + if( !pNC->klazz ){ jclass const klazz = (*env)->FindClass(env, pRef->zName); S3JniExceptionIsFatal("FindClass() unexpectedly threw"); pNC->klazz = S3JniRefGlobal(klazz); - pNC->pRef = pRef - /* Must come last to avoid a race condition where pNC->klass - can be NULL after this function returns. */; } - S3JniMutex_Nph_leave; + S3JniNph_mutex_leave; } assert( pNC->klazz ); return pNC; } -#define S3JniGlobal_nph(PREF) S3JniGlobal__nph(env, PREF) +#define s3jni_nphop(PRef) s3jni__nphop(env, PRef) /* ** Common code for accessor functions for NativePointerHolder and -** OutputPointer types. pRef must be a pointer from S3JniNphRefs. jOut +** OutputPointer types. pRef must be a pointer from S3JniNphOps. jOut ** must be an instance of that class (Java's type safety takes care of ** that requirement). If necessary, this fetches the jfieldID for ** jOut's pRef->zMember, which must be of the type represented by the ** JNI type signature pRef->zTypeSig, and stores it in -** S3JniGlobal.nph[pRef->index]. Fails fatally if the pRef->zMember +** S3JniGlobal.nph.list[pRef->index]. Fails fatally if the pRef->zMember ** property is not found, as that presents a serious internal misuse. ** ** Property lookups are cached on a per-pRef basis. */ -static jfieldID s3jni_nphop_field(JNIEnv * const env, S3JniNphRef const* pRef){ - S3JniNphClass * const pNC = S3JniGlobal_nph(pRef); +static jfieldID s3jni_nphop_field(JNIEnv * const env, S3JniNphOp const* pRef){ + S3JniNphOp * const pNC = s3jni_nphop(pRef); if( !pNC->fidValue ){ - S3JniMutex_Nph_enter; + S3JniNph_mutex_enter; if( !pNC->fidValue ){ pNC->fidValue = (*env)->GetFieldID(env, pNC->klazz, pRef->zMember, pRef->zTypeSig); S3JniExceptionIsFatal("Code maintenance required: missing " - "required S3JniNphClass::fidValue."); + "required S3JniNphOp::fidValue."); } - S3JniMutex_Nph_leave; + S3JniNph_mutex_leave; } assert( pNC->fidValue ); return pNC->fidValue; } /* -** Sets a native ptr value in NativePointerHolder object ppOut. -** zClassName must be a static string so we can use its address -** as a cache key. +** Sets a native ptr value in NativePointerHolder object jNph, +** which must be of the native type described by pRef. jNph +** may not be NULL. */ -static void NativePointerHolder__set(JNIEnv * const env, S3JniNphRef const* pRef, - jobject ppOut, const void * p){ - assert( ppOut ); - (*env)->SetLongField(env, ppOut, s3jni_nphop_field(env, pRef), (jlong)p); +static void NativePointerHolder__set(JNIEnv * const env, S3JniNphOp const* pRef, + jobject jNph, const void * p){ + assert( jNph ); + (*env)->SetLongField(env, jNph, s3jni_nphop_field(env, pRef), (jlong)p); S3JniExceptionIsFatal("Could not set NativePointerHolder.nativePointer."); } -#define NativePointerHolder_set(PREF,PPOUT,P) \ - NativePointerHolder__set(env, PREF, PPOUT, P) +#define NativePointerHolder_set(PREF,JNPH,P) \ + NativePointerHolder__set(env, PREF, JNPH, P) /* -** Fetches a native ptr value from NativePointerHolder object pObj, +** Fetches a native ptr value from NativePointerHolder object jNph, ** which must be of the native type described by pRef. This is a -** no-op if pObj is NULL. +** no-op if jNph is NULL. */ -static void * NativePointerHolder__get(JNIEnv * env, jobject pObj, - S3JniNphRef const* pRef){ +static void * NativePointerHolder__get(JNIEnv * env, jobject jNph, + S3JniNphOp const* pRef){ void * rv = 0; - if( pObj ){ - rv = (void*)(*env)->GetLongField(env, pObj, s3jni_nphop_field(env, pRef)); + if( jNph ){ + rv = (void*)(*env)->GetLongField(env, jNph, s3jni_nphop_field(env, pRef)); S3JniExceptionIsFatal("Cannot fetch NativePointerHolder.nativePointer."); } return rv; @@ -1401,11 +1431,13 @@ static void * NativePointerHolder__get(JNIEnv * env, jobject pObj, ** argument is a Java sqlite3 object, as this operation only has void ** pointers to work with. */ -#define PtrGet_T(T,OBJ) NativePointerHolder_get(OBJ, &S3JniNphRefs.T) +#define PtrGet_T(T,OBJ) (T*)NativePointerHolder_get(OBJ, S3JniNph(T)) #define PtrGet_sqlite3(OBJ) PtrGet_T(sqlite3, OBJ) +#define PtrGet_sqlite3_backup(OBJ) PtrGet_T(sqlite3_backup, OBJ) +#define PtrGet_sqlite3_blob(OBJ) PtrGet_T(sqlite3_blob, OBJ) +#define PtrGet_sqlite3_context(OBJ) PtrGet_T(sqlite3_context, OBJ) #define PtrGet_sqlite3_stmt(OBJ) PtrGet_T(sqlite3_stmt, OBJ) #define PtrGet_sqlite3_value(OBJ) PtrGet_T(sqlite3_value, OBJ) -#define PtrGet_sqlite3_context(OBJ) PtrGet_T(sqlite3_context, OBJ) #if 0 /* @@ -1415,9 +1447,9 @@ static void * NativePointerHolder__get(JNIEnv * env, jobject pObj, */ static sqlite3* PtrGet__sqlite3_lock(JNIEnv * const env, jobject jObj){ sqlite3 *rv; - S3JniMutex_S3JniDb_enter; + S3JniDb_mutex_enter; rv = PtrGet_sqlite3(jObj); - if( !rv ){ S3JniMutex_S3JniDb_leave; } + if( !rv ){ S3JniDb_mutex_leave; } return rv; } #undef PtrGet_sqlite3 @@ -1431,14 +1463,16 @@ static sqlite3* PtrGet__sqlite3_lock(JNIEnv * const env, jobject jObj){ ** associated with jDb via NativePointerHolder_set(). */ static S3JniDb * S3JniDb_alloc(JNIEnv * const env, jobject jDb){ - S3JniDb * rv; - S3JniMutex_S3JniDb_enter; + S3JniDb * rv = 0; + S3JniDb_mutex_enter; if( SJG.perDb.aFree ){ rv = SJG.perDb.aFree; SJG.perDb.aFree = rv->pNext; rv->pNext = 0; s3jni_incr( &SJG.metrics.nPdbRecycled ); - }else{ + } + S3JniDb_mutex_leave; + if( 0==rv ){ rv = s3jni_malloc( sizeof(S3JniDb)); if( rv ){ s3jni_incr( &SJG.metrics.nPdbAlloc ); @@ -1448,74 +1482,36 @@ static S3JniDb * S3JniDb_alloc(JNIEnv * const env, jobject jDb){ memset(rv, 0, sizeof(S3JniDb)); rv->jDb = S3JniRefGlobal(jDb); } - S3JniMutex_S3JniDb_leave; return rv; } -#define S3JniDb_clientdata_key "S3JniDb" - -/* Short-lived code consolidator. */ -#define S3JniDb_search \ - s = pDb ? sqlite3_get_clientdata(pDb, S3JniDb_clientdata_key) : 0 - /* ** Returns the S3JniDb object for the given org.sqlite.jni.sqlite3 ** object, or NULL if jDb is NULL, no pointer can be extracted ** from it, or no matching entry can be found. -** -** Requires locking the S3JniDb mutex. */ static S3JniDb * S3JniDb__from_java(JNIEnv * const env, jobject jDb){ - S3JniDb * s = 0; - sqlite3 * pDb = 0; - - S3JniMutex_S3JniDb_enter; - if( jDb ) pDb = PtrGet_sqlite3(jDb); - S3JniDb_search; - S3JniMutex_S3JniDb_leave; - return s; + sqlite3 * const pDb = jDb ? PtrGet_sqlite3(jDb) : 0; + return pDb ? S3JniDb_from_clientdata(pDb) : 0; } #define S3JniDb_from_java(jObject) S3JniDb__from_java(env,(jObject)) - -static S3JniDb * S3JniDb__from_java_unlocked(JNIEnv * const env, jobject jDb){ - S3JniDb * s = 0; - sqlite3 * pDb = 0; - - S3JniMutex_S3JniDb_assertLocker; - if( jDb ) pDb = PtrGet_sqlite3(jDb); - S3JniDb_search; - return s; -} -#define S3JniDb_from_java_unlocked(JDB) S3JniDb__from_java_unlocked(env, (JDB)) - /* ** S3JniDb finalizer for use with sqlite3_set_clientdata(). */ static void S3JniDb_xDestroy(void *p){ - S3JniDb * ps = p; S3JniDeclLocal_env; - - assert( !ps->pNext ); + S3JniDb * const ps = p; + assert( !ps->pNext && "Else ps is already in the free-list."); S3JniDb_set_aside(ps); } /* -** Returns the S3JniDb object for the sqlite3 object, or NULL if pDb -** is NULL, or no matching entry -** can be found. -** -** Requires locking the S3JniDb mutex. +** Evaluates to the S3JniDb object for the given sqlite3 object, or +** NULL if pDb is NULL or was not initialized via the JNI interfaces. */ -static S3JniDb * S3JniDb__from_c(JNIEnv * const env, sqlite3 *pDb){ - S3JniDb * s = 0; - - S3JniMutex_S3JniDb_enter; - S3JniDb_search; - S3JniMutex_S3JniDb_leave; - return s; -} -#define S3JniDb_from_c(sqlite3Ptr) S3JniDb__from_c(env,(sqlite3Ptr)) +#define S3JniDb_from_c(sqlite3Ptr) \ + ((sqlite3Ptr) ? S3JniDb_from_clientdata(sqlite3Ptr) : 0) /* ** Unref any Java-side state in (S3JniAutoExtension*) AX and zero out @@ -1534,7 +1530,7 @@ static int S3JniAutoExtension_init(JNIEnv *const env, jobject const jAutoExt){ jclass const klazz = (*env)->GetObjectClass(env, jAutoExt); - S3JniMutex_Ext_assertLocker; + S3JniAutoExt_mutex_assertLocker; *ax = S3JniHook_empty; ax->midCallback = (*env)->GetMethodID(env, klazz, "call", "(Lorg/sqlite/jni/sqlite3;)I"); @@ -1548,6 +1544,18 @@ static int S3JniAutoExtension_init(JNIEnv *const env, return 0; } +/* +** Sets the value property of the OutputPointer.Bool jOut object to +** v. +*/ +static void OutputPointer_set_Bool(JNIEnv * const env, jobject const jOut, + int v){ + (*env)->SetBooleanField(env, jOut, s3jni_nphop_field( + env, S3JniNph(OutputPointer_Bool) + ), v ? JNI_TRUE : JNI_FALSE ); + S3JniExceptionIsFatal("Cannot set OutputPointer.Bool.value"); +} + /* ** Sets the value property of the OutputPointer.Int32 jOut object to ** v. @@ -1555,7 +1563,7 @@ static int S3JniAutoExtension_init(JNIEnv *const env, static void OutputPointer_set_Int32(JNIEnv * const env, jobject const jOut, int v){ (*env)->SetIntField(env, jOut, s3jni_nphop_field( - env, &S3JniNphRefs.OutputPointer_Int32 + env, S3JniNph(OutputPointer_Int32) ), (jint)v); S3JniExceptionIsFatal("Cannot set OutputPointer.Int32.value"); } @@ -1567,7 +1575,7 @@ static void OutputPointer_set_Int32(JNIEnv * const env, jobject const jOut, static void OutputPointer_set_Int64(JNIEnv * const env, jobject const jOut, jlong v){ (*env)->SetLongField(env, jOut, s3jni_nphop_field( - env, &S3JniNphRefs.OutputPointer_Int64 + env, S3JniNph(OutputPointer_Int64) ), v); S3JniExceptionIsFatal("Cannot set OutputPointer.Int64.value"); } @@ -1577,42 +1585,13 @@ static void OutputPointer_set_Int64(JNIEnv * const env, jobject const jOut, ** Object type. */ static void OutputPointer_set_obj(JNIEnv * const env, - S3JniNphRef const * const pRef, + S3JniNphOp const * const pRef, jobject const jOut, jobject v){ (*env)->SetObjectField(env, jOut, s3jni_nphop_field(env, pRef), v); S3JniExceptionIsFatal("Cannot set OutputPointer.T.value"); } -/* -** Sets the value property of the OutputPointer.sqlite3 jOut object to -** v. -*/ -static void OutputPointer_set_sqlite3(JNIEnv * const env, jobject const jOut, - jobject jDb){ - OutputPointer_set_obj(env, &S3JniNphRefs.OutputPointer_sqlite3, jOut, jDb); -} - -/* -** Sets the value property of the OutputPointer.sqlite3_stmt jOut object to -** v. -*/ -static void OutputPointer_set_sqlite3_stmt(JNIEnv * const env, jobject const jOut, - jobject jStmt){ - OutputPointer_set_obj(env, &S3JniNphRefs.OutputPointer_sqlite3_stmt, jOut, jStmt); -} - -#ifdef SQLITE_ENABLE_PREUPDATE_HOOK -/* -** Sets the value property of the OutputPointer.sqlite3_value jOut object to -** v. -*/ -static void OutputPointer_set_sqlite3_value(JNIEnv * const env, jobject const jOut, - jobject jValue){ - OutputPointer_set_obj(env, &S3JniNphRefs.OutputPointer_sqlite3_value, jOut, jValue); -} -#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ - #ifdef SQLITE_ENABLE_FTS5 #if 0 /* @@ -1621,9 +1600,10 @@ static void OutputPointer_set_sqlite3_value(JNIEnv * const env, jobject const jO */ static void OutputPointer_set_ByteArray(JNIEnv * const env, jobject const jOut, jbyteArray const v){ - OutputPointer_set_obj(env, &S3JniNphRefs.OutputPointer_ByteArray, jOut, v); + OutputPointer_set_obj(env, S3JniNph(OutputPointer_ByteArray), jOut, v); } #endif +#endif /* SQLITE_ENABLE_FTS5 */ /* ** Sets the value property of the OutputPointer.String jOut object to @@ -1631,9 +1611,8 @@ static void OutputPointer_set_ByteArray(JNIEnv * const env, jobject const jOut, */ static void OutputPointer_set_String(JNIEnv * const env, jobject const jOut, jstring const v){ - OutputPointer_set_obj(env, &S3JniNphRefs.OutputPointer_String, jOut, v); + OutputPointer_set_obj(env, S3JniNph(OutputPointer_String), jOut, v); } -#endif /* SQLITE_ENABLE_FTS5 */ /* ** Returns true if eTextRep is a valid sqlite3 encoding constant, else @@ -1650,43 +1629,42 @@ static int encodingTypeIsValid(int eTextRep){ } /* For use with sqlite3_result/value_pointer() */ -#define ResultJavaValuePtrStr "org.sqlite.jni.ResultJavaVal" +static const char * const ResultJavaValuePtrStr = "org.sqlite.jni.ResultJavaVal"; /* ** If v is not NULL, it must be a jobject global reference. Its -** reference is relinquished and v is freed. +** reference is relinquished. */ -static void ResultJavaValue_finalizer(void *v){ +static void S3Jni_jobject_finalizer(void *v){ if( v ){ S3JniDeclLocal_env; S3JniUnrefGlobal((jobject)v); } } - - /* -** Returns a new Java instance of the class named by zClassName, which +** Returns a new Java instance of the class referred to by pRef, which ** MUST be interface-compatible with NativePointerHolder and MUST have ** a no-arg constructor. The NativePointerHolder_set() method is -** passed the new Java object and pNative. Hypothetically returns NULL -** if Java fails to allocate, but the JNI docs are not entirely clear -** on that detail. +** passed the new Java object (which must not be NULL) and pNative +** (which may be NULL). Hypothetically returns NULL if Java fails to +** allocate, but the JNI docs are not entirely clear on that detail. ** -** Always use a static pointer from the S3JniNphRefs struct for the 2nd -** argument so that we can use pRef->index as an O(1) cache key. +** Always use a static pointer from the S3JniNphOps struct for the +** 2nd argument. */ -static jobject new_NativePointerHolder_object(JNIEnv * const env, S3JniNphRef const * pRef, - const void * pNative){ +static jobject NativePointerHolder_new(JNIEnv * const env, + S3JniNphOp const * pRef, + const void * pNative){ jobject rv = 0; - S3JniNphClass * const pNC = S3JniGlobal_nph(pRef); + S3JniNphOp * const pNC = s3jni_nphop(pRef); if( !pNC->midCtor ){ - S3JniMutex_Nph_enter; + S3JniNph_mutex_enter; if( !pNC->midCtor ){ pNC->midCtor = (*env)->GetMethodID(env, pNC->klazz, "", "()V"); S3JniExceptionIsFatal("Cannot find constructor for class."); } - S3JniMutex_Nph_leave; + S3JniNph_mutex_leave; } rv = (*env)->NewObject(env, pNC->klazz, pNC->midCtor); S3JniExceptionIsFatal("No-arg constructor threw."); @@ -1695,17 +1673,23 @@ static jobject new_NativePointerHolder_object(JNIEnv * const env, S3JniNphRef co return rv; } -static inline jobject new_sqlite3_wrapper(JNIEnv * const env, sqlite3 *sv){ - return new_NativePointerHolder_object(env, &S3JniNphRefs.sqlite3, sv); +static inline jobject new_java_sqlite3(JNIEnv * const env, sqlite3 *sv){ + return NativePointerHolder_new(env, S3JniNph(sqlite3), sv); } -static inline jobject new_sqlite3_context_wrapper(JNIEnv * const env, sqlite3_context *sv){ - return new_NativePointerHolder_object(env, &S3JniNphRefs.sqlite3_context, sv); +static inline jobject new_java_sqlite3_backup(JNIEnv * const env, sqlite3_backup *sv){ + return NativePointerHolder_new(env, S3JniNph(sqlite3_backup), sv); } -static inline jobject new_sqlite3_stmt_wrapper(JNIEnv * const env, sqlite3_stmt *sv){ - return new_NativePointerHolder_object(env, &S3JniNphRefs.sqlite3_stmt, sv); +static inline jobject new_java_sqlite3_blob(JNIEnv * const env, sqlite3_blob *sv){ + return NativePointerHolder_new(env, S3JniNph(sqlite3_blob), sv); } -static inline jobject new_sqlite3_value_wrapper(JNIEnv * const env, sqlite3_value *sv){ - return new_NativePointerHolder_object(env, &S3JniNphRefs.sqlite3_value, sv); +static inline jobject new_java_sqlite3_context(JNIEnv * const env, sqlite3_context *sv){ + return NativePointerHolder_new(env, S3JniNph(sqlite3_context), sv); +} +static inline jobject new_java_sqlite3_stmt(JNIEnv * const env, sqlite3_stmt *sv){ + return NativePointerHolder_new(env, S3JniNph(sqlite3_stmt), sv); +} +static inline jobject new_java_sqlite3_value(JNIEnv * const env, sqlite3_value *sv){ + return NativePointerHolder_new(env, S3JniNph(sqlite3_value), sv); } /* Helper typedefs for UDF callback types. */ @@ -1728,7 +1712,7 @@ typedef void (*udf_xFinal_f)(sqlite3_context*); static S3JniUdf * S3JniUdf_alloc(JNIEnv * const env, jobject jObj){ S3JniUdf * s = 0; - S3JniMutex_Global_enter; + S3JniGlobal_mutex_enter; s3jni_incr(&SJG.metrics.nMutexUdf); if( SJG.udf.aFree ){ s = SJG.udf.aFree; @@ -1736,7 +1720,7 @@ static S3JniUdf * S3JniUdf_alloc(JNIEnv * const env, jobject jObj){ s->pNext = 0; s3jni_incr(&SJG.metrics.nUdfRecycled); } - S3JniMutex_Global_leave; + S3JniGlobal_mutex_leave; if( !s ){ s = s3jni_malloc( sizeof(*s)); s3jni_incr(&SJG.metrics.nUdfAlloc); @@ -1765,7 +1749,8 @@ static S3JniUdf * S3JniUdf_alloc(JNIEnv * const env, jobject jObj){ S3JniUnrefLocal(klazz); if( s->jmidxFunc ) s->type = UDF_SCALAR; else if( s->jmidxStep && s->jmidxFinal ){ - s->type = s->jmidxValue ? UDF_WINDOW : UDF_AGGREGATE; + s->type = (s->jmidxValue && s->jmidxInverse) + ? UDF_WINDOW : UDF_AGGREGATE; }else{ s->type = UDF_UNKNOWN_TYPE; } @@ -1790,10 +1775,10 @@ static void S3JniUdf_free(JNIEnv * const env, S3JniUdf * const s, memset(s, 0, sizeof(*s)); } if( cacheIt ){ - S3JniMutex_Global_enter; + S3JniGlobal_mutex_enter; s->pNext = S3JniGlobal.udf.aFree; S3JniGlobal.udf.aFree = s; - S3JniMutex_Global_leave; + S3JniGlobal_mutex_leave; }else{ sqlite3_free( s ); } @@ -1817,25 +1802,27 @@ typedef struct { ** Converts the given (cx, argc, argv) into arguments for the given ** UDF, writing the result (Java wrappers for cx and argv) in the ** final 2 arguments. Returns 0 on success, SQLITE_NOMEM on allocation -** error. On error *jCx and *jArgv will be set to 0. +** error. On error *jCx and *jArgv will be set to 0. The output +** objects are of type org.sqlite.jni.sqlite3_context and +** array-of-org.sqlite3.jni.sqlite3_value, respectively. */ static int udf_args(JNIEnv *env, sqlite3_context * const cx, int argc, sqlite3_value**argv, jobject * jCx, jobjectArray *jArgv){ jobjectArray ja = 0; - jobject jcx = new_sqlite3_context_wrapper(env, cx); + jobject jcx = new_java_sqlite3_context(env, cx); jint i; *jCx = 0; *jArgv = 0; if( !jcx ) goto error_oom; ja = (*env)->NewObjectArray( - env, argc, S3JniGlobal_nph(&S3JniNphRefs.sqlite3_value)->klazz, + env, argc, s3jni_nphop(S3JniNph(sqlite3_value))->klazz, NULL); s3jni_oom_check( ja ); if( !ja ) goto error_oom; for(i = 0; i < argc; ++i){ - jobject jsv = new_sqlite3_value_wrapper(env, argv[i]); + jobject jsv = new_java_sqlite3_value(env, argv[i]); if( !jsv ) goto error_oom; (*env)->SetObjectArrayElement(env, ja, i, jsv); S3JniUnrefLocal(jsv)/*ja has a ref*/; @@ -1922,7 +1909,7 @@ static int udf_xFV(sqlite3_context* cx, S3JniUdf * s, jmethodID xMethodID, const char *zFuncType){ S3JniDeclLocal_env; - jobject jcx = new_sqlite3_context_wrapper(env, cx); + jobject jcx = new_java_sqlite3_context(env, cx); int rc = 0; int const isFinal = 'F'==zFuncType[1]/*xFinal*/; @@ -2014,6 +2001,11 @@ static void udf_xInverse(sqlite3_context* cx, int argc, JniDecl(jint,JniNameSuffix)(JniArgsEnvClass, jobject pStmt, jint n){ \ return (jint)CName(PtrGet_sqlite3_stmt(pStmt), (int)n); \ } +/** Create a trivial JNI wrapper for (boolish-int CName(sqlite3_stmt*)). */ +#define WRAP_BOOL_STMT(JniNameSuffix,CName) \ + JniDecl(jboolean,JniNameSuffix)(JniArgsEnvClass, jobject pStmt){ \ + return CName(PtrGet_sqlite3_stmt(pStmt)) ? JNI_TRUE : JNI_FALSE; \ + } /** Create a trivial JNI wrapper for (jstring CName(sqlite3_stmt*,int)). */ #define WRAP_STR_STMT_INT(JniNameSuffix,CName) \ JniDecl(jstring,JniNameSuffix)(JniArgsEnvClass, jobject pStmt, jint ndx){ \ @@ -2026,6 +2018,11 @@ static void udf_xInverse(sqlite3_context* cx, int argc, JniDecl(jint,JniNameSuffix)(JniArgsEnvClass, jobject pDb){ \ return (jint)CName(PtrGet_sqlite3(pDb)); \ } +/** Create a trivial JNI wrapper for (boolean CName(sqlite3*)). */ +#define WRAP_BOOL_DB(JniNameSuffix,CName) \ + JniDecl(jboolean,JniNameSuffix)(JniArgsEnvClass, jobject pDb){ \ + return CName(PtrGet_sqlite3(pDb)) ? JNI_TRUE : JNI_FALSE; \ + } /** Create a trivial JNI wrapper for (int64 CName(sqlite3*)). */ #define WRAP_INT64_DB(JniNameSuffix,CName) \ JniDecl(jlong,JniNameSuffix)(JniArgsEnvClass, jobject pDb){ \ @@ -2053,15 +2050,22 @@ WRAP_INT_STMT_INT(1column_1type, sqlite3_column_type) WRAP_INT_STMT(1data_1count, sqlite3_data_count) WRAP_INT_DB(1error_1offset, sqlite3_error_offset) WRAP_INT_DB(1extended_1errcode, sqlite3_extended_errcode) +WRAP_BOOL_DB(1get_1autocommit, sqlite3_get_autocommit) WRAP_MUTF8_VOID(1libversion, sqlite3_libversion) WRAP_INT_VOID(1libversion_1number, sqlite3_libversion_number) +WRAP_INT_VOID(1keyword_1count, sqlite3_keyword_count) #ifdef SQLITE_ENABLE_PREUPDATE_HOOK WRAP_INT_DB(1preupdate_1blobwrite, sqlite3_preupdate_blobwrite) WRAP_INT_DB(1preupdate_1count, sqlite3_preupdate_count) WRAP_INT_DB(1preupdate_1depth, sqlite3_preupdate_depth) #endif +WRAP_INT_INT(1release_1memory, sqlite3_release_memory) WRAP_INT_INT(1sleep, sqlite3_sleep) WRAP_MUTF8_VOID(1sourceid, sqlite3_sourceid) +WRAP_INT_STMT_INT(1stmt_1explain, sqlite3_stmt_explain) +WRAP_INT_STMT(1stmt_1isexplain, sqlite3_stmt_isexplain) +WRAP_BOOL_STMT(1stmt_1readonly, sqlite3_stmt_readonly) +WRAP_INT_DB(1system_1errno, sqlite3_system_errno) WRAP_INT_VOID(1threadsafe, sqlite3_threadsafe) WRAP_INT_DB(1total_1changes, sqlite3_total_changes) WRAP_INT64_DB(1total_1changes64, sqlite3_total_changes64) @@ -2074,6 +2078,7 @@ WRAP_INT_SVALUE(1value_1numeric_1type, sqlite3_value_numeric_type) WRAP_INT_SVALUE(1value_1subtype, sqlite3_value_subtype) WRAP_INT_SVALUE(1value_1type, sqlite3_value_type) +#undef WRAP_BOOL_DB #undef WRAP_INT64_DB #undef WRAP_INT_DB #undef WRAP_INT_INT @@ -2108,41 +2113,50 @@ static int s3jni_run_java_auto_extensions(sqlite3 *pDb, const char **pzErr, if( 0==SJG.autoExt.nExt ) return 0; env = s3jni_env(); jc = S3JniEnv_get(); - ps = jc->pdbOpening; + S3JniDb_mutex_enter; + ps = jc->pdbOpening ? jc->pdbOpening : S3JniDb_from_c(pDb); if( !ps ){ *pzErr = sqlite3_mprintf("Unexpected arrival of null S3JniDb in " "auto-extension runner."); + S3JniDb_mutex_leave; return SQLITE_ERROR; } - jc->pdbOpening = 0; - assert( !ps->pDb && "it's still being opened" ); assert( ps->jDb ); - rc = sqlite3_set_clientdata(pDb, S3JniDb_clientdata_key, - ps, 0/* we'll re-set this after open() - completes. */); - if( rc ){ - return rc; + if( !ps->pDb ){ + assert( jc->pdbOpening == ps ); + rc = sqlite3_set_clientdata(pDb, S3JniDb_clientdata_key, + ps, 0/* we'll re-set this after open() + completes. */); + if( rc ){ + S3JniDb_mutex_leave; + return rc; + } } - NativePointerHolder_set(&S3JniNphRefs.sqlite3, ps->jDb, pDb) - /* As of here, the Java/C connection is complete */; + else{ + assert( ps == jc->pdbOpening ); + jc->pdbOpening = 0; + } + S3JniDb_mutex_leave; + NativePointerHolder_set(S3JniNph(sqlite3), ps->jDb, pDb) + /* As of here, the Java/C connection is complete except for the + (temporary) lack of finalizer for the ps object. */; ps->pDb = pDb; for( i = 0; go && 0==rc; ++i ){ - S3JniAutoExtension ax = {0,0} + S3JniAutoExtension ax = S3JniHook_empty /* We need a copy of the auto-extension object, with our own ** local reference to it, to avoid a race condition with another ** thread manipulating the list during the call and invaliding - ** what ax points to. */; - S3JniMutex_Ext_enter; + ** what ax references. */; + S3JniAutoExt_mutex_enter; if( i >= SJG.autoExt.nExt ){ go = 0; }else{ - ax.jObj = S3JniRefLocal(SJG.autoExt.aExt[i].jObj); - ax.midCallback = SJG.autoExt.aExt[i].midCallback; + S3JniHook_localdup(&SJG.autoExt.aExt[i], &ax); } - S3JniMutex_Ext_leave; + S3JniAutoExt_mutex_leave; if( ax.jObj ){ rc = (*env)->CallIntMethod(env, ax.jObj, ax.midCallback, ps->jDb); - S3JniUnrefLocal(ax.jObj); + S3JniHook_localundup(ax); S3JniIfThrew { jthrowable const ex = (*env)->ExceptionOccurred(env); char * zMsg; @@ -2151,7 +2165,7 @@ static int s3jni_run_java_auto_extensions(sqlite3 *pDb, const char **pzErr, S3JniUnrefLocal(ex); *pzErr = sqlite3_mprintf("auto-extension threw: %s", zMsg); sqlite3_free(zMsg); - if( !rc ) rc = SQLITE_ERROR; + rc = SQLITE_ERROR; } } } @@ -2162,17 +2176,17 @@ S3JniApi(sqlite3_auto_extension(),jint,1auto_1extension)( JniArgsEnvClass, jobject jAutoExt ){ int i; - S3JniAutoExtension * ax; + S3JniAutoExtension * ax = 0; int rc = 0; if( !jAutoExt ) return SQLITE_MISUSE; - S3JniMutex_Ext_enter; + S3JniAutoExt_mutex_enter; for( i = 0; i < SJG.autoExt.nExt; ++i ){ /* Look for a match. */ ax = &SJG.autoExt.aExt[i]; if( ax->jObj && (*env)->IsSameObject(env, ax->jObj, jAutoExt) ){ /* same object, so this is a no-op. */ - S3JniMutex_Ext_leave; + S3JniAutoExt_mutex_leave; return 0; } } @@ -2215,19 +2229,70 @@ S3JniApi(sqlite3_auto_extension(),jint,1auto_1extension)( ++SJG.autoExt.nExt; } } - S3JniMutex_Ext_leave; + S3JniAutoExt_mutex_leave; return rc; } +S3JniApi(sqlite3_backup_finish(),jint,1backup_1finish)( + JniArgsEnvClass, jobject jBack +){ + sqlite3_backup * const pB = PtrGet_sqlite3_backup(jBack); + NativePointerHolder_set(S3JniNph(sqlite3_backup), jBack, 0); + return sqlite3_backup_finish(pB); +} + +S3JniApi(sqlite3_backup_init(),jobject,1backup_1init)( + JniArgsEnvClass, jobject jDbDest, jstring jTDest, + jobject jDbSrc, jstring jTSrc +){ + sqlite3 * const pDest = PtrGet_sqlite3(jDbDest); + sqlite3 * const pSrc = PtrGet_sqlite3(jDbSrc); + char * const zDest = s3jni_jstring_to_utf8(jTDest, 0); + char * const zSrc = s3jni_jstring_to_utf8(jTSrc, 0); + jobject rv = 0; + + if( pDest && pSrc && zDest && zSrc ){ + sqlite3_backup * const pB = + sqlite3_backup_init(pDest, zDest, pSrc, zSrc); + if( pB ){ + rv = new_java_sqlite3_backup(env, pB); + if( !rv ){ + sqlite3_backup_finish( pB ); + } + } + } + sqlite3_free(zDest); + sqlite3_free(zSrc); + return rv; +} + +S3JniApi(sqlite3_backup_pagecount(),jint,1backup_1pagecount)( + JniArgsEnvClass, jobject jBack +){ + return sqlite3_backup_pagecount(PtrGet_sqlite3_backup(jBack)); +} + +S3JniApi(sqlite3_backup_remaining(),jint,1backup_1remaining)( + JniArgsEnvClass, jobject jBack +){ + return sqlite3_backup_remaining(PtrGet_sqlite3_backup(jBack)); +} + +S3JniApi(sqlite3_backup_step(),jint,1backup_1step)( + JniArgsEnvClass, jobject jBack, jint nPage +){ + return sqlite3_backup_step(PtrGet_sqlite3_backup(jBack), (int)nPage); +} + S3JniApi(sqlite3_bind_blob(),jint,1bind_1blob)( JniArgsEnvClass, jobject jpStmt, jint ndx, jbyteArray baData, jint nMax ){ - jbyte * const pBuf = baData ? s3jni_jbytearray_bytes(baData) : 0; + jbyte * const pBuf = baData ? s3jni_jbyteArray_bytes(baData) : 0; int rc; if( pBuf ){ rc = sqlite3_bind_blob(PtrGet_sqlite3_stmt(jpStmt), (int)ndx, pBuf, (int)nMax, SQLITE_TRANSIENT); - s3jni_jbytearray_release(baData, pBuf); + s3jni_jbyteArray_release(baData, pBuf); }else{ rc = baData ? SQLITE_NOMEM @@ -2267,7 +2332,7 @@ S3JniApi(sqlite3_bind_java_object(),jint,1bind_1java_1object)( jobject const rv = val ? S3JniRefGlobal(val) : 0; if( rv ){ rc = sqlite3_bind_pointer(pStmt, ndx, rv, ResultJavaValuePtrStr, - ResultJavaValue_finalizer); + S3Jni_jobject_finalizer); }else if(val){ rc = SQLITE_NOMEM; } @@ -2287,33 +2352,46 @@ S3JniApi(sqlite3_bind_parameter_index(),jint,1bind_1parameter_1index)( JniArgsEnvClass, jobject jpStmt, jbyteArray jName ){ int rc = 0; - jbyte * const pBuf = s3jni_jbytearray_bytes(jName); + jbyte * const pBuf = s3jni_jbyteArray_bytes(jName); if( pBuf ){ rc = sqlite3_bind_parameter_index(PtrGet_sqlite3_stmt(jpStmt), (const char *)pBuf); - s3jni_jbytearray_release(jName, pBuf); + s3jni_jbyteArray_release(jName, pBuf); } return rc; } +S3JniApi(sqlite3_bind_parameter_name(),jstring,1bind_1parameter_1name)( + JniArgsEnvClass, jobject jpStmt, jint ndx +){ + jstring rv = 0; + const char *z = + sqlite3_bind_parameter_name(PtrGet_sqlite3_stmt(jpStmt), (int)ndx); + + if( z ){ + rv = s3jni_utf8_to_jstring(z, -1); + } + return rv; +} + S3JniApi(sqlite3_bind_text(),jint,1bind_1text)( JniArgsEnvClass, jobject jpStmt, jint ndx, jbyteArray baData, jint nMax ){ - jbyte * const pBuf = baData ? s3jni_jbytearray_bytes(baData) : 0; + jbyte * const pBuf = baData ? s3jni_jbyteArray_bytes(baData) : 0; int const rc = sqlite3_bind_text(PtrGet_sqlite3_stmt(jpStmt), (int)ndx, (const char *)pBuf, (int)nMax, SQLITE_TRANSIENT); - s3jni_jbytearray_release(baData, pBuf); + s3jni_jbyteArray_release(baData, pBuf); return (jint)rc; } -S3JniApi(sqlite3_text16(),jint,1bind_1text16)( +S3JniApi(sqlite3_bind_text16(),jint,1bind_1text16)( JniArgsEnvClass, jobject jpStmt, jint ndx, jbyteArray baData, jint nMax ){ - jbyte * const pBuf = baData ? s3jni_jbytearray_bytes(baData) : 0; + jbyte * const pBuf = baData ? s3jni_jbyteArray_bytes(baData) : 0; int const rc = sqlite3_bind_text16(PtrGet_sqlite3_stmt(jpStmt), (int)ndx, pBuf, (int)nMax, SQLITE_TRANSIENT); - s3jni_jbytearray_release(baData, pBuf); + s3jni_jbyteArray_release(baData, pBuf); return (jint)rc; } @@ -2329,6 +2407,93 @@ S3JniApi(sqlite3_bind_zeroblob(),jint,1bind_1zeroblob64)( return (jint)sqlite3_bind_zeroblob(PtrGet_sqlite3_stmt(jpStmt), (int)ndx, (sqlite3_uint64)n); } +S3JniApi(sqlite3_blob_bytes(),jint,1blob_1bytes)( + JniArgsEnvClass, jobject jBlob +){ + return sqlite3_blob_bytes(PtrGet_sqlite3_blob(jBlob)); +} + +S3JniApi(sqlite3_blob_close(),jint,1blob_1close)( + JniArgsEnvClass, jobject jBlob +){ + sqlite3_blob * const b = PtrGet_sqlite3_blob(jBlob); + jint const rc = b ? (jint)sqlite3_blob_close(b) : SQLITE_MISUSE; + if( b ){ + NativePointerHolder_set(S3JniNph(sqlite3_blob), jBlob, 0); + } + return rc; +} + +S3JniApi(sqlite3_blob_open(),jint,1blob_1open)( + JniArgsEnvClass, jobject jDb, jstring jDbName, jstring jTbl, jstring jCol, + jlong jRowId, jint flags, jobject jOut +){ + sqlite3 * const db = PtrGet_sqlite3(jDb); + sqlite3_blob * pBlob = 0; + char * zDbName = 0, * zTableName = 0, * zColumnName = 0; + int rc; + + if( !db || !jDbName || !jTbl || !jCol ) return SQLITE_MISUSE; + zDbName = s3jni_jstring_to_utf8(jDbName,0); + zTableName = zDbName ? s3jni_jstring_to_utf8(jTbl,0) : 0; + zColumnName = zTableName ? s3jni_jstring_to_utf8(jCol,0) : 0; + rc = zColumnName + ? sqlite3_blob_open(db, zDbName, zTableName, zColumnName, + (sqlite3_int64)jRowId, (int)flags, &pBlob) + : SQLITE_NOMEM; + if( 0==rc ){ + jobject rv = new_java_sqlite3_blob(env, pBlob); + if( !rv ){ + sqlite3_blob_close(pBlob); + rc = SQLITE_NOMEM; + } + OutputPointer_set_obj(env, S3JniNph(OutputPointer_sqlite3_blob), jOut, rv); + } + sqlite3_free(zDbName); + sqlite3_free(zTableName); + sqlite3_free(zColumnName); + return rc; +} + +S3JniApi(sqlite3_blob_read(),jint,1blob_1read)( + JniArgsEnvClass, jobject jBlob, jbyteArray jTgt, jint iOffset +){ + jbyte * const pBa = s3jni_jbyteArray_bytes(jTgt); + int rc = jTgt ? (pBa ? SQLITE_MISUSE : SQLITE_NOMEM) : SQLITE_MISUSE; + if( pBa ){ + jsize const nTgt = (*env)->GetArrayLength(env, jTgt); + rc = sqlite3_blob_read(PtrGet_sqlite3_blob(jBlob), pBa, + (int)nTgt, (int)iOffset); + if( 0==rc ){ + s3jni_jbyteArray_commit(jTgt, pBa); + }else{ + s3jni_jbyteArray_release(jTgt, pBa); + } + } + return rc; +} + +S3JniApi(sqlite3_blob_reopen(),jint,1blob_1reopen)( + JniArgsEnvClass, jobject jBlob, jlong iNewRowId +){ + return (jint)sqlite3_blob_reopen(PtrGet_sqlite3_blob(jBlob), + (sqlite3_int64)iNewRowId); +} + +S3JniApi(sqlite3_blob_write(),jint,1blob_1write)( + JniArgsEnvClass, jobject jBlob, jbyteArray jBa, jint iOffset +){ + sqlite3_blob * const b = PtrGet_sqlite3_blob(jBlob); + jbyte * const pBuf = b ? s3jni_jbyteArray_bytes(jBa) : 0; + const jsize nBa = pBuf ? (*env)->GetArrayLength(env, jBa) : 0; + int rc = SQLITE_MISUSE; + if(b && pBuf){ + rc = sqlite3_blob_write( b, pBuf, (int)nBa, (int)iOffset ); + } + s3jni_jbyteArray_release(jBa, pBuf); + return (jint)rc; +} + /* Central C-to-Java busy handler proxy. */ static int s3jni_busy_handler(void* pState, int n){ S3JniDb * const ps = (S3JniDb *)pState; @@ -2342,7 +2507,7 @@ static int s3jni_busy_handler(void* pState, int n){ hook.midCallback, (jint)n); S3JniIfThrew{ S3JniExceptionWarnCallbackThrew("sqlite3_busy_handler() callback"); - rc = s3jni_db_exception(ps, SQLITE_ERROR, + rc = s3jni_db_exception(ps->pDb, SQLITE_ERROR, "sqlite3_busy_handler() callback threw."); } S3JniHook_localundup(hook); @@ -2359,7 +2524,7 @@ S3JniApi(sqlite3_busy_handler(),jint,1busy_1handler)( int rc = 0; if( !ps ) return (jint)SQLITE_MISUSE; - S3JniMutex_S3JniDb_enter; + S3JniDb_mutex_enter; if( jBusy ){ if( pHook->jObj && (*env)->IsSameObject(env, pHook->jObj, jBusy) ){ /* Same object - this is a no-op. */ @@ -2379,7 +2544,7 @@ S3JniApi(sqlite3_busy_handler(),jint,1busy_1handler)( rc = sqlite3_busy_handler(ps->pDb, s3jni_busy_handler, ps); if( 0==rc ){ S3JniHook_unref(pHook); - *pHook = hook; + *pHook = hook /* transfer Java ref ownership */; hook = S3JniHook_empty; } }/* else no-op */ @@ -2391,7 +2556,7 @@ S3JniApi(sqlite3_busy_handler(),jint,1busy_1handler)( } } S3JniHook_unref(&hook); - S3JniMutex_S3JniDb_leave; + S3JniDb_mutex_leave; return rc; } @@ -2401,10 +2566,10 @@ S3JniApi(sqlite3_busy_timeout(),jint,1busy_1timeout)( S3JniDb * const ps = S3JniDb_from_java(jDb); int rc = SQLITE_MISUSE; if( ps ){ - S3JniMutex_S3JniDb_enter; + S3JniDb_mutex_enter; S3JniHook_unref(&ps->hooks.busyHandler); rc = sqlite3_busy_timeout(ps->pDb, (int)ms); - S3JniMutex_S3JniDb_leave; + S3JniDb_mutex_leave; } return rc; } @@ -2415,8 +2580,8 @@ S3JniApi(sqlite3_cancel_auto_extension(),jboolean,1cancel_1auto_1extension)( S3JniAutoExtension * ax; jboolean rc = JNI_FALSE; int i; - S3JniMutex_Ext_enter; - /* This algo mirrors the one in the core. */ + S3JniAutoExt_mutex_enter; + /* This algo corresponds to the one in the core. */ for( i = SJG.autoExt.nExt-1; i >= 0; --i ){ ax = &SJG.autoExt.aExt[i]; if( ax->jObj && (*env)->IsSameObject(env, ax->jObj, jAutoExt) ){ @@ -2430,11 +2595,10 @@ S3JniApi(sqlite3_cancel_auto_extension(),jboolean,1cancel_1auto_1extension)( break; } } - S3JniMutex_Ext_leave; + S3JniAutoExt_mutex_leave; return rc; } - /* Wrapper for sqlite3_close(_v2)(). */ static jint s3jni_close_db(JNIEnv * const env, jobject jDb, int version){ int rc = 0; @@ -2446,7 +2610,7 @@ static jint s3jni_close_db(JNIEnv * const env, jobject jDb, int version){ ? (jint)sqlite3_close(ps->pDb) : (jint)sqlite3_close_v2(ps->pDb); if( 0==rc ){ - NativePointerHolder_set(&S3JniNphRefs.sqlite3, jDb, 0); + NativePointerHolder_set(S3JniNph(sqlite3), jDb, 0); } } return (jint)rc; @@ -2513,10 +2677,10 @@ S3JniApi(sqlite3_collation_needed(),jint,1collation_1needed)( S3JniCollationNeeded * pHook; int rc = 0; - S3JniMutex_S3JniDb_enter; - ps = S3JniDb_from_java_unlocked(jDb); + S3JniDb_mutex_enter; + ps = S3JniDb_from_java(jDb); if( !ps ){ - S3JniMutex_S3JniDb_leave; + S3JniDb_mutex_leave; return SQLITE_MISUSE; } pHook = &ps->hooks.collationNeeded; @@ -2535,11 +2699,12 @@ S3JniApi(sqlite3_collation_needed(),jint,1collation_1needed)( ); S3JniUnrefLocal(klazz); S3JniIfThrew { - rc = s3jni_db_exception(ps, SQLITE_MISUSE, + rc = s3jni_db_exception(ps->pDb, SQLITE_MISUSE, "Cannot not find matching call() in " "CollationNeededCallback object."); }else{ - rc = sqlite3_collation_needed16(ps->pDb, pHook, s3jni_collation_needed_impl16); + rc = sqlite3_collation_needed16(ps->pDb, pHook, + s3jni_collation_needed_impl16); if( 0==rc ){ S3JniHook_unref(pHook); pHook->midCallback = xCallback; @@ -2548,7 +2713,7 @@ S3JniApi(sqlite3_collation_needed(),jint,1collation_1needed)( } } } - S3JniMutex_S3JniDb_leave; + S3JniDb_mutex_leave; return rc; } @@ -2580,7 +2745,7 @@ S3JniApi(sqlite3_column_int64(),jlong,1column_1int64)( return (jlong)sqlite3_column_int64(PtrGet_sqlite3_stmt(jpStmt), (int)ndx); } -S3JniApi(sqlite3_column_text(),jbyteArray,1column_1text_1utf8)( +S3JniApi(sqlite3_column_text(),jbyteArray,1column_1text)( JniArgsEnvClass, jobject jpStmt, jint ndx ){ sqlite3_stmt * const stmt = PtrGet_sqlite3_stmt(jpStmt); @@ -2589,6 +2754,8 @@ S3JniApi(sqlite3_column_text(),jbyteArray,1column_1text_1utf8)( return p ? s3jni_new_jbyteArray(p, n) : NULL; } +#if 0 +// this impl might prove useful. S3JniApi(sqlite3_column_text(),jstring,1column_1text)( JniArgsEnvClass, jobject jpStmt, jint ndx ){ @@ -2597,6 +2764,7 @@ S3JniApi(sqlite3_column_text(),jstring,1column_1text)( const unsigned char * const p = sqlite3_column_text(stmt, (int)ndx); return p ? s3jni_utf8_to_jstring( (const char *)p, n) : 0; } +#endif S3JniApi(sqlite3_column_text16(),jstring,1column_1text16)( JniArgsEnvClass, jobject jpStmt, jint ndx @@ -2612,11 +2780,11 @@ S3JniApi(sqlite3_column_value(),jobject,1column_1value)( ){ sqlite3_value * const sv = sqlite3_column_value(PtrGet_sqlite3_stmt(jpStmt), (int)ndx); - return new_sqlite3_value_wrapper(env, sv); + return new_java_sqlite3_value(env, sv); } /* -** Impl for both commit hooks (if isCommit is true) or rollback hooks. +** Impl for commit hooks (if isCommit is true) or rollback hooks. */ static int s3jni_commit_rollback_hook_impl(int isCommit, S3JniDb * const ps){ S3JniDeclLocal_env; @@ -2631,8 +2799,7 @@ static int s3jni_commit_rollback_hook_impl(int isCommit, S3JniDb * const ps){ ? (int)(*env)->CallIntMethod(env, hook.jObj, hook.midCallback) : (int)((*env)->CallVoidMethod(env, hook.jObj, hook.midCallback), 0); S3JniIfThrew{ - S3JniExceptionClear; - rc = s3jni_db_error(ps->pDb, SQLITE_ERROR, "hook callback threw."); + rc = s3jni_db_exception(ps->pDb, SQLITE_ERROR, "hook callback threw"); } S3JniHook_localundup(hook); } @@ -2649,17 +2816,21 @@ static void s3jni_rollback_hook_impl(void *pP){ (void)s3jni_commit_rollback_hook_impl(0, pP); } +/* +** Proxy for sqlite3_commit_hook() (if isCommit is true) or +** sqlite3_rollback_hook(). +*/ static jobject s3jni_commit_rollback_hook(int isCommit, JNIEnv * const env, jobject jDb, jobject jHook){ S3JniDb * ps; - jobject pOld = 0; - S3JniHook * pHook; + jobject pOld = 0; /* previous hoook */ + S3JniHook * pHook; /* ps->hooks.commit|rollback */ - S3JniMutex_S3JniDb_enter; - ps = S3JniDb_from_java_unlocked(jDb); + S3JniDb_mutex_enter; + ps = S3JniDb_from_java(jDb); if( !ps ){ s3jni_db_error(ps->pDb, SQLITE_NOMEM, 0); - S3JniMutex_S3JniDb_leave; + S3JniDb_mutex_leave; return 0; } pHook = isCommit ? &ps->hooks.commit : &ps->hooks.rollback; @@ -2685,7 +2856,7 @@ static jobject s3jni_commit_rollback_hook(int isCommit, JNIEnv * const env, S3JniExceptionReport; S3JniExceptionClear; s3jni_db_error(ps->pDb, SQLITE_ERROR, - "Cannot not find matching call() in" + "Cannot not find matching call() method in" "hook object."); }else{ pHook->midCallback = xCallback; @@ -2699,7 +2870,7 @@ static jobject s3jni_commit_rollback_hook(int isCommit, JNIEnv * const env, } } } - S3JniMutex_S3JniDb_leave; + S3JniDb_mutex_leave; return pOld; } @@ -2712,12 +2883,29 @@ S3JniApi(sqlite3_commit_hook(),jobject,1commit_1hook)( S3JniApi(sqlite3_compileoption_get(),jstring,1compileoption_1get)( JniArgsEnvClass, jint n ){ - jstring const rv = (*env)->NewStringUTF( env, sqlite3_compileoption_get(n) ) + const char * z = sqlite3_compileoption_get(n); + jstring const rv = z ? (*env)->NewStringUTF( env, z ) : 0; /* We know these to be ASCII, so MUTF-8 is fine. */; - s3jni_oom_check(rv); + s3jni_oom_check(z ? !!rv : 1); return rv; } +S3JniApi(sqlite3_complete(),int,1complete)( + JniArgsEnvClass, jbyteArray jSql +){ + jbyte * const pBuf = s3jni_jbyteArray_bytes(jSql); + const jsize nBa = pBuf ? (*env)->GetArrayLength(env, jSql) : 0; + int rc; + + assert( (nBa>0 ? 0==pBuf[nBa-1] : (pBuf ? 0==*pBuf : 1)) + && "Byte array is not NUL-terminated." ); + rc = (pBuf && 0==pBuf[(nBa ? nBa-1 : 0)]) + ? sqlite3_complete( (const char *)pBuf ) + : (jSql ? SQLITE_NOMEM : SQLITE_ERROR); + s3jni_jbyteArray_release(jSql, pBuf); + return rc; +} + S3JniApi(sqlite3_compileoption_used(),jboolean,1compileoption_1used)( JniArgsEnvClass, jstring name ){ @@ -2740,6 +2928,60 @@ S3JniApi(sqlite3_config() /*for a small subset of options.*/, return SQLITE_MISUSE; } } +/* C-to-Java SQLITE_CONFIG_LOG wrapper. */ +static void s3jni_config_log(void *ignored, int errCode, const char *z){ + S3JniDeclLocal_env; + S3JniHook hook = S3JniHook_empty; + + S3JniHook_localdup(&SJG.hook.configlog, &hook); + if( hook.jObj ){ + jstring const jArg1 = z ? s3jni_utf8_to_jstring(z, -1) : 0; + if( z ? !!jArg1 : 1 ){ + (*env)->CallVoidMethod(env, hook.jObj, hook.midCallback, errCode, jArg1); + } + S3JniIfThrew{ + S3JniExceptionWarnCallbackThrew("SQLITE_CONFIG_LOG callback"); + S3JniExceptionClear; + } + S3JniHook_localundup(hook); + S3JniUnrefLocal(jArg1); + } +} + +S3JniApi(sqlite3_config() /* for SQLITE_CONFIG_LOG */, + jint, 1config__Lorg_sqlite_jni_ConfigLogCallback_2 +)(JniArgsEnvClass, jobject jLog){ + S3JniHook * const pHook = &SJG.hook.configlog; + int rc = 0; + + S3JniGlobal_mutex_enter; + if( !jLog ){ + rc = sqlite3_config( SQLITE_CONFIG_LOG, NULL, NULL ); + if( 0==rc ){ + S3JniHook_unref(pHook); + } + }else if( pHook->jObj && (*env)->IsSameObject(env, jLog, pHook->jObj) ){ + /* No-op */ + }else { + jclass const klazz = (*env)->GetObjectClass(env, jLog); + jmethodID const midCallback = (*env)->GetMethodID(env, klazz, "call", + "(ILjava/lang/String;)V"); + S3JniUnrefLocal(klazz); + if( midCallback ){ + rc = sqlite3_config( SQLITE_CONFIG_LOG, s3jni_config_log, NULL ); + if( 0==rc ){ + S3JniHook_unref(pHook); + pHook->midCallback = midCallback; + pHook->jObj = S3JniRefGlobal(jLog); + } + }else{ + S3JniExceptionWarnIgnore; + rc = SQLITE_ERROR; + } + } + S3JniGlobal_mutex_leave; + return rc; +} #ifdef SQLITE_ENABLE_SQLLOG /* C-to-Java SQLITE_CONFIG_SQLLOG wrapper. */ @@ -2751,7 +2993,7 @@ static void s3jni_config_sqllog(void *ignored, sqlite3 *pDb, const char *z, int S3JniHook hook = S3JniHook_empty; if( ps ){ - S3JniHook_localdup(&SJG.hooks.sqllog, &hook); + S3JniHook_localdup(&SJG.hook.sqllog, &hook); } if( !hook.jObj ) return; jArg0 = S3JniRefLocal(ps->jDb); @@ -2781,19 +3023,18 @@ void sqlite3_init_sqllog(void){ } #endif -S3JniApi(sqlite3_config() /* for SQLLOG */, +S3JniApi(sqlite3_config() /* for SQLITE_CONFIG_SQLLOG */, jint, 1config__Lorg_sqlite_jni_ConfigSqllogCallback_2)( - JniArgsEnvClass, jobject jLog - ){ + JniArgsEnvClass, jobject jLog){ #ifndef SQLITE_ENABLE_SQLLOG return SQLITE_MISUSE; #else - S3JniHook * const pHook = &SJG.hooks.sqllog; + S3JniHook * const pHook = &SJG.hook.sqllog; int rc = 0; - S3JniMutex_Global_enter; + S3JniGlobal_mutex_enter; if( !jLog ){ - rc = sqlite3_config( SQLITE_CONFIG_SQLLOG, s3jni_config_sqllog, 0 ); + rc = sqlite3_config( SQLITE_CONFIG_SQLLOG, NULL ); if( 0==rc ){ S3JniHook_unref(pHook); } @@ -2807,7 +3048,7 @@ S3JniApi(sqlite3_config() /* for SQLLOG */, "I)V"); S3JniUnrefLocal(klazz); if( midCallback ){ - rc = sqlite3_config( SQLITE_CONFIG_SQLLOG, s3jni_config_sqllog, 0 ); + rc = sqlite3_config( SQLITE_CONFIG_SQLLOG, s3jni_config_sqllog, NULL ); if( 0==rc ){ S3JniHook_unref(pHook); pHook->midCallback = midCallback; @@ -2818,7 +3059,7 @@ S3JniApi(sqlite3_config() /* for SQLLOG */, rc = SQLITE_ERROR; } } - S3JniMutex_Global_leave; + S3JniGlobal_mutex_leave; return rc; #endif } @@ -2832,8 +3073,10 @@ S3JniApi(sqlite3_context_db_handle(),jobject,1context_1db_1handle)( return ps ? ps->jDb : 0; } -/** - State for CollationCallbacks. +/* +** State for CollationCallbacks. This used to be its own separate +** type, but has since been consolidated with S3JniHook. It retains +** its own typedef for code legibility and searchability reasons. */ typedef S3JniHook S3JniCollationCallback; @@ -2878,8 +3121,8 @@ S3JniApi(sqlite3_create_collation() sqlite3_create_collation_v2(), int rc; S3JniDb * ps; - S3JniMutex_S3JniDb_enter; - ps = S3JniDb_from_java_unlocked(jDb); + S3JniDb_mutex_enter; + ps = S3JniDb_from_java(jDb); if( !ps ){ rc = SQLITE_MISUSE; }else{ @@ -2892,7 +3135,7 @@ S3JniApi(sqlite3_create_collation() sqlite3_create_collation_v2(), "Could not get call() method from " "CollationCallback object."); }else{ - char * const zName = s3jni_jstring_to_utf8( name, 0); + char * const zName = s3jni_jstring_to_utf8(name, 0); S3JniCollationCallback * const pCC = zName ? S3JniHook_alloc() : 0; if( pCC ){ @@ -2912,11 +3155,12 @@ S3JniApi(sqlite3_create_collation() sqlite3_create_collation_v2(), sqlite3_free(zName); } } - S3JniMutex_S3JniDb_leave; + S3JniDb_mutex_leave; return (jint)rc; } -S3JniApi(sqlite3_create_function() sqlite3_create_function_v2() sqlite3_create_window_function(), +S3JniApi(sqlite3_create_function() sqlite3_create_function_v2() + sqlite3_create_window_function(), jint,1create_1function )(JniArgsEnvClass, jobject jDb, jstring jFuncName, jint nArg, jint eTextRep, jobject jFunctor){ @@ -2944,6 +3188,7 @@ S3JniApi(sqlite3_create_function() sqlite3_create_function_v2() sqlite3_create_w S3JniUdf_free(env, s, 1); goto error_cleanup; } + s->zFuncName = zFuncName /* pass on ownership */; if( UDF_WINDOW == s->type ){ rc = sqlite3_create_window_function(pDb, zFuncName, nArg, eTextRep, s, udf_xStep, udf_xFinal, udf_xValue, @@ -2965,7 +3210,6 @@ S3JniApi(sqlite3_create_function() sqlite3_create_function_v2() sqlite3_create_w error_cleanup: /* Reminder: on sqlite3_create_function() error, s will be ** destroyed via create_function(). */ - sqlite3_free(zFuncName); return (jint)rc; } @@ -3009,7 +3253,7 @@ S3JniApi(sqlite3_db_config() /*for MAINDBNAME*/, switch( (ps && jStr) ? op : 0 ){ case SQLITE_DBCONFIG_MAINDBNAME: - S3JniMutex_S3JniDb_enter + S3JniDb_mutex_enter /* Protect against a race in modifying/freeing ps->zMainDbName. */; zStr = s3jni_jstring_to_utf8( jStr, 0); @@ -3024,7 +3268,7 @@ S3JniApi(sqlite3_db_config() /*for MAINDBNAME*/, }else{ rc = SQLITE_NOMEM; } - S3JniMutex_S3JniDb_leave; + S3JniDb_mutex_leave; break; default: rc = SQLITE_MISUSE; @@ -3089,6 +3333,12 @@ JniDecl(jint,1db_1config__Lorg_sqlite_jni_sqlite3_2IILorg_sqlite_jni_OutputPoint ); } +S3JniApi(sqlite3_db_release_memory(),int,1db_1release_1memory)( + JniArgsEnvClass, jobject jDb +){ + sqlite3 * const pDb = PtrGet_sqlite3(jDb); + return pDb ? sqlite3_db_release_memory(pDb) : SQLITE_MISUSE; +} S3JniApi(sqlite3_db_status(),jint,1db_1status)( JniArgsEnvClass, jobject jDb, jint op, jobject jOutCurrent, @@ -3159,11 +3409,17 @@ S3JniApi(sqlite3_finalize(),jint,1finalize)( sqlite3_stmt * const pStmt = PtrGet_sqlite3_stmt(jpStmt); if( pStmt ){ rc = sqlite3_finalize(pStmt); - NativePointerHolder_set(&S3JniNphRefs.sqlite3_stmt, jpStmt, 0); + NativePointerHolder_set(S3JniNph(sqlite3_stmt), jpStmt, 0); } return rc; } +S3JniApi(sqlite3_get_auxdata(),jobject,1get_1auxdata)( + JniArgsEnvClass, jobject jCx, jint n +){ + return sqlite3_get_auxdata(PtrGet_sqlite3_context(jCx), (int)n); +} + S3JniApi(sqlite3_initialize(),jint,1initialize)( JniArgsEnvClass ){ @@ -3197,12 +3453,40 @@ S3JniApi(sqlite3_is_interrupted(),jboolean,1is_1interrupted)( */ JniDecl(jboolean,1java_1uncache_1thread)(JniArgsEnvClass){ int rc; - S3JniMutex_Env_enter; + S3JniEnv_mutex_enter; rc = S3JniEnv_uncache(env); - S3JniMutex_Env_leave; + S3JniEnv_mutex_leave; return rc ? JNI_TRUE : JNI_FALSE; } +S3JniApi(sqlite3_keyword_check(),jboolean,1keyword_1check)( + JniArgsEnvClass, jstring jWord +){ + int nWord = 0; + char * zWord = s3jni_jstring_to_utf8(jWord, &nWord); + int rc = 0; + + s3jni_oom_check(jWord ? !!zWord : 1); + if( zWord && nWord ){ + rc = sqlite3_keyword_check(zWord, nWord); + } + sqlite3_free(zWord); + return rc ? JNI_TRUE : JNI_FALSE; +} + +S3JniApi(sqlite3_keyword_name(),jstring,1keyword_1name)( + JniArgsEnvClass, jint ndx +){ + const char * zWord = 0; + int n = 0; + jstring rv = 0; + + if( 0==sqlite3_keyword_name(ndx, &zWord, &n) ){ + rv = s3jni_utf8_to_jstring(zWord, n); + } + return rv; +} + S3JniApi(sqlite3_last_insert_rowid(),jlong,1last_1insert_1rowid)( JniArgsEnvClass, jobject jpDb @@ -3215,6 +3499,17 @@ S3JniApi(sqlite3_last_insert_rowid(),jlong,1last_1insert_1rowid)( return rc; } +S3JniApi(sqlite3_limit(),jint,1limit)( + JniArgsEnvClass, jobject jpDb, jint id, jint newVal +){ + jint rc = 0; + sqlite3 * const pDb = PtrGet_sqlite3(jpDb); + if( pDb ){ + rc = sqlite3_limit( pDb, (int)id, (int)newVal ); + } + return rc; +} + /* Pre-open() code common to sqlite3_open[_v2](). */ static int s3jni_open_pre(JNIEnv * const env, S3JniEnv **jc, jstring jDbName, char **zDbName, @@ -3231,7 +3526,7 @@ static int s3jni_open_pre(JNIEnv * const env, S3JniEnv **jc, rc = SQLITE_NOMEM; goto end; } - jDb = new_sqlite3_wrapper(env, 0); + jDb = new_java_sqlite3(env, 0); if( !jDb ){ sqlite3_free(*zDbName); *zDbName = 0; @@ -3272,19 +3567,20 @@ static int s3jni_open_post(JNIEnv * const env, S3JniEnv * const jc, assert(ps->jDb); if( 0==ps->pDb ){ ps->pDb = *ppDb; - NativePointerHolder_set(&S3JniNphRefs.sqlite3, ps->jDb, *ppDb); + NativePointerHolder_set(S3JniNph(sqlite3), ps->jDb, *ppDb); }else{ assert( ps->pDb==*ppDb && "Set up via s3jni_run_java_auto_extensions()" ); } rc = sqlite3_set_clientdata(ps->pDb, S3JniDb_clientdata_key, - ps, S3JniDb_xDestroy); - /* As of here, the Java/C connection is complete */ + ps, S3JniDb_xDestroy) + /* As of here, the Java/C connection is complete */; }else{ S3JniDb_set_aside(ps); ps = 0; } - OutputPointer_set_sqlite3(env, jOut, ps ? ps->jDb : 0); + OutputPointer_set_obj(env, S3JniNph(OutputPointer_sqlite3), + jOut, ps ? ps->jDb : 0); return theRc ? theRc : rc; } @@ -3342,14 +3638,14 @@ jint sqlite3_jni_prepare_v123( int prepVersion, JNIEnv * const env, jclass self, sqlite3_stmt * pStmt = 0; jobject jStmt = 0; const char * zTail = 0; - jbyte * const pBuf = s3jni_jbytearray_bytes(baSql); + jbyte * const pBuf = s3jni_jbyteArray_bytes(baSql); int rc = SQLITE_ERROR; assert(prepVersion==1 || prepVersion==2 || prepVersion==3); if( !pBuf ){ rc = baSql ? SQLITE_NOMEM : SQLITE_MISUSE; goto end; } - jStmt = new_sqlite3_stmt_wrapper(env, 0); + jStmt = new_java_sqlite3_stmt(env, 0); if( !jStmt ){ rc = SQLITE_NOMEM; goto end; @@ -3369,17 +3665,19 @@ jint sqlite3_jni_prepare_v123( int prepVersion, JNIEnv * const env, jclass self, assert(0 && "Invalid prepare() version"); } end: - s3jni_jbytearray_release(baSql,pBuf); + s3jni_jbyteArray_release(baSql,pBuf); if( 0==rc ){ if( 0!=outTail ){ /* Noting that pBuf is deallocated now but its address is all we need for ** what follows... */ assert(zTail ? ((void*)zTail>=(void*)pBuf) : 1); assert(zTail ? (((int)((void*)zTail - (void*)pBuf)) >= 0) : 1); - OutputPointer_set_Int32(env, outTail, (int)(zTail ? (zTail - (const char *)pBuf) : 0)); + OutputPointer_set_Int32( + env, outTail, (int)(zTail ? (zTail - (const char *)pBuf) : 0) + ); } if( pStmt ){ - NativePointerHolder_set(&S3JniNphRefs.sqlite3_stmt, jStmt, pStmt); + NativePointerHolder_set(S3JniNph(sqlite3_stmt), jStmt, pStmt); }else{ /* Happens for comments and whitespace. */ S3JniUnrefLocal(jStmt); @@ -3389,7 +3687,8 @@ end: S3JniUnrefLocal(jStmt); jStmt = 0; } - OutputPointer_set_sqlite3_stmt(env, jOutStmt, jStmt); + OutputPointer_set_obj(env, S3JniNph(OutputPointer_sqlite3_stmt), + jOutStmt, jStmt); return (jint)rc; } S3JniApi(sqlite3_prepare(),jint,1prepare)( @@ -3462,7 +3761,7 @@ static void s3jni_updatepre_hook_impl(void * pState, sqlite3 *pDb, int opId, (jint)opId, jDbName, jTable, (jlong)iKey1); S3JniIfThrew{ S3JniExceptionWarnCallbackThrew("sqlite3_(pre)update_hook() callback"); - s3jni_db_exception(ps, 0, + s3jni_db_exception(ps->pDb, 0, "sqlite3_(pre)update_hook() callback threw"); } } @@ -3507,7 +3806,7 @@ static jobject s3jni_updatepre_hook(JNIEnv * env, int isPre, jobject jDb, jobjec S3JniHook * pHook; if( !ps ) return 0; - S3JniMutex_S3JniDb_enter; + S3JniDb_mutex_enter; pHook = isPre ? #ifdef SQLITE_ENABLE_PREUPDATE_HOOK &ps->hooks.preUpdate @@ -3567,7 +3866,7 @@ static jobject s3jni_updatepre_hook(JNIEnv * env, int isPre, jobject jDb, jobjec } } end: - S3JniMutex_S3JniDb_leave; + S3JniDb_mutex_leave; return pOld; } @@ -3594,13 +3893,13 @@ static int s3jni_preupdate_newold(JNIEnv * const env, int isNew, jobject jDb, isNew ? sqlite3_preupdate_new : sqlite3_preupdate_old; rc = fOrig(pDb, (int)iCol, &pOut); if( 0==rc ){ - jobject pWrap = new_sqlite3_value_wrapper(env, pOut); - if( pWrap ){ - OutputPointer_set_sqlite3_value(env, jOut, pWrap); - S3JniUnrefLocal(pWrap); - }else{ + jobject pWrap = new_java_sqlite3_value(env, pOut); + if( !pWrap ){ rc = SQLITE_NOMEM; } + OutputPointer_set_obj(env, S3JniNph(OutputPointer_sqlite3_value), + jOut, pWrap); + S3JniUnrefLocal(pWrap); } } return rc; @@ -3633,7 +3932,7 @@ static int s3jni_progress_handler_impl(void *pP){ if( hook.jObj ){ rc = (int)(*env)->CallIntMethod(env, hook.jObj, hook.midCallback); S3JniIfThrew{ - rc = s3jni_db_exception(ps, rc, + rc = s3jni_db_exception(ps->pDb, rc, "sqlite3_progress_handler() callback threw"); } S3JniHook_localundup(hook); @@ -3648,7 +3947,7 @@ S3JniApi(sqlite3_progress_handler(),void,1progress_1handler)( S3JniHook * const pHook = ps ? &ps->hooks.progress : 0; if( !ps ) return; - S3JniMutex_S3JniDb_enter; + S3JniDb_mutex_enter; if( n<1 || !jProgress ){ S3JniHook_unref(pHook); sqlite3_progress_handler(ps->pDb, 0, 0, 0); @@ -3668,9 +3967,21 @@ S3JniApi(sqlite3_progress_handler(),void,1progress_1handler)( sqlite3_progress_handler(ps->pDb, (int)n, s3jni_progress_handler_impl, ps); } } - S3JniMutex_S3JniDb_leave; + S3JniDb_mutex_leave; } +S3JniApi(sqlite3_randomness(),void,1randomness)( + JniArgsEnvClass, jbyteArray jTgt +){ + jbyte * const jba = s3jni_jbyteArray_bytes(jTgt); + if( jba ){ + jsize const nTgt = (*env)->GetArrayLength(env, jTgt); + sqlite3_randomness( (int)nTgt, jba ); + s3jni_jbyteArray_commit(jTgt, jba); + } +} + + S3JniApi(sqlite3_reset(),jint,1reset)( JniArgsEnvClass, jobject jpStmt ){ @@ -3681,12 +3992,12 @@ S3JniApi(sqlite3_reset(),jint,1reset)( /* Clears all entries from S3JniGlobal.autoExt. */ static void s3jni_reset_auto_extension(JNIEnv *env){ int i; - S3JniMutex_Ext_enter; + S3JniAutoExt_mutex_enter; for( i = 0; i < SJG.autoExt.nExt; ++i ){ S3JniAutoExtension_clear( &SJG.autoExt.aExt[i] ); } SJG.autoExt.nExt = 0; - S3JniMutex_Ext_leave; + S3JniAutoExt_mutex_leave; } S3JniApi(sqlite3_reset_auto_extension(),void,1reset_1auto_1extension)( @@ -3702,7 +4013,7 @@ static void result_blob_text(int as64 /* true for text64/blob64() mode */, jbyteArray jBa, jlong nMax){ int const asBlob = 0==eTextRep; if( jBa ){ - jbyte * const pBuf = s3jni_jbytearray_bytes(jBa); + jbyte * const pBuf = s3jni_jbyteArray_bytes(jBa); jsize nBa = (*env)->GetArrayLength(env, jBa); if( nMax>=0 && nBa>(jsize)nMax ){ nBa = (jsize)nMax; @@ -3766,7 +4077,7 @@ static void result_blob_text(int as64 /* true for text64/blob64() mode */, break; } } - s3jni_jbytearray_release(jBa, pBuf); + s3jni_jbyteArray_release(jBa, pBuf); } }else{ sqlite3_result_null(pCx); @@ -3796,7 +4107,7 @@ S3JniApi(sqlite3_result_error(),void,1result_1error)( ){ const char * zUnspecified = "Unspecified error."; jsize const baLen = (*env)->GetArrayLength(env, baMsg); - jbyte * const pjBuf = baMsg ? s3jni_jbytearray_bytes(baMsg) : NULL; + jbyte * const pjBuf = baMsg ? s3jni_jbyteArray_bytes(baMsg) : NULL; switch( pjBuf ? eTextRep : SQLITE_UTF8 ){ case SQLITE_UTF8: { const char *zMsg = pjBuf ? (const char *)pjBuf : zUnspecified; @@ -3815,7 +4126,7 @@ S3JniApi(sqlite3_result_error(),void,1result_1error)( "to sqlite3_result_error().", -1); break; } - s3jni_jbytearray_release(baMsg,pjBuf); + s3jni_jbyteArray_release(baMsg,pjBuf); } S3JniApi(sqlite3_result_error_code(),void,1result_1error_1code)( @@ -3855,7 +4166,7 @@ S3JniApi(sqlite3_result_java_object(),void,1result_1java_1object)( jobject const rjv = S3JniRefGlobal(v); if( rjv ){ sqlite3_result_pointer(PtrGet_sqlite3_context(jpCx), rjv, - ResultJavaValuePtrStr, ResultJavaValue_finalizer); + ResultJavaValuePtrStr, S3Jni_jobject_finalizer); }else{ sqlite3_result_error_nomem(PtrGet_sqlite3_context(jpCx)); } @@ -3929,7 +4240,7 @@ int s3jni_xAuth(void* pState, int op,const char*z0, const char*z1, rc = (*env)->CallIntMethod(env, hook.jObj, hook.midCallback, (jint)op, s0, s1, s3, s3); S3JniIfThrew{ - rc = s3jni_db_exception(ps, rc, "sqlite3_set_authorizer() callback"); + rc = s3jni_db_exception(ps->pDb, rc, "sqlite3_set_authorizer() callback"); } S3JniUnrefLocal(s0); S3JniUnrefLocal(s1); @@ -3948,7 +4259,7 @@ S3JniApi(sqlite3_set_authorizer(),jint,1set_1authorizer)( int rc = 0; if( !ps ) return SQLITE_MISUSE; - S3JniMutex_S3JniDb_enter; + S3JniDb_mutex_enter; if( !jHook ){ S3JniHook_unref(pHook); rc = sqlite3_set_authorizer( ps->pDb, 0, 0 ); @@ -3957,7 +4268,7 @@ S3JniApi(sqlite3_set_authorizer(),jint,1set_1authorizer)( if( pHook->jObj ){ if( (*env)->IsSameObject(env, pHook->jObj, jHook) ){ /* Same object - this is a no-op. */ - S3JniMutex_S3JniDb_leave; + S3JniDb_mutex_leave; return 0; } S3JniHook_unref(pHook); @@ -3981,10 +4292,16 @@ S3JniApi(sqlite3_set_authorizer(),jint,1set_1authorizer)( } if( rc ) S3JniHook_unref(pHook); } - S3JniMutex_S3JniDb_leave; + S3JniDb_mutex_leave; return rc; } +S3JniApi(sqlite3_set_auxdata(),void,1set_1auxdata)( + JniArgsEnvClass, jobject jCx, jint n, jobject jAux +){ + sqlite3_set_auxdata(PtrGet_sqlite3_context(jCx), (int)n, + S3JniRefGlobal(jAux), S3Jni_jobject_finalizer); +} S3JniApi(sqlite3_set_last_insert_rowid(),void,1set_1last_1insert_1rowid)( JniArgsEnvClass, jobject jpDb, jlong rowId @@ -3997,16 +4314,20 @@ S3JniApi(sqlite3_shutdown(),jint,1shutdown)( JniArgsEnvClass ){ s3jni_reset_auto_extension(env); +#ifdef SQLITE_ENABLE_SQLLOG + S3JniHook_unref(&SJG.hook.sqllog); +#endif + S3JniHook_unref(&SJG.hook.configlog); /* Free up S3JniDb recycling bin. */ - S3JniMutex_S3JniDb_enter; { + S3JniDb_mutex_enter; { while( S3JniGlobal.perDb.aFree ){ S3JniDb * const d = S3JniGlobal.perDb.aFree; S3JniGlobal.perDb.aFree = d->pNext; S3JniDb_clear(env, d); sqlite3_free(d); } - } S3JniMutex_S3JniDb_leave; - S3JniMutex_Global_enter; { + } S3JniDb_mutex_leave; + S3JniGlobal_mutex_enter; { /* Free up S3JniUdf recycling bin. */ while( S3JniGlobal.udf.aFree ){ S3JniUdf * const u = S3JniGlobal.udf.aFree; @@ -4014,23 +4335,25 @@ S3JniApi(sqlite3_shutdown(),jint,1shutdown)( u->pNext = 0; S3JniUdf_free(env, u, 0); } + } S3JniGlobal_mutex_leave; + S3JniHook_mutex_enter; { /* Free up S3JniHook recycling bin. */ - while( S3JniGlobal.hooks.aFree ){ - S3JniHook * const u = S3JniGlobal.hooks.aFree; - S3JniGlobal.hooks.aFree = u->pNext; + while( S3JniGlobal.hook.aFree ){ + S3JniHook * const u = S3JniGlobal.hook.aFree; + S3JniGlobal.hook.aFree = u->pNext; u->pNext = 0; assert( !u->doXDestroy ); assert( !u->jObj ); assert( !u->jExtra ); sqlite3_free( u ); } - } S3JniMutex_Global_leave; + } S3JniHook_mutex_leave; /* Free up env cache. */ - S3JniMutex_Env_enter; { + S3JniEnv_mutex_enter; { while( SJG.envCache.aHead ){ S3JniEnv_uncache( SJG.envCache.aHead->env ); } - } S3JniMutex_Env_leave; + } S3JniEnv_mutex_leave; #if 0 /* ** Is automatically closing any still-open dbs a good idea? We will @@ -4038,11 +4361,11 @@ S3JniApi(sqlite3_shutdown(),jint,1shutdown)( ** state, at which point we won't have a central list of databases ** to close. */ - S3JniMutex_S3JniDb_enter; + S3JniDb_mutex_enter; while( SJG.perDb.pHead ){ s3jni_close_db(env, SJG.perDb.pHead->jDb, 2); } - S3JniMutex_S3JniDb_leave; + S3JniDb_mutex_leave; #endif /* Do not clear S3JniGlobal.jvm: it's legal to restart the lib. */ return sqlite3_shutdown(); @@ -4074,11 +4397,19 @@ S3JniApi(sqlite3_status64(),jint,1status64)( return (jint)rc; } +S3JniApi(sqlite3_stmt_status(),jint,1stmt_1status)( + JniArgsEnvClass, jobject jStmt, jint op, jboolean reset +){ + return sqlite3_stmt_status(PtrGet_sqlite3_stmt(jStmt), + (int)op, reset ? 1 : 0); +} + + static int s3jni_strlike_glob(int isLike, JNIEnv *const env, jbyteArray baG, jbyteArray baT, jint escLike){ int rc = 0; - jbyte * const pG = s3jni_jbytearray_bytes(baG); - jbyte * const pT = pG ? s3jni_jbytearray_bytes(baT) : 0; + jbyte * const pG = s3jni_jbyteArray_bytes(baG); + jbyte * const pT = pG ? s3jni_jbyteArray_bytes(baT) : 0; s3jni_oom_fatal(pT); /* Note that we're relying on the byte arrays having been @@ -4087,8 +4418,8 @@ static int s3jni_strlike_glob(int isLike, JNIEnv *const env, ? sqlite3_strlike((const char *)pG, (const char *)pT, (unsigned int)escLike) : sqlite3_strglob((const char *)pG, (const char *)pT); - s3jni_jbytearray_release(baG, pG); - s3jni_jbytearray_release(baT, pT); + s3jni_jbyteArray_release(baG, pG); + s3jni_jbyteArray_release(baT, pT); return rc; } @@ -4128,6 +4459,53 @@ S3JniApi(sqlite3_step(),jint,1step)( return rc; } +S3JniApi(sqlite3_table_column_metadata(),int,1table_1column_1metadata)( + JniArgsEnvClass, jobject jDb, jstring jDbName, jstring jTableName, + jstring jColumnName, jobject jDataType, jobject jCollSeq, jobject jNotNull, + jobject jPrimaryKey, jobject jAutoinc +){ + sqlite3 * const db = PtrGet_sqlite3(jDb); + char * zDbName = 0, * zTableName = 0, * zColumnName = 0; + const char * pzCollSeq = 0; + const char * pzDataType = 0; + int pNotNull = 0, pPrimaryKey = 0, pAutoinc = 0; + int rc; + + if( !db || !jDbName || !jTableName || !jColumnName ) return SQLITE_MISUSE; + zDbName = s3jni_jstring_to_utf8(jDbName,0); + zTableName = zDbName ? s3jni_jstring_to_utf8(jTableName,0) : 0; + zColumnName = zTableName ? s3jni_jstring_to_utf8(jColumnName,0) : 0; + rc = zColumnName + ? sqlite3_table_column_metadata(db, zDbName, zTableName, + zColumnName, &pzDataType, &pzCollSeq, + &pNotNull, &pPrimaryKey, &pAutoinc) + : SQLITE_NOMEM; + if( 0==rc ){ + jstring jseq = jCollSeq + ? (pzCollSeq ? s3jni_utf8_to_jstring(pzCollSeq, -1) : 0) + : 0; + jstring jdtype = jDataType + ? (pzDataType ? s3jni_utf8_to_jstring(pzDataType, -1) : 0) + : 0; + if( (jCollSeq && pzCollSeq && !jseq) + || (jDataType && pzDataType && !jdtype) ){ + rc = SQLITE_NOMEM; + }else{ + if( jNotNull ) OutputPointer_set_Bool(env, jNotNull, pNotNull); + if( jPrimaryKey ) OutputPointer_set_Bool(env, jPrimaryKey, pPrimaryKey); + if( jAutoinc ) OutputPointer_set_Bool(env, jAutoinc, pAutoinc); + if( jCollSeq ) OutputPointer_set_String(env, jCollSeq, jseq); + if( jDataType ) OutputPointer_set_String(env, jDataType, jdtype); + } + S3JniUnrefLocal(jseq); + S3JniUnrefLocal(jdtype); + } + sqlite3_free(zDbName); + sqlite3_free(zTableName); + sqlite3_free(zColumnName); + return rc; +} + static int s3jni_trace_impl(unsigned traceflag, void *pC, void *pP, void *pX){ S3JniDb * const ps = (S3JniDb *)pC; S3JniDeclLocal_env; @@ -4157,16 +4535,16 @@ static int s3jni_trace_impl(unsigned traceflag, void *pC, void *pP, void *pX){ case SQLITE_TRACE_ROW: break; case SQLITE_TRACE_CLOSE: - jP = ps->jDb; + jP = jPUnref = S3JniRefLocal(ps->jDb); break; default: - assert(!"cannot happen - unkown trace flag"); + assert(!"cannot happen - unknown trace flag"); rc = SQLITE_ERROR; } if( 0==rc ){ if( !jP ){ /* Create a new temporary sqlite3_stmt wrapper */ - jP = jPUnref = new_sqlite3_stmt_wrapper(env, pP); + jP = jPUnref = new_java_sqlite3_stmt(env, pP); if( !jP ){ rc = SQLITE_NOMEM; } @@ -4176,7 +4554,7 @@ static int s3jni_trace_impl(unsigned traceflag, void *pC, void *pP, void *pX){ rc = (int)(*env)->CallIntMethod(env, hook.jObj, hook.midCallback, (jint)traceflag, jP, jX); S3JniIfThrew{ - rc = s3jni_db_exception(ps, SQLITE_ERROR, + rc = s3jni_db_exception(ps->pDb, SQLITE_ERROR, "sqlite3_trace_v2() callback threw."); } } @@ -4195,10 +4573,10 @@ S3JniApi(sqlite3_trace_v2(),jint,1trace_1v2)( if( !ps ) return SQLITE_MISUSE; if( !traceMask || !jTracer ){ - S3JniMutex_S3JniDb_enter; + S3JniDb_mutex_enter; rc = (jint)sqlite3_trace_v2(ps->pDb, 0, 0, 0); S3JniHook_unref(&ps->hooks.trace); - S3JniMutex_S3JniDb_leave; + S3JniDb_mutex_leave; }else{ jclass const klazz = (*env)->GetObjectClass(env, jTracer); S3JniHook hook = S3JniHook_empty; @@ -4213,15 +4591,34 @@ S3JniApi(sqlite3_trace_v2(),jint,1trace_1v2)( "TracerCallback object."); }else{ hook.jObj = S3JniRefGlobal(jTracer); - S3JniMutex_S3JniDb_enter; + S3JniDb_mutex_enter; rc = sqlite3_trace_v2(ps->pDb, (unsigned)traceMask, s3jni_trace_impl, ps); if( 0==rc ){ S3JniHook_unref(&ps->hooks.trace); - ps->hooks.trace = hook; + ps->hooks.trace = hook /* transfer ownership of reference */; }else{ S3JniHook_unref(&hook); } - S3JniMutex_S3JniDb_leave; + S3JniDb_mutex_leave; + } + } + return rc; +} + +S3JniApi(sqlite3_txn_state(),jint,1txn_1state)( + JniArgsEnvClass,jobject jDb, jstring jSchema +){ + sqlite3 * const pDb = PtrGet_sqlite3(jDb); + int rc = SQLITE_MISUSE; + if( pDb ){ + char * zSchema = jSchema + ? s3jni_jstring_to_utf8(jSchema, 0) + : 0; + if( !jSchema || (zSchema && jSchema) ){ + rc = sqlite3_txn_state(pDb, zSchema); + sqlite3_free(zSchema); + }else{ + rc = SQLITE_NOMEM; } } return rc; @@ -4259,7 +4656,7 @@ S3JniApi(sqlite3_value_dup(),jobject,1value_1dup)( JniArgsEnvClass, jobject jpSVal ){ sqlite3_value * const sv = sqlite3_value_dup(PtrGet_sqlite3_value(jpSVal)); - return sv ? new_sqlite3_value_wrapper(env, sv) : 0; + return sv ? new_java_sqlite3_value(env, sv) : 0; } S3JniApi(sqlite3_value_free(),void,1value_1free)( @@ -4287,7 +4684,7 @@ S3JniApi(sqlite3_value_java_object(),jobject,1value_1java_1object)( ResultJavaValuePtrStr); } -S3JniApi(sqlite3_value_text_utf8(),jbyteArray,1value_1text_1utf8)( +S3JniApi(sqlite3_value_text(),jbyteArray,1value_1text)( JniArgsEnvClass, jobject jpSVal ){ sqlite3_value * const sv = PtrGet_sqlite3_value(jpSVal); @@ -4296,6 +4693,8 @@ S3JniApi(sqlite3_value_text_utf8(),jbyteArray,1value_1text_1utf8)( return p ? s3jni_new_jbyteArray(p, n) : 0; } +#if 0 +// this impl might prove useful. S3JniApi(sqlite3_value_text(),jstring,1value_1text)( JniArgsEnvClass, jobject jpSVal ){ @@ -4304,6 +4703,7 @@ S3JniApi(sqlite3_value_text(),jstring,1value_1text)( const unsigned char * const p = sqlite3_value_text(sv); return p ? s3jni_utf8_to_jstring( (const char *)p, n) : 0; } +#endif S3JniApi(sqlite3_value_text16(),jstring,1value_1text16)( JniArgsEnvClass, jobject jpSVal @@ -4332,10 +4732,12 @@ JniDecl(void,1jni_1internal_1details)(JniArgsEnvClass){ SO(S3JniEnv); SO(S3JniHook); SO(S3JniDb); - SO(S3JniNphRefs); - printf("\t(^^^ %u NativePointerHolder subclasses)\n", + SO(S3JniNphOps); + printf("\t(^^^ %u NativePointerHolder/OutputPointer.T types)\n", (unsigned)S3Jni_NphCache_size); SO(S3JniGlobal); + SO(S3JniGlobal.nph); + SO(S3JniGlobal.metrics); SO(S3JniAutoExtension); SO(S3JniUdf); #undef SO @@ -4347,15 +4749,16 @@ JniDecl(void,1jni_1internal_1details)(JniArgsEnvClass){ printf("Mutex entry:" "\n\tglobal = %u" "\n\tenv = %u" - "\n\tnph = %u for S3JniNphClass init" + "\n\tnph = %u for S3JniNphOp init" + "\n\thook = %u" "\n\tperDb = %u" "\n\tautoExt list = %u" "\n\tS3JniUdf = %u (free-list)" "\n\tmetrics = %u\n", SJG.metrics.nMutexGlobal, SJG.metrics.nMutexEnv, - SJG.metrics.nMutexEnv2, SJG.metrics.nMutexPerDb, - SJG.metrics.nMutexAutoExt, SJG.metrics.nMutexUdf, - SJG.metrics.nMetrics); + SJG.metrics.nMutexNph, SJG.metrics.nMutexHook, + SJG.metrics.nMutexPerDb, SJG.metrics.nMutexAutoExt, + SJG.metrics.nMutexUdf, SJG.metrics.nMetrics); puts("Allocs:"); printf("\tS3JniDb: %u alloced (*%u = %u bytes), %u recycled\n", SJG.metrics.nPdbAlloc, (unsigned) sizeof(S3JniDb), @@ -4390,11 +4793,11 @@ JniDecl(void,1jni_1internal_1details)(JniArgsEnvClass){ /* Creates a verbose JNI Fts5 function name. */ #define JniFuncNameFtsXA(Suffix) \ - Java_org_sqlite_jni_Fts5ExtensionApi_ ## Suffix + Java_org_sqlite_jni_fts5_Fts5ExtensionApi_ ## Suffix #define JniFuncNameFtsApi(Suffix) \ - Java_org_sqlite_jni_fts5_1api_ ## Suffix + Java_org_sqlite_jni_fts5_fts5_1api_ ## Suffix #define JniFuncNameFtsTok(Suffix) \ - Java_org_sqlite_jni_fts5_tokenizer_ ## Suffix + Java_org_sqlite_jni_fts5_fts5_tokenizer_ ## Suffix #define JniDeclFtsXA(ReturnType,Suffix) \ JNIEXPORT ReturnType JNICALL \ @@ -4406,11 +4809,12 @@ JniDecl(void,1jni_1internal_1details)(JniArgsEnvClass){ JNIEXPORT ReturnType JNICALL \ JniFuncNameFtsTok(Suffix) -#define PtrGet_fts5_api(OBJ) NativePointerHolder_get(OBJ,&S3JniNphRefs.fts5_api) -#define PtrGet_fts5_tokenizer(OBJ) NativePointerHolder_get(OBJ,&S3JniNphRefs.fts5_tokenizer) -#define PtrGet_Fts5Context(OBJ) NativePointerHolder_get(OBJ,&S3JniNphRefs.Fts5Context) -#define PtrGet_Fts5Tokenizer(OBJ) NativePointerHolder_get(OBJ,&S3JniNphRefs.Fts5Tokenizer) -#define Fts5ExtDecl Fts5ExtensionApi const * const fext = s3jni_ftsext() +#define PtrGet_fts5_api(OBJ) NativePointerHolder_get(OBJ,S3JniNph(fts5_api)) +#define PtrGet_fts5_tokenizer(OBJ) NativePointerHolder_get(OBJ,S3JniNph(fts5_tokenizer)) +#define PtrGet_Fts5Context(OBJ) NativePointerHolder_get(OBJ,S3JniNph(Fts5Context)) +#define PtrGet_Fts5Tokenizer(OBJ) NativePointerHolder_get(OBJ,S3JniNph(Fts5Tokenizer)) +#define s3jni_ftsext() &sFts5Api/*singleton from sqlite3.c*/ +#define Fts5ExtDecl Fts5ExtensionApi const * const ext = s3jni_ftsext() /** State for binding Java-side FTS5 auxiliary functions. @@ -4450,9 +4854,9 @@ static Fts5JniAux * Fts5JniAux_alloc(JNIEnv * const env, jobject jObj){ memset(s, 0, sizeof(Fts5JniAux)); s->jObj = S3JniRefGlobal(jObj); klazz = (*env)->GetObjectClass(env, jObj); - s->jmid = (*env)->GetMethodID(env, klazz, "xFunction", - "(Lorg/sqlite/jni/Fts5ExtensionApi;" - "Lorg/sqlite/jni/Fts5Context;" + s->jmid = (*env)->GetMethodID(env, klazz, "call", + "(Lorg/sqlite/jni/fts5/Fts5ExtensionApi;" + "Lorg/sqlite/jni/fts5/Fts5Context;" "Lorg/sqlite/jni/sqlite3_context;" "[Lorg/sqlite/jni/sqlite3_value;)V"); S3JniUnrefLocal(klazz); @@ -4466,15 +4870,11 @@ static Fts5JniAux * Fts5JniAux_alloc(JNIEnv * const env, jobject jObj){ return s; } -static inline Fts5ExtensionApi const * s3jni_ftsext(void){ - return &sFts5Api/*singleton from sqlite3.c*/; +static inline jobject new_java_Fts5Context(JNIEnv * const env, Fts5Context *sv){ + return NativePointerHolder_new(env, S3JniNph(Fts5Context), sv); } - -static inline jobject new_Fts5Context_wrapper(JNIEnv * const env, Fts5Context *sv){ - return new_NativePointerHolder_object(env, &S3JniNphRefs.Fts5Context, sv); -} -static inline jobject new_fts5_api_wrapper(JNIEnv * const env, fts5_api *sv){ - return new_NativePointerHolder_object(env, &S3JniNphRefs.fts5_api, sv); +static inline jobject new_java_fts5_api(JNIEnv * const env, fts5_api *sv){ + return NativePointerHolder_new(env, S3JniNph(fts5_api), sv); } /* @@ -4482,20 +4882,20 @@ static inline jobject new_fts5_api_wrapper(JNIEnv * const env, fts5_api *sv){ ** instance, or NULL on OOM. */ static jobject s3jni_getFts5ExensionApi(JNIEnv * const env){ - if( !SJG.fts5.jFtsExt ){ - jobject pNPH = new_NativePointerHolder_object( - env, &S3JniNphRefs.Fts5ExtensionApi, s3jni_ftsext() - ); - S3JniMutex_Env_enter; - if( pNPH ){ - if( !SJG.fts5.jFtsExt ){ - SJG.fts5.jFtsExt = S3JniRefGlobal(pNPH); + if( !SJG.fts5.jExt ){ + S3JniGlobal_mutex_enter; + if( !SJG.fts5.jExt ){ + jobject const pNPH = NativePointerHolder_new( + env, S3JniNph(Fts5ExtensionApi), s3jni_ftsext() + ); + if( pNPH ){ + SJG.fts5.jExt = S3JniRefGlobal(pNPH); + S3JniUnrefLocal(pNPH); } - S3JniUnrefLocal(pNPH); } - S3JniMutex_Env_leave; + S3JniGlobal_mutex_leave; } - return SJG.fts5.jFtsExt; + return SJG.fts5.jExt; } /* @@ -4516,18 +4916,33 @@ static fts5_api *s3jni_fts5_api_from_db(sqlite3 *db){ JniDeclFtsApi(jobject,getInstanceForDb)(JniArgsEnvClass,jobject jDb){ S3JniDb * const ps = S3JniDb_from_java(jDb); +#if 0 jobject rv = 0; if( !ps ) return 0; - else if( ps->jFtsApi ){ - rv = ps->jFtsApi; + else if( ps->fts.jApi ){ + rv = ps->fts.jApi; }else{ fts5_api * const pApi = s3jni_fts5_api_from_db(ps->pDb); if( pApi ){ - rv = new_fts5_api_wrapper(env, pApi); - ps->jFtsApi = rv ? S3JniRefGlobal(rv) : 0; + rv = new_java_fts5_api(env, pApi); + ps->fts.jApi = rv ? S3JniRefGlobal(rv) : 0; } } return rv; +#else + if( ps && !ps->fts.jApi ){ + S3JniDb_mutex_enter; + if( !ps->fts.jApi ){ + fts5_api * const pApi = s3jni_fts5_api_from_db(ps->pDb); + if( pApi ){ + jobject const rv = new_java_fts5_api(env, pApi); + ps->fts.jApi = rv ? S3JniRefGlobal(rv) : 0; + } + } + S3JniDb_mutex_leave; + } + return ps ? ps->fts.jApi : 0; +#endif } @@ -4537,13 +4952,13 @@ JniDeclFtsXA(jobject,getInstance)(JniArgsEnvClass){ JniDeclFtsXA(jint,xColumnCount)(JniArgsEnvObj,jobject jCtx){ Fts5ExtDecl; - return (jint)fext->xColumnCount(PtrGet_Fts5Context(jCtx)); + return (jint)ext->xColumnCount(PtrGet_Fts5Context(jCtx)); } JniDeclFtsXA(jint,xColumnSize)(JniArgsEnvObj,jobject jCtx, jint iIdx, jobject jOut32){ Fts5ExtDecl; int n1 = 0; - int const rc = fext->xColumnSize(PtrGet_Fts5Context(jCtx), (int)iIdx, &n1); + int const rc = ext->xColumnSize(PtrGet_Fts5Context(jCtx), (int)iIdx, &n1); if( 0==rc ) OutputPointer_set_Int32(env, jOut32, n1); return rc; } @@ -4553,7 +4968,7 @@ JniDeclFtsXA(jint,xColumnText)(JniArgsEnvObj,jobject jCtx, jint iCol, Fts5ExtDecl; const char *pz = 0; int pn = 0; - int rc = fext->xColumnText(PtrGet_Fts5Context(jCtx), (int)iCol, + int rc = ext->xColumnText(PtrGet_Fts5Context(jCtx), (int)iCol, &pz, &pn); if( 0==rc ){ jstring jstr = pz ? s3jni_utf8_to_jstring( pz, pn) : 0; @@ -4572,7 +4987,7 @@ JniDeclFtsXA(jint,xColumnText)(JniArgsEnvObj,jobject jCtx, jint iCol, JniDeclFtsXA(jint,xColumnTotalSize)(JniArgsEnvObj,jobject jCtx, jint iCol, jobject jOut64){ Fts5ExtDecl; sqlite3_int64 nOut = 0; - int const rc = fext->xColumnTotalSize(PtrGet_Fts5Context(jCtx), (int)iCol, &nOut); + int const rc = ext->xColumnTotalSize(PtrGet_Fts5Context(jCtx), (int)iCol, &nOut); if( 0==rc && jOut64 ) OutputPointer_set_Int64(env, jOut64, (jlong)nOut); return (jint)rc; } @@ -4597,20 +5012,21 @@ static void s3jni_fts5_extension_function(Fts5ExtensionApi const *pApi, assert(pAux); jFXA = s3jni_getFts5ExensionApi(env); if( !jFXA ) goto error_oom; - jpFts = new_Fts5Context_wrapper(env, pFts); + jpFts = new_java_Fts5Context(env, pFts); if( !jpFts ) goto error_oom; rc = udf_args(env, pCx, argc, argv, &jpCx, &jArgv); if( rc ) goto error_oom; (*env)->CallVoidMethod(env, pAux->jObj, pAux->jmid, jFXA, jpFts, jpCx, jArgv); S3JniIfThrew{ - udf_report_exception(env, 1, pCx, pAux->zFuncName, "xFunction"); + udf_report_exception(env, 1, pCx, pAux->zFuncName, "call"); } S3JniUnrefLocal(jpFts); S3JniUnrefLocal(jpCx); S3JniUnrefLocal(jArgv); return; error_oom: + s3jni_db_oom( sqlite3_context_db_handle(pCx) ); assert( !jArgv ); assert( !jpCx ); S3JniUnrefLocal(jpFts); @@ -4670,7 +5086,7 @@ static void S3JniFts5AuxData_xDestroy(void *x){ JniDeclFtsXA(jobject,xGetAuxdata)(JniArgsEnvObj,jobject jCtx, jboolean bClear){ Fts5ExtDecl; jobject rv = 0; - S3JniFts5AuxData * const pAux = fext->xGetAuxdata(PtrGet_Fts5Context(jCtx), bClear); + S3JniFts5AuxData * const pAux = ext->xGetAuxdata(PtrGet_Fts5Context(jCtx), bClear); if( pAux ){ if( bClear ){ if( pAux->jObj ){ @@ -4690,7 +5106,7 @@ JniDeclFtsXA(jint,xInst)(JniArgsEnvObj,jobject jCtx, jint iIdx, jobject jOutPhra jobject jOutCol, jobject jOutOff){ Fts5ExtDecl; int n1 = 0, n2 = 2, n3 = 0; - int const rc = fext->xInst(PtrGet_Fts5Context(jCtx), (int)iIdx, &n1, &n2, &n3); + int const rc = ext->xInst(PtrGet_Fts5Context(jCtx), (int)iIdx, &n1, &n2, &n3); if( 0==rc ){ OutputPointer_set_Int32(env, jOutPhrase, n1); OutputPointer_set_Int32(env, jOutCol, n2); @@ -4702,14 +5118,14 @@ JniDeclFtsXA(jint,xInst)(JniArgsEnvObj,jobject jCtx, jint iIdx, jobject jOutPhra JniDeclFtsXA(jint,xInstCount)(JniArgsEnvObj,jobject jCtx, jobject jOut32){ Fts5ExtDecl; int nOut = 0; - int const rc = fext->xInstCount(PtrGet_Fts5Context(jCtx), &nOut); + int const rc = ext->xInstCount(PtrGet_Fts5Context(jCtx), &nOut); if( 0==rc && jOut32 ) OutputPointer_set_Int32(env, jOut32, nOut); return (jint)rc; } JniDeclFtsXA(jint,xPhraseCount)(JniArgsEnvObj,jobject jCtx){ Fts5ExtDecl; - return (jint)fext->xPhraseCount(PtrGet_Fts5Context(jCtx)); + return (jint)ext->xPhraseCount(PtrGet_Fts5Context(jCtx)); } /* Copy the 'a' and 'b' fields from pSrc to Fts5PhraseIter object jIter. */ @@ -4743,7 +5159,7 @@ JniDeclFtsXA(jint,xPhraseFirst)(JniArgsEnvObj,jobject jCtx, jint iPhrase, Fts5ExtDecl; Fts5PhraseIter iter; int rc, iCol = 0, iOff = 0; - rc = fext->xPhraseFirst(PtrGet_Fts5Context(jCtx), (int)iPhrase, + rc = ext->xPhraseFirst(PtrGet_Fts5Context(jCtx), (int)iPhrase, &iter, &iCol, &iOff); if( 0==rc ){ OutputPointer_set_Int32(env, jOutCol, iCol); @@ -4758,7 +5174,7 @@ JniDeclFtsXA(jint,xPhraseFirstColumn)(JniArgsEnvObj,jobject jCtx, jint iPhrase, Fts5ExtDecl; Fts5PhraseIter iter; int rc, iCol = 0; - rc = fext->xPhraseFirstColumn(PtrGet_Fts5Context(jCtx), (int)iPhrase, + rc = ext->xPhraseFirstColumn(PtrGet_Fts5Context(jCtx), (int)iPhrase, &iter, &iCol); if( 0==rc ){ OutputPointer_set_Int32(env, jOutCol, iCol); @@ -4773,7 +5189,7 @@ JniDeclFtsXA(void,xPhraseNext)(JniArgsEnvObj,jobject jCtx, jobject jIter, Fts5PhraseIter iter; int iCol = 0, iOff = 0; s3jni_phraseIter_JToN(env, jIter, &iter); - fext->xPhraseNext(PtrGet_Fts5Context(jCtx), &iter, &iCol, &iOff); + ext->xPhraseNext(PtrGet_Fts5Context(jCtx), &iter, &iCol, &iOff); OutputPointer_set_Int32(env, jOutCol, iCol); OutputPointer_set_Int32(env, jOutOff, iOff); s3jni_phraseIter_NToJ(env, &iter, jIter); @@ -4785,7 +5201,7 @@ JniDeclFtsXA(void,xPhraseNextColumn)(JniArgsEnvObj,jobject jCtx, jobject jIter, Fts5PhraseIter iter; int iCol = 0; s3jni_phraseIter_JToN(env, jIter, &iter); - fext->xPhraseNextColumn(PtrGet_Fts5Context(jCtx), &iter, &iCol); + ext->xPhraseNextColumn(PtrGet_Fts5Context(jCtx), &iter, &iCol); OutputPointer_set_Int32(env, jOutCol, iCol); s3jni_phraseIter_NToJ(env, &iter, jIter); } @@ -4793,12 +5209,12 @@ JniDeclFtsXA(void,xPhraseNextColumn)(JniArgsEnvObj,jobject jCtx, jobject jIter, JniDeclFtsXA(jint,xPhraseSize)(JniArgsEnvObj,jobject jCtx, jint iPhrase){ Fts5ExtDecl; - return (jint)fext->xPhraseSize(PtrGet_Fts5Context(jCtx), (int)iPhrase); + return (jint)ext->xPhraseSize(PtrGet_Fts5Context(jCtx), (int)iPhrase); } /* State for use with xQueryPhrase() and xTokenize(). */ struct s3jni_xQueryPhraseState { - Fts5ExtensionApi const * fext; + Fts5ExtensionApi const * ext; S3JniEnv const * jc; jmethodID midCallback; jobject jCallback; @@ -4819,7 +5235,7 @@ static int s3jni_xQueryPhrase(const Fts5ExtensionApi *xapi, struct s3jni_xQueryPhraseState const * s = pData; S3JniDeclLocal_env; int rc = (int)(*env)->CallIntMethod(env, s->jCallback, s->midCallback, - SJG.fts5.jFtsExt, s->jFcx); + SJG.fts5.jExt, s->jFcx); S3JniIfThrew{ S3JniExceptionWarnCallbackThrew("xQueryPhrase() callback"); S3JniExceptionClear; @@ -4839,13 +5255,13 @@ JniDeclFtsXA(jint,xQueryPhrase)(JniArgsEnvObj,jobject jFcx, jint iPhrase, s.jc = jc; s.jCallback = jCallback; s.jFcx = jFcx; - s.fext = fext; - s.midCallback = (*env)->GetMethodID(env, klazz, "xCallback", + s.ext = ext; + s.midCallback = (*env)->GetMethodID(env, klazz, "call", "(Lorg.sqlite.jni.Fts5ExtensionApi;" "Lorg.sqlite.jni.Fts5Context;)I"); S3JniUnrefLocal(klazz); - S3JniExceptionIsFatal("Could not extract xQueryPhraseCallback.xCallback method."); - return (jint)fext->xQueryPhrase(PtrGet_Fts5Context(jFcx), iPhrase, &s, + S3JniExceptionIsFatal("Could not extract xQueryPhraseCallback.call() method."); + return (jint)ext->xQueryPhrase(PtrGet_Fts5Context(jFcx), iPhrase, &s, s3jni_xQueryPhrase); } @@ -4853,14 +5269,14 @@ JniDeclFtsXA(jint,xQueryPhrase)(JniArgsEnvObj,jobject jFcx, jint iPhrase, JniDeclFtsXA(jint,xRowCount)(JniArgsEnvObj,jobject jCtx, jobject jOut64){ Fts5ExtDecl; sqlite3_int64 nOut = 0; - int const rc = fext->xRowCount(PtrGet_Fts5Context(jCtx), &nOut); + int const rc = ext->xRowCount(PtrGet_Fts5Context(jCtx), &nOut); if( 0==rc && jOut64 ) OutputPointer_set_Int64(env, jOut64, (jlong)nOut); return (jint)rc; } JniDeclFtsXA(jlong,xRowid)(JniArgsEnvObj,jobject jCtx){ Fts5ExtDecl; - return (jlong)fext->xRowid(PtrGet_Fts5Context(jCtx)); + return (jlong)ext->xRowid(PtrGet_Fts5Context(jCtx)); } JniDeclFtsXA(int,xSetAuxdata)(JniArgsEnvObj,jobject jCtx, jobject jAux){ @@ -4878,7 +5294,7 @@ JniDeclFtsXA(int,xSetAuxdata)(JniArgsEnvObj,jobject jCtx, jobject jAux){ return SQLITE_NOMEM; } pAux->jObj = S3JniRefGlobal(jAux); - rc = fext->xSetAuxdata(PtrGet_Fts5Context(jCtx), pAux, + rc = ext->xSetAuxdata(PtrGet_Fts5Context(jCtx), pAux, S3JniFts5AuxData_xDestroy); return rc; } @@ -4911,14 +5327,14 @@ static int s3jni_xTokenize_xToken(void *p, int tFlags, const char* z, ** Proxy for Fts5ExtensionApi.xTokenize() and ** fts5_tokenizer.xTokenize() */ -static jint s3jni_fts5_xTokenize(JniArgsEnvObj, S3JniNphRef const *pRef, +static jint s3jni_fts5_xTokenize(JniArgsEnvObj, S3JniNphOp const *pRef, jint tokFlags, jobject jFcx, jbyteArray jbaText, jobject jCallback){ Fts5ExtDecl; S3JniEnv * const jc = S3JniEnv_get(); struct s3jni_xQueryPhraseState s; int rc = 0; - jbyte * const pText = jCallback ? s3jni_jbytearray_bytes(jbaText) : 0; + jbyte * const pText = jCallback ? s3jni_jbyteArray_bytes(jbaText) : 0; jsize nText = pText ? (*env)->GetArrayLength(env, jbaText) : 0; jclass const klazz = jCallback ? (*env)->GetObjectClass(env, jCallback) : NULL; @@ -4927,23 +5343,23 @@ static jint s3jni_fts5_xTokenize(JniArgsEnvObj, S3JniNphRef const *pRef, s.jc = jc; s.jCallback = jCallback; s.jFcx = jFcx; - s.fext = fext; + s.ext = ext; s.midCallback = (*env)->GetMethodID(env, klazz, "call", "(I[BII)I"); S3JniUnrefLocal(klazz); S3JniIfThrew { S3JniExceptionReport; S3JniExceptionClear; - s3jni_jbytearray_release(jbaText, pText); + s3jni_jbyteArray_release(jbaText, pText); return SQLITE_ERROR; } s.tok.jba = S3JniRefLocal(jbaText); s.tok.zPrev = (const char *)pText; s.tok.nPrev = (int)nText; - if( pRef == &S3JniNphRefs.Fts5ExtensionApi ){ - rc = fext->xTokenize(PtrGet_Fts5Context(jFcx), + if( pRef == S3JniNph(Fts5ExtensionApi) ){ + rc = ext->xTokenize(PtrGet_Fts5Context(jFcx), (const char *)pText, (int)nText, &s, s3jni_xTokenize_xToken); - }else if( pRef == &S3JniNphRefs.fts5_tokenizer ){ + }else if( pRef == S3JniNph(fts5_tokenizer) ){ fts5_tokenizer * const pTok = PtrGet_fts5_tokenizer(jSelf); rc = pTok->xTokenize(PtrGet_Fts5Tokenizer(jFcx), &s, tokFlags, (const char *)pText, (int)nText, @@ -4955,26 +5371,26 @@ static jint s3jni_fts5_xTokenize(JniArgsEnvObj, S3JniNphRef const *pRef, assert( s.tok.zPrev ); S3JniUnrefLocal(s.tok.jba); } - s3jni_jbytearray_release(jbaText, pText); + s3jni_jbyteArray_release(jbaText, pText); return (jint)rc; } JniDeclFtsXA(jint,xTokenize)(JniArgsEnvObj,jobject jFcx, jbyteArray jbaText, jobject jCallback){ - return s3jni_fts5_xTokenize(env, jSelf, &S3JniNphRefs.Fts5ExtensionApi, + return s3jni_fts5_xTokenize(env, jSelf, S3JniNph(Fts5ExtensionApi), 0, jFcx, jbaText, jCallback); } JniDeclFtsTok(jint,xTokenize)(JniArgsEnvObj,jobject jFcx, jint tokFlags, jbyteArray jbaText, jobject jCallback){ - return s3jni_fts5_xTokenize(env, jSelf, &S3JniNphRefs.Fts5Tokenizer, + return s3jni_fts5_xTokenize(env, jSelf, S3JniNph(Fts5Tokenizer), tokFlags, jFcx, jbaText, jCallback); } JniDeclFtsXA(jobject,xUserData)(JniArgsEnvObj,jobject jFcx){ Fts5ExtDecl; - Fts5JniAux * const pAux = fext->xUserData(PtrGet_Fts5Context(jFcx)); + Fts5JniAux * const pAux = ext->xUserData(PtrGet_Fts5Context(jFcx)); return pAux ? pAux->jUserData : 0; } @@ -5154,15 +5570,15 @@ Java_org_sqlite_jni_tester_SQLTester_strglob( JniArgsEnvClass, jbyteArray baG, jbyteArray baT ){ int rc = 0; - jbyte * const pG = s3jni_jbytearray_bytes(baG); - jbyte * const pT = pG ? s3jni_jbytearray_bytes(baT) : 0; + jbyte * const pG = s3jni_jbyteArray_bytes(baG); + jbyte * const pT = pG ? s3jni_jbyteArray_bytes(baT) : 0; s3jni_oom_fatal(pT); /* Note that we're relying on the byte arrays having been NUL-terminated on the Java side. */ rc = !SQLTester_strnotglob((const char *)pG, (const char *)pT); - s3jni_jbytearray_release(baG, pG); - s3jni_jbytearray_release(baT, pT); + s3jni_jbyteArray_release(baG, pG); + s3jni_jbyteArray_release(baT, pT); return rc; } @@ -5195,54 +5611,8 @@ Java_org_sqlite_jni_tester_SQLTester_installCustomExtensions(JniArgsEnvClass){ */ JNIEXPORT void JNICALL Java_org_sqlite_jni_SQLite3Jni_init(JniArgsEnvClass){ - enum JType { - JTYPE_INT, - JTYPE_BOOL - }; - typedef struct { - const char *zName; - enum JType jtype; - int value; - } ConfigFlagEntry; - const ConfigFlagEntry aLimits[] = { - {"SQLITE_MAX_ALLOCATION_SIZE", JTYPE_INT, SQLITE_MAX_ALLOCATION_SIZE}, - {"SQLITE_LIMIT_LENGTH", JTYPE_INT, SQLITE_LIMIT_LENGTH}, - {"SQLITE_MAX_LENGTH", JTYPE_INT, SQLITE_MAX_LENGTH}, - {"SQLITE_LIMIT_SQL_LENGTH", JTYPE_INT, SQLITE_LIMIT_SQL_LENGTH}, - {"SQLITE_MAX_SQL_LENGTH", JTYPE_INT, SQLITE_MAX_SQL_LENGTH}, - {"SQLITE_LIMIT_COLUMN", JTYPE_INT, SQLITE_LIMIT_COLUMN}, - {"SQLITE_MAX_COLUMN", JTYPE_INT, SQLITE_MAX_COLUMN}, - {"SQLITE_LIMIT_EXPR_DEPTH", JTYPE_INT, SQLITE_LIMIT_EXPR_DEPTH}, - {"SQLITE_MAX_EXPR_DEPTH", JTYPE_INT, SQLITE_MAX_EXPR_DEPTH}, - {"SQLITE_LIMIT_COMPOUND_SELECT", JTYPE_INT, SQLITE_LIMIT_COMPOUND_SELECT}, - {"SQLITE_MAX_COMPOUND_SELECT", JTYPE_INT, SQLITE_MAX_COMPOUND_SELECT}, - {"SQLITE_LIMIT_VDBE_OP", JTYPE_INT, SQLITE_LIMIT_VDBE_OP}, - {"SQLITE_MAX_VDBE_OP", JTYPE_INT, SQLITE_MAX_VDBE_OP}, - {"SQLITE_LIMIT_FUNCTION_ARG", JTYPE_INT, SQLITE_LIMIT_FUNCTION_ARG}, - {"SQLITE_MAX_FUNCTION_ARG", JTYPE_INT, SQLITE_MAX_FUNCTION_ARG}, - {"SQLITE_LIMIT_ATTACHED", JTYPE_INT, SQLITE_LIMIT_ATTACHED}, - {"SQLITE_MAX_ATTACHED", JTYPE_INT, SQLITE_MAX_ATTACHED}, - {"SQLITE_LIMIT_LIKE_PATTERN_LENGTH", JTYPE_INT, SQLITE_LIMIT_LIKE_PATTERN_LENGTH}, - {"SQLITE_MAX_LIKE_PATTERN_LENGTH", JTYPE_INT, SQLITE_MAX_LIKE_PATTERN_LENGTH}, - {"SQLITE_LIMIT_VARIABLE_NUMBER", JTYPE_INT, SQLITE_LIMIT_VARIABLE_NUMBER}, - {"SQLITE_MAX_VARIABLE_NUMBER", JTYPE_INT, SQLITE_MAX_VARIABLE_NUMBER}, - {"SQLITE_LIMIT_TRIGGER_DEPTH", JTYPE_INT, SQLITE_LIMIT_TRIGGER_DEPTH}, - {"SQLITE_MAX_TRIGGER_DEPTH", JTYPE_INT, SQLITE_MAX_TRIGGER_DEPTH}, - {"SQLITE_LIMIT_WORKER_THREADS", JTYPE_INT, SQLITE_LIMIT_WORKER_THREADS}, - {"SQLITE_MAX_WORKER_THREADS", JTYPE_INT, SQLITE_MAX_WORKER_THREADS}, - {"SQLITE_THREADSAFE", JTYPE_INT, SQLITE_THREADSAFE}, - {0,0} - }; - jfieldID fieldId; jclass klazz; - const ConfigFlagEntry * pConfFlag; -#if 0 - if( 0==sqlite3_threadsafe() ){ - (*env)->FatalError(env, "sqlite3 currently requires SQLITE_THREADSAFE!=0."); - return; - } -#endif memset(&S3JniGlobal, 0, sizeof(S3JniGlobal)); if( (*env)->GetJavaVM(env, &SJG.jvm) ){ (*env)->FatalError(env, "GetJavaVM() failure shouldn't be possible."); @@ -5267,7 +5637,7 @@ Java_org_sqlite_jni_SQLite3Jni_init(JniArgsEnvClass){ "getBytes", "(Ljava/nio/charset/Charset;)[B"); S3JniExceptionIsFatal("Error getting reference to String.getBytes(Charset)."); - { /* StandardCharsets.UTF_8 */ + { /* java.nio.charset.StandardCharsets.UTF_8 */ jfieldID fUtf8; klazz = (*env)->FindClass(env,"java/nio/charset/StandardCharsets"); S3JniExceptionIsFatal("Error getting reference to StandardCharsets class."); @@ -5281,7 +5651,7 @@ Java_org_sqlite_jni_SQLite3Jni_init(JniArgsEnvClass){ } #ifdef SQLITE_ENABLE_FTS5 - klazz = (*env)->FindClass(env, "org/sqlite/jni/Fts5PhraseIter"); + klazz = (*env)->FindClass(env, "org/sqlite/jni/fts5/Fts5PhraseIter"); S3JniExceptionIsFatal("Error getting reference to org.sqlite.jni.Fts5PhraseIter."); SJG.fts5.jPhraseIter.fidA = (*env)->GetFieldID(env, klazz, "a", "J"); S3JniExceptionIsFatal("Cannot get Fts5PhraseIter.a field."); @@ -5292,6 +5662,10 @@ Java_org_sqlite_jni_SQLite3Jni_init(JniArgsEnvClass){ SJG.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); s3jni_oom_fatal( SJG.mutex ); + SJG.hook.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); + s3jni_oom_fatal( SJG.hook.mutex ); + SJG.nph.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); + s3jni_oom_fatal( SJG.nph.mutex ); SJG.envCache.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); s3jni_oom_fatal( SJG.envCache.mutex ); SJG.perDb.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); @@ -5306,23 +5680,5 @@ Java_org_sqlite_jni_SQLite3Jni_init(JniArgsEnvClass){ sqlite3_shutdown() /* So that it becomes legal for Java-level code to call - ** sqlite3_config(), if it's ever implemented. */; - - /* Set up static "consts" of the SQLite3Jni class. */ - for( pConfFlag = &aLimits[0]; pConfFlag->zName; ++pConfFlag ){ - char const * zSig = (JTYPE_BOOL == pConfFlag->jtype) ? "Z" : "I"; - fieldId = (*env)->GetStaticFieldID(env, jKlazz, pConfFlag->zName, zSig); - S3JniExceptionIsFatal("Missing an expected static member of the SQLite3Jni class."); - assert(fieldId); - switch( pConfFlag->jtype ){ - case JTYPE_INT: - (*env)->SetStaticIntField(env, jKlazz, fieldId, (jint)pConfFlag->value); - break; - case JTYPE_BOOL: - (*env)->SetStaticBooleanField(env, jKlazz, fieldId, - pConfFlag->value ? JNI_TRUE : JNI_FALSE); - break; - } - S3JniExceptionIsFatal("Seting a static member of the SQLite3Jni class failed."); - } + ** sqlite3_config(). */; } diff --git a/ext/jni/src/c/sqlite3-jni.h b/ext/jni/src/c/sqlite3-jni.h index 40dd449901..7d87cb7ed7 100644 --- a/ext/jni/src/c/sqlite3-jni.h +++ b/ext/jni/src/c/sqlite3-jni.h @@ -379,6 +379,30 @@ extern "C" { #define org_sqlite_jni_SQLite3Jni_SQLITE_IOCAP_IMMUTABLE 8192L #undef org_sqlite_jni_SQLite3Jni_SQLITE_IOCAP_BATCH_ATOMIC #define org_sqlite_jni_SQLite3Jni_SQLITE_IOCAP_BATCH_ATOMIC 16384L +#undef org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_LENGTH +#define org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_LENGTH 0L +#undef org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_SQL_LENGTH +#define org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_SQL_LENGTH 1L +#undef org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_COLUMN +#define org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_COLUMN 2L +#undef org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_EXPR_DEPTH +#define org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_EXPR_DEPTH 3L +#undef org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_COMPOUND_SELECT +#define org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_COMPOUND_SELECT 4L +#undef org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_VDBE_OP +#define org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_VDBE_OP 5L +#undef org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_FUNCTION_ARG +#define org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_FUNCTION_ARG 6L +#undef org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_ATTACHED +#define org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_ATTACHED 7L +#undef org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_LIKE_PATTERN_LENGTH +#define org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8L +#undef org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_VARIABLE_NUMBER +#define org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_VARIABLE_NUMBER 9L +#undef org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_TRIGGER_DEPTH +#define org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_TRIGGER_DEPTH 10L +#undef org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_WORKER_THREADS +#define org_sqlite_jni_SQLite3Jni_SQLITE_LIMIT_WORKER_THREADS 11L #undef org_sqlite_jni_SQLite3Jni_SQLITE_OPEN_READONLY #define org_sqlite_jni_SQLite3Jni_SQLITE_OPEN_READONLY 1L #undef org_sqlite_jni_SQLite3Jni_SQLITE_OPEN_READWRITE @@ -787,6 +811,46 @@ JNIEXPORT jlong JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1aggregate_1conte JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1auto_1extension (JNIEnv *, jclass, jobject); +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_backup_finish + * Signature: (Lorg/sqlite/jni/sqlite3_backup;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1backup_1finish + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_backup_init + * Signature: (Lorg/sqlite/jni/sqlite3;Ljava/lang/String;Lorg/sqlite/jni/sqlite3;Ljava/lang/String;)Lorg/sqlite/jni/sqlite3_backup; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1backup_1init + (JNIEnv *, jclass, jobject, jstring, jobject, jstring); + +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_backup_pagecount + * Signature: (Lorg/sqlite/jni/sqlite3_backup;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1backup_1pagecount + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_backup_remaining + * Signature: (Lorg/sqlite/jni/sqlite3_backup;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1backup_1remaining + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_backup_step + * Signature: (Lorg/sqlite/jni/sqlite3_backup;I)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1backup_1step + (JNIEnv *, jclass, jobject, jint); + /* * Class: org_sqlite_jni_SQLite3Jni * Method: sqlite3_bind_blob @@ -851,6 +915,14 @@ JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1bind_1parameter_1 JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1bind_1parameter_1index (JNIEnv *, jclass, jobject, jbyteArray); +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_bind_parameter_name + * Signature: (Lorg/sqlite/jni/sqlite3_stmt;I)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1bind_1parameter_1name + (JNIEnv *, jclass, jobject, jint); + /* * Class: org_sqlite_jni_SQLite3Jni * Method: sqlite3_bind_text @@ -883,6 +955,54 @@ JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1bind_1zeroblob JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1bind_1zeroblob64 (JNIEnv *, jclass, jobject, jint, jlong); +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_blob_bytes + * Signature: (Lorg/sqlite/jni/sqlite3_blob;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1blob_1bytes + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_blob_close + * Signature: (Lorg/sqlite/jni/sqlite3_blob;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1blob_1close + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_blob_open + * Signature: (Lorg/sqlite/jni/sqlite3;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JILorg/sqlite/jni/OutputPointer/sqlite3_blob;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1blob_1open + (JNIEnv *, jclass, jobject, jstring, jstring, jstring, jlong, jint, jobject); + +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_blob_read + * Signature: (Lorg/sqlite/jni/sqlite3_blob;[BI)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1blob_1read + (JNIEnv *, jclass, jobject, jbyteArray, jint); + +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_blob_reopen + * Signature: (Lorg/sqlite/jni/sqlite3_blob;J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1blob_1reopen + (JNIEnv *, jclass, jobject, jlong); + +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_blob_write + * Signature: (Lorg/sqlite/jni/sqlite3_blob;[BI)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1blob_1write + (JNIEnv *, jclass, jobject, jbyteArray, jint); + /* * Class: org_sqlite_jni_SQLite3Jni * Method: sqlite3_busy_handler @@ -1037,18 +1157,10 @@ JNIEXPORT jstring JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1column_1table_ /* * Class: org_sqlite_jni_SQLite3Jni - * Method: sqlite3_column_text_utf8 + * Method: sqlite3_column_text * Signature: (Lorg/sqlite/jni/sqlite3_stmt;I)[B */ -JNIEXPORT jbyteArray JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1column_1text_1utf8 - (JNIEnv *, jclass, jobject, jint); - -/* - * Class: org_sqlite_jni_SQLite3Jni - * Method: sqlite3_column_text - * Signature: (Lorg/sqlite/jni/sqlite3_stmt;I)Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1column_1text +JNIEXPORT jbyteArray JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1column_1text (JNIEnv *, jclass, jobject, jint); /* @@ -1083,14 +1195,6 @@ JNIEXPORT jobject JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1column_1value JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1collation_1needed (JNIEnv *, jclass, jobject, jobject); -/* - * Class: org_sqlite_jni_SQLite3Jni - * Method: sqlite3_context_db_handle - * Signature: (Lorg/sqlite/jni/sqlite3_context;)Lorg/sqlite/jni/sqlite3; - */ -JNIEXPORT jobject JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1context_1db_1handle - (JNIEnv *, jclass, jobject); - /* * Class: org_sqlite_jni_SQLite3Jni * Method: sqlite3_commit_hook @@ -1115,6 +1219,14 @@ JNIEXPORT jstring JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1compileoption_ JNIEXPORT jboolean JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1compileoption_1used (JNIEnv *, jclass, jstring); +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_complete + * Signature: ([B)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1complete + (JNIEnv *, jclass, jbyteArray); + /* * Class: org_sqlite_jni_SQLite3Jni * Method: sqlite3_config @@ -1131,6 +1243,22 @@ JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1config__I JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1config__Lorg_sqlite_jni_ConfigSqllogCallback_2 (JNIEnv *, jclass, jobject); +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_config + * Signature: (Lorg/sqlite/jni/ConfigLogCallback;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1config__Lorg_sqlite_jni_ConfigLogCallback_2 + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_context_db_handle + * Signature: (Lorg/sqlite/jni/sqlite3_context;)Lorg/sqlite/jni/sqlite3; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1context_1db_1handle + (JNIEnv *, jclass, jobject); + /* * Class: org_sqlite_jni_SQLite3Jni * Method: sqlite3_create_collation @@ -1187,6 +1315,14 @@ JNIEXPORT jstring JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1db_1filename JNIEXPORT jobject JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1db_1handle (JNIEnv *, jclass, jobject); +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_db_release_memory + * Signature: (Lorg/sqlite/jni/sqlite3;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1db_1release_1memory + (JNIEnv *, jclass, jobject); + /* * Class: org_sqlite_jni_SQLite3Jni * Method: sqlite3_db_status @@ -1203,6 +1339,22 @@ JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1db_1status JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1errcode (JNIEnv *, jclass, jobject); +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_errmsg + * Signature: (Lorg/sqlite/jni/sqlite3;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1errmsg + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_errstr + * Signature: (I)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1errstr + (JNIEnv *, jclass, jint); + /* * Class: org_sqlite_jni_SQLite3Jni * Method: sqlite3_expanded_sql @@ -1229,19 +1381,19 @@ JNIEXPORT jboolean JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1extended_1res /* * Class: org_sqlite_jni_SQLite3Jni - * Method: sqlite3_errmsg - * Signature: (Lorg/sqlite/jni/sqlite3;)Ljava/lang/String; + * Method: sqlite3_get_autocommit + * Signature: (Lorg/sqlite/jni/sqlite3;)Z */ -JNIEXPORT jstring JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1errmsg +JNIEXPORT jboolean JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1get_1autocommit (JNIEnv *, jclass, jobject); /* * Class: org_sqlite_jni_SQLite3Jni - * Method: sqlite3_errstr - * Signature: (I)Ljava/lang/String; + * Method: sqlite3_get_auxdata + * Signature: (Lorg/sqlite/jni/sqlite3_context;I)Ljava/lang/Object; */ -JNIEXPORT jstring JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1errstr - (JNIEnv *, jclass, jint); +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1get_1auxdata + (JNIEnv *, jclass, jobject, jint); /* * Class: org_sqlite_jni_SQLite3Jni @@ -1283,6 +1435,30 @@ JNIEXPORT void JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1interrupt JNIEXPORT jboolean JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1is_1interrupted (JNIEnv *, jclass, jobject); +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_keyword_check + * Signature: (Ljava/lang/String;)Z + */ +JNIEXPORT jboolean JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1keyword_1check + (JNIEnv *, jclass, jstring); + +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_keyword_count + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1keyword_1count + (JNIEnv *, jclass); + +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_keyword_name + * Signature: (I)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1keyword_1name + (JNIEnv *, jclass, jint); + /* * Class: org_sqlite_jni_SQLite3Jni * Method: sqlite3_last_insert_rowid @@ -1307,6 +1483,14 @@ JNIEXPORT jstring JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1libversion JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1libversion_1number (JNIEnv *, jclass); +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_limit + * Signature: (Lorg/sqlite/jni/sqlite3;II)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1limit + (JNIEnv *, jclass, jobject, jint, jint); + /* * Class: org_sqlite_jni_SQLite3Jni * Method: sqlite3_open @@ -1403,6 +1587,22 @@ JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1preupdate_1old JNIEXPORT void JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1progress_1handler (JNIEnv *, jclass, jobject, jint, jobject); +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_randomness + * Signature: ([B)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1randomness + (JNIEnv *, jclass, jbyteArray); + +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_release_memory + * Signature: (I)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1release_1memory + (JNIEnv *, jclass, jint); + /* * Class: org_sqlite_jni_SQLite3Jni * Method: sqlite3_reset @@ -1563,6 +1763,14 @@ JNIEXPORT jobject JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1rollback_1hook JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1set_1authorizer (JNIEnv *, jclass, jobject, jobject); +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_set_auxdata + * Signature: (Lorg/sqlite/jni/sqlite3_context;ILjava/lang/Object;)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1set_1auxdata + (JNIEnv *, jclass, jobject, jint, jobject); + /* * Class: org_sqlite_jni_SQLite3Jni * Method: sqlite3_set_last_insert_rowid @@ -1627,6 +1835,38 @@ JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1status64 JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1step (JNIEnv *, jclass, jobject); +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_stmt_explain + * Signature: (Lorg/sqlite/jni/sqlite3_stmt;I)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1stmt_1explain + (JNIEnv *, jclass, jobject, jint); + +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_stmt_isexplain + * Signature: (Lorg/sqlite/jni/sqlite3_stmt;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1stmt_1isexplain + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_stmt_readonly + * Signature: (Lorg/sqlite/jni/sqlite3_stmt;)Z + */ +JNIEXPORT jboolean JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1stmt_1readonly + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_stmt_status + * Signature: (Lorg/sqlite/jni/sqlite3_stmt;IZ)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1stmt_1status + (JNIEnv *, jclass, jobject, jint, jboolean); + /* * Class: org_sqlite_jni_SQLite3Jni * Method: sqlite3_strglob @@ -1643,6 +1883,22 @@ JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1strglob JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1strlike (JNIEnv *, jclass, jbyteArray, jbyteArray, jint); +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_system_errno + * Signature: (Lorg/sqlite/jni/sqlite3;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1system_1errno + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_table_column_metadata + * Signature: (Lorg/sqlite/jni/sqlite3;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lorg/sqlite/jni/OutputPointer/String;Lorg/sqlite/jni/OutputPointer/String;Lorg/sqlite/jni/OutputPointer/Bool;Lorg/sqlite/jni/OutputPointer/Bool;Lorg/sqlite/jni/OutputPointer/Bool;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1table_1column_1metadata + (JNIEnv *, jclass, jobject, jstring, jstring, jstring, jobject, jobject, jobject, jobject, jobject); + /* * Class: org_sqlite_jni_SQLite3Jni * Method: sqlite3_threadsafe @@ -1675,6 +1931,14 @@ JNIEXPORT jlong JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1total_1changes64 JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1trace_1v2 (JNIEnv *, jclass, jobject, jint, jobject); +/* + * Class: org_sqlite_jni_SQLite3Jni + * Method: sqlite3_txn_state + * Signature: (Lorg/sqlite/jni/sqlite3;Ljava/lang/String;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1txn_1state + (JNIEnv *, jclass, jobject, jstring); + /* * Class: org_sqlite_jni_SQLite3Jni * Method: sqlite3_update_hook @@ -1765,18 +2029,10 @@ JNIEXPORT jobject JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1value_1java_1o /* * Class: org_sqlite_jni_SQLite3Jni - * Method: sqlite3_value_text_utf8 + * Method: sqlite3_value_text * Signature: (Lorg/sqlite/jni/sqlite3_value;)[B */ -JNIEXPORT jbyteArray JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1value_1text_1utf8 - (JNIEnv *, jclass, jobject); - -/* - * Class: org_sqlite_jni_SQLite3Jni - * Method: sqlite3_value_text - * Signature: (Lorg/sqlite/jni/sqlite3_value;)Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1value_1text +JNIEXPORT jbyteArray JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1value_1text (JNIEnv *, jclass, jobject); /* @@ -1835,229 +2091,6 @@ JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1value_1subtype JNIEXPORT void JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1jni_1internal_1details (JNIEnv *, jclass); -#ifdef __cplusplus -} -#endif -#endif -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class org_sqlite_jni_Fts5ExtensionApi */ - -#ifndef _Included_org_sqlite_jni_Fts5ExtensionApi -#define _Included_org_sqlite_jni_Fts5ExtensionApi -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: getInstance - * Signature: ()Lorg/sqlite/jni/Fts5ExtensionApi; - */ -JNIEXPORT jobject JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_getInstance - (JNIEnv *, jclass); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xColumnCount - * Signature: (Lorg/sqlite/jni/Fts5Context;)I - */ -JNIEXPORT jint JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xColumnCount - (JNIEnv *, jobject, jobject); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xColumnSize - * Signature: (Lorg/sqlite/jni/Fts5Context;ILorg/sqlite/jni/OutputPointer/Int32;)I - */ -JNIEXPORT jint JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xColumnSize - (JNIEnv *, jobject, jobject, jint, jobject); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xColumnText - * Signature: (Lorg/sqlite/jni/Fts5Context;ILorg/sqlite/jni/OutputPointer/String;)I - */ -JNIEXPORT jint JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xColumnText - (JNIEnv *, jobject, jobject, jint, jobject); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xColumnTotalSize - * Signature: (Lorg/sqlite/jni/Fts5Context;ILorg/sqlite/jni/OutputPointer/Int64;)I - */ -JNIEXPORT jint JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xColumnTotalSize - (JNIEnv *, jobject, jobject, jint, jobject); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xGetAuxdata - * Signature: (Lorg/sqlite/jni/Fts5Context;Z)Ljava/lang/Object; - */ -JNIEXPORT jobject JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xGetAuxdata - (JNIEnv *, jobject, jobject, jboolean); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xInst - * Signature: (Lorg/sqlite/jni/Fts5Context;ILorg/sqlite/jni/OutputPointer/Int32;Lorg/sqlite/jni/OutputPointer/Int32;Lorg/sqlite/jni/OutputPointer/Int32;)I - */ -JNIEXPORT jint JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xInst - (JNIEnv *, jobject, jobject, jint, jobject, jobject, jobject); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xInstCount - * Signature: (Lorg/sqlite/jni/Fts5Context;Lorg/sqlite/jni/OutputPointer/Int32;)I - */ -JNIEXPORT jint JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xInstCount - (JNIEnv *, jobject, jobject, jobject); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xPhraseCount - * Signature: (Lorg/sqlite/jni/Fts5Context;)I - */ -JNIEXPORT jint JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xPhraseCount - (JNIEnv *, jobject, jobject); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xPhraseFirst - * Signature: (Lorg/sqlite/jni/Fts5Context;ILorg/sqlite/jni/Fts5PhraseIter;Lorg/sqlite/jni/OutputPointer/Int32;Lorg/sqlite/jni/OutputPointer/Int32;)I - */ -JNIEXPORT jint JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xPhraseFirst - (JNIEnv *, jobject, jobject, jint, jobject, jobject, jobject); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xPhraseFirstColumn - * Signature: (Lorg/sqlite/jni/Fts5Context;ILorg/sqlite/jni/Fts5PhraseIter;Lorg/sqlite/jni/OutputPointer/Int32;)I - */ -JNIEXPORT jint JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xPhraseFirstColumn - (JNIEnv *, jobject, jobject, jint, jobject, jobject); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xPhraseNext - * Signature: (Lorg/sqlite/jni/Fts5Context;Lorg/sqlite/jni/Fts5PhraseIter;Lorg/sqlite/jni/OutputPointer/Int32;Lorg/sqlite/jni/OutputPointer/Int32;)V - */ -JNIEXPORT void JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xPhraseNext - (JNIEnv *, jobject, jobject, jobject, jobject, jobject); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xPhraseNextColumn - * Signature: (Lorg/sqlite/jni/Fts5Context;Lorg/sqlite/jni/Fts5PhraseIter;Lorg/sqlite/jni/OutputPointer/Int32;)V - */ -JNIEXPORT void JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xPhraseNextColumn - (JNIEnv *, jobject, jobject, jobject, jobject); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xPhraseSize - * Signature: (Lorg/sqlite/jni/Fts5Context;I)I - */ -JNIEXPORT jint JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xPhraseSize - (JNIEnv *, jobject, jobject, jint); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xQueryPhrase - * Signature: (Lorg/sqlite/jni/Fts5Context;ILorg/sqlite/jni/Fts5ExtensionApi/xQueryPhraseCallback;)I - */ -JNIEXPORT jint JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xQueryPhrase - (JNIEnv *, jobject, jobject, jint, jobject); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xRowCount - * Signature: (Lorg/sqlite/jni/Fts5Context;Lorg/sqlite/jni/OutputPointer/Int64;)I - */ -JNIEXPORT jint JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xRowCount - (JNIEnv *, jobject, jobject, jobject); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xRowid - * Signature: (Lorg/sqlite/jni/Fts5Context;)J - */ -JNIEXPORT jlong JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xRowid - (JNIEnv *, jobject, jobject); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xSetAuxdata - * Signature: (Lorg/sqlite/jni/Fts5Context;Ljava/lang/Object;)I - */ -JNIEXPORT jint JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xSetAuxdata - (JNIEnv *, jobject, jobject, jobject); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xTokenize - * Signature: (Lorg/sqlite/jni/Fts5Context;[BLorg/sqlite/jni/Fts5/xTokenize_callback;)I - */ -JNIEXPORT jint JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xTokenize - (JNIEnv *, jobject, jobject, jbyteArray, jobject); - -/* - * Class: org_sqlite_jni_Fts5ExtensionApi - * Method: xUserData - * Signature: (Lorg/sqlite/jni/Fts5Context;)Ljava/lang/Object; - */ -JNIEXPORT jobject JNICALL Java_org_sqlite_jni_Fts5ExtensionApi_xUserData - (JNIEnv *, jobject, jobject); - -#ifdef __cplusplus -} -#endif -#endif -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class org_sqlite_jni_fts5_api */ - -#ifndef _Included_org_sqlite_jni_fts5_api -#define _Included_org_sqlite_jni_fts5_api -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: org_sqlite_jni_fts5_api - * Method: getInstanceForDb - * Signature: (Lorg/sqlite/jni/sqlite3;)Lorg/sqlite/jni/fts5_api; - */ -JNIEXPORT jobject JNICALL Java_org_sqlite_jni_fts5_1api_getInstanceForDb - (JNIEnv *, jclass, jobject); - -/* - * Class: org_sqlite_jni_fts5_api - * Method: xCreateFunction - * Signature: (Ljava/lang/String;Ljava/lang/Object;Lorg/sqlite/jni/fts5_extension_function;)I - */ -JNIEXPORT jint JNICALL Java_org_sqlite_jni_fts5_1api_xCreateFunction - (JNIEnv *, jobject, jstring, jobject, jobject); - -#ifdef __cplusplus -} -#endif -#endif -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class org_sqlite_jni_fts5_tokenizer */ - -#ifndef _Included_org_sqlite_jni_fts5_tokenizer -#define _Included_org_sqlite_jni_fts5_tokenizer -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: org_sqlite_jni_fts5_tokenizer - * Method: xTokenize - * Signature: (Lorg/sqlite/jni/Fts5Tokenizer;I[BLorg/sqlite/jni/Fts5/xTokenize_callback;)I - */ -JNIEXPORT jint JNICALL Java_org_sqlite_jni_fts5_1tokenizer_xTokenize - (JNIEnv *, jobject, jobject, jint, jbyteArray, jobject); - #ifdef __cplusplus } #endif diff --git a/ext/jni/src/org/sqlite/jni/AuthorizerCallback.java b/ext/jni/src/org/sqlite/jni/AuthorizerCallback.java index a9f15fc6c2..6dbd9cb77b 100644 --- a/ext/jni/src/org/sqlite/jni/AuthorizerCallback.java +++ b/ext/jni/src/org/sqlite/jni/AuthorizerCallback.java @@ -17,7 +17,7 @@ import org.sqlite.jni.annotation.*; /** Callback for use with {@link SQLite3Jni#sqlite3_set_authorizer}. */ -public interface AuthorizerCallback extends SQLite3CallbackProxy { +public interface AuthorizerCallback extends CallbackProxy { /** Must function as described for the C-level sqlite3_set_authorizer() callback. diff --git a/ext/jni/src/org/sqlite/jni/AutoExtensionCallback.java b/ext/jni/src/org/sqlite/jni/AutoExtensionCallback.java index 1f8ace2fb8..4a36941306 100644 --- a/ext/jni/src/org/sqlite/jni/AutoExtensionCallback.java +++ b/ext/jni/src/org/sqlite/jni/AutoExtensionCallback.java @@ -17,7 +17,7 @@ package org.sqlite.jni; Callback for use with the {@link SQLite3Jni#sqlite3_auto_extension} family of APIs. */ -public interface AutoExtensionCallback extends SQLite3CallbackProxy { +public interface AutoExtensionCallback extends CallbackProxy { /** Must function as described for a C-level sqlite3_auto_extension() callback. diff --git a/ext/jni/src/org/sqlite/jni/BusyHandlerCallback.java b/ext/jni/src/org/sqlite/jni/BusyHandlerCallback.java index db9295bb61..30a5edc037 100644 --- a/ext/jni/src/org/sqlite/jni/BusyHandlerCallback.java +++ b/ext/jni/src/org/sqlite/jni/BusyHandlerCallback.java @@ -16,7 +16,7 @@ package org.sqlite.jni; /** Callback for use with {@link SQLite3Jni#sqlite3_busy_handler}. */ -public interface BusyHandlerCallback extends SQLite3CallbackProxy { +public interface BusyHandlerCallback extends CallbackProxy { /** Must function as documented for the C-level sqlite3_busy_handler() callback argument, minus the (void*) diff --git a/ext/jni/src/org/sqlite/jni/SQLite3CallbackProxy.java b/ext/jni/src/org/sqlite/jni/CallbackProxy.java similarity index 97% rename from ext/jni/src/org/sqlite/jni/SQLite3CallbackProxy.java rename to ext/jni/src/org/sqlite/jni/CallbackProxy.java index 5052664937..086c2f8e5d 100644 --- a/ext/jni/src/org/sqlite/jni/SQLite3CallbackProxy.java +++ b/ext/jni/src/org/sqlite/jni/CallbackProxy.java @@ -41,4 +41,4 @@ package org.sqlite.jni;

2) They all have a {@code call()} method but its signature is callback-specific. */ -public interface SQLite3CallbackProxy {} +public interface CallbackProxy {} diff --git a/ext/jni/src/org/sqlite/jni/CollationCallback.java b/ext/jni/src/org/sqlite/jni/CollationCallback.java index 481c6cd956..7f0e79a3fa 100644 --- a/ext/jni/src/org/sqlite/jni/CollationCallback.java +++ b/ext/jni/src/org/sqlite/jni/CollationCallback.java @@ -20,7 +20,7 @@ import org.sqlite.jni.annotation.NotNull; @see AbstractCollationCallback */ public interface CollationCallback - extends SQLite3CallbackProxy, XDestroyCallback { + extends CallbackProxy, XDestroyCallback { /** Must compare the given byte arrays and return the result using {@code memcmp()} semantics. diff --git a/ext/jni/src/org/sqlite/jni/CollationNeededCallback.java b/ext/jni/src/org/sqlite/jni/CollationNeededCallback.java index e6c917a2c2..b72cf1ba53 100644 --- a/ext/jni/src/org/sqlite/jni/CollationNeededCallback.java +++ b/ext/jni/src/org/sqlite/jni/CollationNeededCallback.java @@ -16,7 +16,7 @@ package org.sqlite.jni; /** Callback for use with {@link SQLite3Jni#sqlite3_collation_needed}. */ -public interface CollationNeededCallback extends SQLite3CallbackProxy { +public interface CollationNeededCallback extends CallbackProxy { /** Has the same semantics as the C-level sqlite3_create_collation() callback. diff --git a/ext/jni/src/org/sqlite/jni/CommitHookCallback.java b/ext/jni/src/org/sqlite/jni/CommitHookCallback.java index 253d0b8cfa..2e9a68d25d 100644 --- a/ext/jni/src/org/sqlite/jni/CommitHookCallback.java +++ b/ext/jni/src/org/sqlite/jni/CommitHookCallback.java @@ -16,7 +16,7 @@ package org.sqlite.jni; /** Callback for use with {@link SQLite3Jni#sqlite3_commit_hook}. */ -public interface CommitHookCallback extends SQLite3CallbackProxy { +public interface CommitHookCallback extends CallbackProxy { /** Works as documented for the C-level sqlite3_commit_hook() callback. Must not throw. diff --git a/ext/jni/src/org/sqlite/jni/ValueHolder.java b/ext/jni/src/org/sqlite/jni/ConfigLogCallback.java similarity index 60% rename from ext/jni/src/org/sqlite/jni/ValueHolder.java rename to ext/jni/src/org/sqlite/jni/ConfigLogCallback.java index 7f6a463ba5..2198f1e104 100644 --- a/ext/jni/src/org/sqlite/jni/ValueHolder.java +++ b/ext/jni/src/org/sqlite/jni/ConfigLogCallback.java @@ -1,5 +1,5 @@ /* -** 2023-07-21 +** 2023-08-23 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: @@ -14,12 +14,12 @@ package org.sqlite.jni; /** - A helper class which simply holds a single value. Its current use - is for communicating values out of anonymous classes, as doing so - requires a "final" reference. + A callback for use with sqlite3_config(). */ -public class ValueHolder { - public T value; - public ValueHolder(){} - public ValueHolder(T v){value = v;} +public interface ConfigLogCallback { + /** + Must function as described for a C-level callback for + {@link SQLite3Jni#sqlite3_config(ConfigLogCallback)}, with the slight signature change. + */ + void call(int errCode, String msg); } diff --git a/ext/jni/src/org/sqlite/jni/OutputPointer.java b/ext/jni/src/org/sqlite/jni/OutputPointer.java index 8a59de574b..c6e48ed72f 100644 --- a/ext/jni/src/org/sqlite/jni/OutputPointer.java +++ b/ext/jni/src/org/sqlite/jni/OutputPointer.java @@ -64,6 +64,27 @@ public final class OutputPointer { } } + /** + 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.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.sqlite3_blob get(){return value;} + /** Equivalent to calling get() then clear(). */ + public final org.sqlite.jni.sqlite3_blob take(){ + final org.sqlite.jni.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 @@ -108,6 +129,26 @@ public final class OutputPointer { } } + /** + 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. diff --git a/ext/jni/src/org/sqlite/jni/PreupdateHookCallback.java b/ext/jni/src/org/sqlite/jni/PreupdateHookCallback.java index b68dd4b6d4..a606139328 100644 --- a/ext/jni/src/org/sqlite/jni/PreupdateHookCallback.java +++ b/ext/jni/src/org/sqlite/jni/PreupdateHookCallback.java @@ -16,7 +16,7 @@ package org.sqlite.jni; /** Callback for use with {@link SQLite3Jni#sqlite3_preupdate_hook}. */ -public interface PreupdateHookCallback extends SQLite3CallbackProxy { +public interface PreupdateHookCallback extends CallbackProxy { /** Must function as described for the C-level sqlite3_preupdate_hook() callback. diff --git a/ext/jni/src/org/sqlite/jni/ProgressHandlerCallback.java b/ext/jni/src/org/sqlite/jni/ProgressHandlerCallback.java index d15bf31a11..bc15377037 100644 --- a/ext/jni/src/org/sqlite/jni/ProgressHandlerCallback.java +++ b/ext/jni/src/org/sqlite/jni/ProgressHandlerCallback.java @@ -16,7 +16,7 @@ package org.sqlite.jni; /** Callback for use with {@link SQLite3Jni#sqlite3_progress_handler}. */ -public interface ProgressHandlerCallback extends SQLite3CallbackProxy { +public interface ProgressHandlerCallback extends CallbackProxy { /** Works as documented for the C-level sqlite3_progress_handler() callback. diff --git a/ext/jni/src/org/sqlite/jni/RollbackHookCallback.java b/ext/jni/src/org/sqlite/jni/RollbackHookCallback.java index 3bf9f79a1a..21600c8057 100644 --- a/ext/jni/src/org/sqlite/jni/RollbackHookCallback.java +++ b/ext/jni/src/org/sqlite/jni/RollbackHookCallback.java @@ -16,7 +16,7 @@ package org.sqlite.jni; /** Callback for use with {@link SQLite3Jni#sqlite3_rollback_hook}. */ -public interface RollbackHookCallback extends SQLite3CallbackProxy { +public interface RollbackHookCallback extends CallbackProxy { /** Works as documented for the C-level sqlite3_rollback_hook() callback. diff --git a/ext/jni/src/org/sqlite/jni/SQLite3Jni.java b/ext/jni/src/org/sqlite/jni/SQLite3Jni.java index d32320b26f..eaf2fdd9e7 100644 --- a/ext/jni/src/org/sqlite/jni/SQLite3Jni.java +++ b/ext/jni/src/org/sqlite/jni/SQLite3Jni.java @@ -165,6 +165,26 @@ public final class SQLite3Jni { @Canonical public static native int sqlite3_auto_extension(@NotNull AutoExtensionCallback callback); + @Canonical + public static native int sqlite3_backup_finish(@NotNull sqlite3_backup b); + + @Canonical + public static native sqlite3_backup sqlite3_backup_init( + @NotNull sqlite3 dbDest, @NotNull String destTableName, + @NotNull sqlite3 dbSrc, @NotNull String srcTableName + ); + + @Canonical + public static native int sqlite3_backup_pagecount(@NotNull sqlite3_backup b); + + @Canonical + public static native int sqlite3_backup_remaining(@NotNull sqlite3_backup b); + + @Canonical + public static native int sqlite3_backup_step( + @NotNull sqlite3_backup b, int nPage + ); + /** Results are undefined if data is not null and n<0 || n>=data.length. */ @@ -217,10 +237,17 @@ public final class SQLite3Jni { /** 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. */ @Canonical - public static native int sqlite3_bind_parameter_index( - @NotNull sqlite3_stmt stmt, byte[] paramName + private static native int sqlite3_bind_parameter_index( + @NotNull sqlite3_stmt stmt, @NotNull byte[] paramName ); @Canonical @@ -231,6 +258,11 @@ public final class SQLite3Jni { return sqlite3_bind_parameter_index(stmt, utf8); } + @Canonical + public static native String sqlite3_bind_parameter_name( + @NotNull sqlite3_stmt stmt, int index + ); + /** Works like the C-level sqlite3_bind_text() but assumes SQLITE_TRANSIENT for the final C API parameter. The byte array is @@ -315,10 +347,50 @@ public final class SQLite3Jni { @NotNull sqlite3_stmt stmt, int ndx, long n ); + @Canonical + public static native int sqlite3_blob_bytes(@NotNull sqlite3_blob blob); + + @Canonical + public static native int sqlite3_blob_close(@Nullable sqlite3_blob blob); + + @Canonical + public static native 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 + ); + /** - 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. + 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, dbName, tableName, columnName, iRow, flags, out); + return out.take(); + }; + + @Canonical + public static native int sqlite3_blob_read( + @NotNull sqlite3_blob b, @NotNull byte[] target, int iOffset + ); + + @Canonical + public static native int sqlite3_blob_reopen( + @NotNull sqlite3_blob out, long newRowId + ); + + @Canonical + public static native int sqlite3_blob_write( + @NotNull sqlite3_blob out, @NotNull byte[] bytes, int iOffset + ); + + /** + 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. */ @Canonical public static native int sqlite3_busy_handler( @@ -416,24 +488,15 @@ public final class SQLite3Jni { ); /** - Returns the given column's contents as UTF-8-encoded (not MUTF-8) - text. Returns null if the C-level sqlite3_column_text() returns - NULL. + 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_text + @see #sqlite3_column_text16(sqlite3_stmt,int) */ - @Canonical(cname="sqlite3_column_text") - public static native byte[] sqlite3_column_text_utf8( - @NotNull sqlite3_stmt stmt, int ndx - ); - - /** - Provides the same feature as the same-named C API but returns the - text in Java-native encoding rather than the C API's UTF-8. - - @see #sqlite3_column_text16 - */ - public static native String sqlite3_column_text( + @Canonical + public static native byte[] sqlite3_column_text( @NotNull sqlite3_stmt stmt, int ndx ); @@ -471,7 +534,7 @@ public final class SQLite3Jni { // } // 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_text(v); break; + // case SQLITE_TEXT: rv = sqlite3_value_text16(v); break; // default: break; // } // } @@ -491,18 +554,13 @@ public final class SQLite3Jni { /** This functions like C's sqlite3_collation_needed16() because - Java's string type is compatible with that interface. + Java's string type is inherently compatible with that interface. */ @Canonical public static native int sqlite3_collation_needed( @NotNull sqlite3 db, @Nullable CollationNeededCallback callback ); - @Canonical - public static native sqlite3 sqlite3_context_db_handle( - @NotNull sqlite3_context cx - ); - @Canonical public static native CommitHookCallback sqlite3_commit_hook( @NotNull sqlite3 db, @Nullable CommitHookCallback hook @@ -518,6 +576,24 @@ public final class SQLite3Jni { @NotNull String optName ); + /** + This implementation is private because it's too easy to pass it + non-NUL-terminated arrays. + */ + @Canonical + private static native int sqlite3_complete( + @NotNull byte[] nulTerminatedUtf8Sql + ); + + @Canonical() + public static int sqlite3_complete(@NotNull String sql){ + /* Design note: we don't implement this in native code because we + won't get a NUL-terminated string there unless we make our own + copy to add a terminator. That's much easier to do here. */ + return sqlite3_complete( (sql+"\0").getBytes(StandardCharsets.UTF_8) ); + } + + /**

Works like in the C API with the exception that it only supports the following subset of configution flags: @@ -532,7 +608,6 @@ public final class SQLite3Jni {

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. - */ @Canonical(comment="Option subset: "+ "SQLITE_CONFIG_SINGLETHREAD, SQLITE_CONFIG_MULTITHREAD, "+ @@ -552,11 +627,22 @@ public final class SQLite3Jni {

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. - */ @Canonical(comment="Option subset: SQLITE_CONFIG_SQLLOG") public static native int sqlite3_config( @Nullable ConfigSqllogCallback logger ); + /** + The sqlite3_config() overload for handling the SQLITE_CONFIG_LOG + option. + */ + @Canonical(comment="Option subset: SQLITE_CONFIG_LOG") + public static native int sqlite3_config( @Nullable ConfigLogCallback logger ); + + @Canonical + public static native sqlite3 sqlite3_context_db_handle( + @NotNull sqlite3_context cx + ); + @Canonical public static native int sqlite3_create_collation( @NotNull sqlite3 db, @NotNull String name, int eTextRep, @@ -612,6 +698,9 @@ public final class SQLite3Jni { @Canonical public static native sqlite3 sqlite3_db_handle( @NotNull sqlite3_stmt stmt ); + @Canonical + public static native int sqlite3_db_release_memory(sqlite3 db); + @Canonical public static native int sqlite3_db_status( @NotNull sqlite3 db, int op, @NotNull OutputPointer.Int32 pCurrent, @@ -621,6 +710,12 @@ public final class SQLite3Jni { @Canonical public static native int sqlite3_errcode(@NotNull sqlite3 db); + @Canonical + public static native String sqlite3_errmsg(@NotNull sqlite3 db); + + @Canonical + public static native String sqlite3_errstr(int resultCode); + @Canonical public static native String sqlite3_expanded_sql(@NotNull sqlite3_stmt stmt); @@ -633,10 +728,12 @@ public final class SQLite3Jni { ); @Canonical - public static native String sqlite3_errmsg(@NotNull sqlite3 db); + public static native boolean sqlite3_get_autocommit(@NotNull sqlite3 db); @Canonical - public static native String sqlite3_errstr(int resultCode); + public static native Object sqlite3_get_auxdata( + @NotNull sqlite3_context cx, int n + ); /** Note that the returned byte offset values assume UTF-8-encoded @@ -657,6 +754,16 @@ public final class SQLite3Jni { @Canonical public static native boolean sqlite3_is_interrupted(@NotNull sqlite3 db); + @Canonical + public static native boolean sqlite3_keyword_check(@NotNull String word); + + @Canonical + public static native int sqlite3_keyword_count(); + + @Canonical + public static native String sqlite3_keyword_name(int index); + + @Canonical public static native long sqlite3_last_insert_rowid(@NotNull sqlite3 db); @@ -666,6 +773,9 @@ public final class SQLite3Jni { @Canonical public static native int sqlite3_libversion_number(); + @Canonical + public static native int sqlite3_limit(@NotNull sqlite3 db, int id, int newVal); + /** Works like its C counterpart and makes the native pointer of the underling (sqlite3*) object available via @@ -758,6 +868,7 @@ public final class SQLite3Jni {

Several overloads provided simplified call signatures. */ + @Canonical public static int sqlite3_prepare( @NotNull sqlite3 db, @NotNull byte[] sqlUtf8, @NotNull OutputPointer.sqlite3_stmt outStmt, @@ -812,6 +923,7 @@ public final class SQLite3Jni { output paramter is returned as the index offset into the given byte array at which SQL parsing stopped. */ + @Canonical public static int sqlite3_prepare_v2( @NotNull sqlite3 db, @NotNull byte[] sqlUtf8, @NotNull OutputPointer.sqlite3_stmt outStmt, @@ -862,6 +974,7 @@ public final class SQLite3Jni { output paramter is returned as the index offset into the given byte array at which SQL parsing stopped. */ + @Canonical public static int sqlite3_prepare_v3( @NotNull sqlite3 db, @NotNull byte[] sqlUtf8, int prepFlags, @NotNull OutputPointer.sqlite3_stmt outStmt, @@ -972,6 +1085,12 @@ public final class SQLite3Jni { @NotNull sqlite3 db, int n, @Nullable ProgressHandlerCallback h ); + @Canonical + public static native void sqlite3_randomness(byte[] target); + + @Canonical + public static native int sqlite3_release_memory(int n); + @Canonical public static native int sqlite3_reset(@NotNull sqlite3_stmt stmt); @@ -992,20 +1111,22 @@ public final class SQLite3Jni { 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. + results in the C-level sqlite3_result_error() being called with a + complaint about the invalid argument. */ @Canonical private static native void sqlite3_result_error( @NotNull sqlite3_context cx, @NotNull byte[] msg, int eTextRep ); + @Canonical public static void sqlite3_result_error( @NotNull sqlite3_context cx, @NotNull byte[] utf8 ){ sqlite3_result_error(cx, utf8, SQLITE_UTF8); } + @Canonical public static void sqlite3_result_error( @NotNull sqlite3_context cx, @NotNull String msg ){ @@ -1163,11 +1284,16 @@ public final class SQLite3Jni { @NotNull sqlite3_context cx, long n ); + /** + This overload is private because its final parameter is arguably + unnecessary in Java. + */ @Canonical private static native void sqlite3_result_blob( @NotNull sqlite3_context cx, @Nullable byte[] blob, int maxLen ); + @Canonical public static void sqlite3_result_blob( @NotNull sqlite3_context cx, @Nullable byte[] blob ){ @@ -1186,31 +1312,41 @@ public final class SQLite3Jni { - If @param maxLen is larger than blob.length, it is truncated to - that value. If it is negative, results are undefined. +

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.

*/ @Canonical private static native void sqlite3_result_blob64( @NotNull sqlite3_context cx, @Nullable byte[] blob, long maxLen ); + @Canonical 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. + */ @Canonical private static native void sqlite3_result_text( @NotNull sqlite3_context cx, @Nullable byte[] utf8, int maxLen ); + @Canonical public static void sqlite3_result_text( @NotNull sqlite3_context cx, @Nullable byte[] utf8 ){ sqlite3_result_text(cx, utf8, null==utf8 ? 0 : utf8.length); } + @Canonical public static void sqlite3_result_text( @NotNull sqlite3_context cx, @Nullable String text ){ @@ -1240,6 +1376,9 @@ public final class SQLite3Jni { 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. */ @Canonical private static native void sqlite3_result_text64( @@ -1251,12 +1390,14 @@ public final class SQLite3Jni { Sets the current UDF result to the given bytes, which are assumed be encoded in UTF-16 using the platform's byte order. */ + @Canonical public static void sqlite3_result_text16( @NotNull sqlite3_context cx, @Nullable byte[] utf16 ){ sqlite3_result_text64(cx, utf16, utf16.length, SQLITE_UTF16); } + @Canonical public static void sqlite3_result_text16( @NotNull sqlite3_context cx, @Nullable String text ){ @@ -1277,6 +1418,11 @@ public final class SQLite3Jni { @NotNull sqlite3 db, @Nullable AuthorizerCallback auth ); + @Canonical + public static native void sqlite3_set_auxdata( + @NotNull sqlite3_context cx, int n, @Nullable Object data + ); + @Canonical public static native void sqlite3_set_last_insert_rowid( @NotNull sqlite3 db, long rowid @@ -1304,7 +1450,6 @@ public final class SQLite3Jni { @Canonical public static native String sqlite3_sql(@NotNull sqlite3_stmt stmt); - @Canonical public static native int sqlite3_status( int op, @NotNull OutputPointer.Int32 pCurrent, @@ -1320,15 +1465,39 @@ public final class SQLite3Jni { @Canonical public static native int sqlite3_step(@NotNull sqlite3_stmt stmt); + @Canonical + public static native int sqlite3_stmt_explain( + @NotNull sqlite3_stmt stmt, int op + ); + + @Canonical + public static native int sqlite3_stmt_isexplain(@NotNull sqlite3_stmt stmt); + + @Canonical + public static native boolean sqlite3_stmt_readonly(@NotNull sqlite3_stmt stmt); + + @Canonical + 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. + 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. */ @Canonical private static native int sqlite3_strglob( - @NotNull byte[] glob, @NotNull byte[] txt + @NotNull byte[] glob, @NotNull byte[] nullTerminatedUtf8 ); + @Canonical public static int sqlite3_strglob( @NotNull String glob, @NotNull String txt ){ @@ -1339,14 +1508,15 @@ public final class SQLite3Jni { } /** - Internal impl of the public sqlite3_strlike() method. Neither - argument may be NULL and both MUST be NUL-terminated UTF-8. + The LIKE counterpart of the private sqlite3_strglob() method. */ @Canonical private static native int sqlite3_strlike( - @NotNull byte[] glob, @NotNull byte[] txt, int escChar + @NotNull byte[] glob, @NotNull byte[] nullTerminatedUtf8, + int escChar ); + @Canonical public static int sqlite3_strlike( @NotNull String glob, @NotNull String txt, char escChar ){ @@ -1357,6 +1527,48 @@ public final class SQLite3Jni { ); } + @Canonical + public static native int sqlite3_system_errno(@NotNull sqlite3 db); + + @Canonical + 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; + } + @Canonical public static native int sqlite3_threadsafe(); @@ -1381,10 +1593,25 @@ public final class SQLite3Jni { ); @Canonical - public static native UpdateHookCallback sqlite3_update_hook( - sqlite3 db, UpdateHookCallback hook + public static native int sqlite3_txn_state( + @NotNull sqlite3 db, @Nullable String zSchema ); + @Canonical + public static native UpdateHookCallback sqlite3_update_hook( + @NotNull sqlite3 db, @Nullable UpdateHookCallback 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(). + */ + @Canonical public static native byte[] sqlite3_value_blob(@NotNull sqlite3_value v); @@ -1439,30 +1666,14 @@ public final class SQLite3Jni { } /** - Returns the given value as UTF-8-encoded bytes, or null if the - underlying C-level sqlite3_value_text() returns NULL. + 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. */ - @Canonical(cname="sqlite3_value_text", - comment="Renamed because its String-returning overload would "+ - "otherwise be ambiguous.") - public static native byte[] sqlite3_value_text_utf8(@NotNull sqlite3_value v); + @Canonical + public static native byte[] sqlite3_value_text(@NotNull sqlite3_value v); - /** - Provides the same feature as the same-named C API but returns the - text in Java-native encoding rather than the C API's UTF-8. - - @see #sqlite3_value_text16 - */ - public static native String sqlite3_value_text(@NotNull sqlite3_value v); - - /** - In the Java layer, sqlite3_value_text() and - sqlite3_value_text16() are functionally equivalent, the - difference being only where the encoding to UTF-16 (if necessary) - takes place. This function does it via SQLite and - sqlite3_value_text() fetches UTF-8 (SQLite's default encoding) - and converts it to UTF-16 in Java. - */ @Canonical public static native String sqlite3_value_text16(@NotNull sqlite3_value v); @@ -1497,10 +1708,6 @@ public final class SQLite3Jni { public static final String SQLITE_VERSION = sqlite3_libversion(); public static final String SQLITE_SOURCE_ID = sqlite3_sourceid(); - // Initialized at static init time to the build-time value of - // SQLITE_THREADSAFE. - public static int SQLITE_THREADSAFE = -1; - // access public static final int SQLITE_ACCESS_EXISTS = 0; public static final int SQLITE_ACCESS_READWRITE = 1; @@ -1713,36 +1920,19 @@ public final class SQLite3Jni { public static final int SQLITE_IOCAP_IMMUTABLE = 8192; public static final int SQLITE_IOCAP_BATCH_ATOMIC = 16384; - // limits. These get injected at init-time so that they stay in sync - // with the compile-time options. This unfortunately means they are - // not final, but keeping them in sync with their C values seems - // more important than protecting users from assigning to these - // (with unpredictable results). - public static int SQLITE_MAX_ALLOCATION_SIZE = -1; - public static int SQLITE_LIMIT_LENGTH = -1; - public static int SQLITE_MAX_LENGTH = -1; - public static int SQLITE_LIMIT_SQL_LENGTH = -1; - public static int SQLITE_MAX_SQL_LENGTH = -1; - public static int SQLITE_LIMIT_COLUMN = -1; - public static int SQLITE_MAX_COLUMN = -1; - public static int SQLITE_LIMIT_EXPR_DEPTH = -1; - public static int SQLITE_MAX_EXPR_DEPTH = -1; - public static int SQLITE_LIMIT_COMPOUND_SELECT = -1; - public static int SQLITE_MAX_COMPOUND_SELECT = -1; - public static int SQLITE_LIMIT_VDBE_OP = -1; - public static int SQLITE_MAX_VDBE_OP = -1; - public static int SQLITE_LIMIT_FUNCTION_ARG = -1; - public static int SQLITE_MAX_FUNCTION_ARG = -1; - public static int SQLITE_LIMIT_ATTACHED = -1; - public static int SQLITE_MAX_ATTACHED = -1; - public static int SQLITE_LIMIT_LIKE_PATTERN_LENGTH = -1; - public static int SQLITE_MAX_LIKE_PATTERN_LENGTH = -1; - public static int SQLITE_LIMIT_VARIABLE_NUMBER = -1; - public static int SQLITE_MAX_VARIABLE_NUMBER = -1; - public static int SQLITE_LIMIT_TRIGGER_DEPTH = -1; - public static int SQLITE_MAX_TRIGGER_DEPTH = -1; - public static int SQLITE_LIMIT_WORKER_THREADS = -1; - public static int SQLITE_MAX_WORKER_THREADS = -1; + // 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 = 1; diff --git a/ext/jni/src/org/sqlite/jni/TableColumnMetadata.java b/ext/jni/src/org/sqlite/jni/TableColumnMetadata.java new file mode 100644 index 0000000000..70b7c90ec8 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/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; + +/** + 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/Tester1.java b/ext/jni/src/org/sqlite/jni/Tester1.java index e418de6096..93a3174415 100644 --- a/ext/jni/src/org/sqlite/jni/Tester1.java +++ b/ext/jni/src/org/sqlite/jni/Tester1.java @@ -38,6 +38,17 @@ import java.util.concurrent.Future; @java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD}) @interface SingleThreadOnly{} +/** + A helper class which simply holds a single value. Its current use + is for communicating values out of anonymous classes, as doing so + requires a "final" reference. +*/ +class ValueHolder { + public T value; + public ValueHolder(){} + public ValueHolder(T v){value = v;} +} + public class Tester1 implements Runnable { //! True when running in multi-threaded mode. private static boolean mtMode = false; @@ -68,62 +79,67 @@ public class Tester1 implements Runnable { static final Metrics metrics = new Metrics(); - public synchronized static void outln(){ + public static synchronized void outln(){ if( !quietMode ){ System.out.println(""); } } - public synchronized static void outln(Object val){ + 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 synchronized static void out(Object val){ + public static synchronized void out(Object val){ if( !quietMode ){ System.out.print(val); } } @SuppressWarnings("unchecked") - public synchronized static void out(Object... vals){ + public static synchronized void out(Object... vals){ if( !quietMode ){ - System.out.print(Thread.currentThread().getName()+": "); + outPrefix(); for(Object v : vals) out(v); } } @SuppressWarnings("unchecked") - public synchronized static void outln(Object... vals){ + public static synchronized void outln(Object... vals){ if( !quietMode ){ out(vals); out("\n"); } } static volatile int affirmCount = 0; - public synchronized static void affirm(Boolean v, String comment){ + 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."); } - @ManualTest /* because testing this for threading is pointless */ + @SingleThreadOnly /* because it's thread-agnostic */ private void test1(){ affirm(sqlite3_libversion_number() == SQLITE_VERSION_NUMBER); - affirm(SQLITE_MAX_LENGTH > 0); - affirm(SQLITE_MAX_TRIGGER_DEPTH>0); } - static sqlite3 createNewDb(){ + public static sqlite3 createNewDb(){ final OutputPointer.sqlite3 out = new OutputPointer.sqlite3(); int rc = sqlite3_open(":memory:", out); ++metrics.dbOpen; @@ -141,11 +157,11 @@ public class Tester1 implements Runnable { return db; } - static void execSql(sqlite3 db, String[] sql){ + public static void execSql(sqlite3 db, String[] sql){ execSql(db, String.join("", sql)); } - static int execSql(sqlite3 db, boolean throwOnError, String 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; @@ -186,13 +202,13 @@ public class Tester1 implements Runnable { return rc; } - static void execSql(sqlite3 db, String sql){ + public static void execSql(sqlite3 db, String sql){ execSql(db, true, sql); } - static sqlite3_stmt prepare(sqlite3 db, boolean throwOnError, String 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(db, sql, outStmt); + int rc = sqlite3_prepare_v2(db, sql, outStmt); if( throwOnError ){ affirm( 0 == rc ); } @@ -203,9 +219,11 @@ public class Tester1 implements Runnable { } return rv; } - static sqlite3_stmt prepare(sqlite3 db, String sql){ + + public static sqlite3_stmt prepare(sqlite3 db, String sql){ return prepare(db, true, sql); } + private void showCompileOption(){ int i = 0; String optName; @@ -214,7 +232,15 @@ public class Tester1 implements Runnable { 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(){ @@ -260,6 +286,7 @@ public class Tester1 implements Runnable { 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); if( SQLITE_DONE != rc ){ @@ -335,6 +362,7 @@ public class Tester1 implements Runnable { 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); @@ -360,6 +388,7 @@ public class Tester1 implements Runnable { 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) ); int total2 = 0; while( SQLITE_ROW == sqlite3_step(stmt) ){ total2 += sqlite3_column_int(stmt, 0); @@ -425,7 +454,7 @@ public class Tester1 implements Runnable { sqlite3 db = createNewDb(); execSql(db, "CREATE TABLE t(a)"); sqlite3_stmt stmt = prepare(db, "INSERT INTO t(a) VALUES(?);"); - String[] list1 = { "hell🤩", "w😃rld", "!" }; + String[] list1 = { "hell🤩", "w😃rld", "!🤩" }; int rc; int n = 0; for( String e : list1 ){ @@ -445,15 +474,36 @@ public class Tester1 implements Runnable { 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(sqlite3_column_text(stmt, 0)) ); - affirm( txt.equals(sqlite3_value_text(sv)) ); + 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())); + affirm("w😃rldhell🤩!🤩".equals(sbuf.toString())); + + stmt = prepare(db, "SELECT ?, ?"); + rc = sqlite3_bind_text(stmt, 1, ""); + affirm( 0==rc ); + rc = sqlite3_bind_text(stmt, 2, (String)null); + affirm( 0==rc ); + rc = sqlite3_step(stmt); + affirm( SQLITE_ROW==rc ); + byte[] colBa = sqlite3_column_text(stmt, 0); + affirm( 0==colBa.length ); + colBa = sqlite3_column_text(stmt, 1); + affirm( null==colBa ); + sqlite3_finalize(stmt); + sqlite3_close_v2(db); } @@ -573,7 +623,7 @@ public class Tester1 implements Runnable { affirm( 1 == xDestroyCalled.value ); } - @ManualTest /* because threading is meaningless here */ + @SingleThreadOnly /* because it's thread-agnostic */ private void testToUtf8(){ /** https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html @@ -731,6 +781,7 @@ public class Tester1 implements Runnable { } sqlite3_finalize(stmt); affirm( 1 == n ); + affirm( 0==sqlite3_db_release_memory(db) ); sqlite3_close_v2(db); } @@ -762,7 +813,7 @@ public class Tester1 implements Runnable { 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( null != stmt ); + 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); @@ -774,6 +825,7 @@ public class Tester1 implements Runnable { 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) ){ @@ -940,8 +992,8 @@ public class Tester1 implements Runnable { affirm( 7 == counter.value ); } - @ManualTest /* because threads inherently break this test */ - private void testBusy(){ + @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(); @@ -1224,10 +1276,13 @@ public class Tester1 implements Runnable { } Exception err = null; try { - Class t = Class.forName("org.sqlite.jni.TesterFts5"); + 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; @@ -1273,7 +1328,7 @@ public class Tester1 implements Runnable { final ValueHolder val = new ValueHolder<>(0); final ValueHolder toss = new ValueHolder<>(null); final AutoExtensionCallback ax = new AutoExtensionCallback(){ - @Override public synchronized int call(sqlite3 db){ + @Override public int call(sqlite3 db){ ++val.value; if( null!=toss.value ){ throw new RuntimeException(toss.value); @@ -1300,7 +1355,7 @@ public class Tester1 implements Runnable { sqlite3 db = createNewDb(); affirm( 4==val.value ); - execSql(db, "ATTACH ':memory' as foo"); + execSql(db, "ATTACH ':memory:' as foo"); affirm( 4==val.value, "ATTACH uses the same connection, not sub-connections." ); sqlite3_close(db); db = null; @@ -1359,7 +1414,197 @@ public class Tester1 implements Runnable { affirm( 8 == val.value ); } - @ManualTest /* we really only want to run this test manually. */ + + 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) ); + + final 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") ); + + 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 db1 = createNewDb(); + final sqlite3 db2 = createNewDb(); + + execSql(db1, new String[]{ + "pragma page_size=512; VACUUM;", + "create table t(a);", + "insert into t(a) values(1),(2),(3);" + }); + affirm( null==sqlite3_backup_init(db1,"main",db1,"main") ); + final sqlite3_backup b = sqlite3_backup_init(db2,"main",db1,"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 ); + + sqlite3_close_v2(db1); + + final sqlite3_stmt stmt = prepare(db2,"SELECT sum(a) from t"); + sqlite3_step(stmt); + affirm( sqlite3_column_int(stmt,0) == 6 ); + + sqlite3_finalize(stmt); + sqlite3_close_v2(db2); + } + + 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); + } + + /* 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); @@ -1402,8 +1647,6 @@ public class Tester1 implements Runnable { outln(); } } - testToUtf8(); - test1(); for(java.lang.reflect.Method m : mlist){ nap(); try{ @@ -1413,9 +1656,6 @@ public class Tester1 implements Runnable { throw e; } } - if( !fromThread ){ - testBusy(); - } synchronized( this.getClass() ){ ++nTestRuns; } @@ -1466,6 +1706,7 @@ public class Tester1 implements Runnable { 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++]; @@ -1486,6 +1727,8 @@ public class Tester1 implements Runnable { 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")){ @@ -1509,7 +1752,7 @@ public class Tester1 implements Runnable { }; int rc = sqlite3_config( log ); affirm( 0==rc ); - rc = sqlite3_config( null ); + rc = sqlite3_config( (ConfigSqllogCallback)null ); affirm( 0==rc ); rc = sqlite3_config( log ); affirm( 0==rc ); @@ -1518,6 +1761,19 @@ public class Tester1 implements Runnable { "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, ", @@ -1527,7 +1783,7 @@ public class Tester1 implements Runnable { { // Build list of tests to run from the methods named test*(). testMethods = new ArrayList<>(); - out("Skipping tests in multi-thread mode:"); + int nSkipped = 0; for(final java.lang.reflect.Method m : Tester1.class.getDeclaredMethods()){ final String name = m.getName(); if( name.equals("testFail") ){ @@ -1536,18 +1792,21 @@ public class Tester1 implements Runnable { } }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); } } } - out("\n"); + if( nSkipped>0 ) out("\n"); } final long timeStart = System.currentTimeMillis(); int nLoop = 0; - switch( SQLITE_THREADSAFE ){ /* Sanity checking */ + switch( sqlite3_threadsafe() ){ /* Sanity checking */ case 0: affirm( SQLITE_ERROR==sqlite3_config( SQLITE_CONFIG_SINGLETHREAD ), "Could not switch to single-thread mode." ); @@ -1573,19 +1832,21 @@ public class Tester1 implements Runnable { outln("libversion_number: ", sqlite3_libversion_number(),"\n", sqlite3_libversion(),"\n",SQLITE_SOURCE_ID,"\n", - "SQLITE_THREADSAFE=",SQLITE_THREADSAFE); - outln("Running ",nRepeat," loop(s) with ",nThread," thread(s) each."); + "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; - out((1==nLoop ? "" : " ")+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 ); - //final List> futures = new ArrayList<>(); for( int i = 0; i < nThread; ++i ){ ex.submit( new Tester1(i), i ); } @@ -1608,7 +1869,7 @@ public class Tester1 implements Runnable { if( null!=err ) throw err; } } - outln(); + if( showLoopCount ) outln(); quietMode = false; final long timeEnd = System.currentTimeMillis(); @@ -1618,17 +1879,22 @@ public class Tester1 implements Runnable { if( doSomethingForDev ){ sqlite3_jni_internal_details(); } + affirm( 0==sqlite3_release_memory(1) ); sqlite3_shutdown(); int nMethods = 0; int nNatives = 0; + int nCanonical = 0; final java.lang.reflect.Method[] declaredMethods = SQLite3Jni.class.getDeclaredMethods(); for(java.lang.reflect.Method m : declaredMethods){ - int mod = m.getModifiers(); + final int mod = m.getModifiers(); if( 0!=(mod & java.lang.reflect.Modifier.STATIC) ){ final String name = m.getName(); if(name.startsWith("sqlite3_")){ ++nMethods; + if( m.isAnnotationPresent( org.sqlite.jni.annotation.Canonical.class ) ){ + ++nCanonical; + } if( 0!=(mod & java.lang.reflect.Modifier.NATIVE) ){ ++nNatives; } @@ -1636,8 +1902,10 @@ public class Tester1 implements Runnable { } } outln("\tSQLite3Jni.sqlite3_*() methods: "+ - nNatives+" native methods and "+ - (nMethods - nNatives)+" Java impls"); + nNatives+" native methods, "+ + (nMethods - nNatives)+" Java impls. ", + nCanonical," methods flagged as canonical" + ); outln("\tTotal test time = " +(timeEnd - timeStart)+"ms"); } diff --git a/ext/jni/src/org/sqlite/jni/TraceV2Callback.java b/ext/jni/src/org/sqlite/jni/TraceV2Callback.java index 897aeefa9f..4e69bd8756 100644 --- a/ext/jni/src/org/sqlite/jni/TraceV2Callback.java +++ b/ext/jni/src/org/sqlite/jni/TraceV2Callback.java @@ -17,7 +17,7 @@ import org.sqlite.jni.annotation.Nullable; /** Callback for use with {@link SQLite3Jni#sqlite3_trace_v2}. */ -public interface TraceV2Callback extends SQLite3CallbackProxy { +public interface TraceV2Callback extends CallbackProxy { /** Called by sqlite3 for various tracing operations, as per sqlite3_trace_v2(). Note that this interface elides the 2nd diff --git a/ext/jni/src/org/sqlite/jni/UpdateHookCallback.java b/ext/jni/src/org/sqlite/jni/UpdateHookCallback.java index 4fd0a63240..4d6afb887f 100644 --- a/ext/jni/src/org/sqlite/jni/UpdateHookCallback.java +++ b/ext/jni/src/org/sqlite/jni/UpdateHookCallback.java @@ -16,7 +16,7 @@ package org.sqlite.jni; /** Callback for use with {@link SQLite3Jni#sqlite3_update_hook}. */ -public interface UpdateHookCallback extends SQLite3CallbackProxy { +public interface UpdateHookCallback extends CallbackProxy { /** Must function as described for the C-level sqlite3_update_hook() callback. diff --git a/ext/jni/src/org/sqlite/jni/annotation/Canonical.java b/ext/jni/src/org/sqlite/jni/annotation/Canonical.java index f329aee137..fdb157335f 100644 --- a/ext/jni/src/org/sqlite/jni/annotation/Canonical.java +++ b/ext/jni/src/org/sqlite/jni/annotation/Canonical.java @@ -7,35 +7,26 @@ package org.sqlite.jni.annotation; the Java API.

Canonical functions, unless specifically documented, have the - same semantics as their counterparts in - the C API documentation, - despite their signatures perhaps differing. The Java API adds a - number of overloads to simplify use, as well as a few Java-specific - functions, and those are never flagged as @Canonical. + same semantics as their counterparts in the C API + documentation, despite their signatures perhaps differing + slightly. Canonical forms may be native or implemented in Java. + Sometimes multiple overloads are labeled as Canonical because one + or more of them are just type- or encoding-related conversion + wrappers but provide identical semantics (e.g. from a String to a + byte[]). The Java API adds a number of convenience overloads to + simplify use, as well as a few Java-specific functions, and those + are never flagged as @Canonical.

In some cases, the canonical version of a function is private and exposed to Java via public overloads. -

In rare cases, the Java interface for a canonical function has a - different name than its C counterpart. For such cases, - (cname=the-C-side-name) is passed to this annotation and a - Java-side implementation with a slightly different signature is - added to with the canonical name. As of this writing, that applies - only to {@link org.sqlite.jni.SQLite3Jni#sqlite3_value_text_utf8} - and {@link org.sqlite.jni.SQLite3Jni#sqlite3_column_text_utf8}. -

The comment property can be used to add a comment. */ @java.lang.annotation.Documented -@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.SOURCE) +@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME) @java.lang.annotation.Target(java.lang.annotation.ElementType.METHOD) public @interface Canonical{ - /** - Java functions which directly map to a canonical function but - change its name for some reason should not the original name - in this property. - */ - String cname() default ""/*doesn't allow null*/; /** Brief comments about the binding, e.g. noting any major semantic differences. diff --git a/ext/jni/src/org/sqlite/jni/annotation/NotNull.java b/ext/jni/src/org/sqlite/jni/annotation/NotNull.java index 99eae7370a..49003358db 100644 --- a/ext/jni/src/org/sqlite/jni/annotation/NotNull.java +++ b/ext/jni/src/org/sqlite/jni/annotation/NotNull.java @@ -2,23 +2,37 @@ package org.sqlite.jni.annotation; /** This annotation is for flagging parameters which may not legally be - null. When used in the context of callback methods which are - called into from the C APIs, this annotation communicates that the - C API will never pass a null value to the callback. + null or point to closed/finalized C-side resources. + +

In the case of Java types which map directly to C struct types + (e.g. {@link org.sqlite.jni.sqlite3}, {@link + org.sqlite.jni.sqlite3_stmt}, and {@link + org.sqlite.jni.sqlite3_context}), a closed/finalized resource is + also considered to be null for purposes this annotation because the + C-side effect of passing such a handle is the same as if null is + passed.

+ +

When used in the context of Java interfaces which are called + from the C APIs, this annotation communicates that the C API will + never pass a null value to the callback for that parameter.

+ +

Passing a null, for this annotation's definition of null, for + any parameter marked with this annoation specifically invokes + undefined behavior.

Note that the C-style API does not throw any exceptions on its own because it has a no-throw policy in order to retain its C-style semantics, but it may trigger NullPointerExceptions (or similar) if - passed a null for a parameter flagged with this annotation. + passed a null for a parameter flagged with this annotation.

This annotation is informational only. No policy is in place to programmatically ensure that NotNull is conformed to in client - code. + code.

This annotation is solely for the use by the classes in this package but is made public so that javadoc will link to it from the annotated functions. It is not part of the public API and - client-level code must not rely on it. + client-level code must not rely on it.

*/ @java.lang.annotation.Documented @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.SOURCE) diff --git a/ext/jni/src/org/sqlite/jni/Fts5.java b/ext/jni/src/org/sqlite/jni/fts5/Fts5.java similarity index 77% rename from ext/jni/src/org/sqlite/jni/Fts5.java rename to ext/jni/src/org/sqlite/jni/fts5/Fts5.java index 3135db96d3..2e5678f1ee 100644 --- a/ext/jni/src/org/sqlite/jni/Fts5.java +++ b/ext/jni/src/org/sqlite/jni/fts5/Fts5.java @@ -11,15 +11,13 @@ ************************************************************************* ** 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 */ @@ -28,7 +26,7 @@ public final class Fts5 { /** Callback type for use with xTokenize() variants */ - public static interface xTokenize_callback { + public static interface XTokenizeCallback { int call(int tFlags, byte[] txt, int iStart, int iEnd); } diff --git a/ext/jni/src/org/sqlite/jni/Fts5Context.java b/ext/jni/src/org/sqlite/jni/fts5/Fts5Context.java similarity index 93% rename from ext/jni/src/org/sqlite/jni/Fts5Context.java rename to ext/jni/src/org/sqlite/jni/fts5/Fts5Context.java index e78f67d556..a5b937ceb8 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.*; /** 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 87% rename from ext/jni/src/org/sqlite/jni/Fts5ExtensionApi.java rename to ext/jni/src/org/sqlite/jni/fts5/Fts5ExtensionApi.java index ab2995f378..559f42392d 100644 --- a/ext/jni/src/org/sqlite/jni/Fts5ExtensionApi.java +++ b/ext/jni/src/org/sqlite/jni/fts5/Fts5ExtensionApi.java @@ -11,8 +11,9 @@ ************************************************************************* ** 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.*; import org.sqlite.jni.annotation.*; /** @@ -27,8 +28,8 @@ public final class Fts5ExtensionApi extends NativePointerHolder xDestroyCalled = new ValueHolder<>(false); - ValueHolder xFuncCount = new ValueHolder<>(0); + final int outputs[] = {0, 0}; final fts5_extension_function func = new fts5_extension_function(){ - public void xFunction(Fts5ExtensionApi ext, Fts5Context fCx, - sqlite3_context pCx, sqlite3_value argv[]){ - int nCols = ext.xColumnCount(fCx); + @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 ); - if(true){ - OutputPointer.String op = new OutputPointer.String(); - 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_text(argv[i])) ); - //outln("xFunction col "+i+": "+val); - } + 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 ); } - ++xFuncCount.value; + ++outputs[0]; } public void xDestroy(){ - xDestroyCalled.value = true; + outputs[1] = 1; } }; int rc = fApi.xCreateFunction("myaux", pUserData, func); affirm( 0==rc ); - affirm( 0==xFuncCount.value ); + affirm( 0==outputs[0] ); execSql(db, "select myaux(ft,a,b) from ft;"); - affirm( 2==xFuncCount.value ); - affirm( !xDestroyCalled.value ); + affirm( 2==outputs[0] ); + affirm( 0==outputs[1] ); sqlite3_close_v2(db); - affirm( xDestroyCalled.value ); + affirm( 1==outputs[1] ); } - public TesterFts5(boolean verbose){ - if(verbose){ - final long timeStart = System.currentTimeMillis(); - final int oldAffirmCount = Tester1.affirmCount; - test1(); - final int affirmCount = Tester1.affirmCount - oldAffirmCount; - final long timeEnd = System.currentTimeMillis(); - outln("FTS5 Tests done. Assertions checked = ",affirmCount, - ", Total time = ",(timeEnd - timeStart),"ms"); - }else{ - test1(); - } + private static synchronized void runTests(){ + test1(); + } + + public TesterFts5(){ + runTests(); } - public TesterFts5(){ this(false); } } 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 82% rename from ext/jni/src/org/sqlite/jni/fts5_api.java rename to ext/jni/src/org/sqlite/jni/fts5/fts5_api.java index 92ca7c669a..699941cade 100644 --- a/ext/jni/src/org/sqlite/jni/fts5_api.java +++ b/ext/jni/src/org/sqlite/jni/fts5/fts5_api.java @@ -11,8 +11,9 @@ ************************************************************************* ** 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.*; /** INCOMPLETE AND COMPLETELY UNTESTED. @@ -25,7 +26,8 @@ import org.sqlite.jni.annotation.*; 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 @@ -33,6 +35,31 @@ public final class fts5_api extends NativePointerHolder { */ public static synchronized native fts5_api getInstanceForDb(@NotNull sqlite3 db); + @Canonical + 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, @@ -49,22 +76,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..7ed353ac0b 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.sqlite3_context; +import org.sqlite.jni.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 053434e266..dd5f78bd14 100644 --- a/ext/jni/src/org/sqlite/jni/fts5_tokenizer.java +++ b/ext/jni/src/org/sqlite/jni/fts5/fts5_tokenizer.java @@ -11,8 +11,9 @@ ************************************************************************* ** This file is part of the JNI bindings for the sqlite3 C API. */ -package org.sqlite.jni; -import org.sqlite.jni.annotation.*; +package org.sqlite.jni.fts5; +import org.sqlite.jni.NativePointerHolder; +import org.sqlite.jni.annotation.NotNull; /** INCOMPLETE AND COMPLETELY UNTESTED. @@ -31,7 +32,7 @@ public final class fts5_tokenizer extends NativePointerHolder { public native int xTokenize(@NotNull Fts5Tokenizer t, int tokFlags, @NotNull byte pText[], - @NotNull Fts5.xTokenize_callback callback); + @NotNull Fts5.XTokenizeCallback callback); // int (*xTokenize)(Fts5Tokenizer*, diff --git a/ext/jni/src/org/sqlite/jni/package-info.java b/ext/jni/src/org/sqlite/jni/package-info.java index 2ca997955a..853d76fd73 100644 --- a/ext/jni/src/org/sqlite/jni/package-info.java +++ b/ext/jni/src/org/sqlite/jni/package-info.java @@ -1,19 +1,59 @@ /** This package houses a JNI binding to the SQLite3 C API. -

The docs are in progress. +

The primary interfaces are in {@link + org.sqlite.jni.SQLite3Jni}.

-

The primary interfaces are in {@link org.sqlite.jni.SQLite3Jni}. +

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. + 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: + thread-safe, insofar as the C API guarantees, with some addenda:

    @@ -39,11 +79,11 @@

    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... + 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. + undefined behavior.

    */ package org.sqlite.jni; diff --git a/ext/jni/src/org/sqlite/jni/Fts5Function.java b/ext/jni/src/org/sqlite/jni/sqlite3_backup.java similarity index 50% rename from ext/jni/src/org/sqlite/jni/Fts5Function.java rename to ext/jni/src/org/sqlite/jni/sqlite3_backup.java index 463ec034f5..d8ba78aec8 100644 --- a/ext/jni/src/org/sqlite/jni/Fts5Function.java +++ b/ext/jni/src/org/sqlite/jni/sqlite3_backup.java @@ -1,5 +1,5 @@ /* -** 2023-08-04 +** 2023-09-03 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: @@ -14,14 +14,12 @@ package org.sqlite.jni; /** - Fts5Function is used in conjunction with the - sqlite3_create_fts_function() JNI-bound API to give that native code - access to the callback functions needed in order to implement - FTS5 auxiliary functions in Java. + 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 abstract class Fts5Function { - - public abstract void xFunction(Fts5ExtensionApi pApi, Fts5Context pFts, - sqlite3_context pCtx, sqlite3_value argv[]); - public void xDestroy() {} +public final class sqlite3_backup extends NativePointerHolder { + // Only invoked from JNI. + private sqlite3_backup(){} } diff --git a/ext/jni/src/org/sqlite/jni/sqlite3_blob.java b/ext/jni/src/org/sqlite/jni/sqlite3_blob.java new file mode 100644 index 0000000000..e512130256 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/sqlite3_blob.java @@ -0,0 +1,25 @@ +/* +** 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; + +/** + 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 { + // Only invoked from JNI. + private sqlite3_blob(){} +} diff --git a/ext/wasm/SQLTester/index.html b/ext/wasm/SQLTester/index.html index cd1423ac37..1dffad63e2 100644 --- a/ext/wasm/SQLTester/index.html +++ b/ext/wasm/SQLTester/index.html @@ -96,6 +96,13 @@ logClass('','index.html:',...args); return f; }; + + const timerId = setTimeout( ()=>{ + logClass('error',"The SQLTester module is taking an unusually ", + "long time to load. More information may be available", + "in the dev console."); + }, 3000 /* assuming localhost */ ); + W.onmessage = function({data}){ switch(data.type){ case 'stdout': log2(data.payload.message); break; @@ -105,6 +112,7 @@ log("test results:",data.payload); break; case 'is-ready': + clearTimeout(timerId); runTests(); break; default: log("unhandled onmessage",data); diff --git a/manifest b/manifest index 5855b8a88b..66108b8925 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\ssupport\sfor\sthe\ssqlite3_get_clientdata()\sand\ssqlite3_set_clientdata()\ninterfaces,\sto\sbetter\ssupport\sJNI. -D 2023-09-11T14:55:05.474 +C Merge\sin\sJNI's\ssignificant\sdivergence\ssince\sit\sadopted\ssqlite3_client_data(). +D 2023-09-11T15:20:15.135 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -235,52 +235,54 @@ F ext/fts5/tool/showfts5.tcl d54da0e067306663e2d5d523965ca487698e722c F ext/icu/README.txt 7ab7ced8ae78e3a645b57e78570ff589d4c672b71370f5aa9e1cd7024f400fc9 F ext/icu/icu.c c074519b46baa484bb5396c7e01e051034da8884bad1a1cb7f09bbe6be3f0282 F ext/icu/sqliteicu.h fa373836ed5a1ee7478bdf8a1650689294e41d0c89c1daab26e9ae78a32075a8 -F ext/jni/GNUmakefile 374873bf6d2cd6ceafb458e28b59140dbb074f01f7adddf7e15a3ee3daf44551 +F ext/jni/GNUmakefile 3c82b1333ab4c8224787a490e80ea000a8f97bcb53bf9c9d11e095da1ae862f0 F ext/jni/README.md 1332b1fa27918bd5d9ca2d0d4f3ac3a6ab86b9e3699dc5bfe32904a027f3d2a9 F ext/jni/jar-dist.make 030aaa4ae71dd86e4ec5e7c1e6cd86f9dfa47c4592c070d2e35157e42498e1fa -F ext/jni/src/c/sqlite3-jni.c aaec2851258a7d9c9907d8e864a17e055676ec0adb64f335d979fa19674a0cab -F ext/jni/src/c/sqlite3-jni.h 12e1a5ef5ee1795dc22577c285b4518dfd8aa4af45757f6cb81a555d967bf201 +F ext/jni/src/c/sqlite3-jni.c 438a0141b7be7e8081f23254efe4cb313170aeb0f12fc5dd2c5a42612111bfad +F ext/jni/src/c/sqlite3-jni.h c1b34759d1a1f18ba7659ee1b5d518b5865f7f2f30685216910a149d038cdd05 F ext/jni/src/org/sqlite/jni/AbstractCollationCallback.java 95e88ba04f4aac51ffec65693e878e234088b2f21b387f4e4285c8b72b33e436 F ext/jni/src/org/sqlite/jni/AggregateFunction.java 7312486bc65fecdb91753c0a4515799194e031f45edbe16a6373cea18f404dc4 -F ext/jni/src/org/sqlite/jni/AuthorizerCallback.java d00a2409ab76cae168927e2ca6a7ffbd0621a42547cce88768b4eeebc13827e0 -F ext/jni/src/org/sqlite/jni/AutoExtensionCallback.java 1470e14d09f10729d35568506c6e61318edfb17aa322802e386764fa6d582f14 -F ext/jni/src/org/sqlite/jni/BusyHandlerCallback.java cd12c26dafd3e6c097fc73f80d328aebac0f58b985f66a96ee567ddf8d195f30 -F ext/jni/src/org/sqlite/jni/CollationCallback.java 7d5b246f1a7c9d6b8e974d970bbbb2d05c6264e65448d7be6a85edbf703c823d -F ext/jni/src/org/sqlite/jni/CollationNeededCallback.java 1707b50146c6b805b79e84f89a57c8dbb0134e431799f041f0bec403eca5f841 -F ext/jni/src/org/sqlite/jni/CommitHookCallback.java e4de82c97560982e996e358958268e1e4e307b6115cd9aac0ff4f947d4380d90 +F ext/jni/src/org/sqlite/jni/AuthorizerCallback.java e6135be32f12bf140bffa39be7fd1a45ad83b2661ed49c08dbde04c8485feb38 +F ext/jni/src/org/sqlite/jni/AutoExtensionCallback.java 5e4a75611c026730289d776469d6122cb2699d6970af5f53fe85e74d49930476 +F ext/jni/src/org/sqlite/jni/BusyHandlerCallback.java d316373b12b3bf1a421f1f7eed08128fa8dd52bb98617ba28c161aaabd71d1ee +F ext/jni/src/org/sqlite/jni/CallbackProxy.java 064a8a00e4c63cc501c30504f93ca996d422c5f010067f969b2d0a10f0868153 w ext/jni/src/org/sqlite/jni/SQLite3CallbackProxy.java +F ext/jni/src/org/sqlite/jni/CollationCallback.java df327348e1a34ee65210208d694d690e5ee0bfe901410122e07caf6c98b2b7c8 +F ext/jni/src/org/sqlite/jni/CollationNeededCallback.java 07df5fa161a0b81154295258037f662e7c372735c2899c76e81cb3abd9fd3b39 +F ext/jni/src/org/sqlite/jni/CommitHookCallback.java 77cf8bb4f5548113e9792978f3f8a454614f420fa0ad73939421cbff4e7776f2 +F ext/jni/src/org/sqlite/jni/ConfigLogCallback.java 636ed6b89ed03f15bc2a6f6f47bf7853b8328e5a8269e52e80630708efa703a6 F ext/jni/src/org/sqlite/jni/ConfigSqllogCallback.java e3656909eab7ed0f7e457c5b82df160ca22dd5e954c0a306ec1fca61b0d266b4 -F ext/jni/src/org/sqlite/jni/Fts5.java 3ebfbd5b95fdb9d7bc40306f2e682abd12e247d9224e92510b8dd103b4f96fe8 -F ext/jni/src/org/sqlite/jni/Fts5Context.java 0a5a02047a6a1dd3e4a38b0e542a8dd2de365033ba30e6ae019a676305959890 -F ext/jni/src/org/sqlite/jni/Fts5ExtensionApi.java 2fd11abb7c5403318181d69bb7b702a79cba7ab460105140f5161bea9bc505d1 -F ext/jni/src/org/sqlite/jni/Fts5Function.java 65cde7151e441fee012250a5e03277de7babcd11a0c308a832b7940574259bcc -F ext/jni/src/org/sqlite/jni/Fts5PhraseIter.java 6642beda341c0b1b46af4e2d7f6f9ab03a7aede43277b2c92859176d6bce3be9 -F ext/jni/src/org/sqlite/jni/Fts5Tokenizer.java 91489893596b6528c0df5cd7180bd5b55809c26e2b797fb321dfcdbc1298c060 F ext/jni/src/org/sqlite/jni/NativePointerHolder.java 564087036449a16df148dcf0a067408bd251170bf23286c655f46b5f973e8b2d -F ext/jni/src/org/sqlite/jni/OutputPointer.java 4ae06135decef35eb04498daa2868939d91a294e948747c580ef9ce31563a6b3 -F ext/jni/src/org/sqlite/jni/PreupdateHookCallback.java 500c968b3893edbddf67e8eb773852c3a8ae58097a77bd22320ada6b1af06db1 -F ext/jni/src/org/sqlite/jni/ProgressHandlerCallback.java 0da841810319f5a9dc372d0f2348930d54fac1a4b53e4298884f44c720d67830 +F ext/jni/src/org/sqlite/jni/OutputPointer.java 2f57c05672ddc9b38e3f8eed11759896cf0bf01107ffd24d5182b99f6e7254b6 +F ext/jni/src/org/sqlite/jni/PreupdateHookCallback.java eccaed8dc9c6289f07ef3fc109891c6be1e7cc6c88723d90174b68706fc21cda +F ext/jni/src/org/sqlite/jni/ProgressHandlerCallback.java 7b9ff2218129ece98ba60c57eeedcd8447e9e3b6e5d0f5e5d3eb0f0c5037d48d F ext/jni/src/org/sqlite/jni/ResultCode.java ba701f20213a5f259e94cfbfdd36eb7ac7ce7797f2c6c7fca2004ff12ce20f86 -F ext/jni/src/org/sqlite/jni/RollbackHookCallback.java 16042be9d072a26dbb2f1b1b63e7639989b747bb80d2bd667ba4f7555f56a825 +F ext/jni/src/org/sqlite/jni/RollbackHookCallback.java d12352c0e22840de484ffa9b11ed5058bb0daca2e9f218055d3c54c947a273c4 F ext/jni/src/org/sqlite/jni/SQLFunction.java 544a875d33fd160467d82e2397ac33157b29971d715a821a4fad3c899113ee8c -F ext/jni/src/org/sqlite/jni/SQLite3CallbackProxy.java c2748ab52856075b053a55b317988d95dc7fb4d3d42520f8c33573effe1cd185 -F ext/jni/src/org/sqlite/jni/SQLite3Jni.java 440d64e8c4cff53bd3c0cc676381212489198302d7f1aaa535712c2d7163cc69 +F ext/jni/src/org/sqlite/jni/SQLite3Jni.java a1beb9f9953bfaf76824239d8f556c81a3d8ea81f89307817e17d2cb0cbf7c9c F ext/jni/src/org/sqlite/jni/ScalarFunction.java 6d387bb499fbe3bc13c53315335233dbf6a0c711e8fa7c521683219b041c614c -F ext/jni/src/org/sqlite/jni/Tester1.java 21d78aa59bfc5ce5ff242d4bb6f6d2255d162fba8be5859ab87c9201d61433f0 -F ext/jni/src/org/sqlite/jni/TesterFts5.java 6f135c60e24c89e8eecb9fe61dde0f3bb2906de668ca6c9186bcf34bdaf94629 -F ext/jni/src/org/sqlite/jni/TraceV2Callback.java 641926b05a772c2c05c842a81aa839053ba4a13b78ef04b402f5705d060c6246 -F ext/jni/src/org/sqlite/jni/UpdateHookCallback.java be2bc96ff4f56b3c1fd18ae7dba9b207b25b6c123b8a5fd2f7aaf3cc208d8b7d -F ext/jni/src/org/sqlite/jni/ValueHolder.java f022873abaabf64f3dd71ab0d6037c6e71cece3b8819fa10bf26a5461dc973ee +F ext/jni/src/org/sqlite/jni/TableColumnMetadata.java 54511b4297fa28dcb3f49b24035e34ced10e3fd44fd0e458e784f4d6b0096dab +F ext/jni/src/org/sqlite/jni/Tester1.java a14f34e0404179b2920c20bca79ae178c71130e7c53aabe9a9e5c717850d2dfd +F ext/jni/src/org/sqlite/jni/TraceV2Callback.java beb0b064c1a5f8bfe585a324ed39a4e33edbe379a3fc60f1401661620d3ca7c0 +F ext/jni/src/org/sqlite/jni/UpdateHookCallback.java 8376f4a931f2d5612b295c003c9515ba933ee76d8f95610e89c339727376e36c F ext/jni/src/org/sqlite/jni/WindowFunction.java 488980f4dbb6bdd7067d6cb9c43e4075475e51c54d9b74a5834422654b126246 F ext/jni/src/org/sqlite/jni/XDestroyCallback.java 50c5ca124ef6c6b735a7e136e7a23a557be367e61b56d4aab5777a614ab46cc2 -F ext/jni/src/org/sqlite/jni/annotation/Canonical.java e55b82c8259b617ff754ac493fd8b79602631d659b87a858b987540e4c4fdf56 -F ext/jni/src/org/sqlite/jni/annotation/NotNull.java d48ebd7ae6bbb78bd47d54431c85e1521c89b1d3864a2b6eafd9c0e1b2341457 +F ext/jni/src/org/sqlite/jni/annotation/Canonical.java 2767daa5b3893b96729db80a0f8234d379d266d1b2c21400a057864b538a0ea5 +F ext/jni/src/org/sqlite/jni/annotation/NotNull.java d9b32956cb9fb11d1f8a562e5df70d0599820265285120c63858294dbe2b7711 F ext/jni/src/org/sqlite/jni/annotation/Nullable.java 6f962a98c9a5c6e9d21c50ae8716b16bdfdc934a191608cbb7e12ea588ddb6af F ext/jni/src/org/sqlite/jni/annotation/package-info.java f66bfb621c6494e67c03ed38a9e26a3bd6af99b9f9f6ef79556bcec30a025a22 -F ext/jni/src/org/sqlite/jni/fts5_api.java ee47f1837d32968f7bb62278c7504c0fb572a68ec107371b714578312e9f734b -F ext/jni/src/org/sqlite/jni/fts5_extension_function.java ac825035d7d83fc7fd960347abfa6803e1614334a21533302041823ad5fc894c -F ext/jni/src/org/sqlite/jni/fts5_tokenizer.java a92c2e55bda492e4c76d48ddc73369bcc0d5e8727940840f9339e3292ea58fa7 -F ext/jni/src/org/sqlite/jni/package-info.java 73f7821c240e4d116f164e87b613c5836b8a33ce2666967a29d9acb1ced7ca92 +F ext/jni/src/org/sqlite/jni/fts5/Fts5.java 3268fc61a15da5682e4e2130adbb1ae5385393c2f8a2ed408af806894b4a0625 w ext/jni/src/org/sqlite/jni/Fts5.java +F ext/jni/src/org/sqlite/jni/fts5/Fts5Context.java 7058da97059b8e156c17561a47ecd7faa0fc3e2d8c2588b9a28dbff8d06202dd w ext/jni/src/org/sqlite/jni/Fts5Context.java +F ext/jni/src/org/sqlite/jni/fts5/Fts5ExtensionApi.java 3c337e5690c4de7f0d5478f53ca7ba326e776330eb511e607bd252a35b84d8f7 w ext/jni/src/org/sqlite/jni/Fts5ExtensionApi.java +F ext/jni/src/org/sqlite/jni/fts5/Fts5PhraseIter.java 2a7f3d76a1206e6a43d4c4ed9609b294d5431cc7d8fb875d8419f76efa6e56dc w ext/jni/src/org/sqlite/jni/Fts5PhraseIter.java +F ext/jni/src/org/sqlite/jni/fts5/Fts5Tokenizer.java cc9a53846a168a215238af224c31cef0e8379780e36e8a5e743b00c08145cf19 w ext/jni/src/org/sqlite/jni/Fts5Tokenizer.java +F ext/jni/src/org/sqlite/jni/fts5/TesterFts5.java b5a805c2ecff5c89179133fd2a6ede90b8598796e3940afae4590c7b503d3917 w ext/jni/src/org/sqlite/jni/TesterFts5.java +F ext/jni/src/org/sqlite/jni/fts5/fts5_api.java 6071bf76c2c6a0f035b99adc76715b0324f540a441452b4ff6b94d9360a6a83d w ext/jni/src/org/sqlite/jni/fts5_api.java +F ext/jni/src/org/sqlite/jni/fts5/fts5_extension_function.java 1fe0f5692c1d67475d12b067f0469949073446f18c56eba5ee5da6ddd06db9b9 w ext/jni/src/org/sqlite/jni/fts5_extension_function.java +F ext/jni/src/org/sqlite/jni/fts5/fts5_tokenizer.java 4b56f407977f08e456ea0c956fb657b4cd96ec4635cc4f7a1b7ef10cb2a21d7d w ext/jni/src/org/sqlite/jni/fts5_tokenizer.java +F ext/jni/src/org/sqlite/jni/package-info.java a3946db2504de747a1993c4f6e8ce604bec5a8e5a134b292c3b07527bc321a99 F ext/jni/src/org/sqlite/jni/sqlite3.java 62b1b81935ccf3393472d17cb883dc5ff39c388ec3bc1de547f098a0217158fc +F ext/jni/src/org/sqlite/jni/sqlite3_backup.java d0bb06dd6225e76999ff6b7ab20f2643b1c4d4167431b3a93ea41943e41f094b +F ext/jni/src/org/sqlite/jni/sqlite3_blob.java f28a30134f2e524eb7d5ab87f57f86c90140341a6e8369ee54509ac8bb96fa82 F ext/jni/src/org/sqlite/jni/sqlite3_context.java 66ca95ce904044263a4aff684abe262d56f73e6b06bca6cf650761d79d7779ad F ext/jni/src/org/sqlite/jni/sqlite3_stmt.java 78e6d1b95ac600a9475e9db4623f69449322b0c93d1bd4e1616e76ed547ed9fc F ext/jni/src/org/sqlite/jni/sqlite3_value.java 3d1d4903e267bc0bc81d57d21f5e85978eff389a1a6ed46726dbe75f85e6914a @@ -552,7 +554,7 @@ F ext/wasm/README.md a8a2962c3aebdf8d2104a9102e336c5554e78fc6072746e5daf9c61514e F ext/wasm/SQLTester/GNUmakefile e0794f676d55819951bbfae45cc5e8d7818dc460492dc317ce7f0d2eca15caff F ext/wasm/SQLTester/SQLTester.mjs ec2f6ba63a0f2f0562941a0fb8e46b7dc55589711513f1952349785966edfe50 F ext/wasm/SQLTester/SQLTester.run.mjs c72b7fe2072d05992f7a3d8c6a1d34e95712513ceabe40849784e24e41c84638 -F ext/wasm/SQLTester/index.html 3a12895015c165281307eb47786ce3c46b3c3f06383ad6a9fe3a8526105632f1 +F ext/wasm/SQLTester/index.html 3f8a016df0776be76605abf20e815ecaafbe055abac0e1fe5ea080e7846b760d F ext/wasm/SQLTester/touint8array.c 2d5ece04ec1393a6a60c4bf96385bda5e1a10ad49f3038b96460fc5e5aa7e536 F ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api d6a5078f48a5301ed17b9a30331075d9b2506e1360c1f0dee0c7816c10acd9ab F ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-see fb29e62082a658f0d81102488414d422c393c4b20cc2f685b216bc566237957b @@ -2117,9 +2119,9 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 71548f72ad578db3d6ee409956386ace23ab2258d37cdc29ef2fb126e48f1ee3 7b884832b71c23e62ba3c0d53f7c89199734c351f909d84ac19ac18c7ddccbd8 -R ed133595420eb79056e6b2e7f8f7826c -T +closed 7b884832b71c23e62ba3c0d53f7c89199734c351f909d84ac19ac18c7ddccbd8 -U drh -Z 4388a2fb0fa820775dbaf90786b95f10 +P 9806c0dd2802d68b67c25c4f3347ed532f9a98b051e775d34e9182dd2f099891 caceb52fb58f924e7cbbebac05e7fd7822b7f6f56a3f834392d2be801bf04633 +R c483b1d540b87b26a025edef658546dc +T +closed caceb52fb58f924e7cbbebac05e7fd7822b7f6f56a3f834392d2be801bf04633 Closed\sby\sintegrate-merge. +U stephan +Z d9fe1d695ebe43d0eee6d17283d420b9 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 483c8fee2d..c0a179c50e 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -9806c0dd2802d68b67c25c4f3347ed532f9a98b051e775d34e9182dd2f099891 \ No newline at end of file +e8e07dbbeaa7799eb0b90726e18e994aaf8c8d316ab4bdb06da732bc51cf0919 \ No newline at end of file