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