List of usage examples for android.database Cursor getType
int getType(int columnIndex);
From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java
@Test public void testUpdateDataInExistingLocalTableWithIdWhenIdAlreadyExists_ExpectPass() throws ActionNotAuthorizedException { String tableId = localTestTable; ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId, activeUser, RoleConsts.USER_ROLES_LIST); String testCol = "testColumn"; String testColType = ElementDataType.integer.name(); String testStrCol = "testStrColumn"; String testStrColType = ElementDataType.string.name(); List<Column> columns = new ArrayList<Column>(); columns.add(new Column(testCol, testCol, testColType, "[]")); columns.add(new Column(testStrCol, testStrCol, testStrColType, "[]")); OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createLocalOnlyTableWithColumns(db, tableId, columns);/*w w w . j a v a 2 s .co m*/ int testVal = 5; String testStrVal = "five"; boolean thrown = false; ContentValues cvValues = new ContentValues(); cvValues.put(testCol, testVal); cvValues.put(testStrCol, testStrVal); ODKDatabaseImplUtils.get().insertLocalOnlyRow(db, tableId, cvValues); // Select everything out of the table String sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?"; Object[] selArgs = new Object[1]; selArgs[0] = testVal; Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext); assertEquals(cursor.getCount(), 1); int val = 0; String valStr = null; while (cursor.moveToNext()) { { int ind = cursor.getColumnIndex(testCol); int type = cursor.getType(ind); assertEquals(type, Cursor.FIELD_TYPE_INTEGER); val = cursor.getInt(ind); } { int indStr = cursor.getColumnIndex(testStrCol); int typeStr = cursor.getType(indStr); assertEquals(typeStr, Cursor.FIELD_TYPE_STRING); valStr = cursor.getString(indStr); } } assertEquals(val, testVal); assertEquals(valStr, testStrVal); // Try updating that row in the database int testVal2 = 25; String testStrVal2 = "twenty-five"; ContentValues cvValues2 = new ContentValues(); cvValues2.put(testCol, testVal2); cvValues2.put(testStrCol, testStrVal2); ODKDatabaseImplUtils.get().updateLocalOnlyRow(db, tableId, cvValues2, testCol + "= ?", selArgs); // Select everything out of the table String sel2 = "SELECT * FROM " + tableId; Object[] selArgs2 = new Object[0]; Cursor cursor2 = ODKDatabaseImplUtils.get().rawQuery(db, sel2, selArgs2, null, accessContext); assertEquals(cursor2.getCount(), 1); int val2 = 0; String valStr2 = null; while (cursor2.moveToNext()) { { int ind = cursor2.getColumnIndex(testCol); int type = cursor2.getType(ind); assertEquals(type, Cursor.FIELD_TYPE_INTEGER); val2 = cursor2.getInt(ind); } { int indStr = cursor2.getColumnIndex(testStrCol); int typeStr = cursor2.getType(indStr); assertEquals(typeStr, Cursor.FIELD_TYPE_STRING); valStr2 = cursor2.getString(indStr); } } assertEquals(val2, testVal2); assertEquals(valStr2, testStrVal2); // Drop the table now that the test is done ODKDatabaseImplUtils.get().deleteLocalOnlyTable(db, tableId); }
From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java
@Test public void testWriteDataIntoExistingTableWithIdWhenIdAlreadyExists_ExpectPass() throws ActionNotAuthorizedException { String tableId = testTable;/* w w w . j a v a2 s . co m*/ String testCol = "testColumn"; String testColType = ElementDataType.integer.name(); List<Column> columns = new ArrayList<Column>(); columns.add(new Column(testCol, testCol, testColType, "[]")); OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId, columns); ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId, activeUser, RoleConsts.ADMIN_ROLES_LIST); int testVal = 5; boolean thrown = false; ContentValues cvValues = new ContentValues(); cvValues.put(testCol, testVal); String uuid = UUID.randomUUID().toString(); ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, uuid, activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale); // Select everything out of the table String sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?"; String[] selArgs = { "" + testVal }; Cursor cursor = null; try { cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext); assertEquals(cursor.getCount(), 1); int val = 0; while (cursor.moveToNext()) { int ind = cursor.getColumnIndex(testCol); int type = cursor.getType(ind); assertEquals(type, Cursor.FIELD_TYPE_INTEGER); val = cursor.getInt(ind); } assertEquals(val, testVal); } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } } // Try updating that row in the database int testVal2 = 25; ContentValues cvValues2 = new ContentValues(); cvValues2.put(testCol, testVal2); try { ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues2, uuid, activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale); } catch (ActionNotAuthorizedException ex) { throw ex; } catch (IllegalArgumentException e) { thrown = true; e.printStackTrace(); } assertEquals(thrown, true); /** * NOTE: we expect the log to report a failure to close this cursor. * It is GC'd and closed in its finalizer. This is confirming that * the finalizer is doing the right thing. */ // Select everything out of the table String sel2 = "SELECT * FROM " + tableId; String[] selArgs2 = {}; Cursor cursor2 = ODKDatabaseImplUtils.get().rawQuery(db, sel2, selArgs2, null, accessContext); assertEquals(cursor2.getCount(), 1); int val2 = 0; while (cursor2.moveToNext()) { int ind = cursor2.getColumnIndex(testCol); int type = cursor2.getType(ind); assertEquals(type, Cursor.FIELD_TYPE_INTEGER); val2 = cursor2.getInt(ind); } assertEquals(val2, testVal); // Drop the table now that the test is done ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId); }
From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java
@Test public void testQueryDistinct_ExpectPass() throws ActionNotAuthorizedException { String tableId = testTable;/*from w w w .ja v a2s.co m*/ String testCol = "testColumn"; String testColType = ElementDataType.integer.name(); List<Column> columns = new ArrayList<Column>(); columns.add(new Column(testCol, testCol, testColType, "[]")); OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId, columns); ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId, activeUser, RoleConsts.ADMIN_ROLES_LIST); int testVal = 5; boolean thrown = false; ContentValues cvValues = new ContentValues(); cvValues.put(testCol, testVal); String uuid = UUID.randomUUID().toString(); ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, uuid, activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale); // Select everything out of the table String sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?"; String[] selArgs = { "" + testVal }; Cursor cursor = null; try { cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext); assertEquals(cursor.getCount(), 1); int val = 0; while (cursor.moveToNext()) { int ind = cursor.getColumnIndex(testCol); int type = cursor.getType(ind); assertEquals(type, Cursor.FIELD_TYPE_INTEGER); val = cursor.getInt(ind); } assertEquals(val, testVal); } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } } // Add another row in the database with the same value String uuid2 = UUID.randomUUID().toString(); ContentValues cvValues2 = new ContentValues(); cvValues2.put(testCol, testVal); ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues2, uuid2, activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale); // Select everything out of the table String sel2 = "SELECT * FROM " + tableId; String[] selArgs2 = {}; Cursor cursor2 = ODKDatabaseImplUtils.get().rawQuery(db, sel2, selArgs2, null, accessContext); assertEquals(cursor2.getCount(), 2); System.out.println("testQueryDistinct_ExpectPass: after select * query"); OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface().dumpInfo(false); // Make sure the values are correct int val2 = 0; while (cursor2.moveToNext()) { int ind = cursor2.getColumnIndex(testCol); int type = cursor2.getType(ind); assertEquals(type, Cursor.FIELD_TYPE_INTEGER); val2 = cursor2.getInt(ind); assertEquals(val2, testVal); } // The moment of truth! test the queryDistinct // Get all of the rows of the database but only return testCol String[] retCols = { testCol }; Cursor cursor3 = ODKDatabaseImplUtils.get().queryDistinctForTest(db, tableId, retCols, null, null, null, null, null, null); assertEquals(cursor3.getCount(), 1); int val3 = 0; while (cursor3.moveToNext()) { int ind = cursor3.getColumnIndex(testCol); int type = cursor3.getType(ind); assertEquals(type, Cursor.FIELD_TYPE_INTEGER); val3 = cursor3.getInt(ind); } assertEquals(val3, testVal); // Drop the table now that the test is done ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId); }
From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java
@Test public void testQueryWithData_ExpectPass() { String tableId = testTable;/*from w w w .j a v a 2 s.com*/ List<Column> columns = new ArrayList<Column>(); columns.add(new Column("col1", "col1", "string", "[]")); OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId, columns); // Check that the user defined rows are in the table Cursor cursor = ODKDatabaseImplUtils.get().queryForTest(db, tableId, null, null, null, null, null, null, null); Cursor refCursor = db.query(tableId, null, null, null, null, null, null, null); if (cursor != null && refCursor != null) { int index = 0; while (cursor.moveToNext() && refCursor.moveToNext()) { int testType = cursor.getType(index); int refType = refCursor.getType(index); assertEquals(testType, refType); switch (refType) { case Cursor.FIELD_TYPE_BLOB: byte[] byteArray = cursor.getBlob(index); byte[] refByteArray = refCursor.getBlob(index); assertEquals(byteArray, refByteArray); break; case Cursor.FIELD_TYPE_FLOAT: float valueFloat = cursor.getFloat(index); float refValueFloat = refCursor.getFloat(index); assertEquals(valueFloat, refValueFloat, 0.0); break; case Cursor.FIELD_TYPE_INTEGER: int valueInt = cursor.getInt(index); int refValueInt = refCursor.getInt(index); assertEquals(valueInt, refValueInt); break; case Cursor.FIELD_TYPE_STRING: String valueStr = cursor.getString(index); String refValueStr = refCursor.getString(index); assertEquals(valueStr, refValueStr); break; case Cursor.FIELD_TYPE_NULL: default: break; } } } // Drop the table now that the test is done ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId); }
From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java
@Test public void testDeleteServerConflictRowWithIdAndLocDelOldVals_ExpectPass() throws ActionNotAuthorizedException { String tableId = testTable;/*from w w w . j av a 2 s . c o m*/ String testCol = "testColumn"; String testColType = ElementDataType.integer.name(); List<Column> columns = new ArrayList<Column>(); columns.add(new Column(testCol, testCol, testColType, "[]")); OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId, columns); ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId, activeUser, RoleConsts.ADMIN_ROLES_LIST); int testVal = 5; // local record that is synced and pending deletion... ContentValues cvValues = new ContentValues(); String rowId = LocalizationUtils.genUUID(); cvValues.put(testCol, testVal); cvValues.put(DataTableColumns.ROW_ETAG, LocalizationUtils.genUUID()); cvValues.put(DataTableColumns.SYNC_STATE, SyncState.deleted.name()); ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, rowId, activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale); // Select everything out of the table String sel = "SELECT * FROM " + tableId + " WHERE " + DataTableColumns.ID + " = ? ORDER BY " + DataTableColumns.CONFLICT_TYPE + " ASC"; String[] selArgs = { rowId }; Cursor cursor = null; int val = 0; try { cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext); assertEquals(cursor.getCount(), 1); while (cursor.moveToNext()) { int ind = cursor.getColumnIndex(testCol); int type = cursor.getType(ind); assertEquals(type, Cursor.FIELD_TYPE_INTEGER); val = cursor.getInt(ind); } } finally { cursor.close(); } assertEquals(val, testVal); // NOTE: all metadata fields need to be specified // server has a change... ContentValues updates = new ContentValues(); // data value updates.put(testCol, testVal + 6); // metadata fields updates.put(DataTableColumns.CONFLICT_TYPE, ConflictType.SERVER_UPDATED_UPDATED_VALUES); updates.put(DataTableColumns.SYNC_STATE, SyncState.in_conflict.name()); updates.put(DataTableColumns.ROW_ETAG, LocalizationUtils.genUUID()); // insert in_conflict server row updates.put(DataTableColumns.FORM_ID, "serverForm"); updates.put(DataTableColumns.LOCALE, currentLocale); updates.put(DataTableColumns.SAVEPOINT_TIMESTAMP, TableConstants.nanoSecondsFromMillis(System.currentTimeMillis())); updates.put(DataTableColumns.SAVEPOINT_TYPE, SavepointTypeManipulator.complete()); updates.put(DataTableColumns.SAVEPOINT_CREATOR, "mailto:server@gmail.com"); updates.put(DataTableColumns.DEFAULT_ACCESS, RowFilterScope.Access.FULL.name()); updates.put(DataTableColumns.ROW_OWNER, "mailto:server@gmail.com"); updates.putNull(DataTableColumns.GROUP_READ_ONLY); updates.putNull(DataTableColumns.GROUP_MODIFY); updates.putNull(DataTableColumns.GROUP_PRIVILEGED); // Place row in conflict int conflictType = ConflictType.LOCAL_DELETED_OLD_VALUES; ODKDatabaseImplUtils.get().privilegedPlaceRowIntoConflictWithId(db, tableId, orderedColumns, updates, rowId, conflictType, activeUser, currentLocale); // Run the query again and make sure that the place row in conflict worked as expected String whereClause = DataTableColumns.ID + "=?"; String[] selectionArgs = new String[] { rowId }; String[] orderByKeys = new String[] { DataTableColumns.CONFLICT_TYPE }; String[] orderByDirs = new String[] { "ASC" }; List<String> adminColumns = ODKDatabaseImplUtils.get().getAdminColumns(); String[] adminColArr = adminColumns.toArray(new String[adminColumns.size()]); BaseTable baseTable = ODKDatabaseImplUtils.get().query(db, tableId, QueryUtil.buildSqlStatement(tableId, whereClause, null, null, orderByKeys, orderByDirs), selectionArgs, null, accessContext); UserTable table = new UserTable(baseTable, orderedColumns, adminColArr); assertEquals(table.getNumberOfRows(), 2); Row first = table.getRowAtIndex(0); Row second = table.getRowAtIndex(1); String v; int conflictTypeVal; v = first.getDataByKey(DataTableColumns.CONFLICT_TYPE); assertNotNull(v); conflictTypeVal = Integer.valueOf(v); assertEquals(conflictType, conflictTypeVal); v = second.getDataByKey(DataTableColumns.CONFLICT_TYPE); assertNotNull(v); conflictTypeVal = Integer.valueOf(v); assertEquals(ConflictType.SERVER_UPDATED_UPDATED_VALUES, conflictTypeVal); // Now delete the row ODKDatabaseImplUtils.get().resolveServerConflictWithDeleteRowWithId(db, tableId, rowId, activeUser); // Run the query yet again to make sure that things worked as expected baseTable = ODKDatabaseImplUtils.get().query(db, tableId, QueryUtil.buildSqlStatement(tableId, whereClause, null, null, orderByKeys, orderByDirs), selectionArgs, null, accessContext); table = new UserTable(baseTable, orderedColumns, adminColArr); assertEquals(table.getNumberOfRows(), 0); // Drop the table now that the test is done ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId); }
From source file:org.opendatakit.utilities.test.AbstractODKDatabaseUtilsTest.java
public void testEnforceTypesTableMetadata_ExpectPass() { String tableId = testTable;// w w w . j av a 2 s. c om String testCol = "testColumn"; String testColType = ElementDataType.string.name(); String partition = KeyValueStoreConstants.PARTITION_TABLE; String aspect = KeyValueStoreConstants.ASPECT_DEFAULT; String key = KeyValueStoreConstants.COLUMN_DISPLAY_NAME; String type = ElementDataType.integer.name(); String kvsValue = tableId; List<Column> columns = new ArrayList<Column>(); columns.add(new Column(testCol, testCol, testColType, "[]")); List<KeyValueStoreEntry> kvsEntries = new ArrayList<KeyValueStoreEntry>(); KeyValueStoreEntry kvsEntry = KeyValueStoreUtils.buildEntry(tableId, partition, aspect, key, ElementDataType.valueOf(type), kvsValue); kvsEntries.add(kvsEntry); // createOrOpenTableWithColumnsAndProperties calls enforceTypesTableMetadata try { ODKDatabaseImplUtils.get().createOrOpenTableWithColumnsAndProperties(db, tableId, columns, kvsEntries, true); } catch (Exception e) { e.printStackTrace(); } ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId, activeUser, RoleConsts.ADMIN_ROLES_LIST); // Ensure that the expected properties is in the KVS table String sel = "SELECT * FROM " + DatabaseConstants.KEY_VALUE_STORE_ACTIVE_TABLE_NAME + " WHERE " + KeyValueStoreColumns.PARTITION + " = ? AND " + KeyValueStoreColumns.KEY + " = ? AND " + KeyValueStoreColumns.VALUE + " = ?"; String[] selArgs = { partition, key, kvsValue }; Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext); assertEquals(cursor.getCount(), 1); // Now make sure that the returned value is equal to the original value ArrayList<KeyValueStoreEntry> retKVSEntries = ODKDatabaseImplUtils.get() .getTableMetadata(db, tableId, partition, aspect, key).getEntries(); assertEquals(retKVSEntries.size(), kvsEntries.size()); assertNotSame(retKVSEntries.get(0).type, kvsEntries.get(0).type); assertEquals(retKVSEntries.get(0).type, ElementDataType.object.name()); // Now make sure that the table has the right value for displayName cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext); assertEquals(cursor.getCount(), 1); String val = null; while (cursor.moveToNext()) { int ind = cursor.getColumnIndex(KeyValueStoreColumns.VALUE_TYPE); int resultType = cursor.getType(ind); assertEquals(resultType, Cursor.FIELD_TYPE_STRING); val = cursor.getString(ind); } // In this case the type should have been changed to string assertEquals(val, ElementDataType.object.name()); // Now delete the metadata ODKDatabaseImplUtils.get().deleteTableMetadata(db, tableId, partition, aspect, key); // Drop the table now that the test is done ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId); }
From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java
@Test public void testEnforceTypesTableMetadata_ExpectPass() { String tableId = testTable;/* w ww . j a v a2 s. c om*/ String testCol = "testColumn"; String testColType = ElementDataType.string.name(); String partition = KeyValueStoreConstants.PARTITION_TABLE; String aspect = KeyValueStoreConstants.ASPECT_DEFAULT; String key = KeyValueStoreConstants.COLUMN_DISPLAY_NAME; String type = ElementDataType.integer.name(); String kvsValue = tableId; List<Column> columns = new ArrayList<Column>(); columns.add(new Column(testCol, testCol, testColType, "[]")); List<KeyValueStoreEntry> kvsEntries = new ArrayList<KeyValueStoreEntry>(); KeyValueStoreEntry kvsEntry = KeyValueStoreUtils.buildEntry(tableId, partition, aspect, key, ElementDataType.valueOf(type), kvsValue); kvsEntries.add(kvsEntry); // createOrOpenTableWithColumnsAndProperties calls enforceTypesTableMetadata try { ODKDatabaseImplUtils.get().createOrOpenTableWithColumnsAndProperties(db, tableId, columns, kvsEntries, true); } catch (Exception e) { e.printStackTrace(); } ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId, activeUser, RoleConsts.ADMIN_ROLES_LIST); // Ensure that the expected properties is in the KVS table String sel = "SELECT * FROM " + DatabaseConstants.KEY_VALUE_STORE_ACTIVE_TABLE_NAME + " WHERE " + KeyValueStoreColumns.PARTITION + " = ? AND " + KeyValueStoreColumns.KEY + " = ? AND " + KeyValueStoreColumns.VALUE + " = ?"; String[] selArgs = { partition, key, kvsValue }; Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext); assertEquals(cursor.getCount(), 1); // Now make sure that the returned value is equal to the original value ArrayList<KeyValueStoreEntry> retKVSEntries = ODKDatabaseImplUtils.get() .getTableMetadata(db, tableId, partition, aspect, key).getEntries(); assertEquals(retKVSEntries.size(), kvsEntries.size()); assertNotSame(retKVSEntries.get(0).type, kvsEntries.get(0).type); assertEquals(retKVSEntries.get(0).type, ElementDataType.object.name()); // Now make sure that the table has the right value for displayName cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext); assertEquals(cursor.getCount(), 1); String val = null; while (cursor.moveToNext()) { int ind = cursor.getColumnIndex(KeyValueStoreColumns.VALUE_TYPE); int resultType = cursor.getType(ind); assertEquals(resultType, Cursor.FIELD_TYPE_STRING); val = cursor.getString(ind); } // In this case the type should have been changed to string assertEquals(val, ElementDataType.object.name()); // Now delete the metadata ODKDatabaseImplUtils.get().deleteTableMetadata(db, tableId, partition, aspect, key); // Drop the table now that the test is done ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId); }