+\r
+ fn update_to_next_version(current_version: u32, tx: &rusqlite::Transaction) -> Result<bool> {\r
+ let next_version = current_version + 1;\r
+\r
+ if next_version <= CURRENT_DB_VERSION {\r
+ println!("Update to version {}...", next_version);\r
+ }\r
+\r
+ fn update_version(to_version: u32, tx: &rusqlite::Transaction) -> Result<()> {\r
+ tx.execute("INSERT INTO [Version] ([version], [datetime]) VALUES (?1, datetime('now'))", [to_version]).map(|_| ()).map_err(DBError::from)\r
+ }\r
+\r
+ fn ok(updated: bool) -> Result<bool> {\r
+ if updated {\r
+ println!("Version updated");\r
+ }\r
+ Ok(updated)\r
+ }\r
+\r
+ match next_version {\r
+ 1 => {\r
+ tx.execute_batch(&load_sql_file(next_version)?)?;\r
+ update_version(next_version, tx)?;\r
+\r
+ ok(true)\r
+ }\r
+\r
+ // Version 1 doesn't exist yet.\r
+ 2 =>\r
+ ok(false),\r
+\r
+ v =>\r
+ Err(DBError::UnsupportedVersion(v)),\r
+ }\r
+ }\r
+\r
+ pub fn get_all_recipe_titles(&self) -> Result<Vec<(i32, String)>> {\r
+ let con = self.pool.get()?;\r
+ let mut stmt = con.prepare("SELECT [id], [title] FROM [Recipe] ORDER BY [title]")?;\r
+ let titles =\r
+ stmt.query_map([], |row| {\r
+ Ok((row.get(0)?, row.get(1)?))\r
+ })?.map(|r| r.unwrap()).collect_vec(); // TODO: remove unwrap.\r
+ Ok(titles)\r
+ }\r
+\r
+ pub fn get_all_recipes(&self) -> Result<Vec<model::Recipe>> {\r
+ let con = self.pool.get()?;\r
+ let mut stmt = con.prepare("SELECT [id], [title] FROM [Recipe] ORDER BY [title]")?;\r
+ let recipes =\r
+ stmt.query_map([], |row| {\r
+ Ok(model::Recipe::new(row.get(0)?, row.get(1)?))\r
+ })?.map(|r| r.unwrap()).collect_vec(); // TODO: remove unwrap.\r
+ Ok(recipes)\r
+ }\r
+\r
+ pub fn get_recipe(&self, id: i32) -> Result<model::Recipe> {\r
+ let con = self.pool.get()?;\r
+ con.query_row("SELECT [id], [title] FROM [Recipe] WHERE [id] = ?1", [id], |row| {\r
+ Ok(model::Recipe::new(row.get(0)?, row.get(1)?))\r
+ }).map_err(DBError::from)\r
+ }\r
+}\r
+\r
+fn load_sql_file(version: u32) -> Result<String> {\r
+ let sql_file = consts::SQL_FILENAME.replace("{VERSION}", &version.to_string());\r
+ let mut file = File::open(&sql_file).map_err(|err| DBError::Other(format!("Cannot open SQL file ({}): {}", &sql_file, err.to_string())))?;\r
+ let mut sql = String::new();\r
+ file.read_to_string(&mut sql).map_err(|err| DBError::Other(format!("Cannot read SQL file ({}) : {}", &sql_file, err.to_string())))?;\r
+ Ok(sql)\r