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