From: nishi Date: Tue, 28 May 2024 03:22:49 +0000 (+0000) Subject: insert works, prob X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=879dbbc9109453beb7ac4ca0ddb4214d390cfcfa;p=dataworks.git insert works, prob git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@125 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- diff --git a/Library/database_record.c b/Library/database_record.c index 8937a52..d2c40b9 100644 --- a/Library/database_record.c +++ b/Library/database_record.c @@ -36,9 +36,10 @@ #include #include -void __dw_add_record(struct dataworks_db* db, uint64_t dbindex, uint64_t index, void* _data, uint64_t size, bool set) { +void __dw_add_record(struct dataworks_db* db, uint64_t count, uint64_t dbindex, uint64_t index, void* _data, uint64_t size, bool set) { uint8_t* data = _data; - int fragnum = 0; + uint64_t fragnum = 0; + uint64_t seekmore = 0; __dw_lockfile(db); if(db->version == 1) { char* buf = malloc(1 + 8 + 8 + 1 + 1 + 8 + 8); @@ -51,18 +52,40 @@ void __dw_add_record(struct dataworks_db* db, uint64_t dbindex, uint64_t index, 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; + uint64_t writ = 0; if(dbentry.size <= size) { - if(dbentry.size < size) buf[0] |= DATAWORKS_V1_DBENTRY_FRAGMENT; - __dw_big_endian(dbentry.size, uint64_t, memcpy(buf + 1, __converted_ptr, 8)); - buf[1 + 8 + 8] = index; - buf[1 + 8 + 8 + 1] = dbindex; - __dw_big_endian(fragnum, uint64_t, memcpy(buf + 1 + 8 + 8 + 1 + 1 + 8, __converted_ptr, 8)); - fragnum++; - size -= dbentry.size; + writ = dbentry.size; + } else { + writ = size; } + if(dbentry.size < size) buf[0] |= DATAWORKS_V1_DBENTRY_FRAGMENT; + __dw_big_endian(writ, uint64_t, memcpy(buf + 1, __converted_ptr, 8)); + buf[1 + 8 + 8] = index; + buf[1 + 8 + 8 + 1] = dbindex; + __dw_big_endian(count, uint64_t, memcpy(buf + 1 + 8 + 8 + 1 + 1, __converted_ptr, 8)); + __dw_big_endian(fragnum, uint64_t, memcpy(buf + 1 + 8 + 8 + 1 + 1 + 8, __converted_ptr, 8)); + fragnum++; fwrite(buf, 1, 1 + 8 + 8 + 1 + 1 + 8 + 8, db->fp); + fwrite(data + seekmore, 1, writ, db->fp); + fseek(db->fp, dbentry.size - writ, SEEK_CUR); + size -= writ; + seekmore += writ; + if(size == 0) break; + } else { + fseek(db->fp, dbentry.size, SEEK_CUR); } - fseek(db->fp, dbentry.size, SEEK_CUR); + } + if(size > 0) { + uint8_t flag = DATAWORKS_V1_DBENTRY_USED; + if(seekmore > 0) flag |= DATAWORKS_V1_DBENTRY_FRAGMENT; + fwrite(&flag, 1, 1, db->fp); + __dw_big_endian(size, uint64_t, fwrite(__converted_ptr, 1, 8, db->fp)); + __dw_big_endian(size, uint64_t, fwrite(__converted_ptr, 1, 8, db->fp)); + __dw_big_endian(index, uint8_t, fwrite(__converted_ptr, 1, 1, db->fp)); + __dw_big_endian(dbindex, uint8_t, fwrite(__converted_ptr, 1, 1, db->fp)); + __dw_big_endian(count, uint64_t, fwrite(__converted_ptr, 1, 8, db->fp)); + __dw_big_endian(fragnum, uint64_t, fwrite(__converted_ptr, 1, 8, db->fp)); + fwrite(data + seekmore, 1, size, db->fp); } free(buf); } @@ -92,6 +115,8 @@ struct dataworks_db_result* dataworks_database_insert_record(struct dataworks_db r->errnum = DW_ERR_EXEC_INSUFFICIENT_ARGUMENTS; return r; } + __dw_lockfile(db); + uint64_t count = dataworks_database_get_table_count(db, db->name); for(i = 0; prop[i] != 0; i++) { uint64_t entsize = 0; if(types[i] == DW_RECORD_STRING) { @@ -105,7 +130,9 @@ struct dataworks_db_result* dataworks_database_insert_record(struct dataworks_db } else if(types[i] == DW_RECORD_HELP) { entsize = strlen(fields[i]); } - __dw_add_record(db, dbindex, i, fields[i], entsize, prop[i] == 'S'); + __dw_add_record(db, count, dbindex, i, fields[i], entsize, prop[i] == 'S'); } + dataworks_database_set_table_count(db, db->name, count + 1); + __dw_unlockfile(db); return r; } diff --git a/Library/database_table.c b/Library/database_table.c index 390deda..e3d3ef1 100644 --- a/Library/database_table.c +++ b/Library/database_table.c @@ -209,6 +209,69 @@ char** dataworks_database_get_table_fields(struct dataworks_db* db, const char* return NULL; } +void dataworks_database_set_table_count(struct dataworks_db* db, const char* table, uint64_t count) { + if(db->version == 1) { + __dw_lockfile(db); + 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) { + __dw_big_endian(count, uint64_t, memcpy(buf + 1, __converted_ptr, 8)); + fseek(db->fp, -(1 + 8 + 1 + 256 + 4096), SEEK_CUR); + fwrite(buf, 1, 1 + 8 + 1 + 256 + 4096, db->fp); + free(dbname); + free(buf); + __dw_unlockfile(db); + return; + } + free(dbname); + } + } + free(buf); + __dw_unlockfile(db); + } +} + +uint64_t dataworks_database_get_table_count(struct dataworks_db* db, const char* table) { + if(db->version == 1) { + __dw_lockfile(db); + 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) { + free(dbname); + free(buf); + __dw_unlockfile(db); + return index.count; + } + free(dbname); + } + } + free(buf); + __dw_unlockfile(db); + } else { + return 0; + } +} + char* dataworks_database_get_table_field_types(struct dataworks_db* db, const char* table) { if(db->version == 1) { __dw_lockfile(db); diff --git a/Library/dw_database.h b/Library/dw_database.h index ec265e5..d14d134 100644 --- a/Library/dw_database.h +++ b/Library/dw_database.h @@ -552,6 +552,24 @@ struct dataworks_db_result* dataworks_database_insert_record(struct dataworks_db */ int dataworks_database_use_table(struct dataworks_db* db, const char* table); +/** + * @~english + * @param db Database + * @param table Table name + * @return Count + * + */ +uint64_t dataworks_database_get_table_count(struct dataworks_db* db, const char* table); + +/** + * @~english + * @param db Database + * @param table Table name + * @param count Count + * + */ +void dataworks_database_set_table_count(struct dataworks_db* db, const char* table, uint64_t count); + #ifdef __cplusplus } #endif diff --git a/Library/util.c b/Library/util.c index 288f221..849570f 100644 --- a/Library/util.c +++ b/Library/util.c @@ -68,7 +68,7 @@ bool __dw_lockfile(struct dataworks_db* db) { } off_t off = ftell(db->fp); fseek(db->fp, 0, SEEK_SET); -#if defined(DOS) +#if defined(__WATCOMC__) #elif defined(__MINGW32__) OVERLAPPED overlap = {0}; @@ -88,7 +88,7 @@ bool __dw_unlockfile(struct dataworks_db* db) { off_t off = ftell(db->fp); fseek(db->fp, 0, SEEK_SET); fflush(db->fp); -#if defined(DOS) +#if defined(__WATCOMC__) #elif defined(__MINGW32__) OVERLAPPED overlap = {0}; diff --git a/Platforms/watcom.mk b/Platforms/watcom.mk index 5e1d293..c3c5196 100644 --- a/Platforms/watcom.mk +++ b/Platforms/watcom.mk @@ -12,5 +12,5 @@ STATICLIB_PREFIX = STATICLIB_SUFFIX = .a PLATFORM_M = $(WATCOM_PLATFORM) PLATFORM_P = $(WATCOM_PLATFORM) -CFLAGS += -DDOS -b $(WATCOM_PLATFORM) +CFLAGS += -b $(WATCOM_PLATFORM) LDFLAGS += -b $(WATCOM_PLATFORM)