]> Nishi Git Mirror - dataworks.git/commitdiff
can create table now
authornishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Thu, 23 May 2024 17:15:24 +0000 (17:15 +0000)
committernishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Thu, 23 May 2024 17:15:24 +0000 (17:15 +0000)
git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@97 d4a5a174-5a4a-5b4b-b672-37683c10d7d5

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

index cd07b5e6b7ff83c1a1724d161270f5b96af2a87d..3f1688a540391919debd033a23c02f2016220fe1 100644 (file)
@@ -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]; }
 
index 5447ccd0d84b20ccf349ed35705ae2befef77f3c..bb2038db5a72f661dc8c4a1859f7da26fd5a067d 100644 (file)
 #include "dw_parser.h"
 #include "dw_util.h"
 
-#include <stdlib.h>
 #include <stdbool.h>
+#include <stdlib.h>
 
 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);
+}
index 0e057752d465d2e40c97b181815559c6d82868fe..b370349db2b3045192290b0a54b1f32a09941ef0 100644 (file)
@@ -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