4This module provides functions to interact with a SQLite database for managing storage items.
5It includes functionalities to set up the database, create, fetch, delete, and search items,
6as well as export the data to a CSV file.
9- setup(): Sets up the database by creating the 'storage' table and
10 a trigger for automatic updating of the 'modification_time' column.
11- fetch_csv(): Fetches all data from the 'storage' table and
12 writes it to a CSV file named 'out.csv'.
13- fetch_item(item_id): Fetches an item from the storage database by its id.
14- delete_item(item_id): Deletes an item from the storage database based on the provided item id.
15- create_item(pos, typ, name, jsonData): Creates an item in the storage database.
16- search(search_term): Searches the storage database for entries that match the given search term.
18from typing
import Annotated
23from logger_config
import logger
26db_path: Annotated[str,
"path of database files"] = os.path.join(
27 os.path.dirname(__file__),
"data/storage.db"
29logger.info(f
"Database path: {db_path}")
30mydb: Annotated[sqlite3.Connection,
"helper object for database"] = sqlite3.connect(
31 db_path, check_same_thread=
False
38 Sets up the database by creating the 'storage' table and a trigger for automatic
39 updating of the 'modification_time' column.
40 The 'storage' table contains the following columns:
41 - id: INTEGER, primary key, autoincrement
43 - type: sensor | screw | display | nail | display | cable | miscellaneous | Motor Driver
46 - modification_time: TIMESTAMP, defaults to the current timestamp
47 The trigger 'update_modification_time' ensures that the 'modification_time' column
48 is automatically updated to the current timestamp whenever a row in the 'storage'
50 Commits the changes to the database and prints a confirmation message.
54 CREATE TABLE IF NOT EXISTS storage (
55 id INTEGER PRIMARY KEY AUTOINCREMENT,
60 modification_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
68 CREATE TRIGGER IF NOT EXISTS update_modification_time
69 AFTER UPDATE ON storage
73 SET modification_time = CURRENT_TIMESTAMP
79 logger.info(
"Database setup complete and ready for service")
84 Fetches all data from the 'storage' table in the database and
85 writes it to a CSV file named 'out.csv'.
86 The function executes a SQL query to select all rows from the
87 'storage' table, writes the column headers
88 and all rows to the CSV file, and saves the file with UTF-8 encoding.
90 Any exceptions raised by the database cursor execution or file operations.
93 cursor.execute(
"SELECT * FROM storage;")
94 with open(
"static/database.csv",
"w", newline=
"", encoding=
"utf-8")
as csv_file:
95 csv_writer = csv.writer(csv_file)
96 csv_writer.writerow([i[0]
for i
in cursor.description])
97 csv_writer.writerows(cursor.fetchall())
102) -> Annotated[tuple, "tuple containing the item's data if found, otherwise None"]:
104 Fetch an item from the storage database by its id.
106 itemID (int): The id of the item to fetch.
108 tuple: A tuple containing the item's data if found, otherwise None.
111 cursor.execute(
"select * From storage WHERE id=?;", (item_id,))
112 return cursor.fetchone()
117 Deletes an item from the storage database based on the provided item id.
119 itemID (int): The id of the item to be deleted from the storage.
124 cursor.execute(
"DELETE FROM storage WHERE ID=?;", (item_id,))
130 Creates an item in the storage database.
132 pos (int): The position of the item.
133 typ (str): The type of the item.
134 name (str): The name of the item.
135 jsonData (str): The JSON data associated with the item. If empty JSON object ("{}"),
136 no additional info is stored.
142 "Creating item with position: {}, type: {}, name: {}, json_data: {}",
148 if json_data ==
"{}":
149 query =
"INSERT INTO storage (position, type, name) VALUES (?, ?, ?);"
150 params = (pos, obj_type, name)
152 query =
"INSERT INTO storage (position, type, name, info) VALUES (?, ?, ?, ?);"
153 params = (pos, obj_type, name, json_data)
154 logger.debug(f
"Executing query: {query} with params: {params}")
155 cursor.execute(query, params)
163 "list of tuples containing the rows from the database that match the search criteria",
166 Searches the storage database for entries that match the given search term.
168 searchTerm (str): The term to search for in the database. The term will be split
169 into individual words,
170 and each word will be used to search the 'type', 'name', and
173 list: A list of tuples containing the rows from the database that match the search criteria.
174 Returns None if there is an SQLite programming error.
176 sqlite3.ProgrammingError: If there is an error executing the query.
180 search_terms = search_term.split()
183 conditions =
" AND ".join(
184 [
"(type LIKE ? OR name LIKE ? OR info LIKE ?)" for _
in search_terms]
193 parameters = tuple(f
"%{term}%" for term
in search_terms
for _
in range(3))
196 logger.debug(f
"Executing search query: {query} with parameters: {parameters}")
198 cursor.execute(query, parameters)
199 results = cursor.fetchall()
203 except sqlite3.ProgrammingError
as e:
204 logger.error(f
"SQLite-Error: {str(e)}")
210 Updates an item in the storage database.
212 item_id (int): The id of the item to update.
213 pos (int): The new position of the item.
214 obj_type (str): The new type of the item.
215 name (str): The new name of the item.
216 json_data (str): The new JSON data associated with the item.
221 "Updating item with id: {}, position: {}, type: {}, name: {}, json_data: {}",
230 SET position = ?, type = ?, name = ?, info = ?
233 params = (pos, obj_type, name, json_data, item_id)
234 logger.debug(f
"Executing query: {query} with params: {params}")
235 cursor.execute(query, params)
Annotated[ list, "list of tuples containing the rows from the database that match the search criteria",] search(search_term)
None create_item(pos, obj_type, name, json_data)
None delete_item(item_id)
Annotated[tuple, "tuple containing the item's data if found, otherwise None"] fetch_item(item_id)
None update_item(item_id, pos, obj_type, name, json_data)