Java Code Examples for net.sqlcipher.database.SQLiteDatabase#query()

The following examples show how to use net.sqlcipher.database.SQLiteDatabase#query() . 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
public Set<String> getAllPhoneNumbers() {
  SQLiteDatabase db      = databaseHelper.getReadableDatabase();
  Set<String>    results = new HashSet<>();

  try (Cursor cursor = db.query(TABLE_NAME, new String[] { PHONE }, null, null, null, null, null)) {
    while (cursor != null && cursor.moveToNext()) {
      String number = cursor.getString(cursor.getColumnIndexOrThrow(PHONE));

      if (!TextUtils.isEmpty(number)) {
        results.add(number);
      }
    }
  }

  return results;
}
 
Example 2
private List<RecipientSettings> getRecipientSettingsForSync(@Nullable String query, @Nullable String[] args) {
  SQLiteDatabase          db    = databaseHelper.getReadableDatabase();
  String                  table = TABLE_NAME + " LEFT OUTER JOIN " + IdentityDatabase.TABLE_NAME + " ON " + TABLE_NAME + "." + ID + " = " + IdentityDatabase.TABLE_NAME + "." + IdentityDatabase.RECIPIENT_ID
                                             + " LEFT OUTER JOIN " + GroupDatabase.TABLE_NAME + " ON " + TABLE_NAME + "." + GROUP_ID + " = " + GroupDatabase.TABLE_NAME + "." + GroupDatabase.GROUP_ID;
  List<RecipientSettings> out   = new ArrayList<>();

  String[] columns = ArrayUtils.concat(RECIPIENT_FULL_PROJECTION,
    new String[]{GroupDatabase.TABLE_NAME + "." + GroupDatabase.V2_MASTER_KEY });

  try (Cursor cursor = db.query(table, columns, query, args, null, null, null)) {
    while (cursor != null && cursor.moveToNext()) {
      out.add(getRecipientSettings(context, cursor));
    }
  }

  return out;
}
 
Example 3
public @NonNull List<SessionRow> getAll() {
  SQLiteDatabase   database = databaseHelper.getReadableDatabase();
  List<SessionRow> results  = new LinkedList<>();

  try (Cursor cursor = database.query(TABLE_NAME, null, null, null, null, null, null)) {
    while (cursor != null && cursor.moveToNext()) {
      try {
        results.add(new SessionRow(RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(RECIPIENT_ID))),
                                   cursor.getInt(cursor.getColumnIndexOrThrow(DEVICE)),
                                   new SessionRecord(cursor.getBlob(cursor.getColumnIndexOrThrow(RECORD)))));
      } catch (IOException e) {
        Log.w(TAG, e);
      }
    }
  }

  return results;
}
 
Example 4
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 5
/**
 * @return All storage IDs for ContactRecords, excluding the ones that need to be deleted.
 */
public @NonNull Map<RecipientId, StorageId> getContactStorageSyncIdsMap() {
  SQLiteDatabase              db    = databaseHelper.getReadableDatabase();
  String                      query = STORAGE_SERVICE_ID + " NOT NULL AND " + DIRTY + " != ? AND " + ID + " != ?";
  String[]                    args  = new String[]{String.valueOf(DirtyState.DELETE), Recipient.self().getId().serialize() };
  Map<RecipientId, StorageId> out   = new HashMap<>();

  try (Cursor cursor = db.query(TABLE_NAME, new String[] { ID, STORAGE_SERVICE_ID, GROUP_TYPE }, query, args, null, null, null)) {
    while (cursor != null && cursor.moveToNext()) {
      RecipientId id         = RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID)));
      String      encodedKey = cursor.getString(cursor.getColumnIndexOrThrow(STORAGE_SERVICE_ID));
      GroupType   groupType  = GroupType.fromId(cursor.getInt(cursor.getColumnIndexOrThrow(GROUP_TYPE)));
      byte[]      key        = Base64.decodeOrThrow(encodedKey);

      switch (groupType) {
        case NONE      : out.put(id, StorageId.forContact(key)); break;
        case SIGNAL_V1 : out.put(id, StorageId.forGroupV1(key)); break;
        case SIGNAL_V2 : out.put(id, StorageId.forGroupV2(key)); break;
        default        : throw new AssertionError();
      }
    }
  }

  return out;
}
 
