SQLite Android Bindings
Check-in [be6acc5363]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add extra required utility functions to ExtraUtils.java.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: be6acc53632ef353a269defd98f11d3927bc99c4
User & Date: dan 2013-12-23 07:33:46
Context
2013-12-24
12:07
In SQLITE_HAS_CODEC builds, do not initialize the LOCALIZED collation automatically (as if the SQLiteDatabase.NO_LOCALIZED_COLLATORS flag was set). Require apps to call the enableLocalizedCollators() method to explicitly initialize it. This gives the app an opportunity to execute a PRAGMA statement to configure an encryption key before the database is first accessed. check-in: 9c4a073c3b user: dan tags: trunk
2013-12-23
18:23
Add support for SEE, SQLite's encryption extension. check-in: 409082dd02 user: dan tags: openseedatabase
07:33
Add extra required utility functions to ExtraUtils.java. check-in: be6acc5363 user: dan tags: trunk
2013-12-21
19:32
Update jni/README. check-in: 1b80ccf163 user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/org/sqlite/database/sqlite/ExtraUtils.java.

   108    108        * the requested row.
   109    109        * @hide
   110    110        */
   111    111       public static int cursorPickFillWindowStartPosition(
   112    112               int cursorPosition, int cursorWindowCapacity) {
   113    113           return Math.max(cursorPosition - cursorWindowCapacity / 3, 0);
   114    114       }
          115  +
          116  +    /**
          117  +     * Returns data type of the given object's value.
          118  +     *<p>
          119  +     * Returned values are
          120  +     * <ul>
          121  +     *   <li>{@link Cursor#FIELD_TYPE_NULL}</li>
          122  +     *   <li>{@link Cursor#FIELD_TYPE_INTEGER}</li>
          123  +     *   <li>{@link Cursor#FIELD_TYPE_FLOAT}</li>
          124  +     *   <li>{@link Cursor#FIELD_TYPE_STRING}</li>
          125  +     *   <li>{@link Cursor#FIELD_TYPE_BLOB}</li>
          126  +     *</ul>
          127  +     *</p>
          128  +     *
          129  +     * @param obj the object whose value type is to be returned
          130  +     * @return object value type
          131  +     */
          132  +    public static int getTypeOfObject(Object obj) {
          133  +        if (obj == null) {
          134  +            return Cursor.FIELD_TYPE_NULL;
          135  +        } else if (obj instanceof byte[]) {
          136  +            return Cursor.FIELD_TYPE_BLOB;
          137  +        } else if (obj instanceof Float || obj instanceof Double) {
          138  +            return Cursor.FIELD_TYPE_FLOAT;
          139  +        } else if (obj instanceof Long || obj instanceof Integer
          140  +                || obj instanceof Short || obj instanceof Byte) {
          141  +            return Cursor.FIELD_TYPE_INTEGER;
          142  +        } else {
          143  +            return Cursor.FIELD_TYPE_STRING;
          144  +        }
          145  +    }
          146  +
          147  +    /**
          148  +     * Utility method to run the query on the db and return the value in the
          149  +     * first column of the first row.
          150  +     */
          151  +    public static long longForQuery(
          152  +        SQLiteDatabase db, String query, String[] selectionArgs
          153  +    ) {
          154  +        SQLiteStatement prog = db.compileStatement(query);
          155  +        try {
          156  +            return longForQuery(prog, selectionArgs);
          157  +        } finally {
          158  +            prog.close();
          159  +        }
          160  +    }
          161  +
          162  +    /**
          163  +     * Utility method to run the pre-compiled query and return the value in the
          164  +     * first column of the first row.
          165  +     */
          166  +    public static long longForQuery(
          167  +        SQLiteStatement prog, String[] selectionArgs
          168  +    ) {
          169  +        prog.bindAllArgsAsStrings(selectionArgs);
          170  +        return prog.simpleQueryForLong();
          171  +    }
   115    172   }

