]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/cats.h
Last major change for 1.27 code -- see kes13Nov02
[bacula/bacula] / bacula / src / cats / cats.h
1 /*
2  * SQL header file
3  *
4  *   by Kern E. Sibbald
5  *
6  *   Anyone who accesses the database will need to include
7  *   this file.
8  *
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
13  *
14  *    Version $Id$
15  */
16
17 /*
18    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
19
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.
24
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.
29
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,
33    MA 02111-1307, USA.
34
35  */
36
37 #ifndef __SQL_H_
38 #define __SQL_H_ 1
39
40
41 typedef void (DB_LIST_HANDLER)(void *, char *);
42 typedef int (DB_RESULT_HANDLER)(void *, int, char **);
43
44 #define db_lock(mdb)   _db_lock(__FILE__, __LINE__, mdb)
45 #define db_unlock(mdb) _db_unlock(__FILE__, __LINE__, mdb)
46
47 #ifdef __SQL_C
48
49 #ifdef HAVE_SQLITE
50
51 #define BDB_VERSION 3
52
53 #include <sqlite.h>
54
55 /* Define opaque structure for sqlite */
56 struct sqlite {
57    char dummy;
58 };
59
60 #define IS_NUM(x)             ((x) == 1)
61 #define IS_NOT_NULL(x)        ((x) == 1)
62
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 */
69 } SQL_FIELD;
70
71 /*
72  * This is the "real" definition that should only be
73  * used inside sql.c and associated database interface
74  * subroutines.
75  *                    S Q L I T E
76  */
77 typedef struct s_db {
78    BQUEUE bq;                         /* queue control */
79    brwlock_t lock;                    /* transaction lock */
80    struct sqlite *db;
81    char **result;
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 */
90    int ref_count;
91    char *db_name;
92    char *db_user;
93    char *db_password;
94    int connected;
95    char *sqlite_errmsg;               /* error message returned by sqlite */
96    POOLMEM *errmsg;                   /* nicely edited error message */
97    POOLMEM *cmd;                      /* SQL command string */
98    POOLMEM *cached_path;
99    uint32_t cached_path_id;
100    int transaction;                   /* transaction started */
101    int changes;                       /* changes during transaction */
102    void *jcr;                         /* JCR or NULL */
103 } B_DB;
104
105
106 /* 
107  * "Generic" names for easier conversion   
108  *
109  *                    S Q L I T E
110  */
111 #define sql_store_result(x)   x->result
112 #define sql_free_result(x)    my_sqlite_free_table(x)
113 #define sql_fetch_row(x)      my_sqlite_fetch_row(x)
114 #define sql_query(x, y)       my_sqlite_query(x, y)
115 #define sql_close(x)          sqlite_close(x->db)  
116 #define sql_strerror(x)       x->sqlite_errmsg?x->sqlite_errmsg:"unknown"
117 #define sql_num_rows(x)       x->nrow
118 #define sql_data_seek(x, i)   x->row = i
119 #define sql_affected_rows(x)  1
120 #define sql_insert_id(x)      sqlite_last_insert_rowid(x->db)
121 #define sql_field_seek(x, y)  my_sqlite_field_seek(x, y)
122 #define sql_fetch_field(x)    my_sqlite_fetch_field(x)
123 #define sql_num_fields(x)     (unsigned)((x)->ncolumn)
124 #define SQL_ROW               char**   
125
126
127
128 /* In cats/sqlite.c */
129 extern int     my_sqlite_query(B_DB *mdb, char *cmd);
130 extern SQL_ROW my_sqlite_fetch_row(B_DB *mdb);
131 extern void my_sqlite_free_table(B_DB *mdb);
132
133 #else
134
135 #ifdef HAVE_MYSQL
136
137 #define BDB_VERSION 3
138
139 #include <mysql.h>
140
141 /*
142  * This is the "real" definition that should only be
143  * used inside sql.c and associated database interface
144  * subroutines.
145  *
146  *                     M Y S Q L
147  */
148 typedef struct s_db {
149    BQUEUE bq;                         /* queue control */
150    brwlock_t lock;                    /* transaction lock */
151    MYSQL mysql;
152    MYSQL *db;
153    MYSQL_RES *result;
154    my_ulonglong num_rows;
155    int ref_count;
156    char *db_name;
157    char *db_user;
158    char *db_password;
159    int have_insert_id;                /* do have insert_id() */
160    int connected;
161    POOLMEM *errmsg;                   /* nicely edited error message */
162    POOLMEM *cmd;                      /* SQL command string */
163    POOLMEM *cached_path;
164    uint32_t cached_path_id;
165    int changes;                       /* changes made to db */
166    void *jcr;                         /* JCR or NULL */
167 } B_DB;
168
169
170 /* "Generic" names for easier conversion */
171 #define sql_store_result(x)   mysql_store_result(x->db)
172 #define sql_free_result(x)    mysql_free_result(x->result)
173 #define sql_fetch_row(x)      mysql_fetch_row(x->result)
174 #define sql_query(x, y)       mysql_query(x->db, y)
175 #define sql_close(x)          mysql_close(x->db)  
176 #define sql_strerror(x)       mysql_error(x->db)
177 #define sql_num_rows(x)       mysql_num_rows(x->result)
178 #define sql_data_seek(x, i)   mysql_data_seek(x->result, i)
179 #define sql_affected_rows(x)  mysql_affected_rows(x->db)
180 #define sql_insert_id(x)      mysql_insert_id(x->db)
181 #define sql_field_seek(x, y)  mysql_field_seek(x->result, y)
182 #define sql_fetch_field(x)    mysql_fetch_field(x->result)
183 #define sql_num_fields(x)     mysql_num_fields(x->result)
184 #define SQL_ROW               MYSQL_ROW
185 #define SQL_FIELD             MYSQL_FIELD
186
187 #else  /* USE BACULA DB routines */
188
189 #define HAVE_BACULA_DB 1
190
191 /* Change this each time there is some incompatible
192  * file format change!!!!
193  */
194 #define BDB_VERSION 10                /* file version number */
195
196 struct s_control {
197    int bdb_version;                   /* Version number */
198    uint32_t JobId;                    /* next Job Id */
199    uint32_t PoolId;                   /* next Pool Id */
200    uint32_t MediaId;                  /* next Media Id */
201    uint32_t JobMediaId;               /* next JobMedia Id */
202    uint32_t ClientId;                 /* next Client Id */
203    uint32_t FileSetId;                /* nest FileSet Id */
204    time_t time;                       /* time file written */
205 };
206
207
208 /* This is the REAL definition for using the
209  *  Bacula internal DB
210  */
211 typedef struct s_db {
212    BQUEUE bq;                         /* queue control */
213 /* pthread_mutex_t mutex;  */         /* single thread lock */
214    brwlock_t lock;                    /* transaction lock */
215    int ref_count;                     /* number of times opened */
216    struct s_control control;          /* control file structure */
217    int cfd;                           /* control file device */
218    FILE *jobfd;                       /* Jobs records file descriptor */
219    FILE *poolfd;                      /* Pool records fd */
220    FILE *mediafd;                     /* Media records fd */
221    FILE *jobmediafd;                  /* JobMedia records fd */
222    FILE *clientfd;                    /* Client records fd */
223    FILE *filesetfd;                   /* FileSet records fd */
224    char *db_name;                     /* name of database */
225    POOLMEM *errmsg;                   /* nicely edited error message */
226    POOLMEM *cmd;                      /* Command string */
227    POOLMEM *cached_path;
228    uint32_t cached_path_id;
229    void *jcr;                         /* JCR or NULL */
230 } B_DB;
231
232 #endif /* HAVE_MYSQL */
233 #endif /* HAVE_SQLITE */
234
235 /* Use for better error location printing */
236 #define UPDATE_DB(db, cmd) UpdateDB(__FILE__, __LINE__, db, cmd)
237 #define INSERT_DB(db, cmd) InsertDB(__FILE__, __LINE__, db, cmd)
238 #define QUERY_DB(db, cmd) QueryDB(__FILE__, __LINE__, db, cmd)
239 #define DELETE_DB(db, cmd) DeleteDB(__FILE__, __LINE__, db, cmd)
240
241
242 #else    /* not __SQL_C */
243
244 /* This is a "dummy" definition for use outside of sql.c
245  */
246 typedef struct s_db {     
247    int dummy;                         /* for SunOS compiler */
248 } B_DB;  
249
250 #endif /*  __SQL_C */
251
252 /* ***FIXME*** FileId_t should be uint64_t */
253 typedef uint32_t FileId_t;
254 typedef uint32_t DBId_t;              /* general DB id type */
255 typedef uint32_t JobId_t;
256
257
258 /* Job information passed to create job record and update
259  * job record at end of job. Note, although this record
260  * contains all the fields found in the Job database record,
261  * it also contains fields found in the JobMedia record.
262  */
263 /* Job record */
264 typedef struct {
265    JobId_t JobId;
266    char Job[MAX_NAME_LENGTH];         /* Job unique name */
267    char Name[MAX_NAME_LENGTH];        /* Job base name */
268    int Type;                          /* actually char(1) */
269    int Level;                         /* actually char(1) */
270    int JobStatus;                     /* actually char(1) */
271    uint32_t ClientId;                 /* Id of client */
272    uint32_t PoolId;                   /* Id of pool */
273    uint32_t FileSetId;                /* Id of FileSet */
274    time_t SchedTime;                  /* Time job scheduled */
275    time_t StartTime;                  /* Job start time */
276    time_t EndTime;                    /* Job termination time */
277    utime_t JobTDate;                  /* Backup time/date in seconds */
278    uint32_t VolSessionId;
279    uint32_t VolSessionTime;
280    uint32_t JobFiles;
281    uint32_t JobErrors;
282    uint32_t JobMissingFiles;
283    uint64_t JobBytes;
284
285    /* Note, FirstIndex, LastIndex, Start/End File and Block
286     * are only used in the JobMedia record.
287     */
288    uint32_t FirstIndex;               /* First index this Volume */
289    uint32_t LastIndex;                /* Last index this Volume */
290    uint32_t StartFile;
291    uint32_t EndFile;
292    uint32_t StartBlock;
293    uint32_t EndBlock;
294
295    char cSchedTime[MAX_TIME_LENGTH];
296    char cStartTime[MAX_TIME_LENGTH];
297    char cEndTime[MAX_TIME_LENGTH];
298    /* Extra stuff not in DB */
299    faddr_t rec_addr;
300 } JOB_DBR;
301
302 /* Job Media information used to create the media records
303  * for each Volume used for the job.
304  */
305 /* JobMedia record */
306 typedef struct {
307    uint32_t JobMediaId;               /* record id */
308    JobId_t  JobId;                    /* JobId */
309    uint32_t MediaId;                  /* MediaId */
310    uint32_t FirstIndex;               /* First index this Volume */
311    uint32_t LastIndex;                /* Last index this Volume */
312    uint32_t StartFile;                /* File for start of data */
313    uint32_t EndFile;                  /* End file on Volume */
314    uint32_t StartBlock;               /* start block on tape */
315    uint32_t EndBlock;                 /* last block */
316 } JOBMEDIA_DBR;
317
318
319 /* Attributes record -- NOT same as in database because
320  *  in general, this "record" creates multiple database
321  *  records (e.g. pathname, filename, fileattributes).
322  */
323 typedef struct {
324    char *fname;                       /* full path & filename */
325    char *link;                        /* link if any */
326    char *attr;                        /* attributes statp */
327    uint32_t FileIndex;
328    uint32_t Stream;
329    JobId_t  JobId;
330    uint32_t ClientId;
331    uint32_t PathId;
332    uint32_t FilenameId;
333    FileId_t FileId;
334 } ATTR_DBR;
335
336
337 /* File record -- same format as database */
338 typedef struct {
339    FileId_t FileId;
340    uint32_t FileIndex;
341    JobId_t  JobId;
342    uint32_t FilenameId;
343    uint32_t PathId;
344    JobId_t  MarkId;
345    char LStat[256];
346 /*   int Status; */
347    char MD5[50];
348 } FILE_DBR;
349
350 /* Pool record -- same format as database */
351 typedef struct {
352    uint32_t PoolId;
353    char Name[MAX_NAME_LENGTH];        /* Pool name */
354    uint32_t NumVols;                  /* total number of volumes */
355    uint32_t MaxVols;                  /* max allowed volumes */
356    int UseOnce;                       /* set to use once only */
357    int UseCatalog;                    /* set to use catalog */
358    int AcceptAnyVolume;               /* set to accept any volume sequence */
359    int AutoPrune;                     /* set to prune automatically */
360    int Recycle;                       /* default Vol recycle flag */
361    utime_t VolRetention;              /* retention period in seconds */
362    char PoolType[MAX_NAME_LENGTH];             
363    char LabelFormat[MAX_NAME_LENGTH];
364    /* Extra stuff not in DB */
365    faddr_t rec_addr;
366 } POOL_DBR;
367
368 /* Media record -- same as the database */
369 typedef struct {
370    uint32_t MediaId;                  /* Unique volume id */
371    char VolumeName[MAX_NAME_LENGTH];  /* Volume name */
372    char MediaType[MAX_NAME_LENGTH];   /* Media type */
373    uint32_t PoolId;                   /* Pool id */
374    time_t   FirstWritten;             /* Time Volume first written */
375    time_t   LastWritten;              /* Time Volume last written */
376    time_t   LabelDate;                /* Date/Time Volume labelled */
377    uint32_t VolJobs;                  /* number of jobs on this medium */
378    uint32_t VolFiles;                 /* Number of files */
379    uint32_t VolBlocks;                /* Number of blocks */
380    uint32_t VolMounts;                /* Number of times mounted */
381    uint32_t VolErrors;                /* Number of read/write errors */
382    uint32_t VolWrites;                /* Number of writes */
383    uint32_t VolReads;                 /* Number of reads */
384    uint64_t VolBytes;                 /* Number of bytes written */
385    uint64_t VolMaxBytes;              /* max bytes to write */
386    uint64_t VolCapacityBytes;         /* capacity estimate */
387    utime_t  VolRetention;             /* Volume retention in seconds */
388    int      Recycle;                  /* recycle yes/no */
389    int32_t  Slot;                     /* slot in changer */
390    char VolStatus[20];                /* Volume status */
391    /* Extra stuff not in DB */
392    faddr_t rec_addr;                  /* found record address */
393    /* Since the database returns times as strings, this is how we pass
394     *   them back.
395     */
396    char    cFirstWritten[MAX_TIME_LENGTH];  /* FirstWritten returned from DB */
397    char    cLastWritten[MAX_TIME_LENGTH];  /* LastWritten returned from DB */
398    char    cLabelData[MAX_TIME_LENGTH];  /* LabelData returned from DB */
399 } MEDIA_DBR;
400
401 /* Client record -- same as the database */
402 typedef struct {
403    uint32_t ClientId;                 /* Unique Client id */
404    int AutoPrune;
405    utime_t FileRetention;
406    utime_t JobRetention;
407    char Name[MAX_NAME_LENGTH];        /* Client name */
408    char Uname[256];                   /* Uname for client */
409 } CLIENT_DBR;
410
411 /* FileSet record -- same as the database */
412 typedef struct {
413    uint32_t FileSetId;                /* Unique FileSet id */
414    char FileSet[MAX_NAME_LENGTH];     /* FileSet name */
415    char MD5[50];                      /* MD5 signature of include/exclude */
416 } FILESET_DBR;
417
418
419 #include "protos.h"
420 #include "jcr.h"
421  
422 #endif /* __SQL_H_ */