-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteStatement.java
More file actions
1403 lines (1316 loc) · 52.2 KB
/
Copy pathSQLiteStatement.java
File metadata and controls
1403 lines (1316 loc) · 52.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2010 ALM Works Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.almworks.sqlite4java;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import static com.almworks.sqlite4java.SQLiteConstants.*;
/**
* SQLiteStatement wraps an instance of compiled SQL statement, represented as <strong><code>sqlite3_stmt*</code></strong>
* handle in SQLite C Interface.
* <p/>
* You get instances of SQLiteStatement via {@link SQLiteConnection#prepare} methods. After you've done using
* the statement, you have to free it using {@link #dispose} method. Statements are usually cached, so until
* you release the statement, <code>prepare</code> calls for the same SQL will result in needless compilation.
* <p/>
* Typical use includes binding parameters, then executing steps and reading columns. Most methods directly
* correspond to the sqlite3 C interface methods.
* <pre>
* SQLiteStatement statement = connection.prepare(".....");
* try {
* statement.bind(....).bind(....);
* while (statement.step()) {
* statement.columnXXX(...);
* }
* } finally {
* statement.dispose();
* }
* </pre>
* <p/>
* Unless a method is marked as thread-safe, it is confined to the thread that has opened the connection. Calling
* a confined method from a different thread will result in exception.
*
* @author Igor Sereda
* @see <a href="http://sqlite.org/c3ref/stmt.html">sqlite3_stmt*</a>
*/
public final class SQLiteStatement {
/**
* Public instance of initially disposed, dummy statement. To be used as a guardian object.
*/
public static final SQLiteStatement DISPOSED = new SQLiteStatement();
/**
* The SQL of this statement.
*/
private final SQLParts mySqlParts;
/**
* The profiler for this statement, may be null.
*/
private SQLiteProfiler myProfiler;
/**
* The controller that handles connection-level operations. Initially it is set
*/
private SQLiteController myController;
/**
* Statement handle wrapper. Becomes null when disposed.
*/
private SWIGTYPE_p_sqlite3_stmt myHandle;
/**
* When true, the last step() returned SQLITE_ROW, which means data can be read.
*/
private boolean myHasRow;
/**
* When true, values have been bound to the statement. (and they take up memory)
*/
private boolean myHasBindings;
/**
* When true, the statement has performed step() and needs to be reset to be reused.
*/
private boolean myStepped;
/**
* The number of columns in current result set. If negative, the number is unknown and should
* be requested at first need.
*/
private int myColumnCount = -1;
/**
* All currently active bind streams.
*/
private List<BindStream> myBindStreams;
private List<ColumnStream> myColumnStreams;
/**
* Contains progress handler instance - only when step() is in progress. Used to cancel the execution.
* Protected for MT access with this.
*/
private ProgressHandler myProgressHandler;
/**
* True if statement has been cancelled. Cleared at statement reset.
*/
private boolean myCancelled;
/**
* Instances are constructed only by SQLiteConnection.
*
* @param controller controller, provided by the connection
* @param handle native handle wrapper
* @param sqlParts SQL
* @param profiler an instance of profiler for the statement, or null
* @see SQLiteConnection#prepare(String, boolean)
*/
SQLiteStatement(SQLiteController controller, SWIGTYPE_p_sqlite3_stmt handle, SQLParts sqlParts, SQLiteProfiler profiler) {
assert handle != null;
assert sqlParts.isFixed() : sqlParts;
myController = controller;
myHandle = handle;
mySqlParts = sqlParts;
myProfiler = profiler;
Internal.logFine(this, "instantiated");
}
/**
* Constructs DISPOSED singleton
*/
private SQLiteStatement() {
myController = SQLiteController.getDisposed(null);
myHandle = null;
mySqlParts = new SQLParts().fix();
myProfiler = null;
}
/**
* @return true if the statement is disposed and cannot be used
*/
public boolean isDisposed() {
return myHandle == null;
}
/**
* Returns the immutable SQLParts object that was used to create this instance.
* <p/>
* This method is <strong>thread-safe</strong>.
*
* @return SQL used for this statement
*/
public SQLParts getSqlParts() {
return mySqlParts;
}
/**
* Disposes this statement and frees allocated resources. If the statement's handle is cached,
* it is returned to the connection's cache and can be reused by later calls to <code>prepare</code>
* <p/>
* Calling this method on an already disposed instance has no effect.
* <p/>
* After SQLiteStatement instance is disposed, it is no longer usable and holds no references to its originating
* connection or SQLite database.
*/
public void dispose() {
if (myHandle == null)
return;
try {
myController.validate();
} catch (SQLiteException e) {
Internal.recoverableError(this, "invalid dispose: " + e, true);
return;
}
Internal.logFine(this, "disposing");
myController.dispose(this);
// clear may be called from dispose() too
clear();
}
/**
* Resets the statement if it has been stepped, allowing SQL to be run again. Optionally, clears bindings all binding.
* <p/>
* If <code>clearBinding</code> parameter is false, then all preceding bindings remain in place. You can change
* some or none of them and run statement again.
*
* @param clearBindings if true, all parameters will be set to NULL
* @return this statement
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/reset.html">sqlite3_reset</a>
* @see <a href="http://www.sqlite.org/c3ref/clear_bindings.html">sqlite3_clear_bindings</a>
*/
public SQLiteStatement reset(boolean clearBindings) throws SQLiteException {
myController.validate();
boolean fineLogging = Internal.isFineLogging();
if (fineLogging)
Internal.logFine(this, "reset(" + clearBindings + ")");
SWIGTYPE_p_sqlite3_stmt handle = handle();
clearColumnStreams();
if (myStepped) {
if (fineLogging)
Internal.logFine(this, "resetting");
int rc = _SQLiteSwigged.sqlite3_reset(handle);
myController.throwResult(rc, "reset()", this);
}
myHasRow = false;
myStepped = false;
myColumnCount = -1;
if (clearBindings && myHasBindings) {
if (fineLogging)
Internal.logFine(this, "clearing bindings");
int rc = _SQLiteSwigged.sqlite3_clear_bindings(handle);
myController.throwResult(rc, "reset.clearBindings()", this);
clearBindStreams(false);
myHasBindings = false;
}
synchronized (this) {
myCancelled = false;
}
return this;
}
/**
* Convenience method that resets the statement and clears bindings. See {@link #reset(boolean)} for a detailed
* description.
*
* @return this statement
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
*/
public SQLiteStatement reset() throws SQLiteException {
return reset(true);
}
/**
* Clears parameter bindings, if there are any. All parameters are set to NULL.
*
* @return this statement
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/clear_bindings.html">sqlite3_clear_bindings</a>
*/
public SQLiteStatement clearBindings() throws SQLiteException {
myController.validate();
Internal.logFine(this, "clearBindings");
if (myHasBindings) {
Internal.logFine(this, "clearing bindings");
int rc = _SQLiteSwigged.sqlite3_clear_bindings(handle());
myController.throwResult(rc, "clearBindings()", this);
clearBindStreams(false);
}
myHasBindings = false;
return this;
}
/**
* Evaluates SQL statement until either there's data to be read, an error occurs, or the statement completes.
* <p/>
* An SQL statement is represented as a VM program in SQLite, and a call to <code>step</code> runs that program
* until there's a "break point".
* <p/>
* Note that since SQlite 3.7, {@link #reset} method is called by step() automatically if anything other than
* SQLITE_ROW is returned.
* <p/>
* This method can produce one of the three results:
* <ul>
* <li>If the return value is <strong>true</strong>, there's data to be read using <code>columnXYZ</code> methods;
* <li>If the return value is <strong>false</strong>, the SQL statement is completed and no longer executable until
* {@link #reset(boolean)} is called;
* <li>Exception is thrown if any error occurs.
* </ul>
*
* @return true if there is data (SQLITE_ROW) was returned, false if statement has been completed (SQLITE_DONE)
* @throws SQLiteException if result code from sqlite3_step was neither SQLITE_ROW nor SQLITE_DONE, or if any other problem occurs
* @see <a href="http://www.sqlite.org/c3ref/step.html">sqlite3_step</a>
*/
public boolean step() throws SQLiteException {
myController.validate();
if (Internal.isFineLogging())
Internal.logFine(this, "step");
SWIGTYPE_p_sqlite3_stmt handle = handle();
int rc;
ProgressHandler ph = prepareStep();
try {
SQLiteProfiler profiler = myProfiler;
long from = profiler == null ? 0 : System.nanoTime();
rc = _SQLiteSwigged.sqlite3_step(handle);
if (profiler != null)
profiler.reportStep(myStepped, mySqlParts.toString(), from, System.nanoTime(), rc);
} finally {
finalizeStep(ph, "step");
}
stepResult(rc, "step");
return myHasRow;
}
/**
* Convenience method that ignores the available data and steps through the SQL statement until evaluation is
* completed. See {@link #step} for details.
* <p/>
* Most often it's used to chain calls.
*
* @return this statement
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
*/
public SQLiteStatement stepThrough() throws SQLiteException {
while (step()) { /* do nothing */ }
return this;
}
/**
* Cancels the currently running statement. This method has effect only during execution of the step() method,
* and so it is run from a different thread.
* <p/>
* This method works by setting a cancel flag, which is checked by the progress callback. Hence, if the progress
* callback is disabled, this method will not have effect. Likewise, if <code>stepsPerCallback</code> parameter
* is set to large values, the reaction to this call may be far from immediate.
* <p/>
* If execution is cancelled, the step() method will throw {@link SQLiteInterruptedException}, and transaction
* will be rolled back.
* <p/>
* This method is <strong>thread-safe</strong>.
* <p/>
*
* @see SQLiteConnection#setStepsPerCallback
* @see <a href="http://www.sqlite.org/c3ref/progress_handler.html">sqlite3_progress_callback</a>
*/
public void cancel() {
ProgressHandler handler;
synchronized (this) {
myCancelled = true;
handler = myProgressHandler;
}
if (handler != null) {
handler.cancel();
}
}
/**
* Checks whether there's data to be read with <code>columnXYZ</code> methods.
*
* @return true if last call to {@link #step} has returned true
*/
public boolean hasRow() {
return myHasRow;
}
/**
* Checks if some parameters were bound
*
* @return true if at least one of the statement parameters has been bound to a value
*/
public boolean hasBindings() {
return myHasBindings;
}
/**
* Checks if the statement has been evaluated
*
* @return true if the statement has been stepped at least once, and not reset
*/
public boolean hasStepped() {
return myStepped;
}
/**
* Loads int values returned from a query into a buffer.
* <p/>
* The purpose of this method is to run a query and load a single-column result in bulk. This could save a lot of time
* by making a single JNI call instead of 2*N calls to <code>step()</code> and <code>columnInt()</code>.
* <p/>
* If result set contains NULL value, it's replaced with 0.
* <p/>
* This method may be called iteratively with a fixed-size buffer. For example:
* <pre>
* SQLiteStatement st = connection.prepare("SELECT id FROM articles WHERE text LIKE '%whatever%'");
* try {
* int[] buffer = new int[1000];
* while (!st.hasStepped() || st.hasRow()) {
* int loaded = st.loadInts(0, buffer, 0, buffer.length);
* processResult(buffer, 0, loaded);
* }
* } finally {
* st.dispose();
* }
* </pre>
* <p/>
* After method finishes, the number of rows loaded is returned and statement's {@link #hasRow} method indicates
* whether more rows are available.
*
* @param column column index, as used in {@link #columnInt}
* @param buffer buffer for accepting loaded integers
* @param offset offset in the buffer to start writing
* @param length maximum number of integers to load from the database
* @return actual number of integers loaded
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
*/
public int loadInts(int column, int[] buffer, int offset, int length) throws SQLiteException {
myController.validate();
if (buffer == null || length <= 0 || offset < 0 || offset + length > buffer.length) {
assert false;
return 0;
}
if (Internal.isFineLogging())
Internal.logFine(this, "loadInts(" + column + "," + offset + "," + length + ")");
if (myStepped && !myHasRow)
return 0;
SWIGTYPE_p_sqlite3_stmt handle = handle();
int r;
int rc;
ProgressHandler ph = prepareStep();
try {
_SQLiteManual manual = myController.getSQLiteManual();
SQLiteProfiler profiler = myProfiler;
long from = profiler == null ? 0 : System.nanoTime();
r = manual.wrapper_load_ints(handle, column, buffer, offset, length);
rc = manual.getLastReturnCode();
if (profiler != null) profiler.reportLoadInts(myStepped, mySqlParts.toString(), from, System.nanoTime(), rc, r);
} finally {
finalizeStep(ph, "loadInts");
}
stepResult(rc, "loadInts");
return r;
}
/**
* Loads long values returned from a query into a buffer.
* <p/>
* The purpose of this method is to run a query and load a single-column result in bulk. This could save a lot of time
* by making a single JNI call instead of 2*N calls to <code>step()</code> and <code>columnLong()</code>.
* <p/>
* If result set contains NULL value, it's replaced with 0.
* <p/>
* This method may be called iteratively with a fixed-size buffer. For example:
* <pre>
* SQLiteStatement st = connection.prepare("SELECT id FROM articles WHERE text LIKE '%whatever%'");
* try {
* long[] buffer = new long[1000];
* while (!st.hasStepped() || st.hasRow()) {
* int loaded = st.loadInts(0, buffer, 0, buffer.length);
* processResult(buffer, 0, loaded);
* }
* } finally {
* st.dispose();
* }
* </pre>
* <p/>
* After method finishes, the number of rows loaded is returned and statement's {@link #hasRow} method indicates
* whether more rows are available.
*
* @param column column index, as used in {@link #columnInt}
* @param buffer buffer for accepting loaded longs
* @param offset offset in the buffer to start writing
* @param length maximum number of integers to load from the database
* @return actual number of integers loaded
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
*/
public int loadLongs(int column, long[] buffer, int offset, int length) throws SQLiteException {
myController.validate();
if (buffer == null || length <= 0 || offset < 0 || offset + length > buffer.length) {
assert false;
return 0;
}
if (Internal.isFineLogging())
Internal.logFine(this, "loadLongs(" + column + "," + offset + "," + length + ")");
if (myStepped && !myHasRow)
return 0;
SWIGTYPE_p_sqlite3_stmt handle = handle();
int r;
int rc;
ProgressHandler ph = prepareStep();
try {
_SQLiteManual manual = myController.getSQLiteManual();
SQLiteProfiler profiler = myProfiler;
long from = profiler == null ? 0 : System.nanoTime();
r = manual.wrapper_load_longs(handle, column, buffer, offset, length);
rc = manual.getLastReturnCode();
if (profiler != null) profiler.reportLoadLongs(myStepped, mySqlParts.toString(), from, System.nanoTime(), rc, r);
} finally {
finalizeStep(ph, "loadLongs");
}
stepResult(rc, "loadLongs");
return r;
}
/**
* Returns the number of parameters that can be bound.
*
* @return the number of SQL parameters
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/bind_parameter_count.html">sqlite3_bind_parameter_count</a>
*/
public int getBindParameterCount() throws SQLiteException {
myController.validate();
return _SQLiteSwigged.sqlite3_bind_parameter_count(handle());
}
/**
* Returns the name of a given bind parameter, as defined in the SQL.
*
* @param index the index of a bindable parameter, starting with 1
* @return the name of the parameter, e.g. "?PARAM1", or null if parameter is anonymous (just "?")
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/bind_parameter_name.html">sqlite3_bind_parameter_name</a>
*/
public String getBindParameterName(int index) throws SQLiteException {
myController.validate();
return _SQLiteSwigged.sqlite3_bind_parameter_name(handle(), index);
}
/**
* Returns the index of a bind parameter with a given name, as defined in the SQL.
*
* @param name the name of a named bindable parameter, e.g. "?PARAM1"
* @return the index of the parameter in the SQL, or 0 if no such parameter found
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/bind_parameter_index.html">sqlite3_bind_parameter_index</a>
*/
public int getBindParameterIndex(String name) throws SQLiteException {
myController.validate();
return _SQLiteSwigged.sqlite3_bind_parameter_index(handle(), name);
}
/**
* Binds SQL parameter to a value of type double.
*
* @param index the index of the boundable parameter, starting with 1
* @param value non-null double value
* @return this object
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/bind_blob.html">sqlite3_bind_double</a>
*/
public SQLiteStatement bind(int index, double value) throws SQLiteException {
myController.validate();
if (Internal.isFineLogging())
Internal.logFine(this, "bind(" + index + "," + value + ")");
int rc = _SQLiteSwigged.sqlite3_bind_double(handle(), index, value);
myController.throwResult(rc, "bind(double)", this);
myHasBindings = true;
return this;
}
/**
* Binds SQL parameter to a value of type int.
*
* @param index the index of the boundable parameter, starting with 1
* @param value non-null int value
* @return this object
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/bind_blob.html">sqlite3_bind_int</a>
*/
public SQLiteStatement bind(int index, int value) throws SQLiteException {
myController.validate();
if (Internal.isFineLogging())
Internal.logFine(this, "bind(" + index + "," + value + ")");
int rc = _SQLiteSwigged.sqlite3_bind_int(handle(), index, value);
myController.throwResult(rc, "bind(int)", this);
myHasBindings = true;
return this;
}
/**
* Binds SQL parameter to a value of type long.
*
* @param index the index of the boundable parameter, starting with 1
* @param value non-null long value
* @return this object
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/bind_blob.html">sqlite3_bind_int64</a>
*/
public SQLiteStatement bind(int index, long value) throws SQLiteException {
myController.validate();
if (Internal.isFineLogging())
Internal.logFine(this, "bind(" + index + "," + value + ")");
int rc = _SQLiteSwigged.sqlite3_bind_int64(handle(), index, value);
myController.throwResult(rc, "bind(long)", this);
myHasBindings = true;
return this;
}
/**
* Binds SQL parameter to a value of type String.
*
* @param index the index of the boundable parameter, starting with 1
* @param value String value, if null then {@link #bindNull} will be called
* @return this object
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/bind_blob.html">sqlite3_bind_text</a>
*/
public SQLiteStatement bind(int index, String value) throws SQLiteException {
if (value == null) {
Internal.logFine(this, "bind(null string)");
return bindNull(index);
}
myController.validate();
if (Internal.isFineLogging()) {
if (value.length() <= 20)
Internal.logFine(this, "bind(" + index + "," + value + ")");
else
Internal.logFine(this, "bind(" + index + "," + value.substring(0, 20) + "....)");
}
int rc = _SQLiteManual.sqlite3_bind_text(handle(), index, value);
myController.throwResult(rc, "bind(String)", this);
myHasBindings = true;
return this;
}
/**
* Binds SQL parameter to a BLOB value, represented by a byte array.
*
* @param index the index of the boundable parameter, starting with 1
* @param value an array of bytes to be used as the blob value; if null, {@link #bindNull} is called
* @return this object
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/bind_blob.html">sqlite3_bind_blob</a>
*/
public SQLiteStatement bind(int index, byte[] value) throws SQLiteException {
return value == null ? bindNull(index) : bind(index, value, 0, value.length);
}
/**
* Binds SQL parameter to a BLOB value, represented by a range within byte array.
*
* @param index the index of the boundable parameter, starting with 1
* @param value an array of bytes; if null, {@link #bindNull} is called
* @param offset position in the byte array to start reading value from
* @param length number of bytes to read from value
* @return this object
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/bind_blob.html">sqlite3_bind_blob</a>
*/
public SQLiteStatement bind(int index, byte[] value, int offset, int length) throws SQLiteException {
if (value == null) {
Internal.logFine(this, "bind(null blob)");
return bindNull(index);
}
if (offset < 0 || offset + length > value.length)
throw new ArrayIndexOutOfBoundsException(value.length + " " + offset + " " + length);
myController.validate();
if (Internal.isFineLogging()) {
Internal.logFine(this, "bind(" + index + ",[" + length + "])");
}
int rc = _SQLiteManual.sqlite3_bind_blob(handle(), index, value, offset, length);
myController.throwResult(rc, "bind(blob)", this);
myHasBindings = true;
return this;
}
/**
* Binds SQL parameter to a BLOB value, consiting of a given number of zero bytes.
*
* @param index the index of the boundable parameter, starting with 1
* @param length number of zero bytes to use as a parameter
* @return this object
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/bind_blob.html">sqlite3_bind_zeroblob</a>
*/
public SQLiteStatement bindZeroBlob(int index, int length) throws SQLiteException {
if (length < 0) {
Internal.logFine(this, "bind(null zeroblob)");
return bindNull(index);
}
myController.validate();
if (Internal.isFineLogging()) {
Internal.logFine(this, "bindZeroBlob(" + index + "," + length + ")");
}
int rc = _SQLiteSwigged.sqlite3_bind_zeroblob(handle(), index, length);
myController.throwResult(rc, "bindZeroBlob()", this);
myHasBindings = true;
return this;
}
/**
* Binds SQL parameter to a NULL value.
*
* @param index the index of the boundable parameter, starting with 1
* @return this object
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/bind_blob.html">sqlite3_bind_null</a>
*/
public SQLiteStatement bindNull(int index) throws SQLiteException {
myController.validate();
if (Internal.isFineLogging())
Internal.logFine(this, "bind_null(" + index + ")");
int rc = _SQLiteSwigged.sqlite3_bind_null(handle(), index);
myController.throwResult(rc, "bind(null)", this);
// specifically does not set myHasBindings to true
return this;
}
/**
* Binds SQL parameter to a BLOB value, represented by a stream. The stream can further be used to write into,
* before the first call to {@link #step}.
* <p/>
* After the application is done writing to the parameter stream, it should be closed.
* <p/>
* If statement is executed before the stream is closed, the value will not be set for the parameter.
*
* @param index the index of the boundable parameter, starting with 1
* @return stream to receive data for the BLOB parameter
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/bind_blob.html">sqlite3_bind_blob</a>
*/
public OutputStream bindStream(int index) throws SQLiteException {
return bindStream(index, 0);
}
/**
* Binds SQL parameter to a BLOB value, represented by a stream. The stream can further be used to write into,
* before the first call to {@link #step}.
* <p/>
* After the application is done writing to the parameter stream, it should be closed.
* <p/>
* If statement is executed before the stream is closed, the value will not be set for the parameter.
*
* @param index the index of the boundable parameter, starting with 1
* @param bufferSize the number of bytes to be allocated for the buffer (the buffer will grow as needed)
* @return stream to receive data for the BLOB parameter
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/bind_blob.html">sqlite3_bind_blob</a>
*/
public OutputStream bindStream(int index, int bufferSize) throws SQLiteException {
myController.validate();
if (Internal.isFineLogging())
Internal.logFine(this, "bindStream(" + index + "," + bufferSize + ")");
try {
DirectBuffer buffer = myController.allocateBuffer(bufferSize);
BindStream out = new BindStream(index, buffer);
List<BindStream> list = myBindStreams;
if (list == null) {
myBindStreams = list = new ArrayList<BindStream>(1);
}
list.add(out);
myHasBindings = true;
return out;
} catch (IOException e) {
throw new SQLiteException(WRAPPER_WEIRD, "cannot allocate buffer", e);
}
}
/**
* Gets a column value after step has returned a row of the result set.
* <p/>
* Call this method to retrieve data of type String after {@link #step()} has returned true.
*
* @param column the index of the column, starting with 0
* @return a String value or null if database value is NULL
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/column_blob.html">sqlite3_column_text16</a>
*/
public String columnString(int column) throws SQLiteException {
myController.validate();
SWIGTYPE_p_sqlite3_stmt handle = handle();
checkColumn(column, handle, true);
if (Internal.isFineLogging())
Internal.logFine(this, "columnString(" + column + ")");
_SQLiteManual sqlite = myController.getSQLiteManual();
String result = sqlite.sqlite3_column_text(handle, column);
myController.throwResult(sqlite.getLastReturnCode(), "columnString()", this);
if (Internal.isFineLogging()) {
if (result == null) {
Internal.logFine(this, "columnString(" + column + ") is null");
} else if (result.length() <= 20) {
Internal.logFine(this, "columnString(" + column + ")=" + result);
} else {
Internal.logFine(this, "columnString(" + column + ")=" + result.substring(0, 20) + "....");
}
}
return result;
}
/**
* Gets a column value after step has returned a row of the result set.
* <p/>
* Call this method to retrieve data of type int after {@link #step()} has returned true.
*
* @param column the index of the column, starting with 0
* @return an int value, or value converted to int, or 0 if value is NULL
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/column_blob.html">sqlite3_column_int</a>
*/
public int columnInt(int column) throws SQLiteException {
myController.validate();
SWIGTYPE_p_sqlite3_stmt handle = handle();
checkColumn(column, handle, true);
if (Internal.isFineLogging())
Internal.logFine(this, "columnInt(" + column + ")");
int r = _SQLiteSwigged.sqlite3_column_int(handle, column);
if (Internal.isFineLogging())
Internal.logFine(this, "columnInt(" + column + ")=" + r);
return r;
}
/**
* Gets a column value after step has returned a row of the result set.
* <p/>
* Call this method to retrieve data of type double after {@link #step()} has returned true.
*
* @param column the index of the column, starting with 0
* @return a double value, or value converted to double, or 0.0 if value is NULL
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/column_blob.html">sqlite3_column_double</a>
*/
public double columnDouble(int column) throws SQLiteException {
myController.validate();
SWIGTYPE_p_sqlite3_stmt handle = handle();
checkColumn(column, handle, true);
if (Internal.isFineLogging())
Internal.logFine(this, "columnDouble(" + column + ")");
double r = _SQLiteSwigged.sqlite3_column_double(handle, column);
if (Internal.isFineLogging())
Internal.logFine(this, "columnDouble(" + column + ")=" + r);
return r;
}
/**
* Gets a column value after step has returned a row of the result set.
* <p/>
* Call this method to retrieve data of type long after {@link #step()} has returned true.
*
* @param column the index of the column, starting with 0
* @return a long value, or value converted to long, or 0L if the value is NULL
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/column_blob.html">sqlite3_column_int64</a>
*/
public long columnLong(int column) throws SQLiteException {
myController.validate();
SWIGTYPE_p_sqlite3_stmt handle = handle();
checkColumn(column, handle, true);
if (Internal.isFineLogging())
Internal.logFine(this, "columnLong(" + column + ")");
long r = _SQLiteSwigged.sqlite3_column_int64(handle, column);
if (Internal.isFineLogging())
Internal.logFine(this, "columnLong(" + column + ")=" + r);
return r;
}
/**
* Gets a column value after step has returned a row of the result set.
* <p/>
* Call this method to retrieve data of type BLOB after {@link #step()} has returned true.
*
* @param column the index of the column, starting with 0
* @return a byte array with the value, or null if the value is NULL
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/column_blob.html">sqlite3_column_blob</a>
*/
public byte[] columnBlob(int column) throws SQLiteException {
myController.validate();
SWIGTYPE_p_sqlite3_stmt handle = handle();
checkColumn(column, handle, true);
if (Internal.isFineLogging())
Internal.logFine(this, "columnBytes(" + column + ")");
_SQLiteManual sqlite = myController.getSQLiteManual();
byte[] r = sqlite.sqlite3_column_blob(handle, column);
myController.throwResult(sqlite.getLastReturnCode(), "columnBytes", this);
if (Internal.isFineLogging())
Internal.logFine(this, "columnBytes(" + column + ")=[" + (r == null ? "null" : r.length) + "]");
return r;
}
/**
* Gets an InputStream for reading a BLOB column value after step has returned a row of the result set.
* <p/>
* Call this method to retrieve data of type BLOB after {@link #step()} has returned true.
* <p/>
* The stream should be read and closed before next call to step or reset. Otherwise, the stream is automatically
* closed and disposed, and the following attempts to read from it result in IOException.
*
* @param column the index of the column, starting with 0
* @return a stream to read value from, or null if the value is NULL
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/column_blob.html">sqlite3_column_blob</a>
*/
public InputStream columnStream(int column) throws SQLiteException {
myController.validate();
SWIGTYPE_p_sqlite3_stmt handle = handle();
checkColumn(column, handle, true);
if (Internal.isFineLogging())
Internal.logFine(this, "columnStream(" + column + ")");
_SQLiteManual sqlite = myController.getSQLiteManual();
ByteBuffer buffer = sqlite.wrapper_column_buffer(handle, column);
myController.throwResult(sqlite.getLastReturnCode(), "columnStream", this);
if (buffer == null)
return null;
ColumnStream in = new ColumnStream(buffer);
List<ColumnStream> table = myColumnStreams;
if (table == null)
myColumnStreams = table = new ArrayList<ColumnStream>(1);
table.add(in);
return in;
}
/**
* Checks if the value returned in the given column is null.
*
* @param column the index of the column, starting with 0
* @return true if the result for the column was NULL
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/column_blob.html">sqlite3_column_type</a>
*/
public boolean columnNull(int column) throws SQLiteException {
myController.validate();
int valueType = getColumnType(column, handle());
return valueType == SQLITE_NULL;
}
/**
* Gets the number of columns in the result set.
* <p/>
* This method may be called before statement is executed, during execution or after statement has executed (
* {@link #step} returned false).
* <p/>
* However, for some statements where the number of columns may vary - such as
* "SELECT * FROM ..." - the correct result is guaranteed only if method is called during statement execution,
* when <code>step()</code> has returned true. (That is so because sqlite3_column_count function does not
* force statement recompilation if database schema has changed, but sqlite3_step does.)
*
* @return the number of columns
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/column_count.html">sqlite3_column_count</a>
*/
public int columnCount() throws SQLiteException {
myController.validate();
return getColumnCount(handle());
}
/**
* Gets a column value after step has returned a row of the result set.
* <p/>
* Call this method to retrieve data of any type after {@link #step()} has returned true.
* <p/>
* The class of the object returned depends on the value and the type of value reported by SQLite. It can be:
* <ul>
* <li><code>null</code> if the value is NULL
* <li><code>String</code> if the value has type SQLITE_TEXT
* <li><code>Integer</code> or <code>Long</code> if the value has type SQLITE_INTEGER (depending on the value)
* <li><code>Double</code> if the value has type SQLITE_FLOAT
* <li><code>byte[]</code> if the value has type SQLITE_BLOB
* </ul>
*
* @param column the index of the column, starting with 0
* @return an object containing the value
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/column_blob.html">sqlite3_column_blob</a>
*/
public Object columnValue(int column) throws SQLiteException {
myController.validate();
int valueType = getColumnType(column, handle());
switch (valueType) {
case SQLITE_NULL:
return null;
case SQLITE_FLOAT:
return columnDouble(column);
case SQLITE_INTEGER:
long value = columnLong(column);
if (value == (int)value) {
return (int)value;
} else {
return value;
}
case SQLITE_TEXT:
return columnString(column);
case SQLITE_BLOB:
return columnBlob(column);
default:
Internal.recoverableError(this, "value type " + valueType + " not yet supported", true);
return null;
}
}
/**
* Gets a type of a column after step() has returned a row.
* <p/>
* Call this method to retrieve data of any type after {@link #step()} has returned true.
* <p/>
* Note that SQLite has dynamic typing, so this method returns the affinity of the specified column.
* See <a href="http://sqlite.org/datatype3.html">dynamic typing</a> for details.
* <p/>
* This method returns an integer constant, defined in {@link SQLiteConstants}: <code>SQLITE_NULL</code>,
* <code>SQLITE_INTEGER</code>, <code>SQLITE_TEXT</code>, <code>SQLITE_BLOB</code> or <code>SQLITE_FLOAT</code>.
* <p/>
* The value returned by this method is only meaningful if
* no type conversions have occurred as the result of calling columnNNN() methods.
*
* @param column the index of the column, starting with 0
* @return an integer code, indicating the type affinity of the returned column
* @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class
* @see <a href="http://www.sqlite.org/c3ref/column_blob.html">sqlite3_column_type</a>
*/
public int columnType(int column) throws SQLiteException {