From f4f08b9281630835c8ffa14197b7c7f5c68e7e78 Mon Sep 17 00:00:00 2001 From: nishi Date: Sun, 26 May 2024 11:58:03 +0000 Subject: [PATCH] add methods git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@121 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- FORMATv1.md | 7 ++-- Library/database.c | 2 +- Library/dw_database.h | 74 +++++++++++++++++++++++++++++++++++++++++++ Library/exec.c | 20 ++++++++++++ 4 files changed, 99 insertions(+), 4 deletions(-) diff --git a/FORMATv1.md b/FORMATv1.md index 3da0874..e409d20 100644 --- a/FORMATv1.md +++ b/FORMATv1.md @@ -16,20 +16,21 @@ Database entry (`dbentry`) MUST be in this format: | Name | Size | Type | Info | | ---- | ---- | ---- | ---- | -| length | 4 bytes | uint32\_t | Size of the entry | -| size | 4 bytes | uint32\_t | | | flag | 1 byte | uint8\_t | | +| length | 8 bytes | uint64\_t | Size of the entry | +| size | 8 bytes | uint64\_t | | | field\_index | 1 byte | uint8\_t | | | db\_index | 1 byte | uint8\_t | | | count | 8 bytes | uint64\_t | | | fragnum | 8 bytes | uint64\_t | | -| data | \[size\] bytes | ASCII | | +| data | \[size\] bytes | Binary | | and 2 flags for `dbentry`: | Type | Mask | Info | | ---- | ---- | ---- | | Used | 0b10000000 | Flag this if you are using the entry. | | Fragment | 0b01000000 | Flag this if the entry is fragment. | +| Unset | 0b00100000 | Flag this if the entry is unset. | Index entry (`indexentry`) MUST be in this format: | Name | Size | Type | Info | diff --git a/Library/database.c b/Library/database.c index 861c850..cabdb16 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", "Not used", "Too many tables"}; +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", "Too many tables", "Database not selected"}; #ifdef M_I86 #define BUFSIZE 128 diff --git a/Library/dw_database.h b/Library/dw_database.h index fbf9ecb..d0b47f1 100644 --- a/Library/dw_database.h +++ b/Library/dw_database.h @@ -53,6 +53,20 @@ extern "C" { memcpy(index.dbname, buf + 1 + 8 + 1, 256); \ memcpy(index.fields, buf + 1 + 8 + 1 + 256, 4096); +#define __dw_buffer_to_db_v1_dbentry(buf, dbent) \ + memcpy(&dbent.flag, buf, 1); \ + uint64_t be; \ + memcpy(&be, buf + 1, 8); \ + __dw_native_endian(be, uint64_t, index.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); \ + memcpy(&be, buf + 1 + 8 + 8 + 1 + 1, 8); \ + __dw_native_endian(be, uint64_t, index.count = __converted); \ + memcpy(&be, buf + 1 + 8 + 8 + 1 + 1 + 8, 8); \ + __dw_native_endian(be, uint64_t, index.fragnum = __converted); + /** * @~english * @brief Error enum @@ -142,6 +156,13 @@ enum DW_ERRORS { * */ DW_ERR_TOO_MANY_TABLES, + + /** + * @~english + * @brief Database not selected + * + */ + DW_ERR_DATABASE_NOT_SELECTED, }; /** @@ -299,6 +320,23 @@ struct dataworks_db_v1_indexentry { char fields[4096]; }; +/** + * @~english + * @brief `dbentry` for v1 database. + * @note See \ref FORMATv1 for more info. + * + */ +struct dataworks_db_v1_dbentry { + uint8_t flag; + uint64_t length; + uint64_t size; + uint8_t field_index; + uint8_t db_index; + uint64_t count; + uint64_t fragnum; + uint8_t* data; +}; + /** * @~english * @brief "Used" bitmask for indexentry for v1 database. @@ -307,6 +345,30 @@ struct dataworks_db_v1_indexentry { */ #define DATAWORKS_V1_INDEXENTRY_USED (1 << 7) +/** + * @~english + * @brief "Used" bitmask for dbentry for v1 database. + * @note See \ref FORMATv1 for more info. + * + */ +#define DATAWORKS_V1_DBENTRY_USED (1 << 7) + +/** + * @~english + * @brief "Fragment" bitmask for dbentry for v1 database. + * @note See \ref FORMATv1 for more info. + * + */ +#define DATAWORKS_V1_DBENTRY_FRAGMENT (1 << 6) + +/** + * @~english + * @brief "Unset" bitmask for dbentry for v1 database. + * @note See \ref FORMATv1 for more info. + * + */ +#define DATAWORKS_V1_DBENTRY_UNSET (1 << 5) + /** * @~english * @brief `infoentry` for database. @@ -464,6 +526,18 @@ struct dataworks_db_result* dataworks_database_execute_code(struct dataworks_db* */ void dataworks_database_free_result(struct dataworks_db_result* result); +/** + * @~english + * @brief Inserts the record. + * @param db Database + * @param records Records + * @param prop List which contains character `U` or `S` + * `U` indicates the field is unset + * @return Result + * + */ +struct dataworks_db_result* dataworks_database_insert_record(struct dataworks_db* db, char** fields, const char* prop); + #ifdef __cplusplus } #endif diff --git a/Library/exec.c b/Library/exec.c index 7d7ba7b..e379a12 100644 --- a/Library/exec.c +++ b/Library/exec.c @@ -223,6 +223,26 @@ struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_d } } } + } else if(__dw_strcaseequ(token->name, "insert")) { + if(db->name == NULL) { + r->error = true; + r->errnum = DW_ERR_DATABASE_NOT_SELECTED; + } else { + argc = 0; + int first = -1; + int j; + for(j = 0; results[j] != NULL; j++) { + if(results[j]->value != NULL) { + argc++; + if(first == -1) first = j; + } + } + if(argc == 0) { + r->error = true; + r->errnum = DW_ERR_EXEC_INSUFFICIENT_ARGUMENTS; + } else { + } + } } else { r->error = true; r->errnum = DW_ERR_EXEC_UNKNOWN_METHOD; -- 2.43.0