From dfc979a8ef6d633b576ecb25948d21ca338e0161 Mon Sep 17 00:00:00 2001 From: Darshan K <35736874+DarshanBPatel@users.noreply.github.com> Date: Mon, 3 Jun 2024 21:52:53 +0530 Subject: [PATCH] chore: extract common prefixes into a constant for multiple query (#2747) * chore: extract select to constant for multiple prefix * chore: Add a space at the end of selectClause to facilitate better string merging. Signed-off-by: DarshanBPatel --------- Signed-off-by: DarshanBPatel --- .../postgres_driver/postgres_driver.nim | 124 ++++++++---------- 1 file changed, 58 insertions(+), 66 deletions(-) diff --git a/waku/waku_archive/driver/postgres_driver/postgres_driver.nim b/waku/waku_archive/driver/postgres_driver/postgres_driver.nim index 4b646bb72..1a035b2c1 100644 --- a/waku/waku_archive/driver/postgres_driver/postgres_driver.nim +++ b/waku/waku_archive/driver/postgres_driver/postgres_driver.nim @@ -34,88 +34,80 @@ const InsertRowStmtDefinition = # TODO: get the sql queries from a file version, timestamp, meta) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, CASE WHEN $9 = '' THEN NULL ELSE $9 END) ON CONFLICT DO NOTHING;""" const SelectNoCursorAscStmtName = "SelectWithoutCursorAsc" -const SelectNoCursorAscStmtDef = - """SELECT storedAt, contentTopic, payload, pubsubTopic, version, timestamp, id, messageHash, meta FROM messages - WHERE contentTopic IN ($1) AND - messageHash IN ($2) AND - pubsubTopic = $3 AND - storedAt >= $4 AND - storedAt <= $5 - ORDER BY storedAt ASC, messageHash ASC LIMIT $6;""" +const SelectClause = """SELECT storedAt, contentTopic, payload, pubsubTopic, version, timestamp, id, messageHash, meta FROM messages """ +const SelectNoCursorAscStmtDef = SelectClause & """ + WHERE contentTopic IN ($1) AND + messageHash IN ($2) AND + pubsubTopic = $3 AND + storedAt >= $4 AND + storedAt <= $5 + ORDER BY storedAt ASC, messageHash ASC LIMIT $6;""" const SelectNoCursorDescStmtName = "SelectWithoutCursorDesc" -const SelectNoCursorDescStmtDef = - """SELECT storedAt, contentTopic, payload, pubsubTopic, version, timestamp, id, messageHash, meta FROM messages - WHERE contentTopic IN ($1) AND - messageHash IN ($2) AND - pubsubTopic = $3 AND - storedAt >= $4 AND - storedAt <= $5 - ORDER BY storedAt DESC, messageHash DESC LIMIT $6;""" +const SelectNoCursorDescStmtDef = SelectClause & """ + WHERE contentTopic IN ($1) AND + messageHash IN ($2) AND + pubsubTopic = $3 AND + storedAt >= $4 AND + storedAt <= $5 + ORDER BY storedAt DESC, messageHash DESC LIMIT $6;""" const SelectWithCursorDescStmtName = "SelectWithCursorDesc" -const SelectWithCursorDescStmtDef = - """SELECT storedAt, contentTopic, payload, pubsubTopic, version, timestamp, id, messageHash, meta FROM messages - WHERE contentTopic IN ($1) AND - messageHash IN ($2) AND - pubsubTopic = $3 AND - (storedAt, messageHash) < ($4,$5) AND - storedAt >= $6 AND - storedAt <= $7 - ORDER BY storedAt DESC, messageHash DESC LIMIT $8;""" +const SelectWithCursorDescStmtDef = SelectClause & """ + WHERE contentTopic IN ($1) AND + messageHash IN ($2) AND + pubsubTopic = $3 AND + (storedAt, messageHash) < ($4,$5) AND + storedAt >= $6 AND + storedAt <= $7 + ORDER BY storedAt DESC, messageHash DESC LIMIT $8;""" const SelectWithCursorAscStmtName = "SelectWithCursorAsc" -const SelectWithCursorAscStmtDef = - """SELECT storedAt, contentTopic, payload, pubsubTopic, version, timestamp, id, messageHash, meta FROM messages - WHERE contentTopic IN ($1) AND - messageHash IN ($2) AND - pubsubTopic = $3 AND - (storedAt, messageHash) > ($4,$5) AND - storedAt >= $6 AND - storedAt <= $7 - ORDER BY storedAt ASC, messageHash ASC LIMIT $8;""" +const SelectWithCursorAscStmtDef = SelectClause & """ + WHERE contentTopic IN ($1) AND + messageHash IN ($2) AND + pubsubTopic = $3 AND + (storedAt, messageHash) > ($4,$5) AND + storedAt >= $6 AND + storedAt <= $7 + ORDER BY storedAt ASC, messageHash ASC LIMIT $8;""" const SelectMessageByHashName = "SelectMessageByHash" -const SelectMessageByHashDef = - """SELECT storedAt, contentTopic, payload, pubsubTopic, version, timestamp, id, messageHash, meta FROM messages WHERE messageHash = $1""" +const SelectMessageByHashDef = SelectClause & """WHERE messageHash = $1""" const SelectNoCursorV2AscStmtName = "SelectWithoutCursorV2Asc" -const SelectNoCursorV2AscStmtDef = - """SELECT storedAt, contentTopic, payload, pubsubTopic, version, timestamp, id, messageHash, meta FROM messages - WHERE contentTopic IN ($1) AND - pubsubTopic = $2 AND - storedAt >= $3 AND - storedAt <= $4 - ORDER BY storedAt ASC LIMIT $5;""" +const SelectNoCursorV2AscStmtDef = SelectClause & """ + WHERE contentTopic IN ($1) AND + pubsubTopic = $2 AND + storedAt >= $3 AND + storedAt <= $4 + ORDER BY storedAt ASC LIMIT $5;""" const SelectNoCursorV2DescStmtName = "SelectWithoutCursorV2Desc" -const SelectNoCursorV2DescStmtDef = - """SELECT storedAt, contentTopic, payload, pubsubTopic, version, timestamp, id, messageHash, meta FROM messages - WHERE contentTopic IN ($1) AND - pubsubTopic = $2 AND - storedAt >= $3 AND - storedAt <= $4 - ORDER BY storedAt DESC LIMIT $5;""" +const SelectNoCursorV2DescStmtDef = SelectClause & """ + WHERE contentTopic IN ($1) AND + pubsubTopic = $2 AND + storedAt >= $3 AND + storedAt <= $4 + ORDER BY storedAt DESC LIMIT $5;""" const SelectWithCursorV2DescStmtName = "SelectWithCursorV2Desc" -const SelectWithCursorV2DescStmtDef = - """SELECT storedAt, contentTopic, payload, pubsubTopic, version, timestamp, id, messageHash, meta FROM messages - WHERE contentTopic IN ($1) AND - pubsubTopic = $2 AND - (storedAt, id) < ($3,$4) AND - storedAt >= $5 AND - storedAt <= $6 - ORDER BY storedAt DESC LIMIT $7;""" +const SelectWithCursorV2DescStmtDef = SelectClause & """ + WHERE contentTopic IN ($1) AND + pubsubTopic = $2 AND + (storedAt, id) < ($3,$4) AND + storedAt >= $5 AND + storedAt <= $6 + ORDER BY storedAt DESC LIMIT $7;""" const SelectWithCursorV2AscStmtName = "SelectWithCursorV2Asc" -const SelectWithCursorV2AscStmtDef = - """SELECT storedAt, contentTopic, payload, pubsubTopic, version, timestamp, id, messageHash, meta FROM messages - WHERE contentTopic IN ($1) AND - pubsubTopic = $2 AND - (storedAt, id) > ($3,$4) AND - storedAt >= $5 AND - storedAt <= $6 - ORDER BY storedAt ASC LIMIT $7;""" +const SelectWithCursorV2AscStmtDef = SelectClause & """ + WHERE contentTopic IN ($1) AND + pubsubTopic = $2 AND + (storedAt, id) > ($3,$4) AND + storedAt >= $5 AND + storedAt <= $6 + ORDER BY storedAt ASC LIMIT $7;""" const DefaultMaxNumConns = 50