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