add and use MESSAGE_TABLE const in sql.js

pull/1387/head
Audric Ackermann 5 years ago
parent 5a5d1d60c6
commit 5c8c457282
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -624,7 +624,7 @@ async function updateToSchemaVersion8(currentVersion, instance) {
`); `);
await instance.run(` await instance.run(`
INSERT INTO messages_fts(id, body) INSERT INTO messages_fts(id, body)
SELECT id, body FROM messages; SELECT id, body FROM ${MESSAGES_TABLE};
`); `);
// Then we set up triggers to keep the full-text search table up to date // Then we set up triggers to keep the full-text search table up to date
@ -1019,7 +1019,7 @@ async function updateToLokiSchemaVersion6(currentVersion, instance) {
// Remove RSS Feed conversations // Remove RSS Feed conversations
await instance.run( await instance.run(
`DELETE FROM conversations WHERE `DELETE FROM ${CONVERSATIONS_TABLE} WHERE
type = 'group' AND type = 'group' AND
id LIKE 'rss://%';` id LIKE 'rss://%';`
); );
@ -1811,6 +1811,7 @@ async function updateSwarmNodesForPubkey(pubkey, snodeEdKeys) {
} }
const CONVERSATIONS_TABLE = 'conversations'; const CONVERSATIONS_TABLE = 'conversations';
const MESSAGES_TABLE = 'messages';
async function getConversationCount() { async function getConversationCount() {
const row = await db.get(`SELECT count(*) from ${CONVERSATIONS_TABLE};`); const row = await db.get(`SELECT count(*) from ${CONVERSATIONS_TABLE};`);
@ -2001,7 +2002,7 @@ async function getAllConversationIds() {
async function getAllPublicConversations() { async function getAllPublicConversations() {
const rows = await db.all( const rows = await db.all(
`SELECT json FROM conversations WHERE `SELECT json FROM ${CONVERSATIONS_TABLE} WHERE
type = 'group' AND type = 'group' AND
id LIKE 'publicChat:%' id LIKE 'publicChat:%'
ORDER BY id ASC;` ORDER BY id ASC;`
@ -2012,7 +2013,7 @@ async function getAllPublicConversations() {
async function getPublicConversationsByServer(server) { async function getPublicConversationsByServer(server) {
const rows = await db.all( const rows = await db.all(
`SELECT * FROM conversations WHERE `SELECT * FROM ${CONVERSATIONS_TABLE} WHERE
server = $server server = $server
ORDER BY id ASC;`, ORDER BY id ASC;`,
{ {
@ -2025,7 +2026,7 @@ async function getPublicConversationsByServer(server) {
async function getPubkeysInPublicConversation(id) { async function getPubkeysInPublicConversation(id) {
const rows = await db.all( const rows = await db.all(
`SELECT DISTINCT source FROM messages WHERE `SELECT DISTINCT source FROM ${MESSAGES_TABLE} WHERE
conversationId = $conversationId conversationId = $conversationId
ORDER BY id ASC;`, ORDER BY id ASC;`,
{ {
@ -2124,10 +2125,12 @@ async function searchMessagesInConversation(
} }
async function getMessageCount() { async function getMessageCount() {
const row = await db.get('SELECT count(*) from messages;'); const row = await db.get(`SELECT count(*) from ${MESSAGES_TABLE};`);
if (!row) { if (!row) {
throw new Error('getMessageCount: Unable to get count of messages'); throw new Error(
`getMessageCount: Unable to get count of ${MESSAGES_TABLE}`
);
} }
return row['count(*)']; return row['count(*)'];
@ -2359,7 +2362,7 @@ async function saveMessages(arrayOfMessages, { forceSave } = {}) {
async function removeMessage(id) { async function removeMessage(id) {
if (!Array.isArray(id)) { if (!Array.isArray(id)) {
await db.run('DELETE FROM messages WHERE id = $id;', { $id: id }); await db.run(`DELETE FROM ${MESSAGES_TABLE} WHERE id = $id;`, { $id: id });
return; return;
} }
@ -2369,7 +2372,9 @@ async function removeMessage(id) {
// Our node interface doesn't seem to allow you to replace one single ? with an array // Our node interface doesn't seem to allow you to replace one single ? with an array
await db.run( await db.run(
`DELETE FROM messages WHERE id IN ( ${id.map(() => '?').join(', ')} );`, `DELETE FROM ${MESSAGES_TABLE} WHERE id IN ( ${id
.map(() => '?')
.join(', ')} );`,
id id
); );
} }
@ -2391,7 +2396,7 @@ async function getMessageIdsFromServerIds(serverIds, conversationId) {
So we have to use templating to insert the values. So we have to use templating to insert the values.
*/ */
const rows = await db.all( const rows = await db.all(
`SELECT id FROM messages WHERE `SELECT id FROM ${MESSAGES_TABLE} WHERE
serverId IN (${validIds.join(',')}) AND serverId IN (${validIds.join(',')}) AND
conversationId = $conversationId;`, conversationId = $conversationId;`,
{ {
@ -2402,7 +2407,7 @@ async function getMessageIdsFromServerIds(serverIds, conversationId) {
} }
async function getMessageById(id) { async function getMessageById(id) {
const row = await db.get('SELECT * FROM messages WHERE id = $id;', { const row = await db.get(`SELECT * FROM ${MESSAGES_TABLE} WHERE id = $id;`, {
$id: id, $id: id,
}); });
@ -2414,19 +2419,23 @@ async function getMessageById(id) {
} }
async function getAllMessages() { async function getAllMessages() {
const rows = await db.all('SELECT json FROM messages ORDER BY id ASC;'); const rows = await db.all(
`SELECT json FROM ${MESSAGES_TABLE} ORDER BY id ASC;`
);
return map(rows, row => jsonToObject(row.json)); return map(rows, row => jsonToObject(row.json));
} }
async function getAllMessageIds() { async function getAllMessageIds() {
const rows = await db.all('SELECT id FROM messages ORDER BY id ASC;'); const rows = await db.all(
`SELECT id FROM ${MESSAGES_TABLE} ORDER BY id ASC;`
);
return map(rows, row => row.id); return map(rows, row => row.id);
} }
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
async function getMessageBySender({ source, sourceDevice, sent_at }) { async function getMessageBySender({ source, sourceDevice, sent_at }) {
const rows = await db.all( const rows = await db.all(
`SELECT json FROM messages WHERE `SELECT json FROM ${MESSAGES_TABLE} WHERE
source = $source AND source = $source AND
sourceDevice = $sourceDevice AND sourceDevice = $sourceDevice AND
sent_at = $sent_at;`, sent_at = $sent_at;`,
@ -2442,7 +2451,7 @@ async function getMessageBySender({ source, sourceDevice, sent_at }) {
async function getMessagesBySender({ source, sourceDevice }) { async function getMessagesBySender({ source, sourceDevice }) {
const rows = await db.all( const rows = await db.all(
`SELECT json FROM messages WHERE `SELECT json FROM ${MESSAGES_TABLE} WHERE
source = $source AND source = $source AND
sourceDevice = $sourceDevice`, sourceDevice = $sourceDevice`,
{ {
@ -2456,7 +2465,7 @@ async function getMessagesBySender({ source, sourceDevice }) {
async function getAllUnsentMessages() { async function getAllUnsentMessages() {
const rows = await db.all(` const rows = await db.all(`
SELECT json FROM messages WHERE SELECT json FROM ${MESSAGES_TABLE} WHERE
type IN ('outgoing') AND type IN ('outgoing') AND
NOT sent NOT sent
ORDER BY sent_at DESC; ORDER BY sent_at DESC;
@ -2466,7 +2475,7 @@ async function getAllUnsentMessages() {
async function getUnreadByConversation(conversationId) { async function getUnreadByConversation(conversationId) {
const rows = await db.all( const rows = await db.all(
`SELECT json FROM messages WHERE `SELECT json FROM ${MESSAGES_TABLE} WHERE
unread = $unread AND unread = $unread AND
conversationId = $conversationId conversationId = $conversationId
ORDER BY received_at DESC;`, ORDER BY received_at DESC;`,
@ -2486,7 +2495,7 @@ async function getMessagesByConversation(
) { ) {
const rows = await db.all( const rows = await db.all(
` `
SELECT json FROM messages WHERE SELECT json FROM ${MESSAGES_TABLE} WHERE
conversationId = $conversationId AND conversationId = $conversationId AND
received_at < $received_at AND received_at < $received_at AND
type LIKE $type type LIKE $type
@ -2505,7 +2514,7 @@ async function getMessagesByConversation(
async function getMessagesBySentAt(sentAt) { async function getMessagesBySentAt(sentAt) {
const rows = await db.all( const rows = await db.all(
`SELECT * FROM messages `SELECT * FROM ${MESSAGES_TABLE}
WHERE sent_at = $sent_at WHERE sent_at = $sent_at
ORDER BY received_at DESC;`, ORDER BY received_at DESC;`,
{ {
@ -2547,7 +2556,7 @@ async function getExpiredMessages() {
const now = Date.now(); const now = Date.now();
const rows = await db.all( const rows = await db.all(
`SELECT json FROM messages WHERE `SELECT json FROM ${MESSAGES_TABLE} WHERE
expires_at IS NOT NULL AND expires_at IS NOT NULL AND
expires_at <= $expires_at expires_at <= $expires_at
ORDER BY expires_at ASC;`, ORDER BY expires_at ASC;`,
@ -2561,7 +2570,7 @@ async function getExpiredMessages() {
async function getOutgoingWithoutExpiresAt() { async function getOutgoingWithoutExpiresAt() {
const rows = await db.all(` const rows = await db.all(`
SELECT json FROM messages SELECT json FROM ${MESSAGES_TABLE}
WHERE WHERE
expireTimer > 0 AND expireTimer > 0 AND
expires_at IS NULL AND expires_at IS NULL AND
@ -2574,7 +2583,7 @@ async function getOutgoingWithoutExpiresAt() {
async function getNextExpiringMessage() { async function getNextExpiringMessage() {
const rows = await db.all(` const rows = await db.all(`
SELECT json FROM messages SELECT json FROM ${MESSAGES_TABLE}
WHERE expires_at > 0 WHERE expires_at > 0
ORDER BY expires_at ASC ORDER BY expires_at ASC
LIMIT 1; LIMIT 1;
@ -2811,8 +2820,8 @@ async function removeAll() {
promise = Promise.all([ promise = Promise.all([
db.run('BEGIN TRANSACTION;'), db.run('BEGIN TRANSACTION;'),
...getRemoveConfigurationPromises(), ...getRemoveConfigurationPromises(),
db.run('DELETE FROM conversations;'), db.run(`DELETE FROM ${CONVERSATIONS_TABLE};`),
db.run('DELETE FROM messages;'), db.run(`DELETE FROM ${MESSAGES_TABLE};`),
db.run('DELETE FROM attachment_downloads;'), db.run('DELETE FROM attachment_downloads;'),
db.run('DELETE FROM messages_fts;'), db.run('DELETE FROM messages_fts;'),
db.run('COMMIT TRANSACTION;'), db.run('COMMIT TRANSACTION;'),
@ -2865,7 +2874,7 @@ async function removeAllPrivateConversations() {
async function getMessagesNeedingUpgrade(limit, { maxVersion }) { async function getMessagesNeedingUpgrade(limit, { maxVersion }) {
const rows = await db.all( const rows = await db.all(
`SELECT json FROM messages `SELECT json FROM ${MESSAGES_TABLE}
WHERE schemaVersion IS NULL OR schemaVersion < $maxVersion WHERE schemaVersion IS NULL OR schemaVersion < $maxVersion
LIMIT $limit;`, LIMIT $limit;`,
{ {
@ -2882,7 +2891,7 @@ async function getMessagesWithVisualMediaAttachments(
{ limit } { limit }
) { ) {
const rows = await db.all( const rows = await db.all(
`SELECT json FROM messages WHERE `SELECT json FROM ${MESSAGES_TABLE} WHERE
conversationId = $conversationId AND conversationId = $conversationId AND
hasVisualMediaAttachments = 1 hasVisualMediaAttachments = 1
ORDER BY received_at DESC ORDER BY received_at DESC
@ -2898,7 +2907,7 @@ async function getMessagesWithVisualMediaAttachments(
async function getMessagesWithFileAttachments(conversationId, { limit }) { async function getMessagesWithFileAttachments(conversationId, { limit }) {
const rows = await db.all( const rows = await db.all(
`SELECT json FROM messages WHERE `SELECT json FROM ${MESSAGES_TABLE} WHERE
conversationId = $conversationId AND conversationId = $conversationId AND
hasFileAttachments = 1 hasFileAttachments = 1
ORDER BY received_at DESC ORDER BY received_at DESC
@ -2995,7 +3004,7 @@ async function removeKnownAttachments(allAttachments) {
while (!complete) { while (!complete) {
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
const rows = await db.all( const rows = await db.all(
`SELECT json FROM messages `SELECT json FROM ${MESSAGES_TABLE}
WHERE id > $id WHERE id > $id
ORDER BY id ASC ORDER BY id ASC
LIMIT $chunkSize;`, LIMIT $chunkSize;`,
@ -3037,7 +3046,7 @@ async function removeKnownAttachments(allAttachments) {
while (!complete) { while (!complete) {
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
const rows = await db.all( const rows = await db.all(
`SELECT json FROM conversations `SELECT json FROM ${CONVERSATIONS_TABLE}
WHERE id > $id WHERE id > $id
ORDER BY id ASC ORDER BY id ASC
LIMIT $chunkSize;`, LIMIT $chunkSize;`,
@ -3070,7 +3079,7 @@ async function removeKnownAttachments(allAttachments) {
async function getMessagesCountByConversation(instance, conversationId) { async function getMessagesCountByConversation(instance, conversationId) {
const row = await instance.get( const row = await instance.get(
'SELECT count(*) from messages WHERE conversationId = $conversationId;', `SELECT count(*) from ${MESSAGES_TABLE} WHERE conversationId = $conversationId;`,
{ $conversationId: conversationId } { $conversationId: conversationId }
); );
@ -3079,7 +3088,7 @@ async function getMessagesCountByConversation(instance, conversationId) {
async function removePrefixFromGroupConversations(instance) { async function removePrefixFromGroupConversations(instance) {
const rows = await instance.all( const rows = await instance.all(
`SELECT json FROM conversations WHERE `SELECT json FROM ${CONVERSATIONS_TABLE} WHERE
type = 'group' AND type = 'group' AND
id LIKE '__textsecure_group__!%';` id LIKE '__textsecure_group__!%';`
); );

Loading…
Cancel
Save