| 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 |
.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)
}
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;
+}
/* --- 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;
+}
}
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;
+}
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
/**
* @~english
- * @brief HELP
+ * @brief Help
*
*/
DW_RECORD_HELP = '?',
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
*/
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.
* @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
}
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);
}
}
}
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 {