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 5
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 11 /* 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 /* ***FIXME*** FileId_t should be uint64_t */
269 typedef uint32_t FileId_t;
270 typedef uint32_t DBId_t; /* general DB id type */
271 typedef uint32_t JobId_t;
276 /* Job information passed to create job record and update
277 * job record at end of job. Note, although this record
278 * contains all the fields found in the Job database record,
279 * it also contains fields found in the JobMedia record.
284 char Job[MAX_NAME_LENGTH]; /* Job unique name */
285 char Name[MAX_NAME_LENGTH]; /* Job base name */
286 int Type; /* actually char(1) */
287 int Level; /* actually char(1) */
288 int JobStatus; /* actually char(1) */
289 uint32_t ClientId; /* Id of client */
290 uint32_t PoolId; /* Id of pool */
291 uint32_t FileSetId; /* Id of FileSet */
292 time_t SchedTime; /* Time job scheduled */
293 time_t StartTime; /* Job start time */
294 time_t EndTime; /* Job termination time */
295 utime_t JobTDate; /* Backup time/date in seconds */
296 uint32_t VolSessionId;
297 uint32_t VolSessionTime;
300 uint32_t JobMissingFiles;
303 /* Note, FirstIndex, LastIndex, Start/End File and Block
304 * are only used in the JobMedia record.
306 uint32_t FirstIndex; /* First index this Volume */
307 uint32_t LastIndex; /* Last index this Volume */
313 char cSchedTime[MAX_TIME_LENGTH];
314 char cStartTime[MAX_TIME_LENGTH];
315 char cEndTime[MAX_TIME_LENGTH];
316 /* Extra stuff not in DB */
320 /* Job Media information used to create the media records
321 * for each Volume used for the job.
323 /* JobMedia record */
324 struct JOBMEDIA_DBR {
325 uint32_t JobMediaId; /* record id */
326 JobId_t JobId; /* JobId */
327 uint32_t MediaId; /* MediaId */
328 uint32_t FirstIndex; /* First index this Volume */
329 uint32_t LastIndex; /* Last index this Volume */
330 uint32_t StartFile; /* File for start of data */
331 uint32_t EndFile; /* End file on Volume */
332 uint32_t StartBlock; /* start block on tape */
333 uint32_t EndBlock; /* last block */
337 /* Volume Parameter structure */
339 char VolumeName[MAX_NAME_LENGTH]; /* Volume name */
340 uint32_t VolIndex; /* Volume seqence no. */
341 uint32_t FirstIndex; /* First index this Volume */
342 uint32_t LastIndex; /* Last index this Volume */
343 uint32_t StartFile; /* File for start of data */
344 uint32_t EndFile; /* End file on Volume */
345 uint32_t StartBlock; /* start block on tape */
346 uint32_t EndBlock; /* last block */
350 /* Attributes record -- NOT same as in database because
351 * in general, this "record" creates multiple database
352 * records (e.g. pathname, filename, fileattributes).
355 char *fname; /* full path & filename */
356 char *link; /* link if any */
357 char *attr; /* attributes statp */
368 /* File record -- same format as database */
379 int SigType; /* NO_SIG/MD5_SIG/SHA1_SIG */
382 /* Pool record -- same format as database */
385 char Name[MAX_NAME_LENGTH]; /* Pool name */
386 uint32_t NumVols; /* total number of volumes */
387 uint32_t MaxVols; /* max allowed volumes */
388 int UseOnce; /* set to use once only */
389 int UseCatalog; /* set to use catalog */
390 int AcceptAnyVolume; /* set to accept any volume sequence */
391 int AutoPrune; /* set to prune automatically */
392 int Recycle; /* default Vol recycle flag */
393 utime_t VolRetention; /* retention period in seconds */
394 utime_t VolUseDuration; /* time in secs volume can be used */
395 uint32_t MaxVolJobs; /* Max Jobs on Volume */
396 uint32_t MaxVolFiles; /* Max files on Volume */
397 uint64_t MaxVolBytes; /* Max bytes on Volume */
398 char PoolType[MAX_NAME_LENGTH];
399 char LabelFormat[MAX_NAME_LENGTH];
400 /* Extra stuff not in DB */
404 /* Media record -- same as the database */
406 uint32_t MediaId; /* Unique volume id */
407 char VolumeName[MAX_NAME_LENGTH]; /* Volume name */
408 char MediaType[MAX_NAME_LENGTH]; /* Media type */
409 uint32_t PoolId; /* Pool id */
410 time_t FirstWritten; /* Time Volume first written */
411 time_t LastWritten; /* Time Volume last written */
412 time_t LabelDate; /* Date/Time Volume labeled */
413 uint32_t VolJobs; /* number of jobs on this medium */
414 uint32_t VolFiles; /* Number of files */
415 uint32_t VolBlocks; /* Number of blocks */
416 uint32_t VolMounts; /* Number of times mounted */
417 uint32_t VolErrors; /* Number of read/write errors */
418 uint32_t VolWrites; /* Number of writes */
419 uint32_t VolReads; /* Number of reads */
420 uint64_t VolBytes; /* Number of bytes written */
421 uint64_t MaxVolBytes; /* Max bytes to write to Volume */
422 uint64_t VolCapacityBytes; /* capacity estimate */
423 utime_t VolRetention; /* Volume retention in seconds */
424 utime_t VolUseDuration; /* time in secs volume can be used */
425 uint32_t MaxVolJobs; /* Max Jobs on Volume */
426 uint32_t MaxVolFiles; /* Max files on Volume */
427 int Recycle; /* recycle yes/no */
428 int32_t Slot; /* slot in changer */
429 char VolStatus[20]; /* Volume status */
430 /* Extra stuff not in DB */
431 faddr_t rec_addr; /* found record address */
432 /* Since the database returns times as strings, this is how we pass
435 char cFirstWritten[MAX_TIME_LENGTH]; /* FirstWritten returned from DB */
436 char cLastWritten[MAX_TIME_LENGTH]; /* LastWritten returned from DB */
437 char cLabelData[MAX_TIME_LENGTH]; /* LabelData returned from DB */
440 /* Client record -- same as the database */
442 uint32_t ClientId; /* Unique Client id */
444 utime_t FileRetention;
445 utime_t JobRetention;
446 char Name[MAX_NAME_LENGTH]; /* Client name */
447 char Uname[256]; /* Uname for client */
450 /* FileSet record -- same as the database */
452 uint32_t FileSetId; /* Unique FileSet id */
453 char FileSet[MAX_NAME_LENGTH]; /* FileSet name */
454 char MD5[50]; /* MD5 signature of include/exclude */
461 #endif /* __SQL_H_ */