gnunet-android

GNUnet for Android
Log | Files | Refs | README

SQLiteQuery.java (3147B)


      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 /*
     17 ** Modified to support SQLite extensions by the SQLite developers: 
     18 ** sqlite-dev@sqlite.org.
     19 */
     20 
     21 package org.sqlite.database.sqlite;
     22 
     23 import android.database.CursorWindow;
     24 import android.os.CancellationSignal;
     25 import android.os.OperationCanceledException;
     26 import android.util.Log;
     27 
     28 /**
     29  * Represents a query that reads the resulting rows into a {@link SQLiteQuery}.
     30  * This class is used by {@link SQLiteCursor} and isn't useful itself.
     31  * <p>
     32  * This class is not thread-safe.
     33  * </p>
     34  */
     35 public final class SQLiteQuery extends SQLiteProgram {
     36     private static final String TAG = "SQLiteQuery";
     37 
     38     private final CancellationSignal mCancellationSignal;
     39 
     40     SQLiteQuery(SQLiteDatabase db, String query, CancellationSignal cancellationSignal) {
     41         super(db, query, null, cancellationSignal);
     42 
     43         mCancellationSignal = cancellationSignal;
     44     }
     45 
     46     /**
     47      * Reads rows into a buffer.
     48      *
     49      * @param window The window to fill into
     50      * @param startPos The start position for filling the window.
     51      * @param requiredPos The position of a row that MUST be in the window.
     52      * If it won't fit, then the query should discard part of what it filled.
     53      * @param countAllRows True to count all rows that the query would
     54      * return regardless of whether they fit in the window.
     55      * @return Number of rows that were enumerated.  Might not be all rows
     56      * unless countAllRows is true.
     57      *
     58      * @throws SQLiteException if an error occurs.
     59      * @throws OperationCanceledException if the operation was canceled.
     60      */
     61     int fillWindow(CursorWindow window, int startPos, int requiredPos, boolean countAllRows) {
     62         acquireReference();
     63         try {
     64             window.acquireReference();
     65             try {
     66                 int numRows = getSession().executeForCursorWindow(getSql(), getBindArgs(),
     67                         window, startPos, requiredPos, countAllRows, getConnectionFlags(),
     68                         mCancellationSignal);
     69                 return numRows;
     70             } catch (SQLiteDatabaseCorruptException ex) {
     71                 onCorruption();
     72                 throw ex;
     73             } catch (SQLiteException ex) {
     74                 Log.e(TAG, "exception: " + ex.getMessage() + "; query: " + getSql());
     75                 throw ex;
     76             } finally {
     77                 window.releaseReference();
     78             }
     79         } finally {
     80             releaseReference();
     81         }
     82     }
     83 
     84     @Override
     85     public String toString() {
     86         return "SQLiteQuery: " + getSql();
     87     }
     88 }