Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support dm8 database #630

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
88 changes: 68 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,48 @@ 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 (id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT, "
"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 (id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT, "
"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 +310,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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PG 的支持已经很久不维护了,所以新写的 SQL 语句并没有考虑对 PG 的支持。因此不能仅通过查找针对 PG 写的语句来替代 REPLACE,要把所有 REPLACE 语句搜索出来检查一下。

g_string_printf (sql,
"INSERT INTO \"%s\"(group_name, "
"creator_name, timestamp, parent_group_id) VALUES(?, ?, ?, ?)", table_name);
Expand All @@ -281,7 +324,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 +515,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 +532,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 +542,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 +556,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 +665,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 +740,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 +811,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 +870,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 +936,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 +964,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 +1000,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 +1222,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 +1265,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 +1299,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 +1349,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 +1377,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
44 changes: 44 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,49 @@ 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 (id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT, "
"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 (id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT, "
"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
Loading