Search in sources :

Example 1 with SQLiteQueryBuilder

use of net.sqlcipher.database.SQLiteQueryBuilder in project Signal-Android by signalapp.

the class MmsSmsDatabase method queryTables.

private Cursor queryTables(String[] projection, String selection, String order, String limit) {
    String[] mmsProjection = { MmsDatabase.DATE_SENT + " AS " + MmsSmsColumns.NORMALIZED_DATE_SENT, MmsDatabase.DATE_RECEIVED + " AS " + MmsSmsColumns.NORMALIZED_DATE_RECEIVED, MmsDatabase.TABLE_NAME + "." + MmsDatabase.ID + " AS " + MmsSmsColumns.ID, "'MMS::' || " + MmsDatabase.TABLE_NAME + "." + MmsDatabase.ID + " || '::' || " + MmsDatabase.DATE_SENT + " AS " + MmsSmsColumns.UNIQUE_ROW_ID, AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.ROW_ID + " AS " + AttachmentDatabase.ATTACHMENT_ID_ALIAS, SmsDatabase.BODY, MmsSmsColumns.READ, MmsSmsColumns.THREAD_ID, SmsDatabase.TYPE, SmsDatabase.ADDRESS, SmsDatabase.ADDRESS_DEVICE_ID, SmsDatabase.SUBJECT, MmsDatabase.MESSAGE_TYPE, MmsDatabase.MESSAGE_BOX, SmsDatabase.STATUS, MmsDatabase.PART_COUNT, MmsDatabase.CONTENT_LOCATION, MmsDatabase.TRANSACTION_ID, MmsDatabase.MESSAGE_SIZE, MmsDatabase.EXPIRY, MmsDatabase.STATUS, MmsSmsColumns.DELIVERY_RECEIPT_COUNT, MmsSmsColumns.READ_RECEIPT_COUNT, MmsSmsColumns.MISMATCHED_IDENTITIES, MmsSmsColumns.SUBSCRIPTION_ID, MmsSmsColumns.EXPIRES_IN, MmsSmsColumns.EXPIRE_STARTED, MmsSmsColumns.NOTIFIED, MmsDatabase.NETWORK_FAILURE, TRANSPORT, AttachmentDatabase.UNIQUE_ID, AttachmentDatabase.MMS_ID, AttachmentDatabase.SIZE, AttachmentDatabase.FILE_NAME, AttachmentDatabase.DATA, AttachmentDatabase.THUMBNAIL, AttachmentDatabase.CONTENT_TYPE, AttachmentDatabase.CONTENT_LOCATION, AttachmentDatabase.DIGEST, AttachmentDatabase.FAST_PREFLIGHT_ID, AttachmentDatabase.VOICE_NOTE, AttachmentDatabase.CONTENT_DISPOSITION, AttachmentDatabase.NAME, AttachmentDatabase.TRANSFER_STATE };
    String[] smsProjection = { SmsDatabase.DATE_SENT + " AS " + MmsSmsColumns.NORMALIZED_DATE_SENT, SmsDatabase.DATE_RECEIVED + " AS " + MmsSmsColumns.NORMALIZED_DATE_RECEIVED, MmsSmsColumns.ID, "'SMS::' || " + MmsSmsColumns.ID + " || '::' || " + SmsDatabase.DATE_SENT + " AS " + MmsSmsColumns.UNIQUE_ROW_ID, "NULL AS " + AttachmentDatabase.ATTACHMENT_ID_ALIAS, SmsDatabase.BODY, MmsSmsColumns.READ, MmsSmsColumns.THREAD_ID, SmsDatabase.TYPE, SmsDatabase.ADDRESS, SmsDatabase.ADDRESS_DEVICE_ID, SmsDatabase.SUBJECT, MmsDatabase.MESSAGE_TYPE, MmsDatabase.MESSAGE_BOX, SmsDatabase.STATUS, MmsDatabase.PART_COUNT, MmsDatabase.CONTENT_LOCATION, MmsDatabase.TRANSACTION_ID, MmsDatabase.MESSAGE_SIZE, MmsDatabase.EXPIRY, MmsDatabase.STATUS, MmsSmsColumns.DELIVERY_RECEIPT_COUNT, MmsSmsColumns.READ_RECEIPT_COUNT, MmsSmsColumns.MISMATCHED_IDENTITIES, MmsSmsColumns.SUBSCRIPTION_ID, MmsSmsColumns.EXPIRES_IN, MmsSmsColumns.EXPIRE_STARTED, MmsSmsColumns.NOTIFIED, MmsDatabase.NETWORK_FAILURE, TRANSPORT, AttachmentDatabase.UNIQUE_ID, AttachmentDatabase.MMS_ID, AttachmentDatabase.SIZE, AttachmentDatabase.FILE_NAME, AttachmentDatabase.DATA, AttachmentDatabase.THUMBNAIL, AttachmentDatabase.CONTENT_TYPE, AttachmentDatabase.CONTENT_LOCATION, AttachmentDatabase.DIGEST, AttachmentDatabase.FAST_PREFLIGHT_ID, AttachmentDatabase.VOICE_NOTE, AttachmentDatabase.CONTENT_DISPOSITION, AttachmentDatabase.NAME, AttachmentDatabase.TRANSFER_STATE };
    SQLiteQueryBuilder mmsQueryBuilder = new SQLiteQueryBuilder();
    SQLiteQueryBuilder smsQueryBuilder = new SQLiteQueryBuilder();
    mmsQueryBuilder.setDistinct(true);
    smsQueryBuilder.setDistinct(true);
    smsQueryBuilder.setTables(SmsDatabase.TABLE_NAME);
    mmsQueryBuilder.setTables(MmsDatabase.TABLE_NAME + " LEFT OUTER JOIN " + AttachmentDatabase.TABLE_NAME + " ON " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.ROW_ID + " = " + " (SELECT " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.ROW_ID + " FROM " + AttachmentDatabase.TABLE_NAME + " WHERE " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.MMS_ID + " = " + MmsDatabase.TABLE_NAME + "." + MmsDatabase.ID + " LIMIT 1)");
    Set<String> mmsColumnsPresent = new HashSet<>();
    mmsColumnsPresent.add(MmsSmsColumns.ID);
    mmsColumnsPresent.add(MmsSmsColumns.READ);
    mmsColumnsPresent.add(MmsSmsColumns.THREAD_ID);
    mmsColumnsPresent.add(MmsSmsColumns.BODY);
    mmsColumnsPresent.add(MmsSmsColumns.ADDRESS);
    mmsColumnsPresent.add(MmsSmsColumns.ADDRESS_DEVICE_ID);
    mmsColumnsPresent.add(MmsSmsColumns.DELIVERY_RECEIPT_COUNT);
    mmsColumnsPresent.add(MmsSmsColumns.READ_RECEIPT_COUNT);
    mmsColumnsPresent.add(MmsSmsColumns.MISMATCHED_IDENTITIES);
    mmsColumnsPresent.add(MmsSmsColumns.SUBSCRIPTION_ID);
    mmsColumnsPresent.add(MmsSmsColumns.EXPIRES_IN);
    mmsColumnsPresent.add(MmsSmsColumns.EXPIRE_STARTED);
    mmsColumnsPresent.add(MmsDatabase.MESSAGE_TYPE);
    mmsColumnsPresent.add(MmsDatabase.MESSAGE_BOX);
    mmsColumnsPresent.add(MmsDatabase.DATE_SENT);
    mmsColumnsPresent.add(MmsDatabase.DATE_RECEIVED);
    mmsColumnsPresent.add(MmsDatabase.PART_COUNT);
    mmsColumnsPresent.add(MmsDatabase.CONTENT_LOCATION);
    mmsColumnsPresent.add(MmsDatabase.TRANSACTION_ID);
    mmsColumnsPresent.add(MmsDatabase.MESSAGE_SIZE);
    mmsColumnsPresent.add(MmsDatabase.EXPIRY);
    mmsColumnsPresent.add(MmsDatabase.NOTIFIED);
    mmsColumnsPresent.add(MmsDatabase.STATUS);
    mmsColumnsPresent.add(MmsDatabase.NETWORK_FAILURE);
    mmsColumnsPresent.add(AttachmentDatabase.ROW_ID);
    mmsColumnsPresent.add(AttachmentDatabase.UNIQUE_ID);
    mmsColumnsPresent.add(AttachmentDatabase.MMS_ID);
    mmsColumnsPresent.add(AttachmentDatabase.SIZE);
    mmsColumnsPresent.add(AttachmentDatabase.FILE_NAME);
    mmsColumnsPresent.add(AttachmentDatabase.DATA);
    mmsColumnsPresent.add(AttachmentDatabase.THUMBNAIL);
    mmsColumnsPresent.add(AttachmentDatabase.CONTENT_TYPE);
    mmsColumnsPresent.add(AttachmentDatabase.CONTENT_LOCATION);
    mmsColumnsPresent.add(AttachmentDatabase.DIGEST);
    mmsColumnsPresent.add(AttachmentDatabase.FAST_PREFLIGHT_ID);
    mmsColumnsPresent.add(AttachmentDatabase.VOICE_NOTE);
    mmsColumnsPresent.add(AttachmentDatabase.CONTENT_DISPOSITION);
    mmsColumnsPresent.add(AttachmentDatabase.NAME);
    mmsColumnsPresent.add(AttachmentDatabase.TRANSFER_STATE);
    Set<String> smsColumnsPresent = new HashSet<>();
    smsColumnsPresent.add(MmsSmsColumns.ID);
    smsColumnsPresent.add(MmsSmsColumns.BODY);
    smsColumnsPresent.add(MmsSmsColumns.ADDRESS);
    smsColumnsPresent.add(MmsSmsColumns.ADDRESS_DEVICE_ID);
    smsColumnsPresent.add(MmsSmsColumns.READ);
    smsColumnsPresent.add(MmsSmsColumns.THREAD_ID);
    smsColumnsPresent.add(MmsSmsColumns.DELIVERY_RECEIPT_COUNT);
    smsColumnsPresent.add(MmsSmsColumns.READ_RECEIPT_COUNT);
    smsColumnsPresent.add(MmsSmsColumns.MISMATCHED_IDENTITIES);
    smsColumnsPresent.add(MmsSmsColumns.SUBSCRIPTION_ID);
    smsColumnsPresent.add(MmsSmsColumns.EXPIRES_IN);
    smsColumnsPresent.add(MmsSmsColumns.EXPIRE_STARTED);
    smsColumnsPresent.add(MmsSmsColumns.NOTIFIED);
    smsColumnsPresent.add(SmsDatabase.TYPE);
    smsColumnsPresent.add(SmsDatabase.SUBJECT);
    smsColumnsPresent.add(SmsDatabase.DATE_SENT);
    smsColumnsPresent.add(SmsDatabase.DATE_RECEIVED);
    smsColumnsPresent.add(SmsDatabase.STATUS);
    @SuppressWarnings("deprecation") String mmsSubQuery = mmsQueryBuilder.buildUnionSubQuery(TRANSPORT, mmsProjection, mmsColumnsPresent, 4, MMS_TRANSPORT, selection, null, null, null);
    @SuppressWarnings("deprecation") String smsSubQuery = smsQueryBuilder.buildUnionSubQuery(TRANSPORT, smsProjection, smsColumnsPresent, 4, SMS_TRANSPORT, selection, null, null, null);
    SQLiteQueryBuilder unionQueryBuilder = new SQLiteQueryBuilder();
    String unionQuery = unionQueryBuilder.buildUnionQuery(new String[] { smsSubQuery, mmsSubQuery }, order, limit);
    SQLiteQueryBuilder outerQueryBuilder = new SQLiteQueryBuilder();
    outerQueryBuilder.setTables("(" + unionQuery + ")");
    @SuppressWarnings("deprecation") String query = outerQueryBuilder.buildQuery(projection, null, null, null, null, null, null);
    Log.w("MmsSmsDatabase", "Executing query: " + query);
    SQLiteDatabase db = databaseHelper.getReadableDatabase();
    return db.rawQuery(query, null);
}
Also used : SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) SQLiteQueryBuilder(net.sqlcipher.database.SQLiteQueryBuilder) HashSet(java.util.HashSet)

