20
20
21
21
package android .database .sqlite ;
22
22
23
- import android .database .sqlite .CloseGuard ;
24
-
25
23
import android .database .Cursor ;
26
24
import android .database .CursorWindow ;
27
25
import android .database .DatabaseUtils ;
33
31
import android .util .Log ;
34
32
import android .util .LruCache ;
35
33
import android .util .Printer ;
34
+ import org .sqlite .core .NativeDB ;
35
+ import org .sqlite .core .NativeKt ;
36
36
37
37
import java .text .SimpleDateFormat ;
38
38
import java .util .ArrayList ;
39
39
import java .util .Date ;
40
40
import java .util .Map ;
41
- import java .util .regex .Pattern ;
42
41
43
42
/**
44
43
* Represents a SQLite database connection.
@@ -109,7 +108,7 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen
109
108
private final OperationLog mRecentOperations = new OperationLog ();
110
109
111
110
// The native SQLiteConnection pointer. (FOR INTERNAL USE ONLY)
112
- private long mConnectionPtr ;
111
+ private NativeDB mConnectionPtr ;
113
112
114
113
private boolean mOnlyAllowReadOnlyOperations ;
115
114
@@ -118,49 +117,8 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen
118
117
// times we have attempted to attach a cancellation signal to the connection so that
119
118
// we can ensure that we detach the signal at the right time.
120
119
private int mCancellationSignalAttachCount ;
121
-
122
- private static native long nativeOpen (String path , int openFlags , String label ,
123
- boolean enableTrace , boolean enableProfile );
124
- private static native void nativeClose (long connectionPtr );
125
- private static native void nativeRegisterCustomFunction (long connectionPtr ,
126
- SQLiteCustomFunction function );
127
- private static native void nativeRegisterLocalizedCollators (long connectionPtr , String locale );
128
- private static native long nativePrepareStatement (long connectionPtr , String sql );
129
- private static native void nativeFinalizeStatement (long connectionPtr , long statementPtr );
130
- private static native int nativeGetParameterCount (long connectionPtr , long statementPtr );
131
- private static native boolean nativeIsReadOnly (long connectionPtr , long statementPtr );
132
- private static native int nativeGetColumnCount (long connectionPtr , long statementPtr );
133
- private static native String nativeGetColumnName (long connectionPtr , long statementPtr ,
134
- int index );
135
- private static native void nativeBindNull (long connectionPtr , long statementPtr ,
136
- int index );
137
- private static native void nativeBindLong (long connectionPtr , long statementPtr ,
138
- int index , long value );
139
- private static native void nativeBindDouble (long connectionPtr , long statementPtr ,
140
- int index , double value );
141
- private static native void nativeBindString (long connectionPtr , long statementPtr ,
142
- int index , String value );
143
- private static native void nativeBindBlob (long connectionPtr , long statementPtr ,
144
- int index , byte [] value );
145
- private static native void nativeResetStatementAndClearBindings (
146
- long connectionPtr , long statementPtr );
147
- private static native void nativeExecute (long connectionPtr , long statementPtr );
148
- private static native long nativeExecuteForLong (long connectionPtr , long statementPtr );
149
- private static native String nativeExecuteForString (long connectionPtr , long statementPtr );
150
- private static native int nativeExecuteForBlobFileDescriptor (
151
- long connectionPtr , long statementPtr );
152
- private static native int nativeExecuteForChangedRowCount (long connectionPtr , long statementPtr );
153
- private static native long nativeExecuteForLastInsertedRowId (
154
- long connectionPtr , long statementPtr );
155
- private static native long nativeExecuteForCursorWindow (
156
- long connectionPtr , long statementPtr , CursorWindow win ,
157
- int startPos , int requiredPos , boolean countAllRows );
158
- private static native int nativeGetDbLookaside (long connectionPtr );
159
- private static native void nativeCancel (long connectionPtr );
160
- private static native void nativeResetCancel (long connectionPtr , boolean cancelable );
161
-
162
- private static native boolean nativeHasCodec ();
163
- public static boolean hasCodec (){ return nativeHasCodec (); }
120
+
121
+ public static boolean hasCodec (){ return NativeKt .HasCodec (); }
164
122
165
123
private SQLiteConnection (SQLiteConnectionPool pool ,
166
124
SQLiteDatabaseConfiguration configuration ,
@@ -178,7 +136,7 @@ private SQLiteConnection(SQLiteConnectionPool pool,
178
136
@ Override
179
137
protected void finalize () throws Throwable {
180
138
try {
181
- if (mPool != null && mConnectionPtr != 0 ) {
139
+ if (mPool != null && mConnectionPtr != null ) {
182
140
mPool .onConnectionLeaked ();
183
141
}
184
142
@@ -211,23 +169,23 @@ void close() {
211
169
}
212
170
213
171
private void open () {
214
- mConnectionPtr = nativeOpen (mConfiguration .path , mConfiguration .openFlags ,
172
+ mConnectionPtr = NativeKt . Open (mConfiguration .path , mConfiguration .openFlags ,
215
173
mConfiguration .label ,
216
174
SQLiteDebug .DEBUG_SQL_STATEMENTS , SQLiteDebug .DEBUG_SQL_TIME );
217
175
218
176
setPageSize ();
219
177
setForeignKeyModeFromConfiguration ();
220
178
setJournalSizeLimit ();
221
179
setAutoCheckpointInterval ();
222
- if ( ! nativeHasCodec () ){
180
+ if (! NativeKt . HasCodec () ){
223
181
setWalModeFromConfiguration ();
224
182
setLocaleFromConfiguration ();
225
183
}
226
184
// Register custom functions.
227
185
final int functionCount = mConfiguration .customFunctions .size ();
228
186
for (int i = 0 ; i < functionCount ; i ++) {
229
187
SQLiteCustomFunction function = mConfiguration .customFunctions .get (i );
230
- nativeRegisterCustomFunction (mConnectionPtr , function );
188
+ NativeKt . RegisterCustomFunction (mConnectionPtr , function );
231
189
}
232
190
}
233
191
@@ -239,12 +197,12 @@ private void dispose(boolean finalized) {
239
197
mCloseGuard .close ();
240
198
}
241
199
242
- if (mConnectionPtr != 0 ) {
200
+ if (mConnectionPtr != null ) {
243
201
final int cookie = mRecentOperations .beginOperation ("close" , null , null );
244
202
try {
245
203
mPreparedStatementCache .evictAll ();
246
- nativeClose (mConnectionPtr );
247
- mConnectionPtr = 0 ;
204
+ NativeKt . Close (mConnectionPtr );
205
+ mConnectionPtr = null ;
248
206
} finally {
249
207
mRecentOperations .endOperation (cookie );
250
208
}
@@ -364,7 +322,7 @@ private void setLocaleFromConfiguration() {
364
322
365
323
// Register the localized collators.
366
324
final String newLocale = mConfiguration .locale .toString ();
367
- nativeRegisterLocalizedCollators (mConnectionPtr , newLocale );
325
+ NativeKt . RegisterLocalizedCollators (mConnectionPtr , newLocale );
368
326
369
327
// If the database is read-only, we cannot modify the android metadata table
370
328
// or existing indexes.
@@ -390,7 +348,7 @@ private void setLocaleFromConfiguration() {
390
348
execute ("DELETE FROM android_metadata" , null , null );
391
349
execute ("INSERT INTO android_metadata (locale) VALUES(?)" ,
392
350
new Object [] { newLocale }, null );
393
- execute ("REINDEX LOCALIZED" , null , null );
351
+ // execute("REINDEX LOCALIZED", null, null);
394
352
success = true ;
395
353
} finally {
396
354
execute (success ? "COMMIT" : "ROLLBACK" , null , null );
@@ -402,7 +360,7 @@ private void setLocaleFromConfiguration() {
402
360
}
403
361
404
362
public void enableLocalizedCollators (){
405
- if ( nativeHasCodec () ){
363
+ if ( NativeKt . HasCodec () ){
406
364
setLocaleFromConfiguration ();
407
365
}
408
366
}
@@ -416,7 +374,7 @@ void reconfigure(SQLiteDatabaseConfiguration configuration) {
416
374
for (int i = 0 ; i < functionCount ; i ++) {
417
375
SQLiteCustomFunction function = configuration .customFunctions .get (i );
418
376
if (!mConfiguration .customFunctions .contains (function )) {
419
- nativeRegisterCustomFunction (mConnectionPtr , function );
377
+ NativeKt . RegisterCustomFunction (mConnectionPtr , function );
420
378
}
421
379
}
422
380
@@ -516,14 +474,14 @@ public void prepare(String sql, SQLiteStatementInfo outStatementInfo) {
516
474
outStatementInfo .numParameters = statement .mNumParameters ;
517
475
outStatementInfo .readOnly = statement .mReadOnly ;
518
476
519
- final int columnCount = nativeGetColumnCount (
477
+ final int columnCount = NativeKt . GetColumnCount (
520
478
mConnectionPtr , statement .mStatementPtr );
521
479
if (columnCount == 0 ) {
522
480
outStatementInfo .columnNames = EMPTY_STRING_ARRAY ;
523
481
} else {
524
482
outStatementInfo .columnNames = new String [columnCount ];
525
483
for (int i = 0 ; i < columnCount ; i ++) {
526
- outStatementInfo .columnNames [i ] = nativeGetColumnName (
484
+ outStatementInfo .columnNames [i ] = NativeKt . GetColumnName (
527
485
mConnectionPtr , statement .mStatementPtr , i );
528
486
}
529
487
}
@@ -565,7 +523,7 @@ public void execute(String sql, Object[] bindArgs,
565
523
applyBlockGuardPolicy (statement );
566
524
attachCancellationSignal (cancellationSignal );
567
525
try {
568
- nativeExecute (mConnectionPtr , statement .mStatementPtr );
526
+ NativeKt . Execute (mConnectionPtr , statement .mStatementPtr );
569
527
} finally {
570
528
detachCancellationSignal (cancellationSignal );
571
529
}
@@ -608,7 +566,7 @@ public long executeForLong(String sql, Object[] bindArgs,
608
566
applyBlockGuardPolicy (statement );
609
567
attachCancellationSignal (cancellationSignal );
610
568
try {
611
- return nativeExecuteForLong (mConnectionPtr , statement .mStatementPtr );
569
+ return NativeKt . ExecuteForLong (mConnectionPtr , statement .mStatementPtr );
612
570
} finally {
613
571
detachCancellationSignal (cancellationSignal );
614
572
}
@@ -651,7 +609,7 @@ public String executeForString(String sql, Object[] bindArgs,
651
609
applyBlockGuardPolicy (statement );
652
610
attachCancellationSignal (cancellationSignal );
653
611
try {
654
- return nativeExecuteForString (mConnectionPtr , statement .mStatementPtr );
612
+ return NativeKt . ExecuteForString (mConnectionPtr , statement .mStatementPtr );
655
613
} finally {
656
614
detachCancellationSignal (cancellationSignal );
657
615
}
@@ -697,7 +655,7 @@ public ParcelFileDescriptor executeForBlobFileDescriptor(String sql, Object[] bi
697
655
applyBlockGuardPolicy (statement );
698
656
attachCancellationSignal (cancellationSignal );
699
657
try {
700
- int fd = nativeExecuteForBlobFileDescriptor (
658
+ int fd = NativeKt . ExecuteForBlobFileDescriptor (
701
659
mConnectionPtr , statement .mStatementPtr );
702
660
return fd >= 0 ? ParcelFileDescriptor .adoptFd (fd ) : null ;
703
661
} finally {
@@ -744,7 +702,7 @@ public int executeForChangedRowCount(String sql, Object[] bindArgs,
744
702
applyBlockGuardPolicy (statement );
745
703
attachCancellationSignal (cancellationSignal );
746
704
try {
747
- changedRows = nativeExecuteForChangedRowCount (
705
+ changedRows = NativeKt . ExecuteForChangedRowCount (
748
706
mConnectionPtr , statement .mStatementPtr );
749
707
return changedRows ;
750
708
} finally {
@@ -792,7 +750,7 @@ public long executeForLastInsertedRowId(String sql, Object[] bindArgs,
792
750
applyBlockGuardPolicy (statement );
793
751
attachCancellationSignal (cancellationSignal );
794
752
try {
795
- return nativeExecuteForLastInsertedRowId (
753
+ return NativeKt . ExecuteForLastInsertedRowId (
796
754
mConnectionPtr , statement .mStatementPtr );
797
755
} finally {
798
756
detachCancellationSignal (cancellationSignal );
@@ -855,7 +813,7 @@ public int executeForCursorWindow(String sql, Object[] bindArgs,
855
813
applyBlockGuardPolicy (statement );
856
814
attachCancellationSignal (cancellationSignal );
857
815
try {
858
- final long result = nativeExecuteForCursorWindow (
816
+ final long result = NativeKt . ExecuteForCursorWindow (
859
817
mConnectionPtr , statement .mStatementPtr , window ,
860
818
startPos , requiredPos , countAllRows );
861
819
actualPos = (int )(result >> 32 );
@@ -899,11 +857,11 @@ private PreparedStatement acquirePreparedStatement(String sql) {
899
857
skipCache = true ;
900
858
}
901
859
902
- final long statementPtr = nativePrepareStatement (mConnectionPtr , sql );
860
+ final long statementPtr = NativeKt . PrepareStatement (mConnectionPtr , sql );
903
861
try {
904
- final int numParameters = nativeGetParameterCount (mConnectionPtr , statementPtr );
862
+ final int numParameters = NativeKt . GetParameterCount (mConnectionPtr , statementPtr );
905
863
final int type = DatabaseUtils .getSqlStatementType (sql );
906
- final boolean readOnly = nativeIsReadOnly (mConnectionPtr , statementPtr );
864
+ final boolean readOnly = NativeKt . IsReadOnly (mConnectionPtr , statementPtr );
907
865
statement = obtainPreparedStatement (sql , statementPtr , numParameters , type , readOnly );
908
866
if (!skipCache && isCacheable (type )) {
909
867
mPreparedStatementCache .put (sql , statement );
@@ -913,7 +871,7 @@ private PreparedStatement acquirePreparedStatement(String sql) {
913
871
// Finalize the statement if an exception occurred and we did not add
914
872
// it to the cache. If it is already in the cache, then leave it there.
915
873
if (statement == null || !statement .mInCache ) {
916
- nativeFinalizeStatement (mConnectionPtr , statementPtr );
874
+ NativeKt . FinalizeStatement (mConnectionPtr , statementPtr );
917
875
}
918
876
throw ex ;
919
877
}
@@ -925,7 +883,7 @@ private void releasePreparedStatement(PreparedStatement statement) {
925
883
statement .mInUse = false ;
926
884
if (statement .mInCache ) {
927
885
try {
928
- nativeResetStatementAndClearBindings (mConnectionPtr , statement .mStatementPtr );
886
+ NativeKt . ResetStatementAndClearBindings (mConnectionPtr , statement .mStatementPtr );
929
887
} catch (SQLiteException ex ) {
930
888
// The statement could not be reset due to an error. Remove it from the cache.
931
889
// When remove() is called, the cache will invoke its entryRemoved() callback,
@@ -945,7 +903,7 @@ private void releasePreparedStatement(PreparedStatement statement) {
945
903
}
946
904
947
905
private void finalizePreparedStatement (PreparedStatement statement ) {
948
- nativeFinalizeStatement (mConnectionPtr , statement .mStatementPtr );
906
+ NativeKt . FinalizeStatement (mConnectionPtr , statement .mStatementPtr );
949
907
recyclePreparedStatement (statement );
950
908
}
951
909
@@ -956,7 +914,7 @@ private void attachCancellationSignal(CancellationSignal cancellationSignal) {
956
914
mCancellationSignalAttachCount += 1 ;
957
915
if (mCancellationSignalAttachCount == 1 ) {
958
916
// Reset cancellation flag before executing the statement.
959
- nativeResetCancel (mConnectionPtr , true /*cancelable*/ );
917
+ NativeKt . ResetCancel (mConnectionPtr , true /*cancelable*/ );
960
918
961
919
// After this point, onCancel() may be called concurrently.
962
920
cancellationSignal .setOnCancelListener (this );
@@ -974,7 +932,7 @@ private void detachCancellationSignal(CancellationSignal cancellationSignal) {
974
932
cancellationSignal .setOnCancelListener (null );
975
933
976
934
// Reset cancellation flag after executing the statement.
977
- nativeResetCancel (mConnectionPtr , false /*cancelable*/ );
935
+ NativeKt . ResetCancel (mConnectionPtr , false /*cancelable*/ );
978
936
}
979
937
}
980
938
}
@@ -986,7 +944,7 @@ private void detachCancellationSignal(CancellationSignal cancellationSignal) {
986
944
// that the SQLite connection is still alive.
987
945
@ Override
988
946
public void onCancel () {
989
- nativeCancel (mConnectionPtr );
947
+ NativeKt . Cancel (mConnectionPtr );
990
948
}
991
949
992
950
private void bindArguments (PreparedStatement statement , Object [] bindArgs ) {
@@ -1005,28 +963,28 @@ private void bindArguments(PreparedStatement statement, Object[] bindArgs) {
1005
963
final Object arg = bindArgs [i ];
1006
964
switch (DatabaseUtils .getTypeOfObject (arg )) {
1007
965
case Cursor .FIELD_TYPE_NULL :
1008
- nativeBindNull (mConnectionPtr , statementPtr , i + 1 );
966
+ NativeKt . BindNull (mConnectionPtr , statementPtr , i + 1 );
1009
967
break ;
1010
968
case Cursor .FIELD_TYPE_INTEGER :
1011
- nativeBindLong (mConnectionPtr , statementPtr , i + 1 ,
969
+ NativeKt . BindLong (mConnectionPtr , statementPtr , i + 1 ,
1012
970
((Number )arg ).longValue ());
1013
971
break ;
1014
972
case Cursor .FIELD_TYPE_FLOAT :
1015
- nativeBindDouble (mConnectionPtr , statementPtr , i + 1 ,
973
+ NativeKt . BindDouble (mConnectionPtr , statementPtr , i + 1 ,
1016
974
((Number )arg ).doubleValue ());
1017
975
break ;
1018
976
case Cursor .FIELD_TYPE_BLOB :
1019
- nativeBindBlob (mConnectionPtr , statementPtr , i + 1 , (byte [])arg );
977
+ NativeKt . BindBlob (mConnectionPtr , statementPtr , i + 1 , (byte [])arg );
1020
978
break ;
1021
979
case Cursor .FIELD_TYPE_STRING :
1022
980
default :
1023
981
if (arg instanceof Boolean ) {
1024
982
// Provide compatibility with legacy applications which may pass
1025
983
// Boolean values in bind args.
1026
- nativeBindLong (mConnectionPtr , statementPtr , i + 1 ,
984
+ NativeKt . BindLong (mConnectionPtr , statementPtr , i + 1 ,
1027
985
((Boolean )arg ).booleanValue () ? 1 : 0 );
1028
986
} else {
1029
- nativeBindString (mConnectionPtr , statementPtr , i + 1 , arg .toString ());
987
+ NativeKt . BindString (mConnectionPtr , statementPtr , i + 1 , arg .toString ());
1030
988
}
1031
989
break ;
1032
990
}
@@ -1078,7 +1036,7 @@ public void dump(Printer printer, boolean verbose) {
1078
1036
void dumpUnsafe (Printer printer , boolean verbose ) {
1079
1037
printer .println ("Connection #" + mConnectionId + ":" );
1080
1038
if (verbose ) {
1081
- printer .println (" connectionPtr: 0x" + Long .toHexString (mConnectionPtr ));
1039
+ printer .println (" connectionPtr: 0x" + Long .toHexString (mConnectionPtr . hashCode () ));
1082
1040
}
1083
1041
printer .println (" isPrimaryConnection: " + mIsPrimaryConnection );
1084
1042
printer .println (" onlyAllowReadOnlyOperations: " + mOnlyAllowReadOnlyOperations );
@@ -1115,7 +1073,7 @@ String describeCurrentOperationUnsafe() {
1115
1073
*/
1116
1074
void collectDbStats (ArrayList <DbStats > dbStatsList ) {
1117
1075
// Get information about the main database.
1118
- int lookaside = nativeGetDbLookaside (mConnectionPtr );
1076
+ int lookaside = NativeKt . GetDbLookaside (mConnectionPtr );
1119
1077
long pageCount = 0 ;
1120
1078
long pageSize = 0 ;
1121
1079
try {
0 commit comments