]> Nishi Git Mirror - dataworks.git/commitdiff
add use directive
authornishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Fri, 24 May 2024 02:46:22 +0000 (02:46 +0000)
committernishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Fri, 24 May 2024 02:46:22 +0000 (02:46 +0000)
git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@100 d4a5a174-5a4a-5b4b-b672-37683c10d7d5

Client/main.c
Library/database.c
Library/database_exec.c
Library/dw_database.h

index 2aabcb3bee12f2a57f8dd956f63b47bad99c7f4d..1993d53a1d37f8f4fb41841728a636f681e4c01d 100644 (file)
@@ -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);
index 3f1688a540391919debd033a23c02f2016220fe1..91fe59855621c5157c11f7b456a78fd257d94f57 100644 (file)
@@ -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);
+}
index ea6ab00ec4035c1720cdb27f054ea4d475404fad..73b4840b80a227867bd380795a4756efbf7b681b 100644 (file)
@@ -33,8 +33,9 @@
 
 #include <stdbool.h>
 #include <stdlib.h>
+#include <string.h>
 
-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 {
index b370349db2b3045192290b0a54b1f32a09941ef0..7f2b43d656bf605a81b7d71f3f383e02ca14cae3 100644 (file)
@@ -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