Java Code Examples for net.sqlcipher.Cursor#moveToFirst()

The following examples show how to use net.sqlcipher.Cursor#moveToFirst() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IndexedFixturePathUtils.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
public static IndexedFixtureIdentifier lookupIndexedFixturePaths(SQLiteDatabase db,
                                                                 String fixtureName) {
    Cursor c = db.query(INDEXED_FIXTURE_PATHS_TABLE,
            new String[]{INDEXED_FIXTURE_PATHS_COL_BASE, INDEXED_FIXTURE_PATHS_COL_CHILD, INDEXED_FIXTURE_PATHS_COL_ATTRIBUTES},
            INDEXED_FIXTURE_PATHS_COL_NAME + "=?", new String[]{fixtureName}, null, null, null);
    try {
        if (c.getCount() == 0) {
            return null;
        } else {
            c.moveToFirst();
            return new IndexedFixtureIdentifier(
                    c.getString(c.getColumnIndexOrThrow(INDEXED_FIXTURE_PATHS_COL_BASE)),
                    c.getString(c.getColumnIndexOrThrow(INDEXED_FIXTURE_PATHS_COL_CHILD)),
                    c.getBlob(c.getColumnIndexOrThrow(INDEXED_FIXTURE_PATHS_COL_ATTRIBUTES)));
        }
    } finally {
        c.close();
    }
}
 
Example 2
Source File: IndexedFixturePathUtils.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
public static List<String> getAllIndexedFixtureNames(SQLiteDatabase db) {
    Cursor c = db.query(INDEXED_FIXTURE_PATHS_TABLE,
            new String[]{INDEXED_FIXTURE_PATHS_COL_NAME},
            null, null, null, null, null);
    List<String> fixtureNames = new ArrayList<>();
    try {
        if (c.moveToFirst()) {
            int desiredColumnIndex = c.getColumnIndexOrThrow(
                    INDEXED_FIXTURE_PATHS_COL_NAME);
            while (!c.isAfterLast()) {
                String name = c.getString(desiredColumnIndex);
                fixtureNames.add(name);
                c.moveToNext();
            }
        }
        return fixtureNames;
    } finally {
        if (c != null) {
            c.close();
        }
    }
}
 
Example 3
Source File: HybridFileBackedSqlHelpers.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
protected static Pair<String, byte[]> getEntryFilenameAndKey(AndroidDbHelper helper,
                                                             String table,
                                                             int id) {
    Cursor c;
    c = helper.getHandle().query(table, HybridFileBackedSqlStorage.dataColumns,
            DatabaseHelper.ID_COL + "=?",
            new String[]{String.valueOf(id)}, null, null, null);

    try {
        c.moveToFirst();
        return new Pair<>(c.getString(c.getColumnIndexOrThrow(DatabaseHelper.FILE_COL)),
                c.getBlob(c.getColumnIndexOrThrow(DatabaseHelper.AES_COL)));
    } finally {
        if (c != null) {
            c.close();
        }
    }
}
 
Example 4
Source File: HybridFileBackedSqlHelpers.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
protected static String getEntryFilename(AndroidDbHelper helper,
                                         String table, int id) {
    Cursor c;
    SQLiteDatabase db = helper.getHandle();

    String[] columns = new String[]{DatabaseHelper.FILE_COL};
    c = db.query(table, columns, DatabaseHelper.ID_COL + "=?",
            new String[]{String.valueOf(id)}, null, null, null);

    try {
        c.moveToFirst();
        return c.getString(c.getColumnIndexOrThrow(DatabaseHelper.FILE_COL));
    } finally {
        if (c != null) {
            c.close();
        }
    }
}
 
