From: nishi Date: Thu, 23 May 2024 17:15:24 +0000 (+0000) Subject: can create table now X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=42d09ea11a52099168b4e9792da7b2664f3f4823;p=dataworks.git can create table now git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@97 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- diff --git a/Library/database.c b/Library/database.c index cd07b5e..3f1688a 100644 --- a/Library/database.c +++ b/Library/database.c @@ -134,7 +134,7 @@ uint64_t dataworks_database_get_mtime(struct dataworks_db* db) { return db->mtim int dataworks_database_get_error_number(struct dataworks_db* db) { return db->errnum; } -const char* dw_errors[] = {"Success", "Used already", "File open fail", "Invalid signature", "Invalid version", "Parser returned NULL", "Cannot call non-method", "Unknown method"}; +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"}; const char* dataworks_database_strerror(int n) { return dw_errors[n]; } diff --git a/Library/database_exec.c b/Library/database_exec.c index 5447ccd..bb2038d 100644 --- a/Library/database_exec.c +++ b/Library/database_exec.c @@ -31,19 +31,146 @@ #include "dw_parser.h" #include "dw_util.h" -#include #include +#include struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_db* db, struct __dw_token* token) { struct dataworks_db_result* r = malloc(sizeof(*r)); + r->value = NULL; r->error = false; - if(token->type == __DW_METHOD){ - if(__dw_strcaseequ(token->name, "create_table")){ - }else{ + if(token->type == __DW_METHOD) { + int i; + struct dataworks_db_result** results = malloc(sizeof(*results)); + results[0] = NULL; + int argc = 0; + if(token->token != NULL) { + for(i = 0; token->token[i] != NULL; i++) { + argc++; + if(token->token[i]->type == __DW_METHOD) { + struct dataworks_db_result* r2 = __dataworks_database_execute_code(db, token->token[i]); + if(r2->error) { + int j; + for(j = 0; results[j] != NULL; j++) { + dataworks_database_free_result(results[j]); + } + free(results); + dataworks_database_free_result(r); + r = r2; + break; + } + int j; + for(j = 0; results[j] != NULL; j++) + ; + struct dataworks_db_result** newresults = malloc(sizeof(*newresults) * (j + 2)); + for(j = 0; results[j] != NULL; j++) { + newresults[j] = results[j]; + } + free(results); + results = newresults; + results[j] = r2; + results[j + 1] = NULL; + } else { + struct dataworks_db_result* r2 = malloc(sizeof(*r2)); + r2->error = false; + r2->value = __dw_strdup(token->token[i]->name); + int j; + for(j = 0; results[j] != NULL; j++) + ; + struct dataworks_db_result** newresults = malloc(sizeof(*newresults) * (j + 2)); + for(j = 0; results[j] != NULL; j++) { + newresults[j] = results[j]; + } + free(results); + results = newresults; + results[j] = r2; + results[j + 1] = NULL; + } + } + } + if(r->error) return r; + if(__dw_strcaseequ(token->name, "create_table")) { + 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 < 2) { + r->error = true; + r->errnum = DW_ERR_EXEC_INSUFFICIENT_ARGUMENTS; + } else { + int j; + char* fieldtypes = malloc(argc); + fieldtypes[argc - 1] = 0; + char** fields = malloc(sizeof(*fields) * (argc)); + fields[argc - 1] = NULL; + argc = 0; + for(j = first + 1; results[j] != NULL; j++) { + if(results[j]->value != NULL) { + char* val = __dw_strdup(results[j]->value); + int k; + for(k = 0; val[k] != 0; k++) { + if(val[k] == ':') { + val[k] = 0; + if(__dw_strcaseequ(val, "string")) { + fieldtypes[argc] = 'S'; + } else if(__dw_strcaseequ(val, "integer")) { + fieldtypes[argc] = 'I'; + } else if(__dw_strcaseequ(val, "double")) { + fieldtypes[argc] = 'D'; + } else if(__dw_strcaseequ(val, "logical")) { + fieldtypes[argc] = 'L'; + } else if(__dw_strcaseequ(val, "help")) { + fieldtypes[argc] = '?'; + } + fields[argc] = __dw_strdup(val + k + 1); + argc++; + break; + } + } + free(val); + } + } + int er; + if((er = dataworks_database_create_table(db, results[first]->value, fields, fieldtypes)) != DW_ERR_SUCCESS) { + r->error = true; + r->errnum = er; + } + for(j = 0; fields[j] != NULL; j++) free(fields[j]); + free(fields); + free(fieldtypes); + } + } else if(__dw_strcaseequ(token->name, "concat")) { + r->value = malloc(1); + r->value[0] = 0; + int j; + for(j = 0; results[j] != NULL; j++) { + if(results[j]->value != NULL) { + char* tmp = r->value; + r->value = __dw_strcat(tmp, results[j]->value); + free(tmp); + } + } + } else if(__dw_strcaseequ(token->name, "print")) { + int j; + for(j = 0; results[j] != NULL; j++) { + if(results[j]->value != NULL) { + printf("%s\n", results[j]->value); + } + } + } else { r->error = true; r->errnum = DW_ERR_EXEC_UNKNOWN_METHOD; } - }else{ + int j; + for(j = 0; results[j] != NULL; j++) { + dataworks_database_free_result(results[j]); + } + free(results); + } else { r->error = true; r->errnum = DW_ERR_EXEC_NON_METHOD; } @@ -53,19 +180,25 @@ struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_d struct dataworks_db_result* dataworks_database_execute_code(struct dataworks_db* db, const char* code) { struct dataworks_db_result* r = malloc(sizeof(*r)); r->error = false; + r->value = NULL; struct __dw_token* token = __dw_parser_parse(code); - if(token != NULL){ - if(token->error){ + if(token != NULL) { + if(token->error) { r->error = true; r->errnum = token->errnum; - }else{ - free(r); + } else { + dataworks_database_free_result(r); r = __dataworks_database_execute_code(db, token); } __dw_parser_free(token); - }else{ + } else { r->error = true; r->errnum = DW_ERR_PARSER_NULL; } return r; } + +void dataworks_database_free_result(struct dataworks_db_result* result) { + if(result->value != NULL) free(result->value); + free(result); +} diff --git a/Library/dw_database.h b/Library/dw_database.h index 0e05775..b370349 100644 --- a/Library/dw_database.h +++ b/Library/dw_database.h @@ -114,6 +114,13 @@ enum DW_ERRORS { * */ DW_ERR_EXEC_UNKNOWN_METHOD, + + /** + * @~english + * @brief Insufficient arguments + * + */ + DW_ERR_EXEC_INSUFFICIENT_ARGUMENTS, }; /** @@ -177,6 +184,13 @@ struct dataworks_db_result { * */ int errnum; + + /** + * @~english + * @brief Value. + * + */ + char* value; }; /** @@ -327,6 +341,14 @@ void dataworks_database_update_mtime(struct dataworks_db* db); */ struct dataworks_db_result* dataworks_database_execute_code(struct dataworks_db* db, const char* code); +/** + * @~english + * @brief Frees the result. + * @param result Result + * + */ +void dataworks_database_free_result(struct dataworks_db_result* result); + #ifdef __cplusplus } #endif