isayahc commited on
Commit
cff8370
·
unverified ·
1 Parent(s): 4be58c1

added documenttion to each function

Browse files
Files changed (1) hide show
  1. rag_app/database/db_handler.py +70 -0
rag_app/database/db_handler.py CHANGED
@@ -17,6 +17,15 @@ SQLModel.metadata.create_all(engine)
17
 
18
 
19
  def read_one(hash_id: dict):
 
 
 
 
 
 
 
 
 
20
  with Session(engine) as session:
21
  statement = select(Sources).where(Sources.hash_id == hash_id)
22
  sources = session.exec(statement).first()
@@ -24,6 +33,15 @@ def read_one(hash_id: dict):
24
 
25
 
26
  def add_one(data: dict):
 
 
 
 
 
 
 
 
 
27
  with Session(engine) as session:
28
  if session.exec(
29
  select(Sources).where(Sources.hash_id == data.get("hash_id"))
@@ -39,6 +57,16 @@ def add_one(data: dict):
39
 
40
 
41
  def update_one(hash_id: dict, data: dict):
 
 
 
 
 
 
 
 
 
 
42
  with Session(engine) as session:
43
  # Check if the item with the given hash_id exists
44
  sources = session.exec(
@@ -55,6 +83,15 @@ def update_one(hash_id: dict, data: dict):
55
 
56
 
57
  def delete_one(id: int):
 
 
 
 
 
 
 
 
 
58
  with Session(engine) as session:
59
  # Check if the item with the given hash_id exists
60
  sources = session.exec(
@@ -69,6 +106,15 @@ def delete_one(id: int):
69
 
70
 
71
  def add_many(data: list):
 
 
 
 
 
 
 
 
 
72
  with Session(engine) as session:
73
  for info in data:
74
  # Reuse add_one function for each item
@@ -85,6 +131,15 @@ def add_many(data: list):
85
 
86
 
87
  def delete_many(ids: list):
 
 
 
 
 
 
 
 
 
88
  with Session(engine) as session:
89
  for id in ids:
90
  # Reuse delete_one function for each item
@@ -97,6 +152,15 @@ def delete_many(ids: list):
97
 
98
 
99
  def read_all(query: dict = None):
 
 
 
 
 
 
 
 
 
100
  with Session(engine) as session:
101
  statement = select(Sources)
102
  if query:
@@ -108,6 +172,12 @@ def read_all(query: dict = None):
108
 
109
 
110
  def delete_all():
 
 
 
 
 
 
111
  with Session(engine) as session:
112
  session.exec(Sources).delete()
113
  session.commit()
 
17
 
18
 
19
  def read_one(hash_id: dict):
20
+ """
21
+ Read a single entry from the database by its hash_id.
22
+
23
+ Args:
24
+ hash_id (dict): Dictionary containing the hash_id to search for.
25
+
26
+ Returns:
27
+ Sources: The matching entry from the database, or None if no match is found.
28
+ """
29
  with Session(engine) as session:
30
  statement = select(Sources).where(Sources.hash_id == hash_id)
31
  sources = session.exec(statement).first()
 
33
 
34
 
35
  def add_one(data: dict):
36
+ """
37
+ Add a single entry to the database.
38
+
39
+ Args:
40
+ data (dict): Dictionary containing the data for the new entry.
41
+
42
+ Returns:
43
+ Sources: The added entry, or None if the entry already exists.
44
+ """
45
  with Session(engine) as session:
46
  if session.exec(
47
  select(Sources).where(Sources.hash_id == data.get("hash_id"))
 
57
 
58
 
59
  def update_one(hash_id: dict, data: dict):
60
+ """
61
+ Update a single entry in the database by its hash_id.
62
+
63
+ Args:
64
+ hash_id (dict): Dictionary containing the hash_id to search for.
65
+ data (dict): Dictionary containing the updated data for the entry.
66
+
67
+ Returns:
68
+ Sources: The updated entry, or None if no match is found.
69
+ """
70
  with Session(engine) as session:
71
  # Check if the item with the given hash_id exists
72
  sources = session.exec(
 
83
 
84
 
85
  def delete_one(id: int):
86
+ """
87
+ Delete a single entry from the database by its id.
88
+
89
+ Args:
90
+ id (int): The id of the entry to delete.
91
+
92
+ Returns:
93
+ None
94
+ """
95
  with Session(engine) as session:
96
  # Check if the item with the given hash_id exists
97
  sources = session.exec(
 
106
 
107
 
108
  def add_many(data: list):
109
+ """
110
+ Add multiple entries to the database.
111
+
112
+ Args:
113
+ data (list): List of dictionaries, each containing the data for a new entry.
114
+
115
+ Returns:
116
+ None
117
+ """
118
  with Session(engine) as session:
119
  for info in data:
120
  # Reuse add_one function for each item
 
131
 
132
 
133
  def delete_many(ids: list):
134
+ """
135
+ Delete multiple entries from the database by their ids.
136
+
137
+ Args:
138
+ ids (list): List of ids of the entries to delete.
139
+
140
+ Returns:
141
+ None
142
+ """
143
  with Session(engine) as session:
144
  for id in ids:
145
  # Reuse delete_one function for each item
 
152
 
153
 
154
  def read_all(query: dict = None):
155
+ """
156
+ Read all entries from the database, optionally filtered by a query.
157
+
158
+ Args:
159
+ query (dict, optional): Dictionary containing the query parameters. Defaults to None.
160
+
161
+ Returns:
162
+ list: List of matching entries from the database.
163
+ """
164
  with Session(engine) as session:
165
  statement = select(Sources)
166
  if query:
 
172
 
173
 
174
  def delete_all():
175
+ """
176
+ Delete all entries from the database.
177
+
178
+ Returns:
179
+ None
180
+ """
181
  with Session(engine) as session:
182
  session.exec(Sources).delete()
183
  session.commit()