Skip to content

Commit d008510

Browse files
Jeff BrownAndroid (Google) Code Review
authored andcommitted
Merge "Make default implementation of fillWindow typesafe. Bug: 5218310"
2 parents a39a055 + 80e7b80 commit d008510

File tree

3 files changed

+103
-79
lines changed

3 files changed

+103
-79
lines changed

core/java/android/database/AbstractCursor.java

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ public abstract class AbstractCursor implements CrossProcessCursor {
5353
abstract public boolean isNull(int column);
5454

5555
public int getType(int column) {
56-
throw new UnsupportedOperationException();
56+
// Reflects the assumption that all commonly used field types (meaning everything
57+
// but blobs) are convertible to strings so it should be safe to call
58+
// getString to retrieve them.
59+
return FIELD_TYPE_STRING;
5760
}
5861

5962
// TODO implement getBlob in all cursor types
@@ -185,46 +188,9 @@ public final boolean moveToPosition(int position) {
185188
return result;
186189
}
187190

188-
/**
189-
* Copy data from cursor to CursorWindow
190-
* @param position start position of data
191-
* @param window
192-
*/
191+
@Override
193192
public void fillWindow(int position, CursorWindow window) {
194-
if (position < 0 || position >= getCount()) {
195-
return;
196-
}
197-
window.acquireReference();
198-
try {
199-
int oldpos = mPos;
200-
mPos = position - 1;
201-
window.clear();
202-
window.setStartPosition(position);
203-
int columnNum = getColumnCount();
204-
window.setNumColumns(columnNum);
205-
while (moveToNext() && window.allocRow()) {
206-
for (int i = 0; i < columnNum; i++) {
207-
String field = getString(i);
208-
if (field != null) {
209-
if (!window.putString(field, mPos, i)) {
210-
window.freeLastRow();
211-
break;
212-
}
213-
} else {
214-
if (!window.putNull(mPos, i)) {
215-
window.freeLastRow();
216-
break;
217-
}
218-
}
219-
}
220-
}
221-
222-
mPos = oldpos;
223-
} catch (IllegalStateException e){
224-
// simply ignore it
225-
} finally {
226-
window.releaseReference();
227-
}
193+
DatabaseUtils.cursorFillWindow(this, position, window);
228194
}
229195

230196
public final boolean move(int offset) {

core/java/android/database/CursorWindow.java

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,7 @@ public void freeLastRow(){
271271
* Returns true if the field at the specified row and column index
272272
* has type {@link Cursor#FIELD_TYPE_NULL}.
273273
*
274-
* @param row The zero-based row index, relative to the cursor window's
275-
* start position ({@link #getStartPosition()}).
274+
* @param row The zero-based row index.
276275
* @param column The zero-based column index.
277276
* @return True if the field has type {@link Cursor#FIELD_TYPE_NULL}.
278277
* @deprecated Use {@link #getType(int, int)} instead.
@@ -286,8 +285,7 @@ public boolean isNull(int row, int column) {
286285
* Returns true if the field at the specified row and column index
287286
* has type {@link Cursor#FIELD_TYPE_BLOB} or {@link Cursor#FIELD_TYPE_NULL}.
288287
*
289-
* @param row The zero-based row index, relative to the cursor window's
290-
* start position ({@link #getStartPosition()}).
288+
* @param row The zero-based row index.
291289
* @param column The zero-based column index.
292290
* @return True if the field has type {@link Cursor#FIELD_TYPE_BLOB} or
293291
* {@link Cursor#FIELD_TYPE_NULL}.
@@ -303,8 +301,7 @@ public boolean isBlob(int row, int column) {
303301
* Returns true if the field at the specified row and column index
304302
* has type {@link Cursor#FIELD_TYPE_INTEGER}.
305303
*
306-
* @param row The zero-based row index, relative to the cursor window's
307-
* start position ({@link #getStartPosition()}).
304+
* @param row The zero-based row index.
308305
* @param column The zero-based column index.
309306
* @return True if the field has type {@link Cursor#FIELD_TYPE_INTEGER}.
310307
* @deprecated Use {@link #getType(int, int)} instead.
@@ -318,8 +315,7 @@ public boolean isLong(int row, int column) {
318315
* Returns true if the field at the specified row and column index
319316
* has type {@link Cursor#FIELD_TYPE_FLOAT}.
320317
*
321-
* @param row The zero-based row index, relative to the cursor window's
322-
* start position ({@link #getStartPosition()}).
318+
* @param row The zero-based row index.
323319
* @param column The zero-based column index.
324320
* @return True if the field has type {@link Cursor#FIELD_TYPE_FLOAT}.
325321
* @deprecated Use {@link #getType(int, int)} instead.
@@ -333,8 +329,7 @@ public boolean isFloat(int row, int column) {
333329
* Returns true if the field at the specified row and column index
334330
* has type {@link Cursor#FIELD_TYPE_STRING} or {@link Cursor#FIELD_TYPE_NULL}.
335331
*
336-
* @param row The zero-based row index, relative to the cursor window's
337-
* start position ({@link #getStartPosition()}).
332+
* @param row The zero-based row index.
338333
* @param column The zero-based column index.
339334
* @return True if the field has type {@link Cursor#FIELD_TYPE_STRING}
340335
* or {@link Cursor#FIELD_TYPE_NULL}.
@@ -359,8 +354,7 @@ public boolean isString(int row, int column) {
359354
* </ul>
360355
* </p>
361356
*
362-
* @param row The zero-based row index, relative to the cursor window's
363-
* start position ({@link #getStartPosition()}).
357+
* @param row The zero-based row index.
364358
* @param column The zero-based column index.
365359
* @return The field type.
366360
*/
@@ -390,8 +384,7 @@ public int getType(int row, int column) {
390384
* </ul>
391385
* </p>
392386
*
393-
* @param row The zero-based row index, relative to the cursor window's
394-
* start position ({@link #getStartPosition()}).
387+
* @param row The zero-based row index.
395388
* @param column The zero-based column index.
396389
* @return The value of the field as a byte array.
397390
*/
@@ -426,8 +419,7 @@ public byte[] getBlob(int row, int column) {
426419
* </ul>
427420
* </p>
428421
*
429-
* @param row The zero-based row index, relative to the cursor window's
430-
* start position ({@link #getStartPosition()}).
422+
* @param row The zero-based row index.
431423
* @param column The zero-based column index.
432424
* @return The value of the field as a string.
433425
*/
@@ -465,8 +457,7 @@ public String getString(int row, int column) {
465457
* </ul>
466458
* </p>
467459
*
468-
* @param row The zero-based row index, relative to the cursor window's
469-
* start position ({@link #getStartPosition()}).
460+
* @param row The zero-based row index.
470461
* @param column The zero-based column index.
471462
* @param buffer The {@link CharArrayBuffer} to hold the string. It is automatically
472463
* resized if the requested string is larger than the buffer's current capacity.
@@ -501,8 +492,7 @@ public void copyStringToBuffer(int row, int column, CharArrayBuffer buffer) {
501492
* </ul>
502493
* </p>
503494
*
504-
* @param row The zero-based row index, relative to the cursor window's
505-
* start position ({@link #getStartPosition()}).
495+
* @param row The zero-based row index.
506496
* @param column The zero-based column index.
507497
* @return The value of the field as a <code>long</code>.
508498
*/
@@ -534,8 +524,7 @@ public long getLong(int row, int column) {
534524
* </ul>
535525
* </p>
536526
*
537-
* @param row The zero-based row index, relative to the cursor window's
538-
* start position ({@link #getStartPosition()}).
527+
* @param row The zero-based row index.
539528
* @param column The zero-based column index.
540529
* @return The value of the field as a <code>double</code>.
541530
*/
@@ -556,8 +545,7 @@ public double getDouble(int row, int column) {
556545
* result to <code>short</code>.
557546
* </p>
558547
*
559-
* @param row The zero-based row index, relative to the cursor window's
560-
* start position ({@link #getStartPosition()}).
548+
* @param row The zero-based row index.
561549
* @param column The zero-based column index.
562550
* @return The value of the field as a <code>short</code>.
563551
*/
@@ -573,8 +561,7 @@ public short getShort(int row, int column) {
573561
* result to <code>int</code>.
574562
* </p>
575563
*
576-
* @param row The zero-based row index, relative to the cursor window's
577-
* start position ({@link #getStartPosition()}).
564+
* @param row The zero-based row index.
578565
* @param column The zero-based column index.
579566
* @return The value of the field as an <code>int</code>.
580567
*/
@@ -590,8 +577,7 @@ public int getInt(int row, int column) {
590577
* result to <code>float</code>.
591578
* </p>
592579
*
593-
* @param row The zero-based row index, relative to the cursor window's
594-
* start position ({@link #getStartPosition()}).
580+
* @param row The zero-based row index.
595581
* @param column The zero-based column index.
596582
* @return The value of the field as an <code>float</code>.
597583
*/
@@ -603,8 +589,7 @@ public float getFloat(int row, int column) {
603589
* Copies a byte array into the field at the specified row and column index.
604590
*
605591
* @param value The value to store.
606-
* @param row The zero-based row index, relative to the cursor window's
607-
* start position ({@link #getStartPosition()}).
592+
* @param row The zero-based row index.
608593
* @param column The zero-based column index.
609594
* @return True if successful.
610595
*/
@@ -621,8 +606,7 @@ public boolean putBlob(byte[] value, int row, int column) {
621606
* Copies a string into the field at the specified row and column index.
622607
*
623608
* @param value The value to store.
624-
* @param row The zero-based row index, relative to the cursor window's
625-
* start position ({@link #getStartPosition()}).
609+
* @param row The zero-based row index.
626610
* @param column The zero-based column index.
627611
* @return True if successful.
628612
*/
@@ -639,8 +623,7 @@ public boolean putString(String value, int row, int column) {
639623
* Puts a long integer into the field at the specified row and column index.
640624
*
641625
* @param value The value to store.
642-
* @param row The zero-based row index, relative to the cursor window's
643-
* start position ({@link #getStartPosition()}).
626+
* @param row The zero-based row index.
644627
* @param column The zero-based column index.
645628
* @return True if successful.
646629
*/
@@ -658,8 +641,7 @@ public boolean putLong(long value, int row, int column) {
658641
* specified row and column index.
659642
*
660643
* @param value The value to store.
661-
* @param row The zero-based row index, relative to the cursor window's
662-
* start position ({@link #getStartPosition()}).
644+
* @param row The zero-based row index.
663645
* @param column The zero-based column index.
664646
* @return True if successful.
665647
*/
@@ -675,8 +657,7 @@ public boolean putDouble(double value, int row, int column) {
675657
/**
676658
* Puts a null value into the field at the specified row and column index.
677659
*
678-
* @param row The zero-based row index, relative to the cursor window's
679-
* start position ({@link #getStartPosition()}).
660+
* @param row The zero-based row index.
680661
* @param column The zero-based column index.
681662
* @return True if successful.
682663
*/

core/java/android/database/DatabaseUtils.java

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,90 @@ public static int getTypeOfObject(Object obj) {
237237
return Cursor.FIELD_TYPE_BLOB;
238238
} else if (obj instanceof Float || obj instanceof Double) {
239239
return Cursor.FIELD_TYPE_FLOAT;
240-
} else if (obj instanceof Long || obj instanceof Integer) {
240+
} else if (obj instanceof Long || obj instanceof Integer
241+
|| obj instanceof Short || obj instanceof Byte) {
241242
return Cursor.FIELD_TYPE_INTEGER;
242243
} else {
243244
return Cursor.FIELD_TYPE_STRING;
244245
}
245246
}
246247

248+
/**
249+
* Fills the specified cursor window by iterating over the contents of the cursor.
250+
* The window is filled until the cursor is exhausted or the window runs out
251+
* of space.
252+
*
253+
* The original position of the cursor is left unchanged by this operation.
254+
*
255+
* @param cursor The cursor that contains the data to put in the window.
256+
* @param position The start position for filling the window.
257+
* @param window The window to fill.
258+
* @hide
259+
*/
260+
public static void cursorFillWindow(final Cursor cursor,
261+
int position, final CursorWindow window) {
262+
if (position < 0 || position >= cursor.getCount()) {
263+
return;
264+
}
265+
window.acquireReference();
266+
try {
267+
final int oldPos = cursor.getPosition();
268+
final int numColumns = cursor.getColumnCount();
269+
window.clear();
270+
window.setStartPosition(position);
271+
window.setNumColumns(numColumns);
272+
if (cursor.moveToPosition(position)) {
273+
do {
274+
if (!window.allocRow()) {
275+
break;
276+
}
277+
for (int i = 0; i < numColumns; i++) {
278+
final int type = cursor.getType(i);
279+
final boolean success;
280+
switch (type) {
281+
case Cursor.FIELD_TYPE_NULL:
282+
success = window.putNull(position, i);
283+
break;
284+
285+
case Cursor.FIELD_TYPE_INTEGER:
286+
success = window.putLong(cursor.getLong(i), position, i);
287+
break;
288+
289+
case Cursor.FIELD_TYPE_FLOAT:
290+
success = window.putDouble(cursor.getDouble(i), position, i);
291+
break;
292+
293+
case Cursor.FIELD_TYPE_BLOB: {
294+
final byte[] value = cursor.getBlob(i);
295+
success = value != null ? window.putBlob(value, position, i)
296+
: window.putNull(position, i);
297+
break;
298+
}
299+
300+
default: // assume value is convertible to String
301+
case Cursor.FIELD_TYPE_STRING: {
302+
final String value = cursor.getString(i);
303+
success = value != null ? window.putString(value, position, i)
304+
: window.putNull(position, i);
305+
break;
306+
}
307+
}
308+
if (!success) {
309+
window.freeLastRow();
310+
break;
311+
}
312+
}
313+
position += 1;
314+
} while (cursor.moveToNext());
315+
}
316+
cursor.moveToPosition(oldPos);
317+
} catch (IllegalStateException e){
318+
// simply ignore it
319+
} finally {
320+
window.releaseReference();
321+
}
322+
}
323+
247324
/**
248325
* Appends an SQL string to the given StringBuilder, including the opening
249326
* and closing single quotes. Any single quotes internal to sqlString will

0 commit comments

Comments
 (0)