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