summaryrefslogtreecommitdiffstats
path: root/clib/db.h
blob: 184fb995044db767b39b2621f8cb6e925bf5fc92 (plain)
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: clib/db.h $                                                   */
/*                                                                        */
/* OpenPOWER FFS Project                                                  */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2014,2015                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

/*!
 * @file db.h
 * @brief Database client
 * @details Embedded database API wrapper
 * @author Shaun Wetzstein <shaun@us.ibm.com>
 * @date 2010-2011
 */

#ifndef __DB_H__
#define __DB_H__

#include <stdbool.h>
#include <stdint.h>
#include <sqlite3.h>

#include "exception.h"

/* ======================================================================= */

#define DB_ROW			SQLITE_ROW	//!< Statement contains another row @hideinitializer
#define DB_DONE			SQLITE_DONE	//!< Statement contains no more rows @hideinitializer
#define DB_OK			SQLITE_OK	//!< Database command ok @hideinitializer

#define DB			4	//!< Database exception class
#define SQL			5	//!< Statement exception class

typedef struct db db_t;		//!< Alias for the @em db class
typedef struct statement statement_t;	//!< Alias for the @em statement class

/*!
 * @brief Database access class
 */
struct db {
#ifdef SQLITE3
	const char *path;	//!< @private
	sqlite3 *db;		//!< @private
#endif
};

typedef enum transaction_type transaction_type_t;	//!< Alias for the @em transaction_type enum

/*!
 * @brief Transaction types
 */
enum transaction_type {
	tt_ERROR = -1,		//!< Invalid / unknown transaction
	tt_DEFERRED,		//!< Defferred locking transaction
	tt_IMMEDIATE,		//!< Immediate locking transaction
	tt_EXCLUSIVE,		//!< Exclusive locking transaction
};

#ifdef SQLITE3
typedef sqlite3_context db_context_t;	//!< User-defined database context
typedef sqlite3_value db_value_t;	//!< User-defined value
#else
#error MUST define db_context_t and db_value_t
#endif

/*!
 * @brief User-defined function
 */
typedef void (*db_f) (db_context_t *, int, db_value_t **);

/*!
 * @brief Statement class
 */
struct statement {
	db_t *db;		//!< Database object
#ifdef SQLITE3
	sqlite3_stmt *stmt;	//!< @private
#endif
};

/* ======================================================================= */

#if 0
extern db_t *db_new(const char *) __nonnull((1));
#endif

/*!
 * @brief Constructs a @em db object
 * @memberof db
 * @param self [in] db object @em self pointer
 * @param path [in] db file name
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int db_init(db_t * self, const char *path)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/*!
 * @brief Destructs a @em db object
 * @memberof db
 * @param self [in] db object @em self pointer
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int db_delete(db_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Execute a SQL txt statement
 * @memberof db
 * @param self [in] db object @em self pointer
 * @param fmt [in] printf-like format string
 * @param ... [in] printf-like arguments
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int db_execute(db_t * self, const char *fmt, ...)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/*!
 * @brief Open a @em db object
 * @memberof db
 * @param self [in] db object @em self pointer
 * @param flags [in] Open flagsprintf-like format string
 * @return 0 on success, non-0 otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int db_open(db_t * self, int flags)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Close a @em db object
 * @memberof db
 * @param self [in] db object @em self pointer
 * @return 0 on success, non-0 otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int db_close(db_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Register a user-defined function on a @em db object
 * @memberof db
 * @param self [in] db object @em self pointer
 * @param name [in] db function name
 * @param argc [in] Function argument count
 * @param func [in] User-defined function
 * @param data [in] User-defined data passed to @em finc
 * @return 0 on siccess, non-0 otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int db_register_function(db_t * self, const char *name, int argc,
				db_f func, void *data)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/*!
 * @brief Start a database transaction
 * @memberof db
 * @param self [in] db object @em self pointer
 * @param type [in] Transaction type
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int db_begin(db_t * self, transaction_type_t type)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Commit a database transaction
 * @memberof db
 * @param self [in] db object @em self pointer
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int db_commit(db_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Rollback a database transaction
 * @memberof db
 * @param self [in] db object @em self pointer
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int db_rollback(db_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/* ======================================================================= */

#if 0
extern statement_t *statement_new(db_t *) __nonnull((1));
#endif

