Skip to content

Commit

Permalink
Templated queries (#121)
Browse files Browse the repository at this point in the history
Allow comments of the form `#!TEMPLATE PLACEHOLDER=VALUE` in queries. The effect is that all occurrences of `%PLACEHOLDER%` in the query are replaced by `VALUE`. Very simple and useful.
  • Loading branch information
hannahbast authored Dec 10, 2024
1 parent 76f6e5e commit fcab11b
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions backend/static/js/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,16 @@ async function rewriteQuery(query, kwargs = {}) {
// been replaced by constructs know to QLever.
var query_rewritten = rewriteQueryNoAsyncPart(query);

// Look for comments of the form `#!TEMPLATE PLACEHOLDER=VALUE` and replace
// all occurrences of `%PLACEHOLDER%` in the query with `VALUE`.
const template_regex = /#!TEMPLATE\s+([A-Z_]+)\s*=\s*(.*)/g;
while ((match = template_regex.exec(query_rewritten)) != null) {
const placeholder = "%"+ match[1] + "%";
const value = match[2];
console.log("TEMPLATE: " + placeholder + " -> " + value);
query_rewritten = query_rewritten.replace(new RegExp(placeholder, "g"), value);
}

// If certain conditions are met, rewrite query with name service. This asks
// queries to the backend, and is hence asynchronous.
const apply_name_service = kwargs["name_service" == "always"] ||
Expand Down

0 comments on commit fcab11b

Please sign in to comment.