]> Nishi Git Mirror - dataworks.git/commitdiff
add insert
authornishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Sun, 26 May 2024 16:06:30 +0000 (16:06 +0000)
committernishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Sun, 26 May 2024 16:06:30 +0000 (16:06 +0000)
git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@122 d4a5a174-5a4a-5b4b-b672-37683c10d7d5

FORMATv1.md
Library/Makefile
Library/database.c
Library/database_record.c
Library/database_table.c
Library/dw_database.h
Library/exec.c

index e409d2011b0fc2227ad9695dc53d93ea17010002..485b91a40e45c318a8c7317d69ea7adbd21280f2 100644 (file)
@@ -45,7 +45,7 @@ There are 5 types for `indexentry`:
 | Type | Character | Type | Info |
 | ---- | --------- | ---- | ---- |
 | String | `S` | ASCII | |
-| Integer | `I` | uint64\_t | |
+| Integer | `I` | int64\_t | |
 | Double | `D` | double | |
 | Logical | `L` | uint8\_t | 0 for false, other value for true |
 | Help | `?` | ASCII | Should be just ignored |
index f20e665003be88e22b0f3abc51a78e7673ee2b35..6f24ebdb9c973fe7ab02fba20128b68683370c59 100644 (file)
@@ -3,7 +3,7 @@
 .PHONY: all clean
 .SUFFIXES: .c .o
 
-OBJS = parser.o database.o util.o dataworks.o database_table.o database_db.o exec.o
+OBJS = parser.o database.o util.o dataworks.o database_table.o database_db.o database_record.o exec.o
 
 all: $(LIB_PREFIX)dataworks$(LIB_SUFFIX) $(STATICLIB_PREFIX)dataworks$(STATICLIB_SUFFIX)
 
index cabdb162dfd4ce81f66bf93dfb1d0c9cfd6f798b..31e39801c2df8d4f21abd60ef6cbe3d5babcf291 100644 (file)
@@ -156,3 +156,28 @@ void dataworks_database_free(struct dataworks_db* db) {
        }
        free(db);
 }
+
+int dataworks_database_use_table(struct dataworks_db* db, const char* table) {
+       int errnum = DW_ERR_SUCCESS;
+       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], table) == 0) {
+                               has = true;
+                       }
+                       free(names[k]);
+               }
+               free(names);
+               if(has) {
+                       db->name = __dw_strdup(table);
+               } else {
+                       errnum = DW_ERR_NOT_USED;
+               }
+       } else {
+               errnum = DW_ERR_PARSER_NULL;
+       }
+       return errnum;
+}
index 6ade2f55024b13bde758a1860dca9915389ebd79..f6e70e155c98cc96436eee3ebe22500fb2df6cc8 100644 (file)
 /* --- END LICENSE --- */
 
 #include "dw_database.h"
