ThalamOS
a powerful Flask web application designed to enhance your storage management.
Loading...
Searching...
No Matches
Storage_connector Namespace Reference

Functions

None setup ()
 
None fetch_csv ()
 
Annotated[tuple, "tuple containing the item's data if found, otherwise None"] fetch_item (item_id)
 
None delete_item (item_id)
 
None create_item (pos, obj_type, name, json_data)
 
Annotated[ list, "list of tuples containing the rows from the database that match the search criteria",] search (search_term)
 
None update_item (item_id, pos, obj_type, name, json_data)
 

Variables

Annotated db_path
 
Annotated mydb
 
Annotated cursor = mydb.cursor()
 

Detailed Description

StorageConnector Module

This module provides functions to interact with a SQLite database for managing storage items.
It includes functionalities to set up the database, create, fetch, delete, and search items,
as well as export the data to a CSV file.

Functions:
- setup(): Sets up the database by creating the 'storage' table and
  a trigger for automatic updating of the 'modification_time' column.
- fetch_csv(): Fetches all data from the 'storage' table and
  writes it to a CSV file named 'out.csv'.
- fetch_item(item_id): Fetches an item from the storage database by its id.
- delete_item(item_id): Deletes an item from the storage database based on the provided item id.
- create_item(pos, typ, name, jsonData): Creates an item in the storage database.
- search(search_term): Searches the storage database for entries that match the given search term.

Function Documentation

◆ create_item()

None Storage_connector.create_item ( pos,
obj_type,
name,
json_data )
Creates an item in the storage database.
Args:
    pos (int): The position of the item.
    typ (str): The type of the item.
    name (str): The name of the item.
    jsonData (str): The JSON data associated with the item. If empty JSON object ("{}"),
    no additional info is stored.
Returns:
    None

Definition at line 128 of file Storage_connector.py.

128def create_item(pos, obj_type, name, json_data) -> None:
129 """
130 Creates an item in the storage database.
131 Args:
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.
137 Returns:
138 None
139 """
140
141 logger.info(
142 "Creating item with position: {}, type: {}, name: {}, json_data: {}",
143 pos,
144 obj_type,
145 name,
146 json_data,
147 )
148 if json_data == "{}":
149 query = "INSERT INTO storage (position, type, name) VALUES (?, ?, ?);"
150 params = (pos, obj_type, name)
151 else:
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)
156 mydb.commit()
157
158

◆ delete_item()

None Storage_connector.delete_item ( item_id)
Deletes an item from the storage database based on the provided item id.
Args:
    itemID (int): The id of the item to be deleted from the storage.
Returns:
    None

Definition at line 115 of file Storage_connector.py.

115def delete_item(item_id) -> None:
116 """
117 Deletes an item from the storage database based on the provided item id.
118 Args:
119 itemID (int): The id of the item to be deleted from the storage.
120 Returns:
121 None
122 """
123
124 cursor.execute("DELETE FROM storage WHERE ID=?;", (item_id,))
125 mydb.commit()
126
127

◆ fetch_csv()

None Storage_connector.fetch_csv ( )
Fetches all data from the 'storage' table in the database and
writes it to a CSV file named 'out.csv'.
The function executes a SQL query to select all rows from the
'storage' table, writes the column headers
and all rows to the CSV file, and saves the file with UTF-8 encoding.
Raises:
    Any exceptions raised by the database cursor execution or file operations.

Definition at line 82 of file Storage_connector.py.

82def fetch_csv() -> None:
83 """
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.
89 Raises:
90 Any exceptions raised by the database cursor execution or file operations.
91 """
92
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())
98
99

◆ fetch_item()

Annotated[tuple, "tuple containing the item's data if found, otherwise None"] Storage_connector.fetch_item ( item_id)
Fetch an item from the storage database by its id.
Args:
    itemID (int): The id of the item to fetch.
Returns:
    tuple: A tuple containing the item's data if found, otherwise None.

Definition at line 100 of file Storage_connector.py.

102) -> Annotated[tuple, "tuple containing the item's data if found, otherwise None"]:
103 """
104 Fetch an item from the storage database by its id.
105 Args:
106 itemID (int): The id of the item to fetch.
107 Returns:
108 tuple: A tuple containing the item's data if found, otherwise None.
109 """
110
111 cursor.execute("select * From storage WHERE id=?;", (item_id,))
112 return cursor.fetchone()
113
114

◆ search()

Annotated[ list, "list of tuples containing the rows from the database that match the search criteria", ] Storage_connector.search ( search_term)
Searches the storage database for entries that match the given search term.
Args:
    searchTerm (str): The term to search for in the database. The term will be split
                      into individual words,
                      and each word will be used to search the 'type', 'name', and
                      'info' columns.