/*!
 * @brief Constructs a @em statement object
 * @memberof statement
 * @param self [in] statement object @em self pointer
 * @param db [in] Pointer to owning database object
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int statement_init(statement_t * self, db_t * db)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/*!
 * @brief Destructs a @em statement object
 * @memberof statement
 * @param self [in] statement object @em self pointer
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int statement_delete(statement_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Prepare a @em statement object for execution
 * @memberof statement
 * @param self [in] statement object @em self pointer
 * @return DB_ROW when a row is available, DB_DONE when no more rows left
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int statement_prepare(statement_t * self, const char *sql)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/*!
 * @brief Execute the next step of @em statement object
 * @memberof statement
 * @param self [in] statement object @em self pointer
 * @return DB_OK on success, non-0 otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int statement_step(statement_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Reset a @em statement object
 * @memberof statement
 * @param self [in] statement object @em self pointer
 * @return DB_OK on success, non-0 otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int statement_reset(statement_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Finalize a @em statement object
 * @memberof statement
 * @param self [in] statement object @em self pointer
 * @return DB_OK on success, non-0 otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int statement_finalize(statement_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Binds an integer to a bound variable of a @em statement object
 * @memberof statement
 * @param self [in] statement object @em self pointer
 * @param pos [in] Bound varialble position number
 * @param val [in] Integer value
 * @return DB_OK on success, non-0 otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int statement_bind_int(statement_t * self, int pos, int val)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Binds a long long integer to a bound variable of a @em statement object
 * @memberof statement
 * @param self [in] statement object @em self pointer
 * @param pos [in] Bound varialble position number
 * @param val [in] Long Long Integer value
 * @return DB_OK on success, non-0 otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int statement_bind_int64(statement_t * self, int pos, int64_t val)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Binds a text string to a bound variable of a @em statement object
 * @memberof statement
 * @param self [in] statement object @em self pointer
 * @param pos [in] Bound varialble position number
 * @param val [in] Text string value
 * @return DB_OK on success, non-0 otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int statement_bind_text(statement_t * self, int pos, const char *val)
/*! @cond */
__nonnull((1, 3)) /*! @endcond */ ;

/*!
 * @brief Binds a blob to a bound variable of a @em statement object
 * @memberof statement
 * @param self [in] statement object @em self pointer
 * @param pos [in] Bound varialble position number
 * @param val [in] Blob value
 * @param len [in] Lengh of blob value (in bytes)
 * @return DB_OK on success, non-0 otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int statement_bind_blob(statement_t * self, int pos, const void *val,
			       int len)
/*! @cond */
__nonnull((1, 3)) /*! @endcond */ ;

/*!
 * @brief Return an integer from the result set of a @em statement object
 * @memberof statement
 * @param self [in] statement object @em self pointer
 * @param pos [in] Column position number
 * @return DB_OK on success, non-0 otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int statement_column_int(statement_t * self, int pos)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Return an long long integer from the result set of a @em statement object
 * @memberof statement
 * @param self [in] statement object @em self pointer
 * @param pos [in] Column position number
 * @return DB_OK on success, non-0 otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int64_t statement_column_int64(statement_t * self, int pos)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Return the length of a column from the result set of a @em statement object
 * @memberof statement
 * @param self [in] statement object @em self pointer
 * @param pos [in] Column position number
 * @return DB_OK on success, non-0 otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int statement_column_bytes(statement_t * self, int pos)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Return a text string from the result set of a @em statement object
 * @memberof statement
 * @param self [in] statement object @em self pointer
 * @param pos [in] Column position number
 * @return DB_OK on success, non-0 otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern const unsigned char *statement_column_text(statement_t * self, int pos)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Return a blob from the result set of a @em statement object
 * @memberof statement
 * @param self [in] statement object @em self pointer
 * @param pos [in] Column position number
 * @return DB_OK on success, non-0 otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern const void *statement_column_blob(statement_t * self, int pos)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

#ifdef SQLITE3
#define DBERR(d)			({				\
  err_t * _e = (err_t  *)malloc(sizeof(*_e) + ERR_DATA_SIZE);		\
  memset(_e, 0, sizeof(*_e) + ERR_DATA_SIZE);				\
  _e->size = snprintf(_e->data, ERR_DATA_SIZE, "%s (code=%d)",		\
		      sqlite3_errmsg(d), sqlite3_errcode(d));		\
  _e->magic = ERR_MAGIC, _e->type = DB, _e->code = sqlite3_errcode(d);	\
  _e->file = __FILE__, _e->line = __LINE__; 				\
  err_put(_e);								\
					})

#define SQLERR(d,s)			({				\
  err_t * _e = (err_t  *)malloc(sizeof(*_e) + ERR_DATA_SIZE);		\
  memset(_e, 0, sizeof(*_e) + ERR_DATA_SIZE);				\
  _e->size = snprintf(_e->data, ERR_DATA_SIZE, "'%s' => %s (code=%d)",	\
		      sqlite3_sql(s), sqlite3_errmsg(d),		\
		      sqlite3_errcode(d));				\
  _e->magic = ERR_MAGIC, _e->type = SQL, _e->code = sqlite3_errcode(d);	\
  _e->file = __FILE__, _e->line = __LINE__; 				\
  err_put(_e);								\
					})
#if 0
#define throw_db(x) ({							\
            char __e[strlen(sqlite3_errmsg((x))) + 32];			\
            __exc_throw(DB,((void*)__e),				\
                        sprintf(__e, "(%d) %s",				\
                                sqlite3_errcode((x)),			\
                                sqlite3_errmsg((x))),			\
                        __FILE__, __LINE__);				\
		     })

#define throw_statement(x,s) ({						\
            char __e[strlen(sqlite3_errmsg((x))) +			\
                     strlen(sqlite3_sql((s))) + 32];			\
            __exc_throw(STATEMENT,((void*)__e),				\
                        sprintf(__e, "(%d) %s\n%s",			\
                               sqlite3_errcode((x)),			\
                               sqlite3_errmsg((x)),			\
                               sqlite3_sql((s))),			\
                        __FILE__, __LINE__);				\
			      })
#endif
#else
#error FIX ME
#endif

/* ======================================================================= */

#endif				/* __DB_H__ */
OpenPOWER on IntegriCloud