Example 2 with SQLiteQueryBuilder

use of net.sqlcipher.database.SQLiteQueryBuilder in project Zom-Android by zom.

the class ImpsProvider method getContactId.

private long getContactId(final SQLiteDatabase db, final String accountId, final String contact) {
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    qb.setTables(TABLE_CONTACTS);
    qb.setProjectionMap(sContactsProjectionMap);
    mQueryContactIdSelectionArgs2[0] = accountId;
    mQueryContactIdSelectionArgs2[1] = contact;
    Cursor c = qb.query(db, CONTACT_ID_PROJECTION, CONTACT_ID_QUERY_SELECTION, mQueryContactIdSelectionArgs2, null, null, null, null);
    long contactId = 0;
    try {
        if (c.moveToFirst()) {
            contactId = c.getLong(CONTACT_ID_COLUMN);
        }
    } finally {
        c.close();
    }
    return contactId;
}
Also used : Cursor(android.database.Cursor) SQLiteQueryBuilder(net.sqlcipher.database.SQLiteQueryBuilder)

Example 3 with SQLiteQueryBuilder

use of net.sqlcipher.database.SQLiteQueryBuilder in project Zom-Android by zom.

the class ImpsProvider method queryInternal.

public Cursor queryInternal(Uri url, String[] projectionIn, String selection, String[] selectionArgs, String sort) {
    Debug.onServiceStart();
    if (!mLoadedLibs) {
        SQLiteDatabase.loadLibs(this.getContext().getApplicationContext());
        mLoadedLibs = true;
    }
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    StringBuilder whereClause = new StringBuilder();
    if (selection != null) {
        whereClause.append(selection);
    }
    String groupBy = null;
    String limit = null;
    // Generate the body of the query
    int match = mUrlMatcher.match(url);
    switch(match) {
        case MATCH_PROVIDERS_BY_ID:
            appendWhere(whereClause, Imps.Provider._ID, "=", url.getPathSegments().get(1));
        case MATCH_PROVIDERS:
            qb.setTables(TABLE_PROVIDERS);
            break;
        case MATCH_PROVIDERS_WITH_ACCOUNT:
            qb.setTables(PROVIDER_JOIN_ACCOUNT_TABLE);
            qb.setProjectionMap(sProviderAccountsProjectionMap);
            break;
        case MATCH_ACCOUNTS_WITH_DOMAIN:
            qb.setTables(DOMAIN_JOIN_ACCOUNT_TABLE);
            qb.setProjectionMap(sAccountsByDomainProjectionMap);
            break;
        case MATCH_ACCOUNTS_BY_ID:
            appendWhere(whereClause, Imps.Account._ID, "=", url.getPathSegments().get(1));
        // falls down
        case MATCH_ACCOUNTS:
            qb.setTables(TABLE_ACCOUNTS);
            break;
        case MATCH_CONTACTS:
            qb.setTables(CONTACT_JOIN_PRESENCE_CHAT_AVATAR_TABLE);
            qb.setProjectionMap(sContactsProjectionMap);
            break;
        case MATCH_CONTACTS_JOIN_PRESENCE:
            qb.setTables(CONTACT_JOIN_PRESENCE_TABLE);
            qb.setProjectionMap(sContactsProjectionMap);
            break;
        case MATCH_CONTACTS_BAREBONE:
            qb.setTables(TABLE_CONTACTS);
            break;
        case MATCH_CHATTING_CONTACTS:
            qb.setTables(CONTACT_JOIN_PRESENCE_CHAT_AVATAR_TABLE);
            qb.setProjectionMap(sContactsProjectionMap);
            appendWhere(whereClause, "chats.last_message_date IS NOT NULL");
            // blocked contacts can't have conversations.
            break;
        case MATCH_CONTACTS_BY_PROVIDER:
            buildQueryContactsByProvider(qb, whereClause, url);
            appendWhere(whereClause, NON_BLOCKED_CONTACTS_WHERE_CLAUSE);
            break;
        case MATCH_CHATTING_CONTACTS_BY_PROVIDER:
            buildQueryContactsByProvider(qb, whereClause, url);
            appendWhere(whereClause, "chats.last_message_date IS NOT NULL");
            // blocked contacts can't have conversations.
            break;
        case MATCH_NO_CHATTING_CONTACTS_BY_PROVIDER:
            buildQueryContactsByProvider(qb, whereClause, url);
            appendWhere(whereClause, "chats.last_message_date IS NULL");
            appendWhere(whereClause, NON_BLOCKED_CONTACTS_WHERE_CLAUSE);
            break;
        case MATCH_ONLINE_CONTACTS_BY_PROVIDER:
            buildQueryContactsByProvider(qb, whereClause, url);
            appendWhere(whereClause, Imps.Contacts.PRESENCE_STATUS, "!=", Imps.Presence.OFFLINE);
            appendWhere(whereClause, NON_BLOCKED_CONTACTS_WHERE_CLAUSE);
            break;
        case MATCH_OFFLINE_CONTACTS_BY_PROVIDER:
            buildQueryContactsByProvider(qb, whereClause, url);
            appendWhere(whereClause, Imps.Contacts.PRESENCE_STATUS, "=", Imps.Presence.OFFLINE);
            appendWhere(whereClause, NON_BLOCKED_CONTACTS_WHERE_CLAUSE);
            break;
        case MATCH_BLOCKED_CONTACTS:
            qb.setTables(CONTACT_JOIN_PRESENCE_CHAT_AVATAR_TABLE);
            qb.setProjectionMap(sContactsProjectionMap);
            appendWhere(whereClause, BLOCKED_CONTACTS_WHERE_CLAUSE);
            break;
        case MATCH_CONTACT:
            qb.setTables(CONTACT_JOIN_PRESENCE_CHAT_AVATAR_TABLE);
            qb.setProjectionMap(sContactsProjectionMap);
            appendWhere(whereClause, "contacts._id", "=", url.getPathSegments().get(1));
            break;
        case MATCH_ONLINE_CONTACT_COUNT:
            qb.setTables(CONTACT_JOIN_PRESENCE_CHAT_TABLE);
            qb.setProjectionMap(sContactsProjectionMap);
            appendWhere(whereClause, Imps.Contacts.PRESENCE_STATUS, "!=", Imps.Presence.OFFLINE);
            appendWhere(whereClause, "chats.last_message_date IS NULL");
            appendWhere(whereClause, NON_BLOCKED_CONTACTS_WHERE_CLAUSE);
            groupBy = Imps.Contacts.CONTACTLIST;
            break;
        case MATCH_CONTACTLISTS_BY_PROVIDER:
            appendWhere(whereClause, Imps.ContactList.ACCOUNT, "=", url.getPathSegments().get(2));
        // fall through
        case MATCH_CONTACTLISTS:
            qb.setTables(TABLE_CONTACT_LIST);
            qb.setProjectionMap(sContactListProjectionMap);
            break;
        case MATCH_CONTACTLIST:
            qb.setTables(TABLE_CONTACT_LIST);
            appendWhere(whereClause, Imps.ContactList._ID, "=", url.getPathSegments().get(1));
            break;
        case MATCH_BLOCKEDLIST:
            qb.setTables(BLOCKEDLIST_JOIN_AVATAR_TABLE);
            qb.setProjectionMap(sBlockedListProjectionMap);
            break;
        case MATCH_BLOCKEDLIST_BY_PROVIDER:
            qb.setTables(BLOCKEDLIST_JOIN_AVATAR_TABLE);
            qb.setProjectionMap(sBlockedListProjectionMap);
            appendWhere(whereClause, Imps.BlockedList.ACCOUNT, "=", url.getPathSegments().get(2));
            break;
        case MATCH_CONTACTS_ETAGS:
            qb.setTables(TABLE_CONTACTS_ETAG);
            break;
        case MATCH_CONTACTS_ETAG:
            qb.setTables(TABLE_CONTACTS_ETAG);
            appendWhere(whereClause, "_id", "=", url.getPathSegments().get(1));
            break;
        case MATCH_MESSAGES_BY_THREAD_ID:
            appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", url.getPathSegments().get(1));
        case MATCH_MESSAGES:
            qb.setTables(TABLE_MESSAGES);
            final String selectionClause = whereClause.toString();
            final String query1 = qb.buildQuery(projectionIn, selectionClause, null, null, null, null, null);
            // Build the second query for frequent
            qb = new SQLiteQueryBuilder();
            qb.setTables(TABLE_IN_MEMORY_MESSAGES);
            final String query2 = qb.buildQuery(projectionIn, selectionClause, null, null, null, null, null);
            // Put them together
            final String query = qb.buildUnionQuery(new String[] { query1, query2 }, sort, null);
            final SQLiteDatabase db = getDBHelper().getWritableDatabase();
            String[] doubleArgs = null;
            if (selectionArgs != null) {
                // Arrays.copyOf(selectionArgs, selectionArgs.length * 2);
                doubleArgs = new String[selectionArgs.length * 2];
                System.arraycopy(selectionArgs, 0, doubleArgs, 0, selectionArgs.length);
                System.arraycopy(selectionArgs, 0, doubleArgs, selectionArgs.length, selectionArgs.length);
            }
            Cursor c = db.rawQueryWithFactory(null, query, doubleArgs, TABLE_MESSAGES);
            if ((c != null) && !isTemporary()) {
                c.setNotificationUri(getContext().getContentResolver(), url);
            }
            return c;
        case MATCH_MESSAGES_BY_PACKET_ID:
        case MATCH_OTR_MESSAGES_BY_PACKET_ID:
            appendWhere(whereClause, Imps.Messages.PACKET_ID, "=", url.getPathSegments().get(1));
            qb.setTables(TABLE_MESSAGES);
            final String selectionClausePacketId = whereClause.toString();
            final String query1PacketId = qb.buildQuery(projectionIn, selectionClausePacketId, null, null, null, null, null);
            // Build the second query for frequent
            qb = new SQLiteQueryBuilder();
            qb.setTables(TABLE_IN_MEMORY_MESSAGES);
            final String query2PacketId = qb.buildQuery(projectionIn, selectionClausePacketId, null, null, null, null, null);
            // Put them together
            final String queryPacketId = qb.buildUnionQuery(new String[] { query1PacketId, query2PacketId }, sort, null);
            final SQLiteDatabase dbPacketId = getDBHelper().getWritableDatabase();
            String[] doubleArgsPacketId = null;
            if (selectionArgs != null) {
                // Arrays.copyOf(selectionArgs, selectionArgs.length * 2);
                doubleArgsPacketId = new String[selectionArgs.length * 2];
                System.arraycopy(selectionArgs, 0, doubleArgsPacketId, 0, selectionArgs.length);
                System.arraycopy(selectionArgs, 0, doubleArgsPacketId, selectionArgs.length, selectionArgs.length);
            }
            Cursor cPacketId = dbPacketId.rawQueryWithFactory(null, queryPacketId, doubleArgsPacketId, TABLE_MESSAGES);
            if ((cPacketId != null) && !isTemporary()) {
                cPacketId.setNotificationUri(getContext().getContentResolver(), url);
            }
            return cPacketId;
        case MATCH_MESSAGE:
            qb.setTables(TABLE_MESSAGES);
            appendWhere(whereClause, Imps.Messages._ID, "=", url.getPathSegments().get(1));
            break;
        case MATCH_MESSAGES_BY_CONTACT:
            qb.setTables(MESSAGE_JOIN_CONTACT_TABLE);
            qb.setProjectionMap(sMessagesProjectionMap);
            appendWhere(whereClause, Imps.Contacts.ACCOUNT, "=", url.getPathSegments().get(1));
            appendWhere(whereClause, "contacts.username", "=", decodeURLSegment(url.getPathSegments().get(2)));
            final String sel = whereClause.toString();
            final String q1 = qb.buildQuery(projectionIn, sel, null, null, null, null, null);
            // Build the second query for frequent
            qb = new SQLiteQueryBuilder();
            qb.setTables(IN_MEMORY_MESSAGES_JOIN_CONTACT_TABLE);
            qb.setProjectionMap(sInMemoryMessagesProjectionMap);
            final String q2 = qb.buildQuery(projectionIn, sel, null, null, null, null, null);
            // Put them together
            final String q3 = qb.buildUnionQuery(new String[] { q1, q2 }, sort, null);
            final SQLiteDatabase db2 = getDBHelper().getWritableDatabase();
            Cursor c2 = db2.rawQueryWithFactory(null, q3, null, MESSAGE_JOIN_CONTACT_TABLE);
            if ((c2 != null) && !isTemporary()) {
                c2.setNotificationUri(getContext().getContentResolver(), url);
            }
            return c2;
        case MATCH_MESSAGES_BY_SEARCH:
            qb.setTables(MESSAGE_JOIN_CONTACT_TABLE);
            qb.setProjectionMap(sMessagesProjectionMap);
            limit = "20";
            final String q4 = qb.buildQuery(projectionIn, whereClause.toString(), null, null, null, null, null);
            // Build the second query for frequent
            qb = new SQLiteQueryBuilder();
            qb.setTables(IN_MEMORY_MESSAGES_JOIN_CONTACT_TABLE);
            qb.setProjectionMap(sInMemoryMessagesProjectionMap);
            final String q5 = qb.buildQuery(projectionIn, whereClause.toString(), null, null, null, null, null);
            // Put them together
            final String q6 = qb.buildUnionQuery(new String[] { q4, q5 }, sort, limit);
            final SQLiteDatabase db3 = getDBHelper().getWritableDatabase();
            Cursor c3 = db3.rawQueryWithFactory(null, q6, null, MESSAGE_JOIN_CONTACT_TABLE);
            if ((c3 != null) && !isTemporary()) {
                c3.setNotificationUri(getContext().getContentResolver(), url);
            }
            return c3;
        case MATCH_INVITATIONS:
            qb.setTables(TABLE_INVITATIONS);
            break;
        case MATCH_INVITATION:
            qb.setTables(TABLE_INVITATIONS);
            appendWhere(whereClause, Imps.Invitation._ID, "=", url.getPathSegments().get(1));
            break;
        case MATCH_GROUP_MEMBERS:
            qb.setTables(TABLE_GROUP_MEMBERS);
            break;
        case MATCH_GROUP_MEMBERS_BY_GROUP:
            qb.setTables(TABLE_GROUP_MEMBERS);
            qb.setDistinct(true);
            appendWhere(whereClause, Imps.GroupMembers.GROUP, "=", url.getPathSegments().get(1));
            groupBy = Imps.GroupMembers.USERNAME;
            break;
        case MATCH_AVATARS:
            qb.setTables(TABLE_AVATARS);
            break;
        case MATCH_AVATAR_BY_PROVIDER:
            qb.setTables(TABLE_AVATARS);
            appendWhere(whereClause, Imps.Avatars.ACCOUNT, "=", url.getPathSegments().get(2));
            break;
        case MATCH_CHATS:
            qb.setTables(TABLE_CHATS);
            break;
        case MATCH_CHATS_ID:
            qb.setTables(TABLE_CHATS);
            appendWhere(whereClause, Imps.Chats.CONTACT_ID, "=", url.getPathSegments().get(1));
            break;
        case MATCH_CHATS_BY_ACCOUNT:
            qb.setTables(TABLE_CHATS);
            String accountStr = decodeURLSegment(url.getLastPathSegment());
            appendWhere(whereClause, buildContactIdSelection(Imps.Chats.CONTACT_ID, Imps.Contacts.ACCOUNT + "='" + accountStr + "'"));
            break;
        case MATCH_PRESENCE:
            qb.setTables(TABLE_PRESENCE);
            break;
        case MATCH_PRESENCE_ID:
            qb.setTables(TABLE_PRESENCE);
            appendWhere(whereClause, Imps.Presence.CONTACT_ID, "=", url.getPathSegments().get(1));
            break;
        case MATCH_SESSIONS:
            qb.setTables(TABLE_SESSION_COOKIES);
            break;
        case MATCH_SESSIONS_BY_PROVIDER:
            qb.setTables(TABLE_SESSION_COOKIES);
            appendWhere(whereClause, Imps.SessionCookies.ACCOUNT, "=", url.getPathSegments().get(2));
            break;
        case MATCH_PROVIDER_SETTINGS_BY_ID_AND_NAME:
            appendWhere(whereClause, Imps.ProviderSettings.NAME, "=", url.getPathSegments().get(2));
        // fall through
        case MATCH_PROVIDER_SETTINGS_BY_ID:
            appendWhere(whereClause, Imps.ProviderSettings.PROVIDER, "=", url.getPathSegments().get(1));
        // fall through
        case MATCH_PROVIDER_SETTINGS:
            qb.setTables(TABLE_PROVIDER_SETTINGS);
            break;
        case MATCH_ACCOUNTS_STATUS:
            qb.setTables(TABLE_ACCOUNT_STATUS);
            break;
        case MATCH_ACCOUNT_STATUS:
            qb.setTables(TABLE_ACCOUNT_STATUS);
            appendWhere(whereClause, Imps.AccountStatus.ACCOUNT, "=", url.getPathSegments().get(1));
            break;
        case MATCH_BRANDING_RESOURCE_MAP_CACHE:
            qb.setTables(TABLE_BRANDING_RESOURCE_MAP_CACHE);
            break;
        // mcs and rmq queries
        case MATCH_OUTGOING_RMQ_MESSAGES:
            qb.setTables(TABLE_OUTGOING_RMQ_MESSAGES);
            break;
        case MATCH_OUTGOING_HIGHEST_RMQ_ID:
            qb.setTables(TABLE_OUTGOING_RMQ_MESSAGES);
            sort = "rmq_id DESC";
            limit = "1";
            break;
        case MATCH_LAST_RMQ_ID:
            qb.setTables(TABLE_LAST_RMQ_ID);
            limit = "1";
            break;
        case MATCH_S2D_RMQ_IDS:
            qb.setTables(TABLE_S2D_RMQ_IDS);
            break;
        default:
            throw new IllegalArgumentException("Unknown URL " + url);
    }
    if (getDBHelper() == null)
        return null;
    // run the query
    SQLiteDatabase db;
    try {
        db = getDBHelper().getReadableDatabase();
    } catch (net.sqlcipher.database.SQLiteException e) {
        // Failed to actually open - the passphrase must have been wrong - reset the helper
        synchronized (mDbHelperLock) {
            mDbHelper = null;
            mDbHelperLock.notify();
        }
        throw e;
    }
    net.sqlcipher.Cursor c = null;
    if (!db.isOpen())
        return null;
    try {
        qb.setDistinct(true);
        c = qb.query(db, projectionIn, whereClause.toString(), selectionArgs, groupBy, null, sort, limit);
        if (c != null) {
            switch(match) {
                case MATCH_CHATTING_CONTACTS:
                case MATCH_CHATTING_CONTACTS_BY_PROVIDER:
                    url = Contacts.CONTENT_URI_CHAT_CONTACTS_BY;
                    break;
                case MATCH_CONTACTS_BY_PROVIDER:
                case MATCH_ONLINE_CONTACTS_BY_PROVIDER:
                case MATCH_OFFLINE_CONTACTS_BY_PROVIDER:
                case MATCH_CONTACTS_BAREBONE:
                case MATCH_CONTACTS_JOIN_PRESENCE:
                case MATCH_ONLINE_CONTACT_COUNT:
                    url = Imps.Contacts.CONTENT_URI;
                    break;
            }
            // log("set notify url " + url);
            c.setNotificationUri(getContext().getContentResolver(), url);
        }
        // c = new MyCrossProcessCursorWrapper(c);
        return c;
    } catch (Exception ex) {
        LogCleaner.error(LOG_TAG, "query exc db caught ", ex);
        return null;
    } catch (Error ex) {
        LogCleaner.error(LOG_TAG, "query error db caught ", ex);
        return null;
    }
}
Also used : Cursor(android.database.Cursor) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SQLiteConstraintException(net.sqlcipher.database.SQLiteConstraintException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) SQLiteQueryBuilder(net.sqlcipher.database.SQLiteQueryBuilder)

