DataWorks
Database Library/Client
Loading...
Searching...
No Matches
dw_database.h
Go to the documentation of this file.
1/* $Id: dw_database.h 97 2024-05-23 17:15:24Z nishi $ */
2/* --- START LICENSE --- */
3/* -------------------------------------------------------------------------- */
4/* Copyright (c) 2024 Crabware. */
5/* Redistribution and use in source and binary forms, with or without modific */
6/* ation, are permitted provided that the following conditions are met: */
7/* 1. Redistributions of source code must retain the above copyright noti */
8/* ce, this list of conditions and the following disclaimer. */
9/* 2. Redistributions in binary form must reproduce the above copyright n */
10/* otice, this list of conditions and the following disclaimer in the documen */
11/* tation and/or other materials provided with the distribution. */
12/* 3. Neither the name of the copyright holder nor the names of its contr */
13/* ibutors may be used to endorse or promote products derived from this softw */
14/* are without specific prior written permission. */
15/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS */
16/* " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, TH */
17/* E IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPO */
18/* SE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS */
19/* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CON */
20/* SEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITU */
21/* TE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPT */
22/* ION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, S */
23/* TRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN AN */
24/* Y WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY */
25/* OF SUCH DAMAGE. */
26/* -------------------------------------------------------------------------- */
27/* --- END LICENSE --- */
28
29#ifndef __DATAWORKS_DW_DATABASE_H__
30#define __DATAWORKS_DW_DATABASE_H__
31
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43#include <stdbool.h>
44#include <stdint.h>
45#include <stdio.h>
46
47#define __dw_buffer_to_db_v1_indexentry(buf, index) \
48 memcpy(&index.flag, buf, 1); \
49 uint64_t be_count; \
50 memcpy(&be_count, buf + 1, 8); \
51 __dw_native_endian(be_count, uint64_t, index.count = __converted); \
52 memcpy(&index.dbname_len, buf + 1 + 8, 1); \
53 memcpy(index.dbname, buf + 1 + 8 + 1, 256); \
54 memcpy(index.fields, buf + 1 + 8 + 1 + 256, 4096);
55
125
137 FILE* fp;
138
144 uint16_t version;
145
151 uint64_t mtime;
152
158 bool error;
159
166};
167
179 bool error;
180
187
193 char* value;
194};
195
203 uint8_t flag;
204 uint64_t count;
205 uint8_t dbname_len;
206 char dbname[256];
207 char fields[4096];
208};
209
216#define DATAWORKS_V1_INDEXENTRY_USED (1 << 7)
217
225 uint16_t version;
226 uint64_t mtime;
227};
228
237int dataworks_database_create(const char* fname);
238
248struct dataworks_db* dataworks_database_open(const char* fname);
249
257
266
275
284
292uint64_t dataworks_database_get_mtime(struct dataworks_db* db);
293
302
315int dataworks_database_create_table(struct dataworks_db* db, const char* name, char** fields, const char* fieldtypes);
316
324const char* dataworks_database_strerror(int n);
325
333
342struct dataworks_db_result* dataworks_database_execute_code(struct dataworks_db* db, const char* code);
343
351
352#ifdef __cplusplus
353}
354#endif
355
356#endif
int dataworks_database_create(const char *fname)
Creates the database.
Definition database.c:45
uint64_t dataworks_database_get_mtime(struct dataworks_db *db)
Get the last modified time of the database.
Definition database.c:133
void dataworks_database_close(struct dataworks_db *db)
Closes the database.
Definition database.c:84
void dataworks_database_update_mtime(struct dataworks_db *db)
Update mtime.
Definition database.c:141
struct dataworks_db_result * dataworks_database_execute_code(struct dataworks_db *db, const char *code)
Executes the code.
bool dataworks_database_get_error(struct dataworks_db *db)
Get the error flag of the database.
int dataworks_database_create_table(struct dataworks_db *db, const char *name, char **fields, const char *fieldtypes)
Creates a table.
char ** dataworks_database_get_table_list(struct dataworks_db *db)
Get the table list of the database.
const char * dataworks_database_strerror(int n)
Converts error number to a string.
Definition database.c:139
int dataworks_database_get_error_number(struct dataworks_db *db)
Get the error number of the database.
Definition database.c:135
void dataworks_database_free_result(struct dataworks_db_result *result)
Frees the result.
DW_ERRORS
Error enum.
Definition dw_database.h:61
@ DW_ERR_EXEC_INSUFFICIENT_ARGUMENTS
Insufficient arguments.
@ DW_ERR_INVALID_SIGNATURE
Invalid signature.
Definition dw_database.h:88
@ DW_ERR_EXEC_UNKNOWN_METHOD
Unknown method.
@ DW_ERR_SUCCESS
Success.
Definition dw_database.h:67
@ DW_ERR_INVALID_VERSION
Invalid version.
Definition dw_database.h:95
@ DW_ERR_FAIL_FOPEN
File open fail.
Definition dw_database.h:81
@ DW_ERR_USED
Used already.
Definition dw_database.h:74
@ DW_ERR_EXEC_NON_METHOD
Cannot call non-method.
@ DW_ERR_PARSER_NULL
Parser returned NULL.
struct dataworks_db * dataworks_database_open(const char *fname)
Opens the database.
Definition database.c:89
int dataworks_database_get_version(struct dataworks_db *db)
Get the version of the database.
Definition database.c:131
infoentry for database.
Database result struct.
char * value
Value.
bool error
True if this is an error.
int errnum
Error number.
indexentry for v1 database.
Database struct.
bool error
True if this is an error.
int errnum
Error number.
uint64_t mtime
Last modified time of the database.
FILE * fp
File pointer to the database file.
uint16_t version
Version of the database.