[polari/wip/carlosg/tracker: 444/445] logger: Use SparqlStatement for queries




commit eef9d7db86d13e7a013b97bec9d914ce16ce6a1e
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun Jun 20 13:42:20 2021 +0200

    logger: Use SparqlStatement for queries
    
    There is so far a long running query for each room, and each gets
    a separate statement (which does not help much for query reuse),
    but it's still got the benefit that we don't need to manually escape
    and compose query strings.

 src/logger.js | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
---
diff --git a/src/logger.js b/src/logger.js
index b6ea9ae5..d3201cfa 100644
--- a/src/logger.js
+++ b/src/logger.js
@@ -27,13 +27,24 @@ var GenericQuery  = class {
         });
     }
 
-    run(sparql, cancellable, callback) {
+    run(sparql, args, cancellable, callback) {
         this._task = this._createTask(cancellable, callback);
+        let statement;
 
-        this._connection.query_async(sparql, cancellable, (c, res) => {
+        try {
+            statement = this._connection.query_statement(sparql, cancellable);
+        } catch (e) {
+            this._task.return_error(e);
+            return;
+        }
+
+        for (const name in args)
+            statement.bind_string(name, args[name]);
+
+        statement.execute_async(cancellable, (c, res) => {
             let cursor;
             try {
-                cursor = this._connection.query_finish(res);
+                cursor = statement.execute_finish(res);
             } catch (e) {
                 this._task.return_error(e);
                 return;
@@ -146,7 +157,8 @@ var LogWalker = class {
         if (!this._query) {
             this._query = new GenericQuery(numEvents);
 
-            let channel = 
Tracker.sparql_escape_uri(`urn:channel:${this._room.account.get_path_suffix()}:${this._room.channel_name}`);
+            let channel =
+               `urn:channel:${this._room.account.get_path_suffix()}:${this._room.channel_name}`;
             let sparql = `
                 select polari:text(?msg) as ?text
                        polari:nick(?sender) as ?sender
@@ -157,11 +169,11 @@ var LogWalker = class {
                 { ?msg a polari:Message;
                        polari:time ?time;
                        polari:sender ?sender;
-                       polari:channel <${channel}> .
+                       polari:channel ~channel .
                        bind (exists { ?sender a polari:SelfContact } as ?isSelf)
                 } order by desc(?time) desc(tracker:id(?msg))
             `;
-            this._query.run(sparql, null, returnFunc);
+            this._query.run(sparql, { 'channel': channel }, null, returnFunc);
         } else {
             this._query.next(numEvents, null, returnFunc);
         }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]