From: nishi Date: Fri, 24 May 2024 02:46:22 +0000 (+0000) Subject: add use directive X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=3860f2b24ae308dff535443d2612f2025f2e7a3a;p=dataworks.git add use directive git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@100 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- diff --git a/Client/main.c b/Client/main.c index 2aabcb3..1993d53 100644 --- a/Client/main.c +++ b/Client/main.c @@ -225,9 +225,9 @@ int main(int argc, char** argv) { line[i] = 0; memcpy(line, linebuf, i); - struct dataworks_db_result* r = dataworks_database_execute_code(db, line); + struct dataworks_db_result* r = dataworks_database_execute_code(db, line, true); if(r->error) { - printf("%s\n", dataworks_database_strerror(r->errnum)); + printf("%s.\n", dataworks_database_strerror(r->errnum)); } free(line); diff --git a/Library/database.c b/Library/database.c index 3f1688a..91fe598 100644 --- a/Library/database.c +++ b/Library/database.c @@ -38,6 +38,8 @@ 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"}; + #ifdef M_I86 #define BUFSIZE 128 #endif @@ -90,6 +92,7 @@ struct dataworks_db* dataworks_database_open(const char* fname) { struct dataworks_db* db = malloc(sizeof(*db)); db->error = false; db->fp = NULL; + db->name = NULL; FILE* fp = fopen(fname, "rb+"); if(fp == NULL) { db->error = true; @@ -134,8 +137,6 @@ 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", "Insufficient arguments"}; - const char* dataworks_database_strerror(int n) { return dw_errors[n]; } void dataworks_database_update_mtime(struct dataworks_db* db) { @@ -145,3 +146,13 @@ void dataworks_database_update_mtime(struct dataworks_db* db) { __dw_big_endian(t, int64_t, fwrite(__converted_ptr, 1, 8, db->fp)); __dw_unlockfile(db->fp); } + +void dataworks_database_free(struct dataworks_db* db) { + if(db->fp != NULL) { + fclose(db->fp); + } + if(db->name != NULL) { + free(db->name); + } + free(db); +} diff --git a/Library/database_exec.c b/Library/database_exec.c index ea6ab00..73b4840 100644 --- a/Library/database_exec.c +++ b/Library/database_exec.c @@ -33,8 +33,9 @@ #include #include +#include -struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_db* db, struct __dw_token* token) { +struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_db* db, struct __dw_token* token, bool dolog) { struct dataworks_db_result* r = malloc(sizeof(*r)); r->value = NULL; r->error = false; @@ -47,7 +48,7 @@ struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_d 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]); + struct dataworks_db_result* r2 = __dataworks_database_execute_code(db, token->token[i], dolog); if(r2->error) { int j; for(j = 0; results[j] != NULL; j++) { @@ -161,6 +162,41 @@ struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_d printf("%s\n", results[j]->value); } } + } else if(__dw_strcaseequ(token->name, "use")) { + int j; + bool set = false; + for(j = 0; results[j] != NULL; j++) { + if(results[j]->value != NULL) { + if(set) { + r->error = true; + 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_PARSER_NULL; + } + } + } } else { r->error = true; r->errnum = DW_ERR_EXEC_UNKNOWN_METHOD; @@ -177,18 +213,18 @@ struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_d return r; } -struct dataworks_db_result* dataworks_database_execute_code(struct dataworks_db* db, const char* code) { +struct dataworks_db_result* dataworks_database_execute_code(struct dataworks_db* db, const char* code, bool dolog) { struct dataworks_db_result* r = malloc(sizeof(*r)); r->error = false; r->value = NULL; - struct __dw_token* token = __dw_parser_parse(code, true); + struct __dw_token* token = __dw_parser_parse(code, dolog); if(token != NULL) { if(token->error) { r->error = true; r->errnum = token->errnum; } else { dataworks_database_free_result(r); - r = __dataworks_database_execute_code(db, token); + r = __dataworks_database_execute_code(db, token, dolog); } __dw_parser_free(token); } else { diff --git a/Library/dw_database.h b/Library/dw_database.h index b370349..7f2b43d 100644 --- a/Library/dw_database.h +++ b/Library/dw_database.h @@ -121,6 +121,13 @@ enum DW_ERRORS { * */ DW_ERR_EXEC_INSUFFICIENT_ARGUMENTS, + + /** + * @~english + * @brief Too many arguments + * + */ + DW_ERR_EXEC_TOO_MANY_ARGUMENTS, }; /** @@ -163,6 +170,13 @@ struct dataworks_db { * */ int errnum; + + /** + * @~english + * @brief Database being used. + * + */ + char* name; }; /** @@ -336,10 +350,11 @@ void dataworks_database_update_mtime(struct dataworks_db* db); * @brief Executes the code. * @param db Database * @param code Code + * @param dolog Output the log if true * @return Result * */ -struct dataworks_db_result* dataworks_database_execute_code(struct dataworks_db* db, const char* code); +struct dataworks_db_result* dataworks_database_execute_code(struct dataworks_db* db, const char* code, bool dolog); /** * @~english