Example 4 with SQLiteQueryBuilder

use of net.sqlcipher.database.SQLiteQueryBuilder in project Zom-Android by zom.

the class ImpsProvider method seedInitialPresenceByAccount.

/**
 * make sure the presence for all contacts of a given account is set to
 * offline, and each contact has a presence row associated with it. However,
 * this method does not remove presences for which the corresponding
 * contacts no longer exist. That's probably ok since presence is kept in
 * memory, so it won't stay around for too long. Here is the algorithm.
 *
 * 1. for all presence that have a corresponding contact, make it OFFLINE.
 * This is one sqlite call. 2. query for all the contacts that don't have a
 * presence, and add a presence row for them.
 *
 * TODO simplify the presence management! The desire is to have a presence
 * row for each TODO contact in the database, so later we can just call
 * update() on the presence rows TODO instead of checking for the existence
 * of presence first. The assumption is we get TODO presence updates much
 * more frequently. However, the logic to maintain that goal is TODO overly
 * complicated. One possible solution is to use insert_or_replace the
 * presence rows TODO when updating the presence. That way we don't always
 * need to maintain an empty presence TODO row for each contact.
 *
 * @param account the account of the contacts for which we want to create
 *            seed presence rows.
 */
private void seedInitialPresenceByAccount(long account) {
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    qb.setTables(TABLE_CONTACTS);
    qb.setProjectionMap(sContactsProjectionMap);
    mQueryContactIdSelectionArgs1[0] = String.valueOf(account);
    final SQLiteDatabase db = getDBHelper().getWritableDatabase();
    db.beginTransaction();
    Cursor c = null;
    try {
        ContentValues presenceValues = new ContentValues();
        presenceValues.put(Imps.Presence.PRESENCE_STATUS, Imps.Presence.OFFLINE);
        presenceValues.put(Imps.Presence.PRESENCE_CUSTOM_STATUS, "");
        // update all the presence for the account so they are offline
        StringBuilder buf = new StringBuilder();
        buf.append(Imps.Presence.CONTACT_ID);
        buf.append(" in (select ");
        buf.append(Imps.Contacts._ID);
        buf.append(" from ");
        buf.append(TABLE_CONTACTS);
        buf.append(" where ");
        buf.append(Imps.Contacts.ACCOUNT);
        buf.append("=?) ");
        String selection = buf.toString();
        log("seedInitialPresence: reset presence selection=" + selection);
        int count = db.update(TABLE_PRESENCE, presenceValues, selection, mQueryContactIdSelectionArgs1);
        log("seedInitialPresence: reset " + count + " presence rows to OFFLINE");
        // for in-memory presence table, add a presence row for each contact that
        // doesn't have a presence. in-memory presence table isn't reliable, and goes away
        // when device reboot or IMProvider process dies, so we can't rely on each contact
        // have a corresponding presence.
        {
            log("seedInitialPresence: contacts_with_no_presence_selection => " + CONTACTS_WITH_NO_PRESENCE_SELECTION);
        }
        c = qb.query(db, CONTACT_ID_PROJECTION, CONTACTS_WITH_NO_PRESENCE_SELECTION, mQueryContactIdSelectionArgs1, null, null, null, null);
        log("seedInitialPresence: found " + c.getCount() + " contacts w/o presence");
        count = 0;
        while (c.moveToNext()) {
            long id = c.getLong(CONTACT_ID_COLUMN);
            presenceValues.put(Imps.Presence.CONTACT_ID, id);
            try {
                if (db.insert(TABLE_PRESENCE, null, presenceValues) > 0) {
                    count++;
                }
            } catch (SQLiteConstraintException ex) {
                // we could possibly catch this exception, since there could be a presence
                // row with the same contact_id. That's fine, just ignore the error
                log("seedInitialPresence: insert presence for contact_id " + id + " failed, caught " + ex);
            }
        }
        log("seedInitialPresence: added " + count + " new presence rows");
        db.setTransactionSuccessful();
    } finally {
        if (c != null) {
            c.close();
        }
        db.endTransaction();
    }
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) SQLiteConstraintException(net.sqlcipher.database.SQLiteConstraintException) Cursor(android.database.Cursor) SQLiteQueryBuilder(net.sqlcipher.database.SQLiteQueryBuilder)

Example 5 with SQLiteQueryBuilder

use of net.sqlcipher.database.SQLiteQueryBuilder in project cw-omnibus by commonsguy.

the class Provider method query.

@Override
public Cursor query(Uri url, String[] projection, String selection, String[] selectionArgs, String sort) {
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    qb.setTables(TABLE);
    String orderBy;
    if (TextUtils.isEmpty(sort)) {
        orderBy = Constants.DEFAULT_SORT_ORDER;
    } else {
        orderBy = sort;
    }
    Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);
    c.setNotificationUri(getContext().getContentResolver(), url);
    return (c);
}
Also used : Cursor(android.database.Cursor) SQLiteQueryBuilder(net.sqlcipher.database.SQLiteQueryBuilder)

Aggregations

SQLiteQueryBuilder (net.sqlcipher.database.SQLiteQueryBuilder)5 Cursor (android.database.Cursor)4 SQLiteDatabase (net.sqlcipher.database.SQLiteDatabase)3 SQLiteConstraintException (net.sqlcipher.database.SQLiteConstraintException)2 ContentValues (android.content.ContentValues)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 StreamCorruptedException (java.io.StreamCorruptedException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashSet (java.util.HashSet)1