Returns:
    list: A list of tuples containing the rows from the database that match the search criteria.
          Returns None if there is an SQLite programming error.
Raises:
    sqlite3.ProgrammingError: If there is an error executing the query.

Definition at line 159 of file Storage_connector.py.

164]:
165 """
166 Searches the storage database for entries that match the given search term.
167 Args:
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
171 'info' columns.
172 Returns:
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.
175 Raises:
176 sqlite3.ProgrammingError: If there is an error executing the query.
177 """
178 try:
179 # Split searchTerm into single words
180 search_terms = search_term.split()
181
182 # create statement with placeholders for each search term
183 conditions = " AND ".join(
184 ["(type LIKE ? OR name LIKE ? OR info LIKE ?)" for _ in search_terms]
185 )
186 query = f"""
187 SELECT *
188 FROM storage
189 WHERE {conditions};
190 """
191
192 # create parameters for each placeholder
193 parameters = tuple(f"%{term}%" for term in search_terms for _ in range(3))
194
195 # Debugging: print query and parameters
196 logger.debug(f"Executing search query: {query} with parameters: {parameters}")
197
198 cursor.execute(query, parameters)
199 results = cursor.fetchall()
200
201 return results
202
203 except sqlite3.ProgrammingError as e:
204 logger.error(f"SQLite-Error: {str(e)}")
205 return None
206
207

◆ setup()

None Storage_connector.setup ( )
Sets up the database by creating the 'storage' table and a trigger for automatic
updating of the 'modification_time' column.
The 'storage' table contains the following columns:
- id: INTEGER, primary key, autoincrement
- position: INTEGER
- type: sensor | screw | display | nail | display | cable | miscellaneous | Motor Driver
- name: TEXT
- info: TEXT
- modification_time: TIMESTAMP, defaults to the current timestamp
The trigger 'update_modification_time' ensures that the 'modification_time' column
is automatically updated to the current timestamp whenever a row in the 'storage'
table is updated.
Commits the changes to the database and prints a confirmation message.

Definition at line 36 of file Storage_connector.py.

36def setup() -> None:
37 """
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
42 - position: INTEGER
43 - type: sensor | screw | display | nail | display | cable | miscellaneous | Motor Driver
44 - name: TEXT
45 - info: TEXT
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'
49 table is updated.
50 Commits the changes to the database and prints a confirmation message.
51 """
52 cursor.execute(
53 """
54 CREATE TABLE IF NOT EXISTS storage (
55 id INTEGER PRIMARY KEY AUTOINCREMENT,
56 position INTEGER,
57 type TEXT,
58 name TEXT,
59 info TEXT,
60 modification_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
61 );
62 """
63 )
64
65 # trigger for automatic update of modification_time
66 cursor.execute(
67 """
68 CREATE TRIGGER IF NOT EXISTS update_modification_time
69 AFTER UPDATE ON storage
70 FOR EACH ROW
71 BEGIN
72 UPDATE storage
73 SET modification_time = CURRENT_TIMESTAMP
74 WHERE id = OLD.id;
75 END;
76 """
77 )
78 mydb.commit()
79 logger.info("Database setup complete and ready for service")
80
81

◆ update_item()

None Storage_connector.update_item ( item_id,
pos,
obj_type,
name,
json_data )
Updates an item in the storage database.
Args:
    item_id (int): The id of the item to update.
    pos (int): The new position of the item.
    obj_type (str): The new type of the item.
    name (str): The new name of the item.
    json_data (str): The new JSON data associated with the item.
Returns:
    None

Definition at line 208 of file Storage_connector.py.

208def update_item(item_id, pos, obj_type, name, json_data) -> None:
209 """
210 Updates an item in the storage database.
211 Args:
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.
217 Returns:
218 None
219 """
220 logger.info(
221 "Updating item with id: {}, position: {}, type: {}, name: {}, json_data: {}",
222 item_id,
223 pos,
224 obj_type,
225 name,
226 json_data,
227 )
228 query = """
229 UPDATE storage
230 SET position = ?, type = ?, name = ?, info = ?
231 WHERE id = ?;
232 """
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)
236 mydb.commit()

Variable Documentation

◆ cursor

Annotated Storage_connector.cursor = mydb.cursor()

Definition at line 33 of file Storage_connector.py.

◆ db_path

Annotated Storage_connector.db_path
Initial value:
1= os.path.join(
2 os.path.dirname(__file__), "data/storage.db"
3)

Definition at line 26 of file Storage_connector.py.

◆ mydb

Annotated Storage_connector.mydb
Initial value:
1= sqlite3.connect(
2 db_path, check_same_thread=False
3)

Definition at line 30 of file Storage_connector.py.