1 /*
2  * Copyright (c) 2010 William Pitcock <nenolod@atheme.org>
3  * Rights to this code are as documented in doc/LICENSE.
4  *
5  * Platform-agnostic database backend layer.
6  */
7 
8 #ifndef DATABASE_BACKEND_H
9 #define DATABASE_BACKEND_H
10 
11 #include "common.h"
12 
13 E bool strict_mode;
14 
15 struct database_handle_;
16 typedef struct database_handle_ database_handle_t;
17 
18 typedef struct {
19 	const char *name;
20 
21 	/* Reading stuff. */
22 	bool (*read_next_row)(database_handle_t *hdl);
23 
24 	const char *(*read_word)(database_handle_t *hdl);
25 	const char *(*read_str)(database_handle_t *hdl);
26 	bool (*read_int)(database_handle_t *hdl, int *res);
27 	bool (*read_uint)(database_handle_t *hdl, unsigned int *res);
28 	bool (*read_time)(database_handle_t *hdl, time_t *res);
29 
30 	/* Writing stuff. */
31 	bool (*start_row)(database_handle_t *hdl, const char *type);
32 	bool (*write_word)(database_handle_t *hdl, const char *word);
33 	bool (*write_str)(database_handle_t *hdl, const char *str);
34 	bool (*write_int)(database_handle_t *hdl, int num);
35 	bool (*write_uint)(database_handle_t *hdl, unsigned int num);
36 	bool (*write_time)(database_handle_t *hdl, time_t time);
37 	bool (*commit_row)(database_handle_t *hdl);
38 } database_vtable_t;
39 
40 typedef enum {
41 	DB_READ,
42 	DB_WRITE
43 } database_transaction_t;
44 
45 struct database_handle_ {
46 	void *priv;
47 	database_vtable_t *vt;
48 	database_transaction_t txn;
49 	char *file;
50 	unsigned int line;
51 	unsigned int token;
52 };
53 
54 typedef struct {
55 	database_handle_t *(*db_open)(const char *filename, database_transaction_t txn);
56 	void (*db_close)(database_handle_t *db);
57 	void (*db_parse)(database_handle_t *db);
58 } database_module_t;
59 
60 E database_handle_t *db_open(const char *filename, database_transaction_t txn);
61 E void db_close(database_handle_t *db);
62 E void db_parse(database_handle_t *db);
63 
64 E bool db_read_next_row(database_handle_t *db);
65 
66 E const char *db_read_word(database_handle_t *db);
67 E const char *db_read_str(database_handle_t *db);
68 E bool db_read_int(database_handle_t *db, int *r);
69 E bool db_read_uint(database_handle_t *db, unsigned int *r);
70 E bool db_read_time(database_handle_t *db, time_t *t);
71 
72 E const char *db_sread_word(database_handle_t *db);
73 E const char *db_sread_str(database_handle_t *db);
74 E int db_sread_int(database_handle_t *db);
75 E unsigned int db_sread_uint(database_handle_t *db);
76 E time_t db_sread_time(database_handle_t *db);
77 
78 E bool db_start_row(database_handle_t *db, const char *type);
79 E bool db_write_word(database_handle_t *db, const char *word);
80 E bool db_write_str(database_handle_t *db, const char *str);
81 E bool db_write_int(database_handle_t *db, int num);
82 E bool db_write_uint(database_handle_t *db, unsigned int num);
83 E bool db_write_time(database_handle_t *db, time_t time);
84 E bool db_write_format(database_handle_t *db, const char *str, ...);
85 E bool db_commit_row(database_handle_t *db);
86 
87 typedef void (*database_handler_f)(database_handle_t *db, const char *type);
88 
89 E void db_register_type_handler(const char *type, database_handler_f fun);
90 E void db_unregister_type_handler(const char *type);
91 E void db_process(database_handle_t *db, const char *type);
92 E void db_init(void);
93 E database_module_t *db_mod;
94 
95 #endif
96