Example 6
public List<GroupRecord> getPushGroupsContainingMember(RecipientId recipientId) {
  SQLiteDatabase database   = databaseHelper.getReadableDatabase();
  String         table      = TABLE_NAME + " INNER JOIN " + ThreadDatabase.TABLE_NAME + " ON " + TABLE_NAME + "." + RECIPIENT_ID + " = " + ThreadDatabase.TABLE_NAME + "." + ThreadDatabase.RECIPIENT_ID;
  String         query      = MEMBERS + " LIKE ? AND " + MMS + " = ?";
  String[]       args       = new String[]{"%" + recipientId.serialize() + "%", "0"};
  String         orderBy    = ThreadDatabase.TABLE_NAME + "." + ThreadDatabase.DATE + " DESC";

  List<GroupRecord> groups = new LinkedList<>();

  try (Cursor cursor = database.query(table, null, query, args, null, null, orderBy)) {
    while (cursor != null && cursor.moveToNext()) {
      String serializedMembers = cursor.getString(cursor.getColumnIndexOrThrow(MEMBERS));

      if (RecipientId.serializedListContains(serializedMembers, recipientId)) {
        groups.add(new Reader(cursor).getCurrent());
      }
    }
  }

  return groups;
}
 
Example 7
@WorkerThread
public boolean isCurrentMember(@NonNull GroupId.Push groupId, @NonNull RecipientId recipientId) {
  SQLiteDatabase database = databaseHelper.getReadableDatabase();

  try (Cursor cursor = database.query(TABLE_NAME, new String[] {MEMBERS},
                                      GROUP_ID + " = ?", new String[] {groupId.toString()},
                                      null, null, null))
  {
    if (cursor.moveToNext()) {
      String serializedMembers = cursor.getString(cursor.getColumnIndexOrThrow(MEMBERS));
      return RecipientId.serializedListContains(serializedMembers, recipientId);
    } else {
      return false;
    }
  }
}
 
Example 8
public Optional<IdentityRecord> getIdentity(@NonNull RecipientId recipientId) {
  SQLiteDatabase database = databaseHelper.getReadableDatabase();
  Cursor         cursor   = null;

  try {
    cursor = database.query(TABLE_NAME, null, RECIPIENT_ID + " = ?",
                            new String[] {recipientId.serialize()}, null, null, null);

    if (cursor != null && cursor.moveToFirst()) {
      return Optional.of(getIdentityRecord(cursor));
    }
  } catch (InvalidKeyException | IOException e) {
    throw new AssertionError(e);
  } finally {
    if (cursor != null) cursor.close();
  }

  return Optional.absent();
}
 
Example 9
public Pair<Long, Boolean> getLastSeenAndHasSent(long threadId) {
  SQLiteDatabase db     = databaseHelper.getReadableDatabase();
  Cursor         cursor = db.query(TABLE_NAME, new String[]{LAST_SEEN, HAS_SENT}, ID_WHERE, new String[]{String.valueOf(threadId)}, null, null, null);

  try {
    if (cursor != null && cursor.moveToFirst()) {
      return new Pair<>(cursor.getLong(0), cursor.getLong(1) == 1);
    }

    return new Pair<>(-1L, false);
  } finally {
    if (cursor != null) cursor.close();
  }
}
 
Example 10
private boolean hasMatchingKey(@NonNull RecipientId id, IdentityKey identityKey) {
  SQLiteDatabase db    = databaseHelper.getReadableDatabase();
  String         query = RECIPIENT_ID + " = ? AND " + IDENTITY_KEY + " = ?";
  String[]       args  = new String[]{id.serialize(), Base64.encodeBytes(identityKey.serialize())};

  try (Cursor cursor = db.query(TABLE_NAME, null, query, args, null, null, null)) {
    return cursor != null && cursor.moveToFirst();
  }
}
 
Example 11
private static @NonNull Optional<DataInfo> findDuplicateDataFileInfo(@NonNull SQLiteDatabase database,
                                                                     @NonNull String hash,
                                                                     @Nullable AttachmentId excludedAttachmentId)
{

  Pair<String, String[]> selectorArgs = buildSharedFileSelectorArgs(hash, excludedAttachmentId);
  try (Cursor cursor = database.query(TABLE_NAME,
                                      new String[]{DATA, DATA_RANDOM, SIZE},
                                      selectorArgs.first,
                                      selectorArgs.second,
                                      null,
                                      null,
                                      null,
                                      "1"))
  {
    if (cursor == null || !cursor.moveToFirst()) return Optional.absent();

    if (cursor.getCount() > 0) {
      DataInfo dataInfo = new DataInfo(new File(cursor.getString(cursor.getColumnIndex(DATA))),
                                       cursor.getLong(cursor.getColumnIndex(SIZE)),
                                       cursor.getBlob(cursor.getColumnIndex(DATA_RANDOM)),
                                       hash);
      return Optional.of(dataInfo);
    } else {
      return Optional.absent();
    }
  }
}
 
