8
8
# Copyright (c) 2016, Shay Palachy <[email protected] >
9
9
10
10
# standard library imports
11
+ import datetime
11
12
import pickle
12
13
import time
13
- import datetime
14
14
15
15
pyodbc = None
16
16
# third party imports
17
17
with suppress (ImportError ):
18
18
import pyodbc
19
19
20
20
# local imports
21
- from .base import _BaseCore , RecalculationNeeded
21
+ from .base import RecalculationNeeded , _BaseCore
22
22
23
- class _OdbcCore (_BaseCore ):
24
23
24
+ class _OdbcCore (_BaseCore ):
25
25
def __init__ (
26
- self ,
27
- hash_func ,
28
- wait_for_calc_timeout ,
29
- connection_string ,
30
- table_name ,
26
+ self ,
27
+ hash_func ,
28
+ wait_for_calc_timeout ,
29
+ connection_string ,
30
+ table_name ,
31
31
):
32
32
if "pyodbc" not in sys .modules :
33
33
warnings .warn (
@@ -43,7 +43,8 @@ def __init__(
43
43
def ensure_table_exists (self ):
44
44
with pyodbc .connect (self .connection_string ) as conn :
45
45
cursor = conn .cursor ()
46
- cursor .execute (f"""
46
+ cursor .execute (
47
+ f"""
47
48
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'{ self .table_name } ')
48
49
BEGIN
49
50
CREATE TABLE { self .table_name } (
@@ -54,13 +55,17 @@ def ensure_table_exists(self):
54
55
PRIMARY KEY (key)
55
56
);
56
57
END
57
- """ )
58
+ """
59
+ )
58
60
conn .commit ()
59
61
60
62
def get_entry_by_key (self , key ):
61
63
with pyodbc .connect (self .connection_string ) as conn :
62
64
cursor = conn .cursor ()
63
- cursor .execute (f"SELECT value, time, being_calculated FROM { self .table_name } WHERE key = ?" , key )
65
+ cursor .execute (
66
+ f"SELECT value, time, being_calculated FROM { self .table_name } WHERE key = ?" ,
67
+ key ,
68
+ )
64
69
row = cursor .fetchone ()
65
70
if row :
66
71
return {
@@ -73,34 +78,48 @@ def get_entry_by_key(self, key):
73
78
def set_entry (self , key , func_res ):
74
79
with pyodbc .connect (self .connection_string ) as conn :
75
80
cursor = conn .cursor ()
76
- cursor .execute (f"""
81
+ cursor .execute (
82
+ f"""
77
83
MERGE INTO { self .table_name } USING (SELECT 1 AS dummy) AS src ON (key = ?)
78
84
WHEN MATCHED THEN
79
85
UPDATE SET value = ?, time = GETDATE(), being_calculated = 0
80
86
WHEN NOT MATCHED THEN
81
87
INSERT (key, value, time, being_calculated) VALUES (?, ?, GETDATE(), 0);
82
- """ , key , pickle .dumps (func_res ), key , pickle .dumps (func_res ))
88
+ """ ,
89
+ key ,
90
+ pickle .dumps (func_res ),
91
+ key ,
92
+ pickle .dumps (func_res ),
93
+ )
83
94
conn .commit ()
84
95
85
96
def mark_entry_being_calculated (self , key ):
86
97
with pyodbc .connect (self .connection_string ) as conn :
87
98
cursor = conn .cursor ()
88
- cursor .execute (f"UPDATE { self .table_name } SET being_calculated = 1 WHERE key = ?" , key )
99
+ cursor .execute (
100
+ f"UPDATE { self .table_name } SET being_calculated = 1 WHERE key = ?" ,
101
+ key ,
102
+ )
89
103
conn .commit ()
90
104
91
105
def mark_entry_not_calculated (self , key ):
92
106
with pyodbc .connect (self .connection_string ) as conn :
93
107
cursor = conn .cursor ()
94
- cursor .execute (f"UPDATE { self .table_name } SET being_calculated = 0 WHERE key = ?" , key )
108
+ cursor .execute (
109
+ f"UPDATE { self .table_name } SET being_calculated = 0 WHERE key = ?" ,
110
+ key ,
111
+ )
95
112
conn .commit ()
96
113
97
114
def wait_on_entry_calc (self , key ):
98
115
start_time = datetime .datetime .now ()
99
116
while True :
100
117
entry = self .get_entry_by_key (key )
101
- if entry and not entry ['being_calculated' ]:
102
- return entry ['value' ]
103
- if (datetime .datetime .now () - start_time ).total_seconds () > self .wait_for_calc_timeout :
118
+ if entry and not entry ["being_calculated" ]:
119
+ return entry ["value" ]
120
+ if (
121
+ datetime .datetime .now () - start_time
122
+ ).total_seconds () > self .wait_for_calc_timeout :
104
123
raise RecalculationNeeded ()
105
124
time .sleep (1 )
106
125
@@ -113,5 +132,7 @@ def clear_cache(self):
113
132
def clear_being_calculated (self ):
114
133
with pyodbc .connect (self .connection_string ) as conn :
115
134
cursor = conn .cursor ()
116
- cursor .execute (f"UPDATE { self .table_name } SET being_calculated = 0" )
135
+ cursor .execute (
136
+ f"UPDATE { self .table_name } SET being_calculated = 0"
137
+ )
117
138
conn .commit ()
0 commit comments