1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-29 08:01:23 +03:00

Correct a mis-cast in the JNI wrapper which just happened to accidentally work. Update JNI binding of sqlite3_context_db_handle() to return the bound-at-open() db instance instead of a new/temp proxy object.

FossilOrigin-Name: 9faca5d9ed4a749421e08bd1da8b7672c0fd31366124fdb613c46e19dece0fc1
This commit is contained in:
stephan
2023-07-31 10:55:30 +00:00
parent ede8900912
commit 8f714e21a0
5 changed files with 32 additions and 21 deletions

View File

@ -748,18 +748,29 @@ static void PerDbStateJni_dump(PerDbStateJni *s){
true then a new instance will be allocated if no mapping currently
exists, else NULL is returned if no mapping is found.
The 3rd and 4th args should only be non-0 for
sqlite3_open(_v2)(). For all other cases, they must be 0, in which
The 3rd and 4th args should normally only be non-0 for
sqlite3_open(_v2)(). For most other cases, they must be 0, in which
case the db handle will be fished out of the jDb object and NULL is
returned if jDb does not have any associated PerDbStateJni.
If called with a NULL jDb and non-NULL pDb then allocIfNeeded MUST
be false and it will look for a matching db object. That usage is
required for functions, like sqlite3_context_db_handle(), which
return a (sqlite3*).
*/
FIXME_THREADING
static PerDbStateJni * PerDbStateJni_for_db(JNIEnv *env, jobject jDb, sqlite3 *pDb, int allocIfNeeded){
PerDbStateJni * s = S3Global.perDb.aUsed;
if(!jDb) return 0;
if(!jDb){
if(pDb){
assert(!allocIfNeeded);
}else{
return 0;
}
}
assert(allocIfNeeded ? !!pDb : 1);
if(!allocIfNeeded && !pDb){
pDb = PtrGet_sqlite3_value(jDb);
pDb = PtrGet_sqlite3(jDb);
}
for( ; pDb && s; s = s->pNext){
if(s->pDb == pDb) return s;
@ -1024,10 +1035,6 @@ static jobject new_sqlite3_context_wrapper(JNIEnv *env, sqlite3_context *sv){
return new_NativePointerHolder_object(env, "org/sqlite/jni/sqlite3_context", sv);
}
static jobject new_sqlite3_wrapper(JNIEnv *env, sqlite3 *sv){
return new_NativePointerHolder_object(env, "org/sqlite/jni/sqlite3", sv);
}
enum UDFType {
UDF_SCALAR = 1,
UDF_AGGREGATE,
@ -1670,8 +1677,9 @@ JDECL(jboolean,1compileoption_1used)(JENV_JSELF, jstring name){
}
JDECL(jobject,1context_1db_1handle)(JENV_JSELF, jobject jpCx){
sqlite3 * const db = sqlite3_context_db_handle(PtrGet_sqlite3_context(jpCx));
return db ? new_sqlite3_wrapper(env, db) : NULL;
sqlite3 * const pDb = sqlite3_context_db_handle(PtrGet_sqlite3_context(jpCx));
PerDbStateJni * const ps = pDb ? PerDbStateJni_for_db(env, 0, pDb, 0) : 0;
return ps ? ps->jDb : 0;
}
JDECL(jint,1create_1collation)(JENV_JSELF, jobject jDb,

View File

@ -271,6 +271,10 @@ public final class SQLite3Jni {
public static native int sqlite3_collation_needed(@NotNull sqlite3 db,
@Nullable CollationNeeded callback);
/**
Returns the db handle passed to sqlite3_open() or
sqlite3_open_v2(), as opposed to a new wrapper object.
*/
public static native sqlite3 sqlite3_context_db_handle(@NotNull sqlite3_context cx);
public static native CommitHook sqlite3_commit_hook(@NotNull sqlite3 db, @Nullable CommitHook hook);

View File

@ -457,8 +457,7 @@ public class Tester1 {
// Java...
new SQLFunction.Scalar(){
public void xFunc(sqlite3_context cx, sqlite3_value[] args){
affirm(db.getNativePointer()
== sqlite3_context_db_handle(cx).getNativePointer());
affirm(db == sqlite3_context_db_handle(cx));
int result = 0;
for( sqlite3_value v : args ) result += sqlite3_value_int(v);
xFuncAccum.value += result;// just for post-run testing