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