Changes to src/org/sqlite/database/sqlite/SQLiteConnection.java.

    18     18   
    19     19   /* import dalvik.system.BlockGuard; */
    20     20   import dalvik.system.CloseGuard;
    21     21   
    22     22   import android.database.Cursor;
    23     23   import android.database.CursorWindow;
    24     24   import android.database.DatabaseUtils;
           25  +import org.sqlite.database.ExtraUtils;
    25     26   import org.sqlite.database.sqlite.SQLiteDebug.DbStats;
    26     27   import android.os.CancellationSignal;
    27     28   import android.os.OperationCanceledException;
    28     29   import android.os.ParcelFileDescriptor;
    29     30   import android.util.Log;
    30     31   import android.util.LruCache;
    31     32   import android.util.Printer;
................................................................................
   415    416                   & SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING) != 0;
   416    417           boolean localeChanged = !configuration.locale.equals(mConfiguration.locale);
   417    418   
   418    419           // Update configuration parameters.
   419    420           mConfiguration.updateParametersFrom(configuration);
   420    421   
   421    422           // Update prepared statement cache size.
   422         -        /* mPreparedStatementCache.resize(configuration.maxSqlCacheSize); */
          423  +        mPreparedStatementCache.resize(configuration.maxSqlCacheSize);
   423    424   
   424    425           // Update foreign key mode.
   425    426           if (foreignKeyModeChanged) {
   426    427               setForeignKeyModeFromConfiguration();
   427    428           }
   428    429   
   429    430           // Update WAL.
