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