Example 12
public boolean isArchived(@NonNull RecipientId recipientId) {
  SQLiteDatabase db    = databaseHelper.getReadableDatabase();
  String         query = RECIPIENT_ID + " = ?";
  String[]       args  = new String[]{ recipientId.serialize() };

  try (Cursor cursor = db.query(TABLE_NAME, new String[] { ARCHIVED }, query, args, null, null, null)) {
    if (cursor != null && cursor.moveToFirst()) {
      return cursor.getInt(cursor.getColumnIndexOrThrow(ARCHIVED)) == 1;
    }
  }

  return false;
}
 
Example 13
public @Nullable Cursor getRecentlyUsedStickers(int limit) {
  SQLiteDatabase db        = databaseHelper.getReadableDatabase();
  String         selection = LAST_USED + " > ? AND " + COVER + " = ?";
  String[]       args      = new String[] { "0", "0" };

  Cursor cursor = db.query(TABLE_NAME, null, selection, args, null, null, LAST_USED + " DESC", String.valueOf(limit));
  setNotifyStickerListeners(cursor);

  return cursor;
}
 
Example 14
public List<RecipientId> getSystemContacts() {
  SQLiteDatabase    db      = databaseHelper.getReadableDatabase();
  List<RecipientId> results = new LinkedList<>();

  try (Cursor cursor = db.query(TABLE_NAME, ID_PROJECTION, SYSTEM_DISPLAY_NAME + " IS NOT NULL AND " + SYSTEM_DISPLAY_NAME + " != \"\"", null, null, null, null)) {
    while (cursor != null && cursor.moveToNext()) {
      results.add(RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID))));
    }
  }

  return results;
}
 
Example 15
public int getMessageCountForThread(long threadId, long beforeTime) {
  SQLiteDatabase db = databaseHelper.getReadableDatabase();

  String[] cols  = new String[] {"COUNT(*)"};
  String   query = THREAD_ID + " = ? AND " + DATE_RECEIVED + " < ?";
  String[] args  = new String[]{String.valueOf(threadId), String.valueOf(beforeTime)};

  try (Cursor cursor = db.query(TABLE_NAME, cols, query, args, null, null, null)) {
    if (cursor != null && cursor.moveToFirst()) {
      return cursor.getInt(0);
    }
  }

  return 0;
}
 
Example 16
public @Nullable RecipientId getRecipientIdForThreadId(long threadId) {
  SQLiteDatabase db = databaseHelper.getReadableDatabase();

  try (Cursor cursor = db.query(TABLE_NAME, null, ID + " = ?", new String[]{ threadId + "" }, null, null, null)) {

    if (cursor != null && cursor.moveToFirst()) {
      return RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(RECIPIENT_ID)));
    }
  }

  return null;
}
 
Example 17
public Long getThreadIdFor(@NonNull RecipientId recipientId) {
  SQLiteDatabase db            = databaseHelper.getReadableDatabase();
  String         where         = RECIPIENT_ID + " = ?";
  String[]       recipientsArg = new String[]{recipientId.serialize()};

  try (Cursor cursor = db.query(TABLE_NAME, new String[]{ ID }, where, recipientsArg, null, null, null)) {
    if (cursor != null && cursor.moveToFirst()) {
      return cursor.getLong(cursor.getColumnIndexOrThrow(ID));
    } else {
      return null;
    }
  }
}
 
Example 18
private @NonNull Optional<RecipientId> getByColumn(@NonNull String column, String value) {
  SQLiteDatabase db    = databaseHelper.getWritableDatabase();
  String         query = column + " = ?";
  String[]       args  = new String[] { value };

  try (Cursor cursor = db.query(TABLE_NAME, ID_PROJECTION, query, args, null, null, null)) {
    if (cursor != null && cursor.moveToFirst()) {
      return Optional.of(RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID))));
    } else {
      return Optional.absent();
    }
  }
}
 
Example 19
public int getDistributionType(long threadId) {
  SQLiteDatabase db     = databaseHelper.getReadableDatabase();
  Cursor         cursor = db.query(TABLE_NAME, new String[]{TYPE}, ID_WHERE, new String[]{String.valueOf(threadId)}, null, null, null);

  try {
    if (cursor != null && cursor.moveToNext()) {
      return cursor.getInt(cursor.getColumnIndexOrThrow(TYPE));
    }

    return DistributionTypes.DEFAULT;
  } finally {
    if (cursor != null) cursor.close();
  }

}
 
Example 20
final int getSecureMessageCount(long threadId) {
  SQLiteDatabase db           = databaseHelper.getReadableDatabase();
  String[]       projection   = new String[] {"COUNT(*)"};
  String         query        = getSecureMessageClause() + "AND " + MmsSmsColumns.THREAD_ID + " = ?";
  String[]       args         = new String[]{String.valueOf(threadId)};

  try (Cursor cursor = db.query(getTableName(), projection, query, args, null, null, null, null)) {
    if (cursor != null && cursor.moveToFirst()) {
      return cursor.getInt(0);
    } else {
      return 0;
    }
  }
}