6 * Anyone who accesses the database will need to include
9 * This file contains definitions common to sql.c and
10 * the external world, and definitions destined only
11 * for the external world. This is control with
12 * the define __SQL_C, which is defined only in sql.c
18 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
20 This program is free software; you can redistribute it and/or
21 modify it under the terms of the GNU General Public License as
22 published by the Free Software Foundation; either version 2 of
23 the License, or (at your option) any later version.
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 General Public License for more details.
30 You should have received a copy of the GNU General Public
31 License along with this program; if not, write to the Free
32 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
41 typedef void (DB_LIST_HANDLER)(void *, char *);
42 typedef int (DB_RESULT_HANDLER)(void *, int, char **);
44 #define db_lock(mdb) _db_lock(__FILE__, __LINE__, mdb)
45 #define db_unlock(mdb) _db_unlock(__FILE__, __LINE__, mdb)
55 /* Define opaque structure for sqlite */
60 #define IS_NUM(x) ((x) == 1)
61 #define IS_NOT_NULL(x) ((x) == 1)
63 typedef struct s_sql_field {
64 char *name; /* name of column */
65 uint32_t length; /* length */
66 uint32_t max_length; /* max length */
67 uint32_t type; /* type */
68 uint32_t flags; /* flags */
72 * This is the "real" definition that should only be
73 * used inside sql.c and associated database interface
78 BQUEUE bq; /* queue control */
79 brwlock_t lock; /* transaction lock */
82 int nrow; /* nrow returned from sqlite */
83 int ncolumn; /* ncolum returned from sqlite */
84 int num_rows; /* used by code */
85 int row; /* seek row */
86 int have_insert_id; /* do not have insert id */
87 int fields_defined; /* set when fields defined */
88 int field; /* seek field */
89 SQL_FIELD **fields; /* defined fields */
95 char *sqlite_errmsg; /* error message returned by sqlite */
96 POOLMEM *errmsg; /* nicely edited error message */
97 POOLMEM *cmd; /* SQL command string */
99 uint32_t cached_path_id;
100 int transaction; /* transaction started */
101 int changes; /* changes during transaction */
106 * "Generic" names for easier conversion
110 #define sql_store_result(x) x->result
111 #define sql_free_result(x) my_sqlite_free_table(x)
112 #define sql_fetch_row(x) my_sqlite_fetch_row(x)
113 #define sql_query(x, y) my_sqlite_query(x, y)
114 #define sql_close(x) sqlite_close(x->db)
115 #define sql_strerror(x) x->sqlite_errmsg?x->sqlite_errmsg:"unknown"
116 #define sql_num_rows(x) x->nrow
117 #define sql_data_seek(x, i) x->row = i
118 #define sql_affected_rows(x) 1
119 #define sql_insert_id(x) sqlite_last_insert_rowid(x->db)
120 #define sql_field_seek(x, y) my_sqlite_field_seek(x, y)
121 #define sql_fetch_field(x) my_sqlite_fetch_field(x)
122 #define sql_num_fields(x) (unsigned)((x)->ncolumn)
123 #define SQL_ROW char**
127 /* In cats/sqlite.c */
128 extern int my_sqlite_query(B_DB *mdb, char *cmd);
129 extern SQL_ROW my_sqlite_fetch_row(B_DB *mdb);
130 extern void my_sqlite_free_table(B_DB *mdb);
136 #define BDB_VERSION 2
141 * This is the "real" definition that should only be
142 * used inside sql.c and associated database interface
147 typedef struct s_db {
148 BQUEUE bq; /* queue control */
149 brwlock_t lock; /* transaction lock */
153 my_ulonglong num_rows;
158 int have_insert_id; /* do have insert_id() */
160 POOLMEM *errmsg; /* nicely edited error message */
161 POOLMEM *cmd; /* SQL command string */
162 POOLMEM *cached_path;
163 uint32_t cached_path_id;
164 int changes; /* changes made to db */
168 /* "Generic" names for easier conversion */
169 #define sql_store_result(x) mysql_store_result(x->db)
170 #define sql_free_result(x) mysql_free_result(x->result)
171 #define sql_fetch_row(x) mysql_fetch_row(x->result)
172 #define sql_query(x, y) mysql_query(x->db, y)
173 #define sql_close(x) mysql_close(x->db)
174 #define sql_strerror(x) mysql_error(x->db)
175 #define sql_num_rows(x) mysql_num_rows(x->result)
176 #define sql_data_seek(x, i) mysql_data_seek(x->result, i)
177 #define sql_affected_rows(x) mysql_affected_rows(x->db)
178 #define sql_insert_id(x) mysql_insert_id(x->db)
179 #define sql_field_seek(x, y) mysql_field_seek(x->result, y)
180 #define sql_fetch_field(x) mysql_fetch_field(x->result)
181 #define sql_num_fields(x) mysql_num_fields(x->result)
182 #define SQL_ROW MYSQL_ROW
183 #define SQL_FIELD MYSQL_FIELD
185 #else /* USE BACULA DB routines */
187 #define HAVE_BACULA_DB 1
189 /* Change this each time there is some incompatible
190 * file format change!!!!
192 #define BDB_VERSION 9 /* file version number */
195 int bdb_version; /* Version number */
196 uint32_t JobId; /* next Job Id */
197 uint32_t PoolId; /* next Pool Id */
198 uint32_t MediaId; /* next Media Id */
199 uint32_t JobMediaId; /* next JobMedia Id */
200 uint32_t ClientId; /* next Client Id */
201 uint32_t FileSetId; /* nest FileSet Id */
202 time_t time; /* time file written */
206 /* This is the REAL definition for using the
209 typedef struct s_db {
210 BQUEUE bq; /* queue control */
211 /* pthread_mutex_t mutex; */ /* single thread lock */
212 brwlock_t lock; /* transaction lock */
213 int ref_count; /* number of times opened */
214 struct s_control control; /* control file structure */
215 int cfd; /* control file device */
216 FILE *jobfd; /* Jobs records file descriptor */
217 FILE *poolfd; /* Pool records fd */
218 FILE *mediafd; /* Media records fd */
219 FILE *jobmediafd; /* JobMedia records fd */
220 FILE *clientfd; /* Client records fd */
221 FILE *filesetfd; /* FileSet records fd */
222 char *db_name; /* name of database */
223 POOLMEM *errmsg; /* nicely edited error message */
224 POOLMEM *cmd; /* Command string */
225 POOLMEM *cached_path;
226 uint32_t cached_path_id;
229 #endif /* HAVE_MYSQL */
230 #endif /* HAVE_SQLITE */
232 /* Use for better error location printing */
233 #define UPDATE_DB(db, cmd) UpdateDB(__FILE__, __LINE__, db, cmd)
234 #define INSERT_DB(db, cmd) InsertDB(__FILE__, __LINE__, db, cmd)
235 #define QUERY_DB(db, cmd) QueryDB(__FILE__, __LINE__, db, cmd)
236 #define DELETE_DB(db, cmd) DeleteDB(__FILE__, __LINE__, db, cmd)
239 #else /* not __SQL_C */
241 /* This is a "dummy" definition for use outside of sql.c
243 typedef struct s_db {
244 int dummy; /* for SunOS compiler */
249 /* ***FIXME*** FileId_t should be uint64_t */
250 typedef uint32_t FileId_t;
251 typedef uint32_t DBId_t; /* general DB id type */
252 typedef uint32_t JobId_t;
255 /* Job information passed to create job record and update
256 * job record at end of job. Note, although this record
257 * contains all the fields found in the Job database record,
258 * it also contains fields found in the JobMedia record.
263 char Job[MAX_NAME_LENGTH]; /* Job unique name */
264 char Name[MAX_NAME_LENGTH]; /* Job base name */
265 int Type; /* actually char(1) */
266 int Level; /* actually char(1) */
267 int JobStatus; /* actually char(1) */
268 uint32_t ClientId; /* Id of client */
269 uint32_t PoolId; /* Id of pool */
270 uint32_t FileSetId; /* Id of FileSet */
271 time_t SchedTime; /* Time job scheduled */
272 time_t StartTime; /* Job start time */
273 time_t EndTime; /* Job termination time */
274 btime_t JobTDate; /* Backup time/date in seconds */
275 uint32_t VolSessionId;
276 uint32_t VolSessionTime;
279 uint32_t JobMissingFiles;
282 /* Note, FirstIndex, LastIndex, Start/End File and Block
283 * are only used in the JobMedia record.
285 uint32_t FirstIndex; /* First index this Volume */
286 uint32_t LastIndex; /* Last index this Volume */
292 char cSchedTime[MAX_NAME_LENGTH];
293 char cStartTime[MAX_NAME_LENGTH];
294 char cEndTime[MAX_NAME_LENGTH];
295 /* Extra stuff not in DB */
299 /* Job Media information used to create the media records
300 * for each Volume used for the job.
302 /* JobMedia record */
304 uint32_t JobMediaId; /* record id */
305 JobId_t JobId; /* JobId */
306 uint32_t MediaId; /* MediaId */
307 uint32_t FirstIndex; /* First index this Volume */
308 uint32_t LastIndex; /* Last index this Volume */
309 uint32_t StartFile; /* File for start of data */
310 uint32_t EndFile; /* End file on Volume */
311 uint32_t StartBlock; /* start block on tape */
312 uint32_t EndBlock; /* last block */
316 /* Attributes record -- NOT same as in database because
317 * in general, this "record" creates multiple database
318 * records (e.g. pathname, filename, fileattributes).
321 char *fname; /* full path & filename */
322 char *link; /* link if any */
323 char *attr; /* attributes statp */
334 /* File record -- same format as database */
347 /* Pool record -- same format as database */
350 char Name[MAX_NAME_LENGTH]; /* Pool name */
351 uint32_t NumVols; /* total number of volumes */
352 uint32_t MaxVols; /* max allowed volumes */
353 int UseOnce; /* set to use once only */
354 int UseCatalog; /* set to use catalog */
355 int AcceptAnyVolume; /* set to accept any volume sequence */
356 int AutoPrune; /* set to prune automatically */
357 int Recycle; /* default Vol recycle flag */
358 btime_t VolRetention; /* retention period in seconds */
359 char PoolType[MAX_NAME_LENGTH];
360 char LabelFormat[MAX_NAME_LENGTH];
361 /* Extra stuff not in DB */
365 /* Media record -- same as the database */
367 uint32_t MediaId; /* Unique volume id */
368 char VolumeName[MAX_NAME_LENGTH]; /* Volume name */
369 char MediaType[MAX_NAME_LENGTH]; /* Media type */
370 uint32_t PoolId; /* Pool id */
371 time_t FirstWritten; /* Time Volume first written */
372 time_t LastWritten; /* Time Volume last written */
373 time_t LabelDate; /* Date/Time Volume labelled */
374 uint32_t VolJobs; /* number of jobs on this medium */
375 uint32_t VolFiles; /* Number of files */
376 uint32_t VolBlocks; /* Number of blocks */
377 uint32_t VolMounts; /* Number of times mounted */
378 uint32_t VolErrors; /* Number of read/write errors */
379 uint32_t VolWrites; /* Number of writes */
380 uint32_t VolReads; /* Number of reads */
381 uint64_t VolBytes; /* Number of bytes written */
382 uint64_t VolMaxBytes; /* max bytes to write */
383 uint64_t VolCapacityBytes; /* capacity estimate */
384 btime_t VolRetention; /* Volume retention in seconds */
385 int Recycle; /* recycle yes/no */
386 int32_t Slot; /* slot in changer */
387 char VolStatus[20]; /* Volume status */
388 /* Extra stuff not in DB */
389 faddr_t rec_addr; /* found record address */
392 /* Client record -- same as the database */
394 uint32_t ClientId; /* Unique Client id */
396 btime_t FileRetention;
397 btime_t JobRetention;
398 char Name[MAX_NAME_LENGTH]; /* Client name */
399 char Uname[256]; /* Uname for client */
402 /* FileSet record -- same as the database */
404 uint32_t FileSetId; /* Unique FileSet id */
405 char FileSet[MAX_NAME_LENGTH]; /* FileSet name */
406 char MD5[50]; /* MD5 signature of include/exclude */
413 #endif /* __SQL_H_ */