mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-01-08 08:20:27 +00:00
Query fixes
This commit is contained in:
parent
41a5bcc3a1
commit
85607f7846
@ -145,33 +145,49 @@ SqlTiddlerDatabase.prototype.createBag = function(bagname,description) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Returns array of {recipe_name:,description:}
|
Returns array of {recipe_name:,description:,bag_names: []}
|
||||||
*/
|
*/
|
||||||
SqlTiddlerDatabase.prototype.listRecipes = function() {
|
SqlTiddlerDatabase.prototype.listRecipes = function() {
|
||||||
const rows = this.runStatementGetAll(`
|
const rows = this.runStatementGetAll(`
|
||||||
SELECT recipe_name, description
|
SELECT r.recipe_name, r.description, b.bag_name, rb.position
|
||||||
FROM recipes
|
FROM recipes AS r
|
||||||
ORDER BY recipe_name
|
JOIN recipe_bags AS rb ON rb.recipe_id = r.recipe_id
|
||||||
|
JOIN bags AS b ON rb.bag_id = b.bag_id
|
||||||
|
ORDER BY r.recipe_name, rb.position
|
||||||
`);
|
`);
|
||||||
return rows;
|
const results = [];
|
||||||
|
let currentRecipeName = null, currentRecipeIndex = -1;
|
||||||
|
for(const row of rows) {
|
||||||
|
if(row.recipe_name !== currentRecipeName) {
|
||||||
|
currentRecipeName = row.recipe_name;
|
||||||
|
currentRecipeIndex += 1;
|
||||||
|
results.push({
|
||||||
|
recipe_name: row.recipe_name,
|
||||||
|
description: row.description,
|
||||||
|
bag_names: []
|
||||||
|
});
|
||||||
|
}
|
||||||
|
results[currentRecipeIndex].bag_names.push(row.bag_name);
|
||||||
|
}
|
||||||
|
return results;
|
||||||
};
|
};
|
||||||
|
|
||||||
SqlTiddlerDatabase.prototype.createRecipe = function(recipename,bagnames,description) {
|
SqlTiddlerDatabase.prototype.createRecipe = function(recipename,bagnames,description) {
|
||||||
// Run the queries
|
// Run the queries
|
||||||
this.runStatement(`
|
|
||||||
-- Create the entry in the recipes table if required
|
|
||||||
INSERT OR IGNORE INTO recipes (recipe_name, description)
|
|
||||||
VALUES ($recipe_name, $description)
|
|
||||||
`,{
|
|
||||||
recipe_name: recipename,
|
|
||||||
description: description
|
|
||||||
});
|
|
||||||
this.runStatement(`
|
this.runStatement(`
|
||||||
-- Delete existing recipe_bags entries for this recipe
|
-- Delete existing recipe_bags entries for this recipe
|
||||||
DELETE FROM recipe_bags WHERE recipe_id = (SELECT recipe_id FROM recipes WHERE recipe_name = $recipe_name)
|
DELETE FROM recipe_bags WHERE recipe_id = (SELECT recipe_id FROM recipes WHERE recipe_name = $recipe_name)
|
||||||
`,{
|
`,{
|
||||||
recipe_name: recipename
|
recipe_name: recipename
|
||||||
});
|
});
|
||||||
|
this.runStatement(`
|
||||||
|
-- Create the entry in the recipes table if required
|
||||||
|
INSERT OR REPLACE INTO recipes (recipe_name, description)
|
||||||
|
VALUES ($recipe_name, $description)
|
||||||
|
`,{
|
||||||
|
recipe_name: recipename,
|
||||||
|
description: description
|
||||||
|
});
|
||||||
this.runStatement(`
|
this.runStatement(`
|
||||||
INSERT INTO recipe_bags (recipe_id, bag_id, position)
|
INSERT INTO recipe_bags (recipe_id, bag_id, position)
|
||||||
SELECT r.recipe_id, b.bag_id, j.key as position
|
SELECT r.recipe_id, b.bag_id, j.key as position
|
||||||
@ -390,7 +406,7 @@ SqlTiddlerDatabase.prototype.getRecipeTiddlers = function(recipename) {
|
|||||||
INNER JOIN recipes AS r ON rb.recipe_id = r.recipe_id
|
INNER JOIN recipes AS r ON rb.recipe_id = r.recipe_id
|
||||||
INNER JOIN tiddlers AS t ON b.bag_id = t.bag_id
|
INNER JOIN tiddlers AS t ON b.bag_id = t.bag_id
|
||||||
WHERE r.recipe_name = $recipe_name
|
WHERE r.recipe_name = $recipe_name
|
||||||
GROUP BY t.title
|
GROUP BY t.title, b.bag_name
|
||||||
ORDER BY t.title
|
ORDER BY t.title
|
||||||
)
|
)
|
||||||
`,{
|
`,{
|
||||||
@ -420,12 +436,13 @@ SqlTiddlerDatabase.prototype.getRecipeBags = function(recipename) {
|
|||||||
SELECT bags.bag_name
|
SELECT bags.bag_name
|
||||||
FROM bags
|
FROM bags
|
||||||
JOIN (
|
JOIN (
|
||||||
SELECT rb.bag_id
|
SELECT rb.bag_id, rb.position as position
|
||||||
FROM recipe_bags AS rb
|
FROM recipe_bags AS rb
|
||||||
JOIN recipes AS r ON rb.recipe_id = r.recipe_id
|
JOIN recipes AS r ON rb.recipe_id = r.recipe_id
|
||||||
WHERE r.recipe_name = $recipe_name
|
WHERE r.recipe_name = $recipe_name
|
||||||
ORDER BY rb.position
|
ORDER BY rb.position
|
||||||
) AS bag_priority ON bags.bag_id = bag_priority.bag_id
|
) AS bag_priority ON bags.bag_id = bag_priority.bag_id
|
||||||
|
ORDER BY position
|
||||||
`,{
|
`,{
|
||||||
recipe_name: recipename
|
recipe_name: recipename
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user