+
+#include "dw_util.h"
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void __dw_add_record(struct dataworks_db* db, uint64_t index, void* _data, uint64_t size, bool set) {
+       uint8_t* data = _data;
+       __dw_lockfile(db->fp);
+       if(db->version == 1) {
+               char* buf = malloc(1 + 8 + 8 + 1 + 1 + 8 + 8);
+               fseek(db->fp, 3 + 2 + 8 + (1 + 8 + 1 + 256 + 4096) * 256, SEEK_SET);
+               struct dataworks_db_v1_dbentry dbentry;
+               while(1) {
+                       fread(buf, 1, 1 + 8 + 8 + 1 + 1 + 8 + 8, db->fp);
+                       __dw_buffer_to_db_v1_dbentry(buf, dbentry);
+                       if(feof(db->fp)) break;
+                       if(!(dbentry.flag & DATAWORKS_V1_DBENTRY_USED)) {
+                               fseek(db->fp, -(1 + 8 + 8 + 1 + 1 + 8 + 8), SEEK_CUR);
+                               buf[0] |= DATAWORKS_V1_DBENTRY_USED;
+                               fwrite(buf, 1, 1 + 8 + 8 + 1 + 1 + 8 + 8, db->fp);
+                       }
+                       fseek(db->fp, dbentry.size, SEEK_CUR);
+               }
+               free(buf);
+       }
+       __dw_unlockfile(db->fp);
+}
+
+struct dataworks_db_result* dataworks_database_insert_record(struct dataworks_db* db, void** fields, const char* prop) {
+       struct dataworks_db_result* r = malloc(sizeof(*r));
+       r->error = false;
+       int i;
+       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;
+       }
+       for(i = 0; prop[i] != 0; i++) {
+               uint64_t entsize = 0;
+               if(types[i] == DW_RECORD_STRING) {
+                       entsize = strlen(fields[i]);
+               } else if(types[i] == DW_RECORD_INTEGER) {
+                       entsize = 8;
+               } else if(types[i] == DW_RECORD_DOUBLE) {
+                       entsize = sizeof(double);
+               } else if(types[i] == DW_RECORD_LOGICAL) {
+                       entsize = 1;
+               } else if(types[i] == DW_RECORD_HELP) {
+                       entsize = strlen(fields[i]);
+               }
+               __dw_add_record(db, i, fields[i], entsize, prop[i] == 'S');
+       }
+       return r;
+}
index 41e1f4c9d54c2c5570b82b5e37c55fcf253f7e6a..40ea3a7f1a63f95e6872709b5eba7217bbcb09e3 100644 (file)
@@ -208,3 +208,51 @@ char** dataworks_database_get_table_fields(struct dataworks_db* db, const char*
        }
        return NULL;
 }
