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
Source File: RecipientDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
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 2
Source File: IdentityDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
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 3
Source File: GroupDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
@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 4
Source File: GroupDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
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 5
Source File: RecipientDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @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
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 7
Source File: SessionDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
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 8
Source File: RecipientDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
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 9
Source File: IdentityDatabase.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
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 10
Source File: AttachmentDatabase.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
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 11
Source File: ThreadDatabase.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
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 12
Source File: StickerDatabase.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
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 13
Source File: RecipientDatabase.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
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 14
Source File: SmsDatabase.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
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 15
Source File: ThreadDatabase.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
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 16
Source File: ThreadDatabase.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
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 17
Source File: ThreadDatabase.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
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 18
Source File: RecipientDatabase.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
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
Source File: ThreadDatabase.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
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
Source File: MessagingDatabase.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
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;
    }
  }
}