📄 main.c
字号:
info->primary = conv_boolean(PQgetvalue(res, 0, 1)); strcpy(indexrelid, PQgetvalue(res, 0, 2)); PQclear(res); if (do_query((PGconn *)handle, "Unable to get index info: &1", &res, query_field, 1, indexrelid)) return TRUE; DB.Query.Init(); for (i = 0; i < PQntuples(res); i++) { if (i > 0) DB.Query.Add(","); DB.Query.Add(PQgetvalue(res, i, 0)); } PQclear(res); info->fields = DB.Query.GetNew(); return FALSE;}/***************************************************************************** index_delete() Deletes an index. <handle> is the database handle. <table> is the table name. <index> is the index name. This function returns TRUE if the command has failed, and FALSE if everything was OK.*****************************************************************************/static int index_delete(DB_DATABASE handle, char *table, char *index){ return do_query((PGconn *)handle, "Unable to delete index: &1", NULL, "drop index \"&1\"", 1, index);}/***************************************************************************** index_create() Creates an index. <handle> is the database handle. <table> is the table name. <index> is the index name. <info> points to a structure describing the index. This function returns TRUE if the command has failed, and FALSE if everything was OK.*****************************************************************************/static int index_create(DB_DATABASE handle, char *table, char *index, DB_INDEX *info){ DB.Query.Init(); DB.Query.Add("CREATE "); if (info->unique) DB.Query.Add("UNIQUE "); DB.Query.Add("INDEX \""); DB.Query.Add(index); DB.Query.Add("\" ON "); DB.Query.Add(table); DB.Query.Add(" ( "); DB.Query.Add(info->fields); DB.Query.Add(" )"); return do_query((PGconn *)handle, "Cannot create index: &1", NULL, DB.Query.Get(), 0);}/***************************************************************************** database_exist() Returns if a database exists <handle> is any database handle. <name> is the database name. This function returns TRUE if the database exists, and FALSE if not.*****************************************************************************/static int database_exist(DB_DATABASE handle, char *name){ const char *query = "select datname from pg_database where (datallowconn = 't') " "and (datname = lower('&1'))"; PGconn *conn = (PGconn *)handle; PGresult *res; int exist; if (do_query(conn, "Unable to check database: &1", &res, query, 1, name)) return FALSE; exist = PQntuples(res) == 1; PQclear(res); return exist;}/***************************************************************************** database_list() Returns an array containing the name of each database <handle> is any database handle. <databases> points to a variable that will receive the char* array. This function returns the number of databases, or -1 if the command has failed. Be careful: <databases> can be NULL, so that just the count is returned.*****************************************************************************/static long database_list(DB_DATABASE handle, char ***databases){ const char *query = "select datname from pg_database where datallowconn and datname <> 'template1'"; PGconn *conn = (PGconn *)handle; PGresult *res; int i; long count; if (do_query(conn, "Unable to get databases: &1", &res, query, 0)) return -1; if (databases) { GB.NewArray(databases, sizeof(char *), PQntuples(res)); for (i = 0; i < PQntuples(res); i++) GB.NewString(&((*databases)[i]), PQgetvalue(res, i, 0), 0); } count = PQntuples(res); PQclear(res); return count;}/***************************************************************************** database_is_system() Returns if a database is a system database. <handle> is any database handle. <name> is the database name. This function returns TRUE if the database is a system database, and FALSE if not.*****************************************************************************/static int database_is_system(DB_DATABASE handle, char *name){ const char *query = "select datname from pg_database where datallowconn " "and (datname = lower('&1')) and datistemplate"; PGconn *conn = (PGconn *)handle; PGresult *res; int system; if (do_query(conn, "Unable to check database: &1", &res, query, 1, name)) return TRUE; system = PQntuples(res) == 1; PQclear(res); return system;}/***************************************************************************** table_type() Not Valid in postgresql <handle> is the database handle. <table> is the table name.*****************************************************************************/static char *table_type(DB_DATABASE handle, char *table, char *type){ if (type) GB.Error("PostgreSQL does not have any table types"); return NULL;}/***************************************************************************** database_delete() Deletes a database. <handle> is the database handle. <name> is the database name. This function returns TRUE if the command has failed, and FALSE if everything was OK.*****************************************************************************/static int database_delete(DB_DATABASE handle, char *name){ return do_query((PGconn *)handle, "Unable to delete database: &1", NULL, "drop database \"&1\"", 1, name);}/***************************************************************************** database_create() Creates a database. <handle> is the database handle. <name> is the database name. This function returns TRUE if the command has failed, and FALSE if everything was OK.*****************************************************************************/static int database_create(DB_DATABASE handle, char *name){ return do_query((PGconn *)handle, "Unable to create database: &1", NULL, "create database \"&1\"", 1, name);}/***************************************************************************** user_exist() Returns if a user exists. <handle> is any database handle. <name> is the user name. This function returns TRUE if the user exists, and FALSE if not.*****************************************************************************/static int user_exist(DB_DATABASE handle, char *name){ const char *query = "select usename from pg_user " "where usename = '&1' "; PGresult *res; int exist; if (do_query((PGconn *)handle, "Unable to check user: &1", &res, query, 1, name)) return FALSE; exist = PQntuples(res) == 1; PQclear(res); return exist;}/***************************************************************************** user_list() Returns an array containing the name of each user. <handle> is the database handle. <users> points to a variable that will receive the char* array. This function returns the number of users, or -1 if the command has failed. Be careful: <users> can be NULL, so that just the count is returned.*****************************************************************************/static long user_list(DB_DATABASE handle, char ***users){ const char *query = "select usename from pg_user "; PGresult *res; int i; long count; if (do_query((PGconn *)handle, "Unable to get users: &1", &res, query, 0)) return -1; if (users) { GB.NewArray(users, sizeof(char *), PQntuples(res)); for (i = 0; i < PQntuples(res); i++) GB.NewString(&((*users)[i]), PQgetvalue(res, i, 0), 0); } count = PQntuples(res); PQclear(res); return count;}/***************************************************************************** user_info() Get user description <handle> is the database handle. <name> is the user name. <info> points to a structure filled by the function. This function returns TRUE if the command has failed, and FALSE if everything was OK.*****************************************************************************/static int user_info(DB_DATABASE handle, char *name, DB_USER *info){ const char *query = "select usecreatedb, usesuper from pg_user " "where usename = '&1' "; const char *query_passwd = "select passwd from pg_shadow " "where usename = '&1' "; PGresult *res; if (do_query((PGconn *)handle, "Unable to get user info: &1", &res, query, 1, name)) return TRUE; if (PQntuples(res) != 1) { GB.Error("Unable to find user &1", name); return TRUE; } info->name = NULL; info->admin = conv_boolean(PQgetvalue(res, 0, 1)); /* conv_boolean(PQgetvalue(res, 0, 0)) || */ PQclear(res); if (!do_query((PGconn *)handle, NULL, &res, query_passwd, 1, name)) { if (*PQgetvalue(res, 0, 0)) GB.NewString(&info->password, "***", 0); } return FALSE;}/***************************************************************************** user_delete() Deletes a user. <handle> is any database handle. <name> is the user name. This function returns TRUE if the command has failed, and FALSE if everything was OK.*****************************************************************************/static int user_delete(DB_DATABASE handle, char *name){ return do_query((PGconn *)handle, "Unable to delete user: &1", NULL, "drop user \"&1\"", 1, name);}/***************************************************************************** user_create() Creates a user. <handle> is the database handle. <name> is the user name. <info> points to a structure describing the user. This function returns TRUE if the command has failed, and FALSE if everything was OK.*****************************************************************************/static int user_create(DB_DATABASE handle, char *name, DB_USER *info){ DB.Query.Init(); DB.Query.Add("CREATE USER "); DB.Query.Add(QUOTE_STRING); DB.Query.Add(name); DB.Query.Add(QUOTE_STRING); if (info->admin) DB.Query.Add(" CREATEDB CREATEUSER"); else DB.Query.Add(" NOCREATEDB NOCREATEUSER"); if (info->password && *info->password) { DB.Query.Add(" PASSWORD '"); DB.Query.Add(info->password); DB.Query.Add("'"); } /*_print_query = TRUE;*/ return do_query((PGconn *)handle, "Cannot create user: &1", NULL, DB.Query.Get(), 0);}/***************************************************************************** user_set_password() Change the user password. <handle> is the database handle. <name> is the user name. <password> is the new password This function returns TRUE if the command has failed, and FALSE if everything was OK.*****************************************************************************/static int user_set_password(DB_DATABASE handle, char *name, char *password){ DB.Query.Init(); DB.Query.Add("ALTER USER \""); DB.Query.Add(name); DB.Query.Add("\" PASSWORD '"); DB.Query.Add(password); DB.Query.Add("'"); return do_query((PGconn *)handle, "Cannot change user password: &1", NULL, DB.Query.Get(), 0);}/***************************************************************************** The driver interface*****************************************************************************/static DB_DRIVER _driver ={ "postgresql", (void *)open_database, (void *)close_database, (void *)format_value, (void *)exec_query, (void *)begin_transaction, (void *)commit_transaction, (void *)rollback_transaction, (void *)get_quote, { (void *)query_init, (void *)query_fill, (void *)query_release, { (void *)field_type, (void *)field_name, (void *)field_index, (void *)field_length, }, }, { (void *)field_exist, (void *)field_list, (void *)field_info, }, { (void *)table_init, (void *)table_index, (void *)table_release, (void *)table_exist, (void *)table_list, (void *)table_primary_key, (void *)table_is_system, (void *)table_type, (void *)table_delete, (void *)table_create, }, { (void *)index_exist, (void *)index_list, (void *)index_info, (void *)index_delete, (void *)index_create, }, { (void *)database_exist, (void *)database_list, (void *)database_is_system, (void *)database_delete, (void *)database_create, }, { (void *)user_exist, (void *)user_list, (void *)user_info, (void *)user_delete, (void *)user_create, (void *)user_set_password }};/***************************************************************************** The component entry and exit functions.*****************************************************************************/int GB_INIT(void){ GB.GetInterface("gb.db", DB_INTERFACE_VERSION, &DB); DB.Register(&_driver); return FALSE;}void GB_EXIT(){}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -