From 6fc85354e27852e1791062d2ba5c237aab53d279 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Tue, 22 Mar 2022 14:41:31 +0100 Subject: [PATCH] feat(SQL): improve quoting of token count query (#223) --- lib/Queries.js | 9 ++------- query/count_tokens.sql | 3 +++ 2 files changed, 5 insertions(+), 7 deletions(-) create mode 100644 query/count_tokens.sql diff --git a/lib/Queries.js b/lib/Queries.js index 73250e3..3e4dc37 100644 --- a/lib/Queries.js +++ b/lib/Queries.js @@ -142,15 +142,10 @@ module.exports.matchSubjectObject = function( subject, object, cb ){ }; module.exports._hasTooManyCombinations = function(subject, object) { - const terms = [ subject, object ]; + const stmt = this.prepare(query.count_tokens); - const stmt = this.prepare('SELECT count(*) as cnt from fulltext where fulltext.fulltext = ?'); - - const counts = terms.map(function(token) { - return stmt.get(token).cnt; - }); - + const counts = terms.map(token => stmt.get({ token_quoted: `"${token}"` }).cnt); const combinations = counts.reduce((a, b) => a * b, 1); return combinations >= 1e6; diff --git a/query/count_tokens.sql b/query/count_tokens.sql new file mode 100644 index 0000000..ee9edb2 --- /dev/null +++ b/query/count_tokens.sql @@ -0,0 +1,3 @@ +SELECT COUNT(*) AS cnt +FROM fulltext AS ft +WHERE ft.fulltext MATCH $token_quoted