Bug Summary

File:sqlite_jni.c
Location:line 2841, column 7
Description:Value stored to 'blob' is never read

Annotated Source Code

1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4
5#if HAVE_SQLITE20
6#include "sqlite.h"
7#endif
8
9#if HAVE_SQLITE31
10#include "sqlite3.h"
11#undef HAVE_SQLITE_COMPILE1
12#define HAVE_SQLITE_COMPILE1 1
13#undef HAVE_SQLITE_PROGRESS_HANDLER1
14#define HAVE_SQLITE_PROGRESS_HANDLER1 1
15#undef HAVE_SQLITE_TRACE1
16#define HAVE_SQLITE_TRACE1 1
17#if !HAVE_SQLITE3_MALLOC1
18#define sqlite3_malloc malloc
19#define sqlite3_free free
20#endif
21#if !HAVE_SQLITE3_BIND_PARAMETER_COUNT1
22#define sqlite3_bind_parameter_count(dummy) (1000)
23#endif
24#endif
25
26#if HAVE_SQLITE20 && HAVE_SQLITE31
27#define HAVE_BOTH_SQLITE 1
28#endif
29
30#ifndef HAVE_SQLITE3_SHARED_CACHE1
31#define HAVE_SQLITE3_SHARED_CACHE1 0
32#endif
33
34#include "sqlite_jni.h"
35
36#if defined(_WIN32) || !defined(CANT_PASS_VALIST_AS_CHARPTR1)
37#define MAX_PARAMS32 256
38#else
39#define MAX_PARAMS32 32
40#endif
41
42/* free memory proc */
43
44typedef void (freemem)(void *);
45
46/* internal handle for SQLite database */
47
48typedef struct {
49 void *sqlite; /* SQLite handle */
50#if HAVE_BOTH_SQLITE
51 int is3; /* True for SQLITE3 handle */
52#endif
53 int ver; /* version code */
54 jobject bh; /* BusyHandler object */
55 jobject cb; /* Callback object */
56 jobject ai; /* Authorizer object */
57 jobject tr; /* Trace object */
58 jobject pr; /* Profile object */
59 jobject ph; /* ProgressHandler object */
60 JNIEnv *env; /* Java environment for callbacks */
61 int row1; /* true while processing first row */
62 int haveutf; /* true for SQLite UTF-8 support */
63 jstring enc; /* encoding or 0 */
64 struct hfunc *funcs; /* SQLite user defined function handles */
65#if HAVE_SQLITE_COMPILE1
66 struct hvm *vms; /* Compiled SQLite VMs */
67#endif
68#if HAVE_SQLITE31
69 sqlite3_stmt *stmt; /* For callback() */
70#endif
71#if HAVE_SQLITE31 && HAVE_SQLITE3_INCRBLOBIO1
72 struct hbl *blobs; /* SQLite3 blob handles */
73#endif
74#if HAVE_SQLITE31 && HAVE_SQLITE3_BACKUPAPI1
75 struct hbk *backups; /* SQLite3 backup handles */
76#endif
77} handle;
78
79/* internal handle for SQLite user defined function */
80
81typedef struct hfunc {
82 struct hfunc *next; /* next function */
83#if HAVE_BOTH_SQLITE
84 int is3; /* True for SQLITE3 handle */
85#endif
86 jobject fc; /* FunctionContext object */
87 jobject fi; /* Function object */
88 jobject db; /* Database object */
89 handle *h; /* SQLite database handle */
90 void *sf; /* SQLite function handle */
91 JNIEnv *env; /* Java environment for callbacks */
92} hfunc;
93
94#if HAVE_SQLITE_COMPILE1
95/* internal handle for SQLite VM (sqlite_compile()) */
96
97typedef struct hvm {
98 struct hvm *next; /* next vm handle */
99#if HAVE_BOTH_SQLITE
100 int is3; /* True for SQLITE3 handle */
101#endif
102 void *vm; /* SQLite 2/3 VM/statement */
103 char *tail; /* tail SQL string */
104 int tail_len; /* only for SQLite3/prepare */
105 handle *h; /* SQLite database handle */
106 handle hh; /* fake SQLite database handle */
107} hvm;
108#endif
109
110#if HAVE_SQLITE31 && HAVE_SQLITE3_INCRBLOBIO1
111/* internal handle for sqlite3_blob */
112
113typedef struct hbl {
114 struct hbl *next; /* next blob handle */
115 sqlite3_blob *blob; /* SQLite3 blob */
116 handle *h; /* SQLite database handle */
117} hbl;
118#endif
119
120#if HAVE_SQLITE31 && HAVE_SQLITE3_BACKUPAPI1
121/* internal handle for sqlite3_backup */
122
123typedef struct hbk {
124 struct hbk *next; /* next blob handle */
125 sqlite3_backup *bkup; /* SQLite3 backup handle */
126 handle *h; /* SQLite database handle (source) */
127} hbk;
128#endif
129
130/* ISO to/from UTF-8 translation */
131
132typedef struct {
133 char *result; /* translated C string result */
134 char *tofree; /* memory to be free'd, or 0 */
135 jstring jstr; /* resulting Java string or 0 */
136} transstr;
137
138/* static cached weak class refs, field and method ids */
139
140static jclass C_java_lang_String = 0;
141static jclass C_SQLite_Database = 0;
142
143static jfieldID F_SQLite_Database_handle = 0;
144static jfieldID F_SQLite_Database_error_code = 0;
145static jfieldID F_SQLite_FunctionContext_handle = 0;
146static jfieldID F_SQLite_Vm_handle = 0;
147static jfieldID F_SQLite_Vm_error_code = 0;
148static jfieldID F_SQLite_Stmt_handle = 0;
149static jfieldID F_SQLite_Stmt_error_code = 0;
150static jfieldID F_SQLite_Blob_handle = 0;
151static jfieldID F_SQLite_Blob_size = 0;
152static jfieldID F_SQLite_Backup_handle = 0;
153
154static jmethodID M_java_lang_String_getBytes = 0;
155static jmethodID M_java_lang_String_getBytes2 = 0;
156static jmethodID M_java_lang_String_initBytes = 0;
157static jmethodID M_java_lang_String_initBytes2 = 0;
158
159static const char xdigits[] = "0123456789ABCDEF";
160
161static void
162seterr(JNIEnv *env, jobject obj, int err)
163{
164 jvalue v;
165
166 v.j = 0;
167 v.i = (jint) err;
168 (*env)->SetIntField(env, obj, F_SQLite_Database_error_code, v.i);
169}
170
171#if HAVE_SQLITE_COMPILE1
172static void
173setvmerr(JNIEnv *env, jobject obj, int err)
174{
175 jvalue v;
176
177 v.j = 0;
178 v.i = (jint) err;
179 (*env)->SetIntField(env, obj, F_SQLite_Vm_error_code, v.i);
180}
181
182#if HAVE_SQLITE31
183static void
184setstmterr(JNIEnv *env, jobject obj, int err)
185{
186 jvalue v;
187
188 v.j = 0;
189 v.i = (jint) err;
190 (*env)->SetIntField(env, obj, F_SQLite_Stmt_error_code, v.i);
191}
192
193static int
194jstrlen(const jchar *jstr)
195{
196 int len = 0;
197
198 if (jstr) {
199 while (*jstr++) {
200 len++;
201 }
202 }
203 return len;
204}
205#endif
206#endif
207
208static void *
209gethandle(JNIEnv *env, jobject obj)
210{
211 jvalue v;
212
213 v.j = (*env)->GetLongField(env, obj, F_SQLite_Database_handle);
214 return (void *) v.l;
215}
216
217static void *
218getclrhandle(JNIEnv *env, jobject obj)
219{
220 jvalue v;
221
222 if ((*env)->MonitorEnter(env, obj) != JNI_OK0) {
223 fprintf(stderrstderr, "getclrhandle: MonitorEnter failed\n");
224 return 0;
225 }
226 v.j = (*env)->GetLongField(env, obj, F_SQLite_Database_handle);
227 (*env)->SetLongField(env, obj, F_SQLite_Database_handle, 0);
228 (*env)->MonitorExit(env, obj);
229 return (void *) v.l;
230}
231
232#if HAVE_SQLITE_COMPILE1
233static void *
234gethvm(JNIEnv *env, jobject obj)
235{
236 jvalue v;
237
238 v.j = (*env)->GetLongField(env, obj, F_SQLite_Vm_handle);
239 return (void *) v.l;
240}
241
242static void *
243getclrhvm(JNIEnv *env, jobject obj)
244{
245 jvalue v;
246
247 if ((*env)->MonitorEnter(env, obj) != JNI_OK0) {
248 fprintf(stderrstderr, "getclrhvm: MonitorEnter failed\n");
249 return 0;
250 }
251 v.j = (*env)->GetLongField(env, obj, F_SQLite_Vm_handle);
252 (*env)->SetLongField(env, obj, F_SQLite_Vm_handle, 0);
253 (*env)->MonitorExit(env, obj);
254 return (void *) v.l;
255}
256
257#if HAVE_SQLITE31
258static void *
259gethstmt(JNIEnv *env, jobject obj)
260{
261 jvalue v;
262
263 v.j = (*env)->GetLongField(env, obj, F_SQLite_Stmt_handle);
264 return (void *) v.l;
265}
266
267static void *
268getclrhstmt(JNIEnv *env, jobject obj)
269{
270 jvalue v;
271
272 if ((*env)->MonitorEnter(env, obj) != JNI_OK0) {
273 fprintf(stderrstderr, "getclrhstmt: MonitorEnter failed\n");
274 return 0;
275 }
276 v.j = (*env)->GetLongField(env, obj, F_SQLite_Stmt_handle);
277 (*env)->SetLongField(env, obj, F_SQLite_Stmt_handle, 0);
278 (*env)->MonitorExit(env, obj);
279 return (void *) v.l;
280}
281#endif
282#endif
283
284#if HAVE_SQLITE31 && HAVE_SQLITE3_INCRBLOBIO1
285static void *
286gethbl(JNIEnv *env, jobject obj)
287{
288 jvalue v;
289
290 v.j = (*env)->GetLongField(env, obj, F_SQLite_Blob_handle);
291 return (void *) v.l;
292}
293
294static void *
295getclrhbl(JNIEnv *env, jobject obj)
296{
297 jvalue v;
298
299 if ((*env)->MonitorEnter(env, obj) != JNI_OK0) {
300 fprintf(stderrstderr, "getclrhbl: MonitorEnter failed\n");
301 return 0;
302 }
303 v.j = (*env)->GetLongField(env, obj, F_SQLite_Blob_handle);
304 (*env)->SetLongField(env, obj, F_SQLite_Blob_handle, 0);
305 (*env)->MonitorExit(env, obj);
306 return (void *) v.l;
307}
308#endif
309
310#if HAVE_SQLITE31 && HAVE_SQLITE3_BACKUPAPI1
311static void *
312gethbk(JNIEnv *env, jobject obj)
313{
314 jvalue v;
315
316 v.j = (*env)->GetLongField(env, obj, F_SQLite_Backup_handle);
317 return (void *) v.l;
318}
319
320static void *
321getclrhbk(JNIEnv *env, jobject obj)
322{
323 jvalue v;
324
325 if ((*env)->MonitorEnter(env, obj) != JNI_OK0) {
326 fprintf(stderrstderr, "getclrhbk: MonitorEnter failed\n");
327 return 0;
328 }
329 v.j = (*env)->GetLongField(env, obj, F_SQLite_Backup_handle);
330 (*env)->SetLongField(env, obj, F_SQLite_Backup_handle, 0);
331 (*env)->MonitorExit(env, obj);
332 return (void *) v.l;
333}
334#endif
335
336static void
337delglobrefp(JNIEnv *env, jobject *obj)
338{
339 if (*obj) {
340 (*env)->DeleteGlobalRef(env, *obj);
341 *obj = 0;
342 }
343}
344
345static jobject
346globrefpop(JNIEnv *env, jobject *obj)
347{
348 jobject ret = 0;
349
350 if (*obj) {
351 ret = *obj;
352 *obj = 0;
353 }
354 return ret;
355}
356
357static void
358globrefset(JNIEnv *env, jobject obj, jobject *ref)
359{
360 if (ref) {
361 if (obj) {
362 *ref = (*env)->NewGlobalRef(env, obj);
363 } else {
364 *ref = 0;
365 }
366 }
367}
368
369static void
370freep(char **strp)
371{
372 if (strp && *strp) {
373 free(*strp);
374 *strp = 0;
375 }
376}
377
378static void
379throwex(JNIEnv *env, const char *msg)
380{
381 jclass except = (*env)->FindClass(env, "SQLite/Exception");
382
383 (*env)->ExceptionClear(env);
384 if (except) {
385 (*env)->ThrowNew(env, except, msg);
386 }
387}
388
389static void
390throwoom(JNIEnv *env, const char *msg)
391{
392 jclass except = (*env)->FindClass(env, "java/lang/OutOfMemoryError");
393
394 (*env)->ExceptionClear(env);
395 if (except) {
396 (*env)->ThrowNew(env, except, msg);
397 }
398}
399
400static void
401throwclosed(JNIEnv *env)
402{
403 throwex(env, "database already closed");
404}
405
406#if HAVE_SQLITE31 && HAVE_SQLITE3_INCRBLOBIO1
407static void
408throwioex(JNIEnv *env, const char *msg)
409{
410 jclass except = (*env)->FindClass(env, "java/io/IOException");
411
412 (*env)->ExceptionClear(env);
413 if (except) {
414 (*env)->ThrowNew(env, except, msg);
415 }
416}
417#endif
418
419static void
420transfree(transstr *dest)
421{
422 dest->result = 0;
423 freep(&dest->tofree);
424}
425
426static char *
427trans2iso(JNIEnv *env, int haveutf, jstring enc, jstring src,
428 transstr *dest)
429{
430 jbyteArray bytes = 0;
431 jthrowable exc;
432
433 dest->result = 0;
434 dest->tofree = 0;
435 if (haveutf) {
436#ifndef JNI_VERSION_1_20x00010002
437 const char *utf = (*env)->GetStringUTFChars(env, src, 0);
438
439 dest->result = dest->tofree = malloc(strlen(utf) + 1);
440#else
441 jsize utflen = (*env)->GetStringUTFLength(env, src);
442 jsize uclen = (*env)->GetStringLength(env, src);
443
444 dest->result = dest->tofree = malloc(utflen + 1);
445#endif
446 if (!dest->tofree) {
447 throwoom(env, "string translation failed");
448 return dest->result;
449 }
450#ifndef JNI_VERSION_1_20x00010002
451 strcpy(dest->result, utf);
452 (*env)->ReleaseStringUTFChars(env, src, utf);
453#else
454 (*env)->GetStringUTFRegion(env, src, 0, uclen, dest->result);
455 dest->result[utflen] = '\0';
456#endif
457 return dest->result;
458 }
459 if (enc) {
460 bytes = (*env)->CallObjectMethod(env, src,
461 M_java_lang_String_getBytes2, enc);
462 } else {
463 bytes = (*env)->CallObjectMethod(env, src,
464 M_java_lang_String_getBytes);
465 }
466 exc = (*env)->ExceptionOccurred(env);
467 if (!exc) {
468 jint len = (*env)->GetArrayLength(env, bytes);
469 dest->tofree = malloc(len + 1);
470 if (!dest->tofree) {
471 throwoom(env, "string translation failed");
472 return dest->result;
473 }
474 dest->result = dest->tofree;
475 (*env)->GetByteArrayRegion(env, bytes, 0, len, (jbyte *) dest->result);
476 dest->result[len] = '\0';
477 } else {
478 (*env)->DeleteLocalRef(env, exc);
479 }
480 return dest->result;
481}
482
483static jstring
484trans2utf(JNIEnv *env, int haveutf, jstring enc, const char *src,
485 transstr *dest)
486{
487 jbyteArray bytes = 0;
488 int len;
489
490 dest->result = 0;
491 dest->tofree = 0;
492 dest->jstr = 0;
493 if (!src) {
494 return dest->jstr;
495 }
496 if (haveutf) {
497 dest->jstr = (*env)->NewStringUTF(env, src);
498 return dest->jstr;
499 }
500 len = strlen(src);
501 bytes = (*env)->NewByteArray(env, len);
502 if (bytes) {
503 (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte *) src);
504 if (enc) {
505 dest->jstr =
506 (*env)->NewObject(env, C_java_lang_String,
507 M_java_lang_String_initBytes2, bytes, enc);
508 } else {
509 dest->jstr =
510 (*env)->NewObject(env, C_java_lang_String,
511 M_java_lang_String_initBytes, bytes);
512 }
513 (*env)->DeleteLocalRef(env, bytes);
514 return dest->jstr;
515 }
516 throwoom(env, "string translation failed");
517 return dest->jstr;
518}
519
520#if HAVE_SQLITE20
521static int
522busyhandler(void *udata, const char *table, int count)
523{
524 handle *h = (handle *) udata;
525 JNIEnv *env = h->env;
526 int ret = 0;
527
528 if (env && h->bh) {
529 transstr tabstr;
530 jclass cls = (*env)->GetObjectClass(env, h->bh);
531 jmethodID mid = (*env)->GetMethodID(env, cls, "busy",
532 "(Ljava/lang/String;I)Z");
533
534 if (mid == 0) {
535 (*env)->DeleteLocalRef(env, cls);
536 return ret;
537 }
538 trans2utf(env, h->haveutf, h->enc, table, &tabstr);
539 ret = (*env)->CallBooleanMethod(env, h->bh, mid, tabstr.jstr,
540 (jint) count)
541 != JNI_FALSE0;
542 (*env)->DeleteLocalRef(env, tabstr.jstr);
543 (*env)->DeleteLocalRef(env, cls);
544 }
545 return ret;
546}
547#endif
548
549#if HAVE_SQLITE31
550static int
551busyhandler3(void *udata, int count)
552{
553 handle *h = (handle *) udata;
554 JNIEnv *env = h->env;
555 int ret = 0;
556
557 if (env && h->bh) {
558 jclass cls = (*env)->GetObjectClass(env, h->bh);
559 jmethodID mid = (*env)->GetMethodID(env, cls, "busy",
560 "(Ljava/lang/String;I)Z");
561
562 if (mid == 0) {
563 (*env)->DeleteLocalRef(env, cls);
564 return ret;
565 }
566 ret = (*env)->CallBooleanMethod(env, h->bh, mid, 0, (jint) count)
567 != JNI_FALSE0;
568 (*env)->DeleteLocalRef(env, cls);
569 }
570 return ret;
571}
572#endif
573
574static int
575progresshandler(void *udata)
576{
577 handle *h = (handle *) udata;
578 JNIEnv *env = h->env;
579 int ret = 0;
580
581 if (env && h->ph) {
582 jclass cls = (*env)->GetObjectClass(env, h->ph);
583 jmethodID mid = (*env)->GetMethodID(env, cls, "progress", "()Z");
584
585 if (mid == 0) {
586 (*env)->DeleteLocalRef(env, cls);
587 return ret;
588 }
589 ret = (*env)->CallBooleanMethod(env, h->ph, mid) != JNI_TRUE1;
590 (*env)->DeleteLocalRef(env, cls);
591 }
592 return ret;
593}
594
595static int
596callback(void *udata, int ncol, char **data, char **cols)
597{
598 handle *h = (handle *) udata;
599 JNIEnv *env = h->env;
600
601 if (env && h->cb) {
602 jthrowable exc;
603 jclass cls = (*env)->GetObjectClass(env, h->cb);
604 jmethodID mid;
605 jobjectArray arr = 0;
606 jint i;
607
608 if (h->row1) {
609 mid = (*env)->GetMethodID(env, cls, "columns",
610 "([Ljava/lang/String;)V");
611
612 if (mid) {
613 arr = (*env)->NewObjectArray(env, ncol, C_java_lang_String, 0);
614 for (i = 0; i < ncol; i++) {
615 if (cols[i]) {
616 transstr col;
617
618 trans2utf(env, h->haveutf, h->enc, cols[i], &col);
619 (*env)->SetObjectArrayElement(env, arr, i, col.jstr);
620 exc = (*env)->ExceptionOccurred(env);
621 if (exc) {
622 (*env)->DeleteLocalRef(env, exc);
623 return 1;
624 }
625 (*env)->DeleteLocalRef(env, col.jstr);
626 }
627 }
628 h->row1 = 0;
629 (*env)->CallVoidMethod(env, h->cb, mid, arr);
630 exc = (*env)->ExceptionOccurred(env);
631 if (exc) {
632 (*env)->DeleteLocalRef(env, exc);
633 return 1;
634 }
635 (*env)->DeleteLocalRef(env, arr);
636 }
637#if HAVE_BOTH_SQLITE
638 if (h->is3) {
639 mid = (*env)->GetMethodID(env, cls, "types",
640 "([Ljava/lang/String;)V");
641
642 if (mid && h->stmt) {
643 arr = (*env)->NewObjectArray(env, ncol,
644 C_java_lang_String, 0);
645 for (i = 0; i < ncol; i++) {
646 const char *ctype =
647 sqlite3_column_decltype(h->stmt, i);
648
649 if (!ctype) {
650 switch (sqlite3_column_type(h->stmt, i)) {
651 case SQLITE_INTEGER1: ctype = "integer"; break;
652 case SQLITE_FLOAT2: ctype = "double"; break;
653 default:
654#if defined(SQLITE_TEXT3) && defined(SQLITE3_TEXT3) && (SQLITE_TEXT3 != SQLITE3_TEXT3)
655 case SQLITE_TEXT3:
656#else
657#ifdef SQLITE3_TEXT3
658 case SQLITE3_TEXT3:
659#endif
660#endif
661 ctype = "text"; break;
662 case SQLITE_BLOB4: ctype = "blob"; break;
663 case SQLITE_NULL5: ctype = "null"; break;
664 }
665 }
666 if (ctype) {
667 transstr ty;
668
669 trans2utf(env, 1, 0, ctype, &ty);
670 (*env)->SetObjectArrayElement(env, arr, i,
671 ty.jstr);
672 exc = (*env)->ExceptionOccurred(env);
673 if (exc) {
674 (*env)->DeleteLocalRef(env, exc);
675 return 1;
676 }
677 (*env)->DeleteLocalRef(env, ty.jstr);
678 }
679 }
680 (*env)->CallVoidMethod(env, h->cb, mid, arr);
681 exc = (*env)->ExceptionOccurred(env);
682 if (exc) {
683 (*env)->DeleteLocalRef(env, exc);
684 return 1;
685 }
686 (*env)->DeleteLocalRef(env, arr);
687 }
688 } else {
689 if (h->ver >= 0x020506 && cols[ncol]) {
690 mid = (*env)->GetMethodID(env, cls, "types",
691 "([Ljava/lang/String;)V");
692
693 if (mid) {
694 arr = (*env)->NewObjectArray(env, ncol,
695 C_java_lang_String, 0);
696 for (i = 0; i < ncol; i++) {
697 if (cols[i + ncol]) {
698 transstr ty;
699
700 trans2utf(env, h->haveutf, h->enc,
701 cols[i + ncol], &ty);
702 (*env)->SetObjectArrayElement(env, arr, i,
703 ty.jstr);
704 exc = (*env)->ExceptionOccurred(env);
705 if (exc) {
706 (*env)->DeleteLocalRef(env, exc);
707 return 1;
708 }
709 (*env)->DeleteLocalRef(env, ty.jstr);
710 }
711 }
712 (*env)->CallVoidMethod(env, h->cb, mid, arr);
713 exc = (*env)->ExceptionOccurred(env);
714 if (exc) {
715 (*env)->DeleteLocalRef(env, exc);
716 return 1;
717 }
718 (*env)->DeleteLocalRef(env, arr);
719 }
720 }
721 }
722#else
723#if HAVE_SQLITE20
724 if (h->ver >= 0x020506 && cols[ncol]) {
725 mid = (*env)->GetMethodID(env, cls, "types",
726 "([Ljava/lang/String;)V");
727
728 if (mid) {
729 arr = (*env)->NewObjectArray(env, ncol,
730 C_java_lang_String, 0);
731 for (i = 0; i < ncol; i++) {
732 if (cols[i + ncol]) {
733 transstr ty;
734
735 trans2utf(env, h->haveutf, h->enc,
736 cols[i + ncol], &ty);
737 (*env)->SetObjectArrayElement(env, arr, i,
738 ty.jstr);
739 exc = (*env)->ExceptionOccurred(env);
740 if (exc) {
741 (*env)->DeleteLocalRef(env, exc);
742 return 1;
743 }
744 (*env)->DeleteLocalRef(env, ty.jstr);
745 }
746 }
747 (*env)->CallVoidMethod(env, h->cb, mid, arr);
748 exc = (*env)->ExceptionOccurred(env);
749 if (exc) {
750 (*env)->DeleteLocalRef(env, exc);
751 return 1;
752 }
753 (*env)->DeleteLocalRef(env, arr);
754 }
755 }
756#endif
757#if HAVE_SQLITE31
758 mid = (*env)->GetMethodID(env, cls, "types",
759 "([Ljava/lang/String;)V");
760
761 if (mid && h->stmt) {
762 arr = (*env)->NewObjectArray(env, ncol,
763 C_java_lang_String, 0);
764 for (i = 0; i < ncol; i++) {
765 const char *ctype = sqlite3_column_decltype(h->stmt, i);
766
767 if (!ctype) {
768 switch (sqlite3_column_type(h->stmt, i)) {
769 case SQLITE_INTEGER1: ctype = "integer"; break;
770 case SQLITE_FLOAT2: ctype = "double"; break;
771 default:
772#if defined(SQLITE_TEXT3) && defined(SQLITE3_TEXT3) && (SQLITE_TEXT3 != SQLITE3_TEXT3)
773 case SQLITE_TEXT3:
774#else
775#ifdef SQLITE3_TEXT3
776 case SQLITE3_TEXT3:
777#endif
778#endif
779 ctype = "text"; break;
780 case SQLITE_BLOB4: ctype = "blob"; break;
781 case SQLITE_NULL5: ctype = "null"; break;
782 }
783 }
784 if (ctype) {
785 transstr ty;
786
787 trans2utf(env, 1, 0, ctype, &ty);
788 (*env)->SetObjectArrayElement(env, arr, i, ty.jstr);
789 exc = (*env)->ExceptionOccurred(env);
790 if (exc) {
791 (*env)->DeleteLocalRef(env, exc);
792 return 1;
793 }
794 (*env)->DeleteLocalRef(env, ty.jstr);
795 }
796 }
797 (*env)->CallVoidMethod(env, h->cb, mid, arr);
798 exc = (*env)->ExceptionOccurred(env);
799 if (exc) {
800 (*env)->DeleteLocalRef(env, exc);
801 return 1;
802 }
803 (*env)->DeleteLocalRef(env, arr);
804 }
805#endif
806#endif
807 }
808 if (data) {
809 mid = (*env)->GetMethodID(env, cls, "newrow",
810 "([Ljava/lang/String;)Z");
811 if (mid) {
812 jboolean rc;
813
814 arr = (*env)->NewObjectArray(env, ncol, C_java_lang_String, 0);
815 for (i = 0; arr && i < ncol; i++) {
816 if (data[i]) {
817 transstr dats;
818
819 trans2utf(env, h->haveutf, h->enc, data[i], &dats);
820 (*env)->SetObjectArrayElement(env, arr, i, dats.jstr);
821 exc = (*env)->ExceptionOccurred(env);
822 if (exc) {
823 (*env)->DeleteLocalRef(env, exc);
824 return 1;
825 }
826 (*env)->DeleteLocalRef(env, dats.jstr);
827 }
828 }
829 rc = (*env)->CallBooleanMethod(env, h->cb, mid, arr);
830 exc = (*env)->ExceptionOccurred(env);
831 if (exc) {
832 (*env)->DeleteLocalRef(env, exc);
833 return 1;
834 }
835 if (arr) {
836 (*env)->DeleteLocalRef(env, arr);
837 }
838 (*env)->DeleteLocalRef(env, cls);
839 return rc != JNI_FALSE0;
840 }
841 }
842 }
843 return 0;
844}
845
846static void
847doclose(JNIEnv *env, jobject obj, int final)
848{
849 handle *h = getclrhandle(env, obj);
850
851 if (h) {
852 hfunc *f;
853#if HAVE_SQLITE31 && HAVE_SQLITE3_INCRBLOBIO1
854 hbl *bl;
855#endif
856#if HAVE_SQLITE31 && HAVE_SQLITE3_BACKUPAPI1
857 hbk *bk;
858#endif
859#if HAVE_SQLITE_COMPILE1
860 hvm *v;
861#endif
862
863 if ((*env)->MonitorEnter(env, C_SQLite_Database) != JNI_OK0) {
864 fprintf(stderrstderr, "doclose: MonitorEnter failed\n");
865 return;
866 }
867#if HAVE_SQLITE_COMPILE1
868 while ((v = h->vms)) {
869 h->vms = v->next;
870 v->next = 0;
871 v->h = 0;
872 if (v->vm) {
873#if HAVE_BOTH_SQLITE
874 if (h->is3) {
875 sqlite3_finalize((sqlite3_stmt *) v->vm);
876 } else {
877 sqlite_finalize((sqlite_vm *) v->vm, 0);
878 }
879#else
880#if HAVE_SQLITE20
881 sqlite_finalize((sqlite_vm *) v->vm, 0);
882#endif
883#if HAVE_SQLITE31
884 sqlite3_finalize((sqlite3_stmt *) v->vm);
885#endif
886#endif
887 v->vm = 0;
888 }
889 }
890#endif
891 if (h->sqlite) {
892#if HAVE_BOTH_SQLITE
893 if (h->is3) {
894 sqlite3_close((sqlite3 *) h->sqlite);
895 } else {
896 sqlite_close((sqlite *) h->sqlite);
897 }
898#else
899#if HAVE_SQLITE20
900 sqlite_close((sqlite *) h->sqlite);
901#endif
902#if HAVE_SQLITE31
903 sqlite3_close((sqlite3 *) h->sqlite);
904#endif
905#endif
906 h->sqlite = 0;
907 }
908 while ((f = h->funcs)) {
909 h->funcs = f->next;
910 f->h = 0;
911 f->sf = 0;
912 f->env = 0;
913 if (f->fc) {
914 (*env)->SetLongField(env, f->fc,
915 F_SQLite_FunctionContext_handle, 0);
916 }
917 delglobrefp(env, &f->db);
918 delglobrefp(env, &f->fi);
919 delglobrefp(env, &f->fc);
920 free(f);
921 }
922#if HAVE_SQLITE31 && HAVE_SQLITE3_INCRBLOBIO1
923 while ((bl = h->blobs)) {
924 h->blobs = bl->next;
925 bl->next = 0;
926 bl->h = 0;
927 if (bl->blob) {
928 sqlite3_blob_close(bl->blob);
929 }
930 bl->blob = 0;
931 }
932#endif
933#if HAVE_SQLITE31 && HAVE_SQLITE3_BACKUPAPI1
934 while ((bk = h->backups)) {
935 h->backups = bk->next;
936 bk->next = 0;
937 bk->h = 0;
938 if (bk->bkup) {
939 sqlite3_backup_finish(bk->bkup);
940 }
941 bk->bkup = 0;
942 }
943#endif
944 (*env)->MonitorExit(env, C_SQLite_Database);
945 delglobrefp(env, &h->bh);
946 delglobrefp(env, &h->cb);
947 delglobrefp(env, &h->ai);
948 delglobrefp(env, &h->tr);
949 delglobrefp(env, &h->ph);
950 delglobrefp(env, &h->enc);
951 free(h);
952 return;
953 }
954 if (!final) {
955 throwclosed(env);
956 }
957}
958
959JNIEXPORT__attribute__((visibility("default"))) void JNICALL
960Java_SQLite_Database__1close(JNIEnv *env, jobject obj)
961{
962 doclose(env, obj, 0);
963}
964
965JNIEXPORT__attribute__((visibility("default"))) void JNICALL
966Java_SQLite_Database__1finalize(JNIEnv *env, jobject obj)
967{
968 doclose(env, obj, 1);
969}
970
971JNIEXPORT__attribute__((visibility("default"))) void JNICALL
972Java_SQLite_Database__1busy_1timeout(JNIEnv *env, jobject obj, jint ms)
973{
974 handle *h = gethandle(env, obj);
975
976 if (h && h->sqlite) {
977#if HAVE_BOTH_SQLITE
978 if (h->is3) {
979 sqlite3_busy_timeout((sqlite3 * ) h->sqlite, ms);
980 } else {
981 sqlite_busy_timeout((sqlite *) h->sqlite, ms);
982 }
983#else
984#if HAVE_SQLITE20
985 sqlite_busy_timeout((sqlite *) h->sqlite, ms);
986#endif
987#if HAVE_SQLITE31
988 sqlite3_busy_timeout((sqlite3 * ) h->sqlite, ms);
989#endif
990#endif
991 return;
992 }
993 throwclosed(env);
994}
995
996JNIEXPORT__attribute__((visibility("default"))) jstring JNICALL
997Java_SQLite_Database_version(JNIEnv *env, jclass cls)
998{
999 /* CHECK THIS */
1000#if HAVE_BOTH_SQLITE
1001 return (*env)->NewStringUTF(env, sqlite_libversion());
1002#else
1003#if HAVE_SQLITE20
1004 return (*env)->NewStringUTF(env, sqlite_libversion());
1005#else
1006 return (*env)->NewStringUTF(env, sqlite3_libversion());
1007#endif
1008#endif
1009}
1010
1011JNIEXPORT__attribute__((visibility("default"))) jstring JNICALL
1012Java_SQLite_Database_dbversion(JNIEnv *env, jobject obj)
1013{
1014 handle *h = gethandle(env, obj);
1015
1016 if (h && h->sqlite) {
1017#if HAVE_BOTH_SQLITE
1018 if (h->is3) {
1019 return (*env)->NewStringUTF(env, sqlite3_libversion());
1020 } else {
1021 return (*env)->NewStringUTF(env, sqlite_libversion());
1022 }
1023#else
1024#if HAVE_SQLITE20
1025 return (*env)->NewStringUTF(env, sqlite_libversion());
1026#else
1027 return (*env)->NewStringUTF(env, sqlite3_libversion());
1028#endif
1029#endif
1030 }
1031 return (*env)->NewStringUTF(env, "unknown");
1032}
1033
1034JNIEXPORT__attribute__((visibility("default"))) jlong JNICALL
1035Java_SQLite_Database__1last_1insert_1rowid(JNIEnv *env, jobject obj)
1036{
1037 handle *h = gethandle(env, obj);
1038
1039 if (h && h->sqlite) {
1040#if HAVE_BOTH_SQLITE
1041 if (h->is3) {
1042 return (jlong) sqlite3_last_insert_rowid((sqlite3 *) h->sqlite);
1043 } else {
1044 return (jlong) sqlite_last_insert_rowid((sqlite *) h->sqlite);
1045 }
1046#else
1047#if HAVE_SQLITE20
1048 return (jlong) sqlite_last_insert_rowid((sqlite *) h->sqlite);
1049#endif
1050#if HAVE_SQLITE31
1051 return (jlong) sqlite3_last_insert_rowid((sqlite3 *) h->sqlite);
1052#endif
1053#endif
1054 }
1055 throwclosed(env);
1056 return (jlong) 0;
1057}
1058
1059JNIEXPORT__attribute__((visibility("default"))) jlong JNICALL
1060Java_SQLite_Database__1changes(JNIEnv *env, jobject obj)
1061{
1062 handle *h = gethandle(env, obj);
1063
1064 if (h && h->sqlite) {
1065#if HAVE_BOTH_SQLITE
1066 if (h->is3) {
1067 return (jlong) sqlite3_changes((sqlite3 *) h->sqlite);
1068 } else {
1069 return (jlong) sqlite_changes((sqlite *) h->sqlite);
1070 }
1071#else
1072#if HAVE_SQLITE20
1073 return (jlong) sqlite_changes((sqlite *) h->sqlite);
1074#endif
1075#if HAVE_SQLITE31
1076 return (jlong) sqlite3_changes((sqlite3 *) h->sqlite);
1077#endif
1078#endif
1079 }
1080 throwclosed(env);
1081 return (jlong) 0;
1082}
1083
1084JNIEXPORT__attribute__((visibility("default"))) jboolean JNICALL
1085Java_SQLite_Database__1complete(JNIEnv *env, jclass cls, jstring sql)
1086{
1087 transstr sqlstr;
1088 jboolean result;
1089
1090 if (!sql) {
1091 return JNI_FALSE0;
1092 }
1093#if HAVE_BOTH_SQLITE || HAVE_SQLITE31
1094 /* CHECK THIS */
1095 trans2iso(env, 1, 0, sql, &sqlstr);
1096 result = sqlite3_complete(sqlstr.result) ? JNI_TRUE1 : JNI_FALSE0;
1097#else
1098 trans2iso(env, strcmp(sqlite_libencoding(), "UTF-8")__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(sqlite_libencoding()) && __builtin_constant_p ("UTF-8"
) && (__s1_len = strlen (sqlite_libencoding()), __s2_len
= strlen ("UTF-8"), (!((size_t)(const void *)((sqlite_libencoding
()) + 1) - (size_t)(const void *)(sqlite_libencoding()) == 1)
|| __s1_len >= 4) && (!((size_t)(const void *)(("UTF-8"
) + 1) - (size_t)(const void *)("UTF-8") == 1) || __s2_len >=
4)) ? __builtin_strcmp (sqlite_libencoding(), "UTF-8") : (__builtin_constant_p
(sqlite_libencoding()) && ((size_t)(const void *)((sqlite_libencoding
()) + 1) - (size_t)(const void *)(sqlite_libencoding()) == 1)
&& (__s1_len = strlen (sqlite_libencoding()), __s1_len
< 4) ? (__builtin_constant_p ("UTF-8") && ((size_t
)(const void *)(("UTF-8") + 1) - (size_t)(const void *)("UTF-8"
) == 1) ? __builtin_strcmp (sqlite_libencoding(), "UTF-8") : (
__extension__ ({ const unsigned char *__s2 = (const unsigned char
*) (const char *) ("UTF-8"); int __result = (((const unsigned
char *) (const char *) (sqlite_libencoding()))[0] - __s2[0])
; if (__s1_len > 0 && __result == 0) { __result = (
((const unsigned char *) (const char *) (sqlite_libencoding()
))[1] - __s2[1]); if (__s1_len > 1 && __result == 0
) { __result = (((const unsigned char *) (const char *) (sqlite_libencoding
()))[2] - __s2[2]); if (__s1_len > 2 && __result ==
0) __result = (((const unsigned char *) (const char *) (sqlite_libencoding
()))[3] - __s2[3]); } } __result; }))) : (__builtin_constant_p
("UTF-8") && ((size_t)(const void *)(("UTF-8") + 1) -
(size_t)(const void *)("UTF-8") == 1) && (__s2_len =
strlen ("UTF-8"), __s2_len < 4) ? (__builtin_constant_p (
sqlite_libencoding()) && ((size_t)(const void *)((sqlite_libencoding
()) + 1) - (size_t)(const void *)(sqlite_libencoding()) == 1)
? __builtin_strcmp (sqlite_libencoding(), "UTF-8") : (- (__extension__
({ const unsigned char *__s2 = (const unsigned char *) (const
char *) (sqlite_libencoding()); int __result = (((const unsigned
char *) (const char *) ("UTF-8"))[0] - __s2[0]); if (__s2_len
> 0 && __result == 0) { __result = (((const unsigned
char *) (const char *) ("UTF-8"))[1] - __s2[1]); if (__s2_len
> 1 && __result == 0) { __result = (((const unsigned
char *) (const char *) ("UTF-8"))[2] - __s2[2]); if (__s2_len
> 2 && __result == 0) __result = (((const unsigned
char *) (const char *) ("UTF-8"))[3] - __s2[3]); } } __result
; })))) : __builtin_strcmp (sqlite_libencoding(), "UTF-8"))))
; })
== 0, 0,
1099 sql, &sqlstr);
1100 result = sqlite_complete(sqlstr.result) ? JNI_TRUE1 : JNI_FALSE0;
1101#endif
1102 transfree(&sqlstr);
1103 return result;
1104}
1105
1106JNIEXPORT__attribute__((visibility("default"))) void JNICALL
1107Java_SQLite_Database__1interrupt(JNIEnv *env, jobject obj)
1108{
1109 handle *h = gethandle(env, obj);
1110
1111 if (h && h->sqlite) {
1112#if HAVE_BOTH_SQLITE
1113 if (h->is3) {
1114 sqlite3_interrupt((sqlite3 *) h->sqlite);
1115 } else {
1116 sqlite_interrupt((sqlite *) h->sqlite);
1117 }
1118#else
1119#if HAVE_SQLITE20
1120 sqlite_interrupt((sqlite *) h->sqlite);
1121#endif
1122#if HAVE_SQLITE31
1123 sqlite3_interrupt((sqlite3 *) h->sqlite);
1124#endif
1125#endif
1126 return;
1127 }
1128 throwclosed(env);
1129}
1130
1131JNIEXPORT__attribute__((visibility("default"))) void JNICALL
1132Java_SQLite_Database__1open4(JNIEnv *env, jobject obj, jstring file, jint mode,
1133 jstring vfs, jboolean ver2)
1134{
1135 handle *h = gethandle(env, obj), *hh = 0;
1136 jthrowable exc;
1137 char *err = 0;
1138 transstr filename;
1139 int maj, min, lev;
1140#if HAVE_SQLITE3_OPEN_V21
1141 transstr vfsname;
1142
1143 vfsname.result = 0;
1144 vfsname.tofree = 0;
1145 vfsname.jstr = 0;
1146#endif
1147
1148 if (h) {
1149 if (h->sqlite) {
1150#if HAVE_BOTH_SQLITE
1151 if (h->is3) {
1152 sqlite3_close((sqlite3 *) h->sqlite);
1153 } else {
1154 sqlite_close((sqlite *) h->sqlite);
1155 }
1156 h->is3 = 0;
1157#else
1158#if HAVE_SQLITE20
1159 sqlite_close((sqlite *) h->sqlite);
1160#endif
1161#if HAVE_SQLITE31
1162 sqlite3_close((sqlite3 *) h->sqlite);
1163#endif
1164#endif
1165 h->sqlite = 0;
1166 }
1167 } else {
1168 h = hh = malloc(sizeof (handle));
1169 if (!h) {
1170 throwoom(env, "unable to get SQLite handle");
1171 return;
1172 }
1173 h->sqlite = 0;
1174 h->bh = h->cb = h->ai = h->tr = h->pr = h->ph = 0;
1175 /* CHECK THIS */
1176#if HAVE_BOTH_SQLITE
1177 h->is3 = 0;
1178 h->stmt = 0;
1179 h->haveutf = 1;
1180#else
1181#if HAVE_SQLITE20
1182 h->haveutf = strcmp(sqlite_libencoding(), "UTF-8")__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(sqlite_libencoding()) && __builtin_constant_p ("UTF-8"
) && (__s1_len = strlen (sqlite_libencoding()), __s2_len
= strlen ("UTF-8"), (!((size_t)(const void *)((sqlite_libencoding
()) + 1) - (size_t)(const void *)(sqlite_libencoding()) == 1)
|| __s1_len >= 4) && (!((size_t)(const void *)(("UTF-8"
) + 1) - (size_t)(const void *)("UTF-8") == 1) || __s2_len >=
4)) ? __builtin_strcmp (sqlite_libencoding(), "UTF-8") : (__builtin_constant_p
(sqlite_libencoding()) && ((size_t)(const void *)((sqlite_libencoding
()) + 1) - (size_t)(const void *)(sqlite_libencoding()) == 1)
&& (__s1_len = strlen (sqlite_libencoding()), __s1_len
< 4) ? (__builtin_constant_p ("UTF-8") && ((size_t
)(const void *)(("UTF-8") + 1) - (size_t)(const void *)("UTF-8"
) == 1) ? __builtin_strcmp (sqlite_libencoding(), "UTF-8") : (
__extension__ ({ const unsigned char *__s2 = (const unsigned char
*) (const char *) ("UTF-8"); int __result = (((const unsigned
char *) (const char *) (sqlite_libencoding()))[0] - __s2[0])
; if (__s1_len > 0 && __result == 0) { __result = (
((const unsigned char *) (const char *) (sqlite_libencoding()
))[1] - __s2[1]); if (__s1_len > 1 && __result == 0
) { __result = (((const unsigned char *) (const char *) (sqlite_libencoding
()))[2] - __s2[2]); if (__s1_len > 2 && __result ==
0) __result = (((const unsigned char *) (const char *) (sqlite_libencoding
()))[3] - __s2[3]); } } __result; }))) : (__builtin_constant_p
("UTF-8") && ((size_t)(const void *)(("UTF-8") + 1) -
(size_t)(const void *)("UTF-8") == 1) && (__s2_len =
strlen ("UTF-8"), __s2_len < 4) ? (__builtin_constant_p (
sqlite_libencoding()) && ((size_t)(const void *)((sqlite_libencoding
()) + 1) - (size_t)(const void *)(sqlite_libencoding()) == 1)
? __builtin_strcmp (sqlite_libencoding(), "UTF-8") : (- (__extension__
({ const unsigned char *__s2 = (const unsigned char *) (const
char *) (sqlite_libencoding()); int __result = (((const unsigned
char *) (const char *) ("UTF-8"))[0] - __s2[0]); if (__s2_len
> 0 && __result == 0) { __result = (((const unsigned
char *) (const char *) ("UTF-8"))[1] - __s2[1]); if (__s2_len
> 1 && __result == 0) { __result = (((const unsigned
char *) (const char *) ("UTF-8"))[2] - __s2[2]); if (__s2_len
> 2 && __result == 0) __result = (((const unsigned
char *) (const char *) ("UTF-8"))[3] - __s2[3]); } } __result
; })))) : __builtin_strcmp (sqlite_libencoding(), "UTF-8"))))
; })
== 0;
1183#endif
1184#if HAVE_SQLITE31
1185 h->stmt = 0;
1186 h->haveutf = 1;
1187#endif
1188#endif
1189 h->enc = 0;
1190 h->funcs = 0;
1191 h->ver = 0;
1192#if HAVE_SQLITE_COMPILE1
1193 h->vms = 0;
1194#endif
1195#if HAVE_SQLITE31 && HAVE_SQLITE3_INCRBLOBIO1
1196 h->blobs = 0;
1197#endif
1198#if HAVE_SQLITE31 && HAVE_SQLITE3_BACKUPAPI1
1199 h->backups = 0;
1200#endif
1201 }
1202 h->env = 0;
1203 if (!file) {
1204 if (hh) {
1205 free(hh);
1206 }
1207 throwex(env, err ? err : "invalid file name");
1208 return;
1209 }
1210 trans2iso(env, h->haveutf, h->enc, file, &filename);
1211 exc = (*env)->ExceptionOccurred(env);
1212 if (exc) {
1213 if (hh) {
1214 free(hh);
1215 }
1216 (*env)->DeleteLocalRef(env, exc);
1217 return;
1218 }
1219#if HAVE_SQLITE3_OPEN_V21
1220 if (vfs) {
1221 trans2iso(env, 1, h->enc, vfs, &vfsname);
1222 exc = (*env)->ExceptionOccurred(env);
1223 if (exc) {
1224 transfree(&filename);
1225 if (hh) {
1226 free(hh);
1227 }
1228 (*env)->DeleteLocalRef(env, exc);
1229 return;
1230 }
1231 }
1232#endif
1233#if HAVE_BOTH_SQLITE
1234 {
1235 FILE *f = fopen(filename.result, "rb");
1236 int c_0 = EOF(-1);
1237
1238 if (f) {
1239 c_0 = fgetc(f);
1240 fclose(f);
1241 }
1242 if (c_0 != '*' && ver2 == JNI_FALSE0) {
1243#if HAVE_SQLITE3_OPEN_V21
1244 int rc = sqlite3_open_v2(filename.result, (sqlite3 **) &h->sqlite,
1245 (int) mode, vfsname.result);
1246#else
1247 int rc = sqlite3_open(filename.result, (sqlite3 **) &h->sqlite);
1248#endif
1249
1250 if (rc == SQLITE_OK0) {
1251 h->is3 = 1;
1252 } else if (h->sqlite) {
1253 sqlite3_close((sqlite3 *) h->sqlite);
1254 h->sqlite = 0;
1255 }
1256 } else {
1257 h->sqlite = (void *) sqlite_open(filename.result,
1258 (int) mode, &err);
1259 }
1260 }
1261#else
1262#if HAVE_SQLITE20
1263 h->sqlite = (void *) sqlite_open(filename.result, (int) mode, &err);
1264#endif
1265#if HAVE_SQLITE31
1266#if HAVE_SQLITE3_OPEN_V21
1267 if (sqlite3_open_v2(filename.result, (sqlite3 **) &h->sqlite,
1268 (int) mode, vfsname.result) != SQLITE_OK0)
1269#else
1270 if (sqlite3_open(filename.result, (sqlite3 **) &h->sqlite) != SQLITE_OK0)
1271#endif
1272 {
1273 if (h->sqlite) {
1274 sqlite3_close((sqlite3 *) h->sqlite);
1275 h->sqlite = 0;
1276 }
1277 }
1278#endif
1279#endif
1280 transfree(&filename);
1281#if HAVE_SQLITE3_OPEN_V21
1282 transfree(&vfsname);
1283#endif
1284 exc = (*env)->ExceptionOccurred(env);
1285 if (exc) {
1286 (*env)->DeleteLocalRef(env, exc);
1287#if HAVE_SQLITE20
1288 if (err) {
1289 sqlite_freemem(err);
1290 }
1291#endif
1292 if (h->sqlite) {
1293#if HAVE_BOTH_SQLITE
1294 if (h->is3) {
1295 sqlite3_close((sqlite3 *) h->sqlite);
1296 h->is3 = 0;
1297 } else {
1298 sqlite_close((sqlite *) h->sqlite);
1299 }
1300#else
1301#if HAVE_SQLITE20
1302 sqlite_close((sqlite *) h->sqlite);
1303#endif
1304#if HAVE_SQLITE31
1305 sqlite3_close((sqlite3 *) h->sqlite);
1306#endif
1307#endif
1308 }
1309 h->sqlite = 0;
1310 if (hh) {
1311 free(hh);
1312 }
1313 return;
1314 }
1315 if (h->sqlite) {
1316 jvalue v;
1317
1318 v.j = 0;
1319 v.l = (jobject) h;
1320 (*env)->SetLongField(env, obj, F_SQLite_Database_handle, v.j);
1321 hh = 0;
1322#if HAVE_SQLITE20
1323 if (err) {
1324 sqlite_freemem(err);
1325 }
1326#endif
1327#if HAVE_BOTH_SQLITE
1328 if (h->is3) {
1329 sscanf(sqlite3_libversion(), "%d.%d.%d", &maj, &min, &lev);
1330#if HAVE_SQLITE3_LOAD_EXTENSION0
1331 sqlite3_enable_load_extension((sqlite3 *) h->sqlite, 1);
1332#endif
1333 } else {
1334 sscanf(sqlite_libversion(), "%d.%d.%d", &maj, &min, &lev);
1335 }
1336#else
1337#if HAVE_SQLITE20
1338 sscanf(sqlite_libversion(), "%d.%d.%d", &maj, &min, &lev);
1339#endif
1340#if HAVE_SQLITE31
1341 sscanf(sqlite3_libversion(), "%d.%d.%d", &maj, &min, &lev);
1342#if HAVE_SQLITE3_LOAD_EXTENSION0
1343 sqlite3_enable_load_extension((sqlite3 *) h->sqlite, 1);
1344#endif
1345#endif
1346#endif
1347 h->ver = ((maj & 0xFF) << 16) | ((min & 0xFF) << 8) | (lev & 0xFF);
1348 return;
1349 }
1350 if (hh) {
1351 free(hh);
1352 }
1353 throwex(env, err ? err : "unknown error in open");
1354#if HAVE_SQLITE20
1355 if (err) {
1356 sqlite_freemem(err);
1357 }
1358#endif
1359}
1360
1361JNIEXPORT__attribute__((visibility("default"))) void JNICALL
1362Java_SQLite_Database__1open(JNIEnv *env, jobject obj, jstring file, jint mode)
1363{
1364 Java_SQLite_Database__1open4(env, obj, file, mode, 0, 0);
1365}
1366
1367JNIEXPORT__attribute__((visibility("default"))) void JNICALL
1368Java_SQLite_Database__1open_1aux_1file(JNIEnv *env, jobject obj, jstring file)
1369{
1370 handle *h = gethandle(env, obj);
1371#if HAVE_SQLITE_OPEN_AUX_FILE0
1372 jthrowable exc;
1373 char *err = 0;
1374 transstr filename;
1375 int ret;
1376#endif
1377
1378 if (h && h->sqlite) {
1379#if HAVE_SQLITE_OPEN_AUX_FILE0
1380#if HAVE_BOTH_SQLITE
1381 if (h->is3) {
1382 throwex(env, "unsupported");
1383 }
1384#endif
1385 trans2iso(env, h->haveutf, h->enc, file, &filename);
1386 exc = (*env)->ExceptionOccurred(env);
1387 if (exc) {
1388 (*env)->DeleteLocalRef(env, exc);
1389 return;
1390 }
1391 ret = sqlite_open_aux_file((sqlite *) h->sqlite,
1392 filename.result, &err);
1393 transfree(&filename);
1394 exc = (*env)->ExceptionOccurred(env);
1395 if (exc) {
1396 (*env)->DeleteLocalRef(env, exc);
1397 if (err) {
1398 sqlite_freemem(err);
1399 }
1400 return;
1401 }
1402 if (ret != SQLITE_OK0) {
1403 throwex(env, err ? err : sqlite_error_string(ret));
1404 }
1405 if (err) {
1406 sqlite_freemem(err);
1407 }
1408#else
1409 throwex(env, "unsupported");
1410#endif
1411 return;
1412 }
1413 throwclosed(env);
1414}
1415
1416JNIEXPORT__attribute__((visibility("default"))) void JNICALL
1417Java_SQLite_Database__1busy_1handler(JNIEnv *env, jobject obj, jobject bh)
1418{
1419 handle *h = gethandle(env, obj);
1420
1421 if (h && h->sqlite) {
1422 delglobrefp(env, &h->bh);
1423 globrefset(env, bh, &h->bh);
1424#if HAVE_BOTH_SQLITE
1425 if (h->is3) {
1426 sqlite3_busy_handler((sqlite3 *) h->sqlite, busyhandler3, h);
1427 } else {
1428 sqlite_busy_handler((sqlite *) h->sqlite, busyhandler, h);
1429 }
1430#else
1431#if HAVE_SQLITE20
1432 sqlite_busy_handler((sqlite *) h->sqlite, busyhandler, h);
1433#endif
1434#if HAVE_SQLITE31
1435 sqlite3_busy_handler((sqlite3 *) h->sqlite, busyhandler3, h);
1436#endif
1437#endif
1438 return;
1439 }
1440 throwclosed(env);
1441}
1442
1443JNIEXPORT__attribute__((visibility("default"))) void JNICALL
1444Java_SQLite_Database__1exec__Ljava_lang_String_2LSQLite_Callback_2
1445 (JNIEnv *env, jobject obj, jstring sql, jobject cb)
1446{
1447 handle *h = gethandle(env, obj);
1448 freemem *freeproc;
1449
1450 if (!sql) {
1451 throwex(env, "invalid SQL statement");
1452 return;
1453 }
1454 if (h) {
1455 if (h->sqlite) {
1456 jthrowable exc;
1457 int rc;
1458 char *err = 0;
1459 transstr sqlstr;
1460 jobject oldcb = globrefpop(env, &h->cb);
1461
1462 globrefset(env, cb, &h->cb);
1463 h->env = env;
1464 h->row1 = 1;
1465 trans2iso(env, h->haveutf, h->enc, sql, &sqlstr);
1466 exc = (*env)->ExceptionOccurred(env);
1467 if (exc) {
1468 (*env)->DeleteLocalRef(env, exc);
1469 return;
1470 }
1471#if HAVE_BOTH_SQLITE
1472 if (h->is3) {
1473 rc = sqlite3_exec((sqlite3 *) h->sqlite, sqlstr.result,
1474 callback, h, &err);
1475 freeproc = (freemem *) sqlite3_free;
1476 } else {
1477 rc = sqlite_exec((sqlite *) h->sqlite, sqlstr.result,
1478 callback, h, &err);
1479 freeproc = (freemem *) sqlite_freemem;
1480 }
1481#else
1482#if HAVE_SQLITE20
1483 rc = sqlite_exec((sqlite *) h->sqlite, sqlstr.result,
1484 callback, h, &err);
1485 freeproc = (freemem *) sqlite_freemem;
1486#endif
1487#if HAVE_SQLITE31
1488 rc = sqlite3_exec((sqlite3 *) h->sqlite, sqlstr.result,
1489 callback, h, &err);
1490 freeproc = (freemem *) sqlite3_free;
1491#endif
1492#endif
1493 transfree(&sqlstr);
1494 exc = (*env)->ExceptionOccurred(env);
1495 delglobrefp(env, &h->cb);
1496 h->cb = oldcb;
1497 if (exc) {
1498 (*env)->DeleteLocalRef(env, exc);
1499 if (err) {
1500 freeproc(err);
1501 }
1502 return;
1503 }
1504 if (rc != SQLITE_OK0) {
1505 char msg[128];
1506
1507 seterr(env, obj, rc);
1508 if (!err) {
1509 sprintf(msg, "error %d in sqlite*_exec", rc);
1510 }
1511 throwex(env, err ? err : msg);
1512 }
1513 if (err) {
1514 freeproc(err);
1515 }
1516 return;
1517 }
1518 }
1519 throwclosed(env);
1520}
1521
1522JNIEXPORT__attribute__((visibility("default"))) void JNICALL
1523Java_SQLite_Database__1exec__Ljava_lang_String_2LSQLite_Callback_2_3Ljava_lang_String_2
1524 (JNIEnv *env, jobject obj, jstring sql, jobject cb, jobjectArray args)
1525{
1526 handle *h = gethandle(env, obj);
1527 freemem *freeproc = 0;
1528
1529 if (!sql) {
1530 throwex(env, "invalid SQL statement");
1531 return;
1532 }
1533 if (h) {
1534 if (h->sqlite) {
1535 jthrowable exc;
1536 int rc = SQLITE_ERROR1, nargs, i;
1537 char *err = 0, *p;
1538 const char *str = (*env)->GetStringUTFChars(env, sql, 0);
1539 transstr sqlstr;
1540 struct args {
1541 char *arg;
1542 jobject obj;
1543 transstr trans;
1544 } *argv = 0;
1545 char **cargv = 0;
1546 jobject oldcb = globrefpop(env, &h->cb);
1547
1548 globrefset(env, cb, &h->cb);
1549 p = (char *) str;
1550 nargs = 0;
1551 while (*p) {
1552 if (*p == '%') {
1553 ++p;
1554 if (*p == 'q' || *p == 's') {
1555 nargs++;
1556 if (nargs > MAX_PARAMS32) {
1557 (*env)->ReleaseStringUTFChars(env, sql, str);
1558 delglobrefp(env, &h->cb);
1559 h->cb = oldcb;
1560 throwex(env, "too much SQL parameters");
1561 return;
1562 }
1563 } else if (h->ver >= 0x020500 && *p == 'Q') {
1564 nargs++;
1565 if (nargs > MAX_PARAMS32) {
1566 (*env)->ReleaseStringUTFChars(env, sql, str);
1567 delglobrefp(env, &h->cb);
1568 h->cb = oldcb;
1569 throwex(env, "too much SQL parameters");
1570 return;
1571 }
1572 } else if (*p != '%') {
1573 (*env)->ReleaseStringUTFChars(env, sql, str);
1574 delglobrefp(env, &h->cb);
1575 h->cb = oldcb;
1576 throwex(env, "bad % specification in query");
1577 return;
1578 }
1579 }
1580 ++p;
1581 }
1582 cargv = malloc((sizeof (*argv) + sizeof (char *))
1583 * MAX_PARAMS32);
1584 if (!cargv) {
1585 (*env)->ReleaseStringUTFChars(env, sql, str);
1586 delglobrefp(env, &h->cb);
1587 h->cb = oldcb;
1588 throwoom(env, "unable to allocate arg vector");
1589 return;
1590 }
1591 argv = (struct args *) (cargv + MAX_PARAMS32);
1592 for (i = 0; i < MAX_PARAMS32; i++) {
1593 cargv[i] = 0;
1594 argv[i].arg = 0;
1595 argv[i].obj = 0;
1596 argv[i].trans.result = argv[i].trans.tofree = 0;
1597 }
1598 exc = 0;
1599 for (i = 0; i < nargs; i++) {
1600 jobject so = (*env)->GetObjectArrayElement(env, args, i);
1601
1602 exc = (*env)->ExceptionOccurred(env);
1603 if (exc) {
1604 (*env)->DeleteLocalRef(env, exc);
1605 break;
1606 }
1607 if (so) {
1608 argv[i].obj = so;
1609 argv[i].arg = cargv[i] =
1610 trans2iso(env, h->haveutf, h->enc, argv[i].obj,
1611 &argv[i].trans);
1612 }
1613 }
1614 if (exc) {
1615 for (i = 0; i < nargs; i++) {
1616 if (argv[i].obj) {
1617 transfree(&argv[i].trans);
1618 }
1619 }
1620 freep((char **) &cargv);
1621 (*env)->ReleaseStringUTFChars(env, sql, str);
1622 delglobrefp(env, &h->cb);
1623 h->cb = oldcb;
1624 return;
1625 }
1626 h->env = env;
1627 h->row1 = 1;
1628 trans2iso(env, h->haveutf, h->enc, sql, &sqlstr);
1629 exc = (*env)->ExceptionOccurred(env);
1630 if (!exc) {
1631#if HAVE_BOTH_SQLITE
1632 if (h->is3) {
1633#if defined(_WIN32) || !defined(CANT_PASS_VALIST_AS_CHARPTR1)
1634 char *s = sqlite3_vmprintf(sqlstr.result, (char *) cargv);
1635#else
1636 char *s = sqlite3_mprintf(sqlstr.result,
1637 cargv[0], cargv[1],
1638 cargv[2], cargv[3],
1639 cargv[4], cargv[5],
1640 cargv[6], cargv[7],
1641 cargv[8], cargv[9],
1642 cargv[10], cargv[11],
1643 cargv[12], cargv[13],
1644 cargv[14], cargv[15],
1645 cargv[16], cargv[17],
1646 cargv[18], cargv[19],
1647 cargv[20], cargv[21],
1648 cargv[22], cargv[23],
1649 cargv[24], cargv[25],
1650 cargv[26], cargv[27],
1651 cargv[28], cargv[29],
1652 cargv[30], cargv[31]);
1653#endif
1654
1655 if (s) {
1656 rc = sqlite3_exec((sqlite3 *) h->sqlite, s, callback,
1657 h, &err);
1658 sqlite3_free(s);
1659 } else {
1660 rc = SQLITE_NOMEM7;
1661 }
1662 freeproc = (freemem *) sqlite3_free;
1663 } else {
1664#if defined(_WIN32) || !defined(CANT_PASS_VALIST_AS_CHARPTR1)
1665 rc = sqlite_exec_vprintf((sqlite *) h->sqlite,
1666 sqlstr.result, callback, h, &err,
1667 (char *) cargv);
1668#else
1669 rc = sqlite_exec_printf((sqlite *) h->sqlite,
1670 sqlstr.result, callback,
1671 h, &err,
1672 cargv[0], cargv[1],
1673 cargv[2], cargv[3],
1674 cargv[4], cargv[5],
1675 cargv[6], cargv[7],
1676 cargv[8], cargv[9],
1677 cargv[10], cargv[11],
1678 cargv[12], cargv[13],
1679 cargv[14], cargv[15],
1680 cargv[16], cargv[17],
1681 cargv[18], cargv[19],
1682 cargv[20], cargv[21],
1683 cargv[22], cargv[23],
1684 cargv[24], cargv[25],
1685 cargv[26], cargv[27],
1686 cargv[28], cargv[29],
1687 cargv[30], cargv[31]);
1688#endif
1689 freeproc = (freemem *) sqlite_freemem;
1690 }
1691#else
1692#if HAVE_SQLITE20
1693#if defined(_WIN32) || !defined(CANT_PASS_VALIST_AS_CHARPTR1)
1694 rc = sqlite_exec_vprintf((sqlite *) h->sqlite, sqlstr.result,
1695 callback, h, &err, (char *) cargv);
1696#else
1697 rc = sqlite_exec_printf((sqlite *) h->sqlite, sqlstr.result,
1698 callback, h, &err,
1699 cargv[0], cargv[1],
1700 cargv[2], cargv[3],
1701 cargv[4], cargv[5],
1702 cargv[6], cargv[7],
1703 cargv[8], cargv[9],
1704 cargv[10], cargv[11],
1705 cargv[12], cargv[13],
1706 cargv[14], cargv[15],
1707 cargv[16], cargv[17],
1708 cargv[18], cargv[19],
1709 cargv[20], cargv[21],
1710 cargv[22], cargv[23],
1711 cargv[24], cargv[25],
1712 cargv[26], cargv[27],
1713 cargv[28], cargv[29],
1714 cargv[30], cargv[31]);
1715#endif
1716 freeproc = (freemem *) sqlite_freemem;
1717#endif
1718#if HAVE_SQLITE31
1719#if defined(_WIN32) || !defined(CANT_PASS_VALIST_AS_CHARPTR1)
1720 char *s = sqlite3_vmprintf(sqlstr.result, (char *) cargv);
1721#else
1722 char *s = sqlite3_mprintf(sqlstr.result,
1723 cargv[0], cargv[1],
1724 cargv[2], cargv[3],
1725 cargv[4], cargv[5],
1726 cargv[6], cargv[7],
1727 cargv[8], cargv[9],
1728 cargv[10], cargv[11],
1729 cargv[12], cargv[13],
1730 cargv[14], cargv[15],
1731 cargv[16], cargv[17],
1732 cargv[18], cargv[19],
1733 cargv[20], cargv[21],
1734 cargv[22], cargv[23],
1735 cargv[24], cargv[25],
1736 cargv[26], cargv[27],
1737 cargv[28], cargv[29],
1738 cargv[30], cargv[31]);
1739#endif
1740
1741 if (s) {
1742 rc = sqlite3_exec((sqlite3 *) h->sqlite, s, callback,
1743 h, &err);
1744 sqlite3_free(s);
1745 } else {
1746 rc = SQLITE_NOMEM7;
1747 }
1748 freeproc = (freemem *) sqlite3_free;
1749#endif
1750#endif
1751 exc = (*env)->ExceptionOccurred(env);
1752 }
1753 for (i = 0; i < nargs; i++) {
1754 if (argv[i].obj) {
1755 transfree(&argv[i].trans);
1756 }
1757 }
1758 transfree(&sqlstr);
1759 (*env)->ReleaseStringUTFChars(env, sql, str);
1760 freep((char **) &cargv);
1761 delglobrefp(env, &h->cb);
1762 h->cb = oldcb;
1763 if (exc) {
1764 (*env)->DeleteLocalRef(env, exc);
1765 if (err && freeproc) {
1766 freeproc(err);
1767 }
1768 return;
1769 }
1770 if (rc != SQLITE_OK0) {
1771 char msg[128];
1772
1773 seterr(env, obj, rc);
1774 if (!err) {
1775 sprintf(msg, "error %d in sqlite*_exec", rc);
1776 }
1777 throwex(env, err ? err : msg);
1778 }
1779 if (err && freeproc) {
1780 freeproc(err);
1781 }
1782 return;
1783 }
1784 }
1785 throwclosed(env);
1786}
1787
1788static hfunc *
1789getfunc(JNIEnv *env, jobject obj)
1790{
1791 jvalue v;
1792
1793 v.j = (*env)->GetLongField(env, obj, F_SQLite_FunctionContext_handle);
1794 return (hfunc *) v.l;
1795}
1796
1797#if HAVE_SQLITE20
1798static void
1799call_common(sqlite_func *sf, int isstep, int nargs, const char **args)
1800{
1801 hfunc *f = (hfunc *) sqlite_user_data(sf);
1802
1803 if (f && f->env && f->fi) {
1804 JNIEnv *env = f->env;
1805 jclass cls = (*env)->GetObjectClass(env, f->fi);
1806 jmethodID mid =
1807 (*env)->GetMethodID(env, cls,
1808 isstep ? "step" : "function",
1809 "(LSQLite/FunctionContext;[Ljava/lang/String;)V");
1810 jobjectArray arr;
1811 int i;
1812
1813 if (mid == 0) {
1814 (*env)->DeleteLocalRef(env, cls);
1815 return;
1816 }
1817 arr = (*env)->NewObjectArray(env, nargs, C_java_lang_String, 0);
1818 for (i = 0; i < nargs; i++) {
1819 if (args[i]) {
1820 transstr arg;
1821 jthrowable exc;
1822
1823 trans2utf(env, f->h->haveutf, f->h->enc, args[i], &arg);
1824 (*env)->SetObjectArrayElement(env, arr, i, arg.jstr);
1825 exc = (*env)->ExceptionOccurred(env);
1826 if (exc) {
1827 (*env)->DeleteLocalRef(env, exc);
1828 return;
1829 }
1830 (*env)->DeleteLocalRef(env, arg.jstr);
1831 }
1832 }
1833 f->sf = sf;
1834 (*env)->CallVoidMethod(env, f->fi, mid, f->fc, arr);
1835 (*env)->DeleteLocalRef(env, arr);
1836 (*env)->DeleteLocalRef(env, cls);
1837 }
1838}
1839
1840static void
1841call_func(sqlite_func *sf, int nargs, const char **args)
1842{
1843 call_common(sf, 0, nargs, args);
1844}
1845
1846static void
1847call_step(sqlite_func *sf, int nargs, const char **args)
1848{
1849 call_common(sf, 1, nargs, args);
1850}
1851
1852static void
1853call_final(sqlite_func *sf)
1854{
1855 hfunc *f = (hfunc *) sqlite_user_data(sf);
1856
1857 if (f && f->env && f->fi) {
1858 JNIEnv *env = f->env;
1859 jclass cls = (*env)->GetObjectClass(env, f->fi);
1860 jmethodID mid = (*env)->GetMethodID(env, cls, "last_step",
1861 "(LSQLite/FunctionContext;)V");
1862 if (mid == 0) {
1863 (*env)->DeleteLocalRef(env, cls);
1864 return;
1865 }
1866 f->sf = sf;
1867 (*env)->CallVoidMethod(env, f->fi, mid, f->fc);
1868 (*env)->DeleteLocalRef(env, cls);
1869 }
1870}
1871#endif
1872
1873#if HAVE_SQLITE31
1874static void
1875call3_common(sqlite3_context *sf, int isstep, int nargs, sqlite3_value **args)
1876{
1877 hfunc *f = (hfunc *) sqlite3_user_data(sf);
1878
1879 if (f && f->env && f->fi) {
1880 JNIEnv *env = f->env;
1881 jclass cls = (*env)->GetObjectClass(env, f->fi);
1882 jmethodID mid =
1883 (*env)->GetMethodID(env, cls,
1884 isstep ? "step" : "function",
1885 "(LSQLite/FunctionContext;[Ljava/lang/String;)V");
1886 jobjectArray arr;
1887 int i;
1888
1889 if (mid == 0) {
1890 (*env)->DeleteLocalRef(env, cls);
1891 return;
1892 }
1893 arr = (*env)->NewObjectArray(env, nargs, C_java_lang_String, 0);
1894 for (i = 0; i < nargs; i++) {
1895 if (args[i]) {
1896 transstr arg;
1897 jthrowable exc;
1898
1899 trans2utf(env, 1, 0, (char *) sqlite3_value_text(args[i]),
1900 &arg);
1901 (*env)->SetObjectArrayElement(env, arr, i, arg.jstr);
1902 exc = (*env)->ExceptionOccurred(env);
1903 if (exc) {
1904 (*env)->DeleteLocalRef(env, exc);
1905 return;
1906 }
1907 (*env)->DeleteLocalRef(env, arg.jstr);
1908 }
1909 }
1910 f->sf = sf;
1911 (*env)->CallVoidMethod(env, f->fi, mid, f->fc, arr);
1912 (*env)->DeleteLocalRef(env, arr);
1913 (*env)->DeleteLocalRef(env, cls);
1914 }
1915}
1916
1917static void
1918call3_func(sqlite3_context *sf, int nargs, sqlite3_value **args)
1919{
1920 call3_common(sf, 0, nargs, args);
1921}
1922
1923static void
1924call3_step(sqlite3_context *sf, int nargs, sqlite3_value **args)
1925{
1926 call3_common(sf, 1, nargs, args);
1927}
1928
1929static void
1930call3_final(sqlite3_context *sf)
1931{
1932 hfunc *f = (hfunc *) sqlite3_user_data(sf);
1933
1934 if (f && f->env && f->fi) {
1935 JNIEnv *env = f->env;
1936 jclass cls = (*env)->GetObjectClass(env, f->fi);
1937 jmethodID mid = (*env)->GetMethodID(env, cls, "last_step",
1938 "(LSQLite/FunctionContext;)V");
1939 if (mid == 0) {
1940 (*env)->DeleteLocalRef(env, cls);
1941 return;
1942 }
1943 f->sf = sf;
1944 (*env)->CallVoidMethod(env, f->fi, mid, f->fc);
1945 (*env)->DeleteLocalRef(env, cls);
1946 }
1947}
1948#endif
1949
1950static void
1951mkfunc_common(JNIEnv *env, int isagg, jobject obj, jstring name,
1952 jint nargs, jobject fi)
1953{
1954 handle *h = gethandle(env, obj);
1955
1956 if (h && h->sqlite) {
1957 jclass cls = (*env)->FindClass(env, "SQLite/FunctionContext");
1958 jobject fc;
1959 hfunc *f;
1960 int ret;
1961 transstr namestr;
1962 jvalue v;
1963 jthrowable exc;
1964
1965 fc = (*env)->AllocObject(env, cls);
1966 if (!fi) {
1967 throwex(env, "null SQLite.Function not allowed");
1968 return;
1969 }
1970 f = malloc(sizeof (hfunc));
1971 if (!f) {
1972 throwoom(env, "unable to get SQLite.FunctionContext handle");
1973 return;
1974 }
1975 globrefset(env, fc, &f->fc);
1976 globrefset(env, fi, &f->fi);
1977 globrefset(env, obj, &f->db);
1978 f->h = h;
1979 f->next = h->funcs;
1980 h->funcs = f;
1981 f->sf = 0;
1982 f->env = env;
1983 v.j = 0;
1984 v.l = (jobject) f;
1985 (*env)->SetLongField(env, f->fc, F_SQLite_FunctionContext_handle, v.j);
1986 trans2iso(env, h->haveutf, h->enc, name, &namestr);
1987 exc = (*env)->ExceptionOccurred(env);
1988 if (exc) {
1989 (*env)->DeleteLocalRef(env, exc);
1990 return;
1991 }
1992#if HAVE_BOTH_SQLITE
1993 f->is3 = h->is3;
1994 if (h->is3) {
1995 ret = sqlite3_create_function((sqlite3 *) h->sqlite,
1996 namestr.result,
1997 (int) nargs,
1998 SQLITE_UTF81, f,
1999 isagg ? NULL((void*)0) : call3_func,
2000 isagg ? call3_step : NULL((void*)0),
2001 isagg ? call3_final : NULL((void*)0));
2002
2003 } else {
2004 if (isagg) {
2005 ret = sqlite_create_aggregate((sqlite *) h->sqlite,
2006 namestr.result,
2007 (int) nargs,
2008 call_step, call_final, f);
2009 } else {
2010 ret = sqlite_create_function((sqlite *) h->sqlite,
2011 namestr.result,
2012 (int) nargs,
2013 call_func, f);
2014 }
2015 }
2016#else
2017#if HAVE_SQLITE20
2018 if (isagg) {
2019 ret = sqlite_create_aggregate((sqlite *) h->sqlite, namestr.result,
2020 (int) nargs,
2021 call_step, call_final, f);
2022 } else {
2023 ret = sqlite_create_function((sqlite *) h->sqlite, namestr.result,
2024 (int) nargs,
2025 call_func, f);
2026 }
2027#endif
2028#if HAVE_SQLITE31
2029 ret = sqlite3_create_function((sqlite3 *) h->sqlite,
2030 namestr.result,
2031 (int) nargs,
2032 SQLITE_UTF81, f,
2033 isagg ? NULL((void*)0) : call3_func,
2034 isagg ? call3_step : NULL((void*)0),
2035 isagg ? call3_final : NULL((void*)0));
2036#endif
2037#endif
2038 transfree(&namestr);
2039 if (ret != SQLITE_OK0) {
2040 throwex(env, "error creating function/aggregate");
2041 }
2042 return;
2043 }
2044 throwclosed(env);
2045}
2046
2047JNIEXPORT__attribute__((visibility("default"))) void JNICALL
2048Java_SQLite_Database__1create_1aggregate(JNIEnv *env, jobject obj,
2049 jstring name, jint nargs, jobject fi)
2050{
2051 mkfunc_common(env, 1, obj, name, nargs, fi);
2052}
2053
2054JNIEXPORT__attribute__((visibility("default"))) void JNICALL
2055Java_SQLite_Database__1create_1function(JNIEnv *env, jobject obj,
2056 jstring name, jint nargs, jobject fi)
2057{
2058 mkfunc_common(env, 0, obj, name, nargs, fi);
2059}
2060
2061JNIEXPORT__attribute__((visibility("default"))) void JNICALL
2062Java_SQLite_Database__1function_1type(JNIEnv *env, jobject obj,
2063 jstring name, jint type)
2064{
2065 handle *h = gethandle(env, obj);
2066
2067 if (h && h->sqlite) {
2068#if HAVE_BOTH_SQLITE
2069 if (h->is3) {
2070 return;
2071 }
2072#endif
2073#if HAVE_SQLITE20
2074#if HAVE_SQLITE_FUNCTION_TYPE0
2075 {
2076 int ret;
2077 transstr namestr;
2078 jthrowable exc;
2079
2080 trans2iso(env, h->haveutf, h->enc, name, &namestr);
2081 exc = (*env)->ExceptionOccurred(env);
2082 if (exc) {
2083 (*env)->DeleteLocalRef(env, exc);
2084 return;
2085 }
2086 ret = sqlite_function_type(h->sqlite, namestr.result, (int) type);
2087 transfree(&namestr);
2088 if (ret != SQLITE_OK0) {
2089 throwex(env, sqlite_error_string(ret));
2090 }
2091 }
2092#endif
2093#endif
2094 return;
2095 }
2096 throwclosed(env);
2097}
2098
2099JNIEXPORT__attribute__((visibility("default"))) jint JNICALL
2100Java_SQLite_FunctionContext_count(JNIEnv *env, jobject obj)
2101{
2102 hfunc *f = getfunc(env, obj);
2103 jint r = 0;
2104
2105 if (f && f->sf) {
2106#if HAVE_SQLITE_BOTH
2107 if (f->is3) {
2108 r = (jint) sqlite3_aggregate_count((sqlite3_context *) f->sf);
2109 } else {
2110 r = (jint) sqlite_aggregate_count((sqlite_func *) f->sf);
2111 }
2112#else
2113#if HAVE_SQLITE20
2114 r = (jint) sqlite_aggregate_count((sqlite_func *) f->sf);
2115#endif
2116#if HAVE_SQLITE31
2117 r = (jint) sqlite3_aggregate_count((sqlite3_context *) f->sf);
2118#endif
2119#endif
2120 }
2121 return r;
2122}
2123
2124JNIEXPORT__attribute__((visibility("default"))) void JNICALL
2125Java_SQLite_FunctionContext_set_1error(JNIEnv *env, jobject obj, jstring err)
2126{
2127 hfunc *f = getfunc(env, obj);
2128
2129 if (f && f->sf) {
2130#if HAVE_BOTH_SQLITE
2131 if (!f->is3) {
2132 transstr errstr;
2133 jthrowable exc;
2134
2135 trans2iso(env, f->h->haveutf, f->h->enc, err, &errstr);
2136 exc = (*env)->ExceptionOccurred(env);
2137 if (exc) {
2138 (*env)->DeleteLocalRef(env, exc);
2139 return;
2140 }
2141 sqlite_set_result_error((sqlite_func *) f->sf,
2142 errstr.result, -1);
2143 transfree(&errstr);
2144 } else if (err) {
2145 jsize len = (*env)->GetStringLength(env, err) * sizeof (jchar);
2146 const jchar *str = (*env)->GetStringChars(env, err, 0);
2147
2148 sqlite3_result_error16((sqlite3_context *) f->sf, str, len);
2149 (*env)->ReleaseStringChars(env, err, str);
2150 } else {
2151 sqlite3_result_error((sqlite3_context *) f->sf,
2152 "null error text", -1);
2153 }
2154#else
2155#if HAVE_SQLITE20
2156 transstr errstr;
2157 jthrowable exc;
2158
2159 trans2iso(env, f->h->haveutf, f->h->enc, err, &errstr);
2160 exc = (*env)->ExceptionOccurred(env);
2161 if (exc) {
2162 (*env)->DeleteLocalRef(env, exc);
2163 return;
2164 }
2165 sqlite_set_result_error((sqlite_func *) f->sf, errstr.result, -1);
2166 transfree(&errstr);
2167#endif
2168#if HAVE_SQLITE31
2169 if (err) {
2170 jsize len = (*env)->GetStringLength(env, err) * sizeof (jchar);
2171 const jchar *str = (*env)->GetStringChars(env, err, 0);
2172
2173 sqlite3_result_error16((sqlite3_context *) f->sf, str, len);
2174 (*env)->ReleaseStringChars(env, err, str);
2175 } else {
2176 sqlite3_result_error((sqlite3_context *) f->sf,
2177 "null error text", -1);
2178 }
2179#endif
2180#endif
2181 }
2182}
2183
2184JNIEXPORT__attribute__((visibility("default"))) void JNICALL
2185Java_SQLite_FunctionContext_set_1result__D(JNIEnv *env, jobject obj, jdouble d)
2186{
2187 hfunc *f = getfunc(env, obj);
2188
2189 if (f && f->sf) {
2190#if HAVE_BOTH_SQLITE
2191 if (f->is3) {
2192 sqlite3_result_double((sqlite3_context *) f->sf, (double) d);
2193 } else {
2194 sqlite_set_result_double((sqlite_func *) f->sf, (double) d);
2195 }
2196#else
2197#if HAVE_SQLITE20
2198 sqlite_set_result_double((sqlite_func *) f->sf, (double) d);
2199#endif
2200#if HAVE_SQLITE31
2201 sqlite3_result_double((sqlite3_context *) f->sf, (double) d);
2202#endif
2203#endif
2204 }
2205}
2206
2207JNIEXPORT__attribute__((visibility("default"))) void JNICALL
2208Java_SQLite_FunctionContext_set_1result__I(JNIEnv *env, jobject obj, jint i)
2209{
2210 hfunc *f = getfunc(env, obj);
2211
2212 if (f && f->sf) {
2213#if HAVE_BOTH_SQLITE
2214 if (f->is3) {
2215 sqlite3_result_int((sqlite3_context *) f->sf, (int) i);
2216 } else {
2217 sqlite_set_result_int((sqlite_func *) f->sf, (int) i);
2218 }
2219#else
2220#if HAVE_SQLITE20
2221 sqlite_set_result_int((sqlite_func *) f->sf, (int) i);
2222#endif
2223#if HAVE_SQLITE31
2224 sqlite3_result_int((sqlite3_context *) f->sf, (int) i);
2225#endif
2226#endif
2227 }
2228}
2229
2230JNIEXPORT__attribute__((visibility("default"))) void JNICALL
2231Java_SQLite_FunctionContext_set_1result__Ljava_lang_String_2(JNIEnv *env,
2232 jobject obj,
2233 jstring ret)
2234{
2235 hfunc *f = getfunc(env, obj);
2236
2237 if (f && f->sf) {
2238#if HAVE_BOTH_SQLITE
2239 if (!f->is3) {
2240 transstr retstr;
2241 jthrowable exc;
2242
2243 trans2iso(env, f->h->haveutf, f->h->enc, ret, &retstr);
2244 exc = (*env)->ExceptionOccurred(env);
2245 if (exc) {
2246 (*env)->DeleteLocalRef(env, exc);
2247 return;
2248 }
2249 sqlite_set_result_string((sqlite_func *) f->sf,
2250 retstr.result, -1);
2251 transfree(&retstr);
2252 } else if (ret) {
2253 jsize len = (*env)->GetStringLength(env, ret) * sizeof (jchar);
2254 const jchar *str = (*env)->GetStringChars(env, ret, 0);
2255
2256 sqlite3_result_text16((sqlite3_context *) f->sf, str, len,
2257 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
2258 (*env)->ReleaseStringChars(env, ret, str);
2259 } else {
2260 sqlite3_result_null((sqlite3_context *) f->sf);
2261 }
2262#else
2263#if HAVE_SQLITE20
2264 transstr retstr;
2265 jthrowable exc;
2266
2267 trans2iso(env, f->h->haveutf, f->h->enc, ret, &retstr);
2268 exc = (*env)->ExceptionOccurred(env);
2269 if (exc) {
2270 (*env)->DeleteLocalRef(env, exc);
2271 return;
2272 }
2273 sqlite_set_result_string((sqlite_func *) f->sf, retstr.result, -1);
2274 transfree(&retstr);
2275#endif
2276#if HAVE_SQLITE31
2277 if (ret) {
2278 jsize len = (*env)->GetStringLength(env, ret) * sizeof (jchar);
2279 const jchar *str = (*env)->GetStringChars(env, ret, 0);
2280
2281 sqlite3_result_text16((sqlite3_context *) f->sf, str, len,
2282 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
2283 (*env)->ReleaseStringChars(env, ret, str);
2284 } else {
2285 sqlite3_result_null((sqlite3_context *) f->sf);
2286 }
2287#endif
2288#endif
2289 }
2290}
2291
2292JNIEXPORT__attribute__((visibility("default"))) void JNICALL
2293Java_SQLite_FunctionContext_set_1result___3B(JNIEnv *env, jobject obj,
2294 jbyteArray b)
2295{
2296#if HAVE_SQLITE31
2297 hfunc *f = getfunc(env, obj);
2298
2299 if (f && f->sf) {
2300#if HAVE_BOTH_SQLITE
2301 if (!f->is3) {
2302 /* silently ignored */
2303 return;
2304 }
2305#endif
2306 if (b) {
2307 jsize len;
2308 jbyte *data;
2309
2310 len = (*env)->GetArrayLength(env, b);
2311 data = (*env)->GetByteArrayElements(env, b, 0);
2312 sqlite3_result_blob((sqlite3_context *) f->sf,
2313 data, len, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
2314 (*env)->ReleaseByteArrayElements(env, b, data, 0);
2315 } else {
2316 sqlite3_result_null((sqlite3_context *) f->sf);
2317 }
2318 }
2319#endif
2320}
2321
2322JNIEXPORT__attribute__((visibility("default"))) void JNICALL
2323Java_SQLite_FunctionContext_set_1result_1zeroblob(JNIEnv *env, jobject obj,
2324 jint n)
2325{
2326#if HAVE_SQLITE31 && HAVE_SQLITE3_RESULT_ZEROBLOB1
2327 hfunc *f = getfunc(env, obj);
2328
2329 if (f && f->sf) {
2330#if HAVE_BOTH_SQLITE
2331 if (!f->is3) {
2332 /* silently ignored */
2333 return;
2334 }
2335#endif
2336 sqlite3_result_zeroblob((sqlite3_context *) f->sf, n);
2337 }
2338#endif
2339}
2340
2341JNIEXPORT__attribute__((visibility("default"))) jstring JNICALL
2342Java_SQLite_Database_error_1string(JNIEnv *env, jclass c, jint err)
2343{
2344#if HAVE_SQLITE20
2345 return (*env)->NewStringUTF(env, sqlite_error_string((int) err));
2346#else
2347 return (*env)->NewStringUTF(env, "unkown error");
2348#endif
2349}
2350
2351JNIEXPORT__attribute__((visibility("default"))) jstring JNICALL
2352Java_SQLite_Database__1errmsg(JNIEnv *env, jobject obj)
2353{
2354#if HAVE_SQLITE31
2355 handle *h = gethandle(env, obj);
2356
2357 if (h && h->sqlite) {
2358#if HAVE_BOTH_SQLITE
2359 if (!h->is3) {
2360 return 0;
2361 }
2362#endif
2363 return (*env)->NewStringUTF(env,
2364 sqlite3_errmsg((sqlite3 *) h->sqlite));
2365 }
2366#endif
2367 return 0;
2368}
2369
2370JNIEXPORT__attribute__((visibility("default"))) void JNICALL
2371Java_SQLite_Database__1set_1encoding(JNIEnv *env, jobject obj, jstring enc)
2372{
2373 handle *h = gethandle(env, obj);
2374
2375 if (h && !h->haveutf) {
2376#if HAVE_BOTH_SQLITE
2377 if (!h->is3) {
2378 delglobrefp(env, &h->enc);
2379 h->enc = enc;
2380 globrefset(env, enc, &h->enc);
2381 }
2382#else
2383#if HAVE_SQLITE20
2384 delglobrefp(env, &h->enc);
2385 h->enc = enc;
2386 globrefset(env, enc, &h->enc);
2387#endif
2388#endif
2389 }
2390}
2391
2392#if HAVE_SQLITE_SET_AUTHORIZER0
2393static int
2394doauth(void *arg, int what, const char *arg1, const char *arg2,
2395 const char *arg3, const char *arg4)
2396{
2397 handle *h = (handle *) arg;
2398 JNIEnv *env = h->env;
2399
2400 if (env && h->ai) {
2401 jthrowable exc;
2402 jclass cls = (*env)->GetObjectClass(env, h->ai);
2403 jmethodID mid;
2404 jint i = what;
2405
2406 mid = (*env)->GetMethodID(env, cls, "authorize",
2407 "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I");
2408 if (mid) {
2409 jstring s1 = 0, s2 = 0, s3 = 0, s4 = 0;
2410 transstr tr;
2411
2412 if (arg1) {
2413 trans2utf(env, h->haveutf, h->enc, arg1, &tr);
2414 s1 = tr.jstr;
2415 }
2416 exc = (*env)->ExceptionOccurred(env);
2417 if (exc) {
2418 (*env)->DeleteLocalRef(env, exc);
2419 return SQLITE_DENY1;
2420 }
2421 if (arg2) {
2422 trans2utf(env, h->haveutf, h->enc, arg2, &tr);
2423 s2 = tr.jstr;
2424 }
2425 if (arg3) {
2426 trans2utf(env, h->haveutf, h->enc, arg3, &tr);
2427 s3 = tr.jstr;
2428 }
2429 if (arg4) {
2430 trans2utf(env, h->haveutf, h->enc, arg4, &tr);
2431 s4 = tr.jstr;
2432 }
2433 exc = (*env)->ExceptionOccurred(env);
2434 if (exc) {
2435 (*env)->DeleteLocalRef(env, exc);
2436 return SQLITE_DENY1;
2437 }
2438 i = (*env)->CallIntMethod(env, h->ai, mid, i, s1, s2, s3, s4);
2439 exc = (*env)->ExceptionOccurred(env);
2440 if (exc) {
2441 (*env)->DeleteLocalRef(env, exc);
2442 return SQLITE_DENY1;
2443 }
2444 (*env)->DeleteLocalRef(env, s4);
2445 (*env)->DeleteLocalRef(env, s3);
2446 (*env)->DeleteLocalRef(env, s2);
2447 (*env)->DeleteLocalRef(env, s1);
2448 if (i != SQLITE_OK0 && i != SQLITE_IGNORE2) {
2449 i = SQLITE_DENY1;
2450 }
2451 return (int) i;
2452 }
2453 }
2454 return SQLITE_DENY1;
2455}
2456#endif
2457
2458JNIEXPORT__attribute__((visibility("default"))) void JNICALL
2459Java_SQLite_Database__1set_1authorizer(JNIEnv *env, jobject obj, jobject auth)
2460{
2461 handle *h = gethandle(env, obj);
2462
2463 if (h && h->sqlite) {
2464 delglobrefp(env, &h->ai);
2465 globrefset(env, auth, &h->ai);
2466#if HAVE_SQLITE_SET_AUTHORIZER0
2467 h->env = env;
2468#if HAVE_BOTH_SQLITE
2469 if (h->is3) {
2470 sqlite3_set_authorizer((sqlite3 *) h->sqlite,
2471 h->ai ? doauth : 0, h);
2472 } else {
2473 sqlite_set_authorizer((sqlite *) h->sqlite,
2474 h->ai ? doauth : 0, h);
2475 }
2476#else
2477#if HAVE_SQLITE20
2478 sqlite_set_authorizer((sqlite *) h->sqlite, h->ai ? doauth : 0, h);
2479#endif
2480#if HAVE_SQLITE31
2481 sqlite3_set_authorizer((sqlite3 *) h->sqlite, h->ai ? doauth : 0, h);
2482#endif
2483#endif
2484#endif
2485 return;
2486 }
2487 throwclosed(env);
2488}
2489
2490#if HAVE_SQLITE_TRACE1
2491static void
2492dotrace(void *arg, const char *msg)
2493{
2494 handle *h = (handle *) arg;
2495 JNIEnv *env = h->env;
2496
2497 if (env && h->tr && msg) {
2498 jthrowable exc;
2499 jclass cls = (*env)->GetObjectClass(env, h->tr);
2500 jmethodID mid;
2501
2502 mid = (*env)->GetMethodID(env, cls, "trace", "(Ljava/lang/String;)V");
2503 if (mid) {
2504 transstr tr;
2505
2506 trans2utf(env, h->haveutf, h->enc, msg, &tr);
2507 exc = (*env)->ExceptionOccurred(env);
2508 if (exc) {
2509 (*env)->DeleteLocalRef(env, exc);
2510 (*env)->ExceptionClear(env);
2511 return;
2512 }
2513 (*env)->CallVoidMethod(env, h->tr, mid, tr.jstr);
2514 (*env)->ExceptionClear(env);
2515 (*env)->DeleteLocalRef(env, tr.jstr);
2516 return;
2517 }
2518 }
2519 return;
2520}
2521#endif
2522
2523JNIEXPORT__attribute__((visibility("default"))) void JNICALL
2524Java_SQLite_Database__1trace(JNIEnv *env, jobject obj, jobject tr)
2525{
2526 handle *h = gethandle(env, obj);
2527
2528 if (h && h->sqlite) {
2529 delglobrefp(env, &h->tr);
2530 globrefset(env, tr, &h->tr);
2531#if HAVE_BOTH_SQLITE
2532 if (h->is3) {
2533 sqlite3_trace((sqlite3 *) h->sqlite, h->tr ? dotrace : 0, h);
2534 } else {
2535#if HAVE_SQLITE_TRACE1
2536 sqlite_trace((sqlite *) h->sqlite, h->tr ? dotrace : 0, h);
2537#endif
2538 }
2539#else
2540#if HAVE_SQLITE20
2541#if HAVE_SQLITE_TRACE1
2542 sqlite_trace((sqlite *) h->sqlite, h->tr ? dotrace : 0, h);
2543#endif
2544#endif
2545#if HAVE_SQLITE31
2546 sqlite3_trace((sqlite3 *) h->sqlite, h->tr ? dotrace : 0, h);
2547#endif
2548#endif
2549 return;
2550 }
2551 throwclosed(env);
2552}
2553
2554#if HAVE_SQLITE_COMPILE1
2555static void
2556dovmfinal(JNIEnv *env, jobject obj, int final)
2557{
2558 hvm *v = getclrhvm(env, obj);
2559
2560 if (v) {
2561 if ((*env)->MonitorEnter(env, C_SQLite_Database) != JNI_OK0) {
2562 fprintf(stderrstderr, "dovmfinal: MonitorEnter failed\n");
2563 return;
2564 }
2565 if (v->h) {
2566 handle *h = v->h;
2567 hvm *vv, **vvp;
2568
2569 vvp = &h->vms;
2570 vv = *vvp;
2571 while (vv) {
2572 if (vv == v) {
2573 *vvp = vv->next;
2574 break;
2575 }
2576 vvp = &vv->next;
2577 vv = *vvp;
2578 }
2579 }
2580 (*env)->MonitorExit(env, C_SQLite_Database);
2581 if (v->vm) {
2582#if HAVE_BOTH_SQLITE
2583 if (v->is3) {
2584 sqlite3_finalize((sqlite3_stmt *) v->vm);
2585 } else {
2586 sqlite_finalize((sqlite_vm *) v->vm, 0);
2587 }
2588#else
2589#if HAVE_SQLITE20
2590 sqlite_finalize((sqlite_vm *) v->vm, 0);
2591#endif
2592#if HAVE_SQLITE31
2593 sqlite3_finalize((sqlite3_stmt *) v->vm);
2594#endif
2595#endif
2596 v->vm = 0;
2597 }
2598 free(v);
2599 return;
2600 }
2601 if (!final) {
2602 throwex(env, "vm already closed");
2603 }
2604}
2605#endif
2606
2607#if HAVE_SQLITE31
2608static void
2609dostmtfinal(JNIEnv *env, jobject obj)
2610{
2611 hvm *v = getclrhstmt(env, obj);
2612
2613 if (v) {
2614 if ((*env)->MonitorEnter(env, C_SQLite_Database) != JNI_OK0) {
2615 fprintf(stderrstderr, "dostmtfinal: MonitorEnter failed\n");
2616 return;
2617 }
2618 if (v->h) {
2619 handle *h = v->h;
2620 hvm *vv, **vvp;
2621
2622 vvp = &h->vms;
2623 vv = *vvp;
2624 while (vv) {
2625 if (vv == v) {
2626 *vvp = vv->next;
2627 break;
2628 }
2629 vvp = &vv->next;
2630 vv = *vvp;
2631 }
2632 }
2633 (*env)->MonitorExit(env, C_SQLite_Database);
2634 if (v->vm) {
2635 sqlite3_finalize((sqlite3_stmt *) v->vm);
2636 }
2637 v->vm = 0;
2638 free(v);
2639 }
2640}
2641#endif
2642
2643#if HAVE_SQLITE31 && HAVE_SQLITE3_INCRBLOBIO1
2644static void
2645doblobfinal(JNIEnv *env, jobject obj)
2646{
2647 hbl *bl = getclrhbl(env, obj);
2648
2649 if (bl) {
2650 if ((*env)->MonitorEnter(env, C_SQLite_Database) != JNI_OK0) {
2651 fprintf(stderrstderr, "doblobfinal: MonitorEnter failed\n");
2652 return;
2653 }
2654 if (bl->h) {
2655 handle *h = bl->h;
2656 hbl *blc, **blp;
2657
2658 blp = &h->blobs;
2659 blc = *blp;
2660 while (blc) {
2661 if (blc == bl) {
2662 *blp = blc->next;
2663 break;
2664 }
2665 blp = &blc->next;
2666 blc = *blp;
2667 }
2668 }
2669 (*env)->MonitorExit(env, C_SQLite_Database);
2670 if (bl->blob) {
2671 sqlite3_blob_close(bl->blob);
2672 }
2673 bl->blob = 0;
2674 free(bl);
2675 (*env)->SetIntField(env, obj, F_SQLite_Blob_size, 0);
2676 }
2677}
2678#endif
2679
2680JNIEXPORT__attribute__((visibility("default"))) void JNICALL
2681Java_SQLite_Vm_stop(JNIEnv *env, jobject obj)
2682{
2683#if HAVE_SQLITE_COMPILE1
2684 dovmfinal(env, obj, 0);
2685#else
2686 throwex(env, "unsupported");
2687#endif
2688}
2689
2690JNIEXPORT__attribute__((visibility("default"))) void JNICALL
2691Java_SQLite_Vm_finalize(JNIEnv *env, jobject obj)
2692{
2693#if HAVE_SQLITE_COMPILE1
2694 dovmfinal(env, obj, 1);
2695#endif
2696}
2697
2698#if HAVE_SQLITE_COMPILE1
2699#if HAVE_SQLITE31
2700static void
2701free_tab(void *mem)
2702{
2703 char **p = (char **) mem;
2704 int i, n;
2705
2706 if (!p) {
2707 return;
2708 }
2709 p -= 1;
2710 mem = (void *) p;
2711 n = ((int *) p)[0];
2712 p += n * 2 + 2 + 1;
2713 for (i = 0; i < n; i++) {
2714 if (p[i]) {
2715 free(p[i]);
2716 }
2717 }
2718 free(mem);
2719}
2720#endif
2721#endif
2722
2723JNIEXPORT__attribute__((visibility("default"))) jboolean JNICALL
2724Java_SQLite_Vm_step(JNIEnv *env, jobject obj, jobject cb)
2725{
2726#if HAVE_SQLITE_COMPILE1
2727 hvm *v = gethvm(env, obj);
2728
2729 if (v && v->vm && v->h) {
2730 jthrowable exc;
2731 int ret;
2732#if HAVE_SQLITE20
2733 int tmp;
2734#endif
2735 long ncol = 0;
2736#if HAVE_SQLITE31
2737 freemem *freeproc = 0;
2738 const char **blob = 0;
2739#endif
2740 const char **data = 0, **cols = 0;
2741
2742 v->h->env = env;
2743#if HAVE_BOTH_SQLITE
2744 if (v->is3) {
2745 ret = sqlite3_step((sqlite3_stmt *) v->vm);
2746 if (ret == SQLITE_DONE101 && v->hh.row1) {
2747 ncol = sqlite3_column_count((sqlite3_stmt *) v->vm);
2748 if (ncol > 0) {
2749 data = calloc(ncol * 3 + 3 + 1, sizeof (char *));
2750 if (data) {
2751 data[0] = (const char *) ncol;
2752 ++data;
2753 cols = data + ncol + 1;
2754 blob = cols + ncol + 1;
2755 freeproc = free_tab;
2756 } else {
2757 ret = SQLITE_NOMEM7;
2758 }
2759 }
2760 if (ret != SQLITE_NOMEM7) {
2761 int i;
2762
2763 for (i = 0; i < ncol; i++) {
2764 cols[i] =
2765 sqlite3_column_name((sqlite3_stmt *) v->vm, i);
2766 }
2767 }
2768 } else if (ret == SQLITE_ROW100) {
2769 ncol = sqlite3_data_count((sqlite3_stmt *) v->vm);
2770 if (ncol > 0) {
2771 data = calloc(ncol * 3 + 3 + 1, sizeof (char *));
2772 if (data) {
2773 data[0] = (const char *) ncol;
2774 ++data;
2775 cols = data + ncol + 1;
2776 blob = cols + ncol + 1;
2777 freeproc = free_tab;
2778 } else {
2779 ret = SQLITE_NOMEM7;
2780 }
2781 }
2782 if (ret != SQLITE_NOMEM7) {
2783 int i;
2784
2785 for (i = 0; i < ncol; i++) {
2786 cols[i] =
2787 sqlite3_column_name((sqlite3_stmt *) v->vm, i);
2788 if (sqlite3_column_type((sqlite3_stmt *) v->vm, i)
2789 == SQLITE_BLOB4) {
2790 unsigned char *src = (unsigned char *)
2791 sqlite3_column_blob((sqlite3_stmt *) v->vm, i);
2792 int n =
2793 sqlite3_column_bytes((sqlite3_stmt *) v->vm,
2794 i);
2795
2796 if (src) {
2797 data[i] = malloc(n * 2 + 4);
2798 if (data[i]) {
2799 int k;
2800 char *p = (char *) data[i];
2801
2802 blob[i] = data[i];
2803 *p++ = 'X';
2804 *p++ = '\'';
2805 for (k = 0; k < n; k++) {
2806 *p++ = xdigits[src[k] >> 4];
2807 *p++ = xdigits[src[k] & 0x0F];
2808 }
2809 *p++ = '\'';
2810 *p++ = '\0';
2811 }
2812 }
2813 } else {
2814 data[i] = (const char *)
2815 sqlite3_column_text((sqlite3_stmt *) v->vm, i);
2816 }
2817 }
2818 }
2819 }
2820 } else {
2821 tmp = 0;
2822 ret = sqlite_step((sqlite_vm *) v->vm, &tmp, &data, &cols);
2823 ncol = tmp;
2824 }
2825#else
2826#if HAVE_SQLITE20
2827 tmp = 0;
2828 ret = sqlite_step((sqlite_vm *) v->vm, &tmp, &data, &cols);
2829 ncol = tmp;
2830#endif
2831#if HAVE_SQLITE31
2832 ret = sqlite3_step((sqlite3_stmt *) v->vm);
2833 if (ret == SQLITE_DONE101 && v->hh.row1) {
2834 ncol = sqlite3_column_count((sqlite3_stmt *) v->vm);
2835 if (ncol > 0) {
2836 data = calloc(ncol * 3 + 3 + 1, sizeof (char *));
2837 if (data) {
2838 data[0] = (const char *) ncol;
2839 ++data;
2840 cols = data + ncol + 1;
2841 blob = cols + ncol + 1;
Value stored to 'blob' is never read
2842 freeproc = free_tab;
2843 } else {
2844 ret = SQLITE_NOMEM7;
2845 }
2846 }
2847 if (ret != SQLITE_NOMEM7) {
2848 int i;
2849
2850 for (i = 0; i < ncol; i++) {
2851 cols[i] =
2852 sqlite3_column_name((sqlite3_stmt *) v->vm, i);
2853 }
2854 }
2855 } else if (ret == SQLITE_ROW100) {
2856 ncol = sqlite3_data_count((sqlite3_stmt *) v->vm);
2857 if (ncol > 0) {
2858 data = calloc(ncol * 3 + 3 + 1, sizeof (char *));
2859 if (data) {
2860 data[0] = (const char *) ncol;
2861 ++data;
2862 cols = data + ncol + 1;
2863 blob = cols + ncol + 1;
2864 freeproc = free_tab;
2865 } else {
2866 ret = SQLITE_NOMEM7;
2867 }
2868 }
2869 if (ret != SQLITE_NOMEM7) {
2870 int i;
2871
2872 for (i = 0; i < ncol; i++) {
2873 cols[i] = sqlite3_column_name((sqlite3_stmt *) v->vm, i);
2874 if (sqlite3_column_type((sqlite3_stmt *) v->vm, i)
2875 == SQLITE_BLOB4) {
2876 unsigned char *src = (unsigned char *)
2877 sqlite3_column_blob((sqlite3_stmt *) v->vm, i);
2878 int n =
2879 sqlite3_column_bytes((sqlite3_stmt *) v->vm, i);
2880
2881 if (src) {
2882 data[i] = malloc(n * 2 + 4);
2883 if (data[i]) {
2884 int k;
2885 char *p = (char *) data[i];
2886
2887 blob[i] = data[i];
2888 *p++ = 'X';
2889 *p++ = '\'';
2890 for (k = 0; k < n; k++) {
2891 *p++ = xdigits[src[k] >> 4];
2892 *p++ = xdigits[src[k] & 0x0F];
2893 }
2894 *p++ = '\'';
2895 *p++ = '\0';
2896 }
2897 }
2898 } else {
2899 data[i] = (char *)
2900 sqlite3_column_text((sqlite3_stmt *) v->vm, i);
2901 }
2902 }
2903 }
2904 }
2905#endif
2906#endif
2907 if (ret == SQLITE_ROW100) {
2908 v->hh.cb = cb;
2909 v->hh.env = env;
2910#if HAVE_BOTH_SQLITE
2911 if (v->is3) {
2912 v->hh.stmt = (sqlite3_stmt *) v->vm;
2913 }
2914#else
2915#if HAVE_SQLITE31
2916 v->hh.stmt = (sqlite3_stmt *) v->vm;
2917#endif
2918#endif
2919 callback((void *) &v->hh, ncol, (char **) data, (char **) cols);
2920#if HAVE_SQLITE31
2921 if (data && freeproc) {
2922 freeproc((void *) data);
2923 data = 0;
2924 }
2925#endif
2926 exc = (*env)->ExceptionOccurred(env);
2927 if (exc) {
2928 (*env)->DeleteLocalRef(env, exc);
2929 goto dofin;
2930 }
2931 return JNI_TRUE1;
2932 } else if (ret == SQLITE_DONE101) {
2933dofin:
2934 if (v->hh.row1 && cols) {
2935 v->hh.cb = cb;
2936 v->hh.env = env;
2937#if HAVE_BOTH_SQLITE
2938 if (v->is3) {
2939 v->hh.stmt = (sqlite3_stmt *) v->vm;
2940 }
2941#else
2942#if HAVE_SQLITE31
2943 v->hh.stmt = (sqlite3_stmt *) v->vm;
2944#endif
2945#endif
2946 callback((void *) &v->hh, ncol, (char **) 0, (char **) cols);
2947#if HAVE_SQLITE31
2948 if (data && freeproc) {
2949 freeproc((void *) data);
2950 data = 0;
2951 }
2952#endif
2953 exc = (*env)->ExceptionOccurred(env);
2954 if (exc) {
2955 (*env)->DeleteLocalRef(env, exc);
2956 }
2957 }
2958#if HAVE_BOTH_SQLITE
2959 if (v->is3) {
2960 sqlite3_finalize((sqlite3_stmt *) v->vm);
2961 } else {
2962 sqlite_finalize((sqlite_vm *) v->vm, 0);
2963 }
2964#else
2965#if HAVE_SQLITE20
2966 sqlite_finalize((sqlite_vm *) v->vm, 0);
2967#endif
2968#if HAVE_SQLITE31
2969 sqlite3_finalize((sqlite3_stmt *) v->vm);
2970#endif
2971#endif
2972 v->vm = 0;
2973 return JNI_FALSE0;
2974 }
2975#if HAVE_BOTH_SQLITE
2976 if (v->is3) {
2977 sqlite3_finalize((sqlite3_stmt *) v->vm);
2978 } else {
2979 sqlite_finalize((sqlite_vm *) v->vm, 0);
2980 }
2981#else
2982#if HAVE_SQLITE20
2983 sqlite_finalize((sqlite_vm *) v->vm, 0);
2984#endif
2985#if HAVE_SQLITE31
2986 sqlite3_finalize((sqlite3_stmt *) v->vm);
2987#endif
2988#endif
2989 setvmerr(env, obj, ret);
2990 v->vm = 0;
2991 throwex(env, "error in step");
2992 return JNI_FALSE0;
2993 }
2994 throwex(env, "vm already closed");
2995#else
2996 throwex(env, "unsupported");
2997#endif
2998 return JNI_FALSE0;
2999}
3000
3001JNIEXPORT__attribute__((visibility("default"))) jboolean JNICALL
3002Java_SQLite_Vm_compile(JNIEnv *env, jobject obj)
3003{
3004#if HAVE_SQLITE_COMPILE1
3005 hvm *v = gethvm(env, obj);
3006 void *svm = 0;
3007 char *err = 0;
3008#ifdef HAVE_SQLITE20
3009 char *errfr = 0;
3010#endif
3011 const char *tail;
3012 int ret;
3013
3014 if (v && v->vm) {
3015#if HAVE_BOTH_SQLITE
3016 if (v->is3) {
3017 sqlite3_finalize((sqlite3_stmt *) v->vm);
3018 } else {
3019 sqlite_finalize((sqlite_vm *) v->vm, 0);
3020 }
3021#else
3022#if HAVE_SQLITE20
3023 sqlite_finalize((sqlite_vm *) v->vm, 0);
3024#endif
3025#if HAVE_SQLITE31
3026 sqlite3_finalize((sqlite3_stmt *) v->vm);
3027#endif
3028#endif
3029 v->vm = 0;
3030 }
3031 if (v && v->h && v->h->sqlite) {
3032 if (!v->tail) {
3033 return JNI_FALSE0;
3034 }
3035 v->h->env = env;
3036#if HAVE_BOTH_SQLITE
3037 if (v->is3) {
3038#if HAVE_SQLITE3_PREPARE_V21
3039 ret = sqlite3_prepare_v2((sqlite3 *) v->h->sqlite, v->tail, -1,
3040 (sqlite3_stmt **) &svm, &tail);
3041#else
3042 ret = sqlite3_prepare((sqlite3 *) v->h->sqlite, v->tail, -1,
3043 (sqlite3_stmt **) &svm, &tail);
3044#endif
3045 if (ret != SQLITE_OK0) {
3046 if (svm) {
3047 sqlite3_finalize((sqlite3_stmt *) svm);
3048 svm = 0;
3049 }
3050 err = (char *) sqlite3_errmsg((sqlite3 *) v->h->sqlite);
3051 }
3052 } else {
3053 ret = sqlite_compile((sqlite *) v->h->sqlite, v->tail,
3054 &tail, (sqlite_vm **) &svm, &errfr);
3055 if (ret != SQLITE_OK0) {
3056 err = errfr;
3057 if (svm) {
3058 sqlite_finalize((sqlite_vm *) svm, 0);
3059 svm = 0;
3060 }
3061 }
3062 }
3063#else
3064#if HAVE_SQLITE20
3065 ret = sqlite_compile((sqlite *) v->h->sqlite, v->tail,
3066 &tail, (sqlite_vm **) &svm, &errfr);
3067 if (ret != SQLITE_OK0) {
3068 err = errfr;
3069 if (svm) {
3070 sqlite_finalize((sqlite_vm *) svm, 0);
3071 svm = 0;
3072 }
3073 }
3074#endif
3075#if HAVE_SQLITE31
3076#if HAVE_SQLITE3_PREPARE_V21
3077 ret = sqlite3_prepare_v2((sqlite3 *) v->h->sqlite,
3078 v->tail, -1, (sqlite3_stmt **) &svm, &tail);
3079#else
3080 ret = sqlite3_prepare((sqlite3 *) v->h->sqlite,
3081 v->tail, -1, (sqlite3_stmt **) &svm, &tail);
3082#endif
3083 if (ret != SQLITE_OK0) {
3084 if (svm) {
3085 sqlite3_finalize((sqlite3_stmt *) svm);
3086 svm = 0;
3087 }
3088 err = (char *) sqlite3_errmsg((sqlite3 *) v->h->sqlite);
3089 }
3090#endif
3091#endif
3092 if (ret != SQLITE_OK0) {
3093 setvmerr(env, obj, ret);
3094 v->tail = 0;
3095 throwex(env, err ? err : "error in compile/prepare");
3096#if HAVE_SQLITE20
3097 if (errfr) {
3098 sqlite_freemem(errfr);
3099 }
3100#endif
3101 return JNI_FALSE0;
3102 }
3103#if HAVE_SQLITE20
3104 if (errfr) {
3105 sqlite_freemem(errfr);
3106 }
3107#endif
3108 if (!svm) {
3109 v->tail = 0;
3110 return JNI_FALSE0;
3111 }
3112 v->vm = svm;
3113 v->tail = (char *) tail;
3114 v->hh.row1 = 1;
3115 return JNI_TRUE1;
3116 }
3117 throwex(env, "vm already closed");
3118#else
3119 throwex(env, "unsupported");
3120#endif
3121 return JNI_FALSE0;
3122}
3123
3124JNIEXPORT__attribute__((visibility("default"))) void JNICALL
3125Java_SQLite_Database_vm_1compile(JNIEnv *env, jobject obj, jstring sql,
3126 jobject vm)
3127{
3128#if HAVE_SQLITE_COMPILE1
3129 handle *h = gethandle(env, obj);
3130 void *svm = 0;
3131 hvm *v;
3132 char *err = 0;
3133#if HAVE_SQLITE20
3134 char *errfr = 0;
3135#endif
3136 const char *tail;
3137 transstr tr;
3138 jvalue vv;
3139 int ret;
3140 jthrowable exc;
3141
3142 if (!h) {
3143 throwclosed(env);
3144 return;
3145 }
3146 if (!vm) {
3147 throwex(env, "null vm");
3148 return;
3149 }
3150 if (!sql) {
3151 throwex(env, "null sql");
3152 return;
3153 }
3154 trans2iso(env, h->haveutf, h->enc, sql, &tr);
3155 exc = (*env)->ExceptionOccurred(env);
3156 if (exc) {
3157 (*env)->DeleteLocalRef(env, exc);
3158 return;
3159 }
3160 h->env = env;
3161#if HAVE_BOTH_SQLITE
3162 if (h->is3) {
3163#if HAVE_SQLITE3_PREPARE_V21
3164 ret = sqlite3_prepare_v2((sqlite3 *) h->sqlite, tr.result, -1,
3165 (sqlite3_stmt **) &svm, &tail);
3166#else
3167 ret = sqlite3_prepare((sqlite3 *) h->sqlite, tr.result, -1,
3168 (sqlite3_stmt **) &svm, &tail);
3169#endif
3170 if (ret != SQLITE_OK0) {
3171 if (svm) {
3172 sqlite3_finalize((sqlite3_stmt *) svm);
3173 svm = 0;
3174 }
3175 err = (char *) sqlite3_errmsg((sqlite3 *) h->sqlite);
3176 }
3177 } else {
3178 ret = sqlite_compile((sqlite *) h->sqlite, tr.result, &tail,
3179 (sqlite_vm **) &svm, &errfr);
3180 if (ret != SQLITE_OK0) {
3181 err = errfr;
3182 if (svm) {
3183 sqlite_finalize((sqlite_vm *) svm, 0);
3184 }
3185 }
3186 }
3187#else
3188#if HAVE_SQLITE20
3189 ret = sqlite_compile((sqlite *) h->sqlite, tr.result, &tail,
3190 (sqlite_vm **) &svm, &errfr);
3191 if (ret != SQLITE_OK0) {
3192 err = errfr;
3193 if (svm) {
3194 sqlite_finalize((sqlite_vm *) svm, 0);
3195 svm = 0;
3196 }
3197 }
3198#endif
3199#if HAVE_SQLITE31
3200#if HAVE_SQLITE3_PREPARE_V21
3201 ret = sqlite3_prepare_v2((sqlite3 *) h->sqlite, tr.result, -1,
3202 (sqlite3_stmt **) &svm, &tail);
3203#else
3204 ret = sqlite3_prepare((sqlite3 *) h->sqlite, tr.result, -1,
3205 (sqlite3_stmt **) &svm, &tail);
3206#endif
3207 if (ret != SQLITE_OK0) {
3208 if (svm) {
3209 sqlite3_finalize((sqlite3_stmt *) svm);
3210 svm = 0;
3211 }
3212 err = (char *) sqlite3_errmsg((sqlite3 *) h->sqlite);
3213 }
3214#endif
3215#endif
3216 if (ret != SQLITE_OK0) {
3217 transfree(&tr);
3218 setvmerr(env, vm, ret);
3219 throwex(env, err ? err : "error in prepare/compile");
3220#if HAVE_SQLITE20
3221 if (errfr) {
3222 sqlite_freemem(errfr);
3223 }
3224#endif
3225 return;
3226 }
3227#if HAVE_SQLITE20
3228 if (errfr) {
3229 sqlite_freemem(errfr);
3230 }
3231#endif
3232 if (!svm) {
3233 transfree(&tr);
3234 return;
3235 }
3236 v = malloc(sizeof (hvm) + strlen(tail) + 1);
3237 if (!v) {
3238 transfree(&tr);
3239#if HAVE_BOTH_SQLITE
3240 if (h->is3) {
3241 sqlite3_finalize((sqlite3_stmt *) svm);
3242 } else {
3243 sqlite_finalize((sqlite_vm *) svm, 0);
3244 }
3245#else
3246#if HAVE_SQLITE20
3247 sqlite_finalize((sqlite_vm *) svm, 0);
3248#endif
3249#if HAVE_SQLITE31
3250 sqlite3_finalize((sqlite3_stmt *) svm);
3251#endif
3252#endif
3253 throwoom(env, "unable to get SQLite handle");
3254 return;
3255 }
3256 v->next = h->vms;
3257 h->vms = v;
3258 v->vm = svm;
3259 v->h = h;
3260 v->tail = (char *) (v + 1);
3261#if HAVE_BOTH_SQLITE
3262 v->is3 = v->hh.is3 = h->is3;
3263#endif
3264 strcpy(v->tail, tail);
3265 v->hh.sqlite = 0;
3266 v->hh.haveutf = h->haveutf;
3267 v->hh.ver = h->ver;
3268 v->hh.bh = v->hh.cb = v->hh.ai = v->hh.tr = v->hh.ph = 0;
3269 v->hh.row1 = 1;
3270 v->hh.enc = h->enc;
3271 v->hh.funcs = 0;
3272 v->hh.vms = 0;
3273 v->hh.env = 0;
3274 vv.j = 0;
3275 vv.l = (jobject) v;
3276 (*env)->SetLongField(env, vm, F_SQLite_Vm_handle, vv.j);
3277 transfree(&tr);
3278#else
3279 throwex(env, "unsupported");
3280#endif
3281}
3282
3283JNIEXPORT__attribute__((visibility("default"))) void JNICALL
3284Java_SQLite_Database_vm_1compile_1args(JNIEnv *env,
3285 jobject obj, jstring sql,
3286 jobject vm, jobjectArray args)
3287{
3288#if HAVE_SQLITE_COMPILE1
3289#if HAVE_SQLITE31
3290 handle *h = gethandle(env, obj);
3291#endif
3292
3293#if HAVE_BOTH_SQLITE
3294 if (h && !h->is3) {
3295 throwex(env, "unsupported");
3296 return;
3297 }
3298#else
3299#if HAVE_SQLITE20
3300 throwex(env, "unsupported");
3301#endif
3302#endif
3303#if HAVE_SQLITE31
3304 if (!h || !h->sqlite) {
3305 throwclosed(env);
3306 return;
3307 }
3308 if (!vm) {
3309 throwex(env, "null vm");
3310 return;
3311 }
3312 if (!sql) {
3313 throwex(env, "null sql");
3314 return;
3315 } else {
3316 void *svm = 0;
3317 hvm *v;
3318 jvalue vv;
3319 jthrowable exc;
3320 int rc = SQLITE_ERROR1, nargs, i;
3321 char *p;
3322 const char *str = (*env)->GetStringUTFChars(env, sql, 0);
3323 const char *tail;
3324 transstr sqlstr;
3325 struct args {
3326 char *arg;
3327 jobject obj;
3328 transstr trans;
3329 } *argv = 0;
3330 char **cargv = 0;
3331
3332 p = (char *) str;
3333 nargs = 0;
3334 while (*p) {
3335 if (*p == '%') {
3336 ++p;
3337 if (*p == 'q' || *p == 'Q' || *p == 's') {
3338 nargs++;
3339 if (nargs > MAX_PARAMS32) {
3340 (*env)->ReleaseStringUTFChars(env, sql, str);
3341 throwex(env, "too much SQL parameters");
3342 return;
3343 }
3344 } else if (*p != '%') {
3345 (*env)->ReleaseStringUTFChars(env, sql, str);
3346 throwex(env, "bad % specification in query");
3347 return;
3348 }
3349 }
3350 ++p;
3351 }
3352 cargv = malloc((sizeof (*argv) + sizeof (char *)) * MAX_PARAMS32);
3353 if (!cargv) {
3354 (*env)->ReleaseStringUTFChars(env, sql, str);
3355 throwoom(env, "unable to allocate arg vector");
3356 return;
3357 }
3358 argv = (struct args *) (cargv + MAX_PARAMS32);
3359 for (i = 0; i < MAX_PARAMS32; i++) {
3360 cargv[i] = 0;
3361 argv[i].arg = 0;
3362 argv[i].obj = 0;
3363 argv[i].trans.result = argv[i].trans.tofree = 0;
3364 }
3365 exc = 0;
3366 for (i = 0; i < nargs; i++) {
3367 jobject so = (*env)->GetObjectArrayElement(env, args, i);
3368
3369 exc = (*env)->ExceptionOccurred(env);
3370 if (exc) {
3371 (*env)->DeleteLocalRef(env, exc);
3372 break;
3373 }
3374 if (so) {
3375 argv[i].obj = so;
3376 argv[i].arg = cargv[i] =
3377 trans2iso(env, 1, 0, argv[i].obj, &argv[i].trans);
3378 }
3379 }
3380 if (exc) {
3381 for (i = 0; i < nargs; i++) {
3382 if (argv[i].obj) {
3383 transfree(&argv[i].trans);
3384 }
3385 }
3386 freep((char **) &cargv);
3387 (*env)->ReleaseStringUTFChars(env, sql, str);
3388 return;
3389 }
3390 h->row1 = 1;
3391 trans2iso(env, 1, 0, sql, &sqlstr);
3392 exc = (*env)->ExceptionOccurred(env);
3393 if (!exc) {
3394#if defined(_WIN32) || !defined(CANT_PASS_VALIST_AS_CHARPTR1)
3395 char *s = sqlite3_vmprintf(sqlstr.result, (char *) cargv);
3396#else
3397 char *s = sqlite3_mprintf(sqlstr.result,
3398 cargv[0], cargv[1],
3399 cargv[2], cargv[3],
3400 cargv[4], cargv[5],
3401 cargv[6], cargv[7],
3402 cargv[8], cargv[9],
3403 cargv[10], cargv[11],
3404 cargv[12], cargv[13],
3405 cargv[14], cargv[15],
3406 cargv[16], cargv[17],
3407 cargv[18], cargv[19],
3408 cargv[20], cargv[21],
3409 cargv[22], cargv[23],
3410 cargv[24], cargv[25],
3411 cargv[26], cargv[27],
3412 cargv[28], cargv[29],
3413 cargv[30], cargv[31]);
3414#endif
3415 if (!s) {
3416 rc = SQLITE_NOMEM7;
3417 } else {
3418#if HAVE_SQLITE3_PREPARE_V21
3419 rc = sqlite3_prepare_v2((sqlite3 *) h->sqlite, s, -1,
3420 (sqlite3_stmt **) &svm, &tail);
3421#else
3422 rc = sqlite3_prepare((sqlite3 *) h->sqlite, s, -1,
3423 (sqlite3_stmt **) &svm, &tail);
3424#endif
3425 if (rc != SQLITE_OK0) {
3426 if (svm) {
3427 sqlite3_finalize((sqlite3_stmt *) svm);
3428 svm = 0;
3429 }
3430 }
3431 }
3432 if (rc != SQLITE_OK0) {
3433 sqlite3_free(s);
3434 for (i = 0; i < nargs; i++) {
3435 if (argv[i].obj) {
3436 transfree(&argv[i].trans);
3437 }
3438 }
3439 freep((char **) &cargv);
3440 transfree(&sqlstr);
3441 (*env)->ReleaseStringUTFChars(env, sql, str);
3442 setvmerr(env, vm, rc);
3443 throwex(env, "error in prepare");
3444 return;
3445 }
3446 v = malloc(sizeof (hvm) + strlen(tail) + 1);
3447 if (!v) {
3448 sqlite3_free(s);
3449 for (i = 0; i < nargs; i++) {
3450 if (argv[i].obj) {
3451 transfree(&argv[i].trans);
3452 }
3453 }
3454 freep((char **) &cargv);
3455 transfree(&sqlstr);
3456 (*env)->ReleaseStringUTFChars(env, sql, str);
3457 sqlite3_finalize((sqlite3_stmt *) svm);
3458 setvmerr(env, vm, SQLITE_NOMEM7);
3459 throwoom(env, "unable to get SQLite handle");
3460 return;
3461 }
3462 v->next = h->vms;
3463 h->vms = v;
3464 v->vm = svm;
3465 v->h = h;
3466 v->tail = (char *) (v + 1);
3467#if HAVE_BOTH_SQLITE
3468 v->is3 = v->hh.is3 = h->is3;
3469#endif
3470 strcpy(v->tail, tail);
3471 sqlite3_free(s);
3472 v->hh.sqlite = 0;
3473 v->hh.haveutf = h->haveutf;
3474 v->hh.ver = h->ver;
3475 v->hh.bh = v->hh.cb = v->hh.ai = v->hh.tr = v->hh.ph = 0;
3476 v->hh.row1 = 1;
3477 v->hh.enc = h->enc;
3478 v->hh.funcs = 0;
3479 v->hh.vms = 0;
3480 v->hh.env = 0;
3481 vv.j = 0;
3482 vv.l = (jobject) v;
3483 (*env)->SetLongField(env, vm, F_SQLite_Vm_handle, vv.j);
3484 }
3485 for (i = 0; i < nargs; i++) {
3486 if (argv[i].obj) {
3487 transfree(&argv[i].trans);
3488 }
3489 }
3490 freep((char **) &cargv);
3491 transfree(&sqlstr);
3492 (*env)->ReleaseStringUTFChars(env, sql, str);
3493 if (exc) {
3494 (*env)->DeleteLocalRef(env, exc);
3495 }
3496 }
3497#endif
3498#else
3499 throwex(env, "unsupported");
3500#endif
3501}
3502
3503JNIEXPORT__attribute__((visibility("default"))) void JNICALL
3504Java_SQLite_FunctionContext_internal_1init(JNIEnv *env, jclass cls)
3505{
3506 F_SQLite_FunctionContext_handle =
3507 (*env)->GetFieldID(env, cls, "handle", "J");
3508}
3509
3510JNIEXPORT__attribute__((visibility("default"))) void JNICALL
3511Java_SQLite_Database__1progress_1handler(JNIEnv *env, jobject obj, jint n,
3512 jobject ph)
3513{
3514 handle *h = gethandle(env, obj);
3515
3516 if (h && h->sqlite) {
3517 /* CHECK THIS */
3518#if HAVE_SQLITE_PROGRESS_HANDLER1
3519 delglobrefp(env, &h->ph);
3520#if HAVE_BOTH_SQLITE
3521 if (h->is3) {
3522 if (ph) {
3523 globrefset(env, ph, &h->ph);
3524 sqlite3_progress_handler((sqlite3 *) h->sqlite,
3525 n, progresshandler, h);
3526 } else {
3527 sqlite3_progress_handler((sqlite3 *) h->sqlite,
3528 0, 0, 0);
3529 }
3530 } else {
3531 if (ph) {
3532 globrefset(env, ph, &h->ph);
3533 sqlite_progress_handler((sqlite *) h->sqlite,
3534 n, progresshandler, h);
3535 } else {
3536 sqlite_progress_handler((sqlite *) h->sqlite,
3537 0, 0, 0);
3538 }
3539 }
3540#else
3541#if HAVE_SQLITE20
3542 if (ph) {
3543 globrefset(env, ph, &h->ph);
3544 sqlite_progress_handler((sqlite *) h->sqlite,
3545 n, progresshandler, h);
3546 } else {
3547 sqlite_progress_handler((sqlite *) h->sqlite,
3548 0, 0, 0);
3549 }
3550#endif
3551#if HAVE_SQLITE31
3552 if (ph) {
3553 globrefset(env, ph, &h->ph);
3554 sqlite3_progress_handler((sqlite3 *) h->sqlite,
3555 n, progresshandler, h);
3556 } else {
3557 sqlite3_progress_handler((sqlite3 *) h->sqlite,
3558 0, 0, 0);
3559 }
3560#endif
3561#endif
3562 return;
3563#else
3564 throwex(env, "unsupported");
3565 return;
3566#endif
3567 }
3568 throwclosed(env);
3569}
3570
3571JNIEXPORT__attribute__((visibility("default"))) jboolean JNICALL
3572Java_SQLite_Database_is3(JNIEnv *env, jobject obj)
3573{
3574#if HAVE_BOTH_SQLITE
3575 handle *h = gethandle(env, obj);
3576
3577 if (h) {
3578 return h->is3 ? JNI_TRUE1 : JNI_FALSE0;
3579 }
3580 return JNI_FALSE0;
3581#else
3582#if HAVE_SQLITE20
3583 return JNI_FALSE0;
3584#endif
3585#if HAVE_SQLITE31
3586 return JNI_TRUE1;
3587#endif
3588#endif
3589}
3590
3591JNIEXPORT__attribute__((visibility("default"))) jboolean JNICALL
3592Java_SQLite_Stmt_prepare(JNIEnv *env, jobject obj)
3593{
3594#if HAVE_SQLITE31
3595 hvm *v = gethstmt(env, obj);
3596 void *svm = 0;
3597 char *tail;
3598 int ret;
3599
3600 if (v && v->vm) {
3601 sqlite3_finalize((sqlite3_stmt *) v->vm);
3602 v->vm = 0;
3603 }
3604 if (v && v->h && v->h->sqlite) {
3605 if (!v->tail) {
3606 return JNI_FALSE0;
3607 }
3608 v->h->env = env;
3609#if HAVE_SQLITE3_PREPARE16_V21
3610 ret = sqlite3_prepare16_v2((sqlite3 *) v->h->sqlite,
3611 v->tail, -1, (sqlite3_stmt **) &svm,
3612 (const void **) &tail);
3613#else
3614 ret = sqlite3_prepare16((sqlite3 *) v->h->sqlite,
3615 v->tail, -1, (sqlite3_stmt **) &svm,
3616 (const void **) &tail);
3617#endif
3618 if (ret != SQLITE_OK0) {
3619 if (svm) {
3620 sqlite3_finalize((sqlite3_stmt *) svm);
3621 svm = 0;
3622 }
3623 }
3624 if (ret != SQLITE_OK0) {
3625 const char *err = sqlite3_errmsg(v->h->sqlite);
3626
3627 setstmterr(env, obj, ret);
3628 v->tail = 0;
3629 throwex(env, err ? err : "error in compile/prepare");
3630 return JNI_FALSE0;
3631 }
3632 if (!svm) {
3633 v->tail = 0;
3634 return JNI_FALSE0;
3635 }
3636 v->vm = svm;
3637 v->tail = (char *) tail;
3638 v->hh.row1 = 1;
3639 return JNI_TRUE1;
3640 }
3641 throwex(env, "stmt already closed");
3642#else
3643 throwex(env, "unsupported");
3644#endif
3645 return JNI_FALSE0;
3646}
3647
3648JNIEXPORT__attribute__((visibility("default"))) void JNICALL
3649Java_SQLite_Database_stmt_1prepare(JNIEnv *env, jobject obj, jstring sql,
3650 jobject stmt)
3651{
3652#if HAVE_SQLITE31
3653 handle *h = gethandle(env, obj);
3654 void *svm = 0;
3655 hvm *v;
3656 jvalue vv;
3657 jsize len16;
3658 const jchar *sql16, *tail = 0;
3659 int ret;
3660
3661 if (!h) {
3662 throwclosed(env);
3663 return;
3664 }
3665 if (!stmt) {
3666 throwex(env, "null stmt");
3667 return;
3668 }
3669 if (!sql) {
3670 throwex(env, "null sql");
3671 return;
3672 }
3673#ifdef HAVE_BOTH_SQLITE
3674 if (!h->is3) {
3675 throwex(env, "only on SQLite3 database");
3676 return;
3677 }
3678#endif
3679 len16 = (*env)->GetStringLength(env, sql) * sizeof (jchar);
3680 if (len16 < 1) {
3681 return;
3682 }
3683 h->env = env;
3684 sql16 = (*env)->GetStringChars(env, sql, 0);
3685#if HAVE_SQLITE3_PREPARE16_V21
3686 ret = sqlite3_prepare16_v2((sqlite3 *) h->sqlite, sql16, len16,
3687 (sqlite3_stmt **) &svm, (const void **) &tail);
3688#else
3689 ret = sqlite3_prepare16((sqlite3 *) h->sqlite, sql16, len16,
3690 (sqlite3_stmt **) &svm, (const void **) &tail);
3691#endif
3692 if (ret != SQLITE_OK0) {
3693 if (svm) {
3694 sqlite3_finalize((sqlite3_stmt *) svm);
3695 svm = 0;
3696 }
3697 }
3698 if (ret != SQLITE_OK0) {
3699 const char *err = sqlite3_errmsg(h->sqlite);
3700
3701 (*env)->ReleaseStringChars(env, sql, sql16);
3702 setstmterr(env, stmt, ret);
3703 throwex(env, err ? err : "error in prepare");
3704 return;
3705 }
3706 if (!svm) {
3707 (*env)->ReleaseStringChars(env, sql, sql16);
3708 return;
3709 }
3710 len16 = len16 + sizeof (jchar) - ((char *) tail - (char *) sql16);
3711 if (len16 < sizeof (jchar)) {
3712 len16 = sizeof (jchar);
3713 }
3714 v = malloc(sizeof (hvm) + len16);
3715 if (!v) {
3716 (*env)->ReleaseStringChars(env, sql, sql16);
3717 sqlite3_finalize((sqlite3_stmt *) svm);
3718 throwoom(env, "unable to get SQLite handle");
3719 return;
3720 }
3721 v->next = h->vms;
3722 h->vms = v;
3723 v->vm = svm;
3724 v->h = h;
3725 v->tail = (char *) (v + 1);
3726#if HAVE_BOTH_SQLITE
3727 v->is3 = v->hh.is3 = 1;
3728#endif
3729 memcpy(v->tail, tail, len16);
3730 len16 /= sizeof (jchar);
3731 ((jchar *) v->tail)[len16 - 1] = 0;
3732 (*env)->ReleaseStringChars(env, sql, sql16);
3733 v->hh.sqlite = 0;
3734 v->hh.haveutf = h->haveutf;
3735 v->hh.ver = h->ver;
3736 v->hh.bh = v->hh.cb = v->hh.ai = v->hh.tr = v->hh.ph = 0;
3737 v->hh.row1 = 1;
3738 v->hh.enc = h->enc;
3739 v->hh.funcs = 0;
3740 v->hh.vms = 0;
3741 v->hh.env = 0;
3742 vv.j = 0;
3743 vv.l = (jobject) v;
3744 (*env)->SetLongField(env, stmt, F_SQLite_Stmt_handle, vv.j);
3745#else
3746 throwex(env, "unsupported");
3747#endif
3748}
3749
3750JNIEXPORT__attribute__((visibility("default"))) jboolean JNICALL
3751Java_SQLite_Stmt_step(JNIEnv *env, jobject obj)
3752{
3753#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
3754 hvm *v = gethstmt(env, obj);
3755
3756 if (v && v->vm && v->h) {
3757 int ret;
3758
3759 ret = sqlite3_step((sqlite3_stmt *) v->vm);
3760 if (ret == SQLITE_ROW100) {
3761 return JNI_TRUE1;
3762 }
3763 if (ret != SQLITE_DONE101) {
3764 const char *err = sqlite3_errmsg(v->h->sqlite);
3765
3766 setstmterr(env, obj, ret);
3767 throwex(env, err ? err : "error in step");
3768 }
3769 return JNI_FALSE0;
3770 }
3771 throwex(env, "stmt already closed");
3772#else
3773 throwex(env, "unsupported");
3774#endif
3775 return JNI_FALSE0;
3776}
3777
3778JNIEXPORT__attribute__((visibility("default"))) void JNICALL
3779Java_SQLite_Stmt_close(JNIEnv *env, jobject obj)
3780{
3781#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
3782 hvm *v = gethstmt(env, obj);
3783
3784 if (v && v->vm && v->h) {
3785 int ret;
3786
3787 ret = sqlite3_finalize((sqlite3_stmt *) v->vm);
3788 v->vm = 0;
3789 if (ret != SQLITE_OK0) {
3790 const char *err = sqlite3_errmsg(v->h->sqlite);
3791
3792 setstmterr(env, obj, ret);
3793 throwex(env, err ? err : "error in close");
3794 }
3795 return;
3796 }
3797 throwex(env, "stmt already closed");
3798#else
3799 throwex(env, "unsupported");
3800#endif
3801 return;
3802}
3803
3804JNIEXPORT__attribute__((visibility("default"))) void JNICALL
3805Java_SQLite_Stmt_reset(JNIEnv *env, jobject obj)
3806{
3807#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
3808 hvm *v = gethstmt(env, obj);
3809
3810 if (v && v->vm && v->h) {
3811 sqlite3_reset((sqlite3_stmt *) v->vm);
3812 } else {
3813 throwex(env, "stmt already closed");
3814 }
3815#else
3816 throwex(env, "unsupported");
3817#endif
3818}
3819
3820JNIEXPORT__attribute__((visibility("default"))) void JNICALL
3821Java_SQLite_Stmt_clear_1bindings(JNIEnv *env, jobject obj)
3822{
3823#if HAVE_SQLITE31 && HAVE_SQLITE3_CLEAR_BINDINGS1
3824 hvm *v = gethstmt(env, obj);
3825
3826 if (v && v->vm && v->h) {
3827 sqlite3_clear_bindings((sqlite3_stmt *) v->vm);
3828 } else {
3829 throwex(env, "stmt already closed");
3830 }
3831#else
3832 throwex(env, "unsupported");
3833#endif
3834}
3835
3836JNIEXPORT__attribute__((visibility("default"))) void JNICALL
3837Java_SQLite_Stmt_bind__II(JNIEnv *env, jobject obj, jint pos, jint val)
3838{
3839#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
3840 hvm *v = gethstmt(env, obj);
3841
3842 if (v && v->vm && v->h) {
3843 int npar = sqlite3_bind_parameter_count((sqlite3_stmt *) v->vm);
3844 int ret;
3845
3846 if (pos < 1 || pos > npar) {
3847 throwex(env, "parameter position out of bounds");
3848 return;
3849 }
3850 ret = sqlite3_bind_int((sqlite3_stmt *) v->vm, pos, val);
3851 if (ret != SQLITE_OK0) {
3852 setstmterr(env, obj, ret);
3853 throwex(env, "bind failed");
3854 }
3855 } else {
3856 throwex(env, "stmt already closed");
3857 }
3858#else
3859 throwex(env, "unsupported");
3860#endif
3861}
3862
3863JNIEXPORT__attribute__((visibility("default"))) void JNICALL
3864Java_SQLite_Stmt_bind__IJ(JNIEnv *env, jobject obj, jint pos, jlong val)
3865{
3866#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
3867 hvm *v = gethstmt(env, obj);
3868
3869 if (v && v->vm && v->h) {
3870 int npar = sqlite3_bind_parameter_count((sqlite3_stmt *) v->vm);
3871 int ret;
3872
3873 if (pos < 1 || pos > npar) {
3874 throwex(env, "parameter position out of bounds");
3875 return;
3876 }
3877 ret = sqlite3_bind_int64((sqlite3_stmt *) v->vm, pos, val);
3878 if (ret != SQLITE_OK0) {
3879 setstmterr(env, obj, ret);
3880 throwex(env, "bind failed");
3881 }
3882 } else {
3883 throwex(env, "stmt already closed");
3884 }
3885#else
3886 throwex(env, "unsupported");
3887#endif
3888}
3889
3890JNIEXPORT__attribute__((visibility("default"))) void JNICALL
3891Java_SQLite_Stmt_bind__ID(JNIEnv *env, jobject obj, jint pos, jdouble val)
3892{
3893#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
3894 hvm *v = gethstmt(env, obj);
3895
3896 if (v && v->vm && v->h) {
3897 int npar = sqlite3_bind_parameter_count((sqlite3_stmt *) v->vm);
3898 int ret;
3899
3900 if (pos < 1 || pos > npar) {
3901 throwex(env, "parameter position out of bounds");
3902 return;
3903 }
3904 ret = sqlite3_bind_double((sqlite3_stmt *) v->vm, pos, val);
3905 if (ret != SQLITE_OK0) {
3906 setstmterr(env, obj, ret);
3907 throwex(env, "bind failed");
3908 }
3909 } else {
3910 throwex(env, "stmt already closed");
3911 }
3912#else
3913 throwex(env, "unsupported");
3914#endif
3915}
3916
3917JNIEXPORT__attribute__((visibility("default"))) void JNICALL
3918Java_SQLite_Stmt_bind__I_3B(JNIEnv *env, jobject obj, jint pos, jbyteArray val)
3919{
3920#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
3921 hvm *v = gethstmt(env, obj);
3922
3923 if (v && v->vm && v->h) {
3924 int npar = sqlite3_bind_parameter_count((sqlite3_stmt *) v->vm);
3925 int ret;
3926 jint len;
3927 char *data = 0;
3928
3929 if (pos < 1 || pos > npar) {
3930 throwex(env, "parameter position out of bounds");
3931 return;
3932 }
3933 if (val) {
3934 len = (*env)->GetArrayLength(env, val);
3935 if (len > 0) {
3936 data = sqlite3_malloc(len);
3937 if (!data) {
3938 throwoom(env, "unable to get blob parameter");
3939 return;
3940 }
3941 (*env)->GetByteArrayRegion(env, val, 0, len, (jbyte *) data);
3942 ret = sqlite3_bind_blob((sqlite3_stmt *) v->vm,
3943 pos, data, len, sqlite3_free);
3944 } else {
3945 ret = sqlite3_bind_blob((sqlite3_stmt *) v->vm,
3946 pos, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
3947 }
3948 } else {
3949 ret = sqlite3_bind_null((sqlite3_stmt *) v->vm, pos);
3950 }
3951 if (ret != SQLITE_OK0) {
3952 if (data) {
3953 sqlite3_free(data);
3954 }
3955 setstmterr(env, obj, ret);
3956 throwex(env, "bind failed");
3957 }
3958 } else {
3959 throwex(env, "stmt already closed");
3960 }
3961#else
3962 throwex(env, "unsupported");
3963#endif
3964}
3965
3966JNIEXPORT__attribute__((visibility("default"))) void JNICALL
3967Java_SQLite_Stmt_bind__ILjava_lang_String_2(JNIEnv *env, jobject obj,
3968 jint pos, jstring val)
3969{
3970#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
3971 hvm *v = gethstmt(env, obj);
3972
3973 if (v && v->vm && v->h) {
3974 int npar = sqlite3_bind_parameter_count((sqlite3_stmt *) v->vm);
3975 int ret;
3976 jsize len, count;
3977 char *data = 0;
3978
3979 if (pos < 1 || pos > npar) {
3980 throwex(env, "parameter position out of bounds");
3981 return;
3982 }
3983 if (val) {
3984 count = (*env)->GetStringLength(env, val);
3985 len = count * sizeof (jchar);
3986 if (len > 0) {
3987#ifndef JNI_VERSION_1_20x00010002
3988 const jchar *ch;
3989#endif
3990 data = sqlite3_malloc(len);
3991 if (!data) {
3992 throwoom(env, "unable to get blob parameter");
3993 return;
3994 }
3995#ifndef JNI_VERSION_1_20x00010002
3996 ch = (*env)->GetStringChars(env, val, 0);
3997 memcpy(data, ch, len);
3998 (*env)->ReleaseStringChars(env, val, ch);
3999#else
4000 (*env)->GetStringRegion(env, val, 0, count, (jchar *) data);
4001#endif
4002 ret = sqlite3_bind_text16((sqlite3_stmt *) v->vm,
4003 pos, data, len, sqlite3_free);
4004 } else {
4005 ret = sqlite3_bind_text16((sqlite3_stmt *) v->vm, pos, "", 0,
4006 SQLITE_STATIC((sqlite3_destructor_type)0));
4007 }
4008 } else {
4009 ret = sqlite3_bind_null((sqlite3_stmt *) v->vm, pos);
4010 }
4011 if (ret != SQLITE_OK0) {
4012 if (data) {
4013 sqlite3_free(data);
4014 }
4015 setstmterr(env, obj, ret);
4016 throwex(env, "bind failed");
4017 }
4018 } else {
4019 throwex(env, "stmt already closed");
4020 }
4021#else
4022 throwex(env, "unsupported");
4023#endif
4024}
4025
4026JNIEXPORT__attribute__((visibility("default"))) void JNICALL
4027Java_SQLite_Stmt_bind__I(JNIEnv *env, jobject obj, jint pos)
4028{
4029#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
4030 hvm *v = gethstmt(env, obj);
4031
4032 if (v && v->vm && v->h) {
4033 int npar = sqlite3_bind_parameter_count((sqlite3_stmt *) v->vm);
4034 int ret;
4035
4036 if (pos < 1 || pos > npar) {
4037 throwex(env, "parameter position out of bounds");
4038 return;
4039 }
4040 ret = sqlite3_bind_null((sqlite3_stmt *) v->vm, pos);
4041 if (ret != SQLITE_OK0) {
4042 setstmterr(env, obj, ret);
4043 throwex(env, "bind failed");
4044 }
4045 } else {
4046 throwex(env, "stmt already closed");
4047 }
4048#else
4049 throwex(env, "unsupported");
4050#endif
4051}
4052
4053JNIEXPORT__attribute__((visibility("default"))) void JNICALL
4054Java_SQLite_Stmt_bind_1zeroblob(JNIEnv *env, jobject obj, jint pos, jint len)
4055{
4056#if HAVE_SQLITE31 && HAVE_SQLITE3_BIND_ZEROBLOB1
4057 hvm *v = gethstmt(env, obj);
4058
4059 if (v && v->vm && v->h) {
4060 int npar = sqlite3_bind_parameter_count((sqlite3_stmt *) v->vm);
4061 int ret;
4062
4063 if (pos < 1 || pos > npar) {
4064 throwex(env, "parameter position out of bounds");
4065 return;
4066 }
4067 ret = sqlite3_bind_zeroblob((sqlite3_stmt *) v->vm, pos, len);
4068 if (ret != SQLITE_OK0) {
4069 setstmterr(env, obj, ret);
4070 throwex(env, "bind failed");
4071 }
4072 } else {
4073 throwex(env, "stmt already closed");
4074 }
4075#else
4076 throwex(env, "unsupported");
4077#endif
4078}
4079
4080JNIEXPORT__attribute__((visibility("default"))) jint JNICALL
4081Java_SQLite_Stmt_bind_1parameter_1count(JNIEnv *env, jobject obj)
4082{
4083#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
4084 hvm *v = gethstmt(env, obj);
4085
4086 if (v && v->vm && v->h) {
4087 return sqlite3_bind_parameter_count((sqlite3_stmt *) v->vm);
4088 }
4089 throwex(env, "stmt already closed");
4090#else
4091 throwex(env, "unsupported");
4092#endif
4093 return 0;
4094}
4095
4096JNIEXPORT__attribute__((visibility("default"))) jstring JNICALL
4097Java_SQLite_Stmt_bind_1parameter_1name(JNIEnv *env, jobject obj, jint pos)
4098{
4099#if HAVE_SQLITE31 && HAVE_SQLITE3_BIND_PARAMETER_NAME1
4100 hvm *v = gethstmt(env, obj);
4101
4102 if (v && v->vm && v->h) {
4103 int npar = sqlite3_bind_parameter_count((sqlite3_stmt *) v->vm);
4104 const char *name;
4105
4106 if (pos < 1 || pos > npar) {
4107 throwex(env, "parameter position out of bounds");
4108 return 0;
4109 }
4110 name = sqlite3_bind_parameter_name((sqlite3_stmt *) v->vm, pos);
4111 if (name) {
4112 return (*env)->NewStringUTF(env, name);
4113 }
4114 } else {
4115 throwex(env, "stmt already closed");
4116 }
4117#else
4118 throwex(env, "unsupported");
4119#endif
4120 return 0;
4121}
4122
4123JNIEXPORT__attribute__((visibility("default"))) jint JNICALL
4124Java_SQLite_Stmt_bind_1parameter_1index(JNIEnv *env, jobject obj,
4125 jstring name)
4126{
4127#if HAVE_SQLITE31 && HAVE_SQLITE3_BIND_PARAMETER_INDEX1
4128 hvm *v = gethstmt(env, obj);
4129
4130 if (v && v->vm && v->h) {
4131 int pos;
4132 const char *n;
4133 transstr namestr;
4134 jthrowable exc;
4135
4136 n = trans2iso(env, 1, 0, name, &namestr);
4137 exc = (*env)->ExceptionOccurred(env);
4138 if (exc) {
4139 (*env)->DeleteLocalRef(env, exc);
4140 return -1;
4141 }
4142 pos = sqlite3_bind_parameter_index((sqlite3_stmt *) v->vm, n);
4143 transfree(&namestr);
4144 return pos;
4145 } else {
4146 throwex(env, "stmt already closed");
4147 }
4148#else
4149 throwex(env, "unsupported");
4150#endif
4151 return -1;
4152}
4153
4154JNIEXPORT__attribute__((visibility("default"))) jint JNICALL
4155Java_SQLite_Stmt_column_1int(JNIEnv *env, jobject obj, jint col)
4156{
4157#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
4158 hvm *v = gethstmt(env, obj);
4159
4160 if (v && v->vm && v->h) {
4161 int ncol = sqlite3_data_count((sqlite3_stmt *) v->vm);
4162
4163 if (col < 0 || col >= ncol) {
4164 throwex(env, "column out of bounds");
4165 return 0;
4166 }
4167 return sqlite3_column_int((sqlite3_stmt *) v->vm, col);
4168 }
4169 throwex(env, "stmt already closed");
4170#else
4171 throwex(env, "unsupported");
4172#endif
4173 return 0;
4174}
4175
4176JNIEXPORT__attribute__((visibility("default"))) jlong JNICALL
4177Java_SQLite_Stmt_column_1long(JNIEnv *env, jobject obj, jint col)
4178{
4179#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
4180 hvm *v = gethstmt(env, obj);
4181
4182 if (v && v->vm && v->h) {
4183 int ncol = sqlite3_data_count((sqlite3_stmt *) v->vm);
4184
4185 if (col < 0 || col >= ncol) {
4186 throwex(env, "column out of bounds");
4187 return 0;
4188 }
4189 return sqlite3_column_int64((sqlite3_stmt *) v->vm, col);
4190 }
4191 throwex(env, "stmt already closed");
4192#else
4193 throwex(env, "unsupported");
4194#endif
4195 return 0;
4196}
4197
4198JNIEXPORT__attribute__((visibility("default"))) jdouble JNICALL
4199Java_SQLite_Stmt_column_1double(JNIEnv *env, jobject obj, jint col)
4200{
4201#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
4202 hvm *v = gethstmt(env, obj);
4203
4204 if (v && v->vm && v->h) {
4205 int ncol = sqlite3_data_count((sqlite3_stmt *) v->vm);
4206
4207 if (col < 0 || col >= ncol) {
4208 throwex(env, "column out of bounds");
4209 return 0;
4210 }
4211 return sqlite3_column_double((sqlite3_stmt *) v->vm, col);
4212 }
4213 throwex(env, "stmt already closed");
4214#else
4215 throwex(env, "unsupported");
4216#endif
4217 return 0;
4218}
4219
4220JNIEXPORT__attribute__((visibility("default"))) jbyteArray JNICALL
4221Java_SQLite_Stmt_column_1bytes(JNIEnv *env, jobject obj, jint col)
4222{
4223#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
4224 hvm *v = gethstmt(env, obj);
4225
4226 if (v && v->vm && v->h) {
4227 int ncol = sqlite3_data_count((sqlite3_stmt *) v->vm);
4228 int nbytes;
4229 const jbyte *data;
4230 jbyteArray b = 0;
4231
4232 if (col < 0 || col >= ncol) {
4233 throwex(env, "column out of bounds");
4234 return 0;
4235 }
4236 data = sqlite3_column_blob((sqlite3_stmt *) v->vm, col);
4237 if (data) {
4238 nbytes = sqlite3_column_bytes((sqlite3_stmt *) v->vm, col);
4239 } else {
4240 return 0;
4241 }
4242 b = (*env)->NewByteArray(env, nbytes);
4243 if (!b) {
4244 throwoom(env, "unable to get blob column data");
4245 return 0;
4246 }
4247 (*env)->SetByteArrayRegion(env, b, 0, nbytes, data);
4248 return b;
4249 }
4250 throwex(env, "stmt already closed");
4251#else
4252 throwex(env, "unsupported");
4253#endif
4254 return 0;
4255}
4256
4257JNIEXPORT__attribute__((visibility("default"))) jstring JNICALL
4258Java_SQLite_Stmt_column_1string(JNIEnv *env, jobject obj, jint col)
4259{
4260#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
4261 hvm *v = gethstmt(env, obj);
4262
4263 if (v && v->vm && v->h) {
4264 int ncol = sqlite3_data_count((sqlite3_stmt *) v->vm);
4265 int nbytes;
4266 const jchar *data;
4267 jstring b = 0;
4268
4269 if (col < 0 || col >= ncol) {
4270 throwex(env, "column out of bounds");
4271 return 0;
4272 }
4273 data = sqlite3_column_text16((sqlite3_stmt *) v->vm, col);
4274 if (data) {
4275 nbytes = sqlite3_column_bytes16((sqlite3_stmt *) v->vm, col);
4276 } else {
4277 return 0;
4278 }
4279 nbytes /= sizeof (jchar);
4280 b = (*env)->NewString(env, data, nbytes);
4281 if (!b) {
4282 throwoom(env, "unable to get string column data");
4283 return 0;
4284 }
4285 return b;
4286 }
4287 throwex(env, "stmt already closed");
4288#else
4289 throwex(env, "unsupported");
4290#endif
4291 return 0;
4292}
4293
4294JNIEXPORT__attribute__((visibility("default"))) jint JNICALL
4295Java_SQLite_Stmt_column_1type(JNIEnv *env, jobject obj, jint col)
4296{
4297#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
4298 hvm *v = gethstmt(env, obj);
4299
4300 if (v && v->vm && v->h) {
4301 int ncol = sqlite3_data_count((sqlite3_stmt *) v->vm);
4302
4303 if (col < 0 || col >= ncol) {
4304 throwex(env, "column out of bounds");
4305 return 0;
4306 }
4307 return sqlite3_column_type((sqlite3_stmt *) v->vm, col);
4308 }
4309 throwex(env, "stmt already closed");
4310#else
4311 throwex(env, "unsupported");
4312#endif
4313 return 0;
4314}
4315
4316JNIEXPORT__attribute__((visibility("default"))) jint JNICALL
4317Java_SQLite_Stmt_column_1count(JNIEnv *env, jobject obj)
4318{
4319#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
4320 hvm *v = gethstmt(env, obj);
4321
4322 if (v && v->vm && v->h) {
4323 return sqlite3_column_count((sqlite3_stmt *) v->vm);
4324 }
4325 throwex(env, "stmt already closed");
4326#else
4327 throwex(env, "unsupported");
4328#endif
4329 return 0;
4330}
4331
4332JNIEXPORT__attribute__((visibility("default"))) jstring JNICALL
4333Java_SQLite_Stmt_column_1table_1name(JNIEnv *env, jobject obj, jint col)
4334{
4335#if HAVE_SQLITE31 && HAVE_SQLITE3_COLUMN_TABLE_NAME161
4336 hvm *v = gethstmt(env, obj);
4337
4338 if (v && v->vm && v->h) {
4339 int ncol = sqlite3_column_count((sqlite3_stmt *) v->vm);
4340 const jchar *str;
4341
4342 if (col < 0 || col >= ncol) {
4343 throwex(env, "column out of bounds");
4344 return 0;
4345 }
4346 str = sqlite3_column_table_name16((sqlite3_stmt *) v->vm, col);
4347 if (str) {
4348 return (*env)->NewString(env, str, jstrlen(str));
4349 }
4350 return 0;
4351 }
4352 throwex(env, "stmt already closed");
4353#else
4354 throwex(env, "unsupported");
4355#endif
4356 return 0;
4357}
4358
4359JNIEXPORT__attribute__((visibility("default"))) jstring JNICALL
4360Java_SQLite_Stmt_column_1database_1name(JNIEnv *env, jobject obj, jint col)
4361{
4362#if HAVE_SQLITE31 && HAVE_SQLITE3_COLUMN_DATABASE_NAME161
4363 hvm *v = gethstmt(env, obj);
4364
4365 if (v && v->vm && v->h) {
4366 int ncol = sqlite3_column_count((sqlite3_stmt *) v->vm);
4367 const jchar *str;
4368
4369 if (col < 0 || col >= ncol) {
4370 throwex(env, "column out of bounds");
4371 return 0;
4372 }
4373 str = sqlite3_column_database_name16((sqlite3_stmt *) v->vm, col);
4374 if (str) {
4375 return (*env)->NewString(env, str, jstrlen(str));
4376 }
4377 return 0;
4378 }
4379 throwex(env, "stmt already closed");
4380#else
4381 throwex(env, "unsupported");
4382#endif
4383 return 0;
4384}
4385
4386JNIEXPORT__attribute__((visibility("default"))) jstring JNICALL
4387Java_SQLite_Stmt_column_1decltype(JNIEnv *env, jobject obj, jint col)
4388{
4389#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
4390 hvm *v = gethstmt(env, obj);
4391
4392 if (v && v->vm && v->h) {
4393 int ncol = sqlite3_column_count((sqlite3_stmt *) v->vm);
4394 const jchar *str;
4395
4396 if (col < 0 || col >= ncol) {
4397 throwex(env, "column out of bounds");
4398 return 0;
4399 }
4400 str = sqlite3_column_decltype16((sqlite3_stmt *) v->vm, col);
4401 if (str) {
4402 return (*env)->NewString(env, str, jstrlen(str));
4403 }
4404 return 0;
4405 }
4406 throwex(env, "stmt already closed");
4407#else
4408 throwex(env, "unsupported");
4409#endif
4410 return 0;
4411}
4412
4413JNIEXPORT__attribute__((visibility("default"))) jstring JNICALL
4414Java_SQLite_Stmt_column_1name(JNIEnv *env, jobject obj, jint col)
4415{
4416#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
4417 hvm *v = gethstmt(env, obj);
4418
4419 if (v && v->vm && v->h) {
4420 int ncol = sqlite3_column_count((sqlite3_stmt *) v->vm);
4421 const jchar *str;
4422
4423 if (col < 0 || col >= ncol) {
4424 throwex(env, "column out of bounds");
4425 return 0;
4426 }
4427 str = sqlite3_column_name16((sqlite3_stmt *) v->vm, col);
4428 if (str) {
4429 return (*env)->NewString(env, str, jstrlen(str));
4430 }
4431 return 0;
4432 }
4433 throwex(env, "stmt already closed");
4434#else
4435 throwex(env, "unsupported");
4436#endif
4437 return 0;
4438}
4439
4440JNIEXPORT__attribute__((visibility("default"))) jstring JNICALL
4441Java_SQLite_Stmt_column_1origin_1name(JNIEnv *env, jobject obj, jint col)
4442{
4443#if HAVE_SQLITE31 && HAVE_SQLITE3_COLUMN_ORIGIN_NAME161
4444 hvm *v = gethstmt(env, obj);
4445
4446 if (v && v->vm && v->h) {
4447 int ncol = sqlite3_column_count((sqlite3_stmt *) v->vm);
4448 const jchar *str;
4449
4450 if (col < 0 || col >= ncol) {
4451 throwex(env, "column out of bounds");
4452 return 0;
4453 }
4454 str = sqlite3_column_origin_name16((sqlite3_stmt *) v->vm, col);
4455 if (str) {
4456 return (*env)->NewString(env, str, jstrlen(str));
4457 }
4458 return 0;
4459 }
4460 throwex(env, "stmt already closed");
4461#else
4462 throwex(env, "unsupported");
4463#endif
4464 return 0;
4465}
4466
4467JNIEXPORT__attribute__((visibility("default"))) jint JNICALL
4468Java_SQLite_Stmt_status(JNIEnv *env, jobject obj, jint op, jboolean flg)
4469{
4470 jint count = 0;
4471#if HAVE_SQLITE31 && HAVE_SQLITE3_STMT_STATUS1
4472 hvm *v = gethstmt(env, obj);
4473
4474 if (v && v->vm && v->h) {
4475 count = sqlite3_stmt_status((sqlite3_stmt *) v->vm, op,
4476 flg == JNI_TRUE1);
4477 }
4478#endif
4479 return count;
4480}
4481
4482JNIEXPORT__attribute__((visibility("default"))) void JNICALL
4483Java_SQLite_Stmt_finalize(JNIEnv *env, jobject obj)
4484{
4485#if HAVE_SQLITE31 && HAVE_SQLITE_COMPILE1
4486 dostmtfinal(env, obj);
4487#endif
4488}
4489
4490JNIEXPORT__attribute__((visibility("default"))) void JNICALL
4491Java_SQLite_Database__1open_1blob(JNIEnv *env, jobject obj,
4492 jstring dbname, jstring table,
4493 jstring column, jlong row,
4494 jboolean rw, jobject blobj)
4495{
4496#if HAVE_SQLITE31 && HAVE_SQLITE3_INCRBLOBIO1
4497 handle *h = gethandle(env, obj);
4498 hbl *bl;
4499 jthrowable exc;
4500 transstr dbn, tbl, col;
4501 sqlite3_blob *blob;
4502 jvalue vv;
4503 int ret;
4504
4505 if (!blobj) {
4506 throwex(env, "null blob");
4507 return;
4508 }
4509#if HAVE_BOTH_SQLITE
4510 if (!h->is3) {
4511 throwex(env, "not a SQLite 3 database");
4512 return;
4513 }
4514#endif
4515 if (h && h->sqlite) {
4516 trans2iso(env, h->haveutf, h->enc, dbname, &dbn);
4517 exc = (*env)->ExceptionOccurred(env);
4518 if (exc) {
4519 (*env)->DeleteLocalRef(env, exc);
4520 return;
4521 }
4522 trans2iso(env, h->haveutf, h->enc, table, &tbl);
4523 exc = (*env)->ExceptionOccurred(env);
4524 if (exc) {
4525 transfree(&dbn);
4526 (*env)->DeleteLocalRef(env, exc);
4527 return;
4528 }
4529 trans2iso(env, h->haveutf, h->enc, column, &col);
4530 exc = (*env)->ExceptionOccurred(env);
4531 if (exc) {
4532 transfree(&tbl);
4533 transfree(&dbn);
4534 (*env)->DeleteLocalRef(env, exc);
4535 return;
4536 }
4537 ret = sqlite3_blob_open(h->sqlite,
4538 dbn.result, tbl.result, col.result,
4539 row, rw, &blob);
4540 transfree(&col);
4541 transfree(&tbl);
4542 transfree(&dbn);
4543 if (ret != SQLITE_OK0) {
4544 const char *err = sqlite3_errmsg(h->sqlite);
4545
4546 seterr(env, obj, ret);
4547 throwex(env, err ? err : "error in blob open");
4548 return;
4549 }
4550 bl = malloc(sizeof (hbl));
4551 if (!bl) {
4552 sqlite3_blob_close(blob);
4553 throwoom(env, "unable to get SQLite blob handle");
4554 return;
4555 }
4556 bl->next = h->blobs;
4557 h->blobs = bl;
4558 bl->blob = blob;
4559 bl->h = h;
4560 vv.j = 0;
4561 vv.l = (jobject) bl;
4562 (*env)->SetLongField(env, blobj, F_SQLite_Blob_handle, vv.j);
4563 (*env)->SetIntField(env, blobj, F_SQLite_Blob_size,
4564 sqlite3_blob_bytes(blob));
4565 return;
4566 }
4567 throwex(env, "not an open database");
4568#else
4569 throwex(env, "unsupported");
4570#endif
4571}
4572
4573JNIEXPORT__attribute__((visibility("default"))) jint JNICALL
4574Java_SQLite_Blob_write(JNIEnv *env , jobject obj, jbyteArray b, jint off,
4575 jint pos, jint len)
4576{
4577#if HAVE_SQLITE31 && HAVE_SQLITE3_INCRBLOBIO1
4578 hbl *bl = gethbl(env, obj);
4579
4580 if (bl && bl->h && bl->blob) {
4581 jbyte *buf;
4582 jthrowable exc;
4583 int ret;
4584
4585 if (len <= 0) {
4586 return 0;
4587 }
4588 buf = malloc(len);
4589 if (!buf) {
4590 throwoom(env, "out of buffer space for blob");
4591 return 0;
4592 }
4593 (*env)->GetByteArrayRegion(env, b, off, len, buf);
4594 exc = (*env)->ExceptionOccurred(env);
4595 if (exc) {
4596 free(buf);
4597 return 0;
4598 }
4599 ret = sqlite3_blob_write(bl->blob, buf, len, pos);
4600 free(buf);
4601 if (ret != SQLITE_OK0) {
4602 throwioex(env, "blob write error");
4603 return 0;
4604 }
4605 return len;
4606 }
4607 throwex(env, "blob already closed");
4608#else
4609 throwex(env, "unsupported");
4610#endif
4611 return 0;
4612}
4613
4614JNIEXPORT__attribute__((visibility("default"))) jint JNICALL
4615Java_SQLite_Blob_read(JNIEnv *env , jobject obj, jbyteArray b, jint off,
4616 jint pos, jint len)
4617{
4618#if HAVE_SQLITE31 && HAVE_SQLITE3_INCRBLOBIO1
4619 hbl *bl = gethbl(env, obj);
4620
4621 if (bl && bl->h && bl->blob) {
4622 jbyte *buf;
4623 jthrowable exc;
4624 int ret;
4625
4626 if (len <= 0) {
4627 return 0;
4628 }
4629 buf = malloc(len);
4630 if (!buf) {
4631 throwoom(env, "out of buffer space for blob");
4632 return 0;
4633 }
4634 ret = sqlite3_blob_read(bl->blob, buf, len, pos);
4635 if (ret != SQLITE_OK0) {
4636 free(buf);
4637 throwioex(env, "blob read error");
4638 return 0;
4639 }
4640 (*env)->SetByteArrayRegion(env, b, off, len, buf);
4641 free(buf);
4642 exc = (*env)->ExceptionOccurred(env);
4643 if (exc) {
4644 return 0;
4645 }
4646 return len;
4647 }
4648 throwex(env, "blob already closed");
4649#else
4650 throwex(env, "unsupported");
4651#endif
4652 return 0;
4653}
4654
4655JNIEXPORT__attribute__((visibility("default"))) void JNICALL
4656Java_SQLite_Blob_close(JNIEnv *env, jobject obj)
4657{
4658#if HAVE_SQLITE31 && HAVE_SQLITE3_INCRBLOBIO1
4659 doblobfinal(env, obj);
4660#endif
4661}
4662
4663JNIEXPORT__attribute__((visibility("default"))) void JNICALL
4664Java_SQLite_Blob_finalize(JNIEnv *env, jobject obj)
4665{
4666#if HAVE_SQLITE31 && HAVE_SQLITE3_INCRBLOBIO1
4667 doblobfinal(env, obj);
4668#endif
4669}
4670
4671JNIEXPORT__attribute__((visibility("default"))) void
4672JNICALL Java_SQLite_Database__1key(JNIEnv *env, jobject obj, jbyteArray key)
4673{
4674 jsize len = 0;
4675 jbyte *data = 0;
4676#if HAVE_SQLITE3_KEY0
4677 handle *h = gethandle(env, obj);
4678#endif
4679
4680 if (key) {
4681 len = (*env)->GetArrayLength(env, key);
4682 data = (*env)->GetByteArrayElements(env, key, 0);
4683 if (len == 0) {
4684 (*env)->ReleaseByteArrayElements(env, key, data, 0);
4685 data = 0;
4686 }
4687 }
4688 if (!data) {
4689 len = 0;
4690 }
4691#if HAVE_SQLITE3_KEY0
4692 if (h && h->sqlite) {
4693#if HAVE_BOTH_SQLITE
4694 if (!h->is3) {
4695 if (data) {
4696 memset(data, 0, len);
4697 }
4698 throwex(env, "unsupported");
4699 }
4700#endif
4701 sqlite3_key((sqlite3 *) h->sqlite, data, len);
4702 if (data) {
4703 memset(data, 0, len);
4704 (*env)->ReleaseByteArrayElements(env, key, data, 0);
4705 }
4706 } else {
4707 if (data) {
4708 memset(data, 0, len);
4709 (*env)->ReleaseByteArrayElements(env, key, data, 0);
4710 }
4711 throwclosed(env);
4712 }
4713#else
4714 if (data) {
4715 memset(data, 0, len);
4716 (*env)->ReleaseByteArrayElements(env, key, data, 0);
4717 }
4718 /* no error */
4719#endif
4720}
4721
4722JNIEXPORT__attribute__((visibility("default"))) void JNICALL
4723Java_SQLite_Database__1rekey(JNIEnv *env, jobject obj, jbyteArray key)
4724{
4725 jsize len = 0;
4726 jbyte *data = 0;
4727#if HAVE_SQLITE3_KEY0
4728 handle *h = gethandle(env, obj);
4729#endif
4730
4731 if (key) {
4732 len = (*env)->GetArrayLength(env, key);
4733 data = (*env)->GetByteArrayElements(env, key, 0);
4734 if (len == 0) {
4735 (*env)->ReleaseByteArrayElements(env, key, data, 0);
4736 data = 0;
4737 }
4738 }
4739 if (!data) {
4740 len = 0;
4741 }
4742#if HAVE_SQLITE3_KEY0
4743 if (h && h->sqlite) {
4744#if HAVE_BOTH_SQLITE
4745 if (!h->is3) {
4746 if (data) {
4747 memset(data, 0, len);
4748 }
4749 throwex(env, "unsupported");
4750 }
4751#endif
4752 sqlite3_rekey((sqlite3 *) h->sqlite, data, len);
4753 if (data) {
4754 memset(data, 0, len);
4755 (*env)->ReleaseByteArrayElements(env, key, data, 0);
4756 }
4757 } else {
4758 if (data) {
4759 memset(data, 0, len);
4760 (*env)->ReleaseByteArrayElements(env, key, data, 0);
4761 }
4762 throwclosed(env);
4763 }
4764#else
4765 if (data) {
4766 memset(data, 0, len);
4767 (*env)->ReleaseByteArrayElements(env, key, data, 0);
4768 }
4769 throwex(env, "unsupported");
4770#endif
4771}
4772
4773JNIEXPORT__attribute__((visibility("default"))) jboolean JNICALL
4774Java_SQLite_Database__1enable_1shared_1cache(JNIEnv *env, jclass cls,
4775 jboolean onoff)
4776{
4777#if HAVE_SQLITE3_SHARED_CACHE1
4778 return (sqlite3_enable_shared_cache(onoff == JNI_TRUE1) == SQLITE_OK0) ?
4779 JNI_TRUE1 : JNI_FALSE0;
4780#else
4781 return JNI_FALSE0;
4782#endif
4783}
4784
4785
4786JNIEXPORT__attribute__((visibility("default"))) void JNICALL
4787Java_SQLite_Database__1backup(JNIEnv *env, jclass cls, jobject bkupj,
4788 jobject dest, jstring destName,
4789 jobject src, jstring srcName)
4790{
4791#if HAVE_SQLITE31 && HAVE_SQLITE3_BACKUPAPI1
4792 handle *hsrc = gethandle(env, src);
4793 handle *hdest = gethandle(env, dest);
4794 hbk *bk;
4795 jthrowable exc;
4796 transstr dbns, dbnd;
4797 sqlite3_backup *bkup;
4798 jvalue vv;
4799
4800 if (!bkupj) {
4801 throwex(env, "null backup");
4802 return;
4803 }
4804 if (!hsrc) {
4805 throwex(env, "no source database");
4806 return;
4807 }
4808 if (!hdest) {
4809 throwex(env, "no destination database");
4810 return;
4811 }
4812#if HAVE_BOTH_SQLITE
4813 if (!hsrc->is3 || !hdest->is3) {
4814 throwex(env, "not a SQLite 3 database");
4815 return;
4816 }
4817#endif
4818 if (!hsrc->sqlite) {
4819 throwex(env, "source database not open");
4820 return;
4821 }
4822 if (!hdest->sqlite) {
4823 throwex(env, "destination database not open");
4824 return;
4825 }
4826 trans2iso(env, hdest->haveutf, hdest->enc, destName, &dbnd);
4827 exc = (*env)->ExceptionOccurred(env);
4828 if (exc) {
4829 (*env)->DeleteLocalRef(env, exc);
4830 return;
4831 }
4832 trans2iso(env, hsrc->haveutf, hsrc->enc, srcName, &dbns);
4833 exc = (*env)->ExceptionOccurred(env);
4834 if (exc) {
4835 transfree(&dbnd);
4836 (*env)->DeleteLocalRef(env, exc);
4837 return;
4838 }
4839 bkup = sqlite3_backup_init((sqlite3 *) hdest->sqlite, dbnd.result,
4840 (sqlite3 *) hsrc->sqlite, dbns.result);
4841 transfree(&dbnd);
4842 transfree(&dbns);
4843 if (!bkup) {
4844 const char *err = sqlite3_errmsg((sqlite3 *) hdest->sqlite);
4845
4846 seterr(env, src, sqlite3_errcode((sqlite3 *) hdest->sqlite));
4847 throwex(env, err ? err : "error in backup init");
4848 return;
4849 }
4850 bk = malloc(sizeof (hbk));
4851 if (!bk) {
4852 sqlite3_backup_finish(bkup);
4853 throwoom(env, "unable to get SQLite backup handle");
4854 return;
4855 }
4856 bk->next = hsrc->backups;
4857 hsrc->backups = bk;
4858 bk->bkup = bkup;
4859 bk->h = hsrc;
4860 vv.j = 0;
4861 vv.l = (jobject) bk;
4862 (*env)->SetLongField(env, bkupj, F_SQLite_Backup_handle, vv.j);
4863 return;
4864#else
4865 throwex(env, "unsupported");
4866#endif
4867}
4868
4869JNIEXPORT__attribute__((visibility("default"))) void JNICALL
4870Java_SQLite_Backup__1finalize(JNIEnv *env, jobject obj)
4871{
4872#if HAVE_SQLITE31 && HAVE_SQLITE3_BACKUPAPI1
4873 hbk *bk = getclrhbk(env, obj);
4874 int ret = SQLITE_OK0;
4875 char *err = 0;
4876
4877 if (bk) {
4878 if ((*env)->MonitorEnter(env, C_SQLite_Database) != JNI_OK0) {
4879 fprintf(stderrstderr, "SQLite.Backup.finalize: MonitorEnter failed\n");
4880 return;
4881 }
4882 if (bk->h) {
4883 handle *h = bk->h;
4884 hbk *bkc, **bkp;
4885
4886 bkp = &h->backups;
4887 bkc = *bkp;
4888 while (bkc) {
4889 if (bkc == bk) {
4890 *bkp = bkc->next;
4891 break;
4892 }
4893 bkp = &bkc->next;
4894 bkc = *bkp;
4895 }
4896 }
4897 (*env)->MonitorExit(env, C_SQLite_Database);
4898 if (bk->bkup) {
4899 ret = sqlite3_backup_finish(bk->bkup);
4900 if (ret != SQLITE_OK0 && bk->h) {
4901 err = (char *) sqlite3_errmsg((sqlite3 *) bk->h->sqlite);
4902 }
4903 }
4904 bk->bkup = 0;
4905 free(bk);
4906 if (ret != SQLITE_OK0) {
4907 throwex(env, err ? err : "unknown error");
4908 }
4909 }
4910#endif
4911}
4912
4913JNIEXPORT__attribute__((visibility("default"))) jboolean JNICALL
4914Java_SQLite_Backup__1step(JNIEnv *env, jobject obj, jint n)
4915{
4916 jboolean result = JNI_TRUE1;
4917#if HAVE_SQLITE31 && HAVE_SQLITE3_BACKUPAPI1
4918 hbk *bk = gethbk(env, obj);
4919 int ret;
4920
4921 if (bk) {
4922 if (bk->bkup) {
4923 ret = sqlite3_backup_step(bk->bkup, (int) n);
4924 switch (ret) {
4925 case SQLITE_DONE101:
4926 break;
4927 case SQLITE_LOCKED6:
4928 case SQLITE_BUSY5:
4929 case SQLITE_OK0:
4930 result = JNI_FALSE0;
4931 break;
4932 default:
4933 result = JNI_FALSE0;
4934 throwex(env, "backup step failed");
4935 break;
4936 }
4937 }
4938 } else {
4939 throwex(env, "stale backup object");
4940 }
4941#else
4942 throwex(env, "unsupported");
4943#endif
4944 return result;
4945}
4946
4947JNIEXPORT__attribute__((visibility("default"))) jint JNICALL
4948Java_SQLite_Backup__1remaining(JNIEnv *env, jobject obj)
4949{
4950 jint result = 0;
4951#if HAVE_SQLITE31 && HAVE_SQLITE3_BACKUPAPI1
4952 hbk *bk = gethbk(env, obj);
4953
4954 if (bk) {
4955 if (bk->bkup) {
4956 result = sqlite3_backup_remaining(bk->bkup);
4957 }
4958 }
4959#else
4960 throwex(env, "unsupported");
4961#endif
4962 return result;
4963}
4964
4965JNIEXPORT__attribute__((visibility("default"))) jint JNICALL
4966Java_SQLite_Backup__1pagecount(JNIEnv *env, jobject obj)
4967{
4968 jint result = 0;
4969#if HAVE_SQLITE31 && HAVE_SQLITE3_BACKUPAPI1
4970 hbk *bk = gethbk(env, obj);
4971
4972 if (bk) {
4973 if (bk->bkup) {
4974 result = sqlite3_backup_pagecount(bk->bkup);
4975 }
4976 }
4977#else
4978 throwex(env, "unsupported");
4979#endif
4980 return result;
4981}
4982
4983#if HAVE_SQLITE3_PROFILE1
4984static void
4985doprofile(void *arg, const char *msg, sqlite_uint64 est)
4986{
4987 handle *h = (handle *) arg;
4988 JNIEnv *env = h->env;
4989
4990 if (env && h->pr && msg) {
4991 jthrowable exc;
4992 jclass cls = (*env)->GetObjectClass(env, h->pr);
4993 jmethodID mid;
4994
4995 mid = (*env)->GetMethodID(env, cls, "profile",
4996 "(Ljava/lang/String;J)V");
4997 if (mid) {
4998 transstr tr;
4999#if _MSC_VER && (_MSC_VER < 1300)
5000 jlong ms = est / (3600i64 * 24i64 * 1000i64);
5001#else
5002 jlong ms = est / (3600LL * 24LL * 1000LL);
5003#endif
5004
5005 trans2utf(env, h->haveutf, h->enc, msg, &tr);
5006 exc = (*env)->ExceptionOccurred(env);
5007 if (exc) {
5008 (*env)->DeleteLocalRef(env, exc);
5009 (*env)->ExceptionClear(env);
5010 return;
5011 }
5012 (*env)->CallVoidMethod(env, h->pr, mid, tr.jstr, ms);
5013 (*env)->ExceptionClear(env);
5014 (*env)->DeleteLocalRef(env, tr.jstr);
5015 return;
5016 }
5017 }
5018 return;
5019}
5020#endif
5021
5022JNIEXPORT__attribute__((visibility("default"))) void JNICALL
5023Java_SQLite_Database__1profile(JNIEnv *env, jobject obj, jobject tr)
5024{
5025#if HAVE_SQLITE3_PROFILE1
5026 handle *h = gethandle(env, obj);
5027
5028 if (h && h->sqlite) {
5029 delglobrefp(env, &h->pr);
5030 globrefset(env, tr, &h->pr);
5031#if HAVE_BOTH_SQLITE
5032 if (h->is3) {
5033 sqlite3_profile((sqlite3 *) h->sqlite, h->pr ? doprofile : 0, h);
5034 }
5035#else
5036#if HAVE_SQLITE31
5037 sqlite3_profile((sqlite3 *) h->sqlite, h->pr ? doprofile : 0, h);
5038#endif
5039#endif
5040 }
5041#endif
5042}
5043
5044JNIEXPORT__attribute__((visibility("default"))) jint JNICALL
5045Java_SQLite_Database__1status(JNIEnv *env, jclass cls, jint op,
5046 jintArray info, jboolean flag)
5047{
5048 jint ret = SQLITE_ERROR1;
5049#if HAVE_SQLITE3_STATUS1
5050 int data[2] = { 0, 0 };
5051 jint jdata[2];
5052#if HAVE_SQLITE31
5053 ret = sqlite3_status(op, &data[0], &data[1], flag);
5054 if (ret == SQLITE_OK0) {
5055 jdata[0] = data[0];
5056 jdata[1] = data[1];
5057 (*env)->SetIntArrayRegion(env, info, 0, 2, jdata);
5058 }
5059#endif
5060#endif
5061 return ret;
5062}
5063
5064JNIEXPORT__attribute__((visibility("default"))) jint JNICALL
5065Java_SQLite_Database__1db_1status(JNIEnv *env, jobject obj, jint op,
5066 jintArray info, jboolean flag)
5067{
5068 jint ret = SQLITE_ERROR1;
5069#if HAVE_SQLITE3_DB_STATUS1
5070 handle *h = gethandle(env, obj);
5071 int data[2] = { 0, 0 };
5072 jint jdata[2];
5073
5074 if (h && h->sqlite) {
5075#if HAVE_BOTH_SQLITE
5076 if (h->is3) {
5077 ret = sqlite3_db_status((sqlite3 *) h->sqlite, op, &data[0],
5078 &data[1], flag);
5079 }
5080#else
5081#if HAVE_SQLITE31
5082 ret = sqlite3_db_status((sqlite3 *) h->sqlite, op, &data[0],
5083 &data[1], flag);
5084#endif
5085#endif
5086 if (ret == SQLITE_OK0) {
5087 jdata[0] = data[0];
5088 jdata[1] = data[1];
5089 (*env)->SetIntArrayRegion(env, info, 0, 2, jdata);
5090 }
5091 }
5092#endif
5093 return ret;
5094}
5095
5096JNIEXPORT__attribute__((visibility("default"))) void JNICALL
5097Java_SQLite_Stmt_internal_1init(JNIEnv *env, jclass cls)
5098{
5099 F_SQLite_Stmt_handle =
5100 (*env)->GetFieldID(env, cls, "handle", "J");
5101 F_SQLite_Stmt_error_code =
5102 (*env)->GetFieldID(env, cls, "error_code", "I");
5103}
5104
5105JNIEXPORT__attribute__((visibility("default"))) void JNICALL
5106Java_SQLite_Vm_internal_1init(JNIEnv *env, jclass cls)
5107{
5108 F_SQLite_Vm_handle =
5109 (*env)->GetFieldID(env, cls, "handle", "J");
5110 F_SQLite_Vm_error_code =
5111 (*env)->GetFieldID(env, cls, "error_code", "I");
5112}
5113
5114JNIEXPORT__attribute__((visibility("default"))) void JNICALL
5115Java_SQLite_Blob_internal_1init(JNIEnv *env, jclass cls)
5116{
5117 F_SQLite_Blob_handle =
5118 (*env)->GetFieldID(env, cls, "handle", "J");
5119 F_SQLite_Blob_size =
5120 (*env)->GetFieldID(env, cls, "size", "I");
5121}
5122
5123JNIEXPORT__attribute__((visibility("default"))) void JNICALL
5124Java_SQLite_Backup_internal_1init(JNIEnv *env, jclass cls)
5125{
5126 F_SQLite_Backup_handle =
5127 (*env)->GetFieldID(env, cls, "handle", "J");
5128}
5129
5130JNIEXPORT__attribute__((visibility("default"))) void JNICALL
5131Java_SQLite_Database_internal_1init(JNIEnv *env, jclass cls)
5132{
5133#if defined(DONT_USE_JNI_ONLOAD) || !defined(JNI_VERSION_1_20x00010002)
5134 while (C_java_lang_String == 0) {
5135 jclass jls = (*env)->FindClass(env, "java/lang/String");
5136
5137 C_java_lang_String = (*env)->NewGlobalRef(env, jls);
5138 }
5139#endif
5140 F_SQLite_Database_handle =
5141 (*env)->GetFieldID(env, cls, "handle", "J");
5142 F_SQLite_Database_error_code =
5143 (*env)->GetFieldID(env, cls, "error_code", "I");
5144 M_java_lang_String_getBytes =
5145 (*env)->GetMethodID(env, C_java_lang_String, "getBytes", "()[B");
5146 M_java_lang_String_getBytes2 =
5147 (*env)->GetMethodID(env, C_java_lang_String, "getBytes",
5148 "(Ljava/lang/String;)[B");
5149 M_java_lang_String_initBytes =
5150 (*env)->GetMethodID(env, C_java_lang_String, "<init>", "([B)V");
5151 M_java_lang_String_initBytes2 =
5152 (*env)->GetMethodID(env, C_java_lang_String, "<init>",
5153 "([BLjava/lang/String;)V");
5154}
5155
5156#if !defined(DONT_USE_JNI_ONLOAD) && defined(JNI_VERSION_1_20x00010002)
5157JNIEXPORT__attribute__((visibility("default"))) jint JNICALL
5158JNI_OnLoad(JavaVM *vm, void *reserved)
5159{
5160 JNIEnv *env;
5161 jclass cls1, cls2;
5162
5163#ifndef _WIN32
5164#if HAVE_SQLITE20
5165 if (strcmp(sqlite_libencoding(), "UTF-8")__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(sqlite_libencoding()) && __builtin_constant_p ("UTF-8"
) && (__s1_len = strlen (sqlite_libencoding()), __s2_len
= strlen ("UTF-8"), (!((size_t)(const void *)((sqlite_libencoding
()) + 1) - (size_t)(const void *)(sqlite_libencoding()) == 1)
|| __s1_len >= 4) && (!((size_t)(const void *)(("UTF-8"
) + 1) - (size_t)(const void *)("UTF-8") == 1) || __s2_len >=
4)) ? __builtin_strcmp (sqlite_libencoding(), "UTF-8") : (__builtin_constant_p
(sqlite_libencoding()) && ((size_t)(const void *)((sqlite_libencoding
()) + 1) - (size_t)(const void *)(sqlite_libencoding()) == 1)
&& (__s1_len = strlen (sqlite_libencoding()), __s1_len
< 4) ? (__builtin_constant_p ("UTF-8") && ((size_t
)(const void *)(("UTF-8") + 1) - (size_t)(const void *)("UTF-8"
) == 1) ? __builtin_strcmp (sqlite_libencoding(), "UTF-8") : (
__extension__ ({ const unsigned char *__s2 = (const unsigned char
*) (const char *) ("UTF-8"); int __result = (((const unsigned
char *) (const char *) (sqlite_libencoding()))[0] - __s2[0])
; if (__s1_len > 0 && __result == 0) { __result = (
((const unsigned char *) (const char *) (sqlite_libencoding()
))[1] - __s2[1]); if (__s1_len > 1 && __result == 0
) { __result = (((const unsigned char *) (const char *) (sqlite_libencoding
()))[2] - __s2[2]); if (__s1_len > 2 && __result ==
0) __result = (((const unsigned char *) (const char *) (sqlite_libencoding
()))[3] - __s2[3]); } } __result; }))) : (__builtin_constant_p
("UTF-8") && ((size_t)(const void *)(("UTF-8") + 1) -
(size_t)(const void *)("UTF-8") == 1) && (__s2_len =
strlen ("UTF-8"), __s2_len < 4) ? (__builtin_constant_p (
sqlite_libencoding()) && ((size_t)(const void *)((sqlite_libencoding
()) + 1) - (size_t)(const void *)(sqlite_libencoding()) == 1)
? __builtin_strcmp (sqlite_libencoding(), "UTF-8") : (- (__extension__
({ const unsigned char *__s2 = (const unsigned char *) (const
char *) (sqlite_libencoding()); int __result = (((const unsigned
char *) (const char *) ("UTF-8"))[0] - __s2[0]); if (__s2_len
> 0 && __result == 0) { __result = (((const unsigned
char *) (const char *) ("UTF-8"))[1] - __s2[1]); if (__s2_len
> 1 && __result == 0) { __result = (((const unsigned
char *) (const char *) ("UTF-8"))[2] - __s2[2]); if (__s2_len
> 2 && __result == 0) __result = (((const unsigned
char *) (const char *) ("UTF-8"))[3] - __s2[3]); } } __result
; })))) : __builtin_strcmp (sqlite_libencoding(), "UTF-8"))))
; })
!= 0) {
5166 fprintf(stderrstderr, "WARNING: using non-UTF SQLite2 engine\n");
5167 }
5168#endif
5169#endif
5170 if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_20x00010002)) {
5171 return JNI_ERR(-1);
5172 }
5173 cls1 = (*env)->FindClass(env, "java/lang/String");
5174 if (!cls1) {
5175 return JNI_ERR(-1);
5176 }
5177 cls2 = (*env)->FindClass(env, "SQLite/Database");
5178 if (!cls2) {
5179 return JNI_ERR(-1);
5180 }
5181 C_java_lang_String = (*env)->NewGlobalRef(env, cls1);
5182 C_SQLite_Database = (*env)->NewGlobalRef(env, cls2);
5183 return JNI_VERSION_1_20x00010002;
5184}
5185
5186JNIEXPORT__attribute__((visibility("default"))) void JNICALL
5187JNI_OnUnload(JavaVM *vm, void *reserved)
5188{
5189 JNIEnv *env;
5190
5191 if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_20x00010002)) {
5192 return;
5193 }
5194 if (C_SQLite_Database) {
5195 (*env)->DeleteGlobalRef(env, C_SQLite_Database);
5196 C_SQLite_Database = 0;
5197 }
5198 if (C_java_lang_String) {
5199 (*env)->DeleteGlobalRef(env, C_java_lang_String);
5200 C_java_lang_String = 0;
5201 }
5202}
5203#endif