From 024d46201d1d41c1a1ffafce3764b91f34948666 Mon Sep 17 00:00:00 2001 From: nishi Date: Wed, 26 Jun 2024 02:42:12 +0000 Subject: [PATCH] insert works git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@438 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- Library/database_record.c | 13 +++---------- Library/dw_database.h | 9 +++------ Library/dw_parser.h | 1 + Library/exec.c | 32 ++++++++++++++++++++++++++++++++ Library/parser.c | 9 +++++++++ 5 files changed, 48 insertions(+), 16 deletions(-) diff --git a/Library/database_record.c b/Library/database_record.c index 22efac4..bfe775a 100644 --- a/Library/database_record.c +++ b/Library/database_record.c @@ -96,7 +96,7 @@ void __dw_add_record(struct dataworks_db* db, uint64_t count, uint64_t dbindex, __dw_unlockfile(db); } -struct dataworks_db_result* dataworks_database_insert_record(struct dataworks_db* db, void** fields, const char* prop) { +struct dataworks_db_result* dataworks_database_insert_record(struct dataworks_db* db, void** fields) { struct dataworks_db_result* r = malloc(sizeof(*r)); r->error = false; int i; @@ -112,16 +112,9 @@ struct dataworks_db_result* dataworks_database_insert_record(struct dataworks_db } free(dbi); 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; - } __dw_lockfile(db); uint64_t count = dataworks_database_get_table_count(db, db->name); - for(i = 0; prop[i] != 0; i++) { + for(i = 0; fields[i] != NULL; i++) { uint64_t entsize = 0; if(types[i] == DW_RECORD_STRING) { entsize = strlen(fields[i]); @@ -134,7 +127,7 @@ struct dataworks_db_result* dataworks_database_insert_record(struct dataworks_db } else if(types[i] == DW_RECORD_HELP) { entsize = strlen(fields[i]); } - __dw_add_record(db, count, dbindex, i, fields[i], entsize, prop[i] == 'S'); + __dw_add_record(db, count, dbindex, i, fields[i], entsize, 1); } dataworks_database_set_table_count(db, db->name, count + 1); __dw_unlockfile(db); diff --git a/Library/dw_database.h b/Library/dw_database.h index 8effb4f..101b164 100644 --- a/Library/dw_database.h +++ b/Library/dw_database.h @@ -193,7 +193,7 @@ enum DW_RECORD_TYPES { * @brief String * */ - DW_RECORD_STRING = 'C', + DW_RECORD_STRING = 'S', /** * @~english @@ -613,14 +613,11 @@ 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 - * - `S` indicates the field is set + * @param fields Fields * @return Result * */ -struct dataworks_db_result* dataworks_database_insert_record(struct dataworks_db* db, void** records, const char* prop); +struct dataworks_db_result* dataworks_database_insert_record(struct dataworks_db* db, void** records); /** * @~english diff --git a/Library/dw_parser.h b/Library/dw_parser.h index 462137d..421fbc4 100644 --- a/Library/dw_parser.h +++ b/Library/dw_parser.h @@ -52,6 +52,7 @@ struct Node* __dw_duplicate_node(struct Node* node); void __dw_free_node(struct Node* node); void __dw_free_node2(struct Node* node, bool top); bool __dw_print_node(struct Node* node, bool top); +char __dw_get_node_type(struct Node* node); #ifdef __cplusplus } diff --git a/Library/exec.c b/Library/exec.c index 12a1afa..d328100 100644 --- a/Library/exec.c +++ b/Library/exec.c @@ -195,16 +195,48 @@ struct Node* parser_process(struct dataworks_db* db, struct Node* node, bool dol } else if(i > count) { newnode->errnum = DW_ERR_EXEC_TOO_MANY_ARGUMENTS; } else { + char* typ = dataworks_database_get_table_field_types(db, dataworks_database_get_using_table(db)); struct Node** arr = malloc(sizeof(*arr) * (i + 1)); for(i = 0; node->nodes[i] != NULL; i++) { struct Node* r = parser_process(db, node->nodes[i], false); + char t = __dw_get_node_type(r); + if(!( + (typ[i] == 'F' || typ[i] == 'I') && t == 'N' + || typ[i] == t + )){ + newnode->errnum = DW_ERR_EXEC_TYPE_MISMATCH; + } arr[i] = r; } arr[i] = NULL; + if(newnode->errnum == DW_ERR_SUCCESS){ + void** fields = malloc(sizeof(*fields) * (i + 1)); + fields[i] = NULL; + int k; + for(k = 0; k < i; k++){ + if(typ[k] == DW_RECORD_STRING){ + fields[k] = __dw_strdup(arr[k]->string); + }else if(typ[k] == DW_RECORD_LOGICAL){ + fields[k] = malloc(1); + *(char*)fields[k] = arr[k]->logical; + }else if(typ[k] == DW_RECORD_FLOATING){ + fields[k] = malloc(sizeof(double)); + *(double*)fields[k] = arr[k]->number; + }else if(typ[k] == DW_RECORD_INTEGER){ + fields[k] = malloc(8); + *(uint64_t*)fields[k] = arr[k]->number; + } + } + struct dataworks_db_result* insr = dataworks_database_insert_record(db, fields); + + free(fields); + } + for(i = 0; arr[i] != NULL; i++) { __dw_free_node(arr[i]); } + free(typ); free(arr); } } else if(!used) { diff --git a/Library/parser.c b/Library/parser.c index 1b9bfc6..3444f19 100644 --- a/Library/parser.c +++ b/Library/parser.c @@ -102,6 +102,15 @@ void __dw_free_node2(struct Node* node, bool top) { if(!top) free(node); } +char __dw_get_node_type(struct Node* node){ + if(node->type == 'N' || node->type == 'L'){ + return node->type; + }else if(node->string != NULL){ + return 'S'; + } + return 0; +} + bool __dw_print_node(struct Node* node, bool top) { if(node->type == 'N') { printf("%f", node->number); -- 2.43.0