@@ -41,15 +41,15 @@ def _insert(self, data: List):
41
41
model = data [3 ]
42
42
answer_type = 0
43
43
embedding_data = embedding_data .tobytes ()
44
+ is_deleted = 0
44
45
45
46
table_name = "cache_codegpt_answer"
46
- insert_sql = "INSERT INTO {} (question, answer, answer_type, model, embedding_data) VALUES (%s, %s, %s, %s, _binary%s)" .format (table_name )
47
-
47
+ insert_sql = "INSERT INTO {} (question, answer, answer_type, model, embedding_data, is_deleted) VALUES (%s, %s, %s, %s, _binary%s, %s)" .format (table_name )
48
48
conn = self .pool .connection ()
49
49
try :
50
50
with conn .cursor () as cursor :
51
51
# 执行插入数据操作
52
- values = (question , answer , answer_type , model , embedding_data )
52
+ values = (question , answer , answer_type , model , embedding_data , is_deleted )
53
53
cursor .execute (insert_sql , values )
54
54
conn .commit ()
55
55
id = cursor .lastrowid
@@ -127,18 +127,30 @@ def update_hit_count_by_id(self, primary_id: int):
127
127
conn .close ()
128
128
129
129
def get_ids (self , deleted = True ):
130
- pass
130
+ table_name = "cache_codegpt_answer"
131
+ state = 1 if deleted else 0
132
+ query_sql = "Select id FROM {} WHERE is_deleted = {}" .format (table_name , state )
133
+
134
+ conn = self .pool .connection ()
135
+ try :
136
+ with conn .cursor () as cursor :
137
+ cursor .execute (query_sql )
138
+ ids = [row [0 ] for row in cursor .fetchall ()]
139
+ finally :
140
+ conn .close ()
141
+
142
+ return ids
131
143
132
144
def mark_deleted (self , keys ):
133
145
table_name = "cache_codegpt_answer"
134
- delete_sql = "Delete from {} WHERE id in ({})" .format (table_name , "," .join ([str (i ) for i in keys ]))
146
+ mark_sql = " update {} set is_deleted=1 WHERE id in ({})" .format (table_name , "," .join ([str (i ) for i in keys ]))
135
147
136
148
# 从连接池中获取连接
137
149
conn = self .pool .connection ()
138
150
try :
139
151
with conn .cursor () as cursor :
140
152
# 执行删除数据操作
141
- cursor .execute (delete_sql )
153
+ cursor .execute (mark_sql )
142
154
delete_count = cursor .rowcount
143
155
conn .commit ()
144
156
finally :
@@ -169,10 +181,36 @@ def model_deleted(self, model_name):
169
181
return resp
170
182
171
183
def clear_deleted_data (self ):
172
- pass
184
+ table_name = "cache_codegpt_answer"
185
+ delete_sql = "DELETE FROM {} WHERE is_deleted = 1" .format (table_name )
186
+
187
+ conn = self .pool .connection ()
188
+ try :
189
+ with conn .cursor () as cursor :
190
+ cursor .execute (delete_sql )
191
+ delete_count = cursor .rowcount
192
+ conn .commit ()
193
+ finally :
194
+ conn .close ()
195
+
196
+ return delete_count
173
197
174
198
def count (self , state : int = 0 , is_all : bool = False ):
175
- pass
199
+ table_name = "cache_codegpt_answer"
200
+ if is_all :
201
+ count_sql = "SELECT COUNT(*) FROM {}" .format (table_name )
202
+ else :
203
+ count_sql = "SELECT COUNT(*) FROM {} WHERE is_deleted = {}" .format (table_name ,state )
204
+
205
+ conn = self .pool .connection ()
206
+ try :
207
+ with conn .cursor () as cursor :
208
+ cursor .execute (count_sql )
209
+ num = cursor .fetchone ()[0 ]
210
+ finally :
211
+ conn .close ()
212
+
213
+ return num
176
214
177
215
def close (self ):
178
216
pass
0 commit comments