From: nishi Date: Fri, 24 May 2024 03:06:42 +0000 (+0000) Subject: delete_table works X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=6b0452b7227ca12853c672083ac68f52741beaee;p=dataworks.git delete_table works git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@101 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- diff --git a/Library/database.c b/Library/database.c index 91fe598..a18cdd7 100644 --- a/Library/database.c +++ b/Library/database.c @@ -38,7 +38,7 @@ const char sig[3] = {0x7f, 'D', 'W'}; -const char* dw_errors[] = {"Success", "Used already", "File open fail", "Invalid signature", "Invalid version", "Parser returned NULL", "Cannot call non-method", "Unknown method", "Insufficient arguments", "Too many arguments"}; +const char* dw_errors[] = {"Success", "Used already", "File open fail", "Invalid signature", "Invalid version", "Parser returned NULL", "Cannot call non-method", "Unknown method", "Insufficient arguments", "Too many arguments", "Not used"}; #ifdef M_I86 #define BUFSIZE 128 diff --git a/Library/database_exec.c b/Library/database_exec.c index 73b4840..7d7ba7b 100644 --- a/Library/database_exec.c +++ b/Library/database_exec.c @@ -144,6 +144,29 @@ struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_d free(fields); free(fieldtypes); } + } else if(__dw_strcaseequ(token->name, "delete_table")) { + argc = 0; + int j; + for(j = 0; results[j] != NULL; j++) { + if(results[j]->value != NULL) { + argc++; + } + } + if(argc < 1) { + r->error = true; + r->errnum = DW_ERR_EXEC_INSUFFICIENT_ARGUMENTS; + } else { + for(j = 0; results[j] != NULL; j++) { + if(results[j]->value != NULL) { + int er = dataworks_database_delete_table(db, results[j]->value); + if(er != DW_ERR_SUCCESS) { + r->error = true; + r->errnum = er; + break; + } + } + } + } } else if(__dw_strcaseequ(token->name, "concat")) { r->value = malloc(1); r->value[0] = 0; @@ -190,6 +213,9 @@ struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_d if(dolog) { printf("Using table `%s'.\n", db->name); } + } else { + r->error = true; + r->errnum = DW_ERR_NOT_USED; } } else { r->error = true; diff --git a/Library/database_table.c b/Library/database_table.c index cc9059b..2bb2ae9 100644 --- a/Library/database_table.c +++ b/Library/database_table.c @@ -34,6 +34,37 @@ #include #include +int dataworks_database_delete_table(struct dataworks_db* db, const char* name) { + bool rm = false; + if(db->version == 1) { + __dw_lockfile(db->fp); + fseek(db->fp, 3 + 10, SEEK_SET); + int i; + struct dataworks_db_v1_indexentry index; + char* buf = malloc(1 + 8 + 1 + 256 + 4096); + for(i = 0; i < 256; i++) { + fread(buf, 1, 1 + 8 + 1 + 256 + 4096, db->fp); + __dw_buffer_to_db_v1_indexentry(buf, index); + if(index.flag & DATAWORKS_V1_INDEXENTRY_USED) { + char* dbname = malloc(index.dbname_len + 1); + memcpy(dbname, index.dbname, index.dbname_len); + dbname[index.dbname_len] = 0; + if(strcmp(dbname, name) == 0) { + fseek(db->fp, -(1 + 8 + 1 + 256 + 4096), SEEK_CUR); + buf[0] &= ~DATAWORKS_V1_INDEXENTRY_USED; + fwrite(buf, 1, 1 + 8 + 1 + 256 + 4096, db->fp); + rm = true; + } + free(dbname); + } + } + free(buf); + __dw_unlockfile(db->fp); + } + if(rm) return DW_ERR_SUCCESS; + return DW_ERR_NOT_USED; +} + int dataworks_database_create_table(struct dataworks_db* db, const char* name, char** fields, const char* fieldtypes) { if(db->version == 1) { __dw_lockfile(db->fp); @@ -80,6 +111,7 @@ int dataworks_database_create_table(struct dataworks_db* db, const char* name, c __dw_unlockfile(db->fp); dataworks_database_update_mtime(db); } + return DW_ERR_SUCCESS; } char** dataworks_database_get_table_list(struct dataworks_db* db) { diff --git a/Library/dw_database.h b/Library/dw_database.h index 7f2b43d..cd25f13 100644 --- a/Library/dw_database.h +++ b/Library/dw_database.h @@ -128,6 +128,13 @@ enum DW_ERRORS { * */ DW_ERR_EXEC_TOO_MANY_ARGUMENTS, + + /** + * @~english + * @brief Not used + * + */ + DW_ERR_NOT_USED, }; /** @@ -322,12 +329,24 @@ char** dataworks_database_get_table_list(struct dataworks_db* db); * @param fields Fields * @param fieldtypes Types * @return - * - `0` if success + * - `DW_ERR_SUCCESS` if success * - `DW_ERR_USED` if the name is already used * */ int dataworks_database_create_table(struct dataworks_db* db, const char* name, char** fields, const char* fieldtypes); +/** + * @~english + * @brief Deletes a table. + * @param db Database + * @param name Table name + * @return + * - `DW_ERR_SUCCESS` if success + * - `DW_ERR_NOT_USED` if the name is not used + * + */ +int dataworks_database_delete_table(struct dataworks_db* db, const char* name); + /** * @~english * @brief Converts error number to a string.