Skip to content

Commit

Permalink
Support dm8 database
Browse files Browse the repository at this point in the history
  • Loading branch information
杨赫然 committed Oct 10, 2023
1 parent 7bb8866 commit e5e577b
Show file tree
Hide file tree
Showing 18 changed files with 1,249 additions and 49 deletions.
10 changes: 9 additions & 1 deletion common/branch-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ open_db (SeafBranchManager *mgr)

char *sql;
switch (seaf_db_type (mgr->seaf->db)) {
case SEAF_DB_TYPE_DM:
sql = "CREATE TABLE IF NOT EXISTS Branch ("
"id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT, "
"name VARCHAR(10), repo_id VARCHAR(41), commit_id VARCHAR(41))";
if (seaf_db_query (mgr->seaf->db, sql) < 0)
return -1;
break;
case SEAF_DB_TYPE_MYSQL:
sql = "CREATE TABLE IF NOT EXISTS Branch ("
"id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT, "
Expand Down Expand Up @@ -200,7 +207,7 @@ seaf_branch_manager_add_branch (SeafBranchManager *mgr, SeafBranch *branch)
char *sql;
SeafDB *db = mgr->seaf->db;

if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL) {
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL || seaf_db_type(db) == SEAF_DB_TYPE_DM) {
gboolean exists, err;
int rc;

Expand Down Expand Up @@ -382,6 +389,7 @@ seaf_branch_manager_test_and_update_branch (SeafBranchManager *mgr,
switch (seaf_db_type (mgr->seaf->db)) {
case SEAF_DB_TYPE_MYSQL:
case SEAF_DB_TYPE_PGSQL:
case SEAF_DB_TYPE_DM:
sql = "SELECT commit_id FROM Branch WHERE name=? "
"AND repo_id=? FOR UPDATE";
break;
Expand Down
86 changes: 66 additions & 20 deletions common/group-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ open_db (CcnetGroupManager *manager)
db = open_sqlite_db (manager);
break;
case SEAF_DB_TYPE_PGSQL:
case SEAF_DB_TYPE_DM:
case SEAF_DB_TYPE_MYSQL:
db = manager->session->ccnet_db;
break;
Expand Down Expand Up @@ -225,6 +226,46 @@ static int check_db_table (CcnetGroupManager *manager, CcnetDB *db)
// return -1;
//}

} else if (db_type == SEAF_DB_TYPE_DM) {
g_string_printf (group_sql,
"CREATE TABLE IF NOT EXISTS \"%s\" (group_id INTEGER"
" PRIMARY KEY AUTO_INCREMENT, group_name VARCHAR(255),"
" creator_name VARCHAR(255), timestamp BIGINT,"
" type VARCHAR(32), parent_group_id INTEGER)", table_name);
if (seaf_db_query (db, group_sql->str) < 0) {
g_string_free (group_sql, TRUE);
return -1;
}

sql = "CREATE TABLE IF NOT EXISTS GroupUser (group_id INTEGER, "
"user_name VARCHAR(255), is_staff tinyint)";
if (seaf_db_query (db, sql) < 0)
return -1;

sql = "CREATE UNIQUE INDEX IF NOT EXISTS groupid_username_indx on "
"GroupUser (group_id, user_name)";
if (seaf_db_query (db, sql) < 0)
return -1;

sql = "CREATE INDEX IF NOT EXISTS username_indx on "
"GroupUser (user_name)";
if (seaf_db_query (db, sql) < 0)
return -1;

sql = "CREATE TABLE IF NOT EXISTS GroupDNPair (group_id INTEGER,"
" dn VARCHAR(255))";
if (seaf_db_query (db, sql) < 0)
return -1;

sql = "CREATE TABLE IF NOT EXISTS GroupStructure (group_id INTEGER PRIMARY KEY, "
"path VARCHAR(1024))";
if (seaf_db_query (db, sql) < 0)
return -1;

sql = "CREATE INDEX IF NOT EXISTS path_indx on "
"GroupStructure (path)";
if (seaf_db_query (db, sql) < 0)
return -1;
}
g_string_free (group_sql, TRUE);

Expand Down Expand Up @@ -267,7 +308,7 @@ create_group_common (CcnetGroupManager *mgr,

char *user_name_l = g_ascii_strdown (user_name, -1);

if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL)
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL || seaf_db_type(db) == SEAF_DB_TYPE_DM)
g_string_printf (sql,
"INSERT INTO \"%s\"(group_name, "
"creator_name, timestamp, parent_group_id) VALUES(?, ?, ?, ?)", table_name);
Expand All @@ -281,7 +322,7 @@ create_group_common (CcnetGroupManager *mgr,
"int64", now, "int", parent_group_id) < 0)
goto error;

