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
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;
if(dolog) {
printf("Using table `%s'.\n", db->name);
}
+ } else {
+ r->error = true;
+ r->errnum = DW_ERR_NOT_USED;
}
} else {
r->error = true;
#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);
__dw_unlockfile(db->fp);
dataworks_database_update_mtime(db);
}
+ return DW_ERR_SUCCESS;
}
char** dataworks_database_get_table_list(struct dataworks_db* db) {
*
*/
DW_ERR_EXEC_TOO_MANY_ARGUMENTS,
+
+ /**
+ * @~english
+ * @brief Not used
+ *
+ */
+ DW_ERR_NOT_USED,
};
/**
* @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.