Home | History | Annotate | Download | only in database
      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 package android.database;
     18 
     19 import android.content.ContentResolver;
     20 import android.net.Uri;
     21 import android.os.Bundle;
     22 
     23 import java.util.Map;
     24 
     25 /**
     26  * This interface provides random read-write access to the result set returned
     27  * by a database query.
     28  *
     29  * Cursor implementations are not required to be synchronized so code using a Cursor from multiple
     30  * threads should perform its own synchronization when using the Cursor.
     31  */
     32 public interface Cursor {
     33     /**
     34      * Returns the numbers of rows in the cursor.
     35      *
     36      * @return the number of rows in the cursor.
     37      */
     38     int getCount();
     39 
     40     /**
     41      * Returns the current position of the cursor in the row set.
     42      * The value is zero-based. When the row set is first returned the cursor
     43      * will be at positon -1, which is before the first row. After the
     44      * last row is returned another call to next() will leave the cursor past
     45      * the last entry, at a position of count().
     46      *
     47      * @return the current cursor position.
     48      */
     49     int getPosition();
     50 
     51     /**
     52      * Move the cursor by a relative amount, forward or backward, from the
     53      * current position. Positive offsets move forwards, negative offsets move
     54      * backwards. If the final position is outside of the bounds of the result
     55      * set then the resultant position will be pinned to -1 or count() depending
     56      * on whether the value is off the front or end of the set, respectively.
     57      *
     58      * <p>This method will return true if the requested destination was
     59      * reachable, otherwise, it returns false. For example, if the cursor is at
     60      * currently on the second entry in the result set and move(-5) is called,
     61      * the position will be pinned at -1, and false will be returned.
     62      *
     63      * @param offset the offset to be applied from the current position.
     64      * @return whether the requested move fully succeeded.
     65      */
     66     boolean move(int offset);
     67 
     68     /**
     69      * Move the cursor to an absolute position. The valid
     70      * range of values is -1 &lt;= position &lt;= count.
     71      *
     72      * <p>This method will return true if the request destination was reachable,
     73      * otherwise, it returns false.
     74      *
     75      * @param position the zero-based position to move to.
     76      * @return whether the requested move fully succeeded.
     77      */
     78     boolean moveToPosition(int position);
     79 
     80     /**
     81      * Move the cursor to the first row.
     82      *
     83      * <p>This method will return false if the cursor is empty.
     84      *
     85      * @return whether the move succeeded.
     86      */
     87     boolean moveToFirst();
     88 
     89     /**
     90      * Move the cursor to the last row.
     91      *
     92      * <p>This method will return false if the cursor is empty.
     93      *
     94      * @return whether the move succeeded.
     95      */
     96     boolean moveToLast();
     97 
     98     /**
     99      * Move the cursor to the next row.
    100      *
    101      * <p>This method will return false if the cursor is already past the
    102      * last entry in the result set.
    103      *
    104      * @return whether the move succeeded.
    105      */
    106     boolean moveToNext();
    107 
    108     /**
    109      * Move the cursor to the previous row.
    110      *
    111      * <p>This method will return false if the cursor is already before the
    112      * first entry in the result set.
    113      *
    114      * @return whether the move succeeded.
    115      */
    116     boolean moveToPrevious();
    117 
    118     /**
    119      * Returns whether the cursor is pointing to the first row.
    120      *
    121      * @return whether the cursor is pointing at the first entry.
    122      */
    123     boolean isFirst();
    124 
    125     /**
    126      * Returns whether the cursor is pointing to the last row.
    127      *
    128      * @return whether the cursor is pointing at the last entry.
    129      */
    130     boolean isLast();
    131 
    132     /**
    133      * Returns whether the cursor is pointing to the position before the first
    134      * row.
    135      *
    136      * @return whether the cursor is before the first result.
    137      */
    138     boolean isBeforeFirst();
    139 
    140     /**
    141      * Returns whether the cursor is pointing to the position after the last
    142      * row.
    143      *
    144      * @return whether the cursor is after the last result.
    145      */
    146     boolean isAfterLast();
    147 
    148     /**
    149      * Removes the row at the current cursor position from the underlying data
    150      * store. After this method returns the cursor will be pointing to the row
    151      * after the row that is deleted. This has the side effect of decrementing
    152      * the result of count() by one.
    153      * <p>
    154      * The query must have the row ID column in its selection, otherwise this
    155      * call will fail.
    156      *
    157      * @hide
    158      * @return whether the record was successfully deleted.
    159      * @deprecated use {@link ContentResolver#delete(Uri, String, String[])}
    160      */
    161     @Deprecated
    162     boolean deleteRow();
    163 
    164     /**
    165      * Returns the zero-based index for the given column name, or -1 if the column doesn't exist.
    166      * If you expect the column to exist use {@link #getColumnIndexOrThrow(String)} instead, which
    167      * will make the error more clear.
    168      *
    169      * @param columnName the name of the target column.
    170      * @return the zero-based column index for the given column name, or -1 if
    171      * the column name does not exist.
    172      * @see #getColumnIndexOrThrow(String)
    173      */
    174     int getColumnIndex(String columnName);
    175 
    176     /**
    177      * Returns the zero-based index for the given column name, or throws
    178      * {@link IllegalArgumentException} if the column doesn't exist. If you're not sure if
    179      * a column will exist or not use {@link #getColumnIndex(String)} and check for -1, which
    180      * is more efficient than catching the exceptions.
    181      *
    182      * @param columnName the name of the target column.
    183      * @return the zero-based column index for the given column name
    184      * @see #getColumnIndex(String)
    185      * @throws IllegalArgumentException if the column does not exist
    186      */
    187     int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException;
    188 
    189     /**
    190      * Returns the column name at the given zero-based column index.
    191      *
    192      * @param columnIndex the zero-based index of the target column.
    193      * @return the column name for the given column index.
    194      */
    195     String getColumnName(int columnIndex);
    196 
    197     /**
    198      * Returns a string array holding the names of all of the columns in the
    199      * result set in the order in which they were listed in the result.
    200      *
    201      * @return the names of the columns returned in this query.
    202      */
    203     String[] getColumnNames();
    204 
    205     /**
    206      * Return total number of columns
    207      * @return number of columns
    208      */
    209     int getColumnCount();
    210 
    211     /**
    212      * Returns the value of the requested column as a byte array.
    213      *
    214      * <p>If the native content of that column is not blob exception may throw
    215      *
    216      * @param columnIndex the zero-based index of the target column.
    217      * @return the value of that column as a byte array.
    218      */
    219     byte[] getBlob(int columnIndex);
    220 
    221     /**
    222      * Returns the value of the requested column as a String.
    223      *
    224      * <p>If the native content of that column is not text the result will be
    225      * the result of passing the column value to String.valueOf(x).
    226      *
    227      * @param columnIndex the zero-based index of the target column.
    228      * @return the value of that column as a String.
    229      */
    230     String getString(int columnIndex);
    231 
    232     /**
    233      * Retrieves the requested column text and stores it in the buffer provided.
    234      * If the buffer size is not sufficient, a new char buffer will be allocated
    235      * and assigned to CharArrayBuffer.data
    236      * @param columnIndex the zero-based index of the target column.
    237      *        if the target column is null, return buffer
    238      * @param buffer the buffer to copy the text into.
    239      */
    240     void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer);
    241 
    242     /**
    243      * Returns the value of the requested column as a short.
    244      *
    245      * <p>If the native content of that column is not numeric the result will be
    246      * the result of passing the column value to Short.valueOf(x).
    247      *
    248      * @param columnIndex the zero-based index of the target column.
    249      * @return the value of that column as a short.
    250      */
    251     short getShort(int columnIndex);
    252 
    253     /**
    254      * Returns the value of the requested column as an int.
    255      *
    256      * <p>If the native content of that column is not numeric the result will be
    257      * the result of passing the column value to Integer.valueOf(x).
    258      *
    259      * @param columnIndex the zero-based index of the target column.
    260      * @return the value of that column as an int.
    261      */
    262     int getInt(int columnIndex);
    263 
    264     /**
    265      * Returns the value of the requested column as a long.
    266      *
    267      * <p>If the native content of that column is not numeric the result will be
    268      * the result of passing the column value to Long.valueOf(x).
    269      *
    270      * @param columnIndex the zero-based index of the target column.
    271      * @return the value of that column as a long.
    272      */
    273     long getLong(int columnIndex);
    274 
    275     /**
    276      * Returns the value of the requested column as a float.
    277      *
    278      * <p>If the native content of that column is not numeric the result will be
    279      * the result of passing the column value to Float.valueOf(x).
    280      *
    281      * @param columnIndex the zero-based index of the target column.
    282      * @return the value of that column as a float.
    283      */
    284     float getFloat(int columnIndex);
    285 
    286     /**
    287      * Returns the value of the requested column as a double.
    288      *
    289      * <p>If the native content of that column is not numeric the result will be
    290      * the result of passing the column value to Double.valueOf(x).
    291      *
    292      * @param columnIndex the zero-based index of the target column.
    293      * @return the value of that column as a double.
    294      */
    295     double getDouble(int columnIndex);
    296 
    297     /**
    298      * Returns <code>true</code> if the value in the indicated column is null.
    299      *
    300      * @param columnIndex the zero-based index of the target column.
    301      * @return whether the column value is null.
    302      */
    303     boolean isNull(int columnIndex);
    304 
    305     /**
    306      * Returns <code>true</code> if the cursor supports updates.
    307      *
    308      * @return whether the cursor supports updates.
    309      * @hide
    310      * @deprecated use the {@link ContentResolver} update methods instead of the Cursor
    311      * update methods
    312      */
    313     @Deprecated
    314     boolean supportsUpdates();
    315 
    316     /**
    317      * Returns <code>true</code> if there are pending updates that have not yet been committed.
    318      *
    319      * @return <code>true</code> if there are pending updates that have not yet been committed.
    320      * @hide
    321      * @deprecated use the {@link ContentResolver} update methods instead of the Cursor
    322      * update methods
    323      */
    324     @Deprecated
    325     boolean hasUpdates();
    326 
    327     /**
    328      * Updates the value for the given column in the row the cursor is
    329      * currently pointing at. Updates are not committed to the backing store
    330      * until {@link #commitUpdates()} is called.
    331      *
    332      * @param columnIndex the zero-based index of the target column.
    333      * @param value the new value.
    334      * @return whether the operation succeeded.
    335      * @hide
    336      * @deprecated use the {@link ContentResolver} update methods instead of the Cursor
    337      * update methods
    338      */
    339     @Deprecated
    340     boolean updateBlob(int columnIndex, byte[] value);
    341 
    342     /**
    343      * Updates the value for the given column in the row the cursor is
    344      * currently pointing at. Updates are not committed to the backing store
    345      * until {@link #commitUpdates()} is called.
    346      *
    347      * @param columnIndex the zero-based index of the target column.
    348      * @param value the new value.
    349      * @return whether the operation succeeded.
    350      * @hide
    351      * @deprecated use the {@link ContentResolver} update methods instead of the Cursor
    352      * update methods
    353      */
    354     @Deprecated
    355     boolean updateString(int columnIndex, String value);
    356 
    357     /**
    358      * Updates the value for the given column in the row the cursor is
    359      * currently pointing at. Updates are not committed to the backing store
    360      * until {@link #commitUpdates()} is called.
    361      *
    362      * @param columnIndex the zero-based index of the target column.
    363      * @param value the new value.
    364      * @return whether the operation succeeded.
    365      * @hide
    366      * @deprecated use the {@link ContentResolver} update methods instead of the Cursor
    367      * update methods
    368      */
    369     @Deprecated
    370     boolean updateShort(int columnIndex, short value);
    371 
    372     /**
    373      * Updates the value for the given column in the row the cursor is
    374      * currently pointing at. Updates are not committed to the backing store
    375      * until {@link #commitUpdates()} is called.
    376      *
    377      * @param columnIndex the zero-based index of the target column.
    378      * @param value the new value.
    379      * @return whether the operation succeeded.
    380      * @hide
    381      * @deprecated use the {@link ContentResolver} update methods instead of the Cursor
    382      * update methods
    383      */
    384     @Deprecated
    385     boolean updateInt(int columnIndex, int value);
    386 
    387     /**
    388      * Updates the value for the given column in the row the cursor is
    389      * currently pointing at. Updates are not committed to the backing store
    390      * until {@link #commitUpdates()} is called.
    391      *
    392      * @param columnIndex the zero-based index of the target column.
    393      * @param value the new value.
    394      * @return whether the operation succeeded.
    395      * @hide
    396      * @deprecated use the {@link ContentResolver} update methods instead of the Cursor
    397      * update methods
    398      */
    399     @Deprecated
    400     boolean updateLong(int columnIndex, long value);
    401 
    402     /**
    403      * Updates the value for the given column in the row the cursor is
    404      * currently pointing at. Updates are not committed to the backing store
    405      * until {@link #commitUpdates()} is called.
    406      *
    407      * @param columnIndex the zero-based index of the target column.
    408      * @param value the new value.
    409      * @return whether the operation succeeded.
    410      * @hide
    411      * @deprecated use the {@link ContentResolver} update methods instead of the Cursor
    412      * update methods
    413      */
    414     @Deprecated
    415     boolean updateFloat(int columnIndex, float value);
    416 
    417     /**
    418      * Updates the value for the given column in the row the cursor is
    419      * currently pointing at. Updates are not committed to the backing store
    420      * until {@link #commitUpdates()} is called.
    421      *
    422      * @param columnIndex the zero-based index of the target column.
    423      * @param value the new value.
    424      * @return whether the operation succeeded.
    425      * @hide
    426      * @deprecated use the {@link ContentResolver} update methods instead of the Cursor
    427      * update methods
    428      */
    429     @Deprecated
    430     boolean updateDouble(int columnIndex, double value);
    431 
    432     /**
    433      * Removes the value for the given column in the row the cursor is
    434      * currently pointing at. Updates are not committed to the backing store
    435      * until {@link #commitUpdates()} is called.
    436      *
    437      * @param columnIndex the zero-based index of the target column.
    438      * @return whether the operation succeeded.
    439      * @hide
    440      * @deprecated use the {@link ContentResolver} update methods instead of the Cursor
    441      * update methods
    442      */
    443     @Deprecated
    444     boolean updateToNull(int columnIndex);
    445 
    446     /**
    447      * Atomically commits all updates to the backing store. After completion,
    448      * this method leaves the data in an inconsistent state and you should call
    449      * {@link #requery} before reading data from the cursor again.
    450      *
    451      * @return whether the operation succeeded.
    452      * @hide
    453      * @deprecated use the {@link ContentResolver} update methods instead of the Cursor
    454      * update methods
    455      */
    456     @Deprecated
    457     boolean commitUpdates();
    458 
    459     /**
    460      * Atomically commits all updates to the backing store, as well as the
    461      * updates included in values. After completion,
    462      * this method leaves the data in an inconsistent state and you should call
    463      * {@link #requery} before reading data from the cursor again.
    464      *
    465      * @param values A map from row IDs to Maps associating column names with
    466      *               updated values. A null value indicates the field should be
    467                      removed.
    468      * @return whether the operation succeeded.
    469      * @hide
    470      * @deprecated use the {@link ContentResolver} update methods instead of the Cursor
    471      * update methods
    472      */
    473     @Deprecated
    474     boolean commitUpdates(Map<? extends Long,
    475             ? extends Map<String,Object>> values);
    476 
    477     /**
    478      * Reverts all updates made to the cursor since the last call to
    479      * commitUpdates.
    480      * @hide
    481      * @deprecated use the {@link ContentResolver} update methods instead of the Cursor
    482      * update methods
    483      */
    484     @Deprecated
    485     void abortUpdates();
    486 
    487     /**
    488      * Deactivates the Cursor, making all calls on it fail until {@link #requery} is called.
    489      * Inactive Cursors use fewer resources than active Cursors.
    490      * Calling {@link #requery} will make the cursor active again.
    491      */
    492     void deactivate();
    493 
    494     /**
    495      * Performs the query that created the cursor again, refreshing its
    496      * contents. This may be done at any time, including after a call to {@link
    497      * #deactivate}.
    498      *
    499      * @return true if the requery succeeded, false if not, in which case the
    500      *         cursor becomes invalid.
    501      */
    502     boolean requery();
    503 
    504     /**
    505      * Closes the Cursor, releasing all of its resources and making it completely invalid.
    506      * Unlike {@link #deactivate()} a call to {@link #requery()} will not make the Cursor valid
    507      * again.
    508      */
    509     void close();
    510 
    511     /**
    512      * return true if the cursor is closed
    513      * @return true if the cursor is closed.
    514      */
    515     boolean isClosed();
    516 
    517     /**
    518      * Register an observer that is called when changes happen to the content backing this cursor.
    519      * Typically the data set won't change until {@link #requery()} is called.
    520      *
    521      * @param observer the object that gets notified when the content backing the cursor changes.
    522      * @see #unregisterContentObserver(ContentObserver)
    523      */
    524     void registerContentObserver(ContentObserver observer);
    525 
    526     /**
    527      * Unregister an observer that has previously been registered with this
    528      * cursor via {@link #registerContentObserver}.
    529      *
    530      * @param observer the object to unregister.
    531      * @see #registerContentObserver(ContentObserver)
    532      */
    533     void unregisterContentObserver(ContentObserver observer);
    534 
    535     /**
    536      * Register an observer that is called when changes happen to the contents
    537      * of the this cursors data set, for example, when the data set is changed via
    538      * {@link #requery()}, {@link #deactivate()}, or {@link #close()}.
    539      *
    540      * @param observer the object that gets notified when the cursors data set changes.
    541      * @see #unregisterDataSetObserver(DataSetObserver)
    542      */
    543     void registerDataSetObserver(DataSetObserver observer);
    544 
    545     /**
    546      * Unregister an observer that has previously been registered with this
    547      * cursor via {@link #registerContentObserver}.
    548      *
    549      * @param observer the object to unregister.
    550      * @see #registerDataSetObserver(DataSetObserver)
    551      */
    552     void unregisterDataSetObserver(DataSetObserver observer);
    553 
    554     /**
    555      * Register to watch a content URI for changes. This can be the URI of a specific data row (for
    556      * example, "content://my_provider_type/23"), or a a generic URI for a content type.
    557      *
    558      * @param cr The content resolver from the caller's context. The listener attached to
    559      * this resolver will be notified.
    560      * @param uri The content URI to watch.
    561      */
    562     void setNotificationUri(ContentResolver cr, Uri uri);
    563 
    564     /**
    565      * onMove() will only be called across processes if this method returns true.
    566      * @return whether all cursor movement should result in a call to onMove().
    567      */
    568     boolean getWantsAllOnMoveCalls();
    569 
    570     /**
    571      * Returns a bundle of extra values. This is an optional way for cursors to provide out-of-band
    572      * metadata to their users. One use of this is for reporting on the progress of network requests
    573      * that are required to fetch data for the cursor.
    574      *
    575      * <p>These values may only change when requery is called.
    576      * @return cursor-defined values, or Bundle.EMTPY if there are no values. Never null.
    577      */
    578     Bundle getExtras();
    579 
    580     /**
    581      * This is an out-of-band way for the the user of a cursor to communicate with the cursor. The
    582      * structure of each bundle is entirely defined by the cursor.
    583      *
    584      * <p>One use of this is to tell a cursor that it should retry its network request after it
    585      * reported an error.
    586      * @param extras extra values, or Bundle.EMTPY. Never null.
    587      * @return extra values, or Bundle.EMTPY. Never null.
    588      */
    589     Bundle respond(Bundle extras);
    590 }
    591