@@ -41,15 +41,15 @@ def _insert(self, data: List):
4141 model = data [3 ]
4242 answer_type = 0
4343 embedding_data = embedding_data .tobytes ()
44+ is_deleted = 0
4445
4546 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 )
4848 conn = self .pool .connection ()
4949 try :
5050 with conn .cursor () as cursor :
5151 # 执行插入数据操作
52- values = (question , answer , answer_type , model , embedding_data )
52+ values = (question , answer , answer_type , model , embedding_data , is_deleted )
5353 cursor .execute (insert_sql , values )
5454 conn .commit ()
5555 id = cursor .lastrowid
@@ -127,18 +127,30 @@ def update_hit_count_by_id(self, primary_id: int):
127127 conn .close ()
128128
129129 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
131143
132144 def mark_deleted (self , keys ):
133145 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 ]))
135147
136148 # 从连接池中获取连接
137149 conn = self .pool .connection ()
138150 try :
139151 with conn .cursor () as cursor :
140152 # 执行删除数据操作
141- cursor .execute (delete_sql )
153+ cursor .execute (mark_sql )
142154 delete_count = cursor .rowcount
143155 conn .commit ()
144156 finally :
@@ -169,10 +181,36 @@ def model_deleted(self, model_name):
169181 return resp
170182
171183 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
173197
174198 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
176214
177215 def close (self ):
178216 pass
0 commit comments