]> Nishi Git Mirror - dataworks.git/commitdiff
delete_table works
authornishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Fri, 24 May 2024 03:06:42 +0000 (03:06 +0000)
committernishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Fri, 24 May 2024 03:06:42 +0000 (03:06 +0000)
git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@101 d4a5a174-5a4a-5b4b-b672-37683c10d7d5

Library/database.c
Library/database_exec.c
Library/database_table.c
Library/dw_database.h

index 91fe59855621c5157c11f7b456a78fd257d94f57..a18cdd7b3a5c802c86bd84412becc0ce504f3735 100644 (file)
@@ -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
index 73b4840b80a227867bd380795a4756efbf7b681b..7d7ba7ba7f6202c17b38fb8f1fa5ed9561e4a015 100644 (file)
@@ -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;
index cc9059b33537c556ddb889128bfea5cd591561f0..2bb2ae9ef4b5d9f79612c990f6efaa96f4e5fde2 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 
+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) {
index 7f2b43d656bf605a81b7d71f3f383e02ca14cae3..cd25f13ed820d667f54e89f1927b6a5314bdd0f1 100644 (file)
@@ -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.