+
+char* dataworks_database_get_table_field_types(struct dataworks_db* db, const char* table) {
+       if(db->version == 1) {
+               __dw_lockfile(db->fp);
+               fseek(db->fp, 3 + 10, SEEK_SET);
+               int i;
+               struct dataworks_db_v1_indexentry index;
+               char* buf = malloc(1 + 8 + 1 + 256 + 4096);
+               int c = 0;
+               for(i = 0; i < 256; i++) {
+                       fread(buf, 1, 1 + 8 + 1 + 256 + 4096, db->fp);
+                       __dw_buffer_to_db_v1_indexentry(buf, index);
+                       if(index.flag & DATAWORKS_V1_INDEXENTRY_USED) {
+                               char* dbname = malloc(index.dbname_len + 1);
+                               memcpy(dbname, index.dbname, index.dbname_len);
+                               dbname[index.dbname_len] = 0;
+                               if(strcmp(dbname, table) == 0) {
+                                       int j;
+                                       int count = 0;
+                                       for(j = 0;; j++) {
+                                               if(index.fields[j] == 0) break;
+                                               count++;
+                                               for(; index.fields[j] != 0; j++)
+                                                       ;
+                                       }
+                                       char* list = malloc(count + 1);
+                                       count = 0;
+                                       for(j = 0;; j++) {
+                                               if(index.fields[j] == 0) break;
+                                               list[count] = index.fields[j];
+                                               count++;
+                                               for(; index.fields[j] != 0; j++)
+                                                       ;
+                                       }
+                                       list[count] = 0;
+                                       free(dbname);
+                                       free(buf);
+                                       __dw_unlockfile(db->fp);
+                                       return list;
+                               }
+                               free(dbname);
+                       }
+               }
+               free(buf);
+               __dw_unlockfile(db->fp);
+       }
+       return NULL;
+}
index d0b47f13da07f2b1c716e688aa9092b58b28db12..8deab74df28705c04578c2489f9b2220223c15af 100644 (file)
@@ -57,15 +57,15 @@ extern "C" {
        memcpy(&dbent.flag, buf, 1); \
        uint64_t be; \
        memcpy(&be, buf + 1, 8); \
-       __dw_native_endian(be, uint64_t, index.length = __converted); \
+       __dw_native_endian(be, uint64_t, dbent.length = __converted); \
        memcpy(&be, buf + 1 + 8, 8); \
-       __dw_native_endian(be, uint64_t, index.size = __converted); \
-       memcpy(&dbent.field_index, 1 + 8 + 8, 1); \
-       memcpy(&dbent.db_index, 1 + 8 + 8 + 1, 1); \
+       __dw_native_endian(be, uint64_t, dbent.size = __converted); \
+       memcpy(&dbent.field_index, buf + 1 + 8 + 8, 1); \
+       memcpy(&dbent.db_index, buf + 1 + 8 + 8 + 1, 1); \
        memcpy(&be, buf + 1 + 8 + 8 + 1 + 1, 8); \
-       __dw_native_endian(be, uint64_t, index.count = __converted); \
+       __dw_native_endian(be, uint64_t, dbent.count = __converted); \
        memcpy(&be, buf + 1 + 8 + 8 + 1 + 1 + 8, 8); \
-       __dw_native_endian(be, uint64_t, index.fragnum = __converted);
+       __dw_native_endian(be, uint64_t, dbent.fragnum = __converted);
 
 /**
  * @~english
@@ -201,7 +201,7 @@ enum DW_RECORD_TYPES {
 
        /**
         * @~english
-        * @brief HELP
+        * @brief Help
         *
         */
        DW_RECORD_HELP = '?',
@@ -264,20 +264,6 @@ struct dataworks_db {
        char* name;
 };
 
-/**
- * @~english
- * @brief Database record struct
-
- */
-struct dataworks_db_record {
-       /**
-        * @~english
-        * @brief Type. See enum DW_RECORD_TYPES.
-        *
-        */
-       char type;
-};
-
 /**
  * @~english
  * @brief Database result struct
@@ -464,6 +450,16 @@ char** dataworks_database_get_table_list(struct dataworks_db* db);
  */
 char** dataworks_database_get_table_fields(struct dataworks_db* db, const char* table);
 
+/**
+ * @~english
+ * @brief Get the field types of the table of the database.
+ * @param db Database
+ * @param table Table name
+ * @return Field types of the table
+ *
+ */
+char* dataworks_database_get_table_field_types(struct dataworks_db* db, const char* table);
+
 /**
  * @~english
  * @brief Creates a table.
@@ -531,12 +527,23 @@ void dataworks_database_free_result(struct dataworks_db_result* result);
  * @brief Inserts the record.
  * @param db Database
  * @param records Records
+ * @param fields Fields in binary
  * @param prop List which contains character `U` or `S`
  * `U` indicates the field is unset
+ * `S` indicates the field is set
+ * @return Result
+ *
+ */
+struct dataworks_db_result* dataworks_database_insert_record(struct dataworks_db* db, void** fields, const char* prop);
+
+/**
+ * @~english
+ * @param db Database
+ * @param table Table name
  * @return Result
  *
  */
-struct dataworks_db_result* dataworks_database_insert_record(struct dataworks_db* db, char** fields, const char* prop);
+int dataworks_database_use_table(struct dataworks_db* db, const char* table);
 
 #ifdef __cplusplus
 }
index e379a125df338ac1ce40017ae430eebc8d027a21..01f07c501363d7ceeb3782475ab88e912230b506 100644 (file)
@@ -195,31 +195,11 @@ struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_d
                                                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_NOT_USED;
-                                               }
-                                       } else {
+                                       r->errnum = dataworks_database_use_table(db, results[j]->value);
+                                       if(r->errnum != DW_ERR_SUCCESS) {
                                                r->error = true;
-                                               r->errnum = DW_ERR_PARSER_NULL;
+                                       } else if(dolog) {
+                                               printf("Using table `%s`.\n", results[j]->value);
                                        }
                                }
                        }
@@ -241,6 +221,15 @@ struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_d
                                        r->error = true;
                                        r->errnum = DW_ERR_EXEC_INSUFFICIENT_ARGUMENTS;
                                } else {
+                                       double dn = 123123;
+                                       int64_t in = 123123;
+                                       void* data[3] = {"data", &dn, &in};
+                                       struct dataworks_db_result* dbr = dataworks_database_insert_record(db, data, "SSS");
+                                       if(dbr->error) {
+                                               r->error = true;
+                                               r->errnum = dbr->errnum;
+                                       }
+                                       dataworks_database_free_result(dbr);
                                }
                        }
                } else {