2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
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
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.
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
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.
33 * Anyone who accesses the database will need to include
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
45 Here is how database versions work.
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
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).
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.
74 SQL_TYPE_POSTGRESQL = 1,
80 typedef void (DB_LIST_HANDLER)(void *, const char *);
81 typedef int (DB_RESULT_HANDLER)(void *, int, char **);
83 #define db_lock(mdb) _db_lock(__FILE__, __LINE__, mdb)
84 #define db_unlock(mdb) _db_unlock(__FILE__, __LINE__, mdb)
88 #if defined(BUILDING_CATS)
91 #define BDB_VERSION 10
95 /* Define opaque structure for sqlite */
100 #define IS_NUM(x) ((x) == 1)
101 #define IS_NOT_NULL(x) ((x) == 1)
103 typedef struct s_sql_field {
104 char *name; /* name of column */
105 int length; /* length */
106 int max_length; /* max length */
107 uint32_t type; /* type */
108 uint32_t flags; /* flags */
112 * This is the "real" definition that should only be
113 * used inside sql.c and associated database interface
118 BQUEUE bq; /* queue control */
119 brwlock_t lock; /* transaction lock */
123 int nrow; /* nrow returned from sqlite */
124 int ncolumn; /* ncolum returned from sqlite */
125 int num_rows; /* used by code */
126 int row; /* seek row */
127 int field; /* seek field */
128 SQL_FIELD **fields; /* defined fields */
132 char *db_address; /* host name address */
133 char *db_socket; /* socket for local access */
135 int db_port; /* port for host name address */
136 bool connected; /* connection made to db */
137 bool have_insert_id; /* do not have insert id */
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 */
158 * "Generic" names for easier conversion
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))
167 #define sql_insert_id(x,y) sqlite3_last_insert_rowid((x)->db)
168 #define sql_close(x) sqlite3_close((x)->db)
170 #define sql_insert_id(x,y) sqlite_last_insert_rowid((x)->db)
171 #define sql_close(x) sqlite_close((x)->db)
173 #define sql_strerror(x) (x)->sqlite_errmsg?(x)->sqlite_errmsg:"unknown"
174 #define sql_num_rows(x) (x)->nrow
175 #define sql_data_seek(x, i) (x)->row = (i)
176 #define sql_affected_rows(x) 1
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**
182 #define sql_batch_start(x,y) my_batch_start(x,y)
183 #define sql_batch_end(x,y,z) my_batch_end(x,y,z)
184 #define sql_batch_insert(x,y,z) my_batch_insert(x,y,z)
185 #define sql_batch_lock_path_query my_sqlite_batch_lock_query
186 #define sql_batch_lock_filename_query my_sqlite_batch_lock_query
187 #define sql_batch_unlock_tables_query my_sqlite_batch_unlock_query
188 #define sql_batch_fill_filename_query my_sqlite_batch_fill_filename_query
189 #define sql_batch_fill_path_query my_sqlite_batch_fill_path_query
191 /* In cats/sqlite.c */
192 void my_sqlite_free_table(B_DB *mdb);
193 SQL_ROW my_sqlite_fetch_row(B_DB *mdb);
194 int my_sqlite_query(B_DB *mdb, const char *cmd);
195 void my_sqlite_field_seek(B_DB *mdb, int field);
196 SQL_FIELD *my_sqlite_fetch_field(B_DB *mdb);
197 extern const char* my_sqlite_batch_lock_query;
198 extern const char* my_sqlite_batch_unlock_query;
199 extern const char* my_sqlite_batch_fill_filename_query;
200 extern const char* my_sqlite_batch_fill_path_query;
211 #define BDB_VERSION 10
215 /* Define opaque structure for sqlite */
220 #define IS_NUM(x) ((x) == 1)
221 #define IS_NOT_NULL(x) ((x) == 1)
223 typedef struct s_sql_field {
224 char *name; /* name of column */
225 int length; /* length */
226 int max_length; /* max length */
227 uint32_t type; /* type */
228 uint32_t flags; /* flags */
232 * This is the "real" definition that should only be
233 * used inside sql.c and associated database interface
238 BQUEUE bq; /* queue control */
239 brwlock_t lock; /* transaction lock */
243 int nrow; /* nrow returned from sqlite */
244 int ncolumn; /* ncolum returned from sqlite */
245 int num_rows; /* used by code */
246 int row; /* seek row */
247 int field; /* seek field */
248 SQL_FIELD **fields; /* defined fields */
252 char *db_address; /* host name address */
253 char *db_socket; /* socket for local access */
255 int db_port; /* port for host name address */
256 bool connected; /* connection made to db */
257 bool have_insert_id; /* do not have insert id */
258 bool fields_defined; /* set when fields defined */
259 char *sqlite_errmsg; /* error message returned by sqlite */
260 POOLMEM *errmsg; /* nicely edited error message */
261 POOLMEM *cmd; /* SQL command string */
262 POOLMEM *cached_path; /* cached path name */
263 int cached_path_len; /* length of cached path */
264 uint32_t cached_path_id; /* cached path id */
265 bool allow_transactions; /* transactions allowed */
266 bool transaction; /* transaction started */
267 int changes; /* changes during transaction */
268 POOLMEM *fname; /* Filename only */
269 POOLMEM *path; /* Path only */
270 POOLMEM *esc_name; /* Escaped file name */
271 POOLMEM *esc_path; /* Escaped path name */
272 int fnl; /* file name length */
273 int pnl; /* path name length */
277 * Conversion of sqlite 2 names to sqlite3
279 #define sqlite_last_insert_rowid sqlite3_last_insert_rowid
280 #define sqlite_open sqlite3_open
281 #define sqlite_close sqlite3_close
282 #define sqlite_result sqlite3_result
283 #define sqlite_exec sqlite3_exec
284 #define sqlite_get_table sqlite3_get_table
285 #define sqlite_free_table sqlite3_free_table
289 * "Generic" names for easier conversion
293 #define sql_store_result(x) (x)->result
294 #define sql_free_result(x) my_sqlite_free_table(x)
295 #define sql_fetch_row(x) my_sqlite_fetch_row(x)
296 #define sql_query(x, y) my_sqlite_query((x), (y))
298 #define sql_insert_id(x,y) sqlite3_last_insert_rowid((x)->db)
299 #define sql_close(x) sqlite3_close((x)->db)
301 #define sql_insert_id(x,y) sqlite_last_insert_rowid((x)->db)
302 #define sql_close(x) sqlite_close((x)->db)
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) 1
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_batch_lock_path_query my_sqlite_batch_lock_query
316 #define sql_batch_lock_filename_query my_sqlite_batch_lock_query
317 #define sql_batch_unlock_tables_query my_sqlite_batch_unlock_query
318 #define sql_batch_fill_filename_query my_sqlite_batch_fill_filename_query
319 #define sql_batch_fill_path_query my_sqlite_batch_fill_path_query
321 /* In cats/sqlite.c */
322 void my_sqlite_free_table(B_DB *mdb);
323 SQL_ROW my_sqlite_fetch_row(B_DB *mdb);
324 int my_sqlite_query(B_DB *mdb, const char *cmd);
325 void my_sqlite_field_seek(B_DB *mdb, int field);
326 SQL_FIELD *my_sqlite_fetch_field(B_DB *mdb);
327 extern const char* my_sqlite_batch_lock_query;
328 extern const char* my_sqlite_batch_unlock_query;
329 extern const char* my_sqlite_batch_fill_filename_query;
330 extern const char* my_sqlite_batch_fill_path_query;
337 #define BDB_VERSION 10
342 * This is the "real" definition that should only be
343 * used inside sql.c and associated database interface
349 BQUEUE bq; /* queue control */
350 brwlock_t lock; /* transaction lock */
355 my_ulonglong num_rows;
360 char *db_address; /* host address */
361 char *db_socket; /* socket for local access */
362 int db_port; /* port of host address */
363 int have_insert_id; /* do have insert_id() */
365 POOLMEM *errmsg; /* nicely edited error message */
366 POOLMEM *cmd; /* SQL command string */
367 POOLMEM *cached_path;
368 int cached_path_len; /* length of cached path */
369 uint32_t cached_path_id;
370 int changes; /* changes made to db */
371 POOLMEM *fname; /* Filename only */
372 POOLMEM *path; /* Path only */
373 POOLMEM *esc_name; /* Escaped file name */
374 POOLMEM *esc_path; /* Escaped path name */
375 int fnl; /* file name length */
376 int pnl; /* path name length */
379 #define DB_STATUS int
381 /* "Generic" names for easier conversion */
382 #define sql_store_result(x) mysql_store_result((x)->db)
383 #define sql_use_result(x) mysql_use_result((x)->db)
384 #define sql_free_result(x) my_mysql_free_result(x)
385 #define sql_fetch_row(x) mysql_fetch_row((x)->result)
386 #define sql_query(x, y) mysql_query((x)->db, (y))
387 #define sql_strerror(x) mysql_error((x)->db)
388 #define sql_num_rows(x) mysql_num_rows((x)->result)
389 #define sql_data_seek(x, i) mysql_data_seek((x)->result, (i))
390 #define sql_affected_rows(x) mysql_affected_rows((x)->db)
391 #define sql_insert_id(x,y) mysql_insert_id((x)->db)
392 #define sql_field_seek(x, y) mysql_field_seek((x)->result, (y))
393 #define sql_fetch_field(x) mysql_fetch_field((x)->result)
394 #define sql_num_fields(x) (int)mysql_num_fields((x)->result)
395 #define SQL_ROW MYSQL_ROW
396 #define SQL_FIELD MYSQL_FIELD
398 #define sql_batch_start(x,y) my_batch_start(x,y)
399 #define sql_batch_end(x,y,z) my_batch_end(x,y,z)
400 #define sql_batch_insert(x,y,z) my_batch_insert(x,y,z)
401 #define sql_batch_lock_path_query my_mysql_batch_lock_path_query
402 #define sql_batch_lock_filename_query my_mysql_batch_lock_filename_query
403 #define sql_batch_unlock_tables_query my_mysql_batch_unlock_tables_query
404 #define sql_batch_fill_filename_query my_mysql_batch_fill_filename_query
405 #define sql_batch_fill_path_query my_mysql_batch_fill_path_query
408 extern const char* my_mysql_batch_lock_path_query;
409 extern const char* my_mysql_batch_lock_filename_query;
410 extern const char* my_mysql_batch_unlock_tables_query;
411 extern const char* my_mysql_batch_fill_filename_query;
412 extern const char* my_mysql_batch_fill_path_query;
413 extern void my_mysql_free_result(B_DB *mdb);
417 #ifdef HAVE_POSTGRESQL
419 #define BDB_VERSION 10
421 #include <libpq-fe.h>
423 /* TEMP: the following is taken from select OID, typname from pg_type; */
424 #define IS_NUM(x) ((x) == 20 || (x) == 21 || (x) == 23 || (x) == 700 || (x) == 701)
425 #define IS_NOT_NULL(x) ((x) == 1)
427 typedef char **POSTGRESQL_ROW;
428 typedef struct pg_field {
432 unsigned int flags; // 1 == not null
437 * This is the "real" definition that should only be
438 * used inside sql.c and associated database interface
441 * P O S T G R E S Q L
444 BQUEUE bq; /* queue control */
445 brwlock_t lock; /* transaction lock */
450 POSTGRESQL_FIELD *fields;
452 int row_size; /* size of malloced rows */
454 int fields_size; /* size of malloced fields */
455 int row_number; /* row number from my_postgresql_data_seek */
456 int field_number; /* field number from my_postgresql_field_seek */
461 char *db_address; /* host address */
462 char *db_socket; /* socket for local access */
463 int db_port; /* port of host address */
464 int have_insert_id; /* do have insert_id() */
466 POOLMEM *errmsg; /* nicely edited error message */
467 POOLMEM *cmd; /* SQL command string */
468 POOLMEM *cached_path;
469 int cached_path_len; /* length of cached path */
470 uint32_t cached_path_id;
471 bool allow_transactions; /* transactions allowed */
472 bool transaction; /* transaction started */
473 int changes; /* changes made to db */
474 POOLMEM *fname; /* Filename only */
475 POOLMEM *path; /* Path only */
476 POOLMEM *esc_name; /* Escaped file name */
477 POOLMEM *esc_path; /* Escaped path name */
478 int fnl; /* file name length */
479 int pnl; /* path name length */
482 void my_postgresql_free_result(B_DB *mdb);
483 POSTGRESQL_ROW my_postgresql_fetch_row (B_DB *mdb);
484 int my_postgresql_query (B_DB *mdb, const char *query);
485 void my_postgresql_data_seek (B_DB *mdb, int row);
486 int my_postgresql_currval (B_DB *mdb, const char *table_name);
487 void my_postgresql_field_seek (B_DB *mdb, int row);
488 POSTGRESQL_FIELD * my_postgresql_fetch_field(B_DB *mdb);
490 int my_postgresql_batch_start(JCR *jcr, B_DB *mdb);
491 int my_postgresql_batch_end(JCR *jcr, B_DB *mdb, const char *error);
492 typedef struct ATTR_DBR ATTR_DBR;
493 int my_postgresql_batch_insert(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
494 char *my_postgresql_copy_escape(char *dest, char *src, size_t len);
496 extern const char* my_pg_batch_lock_path_query;
497 extern const char* my_pg_batch_lock_filename_query;
498 extern const char* my_pg_batch_unlock_tables_query;
499 extern const char* my_pg_batch_fill_filename_query;
500 extern const char* my_pg_batch_fill_path_query;
502 /* "Generic" names for easier conversion */
503 #define sql_store_result(x) ((x)->result)
504 #define sql_free_result(x) my_postgresql_free_result(x)
505 #define sql_fetch_row(x) my_postgresql_fetch_row(x)
506 #define sql_query(x, y) my_postgresql_query((x), (y))
507 #define sql_close(x) PQfinish((x)->db)
508 #define sql_strerror(x) PQerrorMessage((x)->db)
509 #define sql_num_rows(x) ((unsigned) PQntuples((x)->result))
510 #define sql_data_seek(x, i) my_postgresql_data_seek((x), (i))
511 #define sql_affected_rows(x) ((unsigned) atoi(PQcmdTuples((x)->result)))
512 #define sql_insert_id(x,y) my_postgresql_currval((x), (y))
513 #define sql_field_seek(x, y) my_postgresql_field_seek((x), (y))
514 #define sql_fetch_field(x) my_postgresql_fetch_field(x)
515 #define sql_num_fields(x) ((x)->num_fields)
517 #define sql_batch_start(x,y) my_postgresql_batch_start(x,y)
518 #define sql_batch_end(x,y,z) my_postgresql_batch_end(x,y,z)
519 #define sql_batch_insert(x,y,z) my_postgresql_batch_insert(x,y,z)
520 #define sql_batch_lock_path_query my_pg_batch_lock_path_query
521 #define sql_batch_lock_filename_query my_pg_batch_lock_filename_query
522 #define sql_batch_unlock_tables_query my_pg_batch_unlock_tables_query
523 #define sql_batch_fill_filename_query my_pg_batch_fill_filename_query
524 #define sql_batch_fill_path_query my_pg_batch_fill_path_query
526 #define SQL_ROW POSTGRESQL_ROW
527 #define SQL_FIELD POSTGRESQL_FIELD
533 #define BDB_VERSION 10
537 #ifdef HAVE_BATCH_FILE_INSERT
538 #include <dbi/dbi-dev.h>
539 #endif //HAVE_BATCH_FILE_INSERT
541 #define IS_NUM(x) ((x) == 1 || (x) == 2 )
542 #define IS_NOT_NULL(x) ((x) == (1 << 0))
544 typedef char **DBI_ROW;
545 typedef struct dbi_field {
549 unsigned int flags; // 1 == not null
552 typedef struct dbi_field_get {
558 * This is the "real" definition that should only be
559 * used inside sql.c and associated database interface
565 BQUEUE bq; /* queue control */
566 brwlock_t lock; /* transaction lock */
570 dbi_error_flag status;
573 DBI_FIELD_GET *field_get;
575 int row_size; /* size of malloced rows */
577 int fields_size; /* size of malloced fields */
578 int row_number; /* row number from my_postgresql_data_seek */
579 int field_number; /* field number from my_postgresql_field_seek */
581 int db_type; /* DBI driver defined */
582 char *db_driverdir ; /* DBI driver dir */
583 char *db_driver; /* DBI type database */
587 char *db_address; /* host address */
588 char *db_socket; /* socket for local access */
589 int db_port; /* port of host address */
590 int have_insert_id; /* do have insert_id() */
592 POOLMEM *errmsg; /* nicely edited error message */
593 POOLMEM *cmd; /* SQL command string */
594 POOLMEM *cached_path;
595 int cached_path_len; /* length of cached path */
596 uint32_t cached_path_id;
597 bool allow_transactions; /* transactions allowed */
598 bool transaction; /* transaction started */
599 int changes; /* changes made to db */
600 POOLMEM *fname; /* Filename only */
601 POOLMEM *path; /* Path only */
602 POOLMEM *esc_name; /* Escaped file name */
603 POOLMEM *esc_path; /* Escaped path name */
604 int fnl; /* file name length */
605 int pnl; /* path name length */
608 void my_dbi_free_result(B_DB *mdb);
609 DBI_ROW my_dbi_fetch_row (B_DB *mdb);
610 int my_dbi_query (B_DB *mdb, const char *query);
611 void my_dbi_data_seek (B_DB *mdb, int row);
612 void my_dbi_field_seek (B_DB *mdb, int row);
613 DBI_FIELD * my_dbi_fetch_field(B_DB *mdb);
614 const char * my_dbi_strerror (B_DB *mdb);
615 int my_dbi_getisnull (dbi_result *result, int row_number, int column_number);
616 char * my_dbi_getvalue (dbi_result *result, int row_number, unsigned int column_number);
617 //int my_dbi_getvalue (dbi_result *result, int row_number, unsigned int column_number, char *value);
618 int my_dbi_sql_insert_id(B_DB *mdb, char *table_name);
620 int my_dbi_batch_start(JCR *jcr, B_DB *mdb);
621 int my_dbi_batch_end(JCR *jcr, B_DB *mdb, const char *error);
622 typedef struct ATTR_DBR ATTR_DBR;
623 int my_dbi_batch_insert(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
624 char *my_postgresql_copy_escape(char *dest, char *src, size_t len);
625 // typedefs for libdbi work with postgresql copy insert
626 typedef int (*custom_function_insert_t)(void*, const char*, int);
627 typedef char* (*custom_function_error_t)(void*);
628 typedef int (*custom_function_end_t)(void*, const char*);
630 extern const char* my_dbi_batch_lock_path_query[4];
631 extern const char* my_dbi_batch_lock_filename_query[4];
632 extern const char* my_dbi_batch_unlock_tables_query[4];
633 extern const char* my_dbi_batch_fill_filename_query[4];
634 extern const char* my_dbi_batch_fill_path_query[4];
636 /* "Generic" names for easier conversion */
637 #define sql_store_result(x) (x)->result
638 #define sql_free_result(x) my_dbi_free_result(x)
639 #define sql_fetch_row(x) my_dbi_fetch_row(x)
640 #define sql_query(x, y) my_dbi_query((x), (y))
641 #define sql_close(x) dbi_conn_close((x)->db)
642 #define sql_strerror(x) my_dbi_strerror(x)
643 #define sql_num_rows(x) dbi_result_get_numrows((x)->result)
644 #define sql_data_seek(x, i) my_dbi_data_seek((x), (i))
645 /* #define sql_affected_rows(x) dbi_result_get_numrows_affected((x)->result) */
646 #define sql_affected_rows(x) 1
647 #define sql_insert_id(x,y) my_dbi_sql_insert_id((x), (y))
648 #define sql_field_seek(x, y) my_dbi_field_seek((x), (y))
649 #define sql_fetch_field(x) my_dbi_fetch_field(x)
650 #define sql_num_fields(x) ((x)->num_fields)
651 #define sql_batch_start(x,y) my_dbi_batch_start(x,y)
652 #define sql_batch_end(x,y,z) my_dbi_batch_end(x,y,z)
653 #define sql_batch_insert(x,y,z) my_dbi_batch_insert(x,y,z)
654 #define sql_batch_lock_path_query my_dbi_batch_lock_path_query[db_type]
655 #define sql_batch_lock_filename_query my_dbi_batch_lock_filename_query[db_type]
656 #define sql_batch_unlock_tables_query my_dbi_batch_unlock_tables_query[db_type]
657 #define sql_batch_fill_filename_query my_dbi_batch_fill_filename_query[db_type]
658 #define sql_batch_fill_path_query my_dbi_batch_fill_path_query[db_type]
660 #define SQL_ROW DBI_ROW
661 #define SQL_FIELD DBI_FIELD
664 #else /* USE BACULA DB routines */
666 #define HAVE_BACULA_DB 1
668 /* Change this each time there is some incompatible
669 * file format change!!!!
671 #define BDB_VERSION 13 /* file version number */
674 int bdb_version; /* Version number */
675 uint32_t JobId; /* next Job Id */
676 uint32_t PoolId; /* next Pool Id */
677 uint32_t MediaId; /* next Media Id */
678 uint32_t JobMediaId; /* next JobMedia Id */
679 uint32_t ClientId; /* next Client Id */
680 uint32_t FileSetId; /* nest FileSet Id */
681 time_t time; /* time file written */
685 /* This is the REAL definition for using the
689 BQUEUE bq; /* queue control */
690 /* pthread_mutex_t mutex; */ /* single thread lock */
691 brwlock_t lock; /* transaction lock */
692 int ref_count; /* number of times opened */
693 struct s_control control; /* control file structure */
694 int cfd; /* control file device */
695 FILE *jobfd; /* Jobs records file descriptor */
696 FILE *poolfd; /* Pool records fd */
697 FILE *mediafd; /* Media records fd */
698 FILE *jobmediafd; /* JobMedia records fd */
699 FILE *clientfd; /* Client records fd */
700 FILE *filesetfd; /* FileSet records fd */
701 char *db_name; /* name of database */
702 POOLMEM *errmsg; /* nicely edited error message */
703 POOLMEM *cmd; /* Command string */
704 POOLMEM *cached_path;
705 int cached_path_len; /* length of cached path */
706 uint32_t cached_path_id;
709 #endif /* HAVE_SQLITE3 */
710 #endif /* HAVE_MYSQL */
711 #endif /* HAVE_SQLITE */
712 #endif /* HAVE_POSTGRESQL */
713 #endif /* HAVE_DBI */
716 /* Use for better error location printing */
717 #define UPDATE_DB(jcr, db, cmd) UpdateDB(__FILE__, __LINE__, jcr, db, cmd)
718 #define INSERT_DB(jcr, db, cmd) InsertDB(__FILE__, __LINE__, jcr, db, cmd)
719 #define QUERY_DB(jcr, db, cmd) QueryDB(__FILE__, __LINE__, jcr, db, cmd)
720 #define DELETE_DB(jcr, db, cmd) DeleteDB(__FILE__, __LINE__, jcr, db, cmd)
723 #else /* not __SQL_C */
725 /* This is a "dummy" definition for use outside of sql.c
728 int dummy; /* for SunOS compiler */
733 /* ==============================================================
735 * What follows are definitions that are used "globally" for all
736 * the different SQL engines and both inside and external to the
740 extern uint32_t bacula_db_version;
743 * These are the sizes of the current definitions of database
744 * Ids. In general, FileId_t can be set to uint64_t and it
745 * *should* work. Users have reported back that it does work
746 * for PostgreSQL. For the other types, all places in Bacula
747 * have been converted, but no one has actually tested it.
748 * In principle, the only field that really should need to be
749 * 64 bits is the FileId_t
751 typedef uint32_t FileId_t;
752 typedef uint32_t DBId_t; /* general DB id type */
753 typedef uint32_t JobId_t;
758 * Structure used when calling db_get_query_ids()
759 * allows the subroutine to return a list of ids.
761 class dbid_list : public SMARTALLOC {
763 DBId_t *DBId; /* array of DBIds */
764 char *PurgedFiles; /* Array of PurgedFile flags */
765 int num_ids; /* num of ids actually stored */
766 int max_ids; /* size of id array */
767 int num_seen; /* number of ids processed */
768 int tot_ids; /* total to process */
770 dbid_list(); /* in sql.c */
771 ~dbid_list(); /* in sql.c */
777 /* Job information passed to create job record and update
778 * job record at end of job. Note, although this record
779 * contains all the fields found in the Job database record,
780 * it also contains fields found in the JobMedia record.
785 char Job[MAX_NAME_LENGTH]; /* Job unique name */
786 char Name[MAX_NAME_LENGTH]; /* Job base name */
787 int JobType; /* actually char(1) */
788 int JobLevel; /* actually char(1) */
789 int JobStatus; /* actually char(1) */
790 DBId_t ClientId; /* Id of client */
791 DBId_t PoolId; /* Id of pool */
792 DBId_t FileSetId; /* Id of FileSet */
793 DBId_t PriorJobId; /* Id of migrated (prior) job */
794 time_t SchedTime; /* Time job scheduled */
795 time_t StartTime; /* Job start time */
796 time_t EndTime; /* Job termination time of orig job */
797 time_t RealEndTime; /* Job termination time of this job */
798 utime_t JobTDate; /* Backup time/date in seconds */
799 uint32_t VolSessionId;
800 uint32_t VolSessionTime;
803 uint32_t JobMissingFiles;
808 /* Note, FirstIndex, LastIndex, Start/End File and Block
809 * are only used in the JobMedia record.
811 uint32_t FirstIndex; /* First index this Volume */
812 uint32_t LastIndex; /* Last index this Volume */
818 char cSchedTime[MAX_TIME_LENGTH];
819 char cStartTime[MAX_TIME_LENGTH];
820 char cEndTime[MAX_TIME_LENGTH];
821 char cRealEndTime[MAX_TIME_LENGTH];
822 /* Extra stuff not in DB */
823 int limit; /* limit records to display */
825 uint32_t FileIndex; /* added during Verify */
828 /* Job Media information used to create the media records
829 * for each Volume used for the job.
831 /* JobMedia record */
832 struct JOBMEDIA_DBR {
833 DBId_t JobMediaId; /* record id */
834 JobId_t JobId; /* JobId */
835 DBId_t MediaId; /* MediaId */
836 uint32_t FirstIndex; /* First index this Volume */
837 uint32_t LastIndex; /* Last index this Volume */
838 uint32_t StartFile; /* File for start of data */
839 uint32_t EndFile; /* End file on Volume */
840 uint32_t StartBlock; /* start block on tape */
841 uint32_t EndBlock; /* last block */
842 uint32_t Copy; /* identical copy */
846 /* Volume Parameter structure */
848 char VolumeName[MAX_NAME_LENGTH]; /* Volume name */
849 char MediaType[MAX_NAME_LENGTH]; /* Media Type */
850 char Storage[MAX_NAME_LENGTH]; /* Storage name */
851 uint32_t VolIndex; /* Volume seqence no. */
852 uint32_t FirstIndex; /* First index this Volume */
853 uint32_t LastIndex; /* Last index this Volume */
854 uint32_t StartFile; /* File for start of data */
855 uint32_t EndFile; /* End file on Volume */
856 uint32_t StartBlock; /* start block on tape */
857 uint32_t EndBlock; /* last block */
858 int32_t Slot; /* Slot */
859 // uint32_t Copy; /* identical copy */
860 // uint32_t Stripe; /* RAIT strip number */
864 /* Attributes record -- NOT same as in database because
865 * in general, this "record" creates multiple database
866 * records (e.g. pathname, filename, fileattributes).
869 char *fname; /* full path & filename */
870 char *link; /* link if any */
871 char *attr; /* attributes statp */
884 /* File record -- same format as database */
893 char Digest[BASE64_SIZE(CRYPTO_DIGEST_MAX_SIZE)];
894 int DigestType; /* NO_SIG/MD5_SIG/SHA1_SIG */
897 /* Pool record -- same format as database */
900 char Name[MAX_NAME_LENGTH]; /* Pool name */
901 uint32_t NumVols; /* total number of volumes */
902 uint32_t MaxVols; /* max allowed volumes */
903 int32_t LabelType; /* Bacula/ANSI/IBM */
904 int32_t UseOnce; /* set to use once only */
905 int32_t UseCatalog; /* set to use catalog */
906 int32_t AcceptAnyVolume; /* set to accept any volume sequence */
907 int32_t AutoPrune; /* set to prune automatically */
908 int32_t Recycle; /* default Vol recycle flag */
909 utime_t VolRetention; /* retention period in seconds */
910 utime_t VolUseDuration; /* time in secs volume can be used */
911 uint32_t MaxVolJobs; /* Max Jobs on Volume */
912 uint32_t MaxVolFiles; /* Max files on Volume */
913 uint64_t MaxVolBytes; /* Max bytes on Volume */
914 DBId_t RecyclePoolId; /* RecyclePool destination when media is purged */
915 char PoolType[MAX_NAME_LENGTH];
916 char LabelFormat[MAX_NAME_LENGTH];
917 /* Extra stuff not in DB */
924 char Name[MAX_NAME_LENGTH]; /* Device name */
925 DBId_t MediaTypeId; /* MediaType */
926 DBId_t StorageId; /* Storage id if autochanger */
927 uint32_t DevMounts; /* Number of times mounted */
928 uint32_t DevErrors; /* Number of read/write errors */
929 uint64_t DevReadBytes; /* Number of bytes read */
930 uint64_t DevWriteBytes; /* Number of bytew written */
931 uint64_t DevReadTime; /* time spent reading volume */
932 uint64_t DevWriteTime; /* time spent writing volume */
933 uint64_t DevReadTimeSincCleaning; /* read time since cleaning */
934 uint64_t DevWriteTimeSincCleaning; /* write time since cleaning */
935 time_t CleaningDate; /* time last cleaned */
936 utime_t CleaningPeriod; /* time between cleanings */
942 char Name[MAX_NAME_LENGTH]; /* Device name */
943 int AutoChanger; /* Set if autochanger */
945 /* Not in database */
946 bool created; /* set if created by db_create ... */
949 class MEDIATYPE_DBR {
952 char MediaType[MAX_NAME_LENGTH]; /* MediaType string */
953 int ReadOnly; /* Set if read-only */
957 /* Media record -- same as the database */
959 DBId_t MediaId; /* Unique volume id */
960 char VolumeName[MAX_NAME_LENGTH]; /* Volume name */
961 char MediaType[MAX_NAME_LENGTH]; /* Media type */
962 DBId_t PoolId; /* Pool id */
963 time_t FirstWritten; /* Time Volume first written this usage */
964 time_t LastWritten; /* Time Volume last written */
965 time_t LabelDate; /* Date/Time Volume labeled */
966 time_t InitialWrite; /* Date/Time Volume first written */
967 int32_t LabelType; /* Label (Bacula/ANSI/IBM) */
968 uint32_t VolJobs; /* number of jobs on this medium */
969 uint32_t VolFiles; /* Number of files */
970 uint32_t VolBlocks; /* Number of blocks */
971 uint32_t VolMounts; /* Number of times mounted */
972 uint32_t VolErrors; /* Number of read/write errors */
973 uint32_t VolWrites; /* Number of writes */
974 uint32_t VolReads; /* Number of reads */
975 uint64_t VolBytes; /* Number of bytes written */
976 uint32_t VolParts; /* Number of parts written */
977 uint64_t MaxVolBytes; /* Max bytes to write to Volume */
978 uint64_t VolCapacityBytes; /* capacity estimate */
979 uint64_t VolReadTime; /* time spent reading volume */
980 uint64_t VolWriteTime; /* time spent writing volume */
981 utime_t VolRetention; /* Volume retention in seconds */
982 utime_t VolUseDuration; /* time in secs volume can be used */
983 uint32_t MaxVolJobs; /* Max Jobs on Volume */
984 uint32_t MaxVolFiles; /* Max files on Volume */
985 int32_t Recycle; /* recycle yes/no */
986 int32_t Slot; /* slot in changer */
987 int32_t Enabled; /* 0=disabled, 1=enabled, 2=archived */
988 int32_t InChanger; /* Volume currently in changer */
989 DBId_t StorageId; /* Storage record Id */
990 uint32_t EndFile; /* Last file on volume */
991 uint32_t EndBlock; /* Last block on volume */
992 uint32_t RecycleCount; /* Number of times recycled */
993 char VolStatus[20]; /* Volume status */
994 DBId_t DeviceId; /* Device where Vol last written */
995 DBId_t LocationId; /* Where Volume is -- user defined */
996 DBId_t ScratchPoolId; /* Where to move if scratch */
997 DBId_t RecyclePoolId; /* Where to move when recycled */
998 /* Extra stuff not in DB */
999 faddr_t rec_addr; /* found record address */
1000 /* Since the database returns times as strings, this is how we pass
1003 char cFirstWritten[MAX_TIME_LENGTH]; /* FirstWritten returned from DB */
1004 char cLastWritten[MAX_TIME_LENGTH]; /* LastWritten returned from DB */
1005 char cLabelDate[MAX_TIME_LENGTH]; /* LabelData returned from DB */
1006 char cInitialWrite[MAX_TIME_LENGTH]; /* InitialWrite returned from DB */
1007 bool set_first_written;
1008 bool set_label_date;
1011 /* Client record -- same as the database */
1013 DBId_t ClientId; /* Unique Client id */
1015 utime_t FileRetention;
1016 utime_t JobRetention;
1017 char Name[MAX_NAME_LENGTH]; /* Client name */
1018 char Uname[256]; /* Uname for client */
1021 /* Counter record as in database */
1022 struct COUNTER_DBR {
1023 char Counter[MAX_NAME_LENGTH];
1026 int32_t CurrentValue;
1027 char WrapCounter[MAX_NAME_LENGTH];
1031 /* FileSet record -- same as the database */
1032 struct FILESET_DBR {
1033 DBId_t FileSetId; /* Unique FileSet id */
1034 char FileSet[MAX_NAME_LENGTH]; /* FileSet name */
1035 char MD5[50]; /* MD5 signature of include/exclude */
1036 time_t CreateTime; /* date created */
1038 * This is where we return CreateTime
1040 char cCreateTime[MAX_TIME_LENGTH]; /* CreateTime as returned from DB */
1041 /* Not in DB but returned by db_create_fileset() */
1042 bool created; /* set when record newly created */
1045 /* Call back context for getting a 32/64 bit value from the database */
1046 struct db_int64_ctx {
1047 int64_t value; /* value returned */
1048 int count; /* number of values seen */
1054 #include "sql_cmds.h"
1057 * Exported globals from sql.c
1059 extern int DLL_IMP_EXP db_type; /* SQL engine type index */
1062 * Some functions exported by sql.c for use within the
1065 void list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type type);
1066 void list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx);
1067 int get_sql_record_max(JCR *jcr, B_DB *mdb);
1068 bool check_tables_version(JCR *jcr, B_DB *mdb);
1069 void _db_unlock(const char *file, int line, B_DB *mdb);
1070 void _db_lock(const char *file, int line, B_DB *mdb);
1071 const char *db_get_type(void);
1073 void print_dashes(B_DB *mdb);
1074 void print_result(B_DB *mdb);
1075 int QueryDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
1076 int InsertDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
1077 int DeleteDB(const char *file, int line, JCR *jcr, B_DB *db, char *delete_cmd);
1078 int UpdateDB(const char *file, int line, JCR *jcr, B_DB *db, char *update_cmd);
1079 void split_path_and_file(JCR *jcr, B_DB *mdb, const char *fname);
1080 #endif /* __SQL_H_ */