]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/cats.h
Fix typo
[bacula/bacula] / bacula / src / cats / cats.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2009 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 and included
11    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 Kern Sibbald.
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  */
42
43 /*
44    Here is how database versions work.
45
46    While I am working on a new release with database changes, the
47    update scripts are in the src/cats directory under the names
48    update_xxx_tables.in.  Most of the time, I make database updates
49    in one go and immediately update the version, but not always.  If
50    there are going to be several updates as is the case with version
51    1.37, then I will often forgo changing the version until the last
52    update otherwise I will end up with too many versions and a lot
53    of confusion.
54
55    When I am pretty sure there will be no more updates, I will
56    change the version from 8 to 9 (in the present case), and when I
57    am 100% sure there will be no more changes, the update script
58    will be copied to the updatedb directory with the correct name
59    (in the present case 8 to 9).
60
61    Now, in principle, each of the different DB implementations
62    can have a different version, but in practice they are all
63    the same (simplifies things). The exception is the internal
64    database, which is no longer used, and hence, no longer changes.
65  */
66
67
68 #ifndef __SQL_H_
69 #define __SQL_H_ 1
70
71 enum {
72    SQL_TYPE_MYSQL      = 0,
73    SQL_TYPE_POSTGRESQL = 1,
74    SQL_TYPE_SQLITE     = 2,
75    SQL_TYPE_SQLITE3    = 3,
76    SQL_TYPE_INGRES     = 4
77 };
78
79
80 typedef void (DB_LIST_HANDLER)(void *, const char *);
81 typedef int (DB_RESULT_HANDLER)(void *, int, char **);
82
83 #define db_lock(mdb)   _db_lock(__FILE__, __LINE__, mdb)
84 #define db_unlock(mdb) _db_unlock(__FILE__, __LINE__, mdb)
85
86 #ifdef __SQL_C
87
88 #if defined(BUILDING_CATS)
89 #ifdef HAVE_SQLITE
90 #error "SQLite2 is now deprecated, use SQLite3 instead."
91
92 #define BDB_VERSION 12
93
94 #include <sqlite.h>
95
96 /* Define opaque structure for sqlite */
97 struct sqlite {
98    char dummy;
99 };
100
101 #define IS_NUM(x)             ((x) == 1)
102 #define IS_NOT_NULL(x)        ((x) == 1)
103
104 typedef struct s_sql_field {
105    char *name;                        /* name of column */
106    int length;                        /* length */
107    int max_length;                    /* max length */
108    uint32_t type;                     /* type */
109    uint32_t flags;                    /* flags */
110 } SQL_FIELD;
111
112 /*
113  * This is the "real" definition that should only be
114  * used inside sql.c and associated database interface
115  * subroutines.
116  *                    S Q L I T E
117  */
118 struct B_DB {
119    BQUEUE bq;                         /* queue control */
120    brwlock_t lock;                    /* transaction lock */
121    struct sqlite *db;
122    char **result;
123    int status;
124    int nrow;                          /* nrow returned from sqlite */
125    int ncolumn;                       /* ncolum returned from sqlite */
126    int num_rows;                      /* used by code */
127    int row;                           /* seek row */
128    int field;                         /* seek field */
129    SQL_FIELD **fields;                /* defined fields */
130    int ref_count;
131    char *db_name;
132    char *db_user;
133    char *db_address;                  /* host name address */
134    char *db_socket;                   /* socket for local access */
135    char *db_password;
136    int  db_port;                      /* port for host name address */
137    bool connected;                    /* connection made to db */
138    bool have_insert_id;               /* do not have insert id */
139    bool fields_defined;               /* set when fields defined */
140    char *sqlite_errmsg;               /* error message returned by sqlite */
141    POOLMEM *errmsg;                   /* nicely edited error message */
142    POOLMEM *cmd;                      /* SQL command string */
143    POOLMEM *cached_path;              /* cached path name */
144    int cached_path_len;               /* length of cached path */
145    uint32_t cached_path_id;           /* cached path id */
146    bool allow_transactions;           /* transactions allowed */
147    bool transaction;                  /* transaction started */
148    int changes;                       /* changes during transaction */
149    POOLMEM *fname;                    /* Filename only */
150    POOLMEM *path;                     /* Path only */
151    POOLMEM *esc_name;                 /* Escaped file name */
152    POOLMEM *esc_path;                 /* Escaped path name */
153    int fnl;                           /* file name length */
154    int pnl;                           /* path name length */
155 };
156
157
158 /*
159  * "Generic" names for easier conversion
160  *
161  *                    S Q L I T E
162  */
163 #define sql_store_result(x)   (x)->result
164 #define sql_free_result(x)    my_sqlite_free_table(x)
165 #define sql_fetch_row(x)      my_sqlite_fetch_row(x)
166 #define sql_query(x, y)       my_sqlite_query((x), (y))
167 #ifdef HAVE_SQLITE3
168 #define sql_insert_id(x,y)    sqlite3_last_insert_rowid((x)->db)
169 #define sql_close(x)          sqlite3_close((x)->db)
170 #define sql_affected_rows(x)  sqlite3_changes((x)->db)
171 #else
172 #define sql_insert_id(x,y)    sqlite_last_insert_rowid((x)->db)
173 #define sql_close(x)          sqlite_close((x)->db)
174 #define sql_affected_rows(x)  1
175 #endif
176 #define sql_strerror(x)       (x)->sqlite_errmsg?(x)->sqlite_errmsg:"unknown"
177 #define sql_num_rows(x)       (x)->nrow
178 #define sql_data_seek(x, i)   (x)->row = (i)
179 #define sql_field_seek(x, y)  my_sqlite_field_seek((x), (y))
180 #define sql_fetch_field(x)    my_sqlite_fetch_field(x)
181 #define sql_num_fields(x)     ((x)->ncolumn)
182 #define SQL_ROW               char**
183 #define SQL_MATCH             "MATCH"
184
185 #define sql_batch_start(x,y)    my_batch_start(x,y)
186 #define sql_batch_end(x,y,z)    my_batch_end(x,y,z)
187 #define sql_batch_insert(x,y,z) my_batch_insert(x,y,z)
188 #define sql_batch_lock_path_query       my_sqlite_batch_lock_query
189 #define sql_batch_lock_filename_query   my_sqlite_batch_lock_query
190 #define sql_batch_unlock_tables_query   my_sqlite_batch_unlock_query
191 #define sql_batch_fill_filename_query   my_sqlite_batch_fill_filename_query
192 #define sql_batch_fill_path_query       my_sqlite_batch_fill_path_query
193
194 /* In cats/sqlite.c */
195 void       my_sqlite_free_table(B_DB *mdb);
196 SQL_ROW    my_sqlite_fetch_row(B_DB *mdb);
197 int        my_sqlite_query(B_DB *mdb, const char *cmd);
198 void       my_sqlite_field_seek(B_DB *mdb, int field);
199 SQL_FIELD *my_sqlite_fetch_field(B_DB *mdb);
200 extern const char* my_sqlite_batch_lock_query;
201 extern const char* my_sqlite_batch_unlock_query;
202 extern const char* my_sqlite_batch_fill_filename_query;
203 extern const char* my_sqlite_batch_fill_path_query;
204
205
206 #else
207
208 /*                    S Q L I T E 3            */
209
210
211 #ifdef HAVE_SQLITE3
212
213
214 #define BDB_VERSION 12
215
216 #include <sqlite3.h>
217
218 /* Define opaque structure for sqlite */
219 struct sqlite3 {
220    char dummy;
221 };
222
223 #define IS_NUM(x)             ((x) == 1)
224 #define IS_NOT_NULL(x)        ((x) == 1)
225
226 typedef struct s_sql_field {
227    char *name;                        /* name of column */
228    int length;                        /* length */
229    int max_length;                    /* max length */
230    uint32_t type;                     /* type */
231    uint32_t flags;                    /* flags */
232 } SQL_FIELD;
233
234 /*
235  * This is the "real" definition that should only be
236  * used inside sql.c and associated database interface
237  * subroutines.
238  *                    S Q L I T E
239  */
240 struct B_DB {
241    BQUEUE bq;                         /* queue control */
242    brwlock_t lock;                    /* transaction lock */
243    struct sqlite3 *db;
244    char **result;
245    int status;
246    int nrow;                          /* nrow returned from sqlite */
247    int ncolumn;                       /* ncolum returned from sqlite */
248    int num_rows;                      /* used by code */
249    int row;                           /* seek row */
250    int field;                         /* seek field */
251    SQL_FIELD **fields;                /* defined fields */
252    int ref_count;
253    char *db_name;
254    char *db_user;
255    char *db_address;                  /* host name address */
256    char *db_socket;                   /* socket for local access */
257    char *db_password;
258    int  db_port;                      /* port for host name address */
259    bool connected;                    /* connection made to db */
260    bool have_insert_id;               /* do not have insert id */
261    bool fields_defined;               /* set when fields defined */
262    char *sqlite_errmsg;               /* error message returned by sqlite */
263    POOLMEM *errmsg;                   /* nicely edited error message */
264    POOLMEM *cmd;                      /* SQL command string */
265    POOLMEM *cached_path;              /* cached path name */
266    int cached_path_len;               /* length of cached path */
267    uint32_t cached_path_id;           /* cached path id */
268    bool allow_transactions;           /* transactions allowed */
269    bool transaction;                  /* transaction started */
270    int changes;                       /* changes during transaction */
271    POOLMEM *fname;                    /* Filename only */
272    POOLMEM *path;                     /* Path only */
273    POOLMEM *esc_name;                 /* Escaped file name */
274    POOLMEM *esc_path;                 /* Escaped path name */
275    int fnl;                           /* file name length */
276    int pnl;                           /* path name length */
277 };
278
279 /*
280  * Conversion of sqlite 2 names to sqlite3
281  */
282 #define sqlite_last_insert_rowid sqlite3_last_insert_rowid
283 #define sqlite_open sqlite3_open
284 #define sqlite_close sqlite3_close
285 #define sqlite_result sqlite3_result
286 #define sqlite_exec sqlite3_exec
287 #define sqlite_get_table sqlite3_get_table
288 #define sqlite_free_table sqlite3_free_table
289
290
291 /*
292  * "Generic" names for easier conversion
293  *
294  *                    S Q L I T E 3
295  */
296 #define sql_store_result(x)   (x)->result
297 #define sql_free_result(x)    my_sqlite_free_table(x)
298 #define sql_fetch_row(x)      my_sqlite_fetch_row(x)
299 #define sql_query(x, y)       my_sqlite_query((x), (y))
300 #ifdef HAVE_SQLITE3
301 #define sql_insert_id(x,y)    sqlite3_last_insert_rowid((x)->db)
302 #define sql_close(x)          sqlite3_close((x)->db)
303 #else
304 #define sql_insert_id(x,y)    sqlite_last_insert_rowid((x)->db)
305 #define sql_close(x)          sqlite_close((x)->db)
306 #endif
307 #define sql_strerror(x)       (x)->sqlite_errmsg?(x)->sqlite_errmsg:"unknown"
308 #define sql_num_rows(x)       (x)->nrow
309 #define sql_data_seek(x, i)   (x)->row = (i)
310 #define sql_affected_rows(x)  sqlite3_changes((x)->db)
311 #define sql_field_seek(x, y)  my_sqlite_field_seek((x), (y))
312 #define sql_fetch_field(x)    my_sqlite_fetch_field(x)
313 #define sql_num_fields(x)     ((x)->ncolumn)
314 #define sql_batch_start(x,y)    my_batch_start(x,y)
315 #define sql_batch_end(x,y,z)    my_batch_end(x,y,z)
316 #define sql_batch_insert(x,y,z) my_batch_insert(x,y,z)
317 #define SQL_ROW               char**
318 #define SQL_MATCH             "MATCH"
319 #define sql_batch_lock_path_query       my_sqlite_batch_lock_query
320 #define sql_batch_lock_filename_query   my_sqlite_batch_lock_query
321 #define sql_batch_unlock_tables_query   my_sqlite_batch_unlock_query
322 #define sql_batch_fill_filename_query   my_sqlite_batch_fill_filename_query
323 #define sql_batch_fill_path_query       my_sqlite_batch_fill_path_query
324
325 /* In cats/sqlite.c */
326 void       my_sqlite_free_table(B_DB *mdb);
327 SQL_ROW    my_sqlite_fetch_row(B_DB *mdb);
328 int        my_sqlite_query(B_DB *mdb, const char *cmd);
329 void       my_sqlite_field_seek(B_DB *mdb, int field);
330 SQL_FIELD *my_sqlite_fetch_field(B_DB *mdb);
331 extern const char* my_sqlite_batch_lock_query;
332 extern const char* my_sqlite_batch_unlock_query;
333 extern const char* my_sqlite_batch_fill_filename_query;
334 extern const char* my_sqlite_batch_fill_path_query;
335
336
337 #else
338
339 #ifdef HAVE_MYSQL
340
341 #define BDB_VERSION 12
342
343 #include <mysql.h>
344
345 /*
346  * This is the "real" definition that should only be
347  * used inside sql.c and associated database interface
348  * subroutines.
349  *
350  *                     M Y S Q L
351  */
352 struct B_DB {
353    BQUEUE bq;                         /* queue control */
354    brwlock_t lock;                    /* transaction lock */
355    MYSQL mysql;
356    MYSQL *db;
357    MYSQL_RES *result;
358    int status;
359    my_ulonglong num_rows;
360    int ref_count;
361    char *db_name;
362    char *db_user;
363    char *db_password;
364    char *db_address;                  /* host address */
365    char *db_socket;                   /* socket for local access */
366    int db_port;                       /* port of host address */
367    int have_insert_id;                /* do have insert_id() */
368    bool connected;
369    POOLMEM *errmsg;                   /* nicely edited error message */
370    POOLMEM *cmd;                      /* SQL command string */
371    POOLMEM *cached_path;
372    int cached_path_len;               /* length of cached path */
373    uint32_t cached_path_id;
374    bool allow_transactions;           /* transactions allowed */ 
375    int changes;                       /* changes made to db */
376    POOLMEM *fname;                    /* Filename only */
377    POOLMEM *path;                     /* Path only */
378    POOLMEM *esc_name;                 /* Escaped file name */
379    POOLMEM *esc_path;                 /* Escaped path name */
380    int fnl;                           /* file name length */
381    int pnl;                           /* path name length */
382 };
383
384 #define DB_STATUS int
385
386 /* "Generic" names for easier conversion */
387 #define sql_store_result(x)   mysql_store_result((x)->db)
388 #define sql_use_result(x)     mysql_use_result((x)->db)
389 #define sql_free_result(x)    my_mysql_free_result(x)
390 #define sql_fetch_row(x)      mysql_fetch_row((x)->result)
391 #define sql_query(x, y)       mysql_query((x)->db, (y))
392 #define sql_strerror(x)       mysql_error((x)->db)
393 #define sql_num_rows(x)       mysql_num_rows((x)->result)
394 #define sql_data_seek(x, i)   mysql_data_seek((x)->result, (i))
395 #define sql_affected_rows(x)  mysql_affected_rows((x)->db)
396 #define sql_insert_id(x,y)    mysql_insert_id((x)->db)
397 #define sql_field_seek(x, y)  mysql_field_seek((x)->result, (y))
398 #define sql_fetch_field(x)    mysql_fetch_field((x)->result)
399 #define sql_num_fields(x)     (int)mysql_num_fields((x)->result)
400 #define SQL_ROW               MYSQL_ROW
401 #define SQL_FIELD             MYSQL_FIELD
402 #define SQL_MATCH             "MATCH"
403
404 #define sql_batch_start(x,y)    my_batch_start(x,y)
405 #define sql_batch_end(x,y,z)    my_batch_end(x,y,z)
406 #define sql_batch_insert(x,y,z) my_batch_insert(x,y,z)
407 #define sql_batch_lock_path_query       my_mysql_batch_lock_path_query
408 #define sql_batch_lock_filename_query   my_mysql_batch_lock_filename_query
409 #define sql_batch_unlock_tables_query   my_mysql_batch_unlock_tables_query
410 #define sql_batch_fill_filename_query   my_mysql_batch_fill_filename_query
411 #define sql_batch_fill_path_query       my_mysql_batch_fill_path_query
412
413
414 extern const char* my_mysql_batch_lock_path_query;
415 extern const char* my_mysql_batch_lock_filename_query;
416 extern const char* my_mysql_batch_unlock_tables_query;
417 extern const char* my_mysql_batch_fill_filename_query;
418 extern const char* my_mysql_batch_fill_path_query;
419 extern void  my_mysql_free_result(B_DB *mdb);
420
421 #else
422
423 #ifdef HAVE_POSTGRESQL
424
425 #define BDB_VERSION 12
426
427 #include <libpq-fe.h>
428
429 /* TEMP: the following is taken from select OID, typname from pg_type; */
430 #define IS_NUM(x)        ((x) == 20 || (x) == 21 || (x) == 23 || (x) == 700 || (x) == 701)
431 #define IS_NOT_NULL(x)   ((x) == 1)
432
433 typedef char **POSTGRESQL_ROW;
434 typedef struct pg_field {
435    char         *name;
436    int           max_length;
437    unsigned int  type;
438    unsigned int  flags;       // 1 == not null
439 } POSTGRESQL_FIELD;
440
441
442 /*
443  * This is the "real" definition that should only be
444  * used inside sql.c and associated database interface
445  * subroutines.
446  *
447  *                     P O S T G R E S Q L
448  */
449 struct B_DB {
450    BQUEUE bq;                         /* queue control */
451    brwlock_t lock;                    /* transaction lock */
452    PGconn *db;
453    PGresult *result;
454    int status;
455    POSTGRESQL_ROW row;
456    POSTGRESQL_FIELD *fields;
457    int num_rows;
458    int row_size;                  /* size of malloced rows */
459    int num_fields;
460    int fields_size;               /* size of malloced fields */
461    int row_number;                /* row number from my_postgresql_data_seek */
462    int field_number;              /* field number from my_postgresql_field_seek */
463    int ref_count;
464    char *db_name;
465    char *db_user;
466    char *db_password;
467    char *db_address;              /* host address */
468    char *db_socket;               /* socket for local access */
469    int db_port;                   /* port of host address */
470    int have_insert_id;            /* do have insert_id() */
471    bool connected;
472    POOLMEM *errmsg;               /* nicely edited error message */
473    POOLMEM *cmd;                  /* SQL command string */
474    POOLMEM *cached_path;
475    int cached_path_len;           /* length of cached path */
476    uint32_t cached_path_id;
477    bool allow_transactions;       /* transactions allowed */
478    bool transaction;              /* transaction started */
479    int changes;                   /* changes made to db */
480    POOLMEM *fname;                /* Filename only */
481    POOLMEM *path;                 /* Path only */
482    POOLMEM *esc_name;             /* Escaped file name */
483    POOLMEM *esc_path;             /* Escaped path name */
484    int fnl;                       /* file name length */
485    int pnl;                       /* path name length */
486 };
487
488 void               my_postgresql_free_result(B_DB *mdb);
489 POSTGRESQL_ROW     my_postgresql_fetch_row  (B_DB *mdb);
490 int                my_postgresql_query      (B_DB *mdb, const char *query);
491 void               my_postgresql_data_seek  (B_DB *mdb, int row);
492 int                my_postgresql_currval    (B_DB *mdb, const char *table_name);
493 void               my_postgresql_field_seek (B_DB *mdb, int row);
494 POSTGRESQL_FIELD * my_postgresql_fetch_field(B_DB *mdb);
495
496 int my_postgresql_batch_start(JCR *jcr, B_DB *mdb);
497 int my_postgresql_batch_end(JCR *jcr, B_DB *mdb, const char *error);
498 typedef struct ATTR_DBR ATTR_DBR;
499 int my_postgresql_batch_insert(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
500 char *my_postgresql_copy_escape(char *dest, char *src, size_t len);
501
502 extern const char* my_pg_batch_lock_path_query;
503 extern const char* my_pg_batch_lock_filename_query;
504 extern const char* my_pg_batch_unlock_tables_query;
505 extern const char* my_pg_batch_fill_filename_query;
506 extern const char* my_pg_batch_fill_path_query;
507
508 /* "Generic" names for easier conversion */
509 #define sql_store_result(x)   ((x)->result)
510 #define sql_free_result(x)    my_postgresql_free_result(x)
511 #define sql_fetch_row(x)      my_postgresql_fetch_row(x)
512 #define sql_query(x, y)       my_postgresql_query((x), (y))
513 #define sql_close(x)          PQfinish((x)->db)
514 #define sql_strerror(x)       PQerrorMessage((x)->db)
515 #define sql_num_rows(x)       ((unsigned) PQntuples((x)->result))
516 #define sql_data_seek(x, i)   my_postgresql_data_seek((x), (i))
517 #define sql_affected_rows(x)  ((unsigned) atoi(PQcmdTuples((x)->result)))
518 #define sql_insert_id(x,y)    my_postgresql_currval((x), (y))
519 #define sql_field_seek(x, y)  my_postgresql_field_seek((x), (y))
520 #define sql_fetch_field(x)    my_postgresql_fetch_field(x)
521 #define sql_num_fields(x)     ((x)->num_fields)
522
523 #define sql_batch_start(x,y)    my_postgresql_batch_start(x,y)
524 #define sql_batch_end(x,y,z)    my_postgresql_batch_end(x,y,z)
525 #define sql_batch_insert(x,y,z) my_postgresql_batch_insert(x,y,z)
526 #define sql_batch_lock_path_query       my_pg_batch_lock_path_query
527 #define sql_batch_lock_filename_query   my_pg_batch_lock_filename_query
528 #define sql_batch_unlock_tables_query   my_pg_batch_unlock_tables_query
529 #define sql_batch_fill_filename_query   my_pg_batch_fill_filename_query
530 #define sql_batch_fill_path_query       my_pg_batch_fill_path_query
531
532 #define SQL_ROW               POSTGRESQL_ROW
533 #define SQL_FIELD             POSTGRESQL_FIELD
534 #define SQL_MATCH             "~"
535
536 #else
537
538 #ifdef HAVE_INGRES
539
540 #include "myingres.h"
541 #include "lib/breg.h"
542
543 #define BDB_VERSION 12
544
545 /* TEMP: the following is taken from select OID, typname from pg_type; */ /*SRE: huh? */
546 #define IS_NUM(x)        ((x) == 20 || (x) == 21 || (x) == 23 || (x) == 700 || (x) == 701)
547 #define IS_NOT_NULL(x)   ((x) == 1)
548
549 typedef char **INGRES_ROW;
550
551 /*
552  * This is the "real" definition that should only be
553  * used inside sql.c and associated database interface
554  * subroutines.
555  *
556  *                     I N G R E S
557  */
558 struct B_DB {
559    BQUEUE bq;                         /* queue control */
560    brwlock_t lock;                    /* transaction lock */
561    INGconn *db;
562    INGresult *result;
563    int status;
564    INGRES_ROW row;
565    INGRES_FIELD *fields;
566    int num_rows;
567    int row_size;                  /* size of malloced rows */
568    int num_fields;
569    int fields_size;               /* size of malloced fields */
570    int row_number;                /* row number from my_ingres_data_seek */
571    int field_number;              /* field number from my_ingres_field_seek */
572    int ref_count;
573    char *db_name;
574    char *db_user;
575    char *db_password;
576    char *db_address;              /* host address */
577    char *db_socket;               /* socket for local access */
578    int db_port;                   /* port of host address */
579    int have_insert_id;            /* do have insert_id() */
580    bool connected;
581    POOLMEM *errmsg;               /* nicely edited error message */
582    POOLMEM *cmd;                  /* SQL command string */
583    POOLMEM *cached_path;
584    int cached_path_len;           /* length of cached path */
585    uint32_t cached_path_id;
586    bool allow_transactions;       /* transactions allowed */
587    bool transaction;              /* transaction started */
588    int changes;                   /* changes made to db */
589    POOLMEM *fname;                /* Filename only */
590    POOLMEM *path;                 /* Path only */
591    POOLMEM *esc_name;             /* Escaped file name */
592    POOLMEM *esc_path;             /* Escaped path name */
593    BREGEXP *limit_filter;         /* Filter LIMIT function in queries into supported SQL */
594    int fnl;                       /* file name length */
595    int pnl;                       /* path name length */
596 };
597
598 void               my_ingres_free_result(B_DB *mdb);
599 INGRES_ROW         my_ingres_fetch_row  (B_DB *mdb);
600 int                my_ingres_query      (B_DB *mdb, const char *query);
601 void               my_ingres_data_seek  (B_DB *mdb, int row);
602 int                my_ingres_currval    (B_DB *mdb, const char *table_name);
603 void               my_ingres_field_seek (B_DB *mdb, int row);
604 INGRES_FIELD *     my_ingres_fetch_field(B_DB *mdb);
605 void               my_ingres_close      (B_DB *mdb);
606
607 int my_ingres_batch_start(JCR *jcr, B_DB *mdb);
608 int my_ingres_batch_end(JCR *jcr, B_DB *mdb, const char *error);
609 typedef struct ATTR_DBR ATTR_DBR;
610 int my_ingres_batch_insert(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
611 char *my_ingres_copy_escape(char *dest, char *src, size_t len);
612
613 extern const char* my_ingres_batch_lock_path_query;
614 extern const char* my_ingres_batch_lock_filename_query;
615 extern const char* my_ingres_batch_unlock_tables_query;
616 extern const char* my_ingres_batch_fill_filename_query;
617 extern const char* my_ingres_batch_fill_path_query;
618
619 /* "Generic" names for easier conversion */
620 #define sql_store_result(x)   ((x)->result)
621 #define sql_free_result(x)    my_ingres_free_result(x)
622 #define sql_fetch_row(x)      my_ingres_fetch_row(x)
623 #define sql_query(x, y)       my_ingres_query((x), (y))
624 #define sql_close(x)          my_ingres_close(x)
625 #define sql_strerror(x)       INGerrorMessage((x)->db)
626 #define sql_num_rows(x)       ((unsigned) INGntuples((x)->result))
627 #define sql_data_seek(x, i)   my_ingres_data_seek((x), (i))
628 #define sql_affected_rows(x)  ((unsigned) atoi(INGcmdTuples((x)->result)))
629 #define sql_insert_id(x,y)    my_ingres_currval((x), (y))
630 #define sql_field_seek(x, y)  my_ingres_field_seek((x), (y))
631 #define sql_fetch_field(x)    my_ingres_fetch_field(x)
632 #define sql_num_fields(x)     ((x)->num_fields)
633
634 #define sql_batch_start(x,y)    my_ingres_batch_start(x,y)
635 #define sql_batch_end(x,y,z)    my_ingres_batch_end(x,y,z)
636 #define sql_batch_insert(x,y,z) my_ingres_batch_insert(x,y,z)
637 #define sql_batch_lock_path_query       my_ingres_batch_lock_path_query
638 #define sql_batch_lock_filename_query   my_ingres_batch_lock_filename_query
639 #define sql_batch_unlock_tables_query   my_ingres_batch_unlock_tables_query
640 #define sql_batch_fill_filename_query   my_ingres_batch_fill_filename_query
641 #define sql_batch_fill_path_query       my_ingres_batch_fill_path_query
642
643 #define SQL_ROW               INGRES_ROW
644 #define SQL_FIELD             INGRES_FIELD
645
646 #else
647
648 #ifdef HAVE_DBI
649
650 #define BDB_VERSION 12
651
652 #include <dbi/dbi.h>
653
654 #ifdef HAVE_BATCH_FILE_INSERT
655 #include <dbi/dbi-dev.h>
656 #endif //HAVE_BATCH_FILE_INSERT
657
658 #define IS_NUM(x)        ((x) == 1 || (x) == 2 )
659 #define IS_NOT_NULL(x)   ((x) == (1 << 0))
660
661 typedef char **DBI_ROW;
662 typedef struct dbi_field {
663    char         *name;
664    int           max_length;
665    unsigned int  type;
666    unsigned int  flags;       // 1 == not null
667 } DBI_FIELD;
668
669 typedef struct dbi_field_get {
670    BQUEUE bq;
671    char *value;
672 } DBI_FIELD_GET;
673
674 /*
675  * This is the "real" definition that should only be
676  * used inside sql.c and associated database interface
677  * subroutines.
678  *
679  *                     D B I
680  */
681 struct B_DB {
682    BQUEUE bq;                         /* queue control */
683    brwlock_t lock;                    /* transaction lock */
684    dbi_conn *db;
685    dbi_result *result;
686    dbi_inst instance;
687    dbi_error_flag status;
688    DBI_ROW row;
689    DBI_FIELD *fields;
690    DBI_FIELD_GET *field_get;
691    int num_rows;
692    int row_size;                  /* size of malloced rows */
693    int num_fields;
694    int fields_size;               /* size of malloced fields */
695    int row_number;                /* row number from my_postgresql_data_seek */
696    int field_number;              /* field number from my_postgresql_field_seek */
697    int ref_count;
698    int db_type;                   /* DBI driver defined */
699    char *db_driverdir ;           /* DBI driver dir */
700    char *db_driver;               /* DBI type database */
701    char *db_name;
702    char *db_user;
703    char *db_password;
704    char *db_address;              /* host address */
705    char *db_socket;               /* socket for local access */
706    int db_port;                   /* port of host address */
707    int have_insert_id;            /* do have insert_id() */
708    bool connected;
709    POOLMEM *errmsg;               /* nicely edited error message */
710    POOLMEM *cmd;                  /* SQL command string */
711    POOLMEM *cached_path;
712    int cached_path_len;           /* length of cached path */
713    uint32_t cached_path_id;
714    bool allow_transactions;       /* transactions allowed */
715    bool transaction;              /* transaction started */
716    int changes;                   /* changes made to db */
717    POOLMEM *fname;                /* Filename only */
718    POOLMEM *path;                 /* Path only */
719    POOLMEM *esc_name;             /* Escaped file name */
720    POOLMEM *esc_path;             /* Escaped path name */
721    int fnl;                       /* file name length */
722    int pnl;                       /* path name length */
723 };
724
725 void               my_dbi_free_result(B_DB *mdb);
726 DBI_ROW            my_dbi_fetch_row  (B_DB *mdb);
727 int                my_dbi_query      (B_DB *mdb, const char *query);
728 void               my_dbi_data_seek  (B_DB *mdb, int row);
729 void               my_dbi_field_seek (B_DB *mdb, int row);
730 DBI_FIELD *        my_dbi_fetch_field(B_DB *mdb);
731 const char *       my_dbi_strerror   (B_DB *mdb);
732 int                my_dbi_getisnull  (dbi_result *result, int row_number, int column_number);
733 char *             my_dbi_getvalue   (dbi_result *result, int row_number, unsigned int column_number);
734 //int                my_dbi_getvalue   (dbi_result *result, int row_number, unsigned int column_number, char *value);
735 int                my_dbi_sql_insert_id(B_DB *mdb, char *table_name);
736
737 int my_dbi_batch_start(JCR *jcr, B_DB *mdb);
738 int my_dbi_batch_end(JCR *jcr, B_DB *mdb, const char *error);
739 typedef struct ATTR_DBR ATTR_DBR;
740 int my_dbi_batch_insert(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
741 char *my_postgresql_copy_escape(char *dest, char *src, size_t len);
742 // typedefs for libdbi work with postgresql copy insert
743 typedef int (*custom_function_insert_t)(void*, const char*, int);
744 typedef char* (*custom_function_error_t)(void*);
745 typedef int (*custom_function_end_t)(void*, const char*);
746
747 extern const char* my_dbi_batch_lock_path_query[4];
748 extern const char* my_dbi_batch_lock_filename_query[4];
749 extern const char* my_dbi_batch_unlock_tables_query[4];
750 extern const char* my_dbi_batch_fill_filename_query[4];
751 extern const char* my_dbi_batch_fill_path_query[4];
752 extern const char* my_dbi_match[4];
753
754 /* "Generic" names for easier conversion */
755 #define sql_store_result(x)   (x)->result
756 #define sql_free_result(x)    my_dbi_free_result(x)
757 #define sql_fetch_row(x)      my_dbi_fetch_row(x)
758 #define sql_query(x, y)       my_dbi_query((x), (y))
759 #define sql_close(x)          dbi_conn_close((x)->db)
760 #define sql_strerror(x)       my_dbi_strerror(x)
761 #define sql_num_rows(x)       dbi_result_get_numrows((x)->result)
762 #define sql_data_seek(x, i)   my_dbi_data_seek((x), (i))
763 #define SQL_MATCH             my_dbi_match[db_type]
764 /* #define sql_affected_rows(x)  dbi_result_get_numrows_affected((x)->result) */
765 #define sql_affected_rows(x)  1
766 #define sql_insert_id(x,y)    my_dbi_sql_insert_id((x), (y))
767 #define sql_field_seek(x, y)  my_dbi_field_seek((x), (y))
768 #define sql_fetch_field(x)    my_dbi_fetch_field(x)
769 #define sql_num_fields(x)     ((x)->num_fields)
770 #define sql_batch_start(x,y)    my_dbi_batch_start(x,y)
771 #define sql_batch_end(x,y,z)    my_dbi_batch_end(x,y,z)
772 #define sql_batch_insert(x,y,z) my_dbi_batch_insert(x,y,z)
773 #define sql_batch_lock_path_query       my_dbi_batch_lock_path_query[db_type]
774 #define sql_batch_lock_filename_query   my_dbi_batch_lock_filename_query[db_type]
775 #define sql_batch_unlock_tables_query   my_dbi_batch_unlock_tables_query[db_type]
776 #define sql_batch_fill_filename_query   my_dbi_batch_fill_filename_query[db_type]
777 #define sql_batch_fill_path_query       my_dbi_batch_fill_path_query[db_type]
778
779 #define SQL_ROW               DBI_ROW
780 #define SQL_FIELD             DBI_FIELD
781
782
783 #else  /* USE BACULA DB routines */
784
785 #define HAVE_BACULA_DB 1
786
787 /* Change this each time there is some incompatible
788  * file format change!!!!
789  */
790 #define BDB_VERSION 13                /* file version number */
791
792 struct s_control {
793    int bdb_version;                   /* Version number */
794    uint32_t JobId;                    /* next Job Id */
795    uint32_t PoolId;                   /* next Pool Id */
796    uint32_t MediaId;                  /* next Media Id */
797    uint32_t JobMediaId;               /* next JobMedia Id */
798    uint32_t ClientId;                 /* next Client Id */
799    uint32_t FileSetId;                /* nest FileSet Id */
800    time_t time;                       /* time file written */
801 };
802
803
804 /* This is the REAL definition for using the
805  *  Bacula internal DB
806  */
807 struct B_DB {
808    BQUEUE bq;                         /* queue control */
809 /* pthread_mutex_t mutex;  */         /* single thread lock */
810    brwlock_t lock;                    /* transaction lock */
811    int ref_count;                     /* number of times opened */
812    struct s_control control;          /* control file structure */
813    int cfd;                           /* control file device */
814    FILE *jobfd;                       /* Jobs records file descriptor */
815    FILE *poolfd;                      /* Pool records fd */
816    FILE *mediafd;                     /* Media records fd */
817    FILE *jobmediafd;                  /* JobMedia records fd */
818    FILE *clientfd;                    /* Client records fd */
819    FILE *filesetfd;                   /* FileSet records fd */
820    char *db_name;                     /* name of database */
821    POOLMEM *errmsg;                   /* nicely edited error message */
822    POOLMEM *cmd;                      /* Command string */
823    POOLMEM *cached_path;
824    int cached_path_len;               /* length of cached path */
825    uint32_t cached_path_id;
826 };
827
828 #endif /* HAVE_SQLITE3 */
829 #endif /* HAVE_MYSQL */
830 #endif /* HAVE_SQLITE */
831 #endif /* HAVE_POSTGRESQL */
832 #endif /* HAVE_INGRES */
833 #endif /* HAVE_DBI */
834 #endif
835
836 /* Use for better error location printing */
837 #define UPDATE_DB(jcr, db, cmd) UpdateDB(__FILE__, __LINE__, jcr, db, cmd)
838 #define INSERT_DB(jcr, db, cmd) InsertDB(__FILE__, __LINE__, jcr, db, cmd)
839 #define QUERY_DB(jcr, db, cmd) QueryDB(__FILE__, __LINE__, jcr, db, cmd)
840 #define DELETE_DB(jcr, db, cmd) DeleteDB(__FILE__, __LINE__, jcr, db, cmd)
841
842
843 #else    /* not __SQL_C */
844
845 /* This is a "dummy" definition for use outside of sql.c
846  */
847 struct B_DB {
848    int dummy;                         /* for SunOS compiler */
849 };
850
851 #endif /*  __SQL_C */
852
853 /* ==============================================================
854  *
855  *  What follows are definitions that are used "globally" for all
856  *   the different SQL engines and both inside and external to the
857  *   cats directory.
858  */
859
860 extern uint32_t bacula_db_version;
861
862 #define faddr_t long
863
864 /*
865  * Structure used when calling db_get_query_ids()
866  *  allows the subroutine to return a list of ids.
867  */
868 class dbid_list : public SMARTALLOC {
869 public:
870    DBId_t *DBId;                      /* array of DBIds */
871    char *PurgedFiles;                 /* Array of PurgedFile flags */
872    int num_ids;                       /* num of ids actually stored */
873    int max_ids;                       /* size of id array */
874    int num_seen;                      /* number of ids processed */
875    int tot_ids;                       /* total to process */
876
877    dbid_list();                       /* in sql.c */
878    ~dbid_list();                      /* in sql.c */
879 };
880
881
882
883
884 /* Job information passed to create job record and update
885  * job record at end of job. Note, although this record
886  * contains all the fields found in the Job database record,
887  * it also contains fields found in the JobMedia record.
888  */
889 /* Job record */
890 struct JOB_DBR {
891    JobId_t JobId;
892    char Job[MAX_NAME_LENGTH];         /* Job unique name */
893    char Name[MAX_NAME_LENGTH];        /* Job base name */
894    int JobType;                       /* actually char(1) */
895    int JobLevel;                      /* actually char(1) */
896    int JobStatus;                     /* actually char(1) */
897    DBId_t ClientId;                   /* Id of client */
898    DBId_t PoolId;                     /* Id of pool */
899    DBId_t FileSetId;                  /* Id of FileSet */
900    DBId_t PriorJobId;                 /* Id of migrated (prior) job */
901    time_t SchedTime;                  /* Time job scheduled */
902    time_t StartTime;                  /* Job start time */
903    time_t EndTime;                    /* Job termination time of orig job */
904    time_t RealEndTime;                /* Job termination time of this job */
905    utime_t JobTDate;                  /* Backup time/date in seconds */
906    uint32_t VolSessionId;
907    uint32_t VolSessionTime;
908    uint32_t JobFiles;
909    uint32_t JobErrors;
910    uint32_t JobMissingFiles;
911    uint64_t JobBytes;
912    uint64_t ReadBytes;
913    int PurgedFiles;
914    int HasBase;
915
916    /* Note, FirstIndex, LastIndex, Start/End File and Block
917     * are only used in the JobMedia record.
918     */
919    uint32_t FirstIndex;               /* First index this Volume */
920    uint32_t LastIndex;                /* Last index this Volume */
921    uint32_t StartFile;
922    uint32_t EndFile;
923    uint32_t StartBlock;
924    uint32_t EndBlock;
925
926    char cSchedTime[MAX_TIME_LENGTH];
927    char cStartTime[MAX_TIME_LENGTH];
928    char cEndTime[MAX_TIME_LENGTH];
929    char cRealEndTime[MAX_TIME_LENGTH];
930    /* Extra stuff not in DB */
931    int limit;                         /* limit records to display */
932    faddr_t rec_addr;
933    uint32_t FileIndex;                /* added during Verify */
934 };
935
936 /* Job Media information used to create the media records
937  * for each Volume used for the job.
938  */
939 /* JobMedia record */
940 struct JOBMEDIA_DBR {
941    DBId_t JobMediaId;                 /* record id */
942    JobId_t  JobId;                    /* JobId */
943    DBId_t MediaId;                    /* MediaId */
944    uint32_t FirstIndex;               /* First index this Volume */
945    uint32_t LastIndex;                /* Last index this Volume */
946    uint32_t StartFile;                /* File for start of data */
947    uint32_t EndFile;                  /* End file on Volume */
948    uint32_t StartBlock;               /* start block on tape */
949    uint32_t EndBlock;                 /* last block */
950 // uint32_t Copy;                     /* identical copy */
951 };
952
953
954 /* Volume Parameter structure */
955 struct VOL_PARAMS {
956    char VolumeName[MAX_NAME_LENGTH];  /* Volume name */
957    char MediaType[MAX_NAME_LENGTH];   /* Media Type */
958    char Storage[MAX_NAME_LENGTH];     /* Storage name */
959    uint32_t VolIndex;                 /* Volume seqence no. */
960    uint32_t FirstIndex;               /* First index this Volume */
961    uint32_t LastIndex;                /* Last index this Volume */
962    int32_t Slot;                      /* Slot */
963    uint64_t StartAddr;                /* Start address */
964    uint64_t EndAddr;                  /* End address */
965    int32_t InChanger;                 /* InChanger flag */
966 // uint32_t Copy;                     /* identical copy */
967 // uint32_t Stripe;                   /* RAIT strip number */
968 };
969
970
971 /* Attributes record -- NOT same as in database because
972  *  in general, this "record" creates multiple database
973  *  records (e.g. pathname, filename, fileattributes).
974  */
975 struct ATTR_DBR {
976    char *fname;                       /* full path & filename */
977    char *link;                        /* link if any */
978    char *attr;                        /* attributes statp */
979    uint32_t FileIndex;
980    uint32_t Stream;
981    uint32_t FileType;
982    JobId_t  JobId;
983    DBId_t ClientId;
984    DBId_t PathId;
985    DBId_t FilenameId;
986    FileId_t FileId;
987    char *Digest;
988    int DigestType;
989 };
990
991
992 /* File record -- same format as database */
993 struct FILE_DBR {
994    FileId_t FileId;
995    uint32_t FileIndex;
996    JobId_t  JobId;
997    DBId_t FilenameId;
998    DBId_t PathId;
999    JobId_t  MarkId;
1000    char LStat[256];
1001    char Digest[BASE64_SIZE(CRYPTO_DIGEST_MAX_SIZE)];
1002    int DigestType;                    /* NO_SIG/MD5_SIG/SHA1_SIG */
1003 };
1004
1005 /* Pool record -- same format as database */
1006 struct POOL_DBR {
1007    DBId_t PoolId;
1008    char Name[MAX_NAME_LENGTH];        /* Pool name */
1009    uint32_t NumVols;                  /* total number of volumes */
1010    uint32_t MaxVols;                  /* max allowed volumes */
1011    int32_t LabelType;                 /* Bacula/ANSI/IBM */
1012    int32_t UseOnce;                   /* set to use once only */
1013    int32_t UseCatalog;                /* set to use catalog */
1014    int32_t AcceptAnyVolume;           /* set to accept any volume sequence */
1015    int32_t AutoPrune;                 /* set to prune automatically */
1016    int32_t Recycle;                   /* default Vol recycle flag */
1017    uint32_t ActionOnPurge;            /* action on purge, e.g. truncate the disk volume */
1018    utime_t  VolRetention;             /* retention period in seconds */
1019    utime_t  VolUseDuration;           /* time in secs volume can be used */
1020    uint32_t MaxVolJobs;               /* Max Jobs on Volume */
1021    uint32_t MaxVolFiles;              /* Max files on Volume */
1022    uint64_t MaxVolBytes;              /* Max bytes on Volume */
1023    DBId_t RecyclePoolId;              /* RecyclePool destination when media is purged */
1024    DBId_t ScratchPoolId;              /* ScratchPool source when media is needed */
1025    char PoolType[MAX_NAME_LENGTH];
1026    char LabelFormat[MAX_NAME_LENGTH];
1027    /* Extra stuff not in DB */
1028    faddr_t rec_addr;
1029 };
1030
1031 class DEVICE_DBR {
1032 public:
1033    DBId_t DeviceId;
1034    char Name[MAX_NAME_LENGTH];        /* Device name */
1035    DBId_t MediaTypeId;                /* MediaType */
1036    DBId_t StorageId;                  /* Storage id if autochanger */
1037    uint32_t DevMounts;                /* Number of times mounted */
1038    uint32_t DevErrors;                /* Number of read/write errors */
1039    uint64_t DevReadBytes;             /* Number of bytes read */
1040    uint64_t DevWriteBytes;            /* Number of bytew written */
1041    uint64_t DevReadTime;              /* time spent reading volume */
1042    uint64_t DevWriteTime;             /* time spent writing volume */
1043    uint64_t DevReadTimeSincCleaning;  /* read time since cleaning */
1044    uint64_t DevWriteTimeSincCleaning; /* write time since cleaning */
1045    time_t   CleaningDate;             /* time last cleaned */
1046    utime_t  CleaningPeriod;           /* time between cleanings */
1047 };
1048
1049 class STORAGE_DBR {
1050 public:
1051    DBId_t StorageId;
1052    char Name[MAX_NAME_LENGTH];        /* Device name */
1053    int AutoChanger;                   /* Set if autochanger */
1054
1055    /* Not in database */
1056    bool created;                      /* set if created by db_create ... */
1057 };
1058
1059 class MEDIATYPE_DBR {
1060 public:
1061    DBId_t MediaTypeId;
1062    char MediaType[MAX_NAME_LENGTH];   /* MediaType string */
1063    int ReadOnly;                      /* Set if read-only */
1064 };
1065
1066
1067 /* Media record -- same as the database */
1068 struct MEDIA_DBR {
1069    DBId_t MediaId;                    /* Unique volume id */
1070    char VolumeName[MAX_NAME_LENGTH];  /* Volume name */
1071    char MediaType[MAX_NAME_LENGTH];   /* Media type */
1072    DBId_t PoolId;                     /* Pool id */
1073    time_t   FirstWritten;             /* Time Volume first written this usage */
1074    time_t   LastWritten;              /* Time Volume last written */
1075    time_t   LabelDate;                /* Date/Time Volume labeled */
1076    time_t   InitialWrite;             /* Date/Time Volume first written */
1077    int32_t  LabelType;                /* Label (Bacula/ANSI/IBM) */
1078    uint32_t VolJobs;                  /* number of jobs on this medium */
1079    uint32_t VolFiles;                 /* Number of files */
1080    uint32_t VolBlocks;                /* Number of blocks */
1081    uint32_t VolMounts;                /* Number of times mounted */
1082    uint32_t VolErrors;                /* Number of read/write errors */
1083    uint32_t VolWrites;                /* Number of writes */
1084    uint32_t VolReads;                 /* Number of reads */
1085    uint64_t VolBytes;                 /* Number of bytes written */
1086    uint32_t VolParts;                 /* Number of parts written */
1087    uint64_t MaxVolBytes;              /* Max bytes to write to Volume */
1088    uint64_t VolCapacityBytes;         /* capacity estimate */
1089    uint64_t VolReadTime;              /* time spent reading volume */
1090    uint64_t VolWriteTime;             /* time spent writing volume */
1091    utime_t  VolRetention;             /* Volume retention in seconds */
1092    utime_t  VolUseDuration;           /* time in secs volume can be used */
1093    uint32_t ActionOnPurge;            /* action on purge, e.g. truncate the disk volume */
1094    uint32_t MaxVolJobs;               /* Max Jobs on Volume */
1095    uint32_t MaxVolFiles;              /* Max files on Volume */
1096    int32_t  Recycle;                  /* recycle yes/no */
1097    int32_t  Slot;                     /* slot in changer */
1098    int32_t  Enabled;                  /* 0=disabled, 1=enabled, 2=archived */
1099    int32_t  InChanger;                /* Volume currently in changer */
1100    DBId_t   StorageId;                /* Storage record Id */
1101    uint32_t EndFile;                  /* Last file on volume */
1102    uint32_t EndBlock;                 /* Last block on volume */
1103    uint32_t RecycleCount;             /* Number of times recycled */
1104    char     VolStatus[20];            /* Volume status */
1105    DBId_t   DeviceId;                 /* Device where Vol last written */
1106    DBId_t   LocationId;               /* Where Volume is -- user defined */
1107    DBId_t   ScratchPoolId;            /* Where to move if scratch */
1108    DBId_t   RecyclePoolId;            /* Where to move when recycled */
1109    /* Extra stuff not in DB */
1110    faddr_t rec_addr;                  /* found record address */
1111    /* Since the database returns times as strings, this is how we pass
1112     *   them back.
1113     */
1114    char    cFirstWritten[MAX_TIME_LENGTH]; /* FirstWritten returned from DB */
1115    char    cLastWritten[MAX_TIME_LENGTH];  /* LastWritten returned from DB */
1116    char    cLabelDate[MAX_TIME_LENGTH];    /* LabelData returned from DB */
1117    char    cInitialWrite[MAX_TIME_LENGTH]; /* InitialWrite returned from DB */
1118    bool    set_first_written;
1119    bool    set_label_date;
1120 };
1121
1122 /* Client record -- same as the database */
1123 struct CLIENT_DBR {
1124    DBId_t ClientId;                   /* Unique Client id */
1125    int AutoPrune;
1126    utime_t FileRetention;
1127    utime_t JobRetention;
1128    char Name[MAX_NAME_LENGTH];        /* Client name */
1129    char Uname[256];                   /* Uname for client */
1130 };
1131
1132 /* Counter record as in database */
1133 struct COUNTER_DBR {
1134    char Counter[MAX_NAME_LENGTH];
1135    int32_t MinValue;
1136    int32_t MaxValue;
1137    int32_t CurrentValue;
1138    char WrapCounter[MAX_NAME_LENGTH];
1139 };
1140
1141
1142 /* FileSet record -- same as the database */
1143 struct FILESET_DBR {
1144    DBId_t FileSetId;                  /* Unique FileSet id */
1145    char FileSet[MAX_NAME_LENGTH];     /* FileSet name */
1146    char MD5[50];                      /* MD5 signature of include/exclude */
1147    time_t CreateTime;                 /* date created */
1148    /*
1149     * This is where we return CreateTime
1150     */
1151    char cCreateTime[MAX_TIME_LENGTH]; /* CreateTime as returned from DB */
1152    /* Not in DB but returned by db_create_fileset() */
1153    bool created;                      /* set when record newly created */
1154 };
1155
1156 /* Call back context for getting a 32/64 bit value from the database */
1157 struct db_int64_ctx {
1158    int64_t value;                     /* value returned */
1159    int count;                         /* number of values seen */
1160 };
1161
1162 /* Call back context for getting a list of comma separated strings from the
1163  * database 
1164  */
1165 class db_list_ctx {
1166 public:
1167    POOLMEM *list;                     /* list */
1168    int count;                         /* number of values seen */
1169
1170    db_list_ctx() { list = get_pool_memory(PM_FNAME); *list = 0; count = 0; }
1171    ~db_list_ctx() { free_pool_memory(list); list = NULL; }
1172
1173 private:
1174    db_list_ctx(const db_list_ctx&);            /* prohibit pass by value */
1175    db_list_ctx &operator=(const db_list_ctx&); /* prohibit class assignment */
1176 };
1177
1178
1179 #include "protos.h"
1180 #include "jcr.h"
1181 #include "sql_cmds.h"
1182
1183 /*
1184  * Exported globals from sql.c
1185  */
1186 extern int CATS_IMP_EXP db_type;        /* SQL engine type index */
1187
1188 /*
1189  * Some functions exported by sql.c for use within the
1190  *   cats directory.
1191  */
1192 void list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type type);
1193 void list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx);
1194 int get_sql_record_max(JCR *jcr, B_DB *mdb);
1195 bool check_tables_version(JCR *jcr, B_DB *mdb);
1196 bool db_check_max_connections(JCR *jcr, B_DB *mdb, uint32_t nb);
1197 void _db_unlock(const char *file, int line, B_DB *mdb);
1198 void _db_lock(const char *file, int line, B_DB *mdb);
1199 const char *db_get_type(void);
1200
1201 void print_dashes(B_DB *mdb);
1202 void print_result(B_DB *mdb);
1203 int QueryDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
1204 int InsertDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
1205 int DeleteDB(const char *file, int line, JCR *jcr, B_DB *db, char *delete_cmd);
1206 int UpdateDB(const char *file, int line, JCR *jcr, B_DB *db, char *update_cmd);
1207 void split_path_and_file(JCR *jcr, B_DB *mdb, const char *fname);
1208 #endif /* __SQL_H_ */