diff --git a/seahub/utils/ccnet_db.py b/seahub/utils/ccnet_db.py index b47268a2213..533cc33d6d5 100644 --- a/seahub/utils/ccnet_db.py +++ b/seahub/utils/ccnet_db.py @@ -4,7 +4,7 @@ def get_ccnet_db_name(): - return os.environ.get('SEAFILE_MYSQL_DB_CCNET_DB_NAME', '') or 'ccnet_db' + return os.environ.get('SEAFILE_MYSQL_DB_CCNET_DB_NAME', '') or 'SYSDBA' class CcnetGroup(object): @@ -40,9 +40,9 @@ def list_org_departments(self, org_id): SELECT g.group_id, group_name, creator_name, timestamp, type, parent_group_id FROM - `{self.db_name}`.`OrgGroup` o + {self.db_name}.OrgGroup o LEFT JOIN - `{self.db_name}`.`Group` g + {self.db_name}.Group g ON o.group_id=g.group_id WHERE org_id={org_id} AND parent_group_id<>0; @@ -93,9 +93,9 @@ def search(q): count_sql = f""" SELECT count(1) FROM - `{self.db_name}`.`EmailUser` t1 + {self.db_name}.EmailUser t1 LEFT JOIN - `{self.db_name}`.`UserRole` t2 + {self.db_name}.UserRole t2 ON t1.email = t2.email WHERE @@ -106,9 +106,9 @@ def search(q): sql = f""" SELECT t1.id, t1.email, t1.is_staff, t1.is_active, t1.ctime, t2.role, t1.passwd FROM - `{self.db_name}`.`EmailUser` t1 + {self.db_name}.EmailUser t1 LEFT JOIN - `{self.db_name}`.`UserRole` t2 + {self.db_name}.UserRol t2 ON t1.email = t2.email WHERE @@ -152,7 +152,7 @@ def get_group_ids_admins_map(self, group_ids): sql = f""" SELECT user_name, group_id FROM - `{self.db_name}`.`GroupUser` + {self.db_name}.GroupUser WHERE group_id IN ({group_ids_str}) AND is_staff = 1 """ @@ -168,7 +168,7 @@ def get_group_ids_admins_map(self, group_ids): def change_groups_into_departments(self, group_id): sql = f""" - UPDATE `{self.db_name}`.`Group` g + UPDATE {self.db_name}.Group g SET g.creator_name = 'system admin', g.parent_group_id = -1 @@ -176,7 +176,7 @@ def change_groups_into_departments(self, group_id): g.group_id = {group_id} """ structure_sql = f""" - INSERT INTO `{self.db_name}`.`GroupStructure` (group_id, path) + INSERT INTO {self.db_name}.GroupStructure (group_id, path) VALUES ('{group_id}', '{group_id}') """ @@ -190,8 +190,8 @@ def get_active_users_by_user_list(self, user_list): user_list_str = ','.join(["'%s'" % str(user) for user in user_list]) active_users = [] sql = f""" - SELECT `email` - FROM `{self.db_name}`.`EmailUser` + SELECT email + FROM {self.db_name}.EmailUser WHERE email IN ({user_list_str}) AND is_active = 1 AND email NOT LIKE '%%@seafile_group' """ @@ -204,7 +204,7 @@ def get_active_users_by_user_list(self, user_list): def get_org_user_count(self, org_id): sql = f""" - SELECT COUNT(1) FROM `{self.db_name}`.`OrgUser` WHERE org_id={org_id} + SELECT COUNT(1) FROM {self.db_name}.OrgUser WHERE org_id={org_id} """ user_count = 0 with connection.cursor() as cursor: diff --git a/seahub/utils/db_api.py b/seahub/utils/db_api.py index a53ab9aaa62..a2aeb02d8b7 100644 --- a/seahub/utils/db_api.py +++ b/seahub/utils/db_api.py @@ -50,7 +50,7 @@ def _get_seafile_db_name(self): if 'sqlite' in config.get('database', 'type'): return '' - db_name = config.get('database', 'db_name') + db_name = config.get('database', 'user') if not db_name: raise Exception("Database name not configured.") @@ -65,7 +65,7 @@ def get_repo_user_share_list(self, repo_id, org_id=''): SELECT s.repo_id, s.from_email, s.to_email, s.permission FROM - `{self.db_name}`.`SharedRepo` s + {self.db_name}.SharedRepo s WHERE repo_id = '{repo_id}'; """ @@ -74,7 +74,7 @@ def get_repo_user_share_list(self, repo_id, org_id=''): SELECT s.repo_id, s.from_email, s.to_email, s.permission FROM - `{self.db_name}`.`OrgSharedRepo` s + {self.db_name}.OrgSharedRepo s WHERE repo_id = '{repo_id}'; """ @@ -106,7 +106,7 @@ def get_repo_group_share_list(self, repo_id, org_id=''): SELECT s.repo_id, s.user_name, s.group_id, s.permission FROM - `{self.db_name}`.`RepoGroup` s + {self.db_name}.RepoGroup s WHERE repo_id = '{repo_id}'; """ @@ -115,7 +115,7 @@ def get_repo_group_share_list(self, repo_id, org_id=''): SELECT s.repo_id, s.owner, s.group_id, s.permission FROM - `{self.db_name}`.`OrgGroupRepo` s + {self.db_name}.OrgGroupRepo s WHERE repo_id = '{repo_id}'; """ @@ -146,7 +146,7 @@ def get_folder_user_share_list(self, repo_id, org_id=''): SELECT v.origin_repo, v.path, s.from_email, s.to_email, s.permission FROM - `{self.db_name}`.`SharedRepo` s join `{self.db_name}`.`VirtualRepo` v + {self.db_name}.SharedRepo s join {self.db_name}.VirtualRepo v ON s.repo_id=v.repo_id WHERE @@ -157,7 +157,7 @@ def get_folder_user_share_list(self, repo_id, org_id=''): SELECT v.origin_repo, v.path, s.from_email, s.to_email, s.permission FROM - `{self.db_name}`.`OrgSharedRepo` s join `{self.db_name}`.`VirtualRepo` v + {self.db_name}.OrgSharedRepo s join {self.db_name}.VirtualRepo v ON s.repo_id=v.repo_id WHERE @@ -191,7 +191,7 @@ def get_folder_group_share_list(self, repo_id, org_id=''): SELECT v.origin_repo, v.path, r.user_name, r.group_id, r.permission FROM - `{self.db_name}`.`RepoGroup` r join `{self.db_name}`.`VirtualRepo` v + {self.db_name}.RepoGroup r join {self.db_name}.VirtualRepo v ON r.repo_id=v.repo_id WHERE @@ -202,7 +202,7 @@ def get_folder_group_share_list(self, repo_id, org_id=''): SELECT v.origin_repo, v.path, r.owner, r.group_id, r.permission FROM - `{self.db_name}`.`OrgGroupRepo` r join `{self.db_name}`.`VirtualRepo` v + {self.db_name}.OrgGroupRepo r join {self.db_name}.VirtualRepo v ON r.repo_id=v.repo_id WHERE @@ -235,11 +235,11 @@ def get_devices_error(self, start, limit): u.repo_id, o.owner_id, u.email, e.token, p.peer_id, p.peer_ip, p.peer_name, p.sync_time, p.client_ver, e.error_time, e.error_con, i.name FROM - `{self.db_name}`.`RepoSyncError` e - LEFT JOIN `{self.db_name}`.`RepoUserToken` u ON e.token = u.token - LEFT JOIN `{self.db_name}`.`RepoInfo` i ON u.repo_id = i.repo_id - LEFT JOIN `{self.db_name}`.`RepoTokenPeerInfo` p ON e.token = p.token - CROSS JOIN `{self.db_name}`.`RepoOwner` o + {self.db_name}.RepoSyncError e + LEFT JOIN {self.db_name}.RepoUserToken u ON e.token = u.token + LEFT JOIN {self.db_name}.RepoInfo i ON u.repo_id = i.repo_id + LEFT JOIN {self.db_name}.RepoTokenPeerInfo p ON e.token = p.token + CROSS JOIN {self.db_name}.RepoOwner o WHERE u.repo_id = o.repo_id ORDER BY @@ -251,11 +251,11 @@ def get_devices_error(self, start, limit): u.repo_id, o.owner_id, u.email, e.token, p.peer_id, p.peer_ip, p.peer_name, p.sync_time, p.client_ver, e.error_time, e.error_con, i.name FROM - `{self.db_name}`.`RepoSyncError` e - LEFT JOIN `{self.db_name}`.`RepoUserToken` u ON e.token = u.token - LEFT JOIN `{self.db_name}`.`RepoInfo` i ON u.repo_id = i.repo_id - LEFT JOIN `{self.db_name}`.`RepoTokenPeerInfo` p ON e.token = p.token - CROSS JOIN `{self.db_name}`.`RepoOwner` o + {self.db_name}.RepoSyncError e + LEFT JOIN {self.db_name}.RepoUserToken u ON e.token = u.token + LEFT JOIN {self.db_name}.RepoInfo i ON u.repo_id = i.repo_id + LEFT JOIN {self.db_name}.RepoTokenPeerInfo p ON e.token = p.token + CROSS JOIN {self.db_name}.RepoOwner o WHERE u.repo_id = o.repo_id ORDER BY @@ -285,8 +285,8 @@ def get_devices_error(self, start, limit): def get_org_trash_repo_list(self, org_id, start, limit): sql = f""" - SELECT repo_id, repo_name, head_id, owner_id, `size`, del_time - FROM `{self.db_name}`.`RepoTrash` + SELECT repo_id, repo_name, head_id, owner_id, size, del_time + FROM {self.db_name}.RepoTrash WHERE org_id = {org_id} ORDER BY del_time DESC LIMIT {limit} OFFSET {start} @@ -321,7 +321,7 @@ def empty_org_repo_trash(self, org_id): def del_repo_trash(cursor, repo_ids): del_file_count_sql = """ DELETE FROM - `%s`.`RepoFileCount` + %s.RepoFileCount WHERE repo_id in %%s; """ % self.db_name @@ -329,7 +329,7 @@ def del_repo_trash(cursor, repo_ids): del_repo_info_sql = """ DELETE FROM - `%s`.`RepoInfo` + %s.RepoInfo WHERE repo_id in %%s; """ % self.db_name @@ -337,7 +337,7 @@ def del_repo_trash(cursor, repo_ids): del_trash_sql = """ DELETE FROM - `%s`.`RepoTrash` + %s.RepoTrash WHERE repo_id in %%s; """ % self.db_name @@ -347,7 +347,7 @@ def del_repo_trash(cursor, repo_ids): SELECT t.repo_id FROM - `{self.db_name}`.`RepoTrash` t + {self.db_name}.RepoTrash t WHERE org_id={org_id}; """ @@ -363,7 +363,7 @@ def del_repo_trash(cursor, repo_ids): def add_repos_to_org_user(self, org_id, username, repo_ids): for repo_id in repo_ids: sql = f""" - INSERT INTO `{self.db_name}`.`OrgRepo` (org_id, repo_id, user) + INSERT INTO {self.db_name}.OrgRepo (org_id, repo_id, user) VALUES ({org_id}, "{repo_id}", "{username}"); """ with connection.cursor() as cursor: @@ -371,9 +371,9 @@ def add_repos_to_org_user(self, org_id, username, repo_ids): def set_repo_type(self, repo_id, repo_type): sql = f""" - UPDATE `{self.db_name}`. `RepoInfo` - SET `type`= '%s' - WHERE `repo_id`='%s' + UPDATE {self.db_name}. RepoInfo + SET type= '%s' + WHERE repo_id='%s' """ % (repo_type, repo_id) with connection.cursor() as cursor: @@ -381,7 +381,7 @@ def set_repo_type(self, repo_id, repo_type): def get_repo_ids_in_repo(self, repo_id): repo_ids_sql = f""" - SELECT repo_id from `{self.db_name}`.`VirtualRepo` where origin_repo="{repo_id}" + SELECT repo_id from {self.db_name}.VirtualRepo where origin_repo="{repo_id}" """ repo_ids = [repo_id, ] with connection.cursor() as cursor: @@ -401,11 +401,11 @@ def set_repo_owner(self, repo_id, new_owner, org_id=None): repo_ids_str = ','.join(["'%s'" % str(rid) for rid in repo_ids]) if org_id: sql = f""" - UPDATE `{self.db_name}`.`OrgRepo` SET user="{new_owner}" WHERE org_id ={org_id} AND repo_id IN ({repo_ids_str}) + UPDATE {self.db_name}.OrgRepo SET user="{new_owner}" WHERE org_id ={org_id} AND repo_id IN ({repo_ids_str}) """ else: sql = f""" - UPDATE `{self.db_name}`.`RepoOwner` SET owner_id="{new_owner}" WHERE repo_id IN ({repo_ids_str}) + UPDATE {self.db_name}.RepoOwner SET owner_id="{new_owner}" WHERE repo_id IN ({repo_ids_str}) """ with connection.cursor() as cursor: cursor.execute(sql) @@ -418,18 +418,18 @@ def set_repo_group_owner(self, repo_id, group_id, current_group_id=None, org_id= current_group_username = "%s@seafile_group" % current_group_id if org_id: sql1 = f""" - DELETE From `{self.db_name}`.`OrgGroupRepo` where owner="{current_group_username}" AND repo_id="{repo_id}" AND org_id="{org_id}" AND group_id="{current_group_id}" + DELETE From {self.db_name}.OrgGroupRepo where owner="{current_group_username}" AND repo_id="{repo_id}" AND org_id="{org_id}" AND group_id="{current_group_id}" """ sql = f""" - INSERT INTO `{self.db_name}`.`OrgGroupRepo` (org_id, repo_id, group_id, owner, permission) VALUES ({org_id}, "{repo_id}", {group_id}, "{group_username}", "rw") + INSERT INTO {self.db_name}.OrgGroupRepo (org_id, repo_id, group_id, owner, permission) VALUES ({org_id}, "{repo_id}", {group_id}, "{group_username}", "rw") ON DUPLICATE KEY UPDATE owner="{group_username}" """ else: sql1 = f""" - DELETE From `{self.db_name}`.`RepoGroup` where user_name="{current_group_username}" AND repo_id="{repo_id}" AND group_id="{current_group_id}" + DELETE From {self.db_name}.RepoGroup where user_name="{current_group_username}" AND repo_id="{repo_id}" AND group_id="{current_group_id}" """ sql = f""" - INSERT INTO `{self.db_name}`.`RepoGroup` (repo_id, group_id, user_name, permission) VALUES ("{repo_id}", {group_id}, "{group_username}", "rw") + INSERT INTO {self.db_name}.RepoGroup (repo_id, group_id, user_name, permission) VALUES ("{repo_id}", {group_id}, "{group_username}", "rw") ON DUPLICATE KEY UPDATE user_name="{group_username}" """ with connection.cursor() as cursor: @@ -443,11 +443,11 @@ def update_repo_user_shares(self, repo_id, new_owner, org_id=None): repo_ids_str = ','.join(["'%s'" % str(rid) for rid in repo_ids]) if org_id: sql = f""" - UPDATE `{self.db_name}`.`OrgSharedRepo` SET from_email="{new_owner}" WHERE org_id={org_id} AND repo_id IN ({repo_ids_str}) + UPDATE {self.db_name}.OrgSharedRepo SET from_email="{new_owner}" WHERE org_id={org_id} AND repo_id IN ({repo_ids_str}) """ else: sql = f""" - UPDATE `{self.db_name}`.`SharedRepo` SET from_email="{new_owner}" WHERE repo_id IN ({repo_ids_str}) + UPDATE {self.db_name}.SharedRepo SET from_email="{new_owner}" WHERE repo_id IN ({repo_ids_str}) """ with connection.cursor() as cursor: cursor.execute(sql) @@ -458,11 +458,11 @@ def update_repo_group_shares(self, repo_id, new_owner, org_id=None): repo_ids_str = ','.join(["'%s'" % str(rid) for rid in repo_ids]) if org_id: sql = f""" - UPDATE `{self.db_name}`.`OrgGroupRepo` SET owner="{new_owner}" WHERE org_id={org_id} AND repo_id IN ({repo_ids_str}) + UPDATE {self.db_name}.OrgGroupRepo SET owner="{new_owner}" WHERE org_id={org_id} AND repo_id IN ({repo_ids_str}) """ else: sql = f""" - UPDATE `{self.db_name}`.`RepoGroup` SET user_name="{new_owner}" WHERE repo_id IN ({repo_ids_str}) + UPDATE {self.db_name}.RepoGroup SET user_name="{new_owner}" WHERE repo_id IN ({repo_ids_str}) """ with connection.cursor() as cursor: cursor.execute(sql) @@ -470,20 +470,20 @@ def update_repo_group_shares(self, repo_id, new_owner, org_id=None): def delete_repo_user_token(self, repo_id, owner): sql = f""" - DELETE FROM `{self.db_name}`.`RepoUserToken` where repo_id="{repo_id}" AND email="{owner}" + DELETE FROM {self.db_name}.RepoUserToken where repo_id="{repo_id}" AND email="{owner}" """ with connection.cursor() as cursor: cursor.execute(sql) - + def get_all_wikis(self, start, limit, order_by): order_by_size_sql = f""" SELECT r.repo_id, i.name, o.owner_id, i.is_encrypted, s.size, i.status, c.file_count, i.update_time FROM - `{self.db_name}`.`Repo` r - LEFT JOIN `{self.db_name}`.`RepoInfo` i ON r.repo_id = i.repo_id - LEFT JOIN `{self.db_name}`.`RepoOwner` o ON i.repo_id = o.repo_id - LEFT JOIN `{self.db_name}`.`RepoSize` s ON s.repo_id = r.repo_id - LEFT JOIN `{self.db_name}`.`RepoFileCount` c ON r.repo_id = c.repo_id + {self.db_name}.Repo r + LEFT JOIN {self.db_name}.RepoInfo i ON r.repo_id = i.repo_id + LEFT JOIN {self.db_name}.RepoOwner o ON i.repo_id = o.repo_id + LEFT JOIN {self.db_name}.RepoSize s ON s.repo_id = r.repo_id + LEFT JOIN {self.db_name}.RepoFileCount c ON r.repo_id = c.repo_id WHERE i.type = 'wiki' ORDER BY @@ -493,11 +493,11 @@ def get_all_wikis(self, start, limit, order_by): order_by_filecount_sql = f""" SELECT r.repo_id, i.name, o.owner_id, i.is_encrypted, s.size, i.status, c.file_count, i.update_time FROM - `{self.db_name}`.`Repo` r - LEFT JOIN `{self.db_name}`.`RepoInfo` i ON r.repo_id = i.repo_id - LEFT JOIN `{self.db_name}`.`RepoOwner` o ON i.repo_id = o.repo_id - LEFT JOIN `{self.db_name}`.`RepoSize` s ON s.repo_id = r.repo_id - LEFT JOIN `{self.db_name}`.`RepoFileCount` c ON r.repo_id = c.repo_id + {self.db_name}.Repo r + LEFT JOIN {self.db_name}.RepoInfo i ON r.repo_id = i.repo_id + LEFT JOIN {self.db_name}.RepoOwner o ON i.repo_id = o.repo_id + LEFT JOIN {self.db_name}.RepoSize s ON s.repo_id = r.repo_id + LEFT JOIN {self.db_name}.RepoFileCount c ON r.repo_id = c.repo_id WHERE i.type = 'wiki' ORDER BY @@ -507,21 +507,21 @@ def get_all_wikis(self, start, limit, order_by): sql = f""" SELECT r.repo_id, i.name, o.owner_id, i.is_encrypted, s.size, i.status, c.file_count, i.update_time FROM - `{self.db_name}`.`Repo` r - LEFT JOIN `{self.db_name}`.`RepoInfo` i ON r.repo_id = i.repo_id - LEFT JOIN `{self.db_name}`.`RepoOwner` o ON r.repo_id = o.repo_id - LEFT JOIN `{self.db_name}`.`RepoSize` s ON r.repo_id = s.repo_id - LEFT JOIN `{self.db_name}`.`RepoFileCount` c ON r.repo_id = c.repo_id + {self.db_name}.Repo r + LEFT JOIN {self.db_name}.RepoInfo i ON r.repo_id = i.repo_id + LEFT JOIN {self.db_name}.RepoOwner o ON r.repo_id = o.repo_id + LEFT JOIN {self.db_name}.RepoSize s ON r.repo_id = s.repo_id + LEFT JOIN {self.db_name}.RepoFileCount c ON r.repo_id = c.repo_id WHERE i.type = 'wiki' LIMIT {limit} OFFSET {start} """ - + with connection.cursor() as cursor: wikis = [] if order_by == 'size': cursor.execute(order_by_size_sql) - + elif order_by == 'file_count': cursor.execute(order_by_filecount_sql) else: