use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method getUsersInternal.
public void getUsersInternal(String usersToLoad, ArrayList<TLRPC.User> result) throws Exception {
if (usersToLoad == null || usersToLoad.length() == 0 || result == null) {
return;
}
SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid IN(%s)", usersToLoad));
while (cursor.next()) {
try {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
TLRPC.User user = TLRPC.User.TLdeserialize(data, data.readInt32(false), false);
data.reuse();
if (user != null) {
if (user.status != null) {
user.status.expires = cursor.intValue(1);
}
result.add(user);
}
}
} catch (Exception e) {
FileLog.e(e);
}
}
cursor.dispose();
}
use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method deleteDialog.
public void deleteDialog(long did, int messagesOnly) {
storageQueue.postRunnable(() -> {
try {
if (messagesOnly == 3) {
int lastMid = -1;
SQLiteCursor cursor = database.queryFinalized("SELECT last_mid FROM dialogs WHERE did = " + did);
if (cursor.next()) {
lastMid = cursor.intValue(0);
}
cursor.dispose();
if (lastMid != 0) {
return;
}
}
if (DialogObject.isEncryptedDialog(did) || messagesOnly == 2) {
SQLiteCursor cursor = database.queryFinalized("SELECT data FROM messages_v2 WHERE uid = " + did);
ArrayList<File> filesToDelete = new ArrayList<>();
ArrayList<String> namesToDelete = new ArrayList<>();
ArrayList<Pair<Long, Integer>> idsToDelete = new ArrayList<>();
try {
while (cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
TLRPC.Message message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
message.readAttachPath(data, getUserConfig().clientUserId);
data.reuse();
addFilesToDelete(message, filesToDelete, idsToDelete, namesToDelete, false);
}
}
} catch (Exception e) {
FileLog.e(e);
}
cursor.dispose();
deleteFromDownloadQueue(idsToDelete, true);
AndroidUtilities.runOnUIThread(() -> getFileLoader().cancelLoadFiles(namesToDelete));
getFileLoader().deleteFiles(filesToDelete, messagesOnly);
}
if (messagesOnly == 0 || messagesOnly == 3) {
database.executeFast("DELETE FROM dialogs WHERE did = " + did).stepThis().dispose();
database.executeFast("DELETE FROM chat_pinned_v2 WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM chat_pinned_count WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM channel_users_v2 WHERE did = " + did).stepThis().dispose();
database.executeFast("DELETE FROM search_recent WHERE did = " + did).stepThis().dispose();
if (!DialogObject.isEncryptedDialog(did)) {
if (DialogObject.isChatDialog(did)) {
database.executeFast("DELETE FROM chat_settings_v2 WHERE uid = " + (-did)).stepThis().dispose();
}
} else {
database.executeFast("DELETE FROM enc_chats WHERE uid = " + DialogObject.getEncryptedChatId(did)).stepThis().dispose();
}
} else if (messagesOnly == 2) {
SQLiteCursor cursor = database.queryFinalized("SELECT last_mid_i, last_mid FROM dialogs WHERE did = " + did);
int messageId = -1;
if (cursor.next()) {
long last_mid_i = cursor.longValue(0);
long last_mid = cursor.longValue(1);
SQLiteCursor cursor2 = database.queryFinalized("SELECT data FROM messages_v2 WHERE uid = " + did + " AND mid IN (" + last_mid_i + "," + last_mid + ")");
try {
while (cursor2.next()) {
NativeByteBuffer data = cursor2.byteBufferValue(0);
if (data != null) {
TLRPC.Message message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
if (message != null) {
message.readAttachPath(data, getUserConfig().clientUserId);
}
data.reuse();
if (message != null) {
messageId = message.id;
}
}
}
} catch (Exception e) {
FileLog.e(e);
}
cursor2.dispose();
database.executeFast("DELETE FROM messages_v2 WHERE uid = " + did + " AND mid != " + last_mid_i + " AND mid != " + last_mid).stepThis().dispose();
database.executeFast("DELETE FROM messages_holes WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM bot_keyboard WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM media_counts_v2 WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM media_v4 WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM media_holes_v2 WHERE uid = " + did).stepThis().dispose();
getMediaDataController().clearBotKeyboard(did, null);
SQLitePreparedStatement state5 = database.executeFast("REPLACE INTO messages_holes VALUES(?, ?, ?)");
SQLitePreparedStatement state6 = database.executeFast("REPLACE INTO media_holes_v2 VALUES(?, ?, ?, ?)");
if (messageId != -1) {
createFirstHoles(did, state5, state6, messageId);
}
state5.dispose();
state6.dispose();
updateWidgets(did);
}
cursor.dispose();
return;
}
database.executeFast("UPDATE dialogs SET unread_count = 0, unread_count_i = 0 WHERE did = " + did).stepThis().dispose();
database.executeFast("DELETE FROM messages_v2 WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM bot_keyboard WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM media_counts_v2 WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM media_v4 WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM messages_holes WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM media_holes_v2 WHERE uid = " + did).stepThis().dispose();
getMediaDataController().clearBotKeyboard(did, null);
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.needReloadRecentDialogsSearch));
resetAllUnreadCounters(false);
updateWidgets(did);
} catch (Exception e) {
FileLog.e(e);
}
});
}
use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method getWidgetDialogs.
public void getWidgetDialogs(int widgetId, int type, ArrayList<Long> dids, LongSparseArray<TLRPC.Dialog> dialogs, LongSparseArray<TLRPC.Message> messages, ArrayList<TLRPC.User> users, ArrayList<TLRPC.Chat> chats) {
CountDownLatch countDownLatch = new CountDownLatch(1);
storageQueue.postRunnable(() -> {
try {
boolean add = false;
ArrayList<Long> usersToLoad = new ArrayList<>();
ArrayList<Long> chatsToLoad = new ArrayList<>();
SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT did FROM shortcut_widget WHERE id = %d ORDER BY ord ASC", widgetId));
while (cursor.next()) {
long id = cursor.longValue(0);
if (id == -1) {
continue;
}
dids.add(id);
if (DialogObject.isUserDialog(id)) {
usersToLoad.add(id);
} else {
chatsToLoad.add(-id);
}
}
cursor.dispose();
if (dids.isEmpty() && type == EditWidgetActivity.TYPE_CONTACTS) {
cursor = getMessagesStorage().getDatabase().queryFinalized("SELECT did FROM chat_hints WHERE type = 0 ORDER BY rating DESC LIMIT 4");
while (cursor.next()) {
long dialogId = cursor.longValue(0);
dids.add(dialogId);
if (DialogObject.isUserDialog(dialogId)) {
usersToLoad.add(dialogId);
} else {
chatsToLoad.add(-dialogId);
}
}
cursor.dispose();
}
if (dids.isEmpty()) {
add = true;
cursor = database.queryFinalized("SELECT d.did, d.last_mid, d.unread_count, d.date, m.data, m.read_state, m.mid, m.send_state, m.date FROM dialogs as d LEFT JOIN messages_v2 as m ON d.last_mid = m.mid AND d.did = m.uid WHERE d.folder_id = 0 ORDER BY d.pinned DESC, d.date DESC LIMIT 0,10");
} else {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT d.did, d.last_mid, d.unread_count, d.date, m.data, m.read_state, m.mid, m.send_state, m.date FROM dialogs as d LEFT JOIN messages_v2 as m ON d.last_mid = m.mid AND d.did = m.uid WHERE d.did IN(%s)", TextUtils.join(",", dids)));
}
while (cursor.next()) {
long dialogId = cursor.longValue(0);
if (DialogObject.isFolderDialogId(dialogId)) {
continue;
}
if (add) {
dids.add(dialogId);
}
TLRPC.Dialog dialog = new TLRPC.TL_dialog();
dialog.id = dialogId;
dialog.top_message = cursor.intValue(1);
dialog.unread_count = cursor.intValue(2);
dialog.last_message_date = cursor.intValue(3);
dialogs.put(dialog.id, dialog);
NativeByteBuffer data = cursor.byteBufferValue(4);
if (data != null) {
TLRPC.Message message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
message.readAttachPath(data, getUserConfig().clientUserId);
data.reuse();
MessageObject.setUnreadFlags(message, cursor.intValue(5));
message.id = cursor.intValue(6);
message.send_state = cursor.intValue(7);
int date = cursor.intValue(8);
if (date != 0) {
dialog.last_message_date = date;
}
message.dialog_id = dialog.id;
messages.put(dialog.id, message);
addUsersAndChatsFromMessage(message, usersToLoad, chatsToLoad);
}
}
cursor.dispose();
if (!add) {
if (dids.size() > dialogs.size()) {
for (int a = 0, N = dids.size(); a < N; a++) {
long did = dids.get(a);
if (dialogs.get(dids.get(a)) == null) {
TLRPC.TL_dialog dialog = new TLRPC.TL_dialog();
dialog.id = did;
dialogs.put(dialog.id, dialog);
if (DialogObject.isChatDialog(did)) {
if (chatsToLoad.contains(-did)) {
chatsToLoad.add(-did);
}
} else {
if (usersToLoad.contains(did)) {
usersToLoad.add(did);
}
}
}
}
}
}
if (!chatsToLoad.isEmpty()) {
getChatsInternal(TextUtils.join(",", chatsToLoad), chats);
}
if (!usersToLoad.isEmpty()) {
getUsersInternal(TextUtils.join(",", usersToLoad), users);
}
} catch (Exception e) {
FileLog.e(e);
} finally {
countDownLatch.countDown();
}
});
try {
countDownLatch.await();
} catch (Exception e) {
FileLog.e(e);
}
}
use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method putMessages.
public void putMessages(TLRPC.messages_Messages messages, long dialogId, int load_type, int max_id, boolean createDialog, boolean scheduled) {
storageQueue.postRunnable(() -> {
try {
if (scheduled) {
database.executeFast(String.format(Locale.US, "DELETE FROM scheduled_messages_v2 WHERE uid = %d AND mid > 0", dialogId)).stepThis().dispose();
SQLitePreparedStatement state_messages = database.executeFast("REPLACE INTO scheduled_messages_v2 VALUES(?, ?, ?, ?, ?, ?, NULL, 0)");
int count = messages.messages.size();
for (int a = 0; a < count; a++) {
TLRPC.Message message = messages.messages.get(a);
if (message instanceof TLRPC.TL_messageEmpty) {
continue;
}
fixUnsupportedMedia(message);
state_messages.requery();
NativeByteBuffer data = new NativeByteBuffer(message.getObjectSize());
message.serializeToStream(data);
state_messages.bindInteger(1, message.id);
state_messages.bindLong(2, dialogId);
state_messages.bindInteger(3, message.send_state);
state_messages.bindInteger(4, message.date);
state_messages.bindByteBuffer(5, data);
state_messages.bindInteger(6, message.ttl);
state_messages.step();
data.reuse();
}
state_messages.dispose();
putUsersInternal(messages.users);
putChatsInternal(messages.chats);
database.commitTransaction();
broadcastScheduledMessagesChange(dialogId);
} else {
int mentionCountUpdate = Integer.MAX_VALUE;
if (messages.messages.isEmpty()) {
if (load_type == 0) {
doneHolesInTable("messages_holes", dialogId, max_id);
doneHolesInMedia(dialogId, max_id, -1);
}
return;
}
database.beginTransaction();
if (load_type == 0) {
int minId = messages.messages.get(messages.messages.size() - 1).id;
closeHolesInTable("messages_holes", dialogId, minId, max_id);
closeHolesInMedia(dialogId, minId, max_id, -1);
} else if (load_type == 1) {
int maxId = messages.messages.get(0).id;
closeHolesInTable("messages_holes", dialogId, max_id, maxId);
closeHolesInMedia(dialogId, max_id, maxId, -1);
} else if (load_type == 3 || load_type == 2 || load_type == 4) {
int maxId = max_id == 0 && load_type != 4 ? Integer.MAX_VALUE : messages.messages.get(0).id;
int minId = messages.messages.get(messages.messages.size() - 1).id;
closeHolesInTable("messages_holes", dialogId, minId, maxId);
closeHolesInMedia(dialogId, minId, maxId, -1);
}
int count = messages.messages.size();
// load_type == 0 ? backward loading
// load_type == 1 ? forward loading
// load_type == 2 ? load from first unread
// load_type == 3 ? load around message
// load_type == 4 ? load around date
ArrayList<File> filesToDelete = new ArrayList<>();
ArrayList<String> namesToDelete = new ArrayList<>();
ArrayList<Pair<Long, Integer>> idsToDelete = new ArrayList<>();
SQLitePreparedStatement state_messages = database.executeFast("REPLACE INTO messages_v2 VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, 0)");
SQLitePreparedStatement state_media = database.executeFast("REPLACE INTO media_v4 VALUES(?, ?, ?, ?, ?)");
SQLitePreparedStatement state_polls = null;
SQLitePreparedStatement state_webpage = null;
SQLitePreparedStatement state_tasks = null;
int minDeleteTime = Integer.MAX_VALUE;
TLRPC.Message botKeyboard = null;
long channelId = 0;
for (int a = 0; a < count; a++) {
TLRPC.Message message = messages.messages.get(a);
if (channelId == 0) {
channelId = message.peer_id.channel_id;
}
if (load_type == -2) {
SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT mid, data, ttl, mention, read_state, send_state FROM messages_v2 WHERE mid = %d AND uid = %d", message.id, MessageObject.getDialogId(message)));
boolean exist;
if (exist = cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(1);
if (data != null) {
TLRPC.Message oldMessage = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
oldMessage.readAttachPath(data, getUserConfig().clientUserId);
data.reuse();
int send_state = cursor.intValue(5);
if (send_state != 3) {
if (MessageObject.getFileName(oldMessage).equals(MessageObject.getFileName(message))) {
message.attachPath = oldMessage.attachPath;
}
message.ttl = cursor.intValue(2);
}
boolean sameMedia = false;
if (oldMessage.media instanceof TLRPC.TL_messageMediaPhoto && message.media instanceof TLRPC.TL_messageMediaPhoto && oldMessage.media.photo != null && message.media.photo != null) {
sameMedia = oldMessage.media.photo.id == message.media.photo.id;
} else if (oldMessage.media instanceof TLRPC.TL_messageMediaDocument && message.media instanceof TLRPC.TL_messageMediaDocument && oldMessage.media.document != null && message.media.document != null) {
sameMedia = oldMessage.media.document.id == message.media.document.id;
}
if (!sameMedia) {
addFilesToDelete(oldMessage, filesToDelete, idsToDelete, namesToDelete, false);
}
}
boolean oldMention = cursor.intValue(3) != 0;
int readState = cursor.intValue(4);
if (oldMention != message.mentioned) {
if (mentionCountUpdate == Integer.MAX_VALUE) {
SQLiteCursor cursor2 = database.queryFinalized("SELECT unread_count_i FROM dialogs WHERE did = " + dialogId);
if (cursor2.next()) {
mentionCountUpdate = cursor2.intValue(0);
}
cursor2.dispose();
}
if (oldMention) {
if (readState <= 1) {
mentionCountUpdate--;
}
} else {
if (message.media_unread) {
mentionCountUpdate++;
}
}
}
}
cursor.dispose();
if (!exist) {
continue;
}
}
if (a == 0 && createDialog) {
int pinned = 0;
int mentions = 0;
int flags = 0;
SQLiteCursor cursor = database.queryFinalized("SELECT pinned, unread_count_i, flags FROM dialogs WHERE did = " + dialogId);
boolean exist;
if (exist = cursor.next()) {
pinned = cursor.intValue(0);
mentions = cursor.intValue(1);
flags = cursor.intValue(2);
}
cursor.dispose();
SQLitePreparedStatement state3;
if (exist) {
state3 = database.executeFast("UPDATE dialogs SET date = ?, last_mid = ?, inbox_max = ?, last_mid_i = ?, pts = ?, date_i = ? WHERE did = ?");
state3.bindInteger(1, message.date);
state3.bindInteger(2, message.id);
state3.bindInteger(3, message.id);
state3.bindInteger(4, message.id);
state3.bindInteger(5, messages.pts);
state3.bindInteger(6, message.date);
state3.bindLong(7, dialogId);
} else {
state3 = database.executeFast("REPLACE INTO dialogs VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
state3.bindLong(1, dialogId);
state3.bindInteger(2, message.date);
state3.bindInteger(3, 0);
state3.bindInteger(4, message.id);
state3.bindInteger(5, message.id);
state3.bindInteger(6, 0);
state3.bindInteger(7, message.id);
state3.bindInteger(8, mentions);
state3.bindInteger(9, messages.pts);
state3.bindInteger(10, message.date);
state3.bindInteger(11, pinned);
state3.bindInteger(12, flags);
state3.bindInteger(13, -1);
state3.bindNull(14);
state3.bindInteger(15, 0);
unknownDialogsIds.put(dialogId, true);
}
state3.step();
state3.dispose();
}
fixUnsupportedMedia(message);
state_messages.requery();
NativeByteBuffer data = new NativeByteBuffer(message.getObjectSize());
message.serializeToStream(data);
state_messages.bindInteger(1, message.id);
state_messages.bindLong(2, dialogId);
state_messages.bindInteger(3, MessageObject.getUnreadFlags(message));
state_messages.bindInteger(4, message.send_state);
state_messages.bindInteger(5, message.date);
state_messages.bindByteBuffer(6, data);
state_messages.bindInteger(7, (MessageObject.isOut(message) || message.from_scheduled ? 1 : 0));
state_messages.bindInteger(8, message.ttl);
if ((message.flags & TLRPC.MESSAGE_FLAG_HAS_VIEWS) != 0) {
state_messages.bindInteger(9, message.views);
} else {
state_messages.bindInteger(9, getMessageMediaType(message));
}
int flags = 0;
if (message.stickerVerified == 0) {
flags |= 1;
} else if (message.stickerVerified == 2) {
flags |= 2;
}
state_messages.bindInteger(10, flags);
state_messages.bindInteger(11, message.mentioned ? 1 : 0);
state_messages.bindInteger(12, message.forwards);
NativeByteBuffer repliesData = null;
if (message.replies != null) {
repliesData = new NativeByteBuffer(message.replies.getObjectSize());
message.replies.serializeToStream(repliesData);
state_messages.bindByteBuffer(13, repliesData);
} else {
state_messages.bindNull(13);
}
if (message.reply_to != null) {
state_messages.bindInteger(14, message.reply_to.reply_to_top_id != 0 ? message.reply_to.reply_to_top_id : message.reply_to.reply_to_msg_id);
} else {
state_messages.bindInteger(14, 0);
}
state_messages.bindLong(15, MessageObject.getChannelId(message));
state_messages.step();
if (MediaDataController.canAddMessageToMedia(message)) {
state_media.requery();
state_media.bindInteger(1, message.id);
state_media.bindLong(2, dialogId);
state_media.bindInteger(3, message.date);
state_media.bindInteger(4, MediaDataController.getMediaType(message));
state_media.bindByteBuffer(5, data);
state_media.step();
} else if (message instanceof TLRPC.TL_messageService && message.action instanceof TLRPC.TL_messageActionHistoryClear) {
try {
database.executeFast(String.format(Locale.US, "DELETE FROM media_v4 WHERE mid = %d AND uid = %d", message.id, dialogId)).stepThis().dispose();
database.executeFast("DELETE FROM media_counts_v2 WHERE uid = " + dialogId).stepThis().dispose();
} catch (Exception e2) {
FileLog.e(e2);
}
}
if (repliesData != null) {
repliesData.reuse();
}
data.reuse();
if (message.ttl_period != 0 && message.id > 0) {
if (state_tasks == null) {
state_tasks = database.executeFast("REPLACE INTO enc_tasks_v4 VALUES(?, ?, ?, ?)");
}
state_tasks.requery();
state_tasks.bindInteger(1, message.id);
state_tasks.bindLong(2, message.dialog_id);
state_tasks.bindInteger(3, message.date + message.ttl_period);
state_tasks.bindInteger(4, 0);
state_tasks.step();
minDeleteTime = Math.min(minDeleteTime, message.date + message.ttl_period);
}
if (message.media instanceof TLRPC.TL_messageMediaPoll) {
if (state_polls == null) {
state_polls = database.executeFast("REPLACE INTO polls_v2 VALUES(?, ?, ?)");
}
TLRPC.TL_messageMediaPoll mediaPoll = (TLRPC.TL_messageMediaPoll) message.media;
state_polls.requery();
state_polls.bindInteger(1, message.id);
state_polls.bindLong(2, message.dialog_id);
state_polls.bindLong(3, mediaPoll.poll.id);
state_polls.step();
} else if (message.media instanceof TLRPC.TL_messageMediaWebPage) {
if (state_webpage == null) {
state_webpage = database.executeFast("REPLACE INTO webpage_pending_v2 VALUES(?, ?, ?)");
}
state_webpage.requery();
state_webpage.bindLong(1, message.media.webpage.id);
state_webpage.bindInteger(2, message.id);
state_webpage.bindLong(3, message.dialog_id);
state_webpage.step();
}
if (load_type == 0 && isValidKeyboardToSave(message)) {
if (botKeyboard == null || botKeyboard.id < message.id) {
botKeyboard = message;
}
}
}
state_messages.dispose();
state_media.dispose();
if (state_webpage != null) {
state_webpage.dispose();
}
if (state_tasks != null) {
state_tasks.dispose();
getMessagesController().didAddedNewTask(minDeleteTime, 0, null);
}
if (state_polls != null) {
state_polls.dispose();
}
if (botKeyboard != null) {
getMediaDataController().putBotKeyboard(dialogId, botKeyboard);
}
deleteFromDownloadQueue(idsToDelete, false);
AndroidUtilities.runOnUIThread(() -> getFileLoader().cancelLoadFiles(namesToDelete));
getFileLoader().deleteFiles(filesToDelete, 0);
putUsersInternal(messages.users);
putChatsInternal(messages.chats);
if (mentionCountUpdate != Integer.MAX_VALUE) {
database.executeFast(String.format(Locale.US, "UPDATE dialogs SET unread_count_i = %d WHERE did = %d", mentionCountUpdate, dialogId)).stepThis().dispose();
LongSparseIntArray sparseArray = new LongSparseIntArray(1);
sparseArray.put(dialogId, mentionCountUpdate);
getMessagesController().processDialogsUpdateRead(null, sparseArray);
}
database.commitTransaction();
if (createDialog) {
updateDialogsWithDeletedMessages(dialogId, channelId, new ArrayList<>(), null, false);
}
}
} catch (Exception e) {
FileLog.e(e);
}
});
}
use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method openDatabase.
public void openDatabase(int openTries) {
File filesDir = ApplicationLoader.getFilesDirFixed();
if (currentAccount != 0) {
filesDir = new File(filesDir, "account" + currentAccount + "/");
filesDir.mkdirs();
}
cacheFile = new File(filesDir, "cache4.db");
walCacheFile = new File(filesDir, "cache4.db-wal");
shmCacheFile = new File(filesDir, "cache4.db-shm");
boolean createTable = false;
if (!cacheFile.exists()) {
createTable = true;
}
try {
database = new SQLiteDatabase(cacheFile.getPath());
database.executeFast("PRAGMA secure_delete = ON").stepThis().dispose();
database.executeFast("PRAGMA temp_store = MEMORY").stepThis().dispose();
database.executeFast("PRAGMA journal_mode = WAL").stepThis().dispose();
database.executeFast("PRAGMA journal_size_limit = 10485760").stepThis().dispose();
if (createTable) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("create new database");
}
database.executeFast("CREATE TABLE messages_holes(uid INTEGER, start INTEGER, end INTEGER, PRIMARY KEY(uid, start));").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS uid_end_messages_holes ON messages_holes(uid, end);").stepThis().dispose();
database.executeFast("CREATE TABLE media_holes_v2(uid INTEGER, type INTEGER, start INTEGER, end INTEGER, PRIMARY KEY(uid, type, start));").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS uid_end_media_holes_v2 ON media_holes_v2(uid, type, end);").stepThis().dispose();
database.executeFast("CREATE TABLE scheduled_messages_v2(mid INTEGER, uid INTEGER, send_state INTEGER, date INTEGER, data BLOB, ttl INTEGER, replydata BLOB, reply_to_message_id INTEGER, PRIMARY KEY(mid, uid))").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS send_state_idx_scheduled_messages_v2 ON scheduled_messages_v2(mid, send_state, date);").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS uid_date_idx_scheduled_messages_v2 ON scheduled_messages_v2(uid, date);").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS reply_to_idx_scheduled_messages_v2 ON scheduled_messages_v2(mid, reply_to_message_id);").stepThis().dispose();
database.executeFast("CREATE TABLE messages_v2(mid INTEGER, uid INTEGER, read_state INTEGER, send_state INTEGER, date INTEGER, data BLOB, out INTEGER, ttl INTEGER, media INTEGER, replydata BLOB, imp INTEGER, mention INTEGER, forwards INTEGER, replies_data BLOB, thread_reply_id INTEGER, is_channel INTEGER, reply_to_message_id INTEGER, PRIMARY KEY(mid, uid))").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS uid_mid_read_out_idx_messages_v2 ON messages_v2(uid, mid, read_state, out);").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS uid_date_mid_idx_messages_v2 ON messages_v2(uid, date, mid);").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS mid_out_idx_messages_v2 ON messages_v2(mid, out);").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS task_idx_messages_v2 ON messages_v2(uid, out, read_state, ttl, date, send_state);").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS send_state_idx_messages_v2 ON messages_v2(mid, send_state, date);").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS uid_mention_idx_messages_v2 ON messages_v2(uid, mention, read_state);").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS is_channel_idx_messages_v2 ON messages_v2(mid, is_channel);").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS reply_to_idx_messages_v2 ON messages_v2(mid, reply_to_message_id);").stepThis().dispose();
database.executeFast("CREATE TABLE download_queue(uid INTEGER, type INTEGER, date INTEGER, data BLOB, parent TEXT, PRIMARY KEY (uid, type));").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS type_date_idx_download_queue ON download_queue(type, date);").stepThis().dispose();
database.executeFast("CREATE TABLE user_contacts_v7(key TEXT PRIMARY KEY, uid INTEGER, fname TEXT, sname TEXT, imported INTEGER)").stepThis().dispose();
database.executeFast("CREATE TABLE user_phones_v7(key TEXT, phone TEXT, sphone TEXT, deleted INTEGER, PRIMARY KEY (key, phone))").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS sphone_deleted_idx_user_phones ON user_phones_v7(sphone, deleted);").stepThis().dispose();
database.executeFast("CREATE TABLE dialogs(did INTEGER PRIMARY KEY, date INTEGER, unread_count INTEGER, last_mid INTEGER, inbox_max INTEGER, outbox_max INTEGER, last_mid_i INTEGER, unread_count_i INTEGER, pts INTEGER, date_i INTEGER, pinned INTEGER, flags INTEGER, folder_id INTEGER, data BLOB, unread_reactions INTEGER)").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS date_idx_dialogs ON dialogs(date);").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS last_mid_idx_dialogs ON dialogs(last_mid);").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS unread_count_idx_dialogs ON dialogs(unread_count);").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS last_mid_i_idx_dialogs ON dialogs(last_mid_i);").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS unread_count_i_idx_dialogs ON dialogs(unread_count_i);").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS folder_id_idx_dialogs ON dialogs(folder_id);").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS flags_idx_dialogs ON dialogs(flags);").stepThis().dispose();
database.executeFast("CREATE TABLE dialog_filter(id INTEGER PRIMARY KEY, ord INTEGER, unread_count INTEGER, flags INTEGER, title TEXT)").stepThis().dispose();
database.executeFast("CREATE TABLE dialog_filter_ep(id INTEGER, peer INTEGER, PRIMARY KEY (id, peer))").stepThis().dispose();
database.executeFast("CREATE TABLE dialog_filter_pin_v2(id INTEGER, peer INTEGER, pin INTEGER, PRIMARY KEY (id, peer))").stepThis().dispose();
database.executeFast("CREATE TABLE randoms_v2(random_id INTEGER, mid INTEGER, uid INTEGER, PRIMARY KEY (random_id, mid, uid))").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS mid_idx_randoms_v2 ON randoms_v2(mid, uid);").stepThis().dispose();
database.executeFast("CREATE TABLE enc_tasks_v4(mid INTEGER, uid INTEGER, date INTEGER, media INTEGER, PRIMARY KEY(mid, uid, media))").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS date_idx_enc_tasks_v4 ON enc_tasks_v4(date);").stepThis().dispose();
database.executeFast("CREATE TABLE messages_seq(mid INTEGER PRIMARY KEY, seq_in INTEGER, seq_out INTEGER);").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS seq_idx_messages_seq ON messages_seq(seq_in, seq_out);").stepThis().dispose();
database.executeFast("CREATE TABLE params(id INTEGER PRIMARY KEY, seq INTEGER, pts INTEGER, date INTEGER, qts INTEGER, lsv INTEGER, sg INTEGER, pbytes BLOB)").stepThis().dispose();
database.executeFast("INSERT INTO params VALUES(1, 0, 0, 0, 0, 0, 0, NULL)").stepThis().dispose();
database.executeFast("CREATE TABLE media_v4(mid INTEGER, uid INTEGER, date INTEGER, type INTEGER, data BLOB, PRIMARY KEY(mid, uid, type))").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS uid_mid_type_date_idx_media_v4 ON media_v4(uid, mid, type, date);").stepThis().dispose();
database.executeFast("CREATE TABLE bot_keyboard(uid INTEGER PRIMARY KEY, mid INTEGER, info BLOB)").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS bot_keyboard_idx_mid_v2 ON bot_keyboard(mid, uid);").stepThis().dispose();
database.executeFast("CREATE TABLE chat_settings_v2(uid INTEGER PRIMARY KEY, info BLOB, pinned INTEGER, online INTEGER, inviter INTEGER, links INTEGER)").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS chat_settings_pinned_idx ON chat_settings_v2(uid, pinned) WHERE pinned != 0;").stepThis().dispose();
database.executeFast("CREATE TABLE user_settings(uid INTEGER PRIMARY KEY, info BLOB, pinned INTEGER)").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS user_settings_pinned_idx ON user_settings(uid, pinned) WHERE pinned != 0;").stepThis().dispose();
database.executeFast("CREATE TABLE chat_pinned_v2(uid INTEGER, mid INTEGER, data BLOB, PRIMARY KEY (uid, mid));").stepThis().dispose();
database.executeFast("CREATE TABLE chat_pinned_count(uid INTEGER PRIMARY KEY, count INTEGER, end INTEGER);").stepThis().dispose();
database.executeFast("CREATE TABLE chat_hints(did INTEGER, type INTEGER, rating REAL, date INTEGER, PRIMARY KEY(did, type))").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS chat_hints_rating_idx ON chat_hints(rating);").stepThis().dispose();
database.executeFast("CREATE TABLE botcache(id TEXT PRIMARY KEY, date INTEGER, data BLOB)").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS botcache_date_idx ON botcache(date);").stepThis().dispose();
database.executeFast("CREATE TABLE users_data(uid INTEGER PRIMARY KEY, about TEXT)").stepThis().dispose();
database.executeFast("CREATE TABLE users(uid INTEGER PRIMARY KEY, name TEXT, status INTEGER, data BLOB)").stepThis().dispose();
database.executeFast("CREATE TABLE chats(uid INTEGER PRIMARY KEY, name TEXT, data BLOB)").stepThis().dispose();
database.executeFast("CREATE TABLE enc_chats(uid INTEGER PRIMARY KEY, user INTEGER, name TEXT, data BLOB, g BLOB, authkey BLOB, ttl INTEGER, layer INTEGER, seq_in INTEGER, seq_out INTEGER, use_count INTEGER, exchange_id INTEGER, key_date INTEGER, fprint INTEGER, fauthkey BLOB, khash BLOB, in_seq_no INTEGER, admin_id INTEGER, mtproto_seq INTEGER)").stepThis().dispose();
database.executeFast("CREATE TABLE channel_users_v2(did INTEGER, uid INTEGER, date INTEGER, data BLOB, PRIMARY KEY(did, uid))").stepThis().dispose();
database.executeFast("CREATE TABLE channel_admins_v3(did INTEGER, uid INTEGER, data BLOB, PRIMARY KEY(did, uid))").stepThis().dispose();
database.executeFast("CREATE TABLE contacts(uid INTEGER PRIMARY KEY, mutual INTEGER)").stepThis().dispose();
database.executeFast("CREATE TABLE user_photos(uid INTEGER, id INTEGER, data BLOB, PRIMARY KEY (uid, id))").stepThis().dispose();
database.executeFast("CREATE TABLE dialog_settings(did INTEGER PRIMARY KEY, flags INTEGER);").stepThis().dispose();
database.executeFast("CREATE TABLE web_recent_v3(id TEXT, type INTEGER, image_url TEXT, thumb_url TEXT, local_url TEXT, width INTEGER, height INTEGER, size INTEGER, date INTEGER, document BLOB, PRIMARY KEY (id, type));").stepThis().dispose();
database.executeFast("CREATE TABLE stickers_v2(id INTEGER PRIMARY KEY, data BLOB, date INTEGER, hash INTEGER);").stepThis().dispose();
database.executeFast("CREATE TABLE stickers_featured(id INTEGER PRIMARY KEY, data BLOB, unread BLOB, date INTEGER, hash INTEGER);").stepThis().dispose();
database.executeFast("CREATE TABLE stickers_dice(emoji TEXT PRIMARY KEY, data BLOB, date INTEGER);").stepThis().dispose();
database.executeFast("CREATE TABLE hashtag_recent_v2(id TEXT PRIMARY KEY, date INTEGER);").stepThis().dispose();
database.executeFast("CREATE TABLE webpage_pending_v2(id INTEGER, mid INTEGER, uid INTEGER, PRIMARY KEY (id, mid, uid));").stepThis().dispose();
database.executeFast("CREATE TABLE sent_files_v2(uid TEXT, type INTEGER, data BLOB, parent TEXT, PRIMARY KEY (uid, type))").stepThis().dispose();
database.executeFast("CREATE TABLE search_recent(did INTEGER PRIMARY KEY, date INTEGER);").stepThis().dispose();
database.executeFast("CREATE TABLE media_counts_v2(uid INTEGER, type INTEGER, count INTEGER, old INTEGER, PRIMARY KEY(uid, type))").stepThis().dispose();
database.executeFast("CREATE TABLE keyvalue(id TEXT PRIMARY KEY, value TEXT)").stepThis().dispose();
database.executeFast("CREATE TABLE bot_info_v2(uid INTEGER, dialogId INTEGER, info BLOB, PRIMARY KEY(uid, dialogId))").stepThis().dispose();
database.executeFast("CREATE TABLE pending_tasks(id INTEGER PRIMARY KEY, data BLOB);").stepThis().dispose();
database.executeFast("CREATE TABLE requested_holes(uid INTEGER, seq_out_start INTEGER, seq_out_end INTEGER, PRIMARY KEY (uid, seq_out_start, seq_out_end));").stepThis().dispose();
database.executeFast("CREATE TABLE sharing_locations(uid INTEGER PRIMARY KEY, mid INTEGER, date INTEGER, period INTEGER, message BLOB, proximity INTEGER);").stepThis().dispose();
database.executeFast("CREATE TABLE shortcut_widget(id INTEGER, did INTEGER, ord INTEGER, PRIMARY KEY (id, did));").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS shortcut_widget_did ON shortcut_widget(did);").stepThis().dispose();
database.executeFast("CREATE TABLE emoji_keywords_v2(lang TEXT, keyword TEXT, emoji TEXT, PRIMARY KEY(lang, keyword, emoji));").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS emoji_keywords_v2_keyword ON emoji_keywords_v2(keyword);").stepThis().dispose();
database.executeFast("CREATE TABLE emoji_keywords_info_v2(lang TEXT PRIMARY KEY, alias TEXT, version INTEGER, date INTEGER);").stepThis().dispose();
database.executeFast("CREATE TABLE wallpapers2(uid INTEGER PRIMARY KEY, data BLOB, num INTEGER)").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS wallpapers_num ON wallpapers2(num);").stepThis().dispose();
database.executeFast("CREATE TABLE unread_push_messages(uid INTEGER, mid INTEGER, random INTEGER, date INTEGER, data BLOB, fm TEXT, name TEXT, uname TEXT, flags INTEGER, PRIMARY KEY(uid, mid))").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS unread_push_messages_idx_date ON unread_push_messages(date);").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS unread_push_messages_idx_random ON unread_push_messages(random);").stepThis().dispose();
database.executeFast("CREATE TABLE polls_v2(mid INTEGER, uid INTEGER, id INTEGER, PRIMARY KEY (mid, uid));").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS polls_id_v2 ON polls_v2(id);").stepThis().dispose();
database.executeFast("CREATE TABLE reactions(data BLOB, hash INTEGER, date INTEGER);").stepThis().dispose();
database.executeFast("CREATE TABLE reaction_mentions(message_id INTEGER, state INTEGER, dialog_id INTEGER, PRIMARY KEY(message_id, dialog_id))").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS reaction_mentions_did ON reaction_mentions(dialog_id);").stepThis().dispose();
// version
database.executeFast("PRAGMA user_version = " + LAST_DB_VERSION).stepThis().dispose();
} else {
int version = database.executeInt("PRAGMA user_version");
if (BuildVars.LOGS_ENABLED) {
FileLog.d("current db version = " + version);
}
if (version == 0) {
throw new Exception("malformed");
}
try {
SQLiteCursor cursor = database.queryFinalized("SELECT seq, pts, date, qts, lsv, sg, pbytes FROM params WHERE id = 1");
if (cursor.next()) {
lastSeqValue = cursor.intValue(0);
lastPtsValue = cursor.intValue(1);
lastDateValue = cursor.intValue(2);
lastQtsValue = cursor.intValue(3);
lastSecretVersion = cursor.intValue(4);
secretG = cursor.intValue(5);
if (cursor.isNull(6)) {
secretPBytes = null;
} else {
secretPBytes = cursor.byteArrayValue(6);
if (secretPBytes != null && secretPBytes.length == 1) {
secretPBytes = null;
}
}
}
cursor.dispose();
} catch (Exception e) {
if (e.getMessage() != null && e.getMessage().contains("malformed")) {
throw new RuntimeException("malformed");
}
FileLog.e(e);
try {
database.executeFast("CREATE TABLE IF NOT EXISTS params(id INTEGER PRIMARY KEY, seq INTEGER, pts INTEGER, date INTEGER, qts INTEGER, lsv INTEGER, sg INTEGER, pbytes BLOB)").stepThis().dispose();
database.executeFast("INSERT INTO params VALUES(1, 0, 0, 0, 0, 0, 0, NULL)").stepThis().dispose();
} catch (Exception e2) {
FileLog.e(e2);
}
}
if (version < LAST_DB_VERSION) {
try {
updateDbToLastVersion(version);
} catch (Exception e) {
if (BuildVars.DEBUG_PRIVATE_VERSION) {
throw e;
}
FileLog.e(e);
throw new RuntimeException("malformed");
}
}
}
} catch (Exception e) {
FileLog.e(e);
if (BuildVars.DEBUG_PRIVATE_VERSION) {
throw new RuntimeException(e);
}
if (openTries < 3 && e.getMessage() != null && e.getMessage().contains("malformed")) {
if (openTries == 2) {
cleanupInternal(true);
for (int a = 0; a < 2; a++) {
getUserConfig().setDialogsLoadOffset(a, 0, 0, 0, 0, 0, 0);
getUserConfig().setTotalDialogsCount(a, 0);
}
getUserConfig().saveConfig(false);
} else {
cleanupInternal(false);
}
openDatabase(openTries == 1 ? 2 : 3);
}
}
AndroidUtilities.runOnUIThread(() -> {
if (databaseMigrationInProgress) {
databaseMigrationInProgress = false;
NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.onDatabaseMigration, false);
}
});
loadDialogFilters();
loadUnreadMessages();
loadPendingTasks();
try {
openSync.countDown();
} catch (Throwable ignore) {
}
}
Aggregations