blob: 1379138b37c0632a9944e7bf0abdd924b8e4c639 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Sudheer Shankaa07b0522019-01-26 10:40:42 -080019import android.annotation.NonNull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.ContentResolver;
21import android.net.Uri;
22import android.os.Bundle;
23
Jeff Brown03bd3022012-03-06 13:48:56 -080024import java.io.Closeable;
Sudheer Shankaa07b0522019-01-26 10:40:42 -080025import java.util.Arrays;
26import java.util.List;
Jeff Brown03bd3022012-03-06 13:48:56 -080027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028/**
29 * This interface provides random read-write access to the result set returned
30 * by a database query.
Jeff Brown511cd352013-08-23 17:43:37 -070031 * <p>
Jeff Hamiltonf3ca9a52010-05-12 15:04:33 -070032 * Cursor implementations are not required to be synchronized so code using a Cursor from multiple
33 * threads should perform its own synchronization when using the Cursor.
Jeff Brown511cd352013-08-23 17:43:37 -070034 * </p><p>
35 * Implementations should subclass {@link AbstractCursor}.
36 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 */
Jeff Brown03bd3022012-03-06 13:48:56 -080038public interface Cursor extends Closeable {
Vasu Nori8b0dd7d2010-05-18 11:54:31 -070039 /*
40 * Values returned by {@link #getType(int)}.
41 * These should be consistent with the corresponding types defined in CursorWindow.h
42 */
43 /** Value returned by {@link #getType(int)} if the specified column is null */
44 static final int FIELD_TYPE_NULL = 0;
45
46 /** Value returned by {@link #getType(int)} if the specified column type is integer */
47 static final int FIELD_TYPE_INTEGER = 1;
48
49 /** Value returned by {@link #getType(int)} if the specified column type is float */
50 static final int FIELD_TYPE_FLOAT = 2;
51
52 /** Value returned by {@link #getType(int)} if the specified column type is string */
53 static final int FIELD_TYPE_STRING = 3;
54
55 /** Value returned by {@link #getType(int)} if the specified column type is blob */
56 static final int FIELD_TYPE_BLOB = 4;
57
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 /**
59 * Returns the numbers of rows in the cursor.
60 *
61 * @return the number of rows in the cursor.
62 */
63 int getCount();
64
65 /**
66 * Returns the current position of the cursor in the row set.
67 * The value is zero-based. When the row set is first returned the cursor
68 * will be at positon -1, which is before the first row. After the
69 * last row is returned another call to next() will leave the cursor past
70 * the last entry, at a position of count().
71 *
72 * @return the current cursor position.
73 */
74 int getPosition();
75
76 /**
77 * Move the cursor by a relative amount, forward or backward, from the
78 * current position. Positive offsets move forwards, negative offsets move
79 * backwards. If the final position is outside of the bounds of the result
80 * set then the resultant position will be pinned to -1 or count() depending
81 * on whether the value is off the front or end of the set, respectively.
82 *
83 * <p>This method will return true if the requested destination was
84 * reachable, otherwise, it returns false. For example, if the cursor is at
85 * currently on the second entry in the result set and move(-5) is called,
86 * the position will be pinned at -1, and false will be returned.
87 *
88 * @param offset the offset to be applied from the current position.
89 * @return whether the requested move fully succeeded.
90 */
91 boolean move(int offset);
92
93 /**
94 * Move the cursor to an absolute position. The valid
95 * range of values is -1 &lt;= position &lt;= count.
96 *
97 * <p>This method will return true if the request destination was reachable,
98 * otherwise, it returns false.
99 *
100 * @param position the zero-based position to move to.
101 * @return whether the requested move fully succeeded.
102 */
103 boolean moveToPosition(int position);
104
105 /**
106 * Move the cursor to the first row.
107 *
108 * <p>This method will return false if the cursor is empty.
109 *
110 * @return whether the move succeeded.
111 */
112 boolean moveToFirst();
113
114 /**
115 * Move the cursor to the last row.
116 *
117 * <p>This method will return false if the cursor is empty.
118 *
119 * @return whether the move succeeded.
120 */
121 boolean moveToLast();
122
123 /**
124 * Move the cursor to the next row.
125 *
126 * <p>This method will return false if the cursor is already past the
127 * last entry in the result set.
128 *
129 * @return whether the move succeeded.
130 */
131 boolean moveToNext();
132
133 /**
134 * Move the cursor to the previous row.
135 *
136 * <p>This method will return false if the cursor is already before the
137 * first entry in the result set.
138 *
139 * @return whether the move succeeded.
140 */
141 boolean moveToPrevious();
142
143 /**
144 * Returns whether the cursor is pointing to the first row.
145 *
146 * @return whether the cursor is pointing at the first entry.
147 */
148 boolean isFirst();
149
150 /**
151 * Returns whether the cursor is pointing to the last row.
152 *
153 * @return whether the cursor is pointing at the last entry.
154 */
155 boolean isLast();
156
157 /**
158 * Returns whether the cursor is pointing to the position before the first
159 * row.
160 *
161 * @return whether the cursor is before the first result.
162 */
163 boolean isBeforeFirst();
164
165 /**
166 * Returns whether the cursor is pointing to the position after the last
167 * row.
168 *
169 * @return whether the cursor is after the last result.
170 */
171 boolean isAfterLast();
172
173 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 * Returns the zero-based index for the given column name, or -1 if the column doesn't exist.
175 * If you expect the column to exist use {@link #getColumnIndexOrThrow(String)} instead, which
176 * will make the error more clear.
177 *
178 * @param columnName the name of the target column.
179 * @return the zero-based column index for the given column name, or -1 if
180 * the column name does not exist.
181 * @see #getColumnIndexOrThrow(String)
182 */
183 int getColumnIndex(String columnName);
184
185 /**
186 * Returns the zero-based index for the given column name, or throws
187 * {@link IllegalArgumentException} if the column doesn't exist. If you're not sure if
188 * a column will exist or not use {@link #getColumnIndex(String)} and check for -1, which
189 * is more efficient than catching the exceptions.
190 *
191 * @param columnName the name of the target column.
192 * @return the zero-based column index for the given column name
193 * @see #getColumnIndex(String)
194 * @throws IllegalArgumentException if the column does not exist
195 */
196 int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException;
197
198 /**
199 * Returns the column name at the given zero-based column index.
200 *
201 * @param columnIndex the zero-based index of the target column.
202 * @return the column name for the given column index.
203 */
204 String getColumnName(int columnIndex);
205
206 /**
207 * Returns a string array holding the names of all of the columns in the
208 * result set in the order in which they were listed in the result.
209 *
210 * @return the names of the columns returned in this query.
211 */
212 String[] getColumnNames();
213
214 /**
215 * Return total number of columns
216 * @return number of columns
217 */
218 int getColumnCount();
219
220 /**
221 * Returns the value of the requested column as a byte array.
222 *
Daniel Trebbien65068b092010-10-31 12:19:07 -0700223 * <p>The result and whether this method throws an exception when the
224 * column value is null or the column type is not a blob type is
225 * implementation-defined.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 *
227 * @param columnIndex the zero-based index of the target column.
228 * @return the value of that column as a byte array.
229 */
230 byte[] getBlob(int columnIndex);
231
232 /**
233 * Returns the value of the requested column as a String.
234 *
Daniel Trebbien65068b092010-10-31 12:19:07 -0700235 * <p>The result and whether this method throws an exception when the
236 * column value is null or the column type is not a string type is
237 * implementation-defined.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 *
239 * @param columnIndex the zero-based index of the target column.
240 * @return the value of that column as a String.
241 */
242 String getString(int columnIndex);
243
244 /**
245 * Retrieves the requested column text and stores it in the buffer provided.
246 * If the buffer size is not sufficient, a new char buffer will be allocated
247 * and assigned to CharArrayBuffer.data
248 * @param columnIndex the zero-based index of the target column.
249 * if the target column is null, return buffer
250 * @param buffer the buffer to copy the text into.
251 */
252 void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer);
253
254 /**
255 * Returns the value of the requested column as a short.
256 *
Daniel Trebbien65068b092010-10-31 12:19:07 -0700257 * <p>The result and whether this method throws an exception when the
258 * column value is null, the column type is not an integral type, or the
259 * integer value is outside the range [<code>Short.MIN_VALUE</code>,
260 * <code>Short.MAX_VALUE</code>] is implementation-defined.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 *
262 * @param columnIndex the zero-based index of the target column.
263 * @return the value of that column as a short.
264 */
265 short getShort(int columnIndex);
266
267 /**
268 * Returns the value of the requested column as an int.
269 *
Daniel Trebbien65068b092010-10-31 12:19:07 -0700270 * <p>The result and whether this method throws an exception when the
271 * column value is null, the column type is not an integral type, or the
272 * integer value is outside the range [<code>Integer.MIN_VALUE</code>,
273 * <code>Integer.MAX_VALUE</code>] is implementation-defined.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 *
275 * @param columnIndex the zero-based index of the target column.
276 * @return the value of that column as an int.
277 */
278 int getInt(int columnIndex);
279
280 /**
281 * Returns the value of the requested column as a long.
282 *
Daniel Trebbien65068b092010-10-31 12:19:07 -0700283 * <p>The result and whether this method throws an exception when the
284 * column value is null, the column type is not an integral type, or the
285 * integer value is outside the range [<code>Long.MIN_VALUE</code>,
286 * <code>Long.MAX_VALUE</code>] is implementation-defined.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 *
288 * @param columnIndex the zero-based index of the target column.
289 * @return the value of that column as a long.
290 */
291 long getLong(int columnIndex);
292
293 /**
294 * Returns the value of the requested column as a float.
295 *
Daniel Trebbien65068b092010-10-31 12:19:07 -0700296 * <p>The result and whether this method throws an exception when the
297 * column value is null, the column type is not a floating-point type, or the
298 * floating-point value is not representable as a <code>float</code> value is
299 * implementation-defined.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 *
301 * @param columnIndex the zero-based index of the target column.
302 * @return the value of that column as a float.
303 */
304 float getFloat(int columnIndex);
305
306 /**
307 * Returns the value of the requested column as a double.
308 *
Daniel Trebbien65068b092010-10-31 12:19:07 -0700309 * <p>The result and whether this method throws an exception when the
310 * column value is null, the column type is not a floating-point type, or the
311 * floating-point value is not representable as a <code>double</code> value is
312 * implementation-defined.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 *
314 * @param columnIndex the zero-based index of the target column.
315 * @return the value of that column as a double.
316 */
317 double getDouble(int columnIndex);
318
319 /**
Vasu Nori8b0dd7d2010-05-18 11:54:31 -0700320 * Returns data type of the given column's value.
321 * The preferred type of the column is returned but the data may be converted to other types
322 * as documented in the get-type methods such as {@link #getInt(int)}, {@link #getFloat(int)}
323 * etc.
324 *<p>
325 * Returned column types are
326 * <ul>
327 * <li>{@link #FIELD_TYPE_NULL}</li>
328 * <li>{@link #FIELD_TYPE_INTEGER}</li>
329 * <li>{@link #FIELD_TYPE_FLOAT}</li>
330 * <li>{@link #FIELD_TYPE_STRING}</li>
331 * <li>{@link #FIELD_TYPE_BLOB}</li>
332 *</ul>
333 *</p>
334 *
335 * @param columnIndex the zero-based index of the target column.
336 * @return column value type
337 */
338 int getType(int columnIndex);
339
340 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 * Returns <code>true</code> if the value in the indicated column is null.
342 *
343 * @param columnIndex the zero-based index of the target column.
344 * @return whether the column value is null.
345 */
346 boolean isNull(int columnIndex);
347
348 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * Deactivates the Cursor, making all calls on it fail until {@link #requery} is called.
350 * Inactive Cursors use fewer resources than active Cursors.
351 * Calling {@link #requery} will make the cursor active again.
Dianne Hackborn81e92762011-10-09 16:00:21 -0700352 * @deprecated Since {@link #requery()} is deprecated, so too is this.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700354 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 void deactivate();
356
357 /**
358 * Performs the query that created the cursor again, refreshing its
359 * contents. This may be done at any time, including after a call to {@link
360 * #deactivate}.
361 *
Vasu Nori20f549f2010-04-15 11:25:51 -0700362 * Since this method could execute a query on the database and potentially take
363 * a while, it could cause ANR if it is called on Main (UI) thread.
364 * A warning is printed if this method is being executed on Main thread.
365 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 * @return true if the requery succeeded, false if not, in which case the
367 * cursor becomes invalid.
Vasu Nori820e9b62010-11-01 18:18:19 -0700368 * @deprecated Don't use this. Just request a new cursor, so you can do this
369 * asynchronously and update your list view once the new cursor comes back.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 */
Vasu Nori820e9b62010-11-01 18:18:19 -0700371 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 boolean requery();
373
374 /**
375 * Closes the Cursor, releasing all of its resources and making it completely invalid.
376 * Unlike {@link #deactivate()} a call to {@link #requery()} will not make the Cursor valid
377 * again.
378 */
379 void close();
380
381 /**
382 * return true if the cursor is closed
383 * @return true if the cursor is closed.
384 */
385 boolean isClosed();
386
387 /**
388 * Register an observer that is called when changes happen to the content backing this cursor.
389 * Typically the data set won't change until {@link #requery()} is called.
390 *
391 * @param observer the object that gets notified when the content backing the cursor changes.
392 * @see #unregisterContentObserver(ContentObserver)
393 */
394 void registerContentObserver(ContentObserver observer);
395
396 /**
397 * Unregister an observer that has previously been registered with this
398 * cursor via {@link #registerContentObserver}.
399 *
400 * @param observer the object to unregister.
401 * @see #registerContentObserver(ContentObserver)
402 */
403 void unregisterContentObserver(ContentObserver observer);
404
405 /**
406 * Register an observer that is called when changes happen to the contents
407 * of the this cursors data set, for example, when the data set is changed via
408 * {@link #requery()}, {@link #deactivate()}, or {@link #close()}.
409 *
410 * @param observer the object that gets notified when the cursors data set changes.
411 * @see #unregisterDataSetObserver(DataSetObserver)
412 */
413 void registerDataSetObserver(DataSetObserver observer);
414
415 /**
416 * Unregister an observer that has previously been registered with this
417 * cursor via {@link #registerContentObserver}.
418 *
419 * @param observer the object to unregister.
420 * @see #registerDataSetObserver(DataSetObserver)
421 */
422 void unregisterDataSetObserver(DataSetObserver observer);
423
424 /**
425 * Register to watch a content URI for changes. This can be the URI of a specific data row (for
426 * example, "content://my_provider_type/23"), or a a generic URI for a content type.
Sudheer Shankaa07b0522019-01-26 10:40:42 -0800427 *
428 * <p>Calling this overrides any previous call to
429 * {@link #setNotificationUris(ContentResolver, List)}.
430 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 * @param cr The content resolver from the caller's context. The listener attached to
432 * this resolver will be notified.
433 * @param uri The content URI to watch.
434 */
435 void setNotificationUri(ContentResolver cr, Uri uri);
436
437 /**
Sudheer Shankaa07b0522019-01-26 10:40:42 -0800438 * Similar to {@link #setNotificationUri(ContentResolver, Uri)}, except this version allows
439 * to watch multiple content URIs for changes.
440 *
441 * <p>If this is not implemented, this is equivalent to calling
442 * {@link #setNotificationUri(ContentResolver, Uri)} with the first URI in {@code uris}.
443 *
444 * <p>Calling this overrides any previous call to
445 * {@link #setNotificationUri(ContentResolver, Uri)}.
446 *
447 * @param cr The content resolver from the caller's context. The listener attached to
448 * this resolver will be notified.
449 * @param uris The content URIs to watch.
450 */
451 default void setNotificationUris(@NonNull ContentResolver cr, @NonNull List<Uri> uris) {
452 setNotificationUri(cr, uris.get(0));
453 }
454
455 /**
Dianne Hackbornc87c92e2013-05-14 13:44:29 -0700456 * Return the URI at which notifications of changes in this Cursor's data
457 * will be delivered, as previously set by {@link #setNotificationUri}.
458 * @return Returns a URI that can be used with
459 * {@link ContentResolver#registerContentObserver(android.net.Uri, boolean, ContentObserver)
460 * ContentResolver.registerContentObserver} to find out about changes to this Cursor's
461 * data. May be null if no notification URI has been set.
462 */
463 Uri getNotificationUri();
464
465 /**
Sudheer Shankaa07b0522019-01-26 10:40:42 -0800466 * Return the URIs at which notifications of changes in this Cursor's data
467 * will be delivered, as previously set by {@link #setNotificationUris}.
468 *
469 * <p>If this is not implemented, this is equivalent to calling {@link #getNotificationUri()}.
470 *
471 * @return Returns URIs that can be used with
472 * {@link ContentResolver#registerContentObserver(android.net.Uri, boolean, ContentObserver)
473 * ContentResolver.registerContentObserver} to find out about changes to this Cursor's
474 * data. May be null if no notification URI has been set.
475 */
476 default List<Uri> getNotificationUris() {
477 final Uri notifyUri = getNotificationUri();
478 return notifyUri == null ? null : Arrays.asList(notifyUri);
479 }
480
481 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 * onMove() will only be called across processes if this method returns true.
483 * @return whether all cursor movement should result in a call to onMove().
484 */
485 boolean getWantsAllOnMoveCalls();
486
487 /**
Jeff Brown4e0951e2015-04-09 18:28:24 -0700488 * Sets a {@link Bundle} that will be returned by {@link #getExtras()}.
489 *
490 * @param extras {@link Bundle} to set, or null to set an empty bundle.
491 */
492 void setExtras(Bundle extras);
493
494 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 * Returns a bundle of extra values. This is an optional way for cursors to provide out-of-band
496 * metadata to their users. One use of this is for reporting on the progress of network requests
497 * that are required to fetch data for the cursor.
498 *
499 * <p>These values may only change when requery is called.
Daniel Trebbien65068b092010-10-31 12:19:07 -0700500 * @return cursor-defined values, or {@link android.os.Bundle#EMPTY Bundle.EMPTY} if there
501 * are no values. Never <code>null</code>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 */
503 Bundle getExtras();
504
505 /**
506 * This is an out-of-band way for the the user of a cursor to communicate with the cursor. The
507 * structure of each bundle is entirely defined by the cursor.
508 *
509 * <p>One use of this is to tell a cursor that it should retry its network request after it
510 * reported an error.
Daniel Trebbien65068b092010-10-31 12:19:07 -0700511 * @param extras extra values, or {@link android.os.Bundle#EMPTY Bundle.EMPTY}.
512 * Never <code>null</code>.
513 * @return extra values, or {@link android.os.Bundle#EMPTY Bundle.EMPTY}.
514 * Never <code>null</code>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 */
516 Bundle respond(Bundle extras);
517}