File: database_backend.c

package info (click to toggle)
atheme-services 7.2.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,992 kB
  • sloc: ansic: 93,230; sh: 7,440; php: 5,032; perl: 3,327; makefile: 1,261; sed: 16; ruby: 15; python: 3
file content (302 lines) | stat: -rw-r--r-- 6,442 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * Copyright (c) 2010 William Pitcock <nenolod@atheme.org>
 * Rights to this code are as documented in doc/LICENSE.
 *
 * Platform-agnostic database backend layer.
 */

#include "atheme.h"

database_module_t *db_mod = NULL;
mowgli_patricia_t *db_types = NULL;

database_handle_t *
db_open(const char *filename, database_transaction_t txn)
{
	return_val_if_fail(db_mod != NULL, NULL);
	return_val_if_fail(db_mod->db_open != NULL, NULL);

	return db_mod->db_open(filename, txn);
}

void
db_close(database_handle_t *db)
{
	return_if_fail(db_mod != NULL);
	return_if_fail(db_mod->db_close != NULL);

	return db_mod->db_close(db);
}

void
db_parse(database_handle_t *db)
{
	return_if_fail(db_mod != NULL);
	return_if_fail(db_mod->db_parse != NULL);

	return db_mod->db_parse(db);
}

bool
db_read_next_row(database_handle_t *db)
{
	return_val_if_fail(db != NULL, NULL);
	return_val_if_fail(db->vt != NULL, NULL);
	return_val_if_fail(db->vt->read_next_row != NULL, NULL);

	return db->vt->read_next_row(db);
}

const char *
db_read_word(database_handle_t *db)
{
	return_val_if_fail(db != NULL, NULL);
	return_val_if_fail(db->vt != NULL, NULL);
	return_val_if_fail(db->vt->read_word != NULL, NULL);

	return db->vt->read_word(db);
}

const char *
db_read_str(database_handle_t *db)
{
	return_val_if_fail(db != NULL, NULL);
	return_val_if_fail(db->vt != NULL, NULL);
	return_val_if_fail(db->vt->read_str != NULL, NULL);

	return db->vt->read_str(db);
}

bool
db_read_int(database_handle_t *db, int *r)
{
	return_val_if_fail(db != NULL, false);
	return_val_if_fail(db->vt != NULL, false);
	return_val_if_fail(db->vt->read_int != NULL, false);

	return db->vt->read_int(db, r);
}

bool
db_read_uint(database_handle_t *db, unsigned int *r)
{
	return_val_if_fail(db != NULL, false);
	return_val_if_fail(db->vt != NULL, false);
	return_val_if_fail(db->vt->read_uint != NULL, false);

	return db->vt->read_uint(db, r);
}

bool
db_read_time(database_handle_t *db, time_t *r)
{
	return_val_if_fail(db != NULL, false);
	return_val_if_fail(db->vt != NULL, false);
	return_val_if_fail(db->vt->read_time != NULL, false);

	return db->vt->read_time(db, r);
}

const char *
db_sread_word(database_handle_t *db)
{
	const char *w = db_read_word(db);
	if (!w)
	{
		slog(LG_ERROR, "db-sread-word: needed word at file %s line %d token %d", db->file, db->line, db->token);
		slog(LG_ERROR, "db-sread-word: exiting to avoid data loss");
		exit(EXIT_FAILURE);
	}
	return w;
}

const char *
db_sread_str(database_handle_t *db)
{
	const char *w = db_read_str(db);
	if (!w)
	{
		slog(LG_ERROR, "db-sread-multiword: needed multiword at file %s line %d token %d", db->file, db->line, db->token);
		slog(LG_ERROR, "db-sread-multiword: exiting to avoid data loss");
		exit(EXIT_FAILURE);
	}
	return w;
}

int
db_sread_int(database_handle_t *db)
{
	int r;
	bool ok = db_read_int(db, &r);

	if (!ok)
	{
		slog(LG_ERROR, "db-read-int: needed int at file %s line %d token %d", db->file, db->line, db->token);
		slog(LG_ERROR, "db-read-int: exiting to avoid data loss");
		exit(EXIT_FAILURE);
	}
	return r;
}

