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