text
stringlengths
0
357
goto cleanup;
}
stmt = dc_sqlite3_prepare(context->sql,
"SELECT m.id"
" FROM msgs m"
" LEFT JOIN contacts ct ON m.from_id=ct.id"
" LEFT JOIN chats c ON m.chat_id=c.id"
" WHERE m.state=?"
" AND m.hidden=0"
" AND m.chat_id>?"
" AND ct.blocked=0"
" AND (c.blocked=0 OR c.blocked=?)"
" ORDER BY m.timestamp DESC,m.id DESC;");
sqlite3_bind_int(stmt, 1, DC_STATE_IN_FRESH);
sqlite3_bind_int(stmt, 2, DC_CHAT_ID_LAST_SPECIAL);
sqlite3_bind_int(stmt, 3, show_deaddrop? DC_CHAT_DEADDROP_BLOCKED : 0);
while (sqlite3_step(stmt)==SQLITE_ROW) {
dc_array_add_id(ret, sqlite3_column_int(stmt, 0));
}
cleanup:
sqlite3_finalize(stmt);
return ret;
}
/**
* Search messages containing the given query string.
* Searching can be done globally (chat_id=0) or in a specified chat only (chat_id
* set).
*
* Global chat results are typically displayed using dc_msg_get_summary(), chat
* search results may just hilite the corresponding messages and present a
* prev/next button.
*
* @memberof dc_context_t
* @param context The context object as returned from dc_context_new().
* @param chat_id ID of the chat to search messages in.
* Set this to 0 for a global search.
* @param query The query to search for.
* @return An array of message IDs. Must be freed using dc_array_unref() when no longer needed.
* If nothing can be found, the function returns NULL.
*/
dc_array_t* dc_search_msgs(dc_context_t* context, uint32_t chat_id, const char* query)
{
//clock_t start = clock();
int success = 0;
dc_array_t* ret = dc_array_new(context, 100);
char* strLikeInText = NULL;
char* strLikeBeg = NULL;
char* real_query = NULL;
sqlite3_stmt* stmt = NULL;
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC || ret==NULL || query==NULL) {
goto cleanup;
}
real_query = dc_strdup(query);
dc_trim(real_query);
if (real_query[0]==0) {
success = 1; /*empty result*/
goto cleanup;
}
strLikeInText = dc_mprintf("%%%s%%", real_query);
strLikeBeg = dc_mprintf("%s%%", real_query); /*for the name search, we use "Name%" which is fast as it can use the index ("%Name%" could not). */
/* Incremental search with "LIKE %query%" cannot take advantages from any index
("query%" could for COLLATE NOCASE indexes, see http://www.sqlite.org/optoverview.html#like_opt)
An alternative may be the FULLTEXT sqlite stuff, however, this does not really help with incremental search.
An extra table with all words and a COLLATE NOCASE indexes may help, however,
this must be updated all the time and probably consumes more time than we can save in tenthousands of searches.
For now, we just expect the following query to be fast enough :-) */
if (chat_id) {
stmt = dc_sqlite3_prepare(context->sql,
"SELECT m.id, m.timestamp FROM msgs m"
" LEFT JOIN contacts ct ON m.from_id=ct.id"
" WHERE m.chat_id=? "
" AND m.hidden=0 "
" AND ct.blocked=0 AND (txt LIKE ? OR ct.name LIKE ?)"
" ORDER BY m.timestamp,m.id;"); /* chats starts with the oldest message*/
sqlite3_bind_int (stmt, 1, chat_id);
sqlite3_bind_text(stmt, 2, strLikeInText, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, strLikeBeg, -1, SQLITE_STATIC);
}
else {
int show_deaddrop = 0;//dc_sqlite3_get_config_int(context->sql, "show_deaddrop", 0);
stmt = dc_sqlite3_prepare(context->sql,
"SELECT m.id, m.timestamp FROM msgs m"
" LEFT JOIN contacts ct ON m.from_id=ct.id"
" LEFT JOIN chats c ON m.chat_id=c.id"
" WHERE m.chat_id>" DC_STRINGIFY(DC_CHAT_ID_LAST_SPECIAL)
" AND m.hidden=0 "
" AND (c.blocked=0 OR c.blocked=?)"
" AND ct.blocked=0 AND (m.txt LIKE ? OR ct.name LIKE ?)"
" ORDER BY m.timestamp DESC,m.id DESC;"); /* chat overview starts with the newest message*/
sqlite3_bind_int (stmt, 1, show_deaddrop? DC_CHAT_DEADDROP_BLOCKED : 0);