def test_prepare_insert(self): sql, sql_tuple = prepare_insert("users", {"k1": "tommy", "k2": "*****@*****.**"}, ["k1", "k2", "k3"]) assert sql == 'INSERT INTO users(k1 , k2) VALUES (? , ?)' assert sql_tuple == ("tommy", "*****@*****.**") sql, sql_tuple = prepare_insert("users", {"k1": "tommy", "k2": "*****@*****.**"}, ["k1"]) assert sql == 'INSERT INTO users(k1) VALUES (?)' assert sql_tuple == ("tommy",) sql, sql_tuple = prepare_insert("users", {"k1": "tommy", "k2": "*****@*****.**", "k3": 1000}) assert sql == 'INSERT INTO users(k1 , k2 , k3) VALUES (? , ? , ?)' assert sql_tuple == ("tommy", "*****@*****.**", 1000)
def insert(self, email, name, pwd, init_code, user_type): """ Insert user record, and return True if succeed, otherwise False. :param email: Unique email id. :param name: :param pwd: :param init_code: :param user_type: :return:True|False """ if len(self.query({"email": email})) != 0: # self.update(email, {'name': name, 'pwd': pwd, 'init_code': init_code, 'user_type': user_type}) return False cur = self.sql_connection.cursor() crypted_pwd = UserService.hash_new_password(pwd) sql, v = prepare_insert( "users", { "name": name, "email": email, "pwd": crypted_pwd, "init_code": init_code, "user_type": user_type }) cur.execute(sql, v) self.sql_connection.commit() return True
def insert_user_in_instance(self, instance_id, user_id, is_auditor): """ Insert users given instance_id for instances_users table. :param instance_id :param user_id: :param is_auditor: :return: """ sql, k_list = prepare_insert("instances_users", {"user_id": user_id, "instance_id": instance_id, "is_auditor": is_auditor}) cur = self.sql_connection.cursor() cur.execute(sql, k_list) self.sql_connection.commit() return True
def insert(self, instance_id, patient_uid, study_uid, suid, folder_name, total_files_number, status): """ Insert record for new instance. :param instance_id: :param patient_uid: :param study_uid: :param folder_name: :param total_files_number: :param status: study status updates :return: """ if len(self.query({"instance_id": instance_id, "suid": suid})) != 0: return False cur = self.sql_connection.cursor() sql, v = prepare_insert("studies", {"instance_id": instance_id, "patient_uid": patient_uid, "study_uid": study_uid, "suid": suid, "folder_name": folder_name, "total_files_number": total_files_number, "status": status}) cur.execute(sql, v) self.sql_connection.commit() return True
def insert(self, name, modality, description, data_path, has_audit, study_num, annotated_num, status): """ Insert record for new instance. :param name: :param modality: :param description: :param data_path: :param has_audit: :param study_num: :param annotated_num: :param status: 0:initialized, 1:importing, 2: annotating, 3:finished :return: True if insert success. """ if len(self.query({"name": name})) != 0: return -1 cur = self.sql_connection.cursor() sql, v = prepare_insert("instances", {"name": name, "modality": modality, "description": description, "data_path": data_path, "has_audit": has_audit, "study_num": study_num, "annotated_num": annotated_num, "status": status}) cur.execute(sql, v) self.sql_connection.commit() return cur.lastrowid
def insert(self, instance_id, label_type, input_type, text, contour_label_value=-1): """ Insert new record for LabelCandidates. :param instance_id: :param label_type: :param input_type: :param text: :param contour_label_value: id for contour label. -1 for default self-increasing id. :return: """ instance_service = InstanceService(self.sql_connection) if len(instance_service.query({"instance_id": instance_id})) == 0: # There is no such instance ID. return False cur = self.sql_connection.cursor() sql, v = prepare_insert("label_candidates", {"instance_id": instance_id, "label_type": label_type, "input_type": input_type, "text": text, "contour_label_value": contour_label_value}) cur.execute(sql, v) self.sql_connection.commit() return cur.lastrowid
def insert(self, series_id, user_id, file_id, content): """ Insert new record for LabelCandidates. :param series_id: :param user_id: :param file_id: :param content: :return: """ cur = self.sql_connection.cursor() has_record = len(self.query({"series_id": series_id, "user_id": user_id, "file_id": file_id})) > 0 # has_record = self.exist({"series_id": series_id, "user_id": user_id, "file_id": file_id}) if has_record: status = self.update({"series_id": series_id, "user_id": user_id, "file_id": file_id}, {"content": content}) else: sql, v = prepare_insert("labels", {"series_id": series_id, "user_id": user_id, "file_id": file_id, "content": content}) cur.execute(sql, v) self.sql_connection.commit() status = True return status
def insert(self, study_id, series_description, series_path, series_files_list, series_files_number, window_width, window_level, x_spacing, y_spacing, z_spacing, x_dimension, y_dimension, z_dimension, patient_id, series_instance_uid, study_date, intercept, slop, status=0): """ :param study_id: :param series_description: :param series_path: :param series_files_list: :param series_files_number: :param window_width: :param window_level: :param x_spacing: :param y_spacing: :param z_spacing: :param x_dimension: :param y_dimension: :param z_dimension: :param patient_id: :param series_instance_uid: :param study_date: :param intercept: :param slop: :return: """ if len( self.query({ "study_id": study_id, "series_description": series_description })) != 0: return False cur = self.sql_connection.cursor() if window_level is not None and window_width is not None: if pydicom.dataelem.isMultiValue(window_level): window_level = float(window_level[0]) else: window_level = float(window_level) if pydicom.dataelem.isMultiValue(window_width): window_width = float(window_width[0]) else: window_width = float(window_width) else: window_width = 1000 window_level = 0 sql, v = prepare_insert( "series", { "study_id": study_id, "series_description": series_description, "series_path": series_path, "series_files_list": str(series_files_list), "series_files_number": series_files_number, "window_width": window_width or window_width, "window_level": window_level or window_level, "x_spacing": x_spacing, "y_spacing": y_spacing, "z_spacing": z_spacing, "x_dimension": x_dimension, "y_dimension": y_dimension, "z_dimension": z_dimension, "patient_id": patient_id, 'series_instance_uid': series_instance_uid, "study_date": study_date, "intercept": intercept, "slop": slop, "status": status }) cur.execute(sql, v) self.sql_connection.commit() return True