| 1 |
/* Database structures and declarations. |
| 2 |
* |
| 3 |
* IRC Services is copyright (c) 1996-2009 Andrew Church. |
| 4 |
* E-mail: <achurch@achurch.org> |
| 5 |
* Parts written by Andrew Kempe and others. |
| 6 |
* This program is free but copyrighted software; see the file GPL.txt for |
| 7 |
* details. |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifndef DATABASE_H |
| 11 |
#define DATABASE_H |
| 12 |
|
| 13 |
#ifndef MODULES_H |
| 14 |
# include "modules.h" |
| 15 |
#endif |
| 16 |
|
| 17 |
/*************************************************************************/ |
| 18 |
|
| 19 |
/* Type constants for DBField.type */ |
| 20 |
typedef enum |
| 21 |
{ |
| 22 |
DBTYPE_INT8, |
| 23 |
DBTYPE_UINT8, |
| 24 |
DBTYPE_INT16, |
| 25 |
DBTYPE_UINT16, |
| 26 |
DBTYPE_INT32, |
| 27 |
DBTYPE_UINT32, |
| 28 |
DBTYPE_TIME, |
| 29 |
DBTYPE_STRING, |
| 30 |
DBTYPE_BUFFER, /* Buffer size in DBField.length */ |
| 31 |
DBTYPE_PASSWORD, |
| 32 |
} DBType; |
| 33 |
|
| 34 |
/* Structure that describes a field of a database table */ |
| 35 |
typedef struct dbfield_ |
| 36 |
{ |
| 37 |
const char *name; /* Field name */ |
| 38 |
DBType type; /* Field type */ |
| 39 |
int offset; /* Offset to field from start of structure |
| 40 |
* (use standard `offsetof' macro) */ |
| 41 |
int length; /* Length of DBTYPE_BUFFER fields */ |
| 42 |
int load_only; /* If nonzero, field is not saved (use for reading |
| 43 |
* obsolete fields) */ |
| 44 |
void (*get) (const void *record, void **value_ret); |
| 45 |
/* Function to retrieve the field's value when |
| 46 |
* saving; `value_ret' points to a variable of |
| 47 |
* the appropriate type (for DBTYPE_STRING, the |
| 48 |
* string should be malloc'd if not NULL) */ |
| 49 |
void (*put) (void *record, const void *value); |
| 50 |
/* Function to set the field's value on load; |
| 51 |
* `value' points to a variable of the |
| 52 |
* appropriate type */ |
| 53 |
} DBField; |
| 54 |
|
| 55 |
/* Structure that describes a database table */ |
| 56 |
typedef struct dbtable_ |
| 57 |
{ |
| 58 |
const char *name; /* Table name */ |
| 59 |
DBField *fields; /* Array of fields, terminated with name==NULL */ |
| 60 |
void *(*newrec) (void); |
| 61 |
/* Routine to allocate a new record */ |
| 62 |
void (*freerec) (void *record); |
| 63 |
/* Routine to free an allocated record (that has |
| 64 |
* not been inserted into the table) */ |
| 65 |
void (*insert) (void *record); |
| 66 |
/* Routine to insert a record (used when loading, |
| 67 |
* returns nonzero for success or 0 for failure) */ |
| 68 |
void *(*first) (void), *(*next) (void); |
| 69 |
/* Routines to iterate through all records (used |
| 70 |
* when saving) */ |
| 71 |
int (*postload) (void); |
| 72 |
/* Routine called after all records have been loaded; |
| 73 |
* returns nonzero for success or 0 for failure */ |
| 74 |
} DBTable; |
| 75 |
|
| 76 |
/* Container for module-implemented database functions */ |
| 77 |
typedef struct dbmodule_ |
| 78 |
{ |
| 79 |
/* Load the given table from permanent storage, returning nonzero for |
| 80 |
* success, zero for failure. */ |
| 81 |
int (*load_table) (DBTable * table); |
| 82 |
/* Save the given table to permanent storage, returning nonzero for |
| 83 |
* success, zero for failure */ |
| 84 |
int (*save_table) (DBTable * table); |
| 85 |
} DBModule; |
| 86 |
|
| 87 |
/*************************************************************************/ |
| 88 |
|
| 89 |
/* Macro to return a pointer to a field in a record */ |
| 90 |
#define DB_FIELDPTR(record,field) ((int8 *)(record) + (field)->offset) |
| 91 |
|
| 92 |
/*************************************************************************/ |
| 93 |
|
| 94 |
/* Initialization/cleanup routines. */ |
| 95 |
extern int database_init(int ac, char **av); |
| 96 |
extern void database_cleanup(void); |
| 97 |
|
| 98 |
/* Register a new database table. Returns nonzero on success, zero on |
| 99 |
* error. */ |
| 100 |
#define register_dbtable(table) _register_dbtable((table), THIS_MODULE) |
| 101 |
extern int _register_dbtable(DBTable * table, const Module * caller); |
| 102 |
|
| 103 |
/* Unregister a database table. Does nothing if the table was not |
| 104 |
* registered in the first place. */ |
| 105 |
extern void unregister_dbtable(DBTable * table); |
| 106 |
|
| 107 |
/* Save all registered database tables to permanent storage. Returns 1 if |
| 108 |
* all tables were successfully saved or no tables are registered, 0 if |
| 109 |
* some tables were successfully saved (but some were not), or -1 if no |
| 110 |
* tables were successfully saved. */ |
| 111 |
extern int save_all_dbtables(void); |
| 112 |
|
| 113 |
/* Register a database module. Returns nonzero on success, zero on error. |
| 114 |
* On success, all registered tables which have not already been loaded |
| 115 |
* will be loaded from permanent storage. Only one database module can be |
| 116 |
* registered. */ |
| 117 |
extern int register_dbmodule(DBModule * module); |
| 118 |
|
| 119 |
/* Unregister a database module. Does nothing if the module was not |
| 120 |
* registered in the first place. */ |
| 121 |
extern void unregister_dbmodule(DBModule * module); |
| 122 |
|
| 123 |
/* Read a value from a database field. The value buffer is assumed to be |
| 124 |
* large enough to hold the retrieved value. */ |
| 125 |
extern void get_dbfield(const void *record, const DBField * field, |
| 126 |
void *buffer); |
| 127 |
|
| 128 |
/* Store a value to a database field. */ |
| 129 |
extern void put_dbfield(void *record, const DBField * field, |
| 130 |
const void *value); |
| 131 |
|
| 132 |
/*************************************************************************/ |
| 133 |
|
| 134 |
#endif /* DATABASE_H */ |
| 135 |
|
| 136 |
/* |
| 137 |
* Local variables: |
| 138 |
* c-file-style: "stroustrup" |
| 139 |
* c-file-offsets: ((case-label . *) (statement-case-intro . *)) |
| 140 |
* indent-tabs-mode: nil |
| 141 |
* End: |
| 142 |
* |
| 143 |
* vim: expandtab shiftwidth=4: |
| 144 |
*/ |