@@ -52,7 +52,7 @@ def __init__(self, db_uri=None, table_name="store", verbose=False):
52
52
self .table_name = table_name
53
53
if db_uri .startswith ("sqlite:///" ):
54
54
db_path = os .path .dirname (self .db_uri .split ("sqlite:///" )[1 ])
55
- if not os .path .exists (db_path ):
55
+ if db_path and not os .path .exists (db_path ):
56
56
os .makedirs (db_path )
57
57
58
58
def open (self ):
@@ -65,11 +65,10 @@ def open(self):
65
65
else :
66
66
self .engine = create_engine (db_uri , echo = self .verbose , echo_pool = self .verbose )
67
67
68
- metadata = MetaData ()
69
- metadata .bind = self .engine
68
+ self .metadata = MetaData ()
70
69
self .table = Table (
71
70
self .table_name ,
72
- metadata ,
71
+ self . metadata ,
73
72
Column ("key" , String (KEY_LEN ), primary_key = True ),
74
73
Column ("value" , LargeBinary (VALUE_LEN )),
75
74
)
@@ -80,38 +79,48 @@ def close(self):
80
79
81
80
def create (self ):
82
81
self .open ()
83
- self .table .create ()
82
+ with self .engine .connect () as conn :
83
+ with conn .begin ():
84
+ self .metadata .create_all (conn )
84
85
self .close ()
85
86
86
87
def destroy (self ):
87
88
self .open ()
88
- self .table .drop ()
89
+ with self .engine .connect () as conn :
90
+ with conn .begin ():
91
+ self .metadata .drop_all (conn )
89
92
self .close ()
90
93
91
94
def __iter__ (self ):
92
- rows = select ([self .table .c .key ]).execute ().fetchall ()
93
- for row in rows :
94
- yield row [0 ]
95
+ with self .engine .connect () as conn :
96
+ rows = conn .execute (select (self .table .c .key ))
97
+ for row in rows :
98
+ yield row [0 ]
95
99
96
100
def __delitem__ (self , key ):
97
- self .table .delete ().where (self .table .c .key == key ).execute ()
101
+ with self .engine .connect () as conn :
102
+ with conn .begin ():
103
+ conn .execute (self .table .delete ().where (self .table .c .key == key ))
98
104
99
105
def __getitem__ (self , key ):
100
- value = select ([self .table .c .value ], self .table .c .key == key ).execute ().fetchone ()
101
- if value is not None :
102
- return value [0 ]
103
- else :
104
- raise KeyError (key )
106
+ with self .engine .connect () as conn :
107
+ value = conn .execute (select (self .table .c .value ).where (self .table .c .key == key )).fetchone ()
108
+ if value is not None :
109
+ return value [0 ]
110
+ else :
111
+ raise KeyError (key )
105
112
106
113
def __setitem__ (self , key , value ):
107
- try :
108
- self .table .insert ().execute (key = key , value = value )
109
- except IntegrityError :
110
- if NAMESPACE_USERPROFILES in self .db_uri :
111
- # userprofiles namespace does support revisions so we update existing row
112
- self .table .update ().execute (key = key , value = value )
113
- else :
114
- raise
114
+ with self .engine .connect () as conn :
115
+ with conn .begin ():
116
+ try :
117
+ conn .execute (self .table .insert ().values (key = key , value = value ))
118
+ except IntegrityError :
119
+ if NAMESPACE_USERPROFILES in self .db_uri :
120
+ # userprofiles namespace does support revisions so we update existing row
121
+ conn .execute (self .table .update ().values (key = key , value = value ))
122
+ else :
123
+ raise
115
124
116
125
117
126
class FileStore (FileMutableStoreMixin , BytesStore , FileMutableStoreBase ):
0 commit comments