]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/cats.h
kes Correct return status on db_batch... routines.
[bacula/bacula] / bacula / src / cats / cats.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation plus additions
11    that are listed in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of John Walker.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  * SQL header file
30  *
31  *   by Kern E. Sibbald
32  *
33  *   Anyone who accesses the database will need to include
34  *   this file.
35  *
36  * This file contains definitions common to sql.c and
37  * the external world, and definitions destined only
38  * for the external world. This is control with
39  * the define __SQL_C, which is defined only in sql.c
40  *
41  *    Version $Id$
42  */
43
44 /*
45    Here is how database versions work. 
46
47    While I am working on a new release with database changes, the
48    update scripts are in the src/cats directory under the names
49    update_xxx_tables.in.  Most of the time, I make database updates
50    in one go and immediately update the version, but not always.  If
51    there are going to be several updates as is the case with version
52    1.37, then I will often forgo changing the version until the last
53    update otherwise I will end up with too many versions and a lot
54    of confusion.
55
56    When I am pretty sure there will be no more updates, I will
57    change the version from 8 to 9 (in the present case), and when I
58    am 100% sure there will be no more changes, the update script
59    will be copied to the updatedb directory with the correct name
60    (in the present case 8 to 9).
61
62    Now, in principle, each of the different DB implementations 
63    can have a different version, but in practice they are all
64    the same (simplifies things). The exception is the internal
65    database, which is no longer used, and hence, no longer changes.
66  */
67
68
69 #ifndef __SQL_H_
70 #define __SQL_H_ 1
71
72
73 typedef void (DB_LIST_HANDLER)(void *, const char *);
74 typedef int (DB_RESULT_HANDLER)(void *, int, char **);
75
76 #define db_lock(mdb)   _db_lock(__FILE__, __LINE__, mdb)
77 #define db_unlock(mdb) _db_unlock(__FILE__, __LINE__, mdb)
78
79 #ifdef __SQL_C
80
81 #if defined(BUILDING_CATS)
82 #ifdef HAVE_SQLITE
83
84 #define BDB_VERSION 10
85
86 #include <sqlite.h>
87
88 /* Define opaque structure for sqlite */
89 struct sqlite {
90    char dummy;
91 };
92
93 #define IS_NUM(x)             ((x) == 1)
94 #define IS_NOT_NULL(x)        ((x) == 1)
95
96 typedef struct s_sql_field {
97    char *name;                        /* name of column */
98    int length;                        /* length */
99    int max_length;                    /* max length */
100    uint32_t type;                     /* type */
101    uint32_t flags;                    /* flags */
102 } SQL_FIELD;
103
104 /*
105  * This is the "real" definition that should only be
106  * used inside sql.c and associated database interface
107  * subroutines.
108  *                    S Q L I T E
109  */
110 struct B_DB {
111    BQUEUE bq;                         /* queue control */
112    brwlock_t lock;                    /* transaction lock */
113    struct sqlite *db;
114    char **result;
115    int status;
116    int nrow;                          /* nrow returned from sqlite */
117    int ncolumn;                       /* ncolum returned from sqlite */
118    int num_rows;                      /* used by code */
119    int row;                           /* seek row */
120    int field;                         /* seek field */
121    SQL_FIELD **fields;                /* defined fields */
122    int ref_count;
123    char *db_name;
124    char *db_user;
125    char *db_address;                  /* host name address */
126    char *db_socket;                   /* socket for local access */
127    char *db_password;
128    int  db_port;                      /* port for host name address */
129    bool connected;                    /* connection made to db */
130    bool have_insert_id;               /* do not have insert id */
131    bool fields_defined;               /* set when fields defined */
132    char *sqlite_errmsg;               /* error message returned by sqlite */
133    POOLMEM *errmsg;                   /* nicely edited error message */
134    POOLMEM *cmd;                      /* SQL command string */
135    POOLMEM *cached_path;              /* cached path name */
136    int cached_path_len;               /* length of cached path */
137    uint32_t cached_path_id;           /* cached path id */
138    bool allow_transactions;           /* transactions allowed */
139    bool transaction;                  /* transaction started */
140    int changes;                       /* changes during transaction */
141    POOLMEM *fname;                    /* Filename only */
142    POOLMEM *path;                     /* Path only */
143    POOLMEM *esc_name;                 /* Escaped file name */
144    POOLMEM *esc_name2;                /* Escaped path name */
145    int fnl;                           /* file name length */
146    int pnl;                           /* path name length */
147 };
148
149
150 /*
151  * "Generic" names for easier conversion
152  *
153  *                    S Q L I T E
154  */
155 #define sql_store_result(x)   (x)->result
156 #define sql_free_result(x)    my_sqlite_free_table(x)
157 #define sql_fetch_row(x)      my_sqlite_fetch_row(x)
158 #define sql_query(x, y)       my_sqlite_query((x), (y))
159 #ifdef HAVE_SQLITE3
160 #define sql_insert_id(x,y)    sqlite3_last_insert_rowid((x)->db)
161 #define sql_close(x)          sqlite3_close((x)->db)
162 #else
163 #define sql_insert_id(x,y)    sqlite_last_insert_rowid((x)->db)
164 #define sql_close(x)          sqlite_close((x)->db)
165 #endif
166 #define sql_strerror(x)       (x)->sqlite_errmsg?(x)->sqlite_errmsg:"unknown"
167 #define sql_num_rows(x)       (x)->nrow
168 #define sql_data_seek(x, i)   (x)->row = (i)
169 #define sql_affected_rows(x)  1
170 #define sql_field_seek(x, y)  my_sqlite_field_seek((x), (y))
171 #define sql_fetch_field(x)    my_sqlite_fetch_field(x)
172 #define sql_num_fields(x)     ((x)->ncolumn)
173 #define SQL_ROW               char**
174 #define sql_batch_lock_path_query       my_sqlite_batch_lock_query
175 #define sql_batch_lock_filename_query   my_sqlite_batch_lock_query
176 #define sql_batch_unlock_tables_query   my_sqlite_batch_unlock_query
177 #define sql_batch_fill_filename_query   my_sqlite_batch_fill_filename_query
178 #define sql_batch_fill_path_query       my_sqlite_batch_fill_path_query    
179
180 /* In cats/sqlite.c */
181 void       my_sqlite_free_table(B_DB *mdb);
182 SQL_ROW    my_sqlite_fetch_row(B_DB *mdb);
183 int        my_sqlite_query(B_DB *mdb, const char *cmd);
184 void       my_sqlite_field_seek(B_DB *mdb, int field);
185 SQL_FIELD *my_sqlite_fetch_field(B_DB *mdb);
186 extern char* my_sqlite_batch_lock_query;
187 extern char* my_sqlite_batch_unlock_query;
188 extern char* my_sqlite_batch_fill_filename_query;
189 extern char* my_sqlite_batch_fill_path_query;
190
191
192 #else
193
194 /*                    S Q L I T E 3            */
195  
196
197 #ifdef HAVE_SQLITE3
198
199
200 #define BDB_VERSION 10
201
202 #include <sqlite3.h>
203
204 /* Define opaque structure for sqlite */
205 struct sqlite3 {
206    char dummy;
207 };
208
209 #define IS_NUM(x)             ((x) == 1)
210 #define IS_NOT_NULL(x)        ((x) == 1)
211
212 typedef struct s_sql_field {
213    char *name;                        /* name of column */
214    int length;                        /* length */
215    int max_length;                    /* max length */
216    uint32_t type;                     /* type */
217    uint32_t flags;                    /* flags */
218 } SQL_FIELD;
219
220 /*
221  * This is the "real" definition that should only be
222  * used inside sql.c and associated database interface
223  * subroutines.
224  *                    S Q L I T E
225  */
226 struct B_DB {
227    BQUEUE bq;                         /* queue control */
228    brwlock_t lock;                    /* transaction lock */
229    struct sqlite3 *db;
230    char **result;
231    int status;
232    int nrow;                          /* nrow returned from sqlite */
233    int ncolumn;                       /* ncolum returned from sqlite */
234    int num_rows;                      /* used by code */
235    int row;                           /* seek row */
236    int field;                         /* seek field */
237    SQL_FIELD **fields;                /* defined fields */
238    int ref_count;
239    char *db_name;
240    char *db_user;
241    char *db_address;                  /* host name address */
242    char *db_socket;                   /* socket for local access */
243    char *db_password;
244    int  db_port;                      /* port for host name address */
245    bool connected;                    /* connection made to db */
246    bool have_insert_id;               /* do not have insert id */
247    bool fields_defined;               /* set when fields defined */
248    char *sqlite_errmsg;               /* error message returned by sqlite */
249    POOLMEM *errmsg;                   /* nicely edited error message */
250    POOLMEM *cmd;                      /* SQL command string */
251    POOLMEM *cached_path;              /* cached path name */
252    int cached_path_len;               /* length of cached path */
253    uint32_t cached_path_id;           /* cached path id */
254    bool allow_transactions;           /* transactions allowed */
255    bool transaction;                  /* transaction started */
256    int changes;                       /* changes during transaction */
257    POOLMEM *fname;                    /* Filename only */
258    POOLMEM *path;                     /* Path only */
259    POOLMEM *esc_name;                 /* Escaped file name */
260    POOLMEM *esc_name2;                /* Escaped path name */
261    int fnl;                           /* file name length */
262    int pnl;                           /* path name length */
263 };
264
265 /*
266  * Conversion of sqlite 2 names to sqlite3
267  */
268 #define sqlite_last_insert_rowid sqlite3_last_insert_rowid
269 #define sqlite_open sqlite3_open
270 #define sqlite_close sqlite3_close
271 #define sqlite_result sqlite3_result
272 #define sqlite_exec sqlite3_exec
273 #define sqlite_get_table sqlite3_get_table
274 #define sqlite_free_table sqlite3_free_table
275
276
277 /*
278  * "Generic" names for easier conversion
279  *
280  *                    S Q L I T E 3
281  */
282 #define sql_store_result(x)   (x)->result
283 #define sql_free_result(x)    my_sqlite_free_table(x)
284 #define sql_fetch_row(x)      my_sqlite_fetch_row(x)
285 #define sql_query(x, y)       my_sqlite_query((x), (y))
286 #ifdef HAVE_SQLITE3
287 #define sql_insert_id(x,y)    sqlite3_last_insert_rowid((x)->db)
288 #define sql_close(x)          sqlite3_close((x)->db)
289 #else
290 #define sql_insert_id(x,y)    sqlite_last_insert_rowid((x)->db)
291 #define sql_close(x)          sqlite_close((x)->db)
292 #endif
293 #define sql_strerror(x)       (x)->sqlite_errmsg?(x)->sqlite_errmsg:"unknown"
294 #define sql_num_rows(x)       (x)->nrow
295 #define sql_data_seek(x, i)   (x)->row = (i)
296 #define sql_affected_rows(x)  1
297 #define sql_field_seek(x, y)  my_sqlite_field_seek((x), (y))
298 #define sql_fetch_field(x)    my_sqlite_fetch_field(x)
299 #define sql_num_fields(x)     ((x)->ncolumn)
300 #define SQL_ROW               char**
301 #define sql_batch_lock_path_query       my_sqlite_batch_lock_query
302 #define sql_batch_lock_filename_query   my_sqlite_batch_lock_query
303 #define sql_batch_unlock_tables_query   my_sqlite_batch_unlock_query
304 #define sql_batch_fill_filename_query   my_sqlite_batch_fill_filename_query
305 #define sql_batch_fill_path_query       my_sqlite_batch_fill_path_query
306
307 /* In cats/sqlite.c */
308 void       my_sqlite_free_table(B_DB *mdb);
309 SQL_ROW    my_sqlite_fetch_row(B_DB *mdb);
310 int        my_sqlite_query(B_DB *mdb, const char *cmd);
311 void       my_sqlite_field_seek(B_DB *mdb, int field);
312 SQL_FIELD *my_sqlite_fetch_field(B_DB *mdb);
313 extern char* my_sqlite_batch_lock_query;
314 extern char* my_sqlite_batch_unlock_query;
315 extern char* my_sqlite_batch_fill_filename_query;
316 extern char* my_sqlite_batch_fill_path_query;
317
318
319 #else
320
321 #ifdef HAVE_MYSQL
322
323 #define BDB_VERSION 10
324
325 #include <mysql.h>
326
327 /*
328  * This is the "real" definition that should only be
329  * used inside sql.c and associated database interface
330  * subroutines.
331  *
332  *                     M Y S Q L
333  */
334 struct B_DB {
335    BQUEUE bq;                         /* queue control */
336    brwlock_t lock;                    /* transaction lock */
337    MYSQL mysql;
338    MYSQL *db;
339    MYSQL_RES *result;
340    int status;
341    my_ulonglong num_rows;
342    int ref_count;
343    char *db_name;
344    char *db_user;
345    char *db_password;
346    char *db_address;                  /* host address */
347    char *db_socket;                   /* socket for local access */
348    int db_port;                       /* port of host address */
349    int have_insert_id;                /* do have insert_id() */
350    bool connected;
351    POOLMEM *errmsg;                   /* nicely edited error message */
352    POOLMEM *cmd;                      /* SQL command string */
353    POOLMEM *cached_path;
354    int cached_path_len;               /* length of cached path */
355    uint32_t cached_path_id;
356    int changes;                       /* changes made to db */
357    POOLMEM *fname;                    /* Filename only */
358    POOLMEM *path;                     /* Path only */
359    POOLMEM *esc_name;                 /* Escaped file name */
360    POOLMEM *esc_name2;                /* Escaped path name */
361    int fnl;                           /* file name length */
362    int pnl;                           /* path name length */
363 };
364
365 #define DB_STATUS int
366
367 /* "Generic" names for easier conversion */
368 #define sql_store_result(x)   mysql_store_result((x)->db)
369 #define sql_use_result(x)     mysql_use_result((x)->db)
370 #define sql_free_result(x)    mysql_free_result((x)->result)
371 #define sql_fetch_row(x)      mysql_fetch_row((x)->result)
372 #define sql_query(x, y)       mysql_query((x)->db, (y))
373 #define sql_close(x)          mysql_close((x)->db)
374 #define sql_strerror(x)       mysql_error((x)->db)
375 #define sql_num_rows(x)       mysql_num_rows((x)->result)
376 #define sql_data_seek(x, i)   mysql_data_seek((x)->result, (i))
377 #define sql_affected_rows(x)  mysql_affected_rows((x)->db)
378 #define sql_insert_id(x,y)    mysql_insert_id((x)->db)
379 #define sql_field_seek(x, y)  mysql_field_seek((x)->result, (y))
380 #define sql_fetch_field(x)    mysql_fetch_field((x)->result)
381 #define sql_num_fields(x)     (int)mysql_num_fields((x)->result)
382 #define sql_batch_start(x)    db_batch_start(x)
383 #define sql_batch_lock_filename_query   my_mysql_batch_lock_filename_query
384 #define sql_batch_unlock_tables_query   my_mysql_batch_unlock_tables_query
385 #define sql_batch_fill_filename_query   my_mysql_batch_fill_filename_query
386 #define sql_batch_fill_path_query       my_mysql_batch_fill_path_query
387 #define SQL_ROW               MYSQL_ROW
388 #define SQL_FIELD             MYSQL_FIELD
389
390
391 int my_mysql_batch_start(B_DB *mdb);
392 extern char* my_mysql_batch_lock_path_query;
393 extern char* my_mysql_batch_lock_filename_query;
394 extern char* my_mysql_batch_unlock_tables_query;
395 extern char* my_mysql_batch_fill_filename_query;
396 extern char* my_mysql_batch_fill_path_query;
397
398 #else
399
400 #ifdef HAVE_POSTGRESQL
401
402 #define BDB_VERSION 10
403
404 #include <libpq-fe.h>
405
406 /* TEMP: the following is taken from select OID, typname from pg_type; */
407 #define IS_NUM(x)        ((x) == 20 || (x) == 21 || (x) == 23 || (x) == 700 || (x) == 701)
408 #define IS_NOT_NULL(x)   ((x) == 1)
409
410 typedef char **POSTGRESQL_ROW;
411 typedef struct pg_field {
412    char         *name;
413    int           max_length;
414    unsigned int  type;
415    unsigned int  flags;       // 1 == not null
416 } POSTGRESQL_FIELD;
417
418
419 /*
420  * This is the "real" definition that should only be
421  * used inside sql.c and associated database interface
422  * subroutines.
423  *
424  *                     P O S T G R E S Q L
425  */
426 struct B_DB {
427    BQUEUE bq;                         /* queue control */
428    brwlock_t lock;                    /* transaction lock */
429    PGconn *db;
430    PGresult *result;
431    int status;
432    POSTGRESQL_ROW row;
433    POSTGRESQL_FIELD *fields;
434    int num_rows;
435    int num_fields;
436    int row_number;            /* what row number did we get via my_postgresql_data_seek? */
437    int field_number;          /* what field number did we get via my_postgresql_field_seek? */
438    int ref_count;
439    char *db_name;
440    char *db_user;
441    char *db_password;
442    char *db_address;              /* host address */
443    char *db_socket;               /* socket for local access */
444    int db_port;                   /* port of host address */
445    int have_insert_id;            /* do have insert_id() */
446    bool connected;
447    POOLMEM *errmsg;               /* nicely edited error message */
448    POOLMEM *cmd;                  /* SQL command string */
449    POOLMEM *cached_path;
450    int cached_path_len;           /* length of cached path */
451    uint32_t cached_path_id;
452    bool allow_transactions;       /* transactions allowed */
453    bool transaction;              /* transaction started */
454    int changes;                   /* changes made to db */
455    POOLMEM *fname;                /* Filename only */
456    POOLMEM *path;                 /* Path only */
457    POOLMEM *esc_name;             /* Escaped file name */
458    POOLMEM *esc_name2;            /* Escaped path name */
459    int fnl;                       /* file name length */
460    int pnl;                       /* path name length */
461 };     
462
463 void               my_postgresql_free_result(B_DB *mdb);
464 POSTGRESQL_ROW     my_postgresql_fetch_row  (B_DB *mdb);
465 int                my_postgresql_query      (B_DB *mdb, const char *query);
466 void               my_postgresql_data_seek  (B_DB *mdb, int row);
467 int                my_postgresql_currval    (B_DB *mdb, char *table_name);
468 void               my_postgresql_field_seek (B_DB *mdb, int row);
469 POSTGRESQL_FIELD * my_postgresql_fetch_field(B_DB *mdb);
470 int my_postgresql_lock_table(B_DB *mdb, const char *table);
471 int my_postgresql_unlock_table(B_DB *mdb);
472 int my_postgresql_batch_start(B_DB *mdb);
473 int my_postgresql_batch_end(B_DB *mdb, const char *error);
474 typedef struct ATTR_DBR ATTR_DBR;
475 int my_postgresql_batch_insert(B_DB *mdb, ATTR_DBR *ar);
476 char *my_postgresql_copy_escape(char *dest, char *src, size_t len);
477
478 extern char* my_pg_batch_lock_path_query;
479 extern char* my_pg_batch_lock_filename_query;
480 extern char* my_pg_batch_unlock_tables_query;
481 extern char* my_pg_batch_fill_filename_query;
482 extern char* my_pg_batch_fill_path_query;
483
484 /* "Generic" names for easier conversion */
485 #define sql_store_result(x)   ((x)->result)
486 #define sql_free_result(x)    my_postgresql_free_result(x)
487 #define sql_fetch_row(x)      my_postgresql_fetch_row(x)
488 #define sql_query(x, y)       my_postgresql_query((x), (y))
489 #define sql_close(x)          PQfinish((x)->db)
490 #define sql_strerror(x)       PQresultErrorMessage((x)->result)
491 #define sql_num_rows(x)       ((unsigned) PQntuples((x)->result))
492 #define sql_data_seek(x, i)   my_postgresql_data_seek((x), (i))
493 #define sql_affected_rows(x)  ((unsigned) atoi(PQcmdTuples((x)->result)))
494 #define sql_insert_id(x,y)    my_postgresql_currval((x), (y))
495 #define sql_field_seek(x, y)  my_postgresql_field_seek((x), (y))
496 #define sql_fetch_field(x)    my_postgresql_fetch_field(x)
497 #define sql_num_fields(x)     ((x)->num_fields)
498 #define sql_lock_table(x,y)   my_postgresql_lock_table(x, y)
499 #define sql_unlock_table(x,y) my_postgresql_unlock_table(x)
500 #define sql_batch_lock_path_query       my_pg_batch_lock_path_query
501 #define sql_batch_lock_filename_query   my_pg_batch_lock_filename_query
502 #define sql_batch_unlock_tables_query   my_pg_batch_unlock_tables_query
503 #define sql_batch_fill_filename_query   my_pg_batch_fill_filename_query
504 #define sql_batch_fill_path_query       my_pg_batch_fill_path_query
505
506 #define SQL_ROW               POSTGRESQL_ROW
507 #define SQL_FIELD             POSTGRESQL_FIELD
508
509 #else  /* USE BACULA DB routines */
510
511 #define HAVE_BACULA_DB 1
512
513 /* Change this each time there is some incompatible
514  * file format change!!!!
515  */
516 #define BDB_VERSION 13                /* file version number */
517
518 struct s_control {
519    int bdb_version;                   /* Version number */
520    uint32_t JobId;                    /* next Job Id */
521    uint32_t PoolId;                   /* next Pool Id */
522    uint32_t MediaId;                  /* next Media Id */
523    uint32_t JobMediaId;               /* next JobMedia Id */
524    uint32_t ClientId;                 /* next Client Id */
525    uint32_t FileSetId;                /* nest FileSet Id */
526    time_t time;                       /* time file written */
527 };
528
529
530 /* This is the REAL definition for using the
531  *  Bacula internal DB
532  */
533 struct B_DB {
534    BQUEUE bq;                         /* queue control */
535 /* pthread_mutex_t mutex;  */         /* single thread lock */
536    brwlock_t lock;                    /* transaction lock */
537    int ref_count;                     /* number of times opened */
538    struct s_control control;          /* control file structure */
539    int cfd;                           /* control file device */
540    FILE *jobfd;                       /* Jobs records file descriptor */
541    FILE *poolfd;                      /* Pool records fd */
542    FILE *mediafd;                     /* Media records fd */
543    FILE *jobmediafd;                  /* JobMedia records fd */
544    FILE *clientfd;                    /* Client records fd */
545    FILE *filesetfd;                   /* FileSet records fd */
546    char *db_name;                     /* name of database */
547    POOLMEM *errmsg;                   /* nicely edited error message */
548    POOLMEM *cmd;                      /* Command string */
549    POOLMEM *cached_path;
550    int cached_path_len;               /* length of cached path */
551    uint32_t cached_path_id;
552 };
553
554 #endif /* HAVE_SQLITE3 */
555 #endif /* HAVE_MYSQL */
556 #endif /* HAVE_SQLITE */
557 #endif /* HAVE_POSTGRESQL */
558 #endif
559
560 /* Use for better error location printing */
561 #define UPDATE_DB(jcr, db, cmd) UpdateDB(__FILE__, __LINE__, jcr, db, cmd)
562 #define INSERT_DB(jcr, db, cmd) InsertDB(__FILE__, __LINE__, jcr, db, cmd)
563 #define QUERY_DB(jcr, db, cmd) QueryDB(__FILE__, __LINE__, jcr, db, cmd)
564 #define DELETE_DB(jcr, db, cmd) DeleteDB(__FILE__, __LINE__, jcr, db, cmd)
565
566
567 #else    /* not __SQL_C */
568
569 /* This is a "dummy" definition for use outside of sql.c
570  */
571 struct B_DB {
572    int dummy;                         /* for SunOS compiler */
573 };     
574
575 #endif /*  __SQL_C */
576
577 extern uint32_t bacula_db_version;
578
579 /* ***FIXME*** FileId_t should *really* be uint64_t
580  *  but at the current time, this breaks MySQL.
581  */
582 typedef uint32_t FileId_t;
583 typedef uint32_t DBId_t;              /* general DB id type */
584 typedef uint32_t JobId_t;
585
586 #define faddr_t long
587
588
589 /* Job information passed to create job record and update
590  * job record at end of job. Note, although this record
591  * contains all the fields found in the Job database record,
592  * it also contains fields found in the JobMedia record.
593  */
594 /* Job record */
595 struct JOB_DBR {
596    JobId_t JobId;
597    char Job[MAX_NAME_LENGTH];         /* Job unique name */
598    char Name[MAX_NAME_LENGTH];        /* Job base name */
599    int JobType;                       /* actually char(1) */
600    int JobLevel;                      /* actually char(1) */
601    int JobStatus;                     /* actually char(1) */
602    DBId_t ClientId;                   /* Id of client */
603    DBId_t PoolId;                     /* Id of pool */
604    DBId_t FileSetId;                  /* Id of FileSet */
605    DBId_t PriorJobId;                 /* Id of migrated (prior) job */
606    time_t SchedTime;                  /* Time job scheduled */
607    time_t StartTime;                  /* Job start time */
608    time_t EndTime;                    /* Job termination time of orig job */
609    time_t RealEndTime;                /* Job termination time of this job */
610    utime_t JobTDate;                  /* Backup time/date in seconds */
611    uint32_t VolSessionId;
612    uint32_t VolSessionTime;
613    uint32_t JobFiles;
614    uint32_t JobErrors;
615    uint32_t JobMissingFiles;
616    uint64_t JobBytes;
617    int PurgedFiles;
618    int HasBase;
619
620    /* Note, FirstIndex, LastIndex, Start/End File and Block
621     * are only used in the JobMedia record.
622     */
623    uint32_t FirstIndex;               /* First index this Volume */
624    uint32_t LastIndex;                /* Last index this Volume */
625    uint32_t StartFile;
626    uint32_t EndFile;
627    uint32_t StartBlock;
628    uint32_t EndBlock;
629
630    char cSchedTime[MAX_TIME_LENGTH];
631    char cStartTime[MAX_TIME_LENGTH];
632    char cEndTime[MAX_TIME_LENGTH];
633    char cRealEndTime[MAX_TIME_LENGTH];
634    /* Extra stuff not in DB */
635    int limit;                         /* limit records to display */
636    faddr_t rec_addr;
637 };
638
639 /* Job Media information used to create the media records
640  * for each Volume used for the job.
641  */
642 /* JobMedia record */
643 struct JOBMEDIA_DBR {
644    DBId_t JobMediaId;                 /* record id */
645    JobId_t  JobId;                    /* JobId */
646    DBId_t MediaId;                    /* MediaId */
647    uint32_t FirstIndex;               /* First index this Volume */
648    uint32_t LastIndex;                /* Last index this Volume */
649    uint32_t StartFile;                /* File for start of data */
650    uint32_t EndFile;                  /* End file on Volume */
651    uint32_t StartBlock;               /* start block on tape */
652    uint32_t EndBlock;                 /* last block */
653    uint32_t Copy;                     /* identical copy */
654 };
655
656
657 /* Volume Parameter structure */
658 struct VOL_PARAMS {
659    char VolumeName[MAX_NAME_LENGTH];  /* Volume name */
660    char MediaType[MAX_NAME_LENGTH];   /* Media Type */
661    char Storage[MAX_NAME_LENGTH];     /* Storage name */
662    uint32_t VolIndex;                 /* Volume seqence no. */
663    uint32_t FirstIndex;               /* First index this Volume */
664    uint32_t LastIndex;                /* Last index this Volume */
665    uint32_t StartFile;                /* File for start of data */
666    uint32_t EndFile;                  /* End file on Volume */
667    uint32_t StartBlock;               /* start block on tape */
668    uint32_t EndBlock;                 /* last block */
669    int32_t Slot;                      /* Slot */
670 // uint32_t Copy;                     /* identical copy */
671 // uint32_t Stripe;                   /* RAIT strip number */
672 };
673
674
675 /* Attributes record -- NOT same as in database because
676  *  in general, this "record" creates multiple database
677  *  records (e.g. pathname, filename, fileattributes).
678  */
679 struct ATTR_DBR {
680    char *fname;                       /* full path & filename */
681    char *link;                        /* link if any */
682    char *attr;                        /* attributes statp */
683    uint32_t FileIndex;
684    uint32_t Stream;
685    JobId_t  JobId;
686    DBId_t ClientId;
687    DBId_t PathId;
688    DBId_t FilenameId;
689    FileId_t FileId;
690    char *Digest;
691    int DigestType;
692 };
693
694
695 /* File record -- same format as database */
696 struct FILE_DBR {
697    FileId_t FileId;
698    uint32_t FileIndex;
699    JobId_t  JobId;
700    DBId_t FilenameId;
701    DBId_t PathId;
702    JobId_t  MarkId;
703    char LStat[256];
704    char Digest[BASE64_SIZE(CRYPTO_DIGEST_MAX_SIZE)];
705    int DigestType;                    /* NO_SIG/MD5_SIG/SHA1_SIG */
706 };
707
708 /* Pool record -- same format as database */
709 struct POOL_DBR {
710    DBId_t PoolId;
711    char Name[MAX_NAME_LENGTH];        /* Pool name */
712    uint32_t NumVols;                  /* total number of volumes */
713    uint32_t MaxVols;                  /* max allowed volumes */
714    int32_t LabelType;                 /* Bacula/ANSI/IBM */
715    int32_t UseOnce;                   /* set to use once only */
716    int32_t UseCatalog;                /* set to use catalog */
717    int32_t AcceptAnyVolume;           /* set to accept any volume sequence */
718    int32_t AutoPrune;                 /* set to prune automatically */
719    int32_t Recycle;                   /* default Vol recycle flag */
720    utime_t  VolRetention;             /* retention period in seconds */
721    utime_t  VolUseDuration;           /* time in secs volume can be used */
722    uint32_t MaxVolJobs;               /* Max Jobs on Volume */
723    uint32_t MaxVolFiles;              /* Max files on Volume */
724    uint64_t MaxVolBytes;              /* Max bytes on Volume */
725    DBId_t RecyclePoolId;              /* RecyclePool destination when media is purged */
726    char PoolType[MAX_NAME_LENGTH];
727    char LabelFormat[MAX_NAME_LENGTH];
728    /* Extra stuff not in DB */
729    faddr_t rec_addr;
730 };
731
732 class DEVICE_DBR {
733 public:
734    DBId_t DeviceId;
735    char Name[MAX_NAME_LENGTH];        /* Device name */
736    DBId_t MediaTypeId;                /* MediaType */
737    DBId_t StorageId;                  /* Storage id if autochanger */
738    uint32_t DevMounts;                /* Number of times mounted */
739    uint32_t DevErrors;                /* Number of read/write errors */
740    uint64_t DevReadBytes;             /* Number of bytes read */
741    uint64_t DevWriteBytes;            /* Number of bytew written */
742    uint64_t DevReadTime;              /* time spent reading volume */
743    uint64_t DevWriteTime;             /* time spent writing volume */
744    uint64_t DevReadTimeSincCleaning;  /* read time since cleaning */
745    uint64_t DevWriteTimeSincCleaning; /* write time since cleaning */
746    time_t   CleaningDate;             /* time last cleaned */
747    utime_t  CleaningPeriod;           /* time between cleanings */
748 };
749
750 class STORAGE_DBR {
751 public:
752    DBId_t StorageId;
753    char Name[MAX_NAME_LENGTH];        /* Device name */
754    int AutoChanger;                   /* Set if autochanger */
755
756    /* Not in database */
757    bool created;                      /* set if created by db_create ... */
758 };
759
760 class MEDIATYPE_DBR {
761 public:
762    DBId_t MediaTypeId;
763    char MediaType[MAX_NAME_LENGTH];   /* MediaType string */
764    int ReadOnly;                      /* Set if read-only */
765 };
766
767
768 /* Media record -- same as the database */
769 struct MEDIA_DBR {
770    DBId_t MediaId;                    /* Unique volume id */
771    char VolumeName[MAX_NAME_LENGTH];  /* Volume name */
772    char MediaType[MAX_NAME_LENGTH];   /* Media type */
773    DBId_t PoolId;                     /* Pool id */
774    time_t   FirstWritten;             /* Time Volume first written this usage */
775    time_t   LastWritten;              /* Time Volume last written */
776    time_t   LabelDate;                /* Date/Time Volume labeled */
777    time_t   InitialWrite;             /* Date/Time Volume first written */
778    int32_t  LabelType;                /* Label (Bacula/ANSI/IBM) */
779    uint32_t VolJobs;                  /* number of jobs on this medium */
780    uint32_t VolFiles;                 /* Number of files */
781    uint32_t VolBlocks;                /* Number of blocks */
782    uint32_t VolMounts;                /* Number of times mounted */
783    uint32_t VolErrors;                /* Number of read/write errors */
784    uint32_t VolWrites;                /* Number of writes */
785    uint32_t VolReads;                 /* Number of reads */
786    uint64_t VolBytes;                 /* Number of bytes written */
787    uint32_t VolParts;                 /* Number of parts written */
788    uint64_t MaxVolBytes;              /* Max bytes to write to Volume */
789    uint64_t VolCapacityBytes;         /* capacity estimate */
790    uint64_t VolReadTime;              /* time spent reading volume */
791    uint64_t VolWriteTime;             /* time spent writing volume */
792    utime_t  VolRetention;             /* Volume retention in seconds */
793    utime_t  VolUseDuration;           /* time in secs volume can be used */
794    uint32_t MaxVolJobs;               /* Max Jobs on Volume */
795    uint32_t MaxVolFiles;              /* Max files on Volume */
796    int32_t  Recycle;                  /* recycle yes/no */
797    int32_t  Slot;                     /* slot in changer */
798    int32_t  Enabled;                  /* 0=disabled, 1=enabled, 2=archived */
799    int32_t  InChanger;                /* Volume currently in changer */
800    DBId_t   StorageId;                /* Storage record Id */
801    uint32_t EndFile;                  /* Last file on volume */
802    uint32_t EndBlock;                 /* Last block on volume */
803    uint32_t RecycleCount;             /* Number of times recycled */
804    char     VolStatus[20];            /* Volume status */
805    DBId_t   DeviceId;                 /* Device where Vol last written */
806    DBId_t   LocationId;               /* Where Volume is -- user defined */
807    DBId_t   ScratchPoolId;            /* Where to move if scratch */
808    DBId_t   RecyclePoolId;            /* Where to move when recycled */
809    /* Extra stuff not in DB */
810    faddr_t rec_addr;                  /* found record address */
811    /* Since the database returns times as strings, this is how we pass
812     *   them back.
813     */
814    char    cFirstWritten[MAX_TIME_LENGTH]; /* FirstWritten returned from DB */
815    char    cLastWritten[MAX_TIME_LENGTH];  /* LastWritten returned from DB */
816    char    cLabelDate[MAX_TIME_LENGTH];    /* LabelData returned from DB */
817    char    cInitialWrite[MAX_TIME_LENGTH]; /* InitialWrite returned from DB */
818    bool    set_first_written;                
819    bool    set_label_date;
820 };
821
822 /* Client record -- same as the database */
823 struct CLIENT_DBR {
824    DBId_t ClientId;                   /* Unique Client id */
825    int AutoPrune;
826    utime_t FileRetention;
827    utime_t JobRetention;
828    char Name[MAX_NAME_LENGTH];        /* Client name */
829    char Uname[256];                   /* Uname for client */
830 };
831
832 /* Counter record as in database */
833 struct COUNTER_DBR {
834    char Counter[MAX_NAME_LENGTH];
835    int32_t MinValue;
836    int32_t MaxValue;
837    int32_t CurrentValue;
838    char WrapCounter[MAX_NAME_LENGTH];
839 };
840
841
842 /* FileSet record -- same as the database */
843 struct FILESET_DBR {
844    DBId_t FileSetId;                  /* Unique FileSet id */
845    char FileSet[MAX_NAME_LENGTH];     /* FileSet name */
846    char MD5[50];                      /* MD5 signature of include/exclude */
847    time_t CreateTime;                 /* date created */
848    /*
849     * This is where we return CreateTime
850     */
851    char cCreateTime[MAX_TIME_LENGTH]; /* CreateTime as returned from DB */
852    /* Not in DB but returned by db_create_fileset() */
853    bool created;                      /* set when record newly created */
854 };
855
856 /* Call back context for getting a 32/64 bit value from the database */
857 struct db_int64_ctx {
858    int64_t value;                     /* value returned */
859    int count;                         /* number of values seen */
860 };
861
862
863 #include "protos.h"
864 #include "jcr.h"
865 #include "sql_cmds.h"
866
867 /*
868  * Some functions exported by sql.c for use within the
869  *   cats directory.
870  */
871 void list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type type);
872 void list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx);
873 int get_sql_record_max(JCR *jcr, B_DB *mdb);
874 bool check_tables_version(JCR *jcr, B_DB *mdb);
875 void _db_unlock(const char *file, int line, B_DB *mdb);
876 void _db_lock(const char *file, int line, B_DB *mdb);
877 const char *db_get_type(void);
878
879 void print_dashes(B_DB *mdb);
880 void print_result(B_DB *mdb);
881 int QueryDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
882 int InsertDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
883 int DeleteDB(const char *file, int line, JCR *jcr, B_DB *db, char *delete_cmd);
884 int UpdateDB(const char *file, int line, JCR *jcr, B_DB *db, char *update_cmd);
885 void split_path_and_file(JCR *jcr, B_DB *mdb, const char *fname);
886 #endif /* __SQL_H_ */