Cursor.java revision 8b0dd7da360d70920a37802eb455ba41500d3b45
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
17package android.database;
18
19import android.content.ContentResolver;
20import android.net.Uri;
21import android.os.Bundle;
22
23import 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 */
32public interface Cursor {
33    /*
34     * Values returned by {@link #getType(int)}.
35     * These should be consistent with the corresponding types defined in CursorWindow.h
36     */
37    /** Value returned by {@link #getType(int)} if the specified column is null */
38    static final int FIELD_TYPE_NULL = 0;
39
40    /** Value returned by {@link #getType(int)} if the specified  column type is integer */
41    static final int FIELD_TYPE_INTEGER = 1;
42
43    /** Value returned by {@link #getType(int)} if the specified column type is float */
44    static final int FIELD_TYPE_FLOAT = 2;
45
46    /** Value returned by {@link #getType(int)} if the specified column type is string */
47    static final int FIELD_TYPE_STRING = 3;
48
49    /** Value returned by {@link #getType(int)} if the specified column type is blob */
50    static final int FIELD_TYPE_BLOB = 4;
51
52    /**
53     * Returns the numbers of rows in the cursor.
54     *
55     * @return the number of rows in the cursor.
56     */
57    int getCount();
58
59    /**
60     * Returns the current position of the cursor in the row set.
61     * The value is zero-based. When the row set is first returned the cursor
62     * will be at positon -1, which is before the first row. After the
63     * last row is returned another call to next() will leave the cursor past
64     * the last entry, at a position of count().
65     *
66     * @return the current cursor position.
67     */
68    int getPosition();
69
70    /**
71     * Move the cursor by a relative amount, forward or backward, from the
72     * current position. Positive offsets move forwards, negative offsets move
73     * backwards. If the final position is outside of the bounds of the result
74     * set then the resultant position will be pinned to -1 or count() depending
75     * on whether the value is off the front or end of the set, respectively.
76     *
77     * <p>This method will return true if the requested destination was
78     * reachable, otherwise, it returns false. For example, if the cursor is at
79     * currently on the second entry in the result set and move(-5) is called,
80     * the position will be pinned at -1, and false will be returned.
81     *
82     * @param offset the offset to be applied from the current position.
83     * @return whether the requested move fully succeeded.
84     */
85    boolean move(int offset);
86
87    /**
88     * Move the cursor to an absolute position. The valid
89     * range of values is -1 &lt;= position &lt;= count.
90     *
91     * <p>This method will return true if the request destination was reachable,
92     * otherwise, it returns false.
93     *
94     * @param position the zero-based position to move to.
95     * @return whether the requested move fully succeeded.
96     */
97    boolean moveToPosition(int position);
98
99    /**
100     * Move the cursor to the first row.
101     *
102     * <p>This method will return false if the cursor is empty.
103     *
104     * @return whether the move succeeded.
105     */
106    boolean moveToFirst();
107
108    /**
109     * Move the cursor to the last row.
110     *
111     * <p>This method will return false if the cursor is empty.
112     *
113     * @return whether the move succeeded.
114     */
115    boolean moveToLast();
116
117    /**
118     * Move the cursor to the next row.
119     *
120     * <p>This method will return false if the cursor is already past the
121     * last entry in the result set.
122     *
123     * @return whether the move succeeded.
124     */
125    boolean moveToNext();
126
127    /**
128     * Move the cursor to the previous row.
129     *
130     * <p>This method will return false if the cursor is already before the
131     * first entry in the result set.
132     *
133     * @return whether the move succeeded.
134     */
135    boolean moveToPrevious();
136
137    /**
138     * Returns whether the cursor is pointing to the first row.
139     *
140     * @return whether the cursor is pointing at the first entry.
141     */
142    boolean isFirst();
143
144    /**
145     * Returns whether the cursor is pointing to the last row.
146     *
147     * @return whether the cursor is pointing at the last entry.
148     */
149    boolean isLast();
150
151    /**
152     * Returns whether the cursor is pointing to the position before the first
153     * row.
154     *
155     * @return whether the cursor is before the first result.
156     */
157    boolean isBeforeFirst();
158
159    /**
160     * Returns whether the cursor is pointing to the position after the last
161     * row.
162     *
163     * @return whether the cursor is after the last result.
164     */
165    boolean isAfterLast();
166
167    /**
168     * Returns the zero-based index for the given column name, or -1 if the column doesn't exist.
169     * If you expect the column to exist use {@link #getColumnIndexOrThrow(String)} instead, which
170     * will make the error more clear.
171     *
172     * @param columnName the name of the target column.
173     * @return the zero-based column index for the given column name, or -1 if
174     * the column name does not exist.
175     * @see #getColumnIndexOrThrow(String)
176     */
177    int getColumnIndex(String columnName);
178
179    /**
180     * Returns the zero-based index for the given column name, or throws
181     * {@link IllegalArgumentException} if the column doesn't exist. If you're not sure if
182     * a column will exist or not use {@link #getColumnIndex(String)} and check for -1, which
183     * is more efficient than catching the exceptions.
184     *
185     * @param columnName the name of the target column.
186     * @return the zero-based column index for the given column name
187     * @see #getColumnIndex(String)
188     * @throws IllegalArgumentException if the column does not exist
189     */
190    int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException;
191
192    /**
193     * Returns the column name at the given zero-based column index.
194     *
195     * @param columnIndex the zero-based index of the target column.
196     * @return the column name for the given column index.
197     */
198    String getColumnName(int columnIndex);
199
200    /**
201     * Returns a string array holding the names of all of the columns in the
202     * result set in the order in which they were listed in the result.
203     *
204     * @return the names of the columns returned in this query.
205     */
206    String[] getColumnNames();
207
208    /**
209     * Return total number of columns
210     * @return number of columns
211     */
212    int getColumnCount();
213
214    /**
215     * Returns the value of the requested column as a byte array.
216     *
217     * <p>If the native content of that column is not blob exception may throw
218     *
219     * @param columnIndex the zero-based index of the target column.
220     * @return the value of that column as a byte array.
221     */
222    byte[] getBlob(int columnIndex);
223
224    /**
225     * Returns the value of the requested column as a String.
226     *
227     * <p>If the native content of that column is not text the result will be
228     * the result of passing the column value to String.valueOf(x).
229     *
230     * @param columnIndex the zero-based index of the target column.
231     * @return the value of that column as a String.
232     */
233    String getString(int columnIndex);
234
235    /**
236     * Retrieves the requested column text and stores it in the buffer provided.
237     * If the buffer size is not sufficient, a new char buffer will be allocated
238     * and assigned to CharArrayBuffer.data
239     * @param columnIndex the zero-based index of the target column.
240     *        if the target column is null, return buffer
241     * @param buffer the buffer to copy the text into.
242     */
243    void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer);
244
245    /**
246     * Returns the value of the requested column as a short.
247     *
248     * <p>If the native content of that column is not numeric the result will be
249     * the result of passing the column value to Short.valueOf(x).
250     *
251     * @param columnIndex the zero-based index of the target column.
252     * @return the value of that column as a short.
253     */
254    short getShort(int columnIndex);
255
256    /**
257     * Returns the value of the requested column as an int.
258     *
259     * <p>If the native content of that column is not numeric the result will be
260     * the result of passing the column value to Integer.valueOf(x).
261     *
262     * @param columnIndex the zero-based index of the target column.
263     * @return the value of that column as an int.
264     */
265    int getInt(int columnIndex);
266
267    /**
268     * Returns the value of the requested column as a long.
269     *
270     * <p>If the native content of that column is not numeric the result will be
271     * the result of passing the column value to Long.valueOf(x).
272     *
273     * @param columnIndex the zero-based index of the target column.
274     * @return the value of that column as a long.
275     */
276    long getLong(int columnIndex);
277
278    /**
279     * Returns the value of the requested column as a float.
280     *
281     * <p>If the native content of that column is not numeric the result will be
282     * the result of passing the column value to Float.valueOf(x).
283     *
284     * @param columnIndex the zero-based index of the target column.
285     * @return the value of that column as a float.
286     */
287    float getFloat(int columnIndex);
288
289    /**
290     * Returns the value of the requested column as a double.
291     *
292     * <p>If the native content of that column is not numeric the result will be
293     * the result of passing the column value to Double.valueOf(x).
294     *
295     * @param columnIndex the zero-based index of the target column.
296     * @return the value of that column as a double.
297     */
298    double getDouble(int columnIndex);
299
300    /**
301     * Returns data type of the given column's value.
302     * The preferred type of the column is returned but the data may be converted to other types
303     * as documented in the get-type methods such as {@link #getInt(int)}, {@link #getFloat(int)}
304     * etc.
305     *<p>
306     * Returned column types are
307     * <ul>
308     *   <li>{@link #FIELD_TYPE_NULL}</li>
309     *   <li>{@link #FIELD_TYPE_INTEGER}</li>
310     *   <li>{@link #FIELD_TYPE_FLOAT}</li>
311     *   <li>{@link #FIELD_TYPE_STRING}</li>
312     *   <li>{@link #FIELD_TYPE_BLOB}</li>
313     *</ul>
314     *</p>
315     *
316     * @param columnIndex the zero-based index of the target column.
317     * @return column value type
318     */
319    int getType(int columnIndex);
320
321    /**
322     * Returns <code>true</code> if the value in the indicated column is null.
323     *
324     * @param columnIndex the zero-based index of the target column.
325     * @return whether the column value is null.
326     */
327    boolean isNull(int columnIndex);
328
329    /**
330     * Deactivates the Cursor, making all calls on it fail until {@link #requery} is called.
331     * Inactive Cursors use fewer resources than active Cursors.
332     * Calling {@link #requery} will make the cursor active again.
333     */
334    void deactivate();
335
336    /**
337     * Performs the query that created the cursor again, refreshing its
338     * contents. This may be done at any time, including after a call to {@link
339     * #deactivate}.
340     *
341     * Since this method could execute a query on the database and potentially take
342     * a while, it could cause ANR if it is called on Main (UI) thread.
343     * A warning is printed if this method is being executed on Main thread.
344     *
345     * @return true if the requery succeeded, false if not, in which case the
346     *         cursor becomes invalid.
347     */
348    boolean requery();
349
350    /**
351     * Closes the Cursor, releasing all of its resources and making it completely invalid.
352     * Unlike {@link #deactivate()} a call to {@link #requery()} will not make the Cursor valid
353     * again.
354     */
355    void close();
356
357    /**
358     * return true if the cursor is closed
359     * @return true if the cursor is closed.
360     */
361    boolean isClosed();
362
363    /**
364     * Register an observer that is called when changes happen to the content backing this cursor.
365     * Typically the data set won't change until {@link #requery()} is called.
366     *
367     * @param observer the object that gets notified when the content backing the cursor changes.
368     * @see #unregisterContentObserver(ContentObserver)
369     */
370    void registerContentObserver(ContentObserver observer);
371
372    /**
373     * Unregister an observer that has previously been registered with this
374     * cursor via {@link #registerContentObserver}.
375     *
376     * @param observer the object to unregister.
377     * @see #registerContentObserver(ContentObserver)
378     */
379    void unregisterContentObserver(ContentObserver observer);
380
381    /**
382     * Register an observer that is called when changes happen to the contents
383     * of the this cursors data set, for example, when the data set is changed via
384     * {@link #requery()}, {@link #deactivate()}, or {@link #close()}.
385     *
386     * @param observer the object that gets notified when the cursors data set changes.
387     * @see #unregisterDataSetObserver(DataSetObserver)
388     */
389    void registerDataSetObserver(DataSetObserver observer);
390
391    /**
392     * Unregister an observer that has previously been registered with this
393     * cursor via {@link #registerContentObserver}.
394     *
395     * @param observer the object to unregister.
396     * @see #registerDataSetObserver(DataSetObserver)
397     */
398    void unregisterDataSetObserver(DataSetObserver observer);
399
400    /**
401     * Register to watch a content URI for changes. This can be the URI of a specific data row (for
402     * example, "content://my_provider_type/23"), or a a generic URI for a content type.
403     *
404     * @param cr The content resolver from the caller's context. The listener attached to
405     * this resolver will be notified.
406     * @param uri The content URI to watch.
407     */
408    void setNotificationUri(ContentResolver cr, Uri uri);
409
410    /**
411     * onMove() will only be called across processes if this method returns true.
412     * @return whether all cursor movement should result in a call to onMove().
413     */
414    boolean getWantsAllOnMoveCalls();
415
416    /**
417     * Returns a bundle of extra values. This is an optional way for cursors to provide out-of-band
418     * metadata to their users. One use of this is for reporting on the progress of network requests
419     * that are required to fetch data for the cursor.
420     *
421     * <p>These values may only change when requery is called.
422     * @return cursor-defined values, or Bundle.EMTPY if there are no values. Never null.
423     */
424    Bundle getExtras();
425
426    /**
427     * This is an out-of-band way for the the user of a cursor to communicate with the cursor. The
428     * structure of each bundle is entirely defined by the cursor.
429     *
430     * <p>One use of this is to tell a cursor that it should retry its network request after it
431     * reported an error.
432     * @param extras extra values, or Bundle.EMTPY. Never null.
433     * @return extra values, or Bundle.EMTPY. Never null.
434     */
435    Bundle respond(Bundle extras);
436}
437