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-2003 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 */
93 char *db_address; /* host name address */
94 char *db_socket; /* socket for local access */
96 int db_port; /* port for host name address */
98 char *sqlite_errmsg; /* error message returned by sqlite */
99 POOLMEM *errmsg; /* nicely edited error message */
100 POOLMEM *cmd; /* SQL command string */
101 POOLMEM *cached_path; /* cached path name */
102 int cached_path_len; /* length of cached path */
103 uint32_t cached_path_id; /* cached path id */
104 int transaction; /* transaction started */
105 int changes; /* changes during transaction */
106 POOLMEM *fname; /* Filename only */
107 POOLMEM *path; /* Path only */
108 POOLMEM *esc_name; /* Escaped file/path name */
109 int fnl; /* file name length */
110 int pnl; /* path name length */
115 * "Generic" names for easier conversion
119 #define sql_store_result(x) x->result
120 #define sql_free_result(x) my_sqlite_free_table(x)
121 #define sql_fetch_row(x) my_sqlite_fetch_row(x)
122 #define sql_query(x, y) my_sqlite_query(x, y)
123 #define sql_close(x) sqlite_close((x)->db)
124 #define sql_strerror(x) (x)->sqlite_errmsg?(x)->sqlite_errmsg:"unknown"
125 #define sql_num_rows(x) (x)->nrow
126 #define sql_data_seek(x, i) (x)->row = i
127 #define sql_affected_rows(x) 1
128 #define sql_insert_id(x) sqlite_last_insert_rowid((x)->db)
129 #define sql_field_seek(x, y) my_sqlite_field_seek(x, y)
130 #define sql_fetch_field(x) my_sqlite_fetch_field(x)
131 #define sql_num_fields(x) (unsigned)((x)->ncolumn)
132 #define SQL_ROW char**
136 /* In cats/sqlite.c */
137 extern int my_sqlite_query(B_DB *mdb, char *cmd);
138 extern SQL_ROW my_sqlite_fetch_row(B_DB *mdb);
139 extern void my_sqlite_free_table(B_DB *mdb);
145 #define BDB_VERSION 6
150 * This is the "real" definition that should only be
151 * used inside sql.c and associated database interface
156 typedef struct s_db {
157 BQUEUE bq; /* queue control */
158 brwlock_t lock; /* transaction lock */
162 my_ulonglong num_rows;
167 char *db_address; /* host address */
168 char *db_socket; /* socket for local access */
169 int db_port; /* port of host address */
170 int have_insert_id; /* do have insert_id() */
172 POOLMEM *errmsg; /* nicely edited error message */
173 POOLMEM *cmd; /* SQL command string */
174 POOLMEM *cached_path;
175 int cached_path_len; /* length of cached path */
176 uint32_t cached_path_id;
177 int changes; /* changes made to db */
178 POOLMEM *fname; /* Filename only */
179 POOLMEM *path; /* Path only */
180 POOLMEM *esc_name; /* Escaped file/path name */
181 int fnl; /* file name length */
182 int pnl; /* path name length */
186 /* "Generic" names for easier conversion */
187 #define sql_store_result(x) mysql_store_result((x)->db)
188 #define sql_free_result(x) mysql_free_result((x)->result)
189 #define sql_fetch_row(x) mysql_fetch_row((x)->result)
190 #define sql_query(x, y) mysql_query((x)->db, y)
191 #define sql_close(x) mysql_close((x)->db)
192 #define sql_strerror(x) mysql_error((x)->db)
193 #define sql_num_rows(x) mysql_num_rows((x)->result)
194 #define sql_data_seek(x, i) mysql_data_seek((x)->result, i)
195 #define sql_affected_rows(x) mysql_affected_rows((x)->db)
196 #define sql_insert_id(x) mysql_insert_id((x)->db)
197 #define sql_field_seek(x, y) mysql_field_seek((x)->result, y)
198 #define sql_fetch_field(x) mysql_fetch_field((x)->result)
199 #define sql_num_fields(x) mysql_num_fields((x)->result)
200 #define SQL_ROW MYSQL_ROW
201 #define SQL_FIELD MYSQL_FIELD
203 #else /* USE BACULA DB routines */
205 #define HAVE_BACULA_DB 1
207 /* Change this each time there is some incompatible
208 * file format change!!!!
210 #define BDB_VERSION 12 /* file version number */
213 int bdb_version; /* Version number */
214 uint32_t JobId; /* next Job Id */
215 uint32_t PoolId; /* next Pool Id */
216 uint32_t MediaId; /* next Media Id */
217 uint32_t JobMediaId; /* next JobMedia Id */
218 uint32_t ClientId; /* next Client Id */
219 uint32_t FileSetId; /* nest FileSet Id */
220 time_t time; /* time file written */
224 /* This is the REAL definition for using the
227 typedef struct s_db {
228 BQUEUE bq; /* queue control */
229 /* pthread_mutex_t mutex; */ /* single thread lock */
230 brwlock_t lock; /* transaction lock */
231 int ref_count; /* number of times opened */
232 struct s_control control; /* control file structure */
233 int cfd; /* control file device */
234 FILE *jobfd; /* Jobs records file descriptor */
235 FILE *poolfd; /* Pool records fd */
236 FILE *mediafd; /* Media records fd */
237 FILE *jobmediafd; /* JobMedia records fd */
238 FILE *clientfd; /* Client records fd */
239 FILE *filesetfd; /* FileSet records fd */
240 char *db_name; /* name of database */
241 POOLMEM *errmsg; /* nicely edited error message */
242 POOLMEM *cmd; /* Command string */
243 POOLMEM *cached_path;
244 int cached_path_len; /* length of cached path */
245 uint32_t cached_path_id;
248 #endif /* HAVE_MYSQL */
249 #endif /* HAVE_SQLITE */
251 /* Use for better error location printing */
252 #define UPDATE_DB(jcr, db, cmd) UpdateDB(__FILE__, __LINE__, jcr, db, cmd)
253 #define INSERT_DB(jcr, db, cmd) InsertDB(__FILE__, __LINE__, jcr, db, cmd)
254 #define QUERY_DB(jcr, db, cmd) QueryDB(__FILE__, __LINE__, jcr, db, cmd)
255 #define DELETE_DB(jcr, db, cmd) DeleteDB(__FILE__, __LINE__, jcr, db, cmd)
258 #else /* not __SQL_C */
260 /* This is a "dummy" definition for use outside of sql.c
262 typedef struct s_db {
263 int dummy; /* for SunOS compiler */
268 extern uint32_t bacula_db_version;
270 /* ***FIXME*** FileId_t should be uint64_t */
271 typedef uint32_t FileId_t;
272 typedef uint32_t DBId_t; /* general DB id type */
273 typedef uint32_t JobId_t;
278 /* Job information passed to create job record and update
279 * job record at end of job. Note, although this record
280 * contains all the fields found in the Job database record,
281 * it also contains fields found in the JobMedia record.
286 char Job[MAX_NAME_LENGTH]; /* Job unique name */
287 char Name[MAX_NAME_LENGTH]; /* Job base name */
288 int Type; /* actually char(1) */
289 int Level; /* actually char(1) */
290 int JobStatus; /* actually char(1) */
291 uint32_t ClientId; /* Id of client */
292 uint32_t PoolId; /* Id of pool */
293 uint32_t FileSetId; /* Id of FileSet */
294 time_t SchedTime; /* Time job scheduled */
295 time_t StartTime; /* Job start time */
296 time_t EndTime; /* Job termination time */
297 utime_t JobTDate; /* Backup time/date in seconds */
298 uint32_t VolSessionId;
299 uint32_t VolSessionTime;
302 uint32_t JobMissingFiles;
305 /* Note, FirstIndex, LastIndex, Start/End File and Block
306 * are only used in the JobMedia record.
308 uint32_t FirstIndex; /* First index this Volume */
309 uint32_t LastIndex; /* Last index this Volume */
315 char cSchedTime[MAX_TIME_LENGTH];
316 char cStartTime[MAX_TIME_LENGTH];
317 char cEndTime[MAX_TIME_LENGTH];
318 /* Extra stuff not in DB */
322 /* Job Media information used to create the media records
323 * for each Volume used for the job.
325 /* JobMedia record */
326 struct JOBMEDIA_DBR {
327 uint32_t JobMediaId; /* record id */
328 JobId_t JobId; /* JobId */
329 uint32_t MediaId; /* MediaId */
330 uint32_t FirstIndex; /* First index this Volume */
331 uint32_t LastIndex; /* Last index this Volume */
332 uint32_t StartFile; /* File for start of data */
333 uint32_t EndFile; /* End file on Volume */
334 uint32_t StartBlock; /* start block on tape */
335 uint32_t EndBlock; /* last block */
339 /* Volume Parameter structure */
341 char VolumeName[MAX_NAME_LENGTH]; /* Volume name */
342 uint32_t VolIndex; /* Volume seqence no. */
343 uint32_t FirstIndex; /* First index this Volume */
344 uint32_t LastIndex; /* Last index this Volume */
345 uint32_t StartFile; /* File for start of data */
346 uint32_t EndFile; /* End file on Volume */
347 uint32_t StartBlock; /* start block on tape */
348 uint32_t EndBlock; /* last block */
352 /* Attributes record -- NOT same as in database because
353 * in general, this "record" creates multiple database
354 * records (e.g. pathname, filename, fileattributes).
357 char *fname; /* full path & filename */
358 char *link; /* link if any */
359 char *attr; /* attributes statp */
370 /* File record -- same format as database */
381 int SigType; /* NO_SIG/MD5_SIG/SHA1_SIG */
384 /* Pool record -- same format as database */
387 char Name[MAX_NAME_LENGTH]; /* Pool name */
388 uint32_t NumVols; /* total number of volumes */
389 uint32_t MaxVols; /* max allowed volumes */
390 int UseOnce; /* set to use once only */
391 int UseCatalog; /* set to use catalog */
392 int AcceptAnyVolume; /* set to accept any volume sequence */
393 int AutoPrune; /* set to prune automatically */
394 int Recycle; /* default Vol recycle flag */
395 utime_t VolRetention; /* retention period in seconds */
396 utime_t VolUseDuration; /* time in secs volume can be used */
397 uint32_t MaxVolJobs; /* Max Jobs on Volume */
398 uint32_t MaxVolFiles; /* Max files on Volume */
399 uint64_t MaxVolBytes; /* Max bytes on Volume */
400 char PoolType[MAX_NAME_LENGTH];
401 char LabelFormat[MAX_NAME_LENGTH];
402 /* Extra stuff not in DB */
406 /* Media record -- same as the database */
408 uint32_t MediaId; /* Unique volume id */
409 char VolumeName[MAX_NAME_LENGTH]; /* Volume name */
410 char MediaType[MAX_NAME_LENGTH]; /* Media type */
411 uint32_t PoolId; /* Pool id */
412 time_t FirstWritten; /* Time Volume first written */
413 time_t LastWritten; /* Time Volume last written */
414 time_t LabelDate; /* Date/Time Volume labeled */
415 uint32_t VolJobs; /* number of jobs on this medium */
416 uint32_t VolFiles; /* Number of files */
417 uint32_t VolBlocks; /* Number of blocks */
418 uint32_t VolMounts; /* Number of times mounted */
419 uint32_t VolErrors; /* Number of read/write errors */
420 uint32_t VolWrites; /* Number of writes */
421 uint32_t VolReads; /* Number of reads */
422 uint64_t VolBytes; /* Number of bytes written */
423 uint64_t MaxVolBytes; /* Max bytes to write to Volume */
424 uint64_t VolCapacityBytes; /* capacity estimate */
425 utime_t VolRetention; /* Volume retention in seconds */
426 utime_t VolUseDuration; /* time in secs volume can be used */
427 uint32_t MaxVolJobs; /* Max Jobs on Volume */
428 uint32_t MaxVolFiles; /* Max files on Volume */
429 int Recycle; /* recycle yes/no */
430 int32_t Slot; /* slot in changer */
431 char VolStatus[20]; /* Volume status */
432 /* Extra stuff not in DB */
433 faddr_t rec_addr; /* found record address */
434 /* Since the database returns times as strings, this is how we pass
437 char cFirstWritten[MAX_TIME_LENGTH]; /* FirstWritten returned from DB */
438 char cLastWritten[MAX_TIME_LENGTH]; /* LastWritten returned from DB */
439 char cLabelData[MAX_TIME_LENGTH]; /* LabelData returned from DB */
442 /* Client record -- same as the database */
444 uint32_t ClientId; /* Unique Client id */
446 utime_t FileRetention;
447 utime_t JobRetention;
448 char Name[MAX_NAME_LENGTH]; /* Client name */
449 char Uname[256]; /* Uname for client */
452 /* Counter record as in database */
454 char Counter[MAX_NAME_LENGTH];
457 int32_t CurrentValue;
458 char WrapCounter[MAX_NAME_LENGTH];
462 /* FileSet record -- same as the database */
464 uint32_t FileSetId; /* Unique FileSet id */
465 char FileSet[MAX_NAME_LENGTH]; /* FileSet name */
466 char MD5[50]; /* MD5 signature of include/exclude */
467 time_t CreateTime; /* date created */
469 * This is where we return CreateTime
471 char cCreateTime[MAX_TIME_LENGTH]; /* CreateTime as returned from DB */
472 /* Not in DB but returned by db_create_fileset() */
473 bool created; /* set when record newly created */
480 #endif /* __SQL_H_ */