if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL)
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL || seaf_db_type(db) == SEAF_DB_TYPE_DM)
g_string_printf (sql,
"SELECT group_id FROM \"%s\" WHERE "
"group_name = ? AND creator_name = ? "
Expand Down Expand Up @@ -472,7 +513,7 @@ int ccnet_group_manager_remove_group (CcnetGroupManager *mgr,
* can remove group.
*/
if (remove_anyway != TRUE) {
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL)
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL || seaf_db_type(db) == SEAF_DB_TYPE_DM)
g_string_printf (sql, "SELECT 1 FROM \"%s\" WHERE parent_group_id=?", table_name);
else
g_string_printf (sql, "SELECT 1 FROM `%s` WHERE parent_group_id=?", table_name);
Expand All @@ -489,7 +530,7 @@ int ccnet_group_manager_remove_group (CcnetGroupManager *mgr,
}
}

if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL)
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL || seaf_db_type(db) == SEAF_DB_TYPE_DM)
g_string_printf (sql, "DELETE FROM \"%s\" WHERE group_id=?", table_name);
else
g_string_printf (sql, "DELETE FROM `%s` WHERE group_id=?", table_name);
Expand All @@ -499,7 +540,7 @@ int ccnet_group_manager_remove_group (CcnetGroupManager *mgr,
seaf_db_statement_query (db, sql->str, 1, "int", group_id);

g_string_printf (sql, "DELETE FROM GroupStructure WHERE group_id=?");
seaf_db_statement_query (db, sql->str, 1, "int", group_id);
int ret = seaf_db_statement_query (db, sql->str, 1, "int", group_id);

g_string_free (sql, TRUE);

Expand All @@ -513,7 +554,7 @@ check_group_exists (CcnetGroupManager *mgr, CcnetDB *db, int group_id)
const char *table_name = mgr->priv->table_name;
gboolean exists, err;

if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL) {
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL || seaf_db_type(db) == SEAF_DB_TYPE_DM) {
g_string_printf (sql, "SELECT group_id FROM \"%s\" WHERE group_id=?", table_name);
exists = seaf_db_statement_exists (db, sql->str, &err, 1, "int", group_id);
} else {
Expand Down Expand Up @@ -622,7 +663,7 @@ int ccnet_group_manager_set_group_name (CcnetGroupManager *mgr,
GString *sql = g_string_new ("");
CcnetDB *db = mgr->priv->db;

if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL) {
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL || seaf_db_type(db) == SEAF_DB_TYPE_DM) {
g_string_printf (sql, "UPDATE \"%s\" SET group_name = ? "
"WHERE group_id = ?", table_name);
seaf_db_statement_query (db, sql->str, 2, "string", group_name, "int", group_id);
Expand Down Expand Up @@ -697,7 +738,7 @@ ccnet_group_manager_get_ancestor_groups (CcnetGroupManager *mgr, int group_id)
char *path = seaf_db_statement_get_string (db, sql->str, 1, "int", group_id);

if (path) {
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL)
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL || seaf_db_type(db) == SEAF_DB_TYPE_DM)
g_string_printf (sql, "SELECT g.group_id, group_name, creator_name, timestamp, parent_group_id FROM "
"\"%s\" g WHERE g.group_id IN(%s) "
"ORDER BY g.group_id",
Expand Down Expand Up @@ -768,7 +809,7 @@ ccnet_group_manager_get_groups_by_user (CcnetGroupManager *mgr,
CcnetGroup *group;
int parent_group_id = 0, group_id = 0;

if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL)
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL || seaf_db_type(db) == SEAF_DB_TYPE_DM)
g_string_printf (sql,
"SELECT g.group_id, group_name, creator_name, timestamp, parent_group_id FROM "
"\"%s\" g, GroupUser u WHERE g.group_id = u.group_id AND user_name=? ORDER BY g.group_id DESC",
Expand Down Expand Up @@ -827,9 +868,14 @@ ccnet_group_manager_get_groups_by_user (CcnetGroupManager *mgr,
goto out;
}

g_string_printf (sql, "SELECT g.group_id, group_name, creator_name, timestamp, parent_group_id FROM "
"`%s` g WHERE g.group_id IN (%s) ORDER BY g.group_id DESC",
table_name, paths->str);
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL || seaf_db_type(db) == SEAF_DB_TYPE_DM)
g_string_printf (sql, "SELECT g.group_id, group_name, creator_name, timestamp, parent_group_id FROM "
"\"%s\" g WHERE g.group_id IN (%s) ORDER BY g.group_id DESC",
table_name, paths->str);
else
g_string_printf (sql, "SELECT g.group_id, group_name, creator_name, timestamp, parent_group_id FROM "
"`%s` g WHERE g.group_id IN (%s) ORDER BY g.group_id DESC",
table_name, paths->str);
if (seaf_db_statement_foreach_row (db,
sql->str,
get_user_groups_cb,
Expand Down Expand Up @@ -888,7 +934,7 @@ ccnet_group_manager_get_child_groups (CcnetGroupManager *mgr, int group_id,
GList *ret = NULL;
const char *table_name = mgr->priv->table_name;

if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL)
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL || seaf_db_type(db) == SEAF_DB_TYPE_DM)
g_string_printf (sql,
"SELECT group_id, group_name, creator_name, timestamp, parent_group_id FROM "
"\"%s\" WHERE parent_group_id=?", table_name);
Expand Down Expand Up @@ -916,7 +962,7 @@ ccnet_group_manager_get_descendants_groups(CcnetGroupManager *mgr, int group_id,
const char *table_name = mgr->priv->table_name;

GString *sql = g_string_new("");
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL)
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL || seaf_db_type(db) == SEAF_DB_TYPE_DM)
g_string_printf (sql, "SELECT g.group_id, group_name, creator_name, timestamp, "
"parent_group_id FROM \"%s\" g, GroupStructure s "
"WHERE g.group_id=s.group_id "
Expand Down Expand Up @@ -952,7 +998,7 @@ ccnet_group_manager_get_group (CcnetGroupManager *mgr, int group_id,
CcnetGroup *ccnetgroup = NULL;
const char *table_name = mgr->priv->table_name;

if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL)
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL || seaf_db_type(db) == SEAF_DB_TYPE_DM)
g_string_printf (sql,
"SELECT group_id, group_name, creator_name, timestamp, parent_group_id FROM "
"\"%s\" WHERE group_id = ?", table_name);
Expand Down Expand Up @@ -1174,7 +1220,7 @@ ccnet_group_manager_get_top_groups (CcnetGroupManager *mgr,
const char *table_name = mgr->priv->table_name;
int rc;

if (seaf_db_type(mgr->priv->db) == SEAF_DB_TYPE_PGSQL) {
if (seaf_db_type(mgr->priv->db) == SEAF_DB_TYPE_PGSQL || seaf_db_type(mgr->priv->db) == SEAF_DB_TYPE_DM) {
if (including_org)
g_string_printf (sql, "SELECT group_id, group_name, "
"creator_name, timestamp, parent_group_id FROM \"%s\" "
Expand Down Expand Up @@ -1217,7 +1263,7 @@ ccnet_group_manager_list_all_departments (CcnetGroupManager *mgr,
int rc;
int db_type = seaf_db_type(db);

if (db_type == SEAF_DB_TYPE_PGSQL) {
if (db_type == SEAF_DB_TYPE_PGSQL || db_type == SEAF_DB_TYPE_DM) {
g_string_printf (sql, "SELECT group_id, group_name, "
"creator_name, timestamp, type, "
"parent_group_id FROM \"%s\" "
Expand Down Expand Up @@ -1251,7 +1297,7 @@ ccnet_group_manager_get_all_groups (CcnetGroupManager *mgr,
const char *table_name = mgr->priv->table_name;
int rc;

if (seaf_db_type(mgr->priv->db) == SEAF_DB_TYPE_PGSQL) {
if (seaf_db_type(mgr->priv->db) == SEAF_DB_TYPE_PGSQL || seaf_db_type(mgr->priv->db) == SEAF_DB_TYPE_DM) {
if (start == -1 && limit == -1) {
g_string_printf (sql, "SELECT group_id, group_name, "
"creator_name, timestamp, parent_group_id FROM \"%s\" "
Expand Down Expand Up @@ -1301,7 +1347,7 @@ ccnet_group_manager_set_group_creator (CcnetGroupManager *mgr,
const char *table_name = mgr->priv->table_name;
GString *sql = g_string_new ("");

if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL) {
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL || seaf_db_type(db) == SEAF_DB_TYPE_DM) {
g_string_printf (sql, "UPDATE \"%s\" SET creator_name = ? WHERE group_id = ?",
table_name);
} else {
Expand Down Expand Up @@ -1329,7 +1375,7 @@ ccnet_group_manager_search_groups (CcnetGroupManager *mgr,
int rc;
char *db_patt = g_strdup_printf ("%%%s%%", keyword);

if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL) {
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL || seaf_db_type(db) == SEAF_DB_TYPE_DM) {
if (start == -1 && limit == -1) {
g_string_printf (sql,
"SELECT group_id, group_name, "
Expand Down
42 changes: 42 additions & 0 deletions common/org-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ open_db (CcnetOrgManager *manager)
db = open_sqlite_db (manager);
break;
case SEAF_DB_TYPE_PGSQL:
case SEAF_DB_TYPE_DM:
case SEAF_DB_TYPE_MYSQL:
db = manager->session->ccnet_db;
break;
Expand Down Expand Up @@ -203,6 +204,47 @@ static int check_db_table (CcnetDB *db)
// if (seaf_db_query (db, sql) < 0)
// return -1;
//}
} else if (db_type == SEAF_DB_TYPE_DM) {
sql = "CREATE TABLE IF NOT EXISTS Organization (org_id INTEGER"
" PRIMARY KEY, org_name VARCHAR(255),"
" url_prefix VARCHAR(255), "
" creator VARCHAR(255), ctime BIGINT)";
if (seaf_db_query (db, sql) < 0)
return -1;

sql = "CREATE UNIQUE INDEX IF NOT EXISTS url_prefix_indx on "
"Organization (url_prefix)";
if (seaf_db_query (db, sql) < 0)
return -1;

sql = "CREATE TABLE IF NOT EXISTS OrgUser (org_id INTEGER, "
"email VARCHAR(255), is_staff INTEGER NOT NULL)";
if (seaf_db_query (db, sql) < 0)
return -1;

sql = "CREATE INDEX IF NOT EXISTS email_indx on "
"OrgUser (email)";
if (seaf_db_query (db, sql) < 0)
return -1;

sql = "CREATE UNIQUE INDEX IF NOT EXISTS orgid_email_indx on "
"OrgUser (org_id, email)";
if (seaf_db_query (db, sql) < 0)
return -1;

sql = "CREATE TABLE IF NOT EXISTS OrgGroup (org_id INTEGER, "
"group_id INTEGER)";
if (seaf_db_query (db, sql) < 0)
return -1;

sql = "CREATE INDEX IF NOT EXISTS groupid_indx on OrgGroup (group_id)";
if (seaf_db_query (db, sql) < 0)
return -1;

sql = "CREATE UNIQUE INDEX IF NOT EXISTS org_group_indx on "
"OrgGroup (org_id, group_id)";
if (seaf_db_query (db, sql) < 0)
return -1;
}

return 0;
Expand Down
Loading

0 comments on commit e5e577b

Please sign in to comment.