Example 5
Source File: AndroidCaseIndexTable.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
public HashMap<Integer,Vector<Pair<String, String>>> getCaseIndexMap() {
    String[] projection = new String[] {COL_CASE_RECORD_ID, COL_INDEX_TARGET, COL_INDEX_RELATIONSHIP};
    HashMap<Integer,Vector<Pair<String, String>>> caseIndexMap = new HashMap<>();
    Cursor c = db.query(TABLE_NAME, projection, null ,null, null, null, null);

    int recordColumn = c.getColumnIndexOrThrow(COL_CASE_RECORD_ID);
    int targetColumn = c.getColumnIndexOrThrow(COL_INDEX_TARGET);
    int relationshipColumn = c.getColumnIndexOrThrow(COL_INDEX_RELATIONSHIP);

    try {
        c.moveToFirst();
        while (!c.isAfterLast()) {
            int caseRecordId = c.getInt(recordColumn);
            String targetCase = c.getString(targetColumn);
            String relationship = c.getString(relationshipColumn);

            c.moveToNext();

            Pair<String, String> index  = new Pair<> (targetCase, relationship);

            Vector<Pair<String, String>> indexList;
            if (!caseIndexMap.containsKey(caseRecordId)) {
                indexList = new Vector<>();
            } else {
                indexList = caseIndexMap.get(caseRecordId);
            }
            indexList.add(index);
            caseIndexMap.put(caseRecordId, indexList);
        }

        return caseIndexMap;
    } finally {
        c.close();
    }
}
 
Example 6
Source File: AndroidCaseIndexTable.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
public int loadIntoIndexTable(HashMap<String, Vector<Integer>> indexCache, String indexName) {
    int resultsReturned = 0;
    String[] args = new String[]{indexName};
    if (SqlStorage.STORAGE_OUTPUT_DEBUG) {
        String query = String.format("SELECT %s,%s %s FROM %s where %s = '%s'", COL_CASE_RECORD_ID, COL_INDEX_NAME, COL_INDEX_TARGET, TABLE_NAME, COL_INDEX_NAME, indexName);
        DbUtil.explainSql(db, query, null);
    }

    Cursor c = db.query(TABLE_NAME, new String[]{COL_CASE_RECORD_ID, COL_INDEX_NAME, COL_INDEX_TARGET},COL_INDEX_NAME + " = ?", args, null, null, null);

    try {
        if (c.moveToFirst()) {
            while (!c.isAfterLast()) {
                resultsReturned++;
                int id = c.getInt(c.getColumnIndexOrThrow(COL_CASE_RECORD_ID));
                String target = c.getString(c.getColumnIndexOrThrow(COL_INDEX_TARGET));

                String cacheID = indexName + "|" + target;
                Vector<Integer> cache;
                if (indexCache.containsKey(cacheID)){
                    cache = indexCache.get(cacheID);
                } else {
                    cache = new Vector<>();
                }
                cache.add(id);
                indexCache.put(cacheID, cache);
                c.moveToNext();
            }
        }

        return resultsReturned;
    } finally {
        if (c != null) {
            c.close();
        }
    }

}
 
Example 7
Source File: HybridFileBackedSqlHelpers.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
/**
 * Remove files in the orphaned file table. Files are added to this table
 * when file-backed db transactions fail, leaving the file on the
 * filesystem.
 *
 * Order of operations expects filenames to be globally unique.
 */
public static void removeOrphanedFiles(SQLiteDatabase db) {
    Cursor cur = db.query(DbUtil.orphanFileTableName, new String[]{DatabaseHelper.FILE_COL}, null, null, null, null, null);
    ArrayList<String> files = new ArrayList<>();
    try {
        if (cur.getCount() > 0) {
            cur.moveToFirst();
            int fileColIndex = cur.getColumnIndexOrThrow(DatabaseHelper.FILE_COL);
            while (!cur.isAfterLast()) {
                files.add(cur.getString(fileColIndex));
                cur.moveToNext();
            }
        }
    } finally {
        if (cur != null) {
            cur.close();
        }
    }

    removeFiles(files);

    db.beginTransaction();
    try {
        db.delete(DbUtil.orphanFileTableName, null, null);
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}