mirror of
				https://github.com/Jermolene/TiddlyWiki5
				synced 2025-11-04 09:33:00 +00:00 
			
		
		
		
	Database methods that mutate tables should return IDs
This commit is contained in:
		@@ -87,13 +87,17 @@ SqlTiddlerDatabase.prototype.createTables = function() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
SqlTiddlerDatabase.prototype.listBags = function() {
 | 
					SqlTiddlerDatabase.prototype.listBags = function() {
 | 
				
			||||||
	const rows = this.engine.runStatementGetAll(`
 | 
						const rows = this.engine.runStatementGetAll(`
 | 
				
			||||||
		SELECT bag_name, accesscontrol, description
 | 
							SELECT bag_name, bag_id, accesscontrol, description
 | 
				
			||||||
		FROM bags
 | 
							FROM bags
 | 
				
			||||||
		ORDER BY bag_name
 | 
							ORDER BY bag_name
 | 
				
			||||||
	`);
 | 
						`);
 | 
				
			||||||
	return rows;
 | 
						return rows;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					Create or update a bag
 | 
				
			||||||
 | 
					Returns the bag_id of the bag
 | 
				
			||||||
 | 
					*/
 | 
				
			||||||
SqlTiddlerDatabase.prototype.createBag = function(bag_name,description,accesscontrol) {
 | 
					SqlTiddlerDatabase.prototype.createBag = function(bag_name,description,accesscontrol) {
 | 
				
			||||||
	accesscontrol = accesscontrol || "";
 | 
						accesscontrol = accesscontrol || "";
 | 
				
			||||||
	// Run the queries
 | 
						// Run the queries
 | 
				
			||||||
@@ -103,7 +107,7 @@ SqlTiddlerDatabase.prototype.createBag = function(bag_name,description,accesscon
 | 
				
			|||||||
	`,{
 | 
						`,{
 | 
				
			||||||
		$bag_name: bag_name
 | 
							$bag_name: bag_name
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
	this.engine.runStatement(`
 | 
						const updateBags = this.engine.runStatement(`
 | 
				
			||||||
		UPDATE bags
 | 
							UPDATE bags
 | 
				
			||||||
		SET accesscontrol = $accesscontrol,
 | 
							SET accesscontrol = $accesscontrol,
 | 
				
			||||||
		description = $description 
 | 
							description = $description 
 | 
				
			||||||
@@ -113,6 +117,7 @@ SqlTiddlerDatabase.prototype.createBag = function(bag_name,description,accesscon
 | 
				
			|||||||
		$accesscontrol: accesscontrol,
 | 
							$accesscontrol: accesscontrol,
 | 
				
			||||||
		$description: description
 | 
							$description: description
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
						return updateBags.lastInsertRowid;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
@@ -120,7 +125,7 @@ Returns array of {recipe_name:,description:,bag_names: []}
 | 
				
			|||||||
*/
 | 
					*/
 | 
				
			||||||
SqlTiddlerDatabase.prototype.listRecipes = function() {
 | 
					SqlTiddlerDatabase.prototype.listRecipes = function() {
 | 
				
			||||||
	const rows = this.engine.runStatementGetAll(`
 | 
						const rows = this.engine.runStatementGetAll(`
 | 
				
			||||||
		SELECT r.recipe_name, r.description, b.bag_name, rb.position
 | 
							SELECT r.recipe_name, r.recipe_id, r.description, b.bag_name, rb.position
 | 
				
			||||||
		FROM recipes AS r
 | 
							FROM recipes AS r
 | 
				
			||||||
		JOIN recipe_bags AS rb ON rb.recipe_id = r.recipe_id
 | 
							JOIN recipe_bags AS rb ON rb.recipe_id = r.recipe_id
 | 
				
			||||||
		JOIN bags AS b ON rb.bag_id = b.bag_id
 | 
							JOIN bags AS b ON rb.bag_id = b.bag_id
 | 
				
			||||||
@@ -134,6 +139,7 @@ SqlTiddlerDatabase.prototype.listRecipes = function() {
 | 
				
			|||||||
			currentRecipeIndex += 1;
 | 
								currentRecipeIndex += 1;
 | 
				
			||||||
			results.push({
 | 
								results.push({
 | 
				
			||||||
				recipe_name: row.recipe_name,
 | 
									recipe_name: row.recipe_name,
 | 
				
			||||||
 | 
									recipe_id: row.recipe_id,
 | 
				
			||||||
				description: row.description,
 | 
									description: row.description,
 | 
				
			||||||
				bag_names: []
 | 
									bag_names: []
 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
@@ -143,6 +149,10 @@ SqlTiddlerDatabase.prototype.listRecipes = function() {
 | 
				
			|||||||
	return results;
 | 
						return results;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					Create or update a recipe
 | 
				
			||||||
 | 
					Returns the recipe_id of the recipe
 | 
				
			||||||
 | 
					*/
 | 
				
			||||||
SqlTiddlerDatabase.prototype.createRecipe = function(recipe_name,bag_names,description) {
 | 
					SqlTiddlerDatabase.prototype.createRecipe = function(recipe_name,bag_names,description) {
 | 
				
			||||||
	// Run the queries
 | 
						// Run the queries
 | 
				
			||||||
	this.engine.runStatement(`
 | 
						this.engine.runStatement(`
 | 
				
			||||||
@@ -151,7 +161,7 @@ SqlTiddlerDatabase.prototype.createRecipe = function(recipe_name,bag_names,descr
 | 
				
			|||||||
	`,{
 | 
						`,{
 | 
				
			||||||
		$recipe_name: recipe_name
 | 
							$recipe_name: recipe_name
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
	this.engine.runStatement(`
 | 
						const updateRecipes = this.engine.runStatement(`
 | 
				
			||||||
		-- Create the entry in the recipes table if required
 | 
							-- Create the entry in the recipes table if required
 | 
				
			||||||
		INSERT OR REPLACE INTO recipes (recipe_name, description)
 | 
							INSERT OR REPLACE INTO recipes (recipe_name, description)
 | 
				
			||||||
		VALUES ($recipe_name, $description)
 | 
							VALUES ($recipe_name, $description)
 | 
				
			||||||
@@ -170,6 +180,7 @@ SqlTiddlerDatabase.prototype.createRecipe = function(recipe_name,bag_names,descr
 | 
				
			|||||||
		$recipe_name: recipe_name,
 | 
							$recipe_name: recipe_name,
 | 
				
			||||||
		$bag_names: JSON.stringify(bag_names)
 | 
							$bag_names: JSON.stringify(bag_names)
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
						return updateRecipes.lastInsertRowid;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -37,23 +37,23 @@ function runSqlDatabaseTests(engine) {
 | 
				
			|||||||
	// Run tests
 | 
						// Run tests
 | 
				
			||||||
	it("should save and retrieve tiddlers using engine: " + engine, function() {
 | 
						it("should save and retrieve tiddlers using engine: " + engine, function() {
 | 
				
			||||||
		// Create bags and recipes
 | 
							// Create bags and recipes
 | 
				
			||||||
		sqlTiddlerDatabase.createBag("bag-alpha","Bag alpha");
 | 
							expect(sqlTiddlerDatabase.createBag("bag-alpha","Bag alpha")).toEqual(1);
 | 
				
			||||||
		sqlTiddlerDatabase.createBag("bag-beta","Bag beta");
 | 
							expect(sqlTiddlerDatabase.createBag("bag-beta","Bag beta")).toEqual(2);
 | 
				
			||||||
		sqlTiddlerDatabase.createBag("bag-gamma","Bag gamma");
 | 
							expect(sqlTiddlerDatabase.createBag("bag-gamma","Bag gamma")).toEqual(3);
 | 
				
			||||||
		expect(sqlTiddlerDatabase.listBags()).toEqual([
 | 
							expect(sqlTiddlerDatabase.listBags()).toEqual([
 | 
				
			||||||
			{ bag_name: 'bag-alpha', accesscontrol: '', description: "Bag alpha" },
 | 
								{ bag_name: 'bag-alpha', bag_id: 1, accesscontrol: '', description: "Bag alpha" },
 | 
				
			||||||
			{ bag_name: 'bag-beta', accesscontrol: '', description: "Bag beta" },
 | 
								{ bag_name: 'bag-beta', bag_id: 2, accesscontrol: '', description: "Bag beta" },
 | 
				
			||||||
			{ bag_name: 'bag-gamma', accesscontrol: '', description: "Bag gamma" }
 | 
								{ bag_name: 'bag-gamma', bag_id: 3, accesscontrol: '', description: "Bag gamma" }
 | 
				
			||||||
		]);
 | 
							]);
 | 
				
			||||||
		sqlTiddlerDatabase.createRecipe("recipe-rho",["bag-alpha","bag-beta"],"Recipe rho");
 | 
							expect(sqlTiddlerDatabase.createRecipe("recipe-rho",["bag-alpha","bag-beta"],"Recipe rho")).toEqual(1);
 | 
				
			||||||
		sqlTiddlerDatabase.createRecipe("recipe-sigma",["bag-alpha","bag-gamma"],"Recipe sigma");
 | 
							expect(sqlTiddlerDatabase.createRecipe("recipe-sigma",["bag-alpha","bag-gamma"],"Recipe sigma")).toEqual(2);
 | 
				
			||||||
		sqlTiddlerDatabase.createRecipe("recipe-tau",["bag-alpha"],"Recipe tau");
 | 
							expect(sqlTiddlerDatabase.createRecipe("recipe-tau",["bag-alpha"],"Recipe tau")).toEqual(3);
 | 
				
			||||||
		sqlTiddlerDatabase.createRecipe("recipe-upsilon",["bag-alpha","bag-gamma","bag-beta"],"Recipe upsilon");
 | 
							expect(sqlTiddlerDatabase.createRecipe("recipe-upsilon",["bag-alpha","bag-gamma","bag-beta"],"Recipe upsilon")).toEqual(4);
 | 
				
			||||||
		expect(sqlTiddlerDatabase.listRecipes()).toEqual([
 | 
							expect(sqlTiddlerDatabase.listRecipes()).toEqual([
 | 
				
			||||||
			{ recipe_name: 'recipe-rho', bag_names: ["bag-alpha","bag-beta"], description: "Recipe rho" },
 | 
								{ recipe_name: 'recipe-rho', recipe_id: 1, bag_names: ["bag-alpha","bag-beta"], description: "Recipe rho" },
 | 
				
			||||||
			{ recipe_name: 'recipe-sigma', bag_names: ["bag-alpha","bag-gamma"], description: "Recipe sigma" },
 | 
								{ recipe_name: 'recipe-sigma', recipe_id: 2, bag_names: ["bag-alpha","bag-gamma"], description: "Recipe sigma" },
 | 
				
			||||||
			{ recipe_name: 'recipe-tau', bag_names: ["bag-alpha"], description: "Recipe tau" },
 | 
								{ recipe_name: 'recipe-tau', recipe_id: 3, bag_names: ["bag-alpha"], description: "Recipe tau" },
 | 
				
			||||||
			{ recipe_name: 'recipe-upsilon', bag_names: ["bag-alpha","bag-gamma","bag-beta"], description: "Recipe upsilon" }
 | 
								{ recipe_name: 'recipe-upsilon', recipe_id: 4, bag_names: ["bag-alpha","bag-gamma","bag-beta"], description: "Recipe upsilon" }
 | 
				
			||||||
		]);
 | 
							]);
 | 
				
			||||||
		expect(sqlTiddlerDatabase.getRecipeBags("recipe-rho")).toEqual(["bag-alpha","bag-beta"]);
 | 
							expect(sqlTiddlerDatabase.getRecipeBags("recipe-rho")).toEqual(["bag-alpha","bag-beta"]);
 | 
				
			||||||
		expect(sqlTiddlerDatabase.getRecipeBags("recipe-sigma")).toEqual(["bag-alpha","bag-gamma"]);
 | 
							expect(sqlTiddlerDatabase.getRecipeBags("recipe-sigma")).toEqual(["bag-alpha","bag-gamma"]);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -48,6 +48,7 @@ function runSqlStoreTests(engine) {
 | 
				
			|||||||
		expect(store.createBag("bag-alpha", "Bag alpha")).toEqual(null);
 | 
							expect(store.createBag("bag-alpha", "Bag alpha")).toEqual(null);
 | 
				
			||||||
		expect(store.listBags()).toEqual([{
 | 
							expect(store.listBags()).toEqual([{
 | 
				
			||||||
			bag_name: "bag-alpha",
 | 
								bag_name: "bag-alpha",
 | 
				
			||||||
 | 
								bag_id: 1,
 | 
				
			||||||
			accesscontrol: "",
 | 
								accesscontrol: "",
 | 
				
			||||||
			description: "Bag alpha"
 | 
								description: "Bag alpha"
 | 
				
			||||||
		}]);
 | 
							}]);
 | 
				
			||||||
@@ -65,6 +66,7 @@ function runSqlStoreTests(engine) {
 | 
				
			|||||||
		expect(store.createBag("bag-alpha", "Different description")).toEqual(null);
 | 
							expect(store.createBag("bag-alpha", "Different description")).toEqual(null);
 | 
				
			||||||
		expect(store.listBags()).toEqual([{
 | 
							expect(store.listBags()).toEqual([{
 | 
				
			||||||
			bag_name: "bag-alpha",
 | 
								bag_name: "bag-alpha",
 | 
				
			||||||
 | 
								bag_id: 1,
 | 
				
			||||||
			accesscontrol: "",
 | 
								accesscontrol: "",
 | 
				
			||||||
			description: "Different description"
 | 
								description: "Different description"
 | 
				
			||||||
		}]);
 | 
							}]);
 | 
				
			||||||
@@ -100,7 +102,7 @@ function runSqlStoreTests(engine) {
 | 
				
			|||||||
		expect(store.createRecipe("recipe-rho",["bag-alpha","bag-beta"],"Recipe rho")).toEqual(null);
 | 
							expect(store.createRecipe("recipe-rho",["bag-alpha","bag-beta"],"Recipe rho")).toEqual(null);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		expect(store.listRecipes()).toEqual([
 | 
							expect(store.listRecipes()).toEqual([
 | 
				
			||||||
			{ recipe_name: "recipe-rho", bag_names: ["bag-alpha","bag-beta"], description: "Recipe rho" }
 | 
								{ recipe_name: "recipe-rho", recipe_id: 1, bag_names: ["bag-alpha","bag-beta"], description: "Recipe rho" }
 | 
				
			||||||
		]);
 | 
							]);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user