21
21
22
22
23
23
class SqliteImageRecordStorage (ImageRecordStorageBase ):
24
- _conn : sqlite3 .Connection
25
- _cursor : sqlite3 .Cursor
26
-
27
24
def __init__ (self , db : SqliteDatabase ) -> None :
28
25
super ().__init__ ()
29
26
self ._conn = db .conn
30
- self ._cursor = self ._conn .cursor ()
31
27
32
28
def get (self , image_name : str ) -> ImageRecord :
33
29
try :
34
- self ._cursor .execute (
30
+ cursor = self ._conn .cursor ()
31
+ cursor .execute (
35
32
f"""--sql
36
33
SELECT { IMAGE_DTO_COLS } FROM images
37
34
WHERE image_name = ?;
38
35
""" ,
39
36
(image_name ,),
40
37
)
41
38
42
- result = cast (Optional [sqlite3 .Row ], self . _cursor .fetchone ())
39
+ result = cast (Optional [sqlite3 .Row ], cursor .fetchone ())
43
40
except sqlite3 .Error as e :
44
41
raise ImageRecordNotFoundException from e
45
42
@@ -50,15 +47,16 @@ def get(self, image_name: str) -> ImageRecord:
50
47
51
48
def get_metadata (self , image_name : str ) -> Optional [MetadataField ]:
52
49
try :
53
- self ._cursor .execute (
50
+ cursor = self ._conn .cursor ()
51
+ cursor .execute (
54
52
"""--sql
55
53
SELECT metadata FROM images
56
54
WHERE image_name = ?;
57
55
""" ,
58
56
(image_name ,),
59
57
)
60
58
61
- result = cast (Optional [sqlite3 .Row ], self . _cursor .fetchone ())
59
+ result = cast (Optional [sqlite3 .Row ], cursor .fetchone ())
62
60
63
61
if not result :
64
62
raise ImageRecordNotFoundException
@@ -75,9 +73,10 @@ def update(
75
73
changes : ImageRecordChanges ,
76
74
) -> None :
77
75
try :
76
+ cursor = self ._conn .cursor ()
78
77
# Change the category of the image
79
78
if changes .image_category is not None :
80
- self . _cursor .execute (
79
+ cursor .execute (
81
80
"""--sql
82
81
UPDATE images
83
82
SET image_category = ?
@@ -88,7 +87,7 @@ def update(
88
87
89
88
# Change the session associated with the image
90
89
if changes .session_id is not None :
91
- self . _cursor .execute (
90
+ cursor .execute (
92
91
"""--sql
93
92
UPDATE images
94
93
SET session_id = ?
@@ -99,7 +98,7 @@ def update(
99
98
100
99
# Change the image's `is_intermediate`` flag
101
100
if changes .is_intermediate is not None :
102
- self . _cursor .execute (
101
+ cursor .execute (
103
102
"""--sql
104
103
UPDATE images
105
104
SET is_intermediate = ?
@@ -110,7 +109,7 @@ def update(
110
109
111
110
# Change the image's `starred`` state
112
111
if changes .starred is not None :
113
- self . _cursor .execute (
112
+ cursor .execute (
114
113
"""--sql
115
114
UPDATE images
116
115
SET starred = ?
@@ -136,6 +135,8 @@ def get_many(
136
135
board_id : Optional [str ] = None ,
137
136
search_term : Optional [str ] = None ,
138
137
) -> OffsetPaginatedResults [ImageRecord ]:
138
+ cursor = self ._conn .cursor ()
139
+
139
140
# Manually build two queries - one for the count, one for the records
140
141
count_query = """--sql
141
142
SELECT COUNT(*)
@@ -216,21 +217,22 @@ def get_many(
216
217
images_params .extend ([limit , offset ])
217
218
218
219
# Build the list of images, deserializing each row
219
- self . _cursor .execute (images_query , images_params )
220
- result = cast (list [sqlite3 .Row ], self . _cursor .fetchall ())
220
+ cursor .execute (images_query , images_params )
221
+ result = cast (list [sqlite3 .Row ], cursor .fetchall ())
221
222
images = [deserialize_image_record (dict (r )) for r in result ]
222
223
223
224
# Set up and execute the count query, without pagination
224
225
count_query += query_conditions + ";"
225
226
count_params = query_params .copy ()
226
- self . _cursor .execute (count_query , count_params )
227
- count = cast (int , self . _cursor .fetchone ()[0 ])
227
+ cursor .execute (count_query , count_params )
228
+ count = cast (int , cursor .fetchone ()[0 ])
228
229
229
230
return OffsetPaginatedResults (items = images , offset = offset , limit = limit , total = count )
230
231
231
232
def delete (self , image_name : str ) -> None :
232
233
try :
233
- self ._cursor .execute (
234
+ cursor = self ._conn .cursor ()
235
+ cursor .execute (
234
236
"""--sql
235
237
DELETE FROM images
236
238
WHERE image_name = ?;
@@ -244,41 +246,45 @@ def delete(self, image_name: str) -> None:
244
246
245
247
def delete_many (self , image_names : list [str ]) -> None :
246
248
try :
249
+ cursor = self ._conn .cursor ()
250
+
247
251
placeholders = "," .join ("?" for _ in image_names )
248
252
249
253
# Construct the SQLite query with the placeholders
250
254
query = f"DELETE FROM images WHERE image_name IN ({ placeholders } )"
251
255
252
256
# Execute the query with the list of IDs as parameters
253
- self . _cursor .execute (query , image_names )
257
+ cursor .execute (query , image_names )
254
258
255
259
self ._conn .commit ()
256
260
except sqlite3 .Error as e :
257
261
self ._conn .rollback ()
258
262
raise ImageRecordDeleteException from e
259
263
260
264
def get_intermediates_count (self ) -> int :
261
- self ._cursor .execute (
265
+ cursor = self ._conn .cursor ()
266
+ cursor .execute (
262
267
"""--sql
263
268
SELECT COUNT(*) FROM images
264
269
WHERE is_intermediate = TRUE;
265
270
"""
266
271
)
267
- count = cast (int , self . _cursor .fetchone ()[0 ])
272
+ count = cast (int , cursor .fetchone ()[0 ])
268
273
self ._conn .commit ()
269
274
return count
270
275
271
276
def delete_intermediates (self ) -> list [str ]:
272
277
try :
273
- self ._cursor .execute (
278
+ cursor = self ._conn .cursor ()
279
+ cursor .execute (
274
280
"""--sql
275
281
SELECT image_name FROM images
276
282
WHERE is_intermediate = TRUE;
277
283
"""
278
284
)
279
- result = cast (list [sqlite3 .Row ], self . _cursor .fetchall ())
285
+ result = cast (list [sqlite3 .Row ], cursor .fetchall ())
280
286
image_names = [r [0 ] for r in result ]
281
- self . _cursor .execute (
287
+ cursor .execute (
282
288
"""--sql
283
289
DELETE FROM images
284
290
WHERE is_intermediate = TRUE;
@@ -305,7 +311,8 @@ def save(
305
311
metadata : Optional [str ] = None ,
306
312
) -> datetime :
307
313
try :
308
- self ._cursor .execute (
314
+ cursor = self ._conn .cursor ()
315
+ cursor .execute (
309
316
"""--sql
310
317
INSERT OR IGNORE INTO images (
311
318
image_name,
@@ -338,7 +345,7 @@ def save(
338
345
)
339
346
self ._conn .commit ()
340
347
341
- self . _cursor .execute (
348
+ cursor .execute (
342
349
"""--sql
343
350
SELECT created_at
344
351
FROM images
@@ -347,15 +354,16 @@ def save(
347
354
(image_name ,),
348
355
)
349
356
350
- created_at = datetime .fromisoformat (self . _cursor .fetchone ()[0 ])
357
+ created_at = datetime .fromisoformat (cursor .fetchone ()[0 ])
351
358
352
359
return created_at
353
360
except sqlite3 .Error as e :
354
361
self ._conn .rollback ()
355
362
raise ImageRecordSaveException from e
356
363
357
364
def get_most_recent_image_for_board (self , board_id : str ) -> Optional [ImageRecord ]:
358
- self ._cursor .execute (
365
+ cursor = self ._conn .cursor ()
366
+ cursor .execute (
359
367
"""--sql
360
368
SELECT images.*
361
369
FROM images
@@ -368,7 +376,7 @@ def get_most_recent_image_for_board(self, board_id: str) -> Optional[ImageRecord
368
376
(board_id ,),
369
377
)
370
378
371
- result = cast (Optional [sqlite3 .Row ], self . _cursor .fetchone ())
379
+ result = cast (Optional [sqlite3 .Row ], cursor .fetchone ())
372
380
373
381
if result is None :
374
382
return None
0 commit comments