From: nishi Date: Sun, 26 May 2024 16:06:30 +0000 (+0000) Subject: add insert X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=6f8201a7d4daecc09b7c6741888530882ce0c573;p=dataworks.git add insert git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@122 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- diff --git a/FORMATv1.md b/FORMATv1.md index e409d20..485b91a 100644 --- a/FORMATv1.md +++ b/FORMATv1.md @@ -45,7 +45,7 @@ There are 5 types for `indexentry`: | Type | Character | Type | Info | | ---- | --------- | ---- | ---- | | String | `S` | ASCII | | -| Integer | `I` | uint64\_t | | +| Integer | `I` | int64\_t | | | Double | `D` | double | | | Logical | `L` | uint8\_t | 0 for false, other value for true | | Help | `?` | ASCII | Should be just ignored | diff --git a/Library/Makefile b/Library/Makefile index f20e665..6f24ebd 100644 --- a/Library/Makefile +++ b/Library/Makefile @@ -3,7 +3,7 @@ .PHONY: all clean .SUFFIXES: .c .o -OBJS = parser.o database.o util.o dataworks.o database_table.o database_db.o exec.o +OBJS = parser.o database.o util.o dataworks.o database_table.o database_db.o database_record.o exec.o all: $(LIB_PREFIX)dataworks$(LIB_SUFFIX) $(STATICLIB_PREFIX)dataworks$(STATICLIB_SUFFIX) diff --git a/Library/database.c b/Library/database.c index cabdb16..31e3980 100644 --- a/Library/database.c +++ b/Library/database.c @@ -156,3 +156,28 @@ void dataworks_database_free(struct dataworks_db* db) { } free(db); } + +int dataworks_database_use_table(struct dataworks_db* db, const char* table) { + int errnum = DW_ERR_SUCCESS; + if(db->name != NULL) free(db->name); + bool has = false; + char** names = dataworks_database_get_table_list(db); + if(names != NULL) { + int k; + for(k = 0; names[k] != NULL; k++) { + if(strcmp(names[k], table) == 0) { + has = true; + } + free(names[k]); + } + free(names); + if(has) { + db->name = __dw_strdup(table); + } else { + errnum = DW_ERR_NOT_USED; + } + } else { + errnum = DW_ERR_PARSER_NULL; + } + return errnum; +} diff --git a/Library/database_record.c b/Library/database_record.c index 6ade2f5..f6e70e1 100644 --- a/Library/database_record.c +++ b/Library/database_record.c @@ -27,3 +27,64 @@ /* --- END LICENSE --- */ #include "dw_database.h" + +#include "dw_util.h" + +#include +#include +#include +#include +#include + +void __dw_add_record(struct dataworks_db* db, uint64_t index, void* _data, uint64_t size, bool set) { + uint8_t* data = _data; + __dw_lockfile(db->fp); + if(db->version == 1) { + char* buf = malloc(1 + 8 + 8 + 1 + 1 + 8 + 8); + fseek(db->fp, 3 + 2 + 8 + (1 + 8 + 1 + 256 + 4096) * 256, SEEK_SET); + struct dataworks_db_v1_dbentry dbentry; + while(1) { + fread(buf, 1, 1 + 8 + 8 + 1 + 1 + 8 + 8, db->fp); + __dw_buffer_to_db_v1_dbentry(buf, dbentry); + if(feof(db->fp)) break; + if(!(dbentry.flag & DATAWORKS_V1_DBENTRY_USED)) { + fseek(db->fp, -(1 + 8 + 8 + 1 + 1 + 8 + 8), SEEK_CUR); + buf[0] |= DATAWORKS_V1_DBENTRY_USED; + fwrite(buf, 1, 1 + 8 + 8 + 1 + 1 + 8 + 8, db->fp); + } + fseek(db->fp, dbentry.size, SEEK_CUR); + } + free(buf); + } + __dw_unlockfile(db->fp); +} + +struct dataworks_db_result* dataworks_database_insert_record(struct dataworks_db* db, void** fields, const char* prop) { + struct dataworks_db_result* r = malloc(sizeof(*r)); + r->error = false; + int i; + char* types = dataworks_database_get_table_field_types(db, db->name); + for(i = 0; prop[i] != 0; i++) + ; + if(strlen(types) != i) { + r->error = true; + r->errnum = DW_ERR_EXEC_INSUFFICIENT_ARGUMENTS; + return r; + } + for(i = 0; prop[i] != 0; i++) { + uint64_t entsize = 0; + if(types[i] == DW_RECORD_STRING) { + entsize = strlen(fields[i]); + } else if(types[i] == DW_RECORD_INTEGER) { + entsize = 8; + } else if(types[i] == DW_RECORD_DOUBLE) { + entsize = sizeof(double); + } else if(types[i] == DW_RECORD_LOGICAL) { + entsize = 1; + } else if(types[i] == DW_RECORD_HELP) { + entsize = strlen(fields[i]); + } + __dw_add_record(db, i, fields[i], entsize, prop[i] == 'S'); + } + return r; +} diff --git a/Library/database_table.c b/Library/database_table.c index 41e1f4c..40ea3a7 100644 --- a/Library/database_table.c +++ b/Library/database_table.c @@ -208,3 +208,51 @@ char** dataworks_database_get_table_fields(struct dataworks_db* db, const char* } return NULL; } + +char* dataworks_database_get_table_field_types(struct dataworks_db* db, const char* table) { + 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); + int c = 0; + 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, table) == 0) { + int j; + int count = 0; + for(j = 0;; j++) { + if(index.fields[j] == 0) break; + count++; + for(; index.fields[j] != 0; j++) + ; + } + char* list = malloc(count + 1); + count = 0; + for(j = 0;; j++) { + if(index.fields[j] == 0) break; + list[count] = index.fields[j]; + count++; + for(; index.fields[j] != 0; j++) + ; + } + list[count] = 0; + free(dbname); + free(buf); + __dw_unlockfile(db->fp); + return list; + } + free(dbname); + } + } + free(buf); + __dw_unlockfile(db->fp); + } + return NULL; +} diff --git a/Library/dw_database.h b/Library/dw_database.h index d0b47f1..8deab74 100644 --- a/Library/dw_database.h +++ b/Library/dw_database.h @@ -57,15 +57,15 @@ extern "C" { memcpy(&dbent.flag, buf, 1); \ uint64_t be; \ memcpy(&be, buf + 1, 8); \ - __dw_native_endian(be, uint64_t, index.length = __converted); \ + __dw_native_endian(be, uint64_t, dbent.length = __converted); \ memcpy(&be, buf + 1 + 8, 8); \ - __dw_native_endian(be, uint64_t, index.size = __converted); \ - memcpy(&dbent.field_index, 1 + 8 + 8, 1); \ - memcpy(&dbent.db_index, 1 + 8 + 8 + 1, 1); \ + __dw_native_endian(be, uint64_t, dbent.size = __converted); \ + memcpy(&dbent.field_index, buf + 1 + 8 + 8, 1); \ + memcpy(&dbent.db_index, buf + 1 + 8 + 8 + 1, 1); \ memcpy(&be, buf + 1 + 8 + 8 + 1 + 1, 8); \ - __dw_native_endian(be, uint64_t, index.count = __converted); \ + __dw_native_endian(be, uint64_t, dbent.count = __converted); \ memcpy(&be, buf + 1 + 8 + 8 + 1 + 1 + 8, 8); \ - __dw_native_endian(be, uint64_t, index.fragnum = __converted); + __dw_native_endian(be, uint64_t, dbent.fragnum = __converted); /** * @~english @@ -201,7 +201,7 @@ enum DW_RECORD_TYPES { /** * @~english - * @brief HELP + * @brief Help * */ DW_RECORD_HELP = '?', @@ -264,20 +264,6 @@ struct dataworks_db { char* name; }; -/** - * @~english - * @brief Database record struct - - */ -struct dataworks_db_record { - /** - * @~english - * @brief Type. See enum DW_RECORD_TYPES. - * - */ - char type; -}; - /** * @~english * @brief Database result struct @@ -464,6 +450,16 @@ char** dataworks_database_get_table_list(struct dataworks_db* db); */ char** dataworks_database_get_table_fields(struct dataworks_db* db, const char* table); +/** + * @~english + * @brief Get the field types of the table of the database. + * @param db Database + * @param table Table name + * @return Field types of the table + * + */ +char* dataworks_database_get_table_field_types(struct dataworks_db* db, const char* table); + /** * @~english * @brief Creates a table. @@ -531,12 +527,23 @@ void dataworks_database_free_result(struct dataworks_db_result* result); * @brief Inserts the record. * @param db Database * @param records Records + * @param fields Fields in binary * @param prop List which contains character `U` or `S` * `U` indicates the field is unset + * `S` indicates the field is set + * @return Result + * + */ +struct dataworks_db_result* dataworks_database_insert_record(struct dataworks_db* db, void** fields, const char* prop); + +/** + * @~english + * @param db Database + * @param table Table name * @return Result * */ -struct dataworks_db_result* dataworks_database_insert_record(struct dataworks_db* db, char** fields, const char* prop); +int dataworks_database_use_table(struct dataworks_db* db, const char* table); #ifdef __cplusplus } diff --git a/Library/exec.c b/Library/exec.c index e379a12..01f07c5 100644 --- a/Library/exec.c +++ b/Library/exec.c @@ -195,31 +195,11 @@ struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_d r->errnum = DW_ERR_EXEC_TOO_MANY_ARGUMENTS; break; } - if(db->name != NULL) free(db->name); - bool has = false; - char** names = dataworks_database_get_table_list(db); - if(names != NULL) { - int k; - for(k = 0; names[k] != NULL; k++) { - if(strcmp(names[k], results[j]->value) == 0) { - has = true; - } - free(names[k]); - } - free(names); - if(has) { - db->name = __dw_strdup(results[j]->value); - set = true; - if(dolog) { - printf("Using table `%s'.\n", db->name); - } - } else { - r->error = true; - r->errnum = DW_ERR_NOT_USED; - } - } else { + r->errnum = dataworks_database_use_table(db, results[j]->value); + if(r->errnum != DW_ERR_SUCCESS) { r->error = true; - r->errnum = DW_ERR_PARSER_NULL; + } else if(dolog) { + printf("Using table `%s`.\n", results[j]->value); } } } @@ -241,6 +221,15 @@ struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_d r->error = true; r->errnum = DW_ERR_EXEC_INSUFFICIENT_ARGUMENTS; } else { + double dn = 123123; + int64_t in = 123123; + void* data[3] = {"data", &dn, &in}; + struct dataworks_db_result* dbr = dataworks_database_insert_record(db, data, "SSS"); + if(dbr->error) { + r->error = true; + r->errnum = dbr->errnum; + } + dataworks_database_free_result(dbr); } } } else {