................................................................................
   972    973       // detachCancellationSignal, while a statement is executing.  We can safely assume
   973    974       // that the SQLite connection is still alive.
   974    975       @Override
   975    976       public void onCancel() {
   976    977           nativeCancel(mConnectionPtr);
   977    978       }
   978    979   
   979         -    private static int databaseutils_getTypeOfObject(Object obj) {
   980         -        if (obj == null) {
   981         -            return Cursor.FIELD_TYPE_NULL;
   982         -        } else if (obj instanceof byte[]) {
   983         -            return Cursor.FIELD_TYPE_BLOB;
   984         -        } else if (obj instanceof Float || obj instanceof Double) {
   985         -            return Cursor.FIELD_TYPE_FLOAT;
   986         -        } else if (obj instanceof Long || obj instanceof Integer
   987         -                || obj instanceof Short || obj instanceof Byte) {
   988         -            return Cursor.FIELD_TYPE_INTEGER;
   989         -        } else {
   990         -            return Cursor.FIELD_TYPE_STRING;
   991         -        }
   992         -    }
   993         -
   994    980       private void bindArguments(PreparedStatement statement, Object[] bindArgs) {
   995    981           final int count = bindArgs != null ? bindArgs.length : 0;
   996    982           if (count != statement.mNumParameters) {
   997    983               throw new SQLiteBindOrColumnIndexOutOfRangeException(
   998    984                       "Expected " + statement.mNumParameters + " bind arguments but "
   999    985                       + count + " were provided.");
  1000    986           }
................................................................................
  1001    987           if (count == 0) {
  1002    988               return;
  1003    989           }
  1004    990   
  1005    991           final int statementPtr = statement.mStatementPtr;
  1006    992           for (int i = 0; i < count; i++) {
  1007    993               final Object arg = bindArgs[i];
  1008         -            switch (databaseutils_getTypeOfObject(arg)) {
          994  +            switch (ExtraUtils.getTypeOfObject(arg)) {
  1009    995                   case Cursor.FIELD_TYPE_NULL:
  1010    996                       nativeBindNull(mConnectionPtr, statementPtr, i + 1);
  1011    997                       break;
  1012    998                   case Cursor.FIELD_TYPE_INTEGER:
  1013    999                       nativeBindLong(mConnectionPtr, statementPtr, i + 1,
  1014   1000                               ((Number)arg).longValue());
  1015   1001                       break;

Changes to src/org/sqlite/database/sqlite/SQLiteDatabase.java.

    16     16   
    17     17   package org.sqlite.database.sqlite;
    18     18   
    19     19   import android.content.ContentValues;
    20     20   import android.database.Cursor;
    21     21   import org.sqlite.database.DatabaseErrorHandler;
    22     22   import android.database.DatabaseUtils;
           23  +import org.sqlite.database.ExtraUtils;
    23     24   import org.sqlite.database.DefaultDatabaseErrorHandler;
    24     25   import org.sqlite.database.SQLException;
    25     26   import org.sqlite.database.sqlite.SQLiteDebug.DbStats;
    26     27   import android.os.CancellationSignal;
    27     28   import android.os.Looper;
    28     29   import android.os.OperationCanceledException;
    29     30   import android.text.TextUtils;
................................................................................
   849    850               } catch (RuntimeException ex) {
   850    851                   mConfigurationLocked.customFunctions.remove(wrapper);
   851    852                   throw ex;
   852    853               }
   853    854           }
   854    855       }
   855    856   
   856         -    /**
   857         -     * Utility method to run the query on the db and return the value in the
   858         -     * first column of the first row.
   859         -     */
   860         -    private static long databaseutils_longForQuery(
   861         -        SQLiteDatabase db, String query, String[] selectionArgs
   862         -    ) {
   863         -        SQLiteStatement prog = db.compileStatement(query);
   864         -        try {
   865         -            return databaseutils_longForQuery(prog, selectionArgs);
   866         -        } finally {
   867         -            prog.close();
   868         -        }
   869         -    }
   870         -
   871         -    /**
   872         -     * Utility method to run the pre-compiled query and return the value in the
   873         -     * first column of the first row.
   874         -     */
   875         -    private static long databaseutils_longForQuery(
   876         -        SQLiteStatement prog, String[] selectionArgs
   877         -    ) {
   878         -        prog.bindAllArgsAsStrings(selectionArgs);
   879         -        return prog.simpleQueryForLong();
   880         -    }
   881         -
   882    857       /**
   883    858        * Gets the database version.
   884    859        *
   885    860        * @return the database version
   886    861        */
   887    862       public int getVersion() {
   888         -        return ((Long) databaseutils_longForQuery(this, "PRAGMA user_version;", null)).intValue();
          863  +        return ((Long) ExtraUtils.longForQuery(this, "PRAGMA user_version;", null)).intValue();
   889    864       }
   890    865   
   891    866       /**
   892    867        * Sets the database version.
   893    868        *
   894    869        * @param version the new database version
   895    870        */
................................................................................
   899    874   
   900    875       /**
   901    876        * Returns the maximum size the database may grow to.
   902    877        *
   903    878        * @return the new maximum database size
   904    879        */
   905    880       public long getMaximumSize() {
   906         -        long pageCount = databaseutils_longForQuery(this, "PRAGMA max_page_count;", null);
          881  +        long pageCount = ExtraUtils.longForQuery(this, "PRAGMA max_page_count;", null);
   907    882           return pageCount * getPageSize();
   908    883       }
   909    884   
   910    885       /**
   911    886        * Sets the maximum size the database will grow to. The maximum size cannot
   912    887        * be set below the current size.
   913    888        *
................................................................................
   917    892       public long setMaximumSize(long numBytes) {
   918    893           long pageSize = getPageSize();
   919    894           long numPages = numBytes / pageSize;
   920    895           // If numBytes isn't a multiple of pageSize, bump up a page
   921    896           if ((numBytes % pageSize) != 0) {
   922    897               numPages++;
   923    898           }
   924         -        long newPageCount = databaseutils_longForQuery(this, "PRAGMA max_page_count = " + numPages,
          899  +        long newPageCount = ExtraUtils.longForQuery(this, "PRAGMA max_page_count = " + numPages,
   925    900                   null);
   926    901           return newPageCount * pageSize;
   927    902       }
   928    903   
   929    904       /**
   930    905        * Returns the current database page size, in bytes.
   931    906        *
   932    907        * @return the database page size, in bytes
   933    908        */
   934    909       public long getPageSize() {
   935         -        return databaseutils_longForQuery(this, "PRAGMA page_size;", null);
          910  +        return ExtraUtils.longForQuery(this, "PRAGMA page_size;", null);
   936    911       }
   937    912   
   938    913       /**
   939    914        * Sets the database page size. The page size must be a power of two. This
   940    915        * method does not work if any data has been written to the database file,
   941    916        * and must be called right after the database has been created.
   942    917        *