unsigned int
db_sread_uint(database_handle_t *db)
{
	unsigned int r;
	bool ok = db_read_uint(db, &r);

	if (!ok)
	{
		slog(LG_ERROR, "db-read-uint: needed int at file %s line %d token %d", db->file, db->line, db->token);
		slog(LG_ERROR, "db-read-uint: exiting to avoid data loss");
		exit(EXIT_FAILURE);
	}
	return r;
}

time_t
db_sread_time(database_handle_t *db)
{
	time_t r;
	bool ok = db_read_time(db, &r);

	if (!ok)
	{
		slog(LG_ERROR, "db-read-time: needed int at file %s line %d token %d", db->file, db->line, db->token);
		slog(LG_ERROR, "db-read-time: exiting to avoid data loss");
		exit(EXIT_FAILURE);
	}
	return r;
}

bool
db_start_row(database_handle_t *db, const char *type)
{
	return_val_if_fail(db != NULL, false);
	return_val_if_fail(db->vt != NULL, false);
	return_val_if_fail(db->vt->start_row != NULL, false);

	return db->vt->start_row(db, type);
}

bool
db_write_word(database_handle_t *db, const char *word)
{
	return_val_if_fail(db != NULL, false);
	return_val_if_fail(db->vt != NULL, false);
	return_val_if_fail(db->vt->write_word != NULL, false);

	return db->vt->write_word(db, word);
}

bool
db_write_str(database_handle_t *db, const char *str)
{
	return_val_if_fail(db != NULL, false);
	return_val_if_fail(db->vt != NULL, false);
	return_val_if_fail(db->vt->write_str != NULL, false);

	return db->vt->write_str(db, str);
}

bool
db_write_int(database_handle_t *db, int num)
{
	return_val_if_fail(db != NULL, false);
	return_val_if_fail(db->vt != NULL, false);
	return_val_if_fail(db->vt->write_int != NULL, false);

	return db->vt->write_int(db, num);
}

bool
db_write_uint(database_handle_t *db, unsigned int num)
{
	return_val_if_fail(db != NULL, false);
	return_val_if_fail(db->vt != NULL, false);
	return_val_if_fail(db->vt->write_uint != NULL, false);

	return db->vt->write_uint(db, num);
}

bool
db_write_time(database_handle_t *db, time_t tm)
{
	return_val_if_fail(db != NULL, false);
	return_val_if_fail(db->vt != NULL, false);
	return_val_if_fail(db->vt->write_time != NULL, false);

	return db->vt->write_time(db, tm);
}

bool
db_commit_row(database_handle_t *db)
{
	return_val_if_fail(db != NULL, false);
	return_val_if_fail(db->vt != NULL, false);
	return_val_if_fail(db->vt->commit_row != NULL, false);

	return db->vt->commit_row(db);
}

void
db_register_type_handler(const char *type, database_handler_f fun)
{
	return_if_fail(db_types != NULL);
	return_if_fail(type != NULL);
	return_if_fail(fun != NULL);

	mowgli_patricia_add(db_types, type, fun);
}

void
db_unregister_type_handler(const char *type)
{
	return_if_fail(db_types != NULL);
	return_if_fail(type != NULL);

	mowgli_patricia_delete(db_types, type);
}

void
db_process(database_handle_t *db, const char *type)
{
	database_handler_f fun;

	return_if_fail(db_types != NULL);
	return_if_fail(db != NULL);
	return_if_fail(type != NULL);

	fun = mowgli_patricia_retrieve(db_types, type);

	if (!fun)
	{
		fun = mowgli_patricia_retrieve(db_types, "???");
	}

	fun(db, type);
}

bool
db_write_format(database_handle_t *db, const char *fmt, ...)
{
	va_list va;
	char buf[BUFSIZE];

	va_start(va, fmt);
	vsnprintf(buf, BUFSIZE, fmt, va);
	va_end(va);

	return db_write_word(db, buf);
}

void
db_init(void)
{
	db_types = mowgli_patricia_create(strcasecanon);

	if (db_types == NULL)
	{
		slog(LG_ERROR, "db_init(): object allocator failure");
		exit(EXIT_FAILURE);
	}
}