2023-06-29 06:55:59 +00:00
|
|
|
/*\
|
|
|
|
title: $:/plugins/tiddlywiki/sqlite3store/sql-functions.js
|
|
|
|
type: application/javascript
|
|
|
|
|
|
|
|
Functions to perform basic tiddler operations with a sqlite3 database
|
|
|
|
|
|
|
|
This file is spliced into the HTML file to be executed before the boot kernel has been loaded.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
$tw.SqlFunctions = function(options) {
|
|
|
|
options = options || {};
|
|
|
|
var self = this;
|
2023-07-18 18:33:21 +00:00
|
|
|
// Setting useCustomCollation to true allows the tests to pass (run `tiddlywiki editions/test/ --build index`)
|
|
|
|
// - but it takes 6 times longer to boot the prerelease than with useCustomCollation set to false
|
|
|
|
var useCustomCollation = false;
|
|
|
|
var COLLATION_CLAUSE = useCustomCollation ? "COLLATE custom_collation" : "";
|
|
|
|
// Create anonymous database
|
2023-07-06 10:54:03 +00:00
|
|
|
this.db = new $tw.sqlite3.oo1.DB("","c");
|
2023-06-30 09:46:27 +00:00
|
|
|
// Setup custom collation to precisely match existing sort orders
|
|
|
|
// Create field with `title TEXT NOT NULL COLLATE custom_collation`
|
|
|
|
// Use it like `... order by shadow collate custom_collation`
|
2023-07-18 18:33:21 +00:00
|
|
|
if(useCustomCollation) {
|
|
|
|
function customCollation(ptr,lenA,a,lenB,b) {
|
|
|
|
// There may be a problem here: lenA and lenB are the lengths of the two UTF8 strings in bytes,
|
|
|
|
// and yet we're using them with JS slice() method which counts in characters
|
|
|
|
var jsA = $tw.sqlite3.wasm.cstrToJs(a).slice(0,lenA),
|
|
|
|
jsB = $tw.sqlite3.wasm.cstrToJs(b).slice(0,lenB);
|
|
|
|
return jsA.localeCompare(jsB);
|
|
|
|
}
|
|
|
|
var SQLITE_UTF8 = 1; /* IMP: R-37514-35566 */
|
|
|
|
var SQLITE_UTF16LE = 2; /* IMP: R-03371-37637 */
|
|
|
|
var SQLITE_UTF16BE = 3; /* IMP: R-51971-34154 */
|
|
|
|
var SQLITE_UTF16 = 4; /* Use native byte order */
|
|
|
|
var SQLITE_ANY = 5; /* Deprecated */
|
|
|
|
var SQLITE_UTF16_ALIGNED = 8; /* sqlite3_create_collation only */
|
|
|
|
var collationResult = $tw.sqlite3.capi.sqlite3_create_collation_v2(this.db.pointer,"custom_collation",SQLITE_UTF8,this,customCollation,0);
|
2023-07-07 07:20:57 +00:00
|
|
|
}
|
2023-06-29 06:55:59 +00:00
|
|
|
/*
|
|
|
|
Create tables and indexes
|
|
|
|
*/
|
|
|
|
self.db.exec({
|
|
|
|
sql: `
|
2023-10-21 11:00:51 +00:00
|
|
|
DROP TABLE IF EXISTS plugins;
|
|
|
|
CREATE TABLE plugins (
|
2023-10-24 17:41:34 +00:00
|
|
|
plugintitle TEXT NOT NULL, -- Empty string shoud be the highest priority
|
2023-10-21 11:00:51 +00:00
|
|
|
priority INTEGER NOT NULL,
|
|
|
|
PRIMARY KEY(plugintitle)
|
|
|
|
);
|
2023-10-25 10:38:08 +00:00
|
|
|
CREATE INDEX IF NOT EXISTS plugins_plugintitle_index ON plugins(plugintitle);
|
2023-07-18 18:33:21 +00:00
|
|
|
DROP TABLE IF EXISTS tiddlers;
|
|
|
|
CREATE TABLE tiddlers (
|
|
|
|
title TEXT NOT NULL ${COLLATION_CLAUSE},
|
2023-10-24 17:41:34 +00:00
|
|
|
plugintitle TEXT NOT NULL, -- Empty string for tiddlers that are not part of a plugin
|
2023-07-18 18:33:21 +00:00
|
|
|
meta TEXT NOT NULL,
|
|
|
|
text TEXT NOT NULL,
|
2023-10-21 11:00:51 +00:00
|
|
|
PRIMARY KEY(title,plugintitle)
|
2023-07-18 18:33:21 +00:00
|
|
|
);
|
2023-10-25 10:38:08 +00:00
|
|
|
CREATE INDEX IF NOT EXISTS tiddlers_title_index ON tiddlers(title);
|
|
|
|
CREATE INDEX IF NOT EXISTS tiddlers_plugintitle_index ON tiddlers(plugintitle);
|
2023-10-21 11:00:51 +00:00
|
|
|
DROP TABLE IF EXISTS titles;
|
|
|
|
CREATE TABLE titles (
|
|
|
|
title TEXT NOT NULL ${COLLATION_CLAUSE},
|
2023-10-24 17:41:34 +00:00
|
|
|
plugintitle TEXT NOT NULL, -- Empty string for tiddlers that are not part of a plugin
|
2023-10-21 11:00:51 +00:00
|
|
|
PRIMARY KEY(title)
|
2023-07-18 18:33:21 +00:00
|
|
|
);
|
2023-10-25 10:38:08 +00:00
|
|
|
CREATE INDEX IF NOT EXISTS titles_title_index ON titles(title);
|
|
|
|
CREATE INDEX IF NOT EXISTS titles_plugintitle_index ON titles(plugintitle);
|
2023-10-25 10:29:38 +00:00
|
|
|
DROP TABLE IF EXISTS tags;
|
|
|
|
CREATE TABLE tags (
|
|
|
|
tag_id INTEGER PRIMARY KEY,
|
|
|
|
tag TEXT NOT NULL
|
|
|
|
);
|
|
|
|
DROP TABLE IF EXISTS tiddler_tags;
|
|
|
|
CREATE TABLE tiddler_tags (
|
|
|
|
title TEXT NOT NULL,
|
|
|
|
plugintitle INTEGER NOT NULL,
|
|
|
|
tag_id INTEGER NOT NULL,
|
|
|
|
FOREIGN KEY (title, plugintitle) REFERENCES tiddlers (title, plugintitle) ON DELETE CASCADE ON UPDATE CASCADE,
|
|
|
|
FOREIGN KEY (tag_id) REFERENCES tags (tag_id) ON DELETE CASCADE ON UPDATE CASCADE,
|
|
|
|
PRIMARY KEY (title, plugintitle, tag_id)
|
|
|
|
);
|
2023-10-27 09:31:39 +00:00
|
|
|
CREATE INDEX IF NOT EXISTS tiddler_tags_title_index ON tiddler_tags(title);
|
|
|
|
CREATE INDEX IF NOT EXISTS tiddler_tags_plugintitle_index ON tiddler_tags(plugintitle);
|
2023-10-25 10:38:08 +00:00
|
|
|
CREATE INDEX IF NOT EXISTS tiddler_tags_tag_id_index ON tiddler_tags(tag_id);
|
2023-06-29 06:55:59 +00:00
|
|
|
`
|
|
|
|
});
|
|
|
|
/*
|
2023-10-21 11:00:51 +00:00
|
|
|
Debugging
|
|
|
|
*/
|
|
|
|
var statementLogTiddlersTable = self.db.prepare("select title, plugintitle, meta, text from tiddlers order by title, plugintitle;"),
|
2023-10-25 10:29:38 +00:00
|
|
|
statementLogPluginsTable = self.db.prepare("select plugintitle, priority from plugins order by plugintitle;"),
|
|
|
|
statementLogTitlesTable = self.db.prepare("select title, plugintitle from titles order by title;"),
|
|
|
|
statementLogTagsTable = self.db.prepare("select tag_id, tag from tags order by tag_id;"),
|
|
|
|
statementLogTiddlerTagsTable = self.db.prepare("select title, plugintitle, tag_id from tiddler_tags order by title, plugintitle;");
|
2023-10-21 11:00:51 +00:00
|
|
|
function sqlLogTable(statement) {
|
|
|
|
let resultRows = [];
|
|
|
|
while(statement.step()) {
|
|
|
|
var row = statement.get({});
|
|
|
|
resultRows.push(row);
|
|
|
|
}
|
|
|
|
statement.reset();
|
|
|
|
return resultRows;
|
|
|
|
}
|
|
|
|
this.sqlLogTables = function() {
|
|
|
|
console.log("tiddlers",sqlLogTable(statementLogTiddlersTable));
|
|
|
|
console.log("plugins",sqlLogTable(statementLogPluginsTable));
|
|
|
|
console.log("titles",sqlLogTable(statementLogTitlesTable));
|
2023-10-25 10:29:38 +00:00
|
|
|
console.log("tags",sqlLogTable(statementLogTagsTable));
|
|
|
|
console.log("tiddlertags",sqlLogTable(statementLogTiddlerTagsTable));
|
2023-10-21 11:00:51 +00:00
|
|
|
};
|
|
|
|
/*
|
|
|
|
Set the plugin priorities
|
|
|
|
*/
|
|
|
|
this.sqlSetPluginPriorities = function(prioritisedPluginTitles) {
|
2023-10-24 17:41:34 +00:00
|
|
|
const plugintitles = prioritisedPluginTitles.concat([""]);
|
2023-10-21 11:00:51 +00:00
|
|
|
self.db.exec({
|
|
|
|
sql: "DELETE FROM plugins"
|
|
|
|
});
|
|
|
|
let priority = 1;
|
2023-10-24 17:41:34 +00:00
|
|
|
for(const plugintitle of plugintitles) {
|
2023-10-21 11:00:51 +00:00
|
|
|
self.db.exec({
|
|
|
|
sql: "insert or replace into plugins (plugintitle, priority) values ($plugintitle, $priority)",
|
|
|
|
bind: {
|
|
|
|
$plugintitle: plugintitle,
|
|
|
|
$priority: priority++
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
Save a tiddler
|
2023-06-29 06:55:59 +00:00
|
|
|
*/
|
2023-07-18 18:33:21 +00:00
|
|
|
var querySaveTiddlerTableTiddlers = self.db.prepare(`
|
|
|
|
-- Insert the new tiddler into the tiddlers table
|
2023-10-21 11:00:51 +00:00
|
|
|
INSERT OR REPLACE INTO tiddlers (title, plugintitle, meta, text)
|
|
|
|
VALUES ($title, $plugintitle, $meta, $text);
|
2023-07-18 18:33:21 +00:00
|
|
|
`);
|
2023-10-21 11:00:51 +00:00
|
|
|
var querySaveTiddlerTableTitles = self.db.prepare(`
|
|
|
|
-- Insert the new title into the titles table
|
|
|
|
INSERT OR REPLACE INTO titles (title, plugintitle)
|
|
|
|
SELECT
|
|
|
|
t.title,
|
2023-10-24 17:41:34 +00:00
|
|
|
(SELECT t2.plugintitle
|
|
|
|
FROM tiddlers AS t2
|
|
|
|
JOIN plugins AS p ON t2.plugintitle = p.plugintitle
|
|
|
|
WHERE t2.title = t.title
|
|
|
|
ORDER BY p.priority DESC
|
|
|
|
LIMIT 1
|
|
|
|
) AS plugintitle
|
|
|
|
FROM tiddlers AS t
|
|
|
|
WHERE t.title = $title;
|
2023-07-18 18:33:21 +00:00
|
|
|
`);
|
2023-10-25 10:29:38 +00:00
|
|
|
var querySaveTiddlerTableTags = self.db.prepare(`
|
|
|
|
-- Parse and insert tags from the $tags JSON array
|
|
|
|
WITH tag_values AS (
|
|
|
|
SELECT json_each.value AS tag
|
|
|
|
FROM json_each($tags)
|
|
|
|
)
|
|
|
|
INSERT INTO tags (tag)
|
|
|
|
SELECT DISTINCT tag
|
|
|
|
FROM tag_values
|
|
|
|
WHERE tag NOT IN (
|
|
|
|
SELECT tag
|
|
|
|
FROM tags
|
|
|
|
);
|
|
|
|
`);
|
|
|
|
var querySaveTiddlerTableTiddlerTags = self.db.prepare(`
|
|
|
|
-- Associate the new tiddler with the tags in the tiddler_tags table
|
|
|
|
WITH tag_values AS (
|
|
|
|
SELECT json_each.value AS tag
|
|
|
|
FROM json_each($tags)
|
|
|
|
)
|
|
|
|
INSERT OR IGNORE INTO tiddler_tags (title, plugintitle, tag_id)
|
|
|
|
SELECT $title, $plugintitle, tags.tag_id
|
|
|
|
FROM tag_values
|
|
|
|
JOIN tags ON tag_values.tag = tags.tag;
|
|
|
|
`);
|
2023-10-21 11:00:51 +00:00
|
|
|
this.sqlSaveTiddler = function(tiddlerFields,plugintitle) {
|
2023-10-24 17:41:34 +00:00
|
|
|
plugintitle = plugintitle || "";
|
2023-10-25 10:29:38 +00:00
|
|
|
// Normalise the tags by removing any double square brackets
|
|
|
|
let tags = tiddlerFields.tags;
|
|
|
|
if(typeof tags === "string") {
|
|
|
|
tags = $tw.utils.parseStringArray(tags);
|
|
|
|
}
|
|
|
|
const normalisedTags = (tags || []).map(tag => {
|
|
|
|
const match = /^[^\S\xA0]*\[\[(.*)\]\][^\S\xA0]*$/mg.exec(tag);
|
|
|
|
if(match) {
|
|
|
|
return match[1];
|
|
|
|
} else {
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const jsonNormalisedTags = JSON.stringify(normalisedTags);
|
2023-07-18 18:33:21 +00:00
|
|
|
querySaveTiddlerTableTiddlers.bind({
|
2023-06-29 06:55:59 +00:00
|
|
|
$title: tiddlerFields.title,
|
2023-10-24 17:41:34 +00:00
|
|
|
$plugintitle: plugintitle,
|
2023-06-29 06:55:59 +00:00
|
|
|
$meta: JSON.stringify(Object.assign({},tiddlerFields,{title: undefined, text: undefined})),
|
|
|
|
$text: tiddlerFields.text || ""
|
|
|
|
});
|
2023-07-18 18:33:21 +00:00
|
|
|
querySaveTiddlerTableTiddlers.step();
|
|
|
|
querySaveTiddlerTableTiddlers.reset();
|
2023-10-21 11:00:51 +00:00
|
|
|
querySaveTiddlerTableTitles.bind({
|
2023-07-18 18:33:21 +00:00
|
|
|
$title: tiddlerFields.title,
|
|
|
|
});
|
2023-10-21 11:00:51 +00:00
|
|
|
querySaveTiddlerTableTitles.step();
|
|
|
|
querySaveTiddlerTableTitles.reset();
|
2023-10-25 10:29:38 +00:00
|
|
|
querySaveTiddlerTableTags.bind({
|
|
|
|
$tags: jsonNormalisedTags
|
|
|
|
});
|
|
|
|
querySaveTiddlerTableTags.step();
|
|
|
|
querySaveTiddlerTableTags.reset();
|
|
|
|
querySaveTiddlerTableTiddlerTags.bind({
|
|
|
|
$title: tiddlerFields.title,
|
|
|
|
$plugintitle: plugintitle || "",
|
|
|
|
$tags: jsonNormalisedTags
|
|
|
|
});
|
|
|
|
querySaveTiddlerTableTiddlerTags.step();
|
|
|
|
querySaveTiddlerTableTiddlerTags.reset();
|
2023-06-29 06:55:59 +00:00
|
|
|
};
|
2023-10-21 11:00:51 +00:00
|
|
|
/*
|
|
|
|
Delete a tiddler
|
|
|
|
*/
|
|
|
|
var statementDeleteTiddlerDeleteFromTiddlers = self.db.prepare(`
|
|
|
|
DELETE FROM tiddlers
|
2023-10-24 17:41:34 +00:00
|
|
|
WHERE title = $title AND plugintitle = $plugintitle;
|
2023-10-21 11:00:51 +00:00
|
|
|
`);
|
|
|
|
var statementDeleteTiddlerFindShadow = self.db.prepare(`
|
2023-10-24 17:41:34 +00:00
|
|
|
SELECT t.title, t.plugintitle
|
|
|
|
FROM tiddlers AS t
|
|
|
|
JOIN plugins AS p ON t.plugintitle = p.plugintitle
|
|
|
|
WHERE t.title = $title
|
|
|
|
ORDER BY p.priority DESC
|
|
|
|
LIMIT 1;
|
2023-10-21 11:00:51 +00:00
|
|
|
`);
|
2023-10-24 17:41:34 +00:00
|
|
|
this.sqlDeleteTiddler = function(title,plugintitle) {
|
|
|
|
plugintitle = plugintitle || "";
|
2023-10-21 11:00:51 +00:00
|
|
|
// Delete the tiddler from the tiddlers table
|
|
|
|
statementDeleteTiddlerDeleteFromTiddlers.bind({
|
2023-10-24 17:41:34 +00:00
|
|
|
$title: title,
|
|
|
|
$plugintitle: plugintitle
|
2023-06-29 06:55:59 +00:00
|
|
|
});
|
2023-10-21 11:00:51 +00:00
|
|
|
statementDeleteTiddlerDeleteFromTiddlers.step();
|
|
|
|
statementDeleteTiddlerDeleteFromTiddlers.reset();
|
|
|
|
// Find any corresponding shadow tiddler
|
|
|
|
statementDeleteTiddlerFindShadow.bind({
|
|
|
|
$title: title
|
|
|
|
});
|
|
|
|
if(statementDeleteTiddlerFindShadow.step()) {
|
|
|
|
var row = statementDeleteTiddlerFindShadow.get({});
|
2023-10-24 17:41:34 +00:00
|
|
|
statementDeleteTiddlerFindShadow.reset();
|
2023-10-21 11:00:51 +00:00
|
|
|
// Replace the tiddler with the shadow
|
|
|
|
self.db.exec({
|
|
|
|
sql: "insert or replace into titles (title, plugintitle) values ($title, $plugintitle)",
|
|
|
|
bind: {
|
|
|
|
$title: title,
|
|
|
|
$plugintitle: row.plugintitle
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2023-10-24 17:41:34 +00:00
|
|
|
statementDeleteTiddlerFindShadow.reset();
|
|
|
|
// There is no shadow tiddler, so just delete the tiddler
|
2023-10-21 11:00:51 +00:00
|
|
|
self.db.exec({
|
|
|
|
sql: "delete from titles where title = $title",
|
|
|
|
bind: {
|
|
|
|
$title: title
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-06-29 06:55:59 +00:00
|
|
|
};
|
2023-10-21 11:00:51 +00:00
|
|
|
/*
|
|
|
|
Remove all shadow tiddlers
|
|
|
|
*/
|
2023-06-29 06:55:59 +00:00
|
|
|
this.sqlClearShadows = function() {
|
|
|
|
self.db.exec({
|
2023-10-24 17:41:34 +00:00
|
|
|
sql: "delete from tiddlers where plugintitle != '';"
|
2023-10-21 11:00:51 +00:00
|
|
|
});
|
|
|
|
self.db.exec({
|
2023-10-24 17:41:34 +00:00
|
|
|
sql: "delete from titles where plugintitle != '';"
|
2023-06-29 06:55:59 +00:00
|
|
|
});
|
|
|
|
};
|
2023-10-21 11:00:51 +00:00
|
|
|
/*
|
|
|
|
Check whether a tiddler exists
|
|
|
|
*/
|
2023-10-24 17:41:34 +00:00
|
|
|
var statementTiddlerExists = self.db.prepare(`select title from titles where title = $title and plugintitle = '';`)
|
2023-06-29 06:55:59 +00:00
|
|
|
this.sqlTiddlerExists = function(title) {
|
|
|
|
statementTiddlerExists.bind({
|
|
|
|
$title: title
|
|
|
|
});
|
|
|
|
if(statementTiddlerExists.step()) {
|
|
|
|
statementTiddlerExists.reset();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
statementTiddlerExists.reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2023-10-21 11:00:51 +00:00
|
|
|
/*
|
|
|
|
Get the value of a tiddler
|
|
|
|
*/
|
|
|
|
var statementGetTiddler = self.db.prepare(`
|
|
|
|
select t.title, ti.meta, ti.text
|
|
|
|
FROM titles AS t
|
|
|
|
JOIN tiddlers AS ti
|
2023-10-24 17:41:34 +00:00
|
|
|
ON t.title = ti.title AND t.plugintitle = ti.plugintitle
|
2023-10-21 11:00:51 +00:00
|
|
|
WHERE t.title = $title;
|
|
|
|
`);
|
2023-06-29 06:55:59 +00:00
|
|
|
this.sqlGetTiddler = function(title) {
|
|
|
|
statementGetTiddler.bind({
|
|
|
|
$title: title
|
|
|
|
});
|
|
|
|
if(statementGetTiddler.step()) {
|
|
|
|
var row = statementGetTiddler.get({});
|
|
|
|
statementGetTiddler.reset();
|
|
|
|
return Object.assign({},JSON.parse(row.meta),{title: row.title, text: row.text});
|
|
|
|
} else {
|
|
|
|
statementGetTiddler.reset();
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
};
|
2023-10-21 11:00:51 +00:00
|
|
|
/*
|
|
|
|
Get the plugin from which a tiddler came
|
|
|
|
*/
|
|
|
|
var statementGetShadowSource = self.db.prepare(`
|
2023-10-24 17:41:34 +00:00
|
|
|
SELECT t.title, t.plugintitle
|
|
|
|
FROM tiddlers AS t
|
|
|
|
LEFT JOIN plugins AS p ON t.plugintitle = p.plugintitle
|
|
|
|
WHERE t.title = $title AND t.plugintitle <> ''
|
|
|
|
ORDER BY p.priority DESC
|
|
|
|
LIMIT 1;
|
2023-10-21 11:00:51 +00:00
|
|
|
`);
|
2023-06-29 06:55:59 +00:00
|
|
|
this.sqlGetShadowSource = function(title) {
|
|
|
|
statementGetShadowSource.bind({
|
|
|
|
$title: title
|
|
|
|
});
|
|
|
|
if(statementGetShadowSource.step()) {
|
|
|
|
var row = statementGetShadowSource.get({});
|
|
|
|
statementGetShadowSource.reset();
|
2023-10-21 11:00:51 +00:00
|
|
|
return row.plugintitle;
|
2023-06-29 06:55:59 +00:00
|
|
|
} else {
|
|
|
|
statementGetShadowSource.reset();
|
2023-10-21 11:00:51 +00:00
|
|
|
return null;
|
2023-06-29 06:55:59 +00:00
|
|
|
}
|
|
|
|
};
|
2023-10-21 11:00:51 +00:00
|
|
|
/*
|
|
|
|
Get all titles
|
|
|
|
*/
|
2023-10-25 17:02:56 +00:00
|
|
|
var statementAllTitles = self.db.prepare(`select title from titles where plugintitle = '' order by title ${COLLATION_CLAUSE}`);
|
2023-06-29 06:55:59 +00:00
|
|
|
this.sqlAllTitles = function() {
|
|
|
|
let resultRows = [];
|
|
|
|
while(statementAllTitles.step()) {
|
|
|
|
var row = statementAllTitles.get({});
|
|
|
|
resultRows.push(row.title);
|
|
|
|
}
|
|
|
|
statementAllTitles.reset();
|
|
|
|
return resultRows;
|
|
|
|
};
|
2023-10-21 11:00:51 +00:00
|
|
|
/*
|
|
|
|
All shadow titles
|
|
|
|
*/
|
|
|
|
var statementAllShadowTitles = self.db.prepare(`
|
|
|
|
SELECT title
|
|
|
|
FROM tiddlers
|
2023-10-24 17:41:34 +00:00
|
|
|
WHERE plugintitle != ''
|
2023-10-21 11:00:51 +00:00
|
|
|
ORDER BY title ${COLLATION_CLAUSE}
|
|
|
|
`);
|
2023-06-29 06:55:59 +00:00
|
|
|
this.sqlAllShadowTitles = function() {
|
|
|
|
let resultRows = [];
|
|
|
|
while(statementAllShadowTitles.step()) {
|
|
|
|
var row = statementAllShadowTitles.get({});
|
|
|
|
resultRows.push(row.title);
|
|
|
|
}
|
|
|
|
statementAllShadowTitles.reset();
|
|
|
|
return resultRows;
|
|
|
|
};
|
2023-10-21 11:00:51 +00:00
|
|
|
/*
|
|
|
|
Iterate through each tiddler
|
|
|
|
*/
|
|
|
|
var statementEachTiddler = self.db.prepare(`
|
|
|
|
SELECT t.title, ti.meta, ti.text
|
|
|
|
FROM titles AS t
|
2023-10-24 17:41:34 +00:00
|
|
|
LEFT JOIN tiddlers AS ti ON t.title = ti.title AND t.plugintitle = ti.plugintitle
|
|
|
|
WHERE t.plugintitle == ''
|
2023-10-21 11:00:51 +00:00
|
|
|
ORDER BY t.title ${COLLATION_CLAUSE}
|
|
|
|
`);
|
2023-06-29 06:55:59 +00:00
|
|
|
this.sqlEachTiddler = function(callback) {
|
|
|
|
while(statementEachTiddler.step()) {
|
|
|
|
var row = statementEachTiddler.get({}),
|
|
|
|
tiddlerFields = Object.assign({},JSON.parse(row.meta),{title: row.title, text: row.text});
|
|
|
|
callback(tiddlerFields,row.title);
|
|
|
|
}
|
|
|
|
statementEachTiddler.reset();
|
|
|
|
};
|
|
|
|
/*
|
2023-10-21 11:00:51 +00:00
|
|
|
Iterate through each tiddler that is a shadow (including overridden shadows)
|
2023-06-29 06:55:59 +00:00
|
|
|
*/
|
|
|
|
var statementEachShadowTiddler = self.db.prepare(`
|
2023-10-21 11:00:51 +00:00
|
|
|
SELECT DISTINCT t.title, td.meta, td.text
|
|
|
|
FROM titles AS t
|
|
|
|
JOIN tiddlers AS td ON t.title = td.title
|
2023-10-24 17:41:34 +00:00
|
|
|
WHERE td.plugintitle != ''
|
2023-10-21 11:00:51 +00:00
|
|
|
ORDER BY t.title ${COLLATION_CLAUSE};
|
2023-06-29 06:55:59 +00:00
|
|
|
`);
|
|
|
|
this.sqlEachShadowTiddler = function(callback) {
|
|
|
|
while(statementEachShadowTiddler.step()) {
|
|
|
|
var row = statementEachShadowTiddler.get({});
|
|
|
|
var tiddlerFields = Object.assign({},JSON.parse(row.meta),{title: row.title, text: row.text});
|
|
|
|
callback(tiddlerFields,row.title);
|
|
|
|
}
|
|
|
|
statementEachShadowTiddler.reset();
|
|
|
|
};
|
|
|
|
/*
|
2023-10-21 11:00:51 +00:00
|
|
|
Iterate all tiddlers, and then the shadows
|
2023-06-29 06:55:59 +00:00
|
|
|
*/
|
|
|
|
this.sqlEachTiddlerPlusShadows = function(callback) {
|
2023-10-21 11:00:51 +00:00
|
|
|
const titles = Object.create(null);
|
|
|
|
self.sqlEachTiddler(function(fields,title) {
|
|
|
|
titles[title] = true;
|
|
|
|
callback(fields,title);
|
|
|
|
});
|
|
|
|
self.sqlEachShadowTiddler(function(fields,title) {
|
|
|
|
if(!titles[title]) {
|
|
|
|
callback(fields,title);
|
|
|
|
}
|
|
|
|
});
|
2023-06-29 06:55:59 +00:00
|
|
|
};
|
|
|
|
/*
|
2023-10-21 11:00:51 +00:00
|
|
|
Iterate all shadows, and then the tiddlers
|
2023-06-29 06:55:59 +00:00
|
|
|
*/
|
|
|
|
this.sqlEachShadowPlusTiddlers = function(callback) {
|
2023-10-21 11:00:51 +00:00
|
|
|
const titles = Object.create(null);
|
|
|
|
self.sqlEachShadowTiddler(function(fields,title) {
|
|
|
|
titles[title] = true;
|
|
|
|
callback(fields,title);
|
2023-07-18 18:33:21 +00:00
|
|
|
});
|
2023-10-21 11:00:51 +00:00
|
|
|
self.sqlEachTiddler(function(fields,title) {
|
|
|
|
if(!titles[title]) {
|
|
|
|
callback(fields,title);
|
2023-07-21 12:21:04 +00:00
|
|
|
}
|
2023-10-21 11:00:51 +00:00
|
|
|
});
|
2023-07-21 12:21:04 +00:00
|
|
|
};
|
|
|
|
/*
|
2023-10-21 11:00:51 +00:00
|
|
|
Return all tiddlers with a given tag
|
2023-07-18 18:33:21 +00:00
|
|
|
*/
|
2023-10-25 10:29:38 +00:00
|
|
|
var statementGetTiddlersWithTag = self.db.prepare(`
|
|
|
|
SELECT titles.title
|
|
|
|
FROM titles
|
|
|
|
JOIN tiddlers ON titles.title = tiddlers.title AND titles.plugintitle = tiddlers.plugintitle
|
|
|
|
JOIN plugins ON titles.plugintitle = plugins.plugintitle
|
|
|
|
JOIN tiddler_tags ON tiddlers.title = tiddler_tags.title AND tiddlers.plugintitle = tiddler_tags.plugintitle
|
|
|
|
JOIN tags ON tiddler_tags.tag_id = tags.tag_id
|
|
|
|
WHERE tags.tag = $tag
|
|
|
|
ORDER BY CASE
|
|
|
|
WHEN titles.plugintitle <> '' THEN 1
|
|
|
|
ELSE 2
|
|
|
|
END, titles.title ${COLLATION_CLAUSE} ASC;
|
|
|
|
`);
|
|
|
|
this.sqlGetTiddlersWithTag = function(tag,method) {
|
|
|
|
statementGetTiddlersWithTag.bind({
|
|
|
|
$tag: tag
|
|
|
|
});
|
|
|
|
var resultRows = [];
|
|
|
|
while(statementGetTiddlersWithTag.step()) {
|
|
|
|
var row = statementGetTiddlersWithTag.get({});
|
|
|
|
resultRows.push(row.title);
|
|
|
|
}
|
|
|
|
statementGetTiddlersWithTag.reset();
|
|
|
|
return resultRows;
|
|
|
|
};
|
2023-06-29 06:55:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|
2023-07-18 18:33:21 +00:00
|
|
|
//# sourceURL=$:/plugins/tiddlywiki/sqlite3store/sql-functions.js
|