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 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.
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,
79 typedef void (DB_LIST_HANDLER)(void *, const char *);
80 typedef int (DB_RESULT_HANDLER)(void *, int, char **);
82 #define db_lock(mdb) _db_lock(__FILE__, __LINE__, mdb)
83 #define db_unlock(mdb) _db_unlock(__FILE__, __LINE__, mdb)
87 #if defined(BUILDING_CATS)
90 #define BDB_VERSION 10
94 /* Define opaque structure for sqlite */
99 #define IS_NUM(x) ((x) == 1)
100 #define IS_NOT_NULL(x) ((x) == 1)
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 */
111 * This is the "real" definition that should only be
112 * used inside sql.c and associated database interface
117 BQUEUE bq; /* queue control */
118 brwlock_t lock; /* transaction lock */
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 */
131 char *db_address; /* host name address */
132 char *db_socket; /* socket for local access */
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 */
157 * "Generic" names for easier conversion
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))
166 #define sql_insert_id(x,y) sqlite3_last_insert_rowid((x)->db)
167 #define sql_close(x) sqlite3_close((x)->db)
169 #define sql_insert_id(x,y) sqlite_last_insert_rowid((x)->db)
170 #define sql_close(x) sqlite_close((x)->db)
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**
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
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;
210 #define BDB_VERSION 10
214 /* Define opaque structure for sqlite */
219 #define IS_NUM(x) ((x) == 1)
220 #define IS_NOT_NULL(x) ((x) == 1)
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 */
231 * This is the "real" definition that should only be
232 * used inside sql.c and associated database interface
237 BQUEUE bq; /* queue control */
238 brwlock_t lock; /* transaction lock */
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 */
251 char *db_address; /* host name address */
252 char *db_socket; /* socket for local access */
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 */
276 * Conversion of sqlite 2 names to sqlite3
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
288 * "Generic" names for easier conversion
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))
297 #define sql_insert_id(x,y) sqlite3_last_insert_rowid((x)->db)
298 #define sql_close(x) sqlite3_close((x)->db)
300 #define sql_insert_id(x,y) sqlite_last_insert_rowid((x)->db)
301 #define sql_close(x) sqlite_close((x)->db)
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
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;
336 #define BDB_VERSION 10
341 * This is the "real" definition that should only be
342 * used inside sql.c and associated database interface
348 BQUEUE bq; /* queue control */
349 brwlock_t lock; /* transaction lock */
354 my_ulonglong num_rows;
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() */
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 */
378 #define DB_STATUS int
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
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
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);
416 #ifdef HAVE_POSTGRESQL
418 #define BDB_VERSION 10
420 #include <libpq-fe.h>
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)
426 typedef char **POSTGRESQL_ROW;
427 typedef struct pg_field {
431 unsigned int flags; // 1 == not null
436 * This is the "real" definition that should only be
437 * used inside sql.c and associated database interface
440 * P O S T G R E S Q L
443 BQUEUE bq; /* queue control */
444 brwlock_t lock; /* transaction lock */
449 POSTGRESQL_FIELD *fields;
451 int row_size; /* size of malloced rows */
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 */
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() */
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 */
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, const char *table_name);
486 void my_postgresql_field_seek (B_DB *mdb, int row);
487 POSTGRESQL_FIELD * my_postgresql_fetch_field(B_DB *mdb);
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);
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;
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)
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
525 #define SQL_ROW POSTGRESQL_ROW
526 #define SQL_FIELD POSTGRESQL_FIELD
532 #define BDB_VERSION 10
536 #ifdef HAVE_BATCH_FILE_INSERT
537 #include <dbi/dbi-dev.h>
538 #endif //HAVE_BATCH_FILE_INSERT
540 #define IS_NUM(x) ((x) == 1 || (x) == 2 )
541 #define IS_NOT_NULL(x) ((x) == (1 << 0))
543 typedef char **DBI_ROW;
544 typedef struct dbi_field {
548 unsigned int flags; // 1 == not null
551 typedef struct dbi_field_get {
557 * This is the "real" definition that should only be
558 * used inside sql.c and associated database interface
564 BQUEUE bq; /* queue control */
565 brwlock_t lock; /* transaction lock */
569 dbi_error_flag status;
572 DBI_FIELD_GET *field_get;
574 int row_size; /* size of malloced rows */
576 int fields_size; /* size of malloced fields */
577 int row_number; /* row number from my_postgresql_data_seek */
578 int field_number; /* field number from my_postgresql_field_seek */
580 int db_type; /* DBI driver defined */
581 char *db_driverdir ; /* DBI driver dir */
582 char *db_driver; /* DBI type database */
586 char *db_address; /* host address */
587 char *db_socket; /* socket for local access */
588 int db_port; /* port of host address */
589 int have_insert_id; /* do have insert_id() */
591 POOLMEM *errmsg; /* nicely edited error message */
592 POOLMEM *cmd; /* SQL command string */
593 POOLMEM *cached_path;
594 int cached_path_len; /* length of cached path */
595 uint32_t cached_path_id;
596 bool allow_transactions; /* transactions allowed */
597 bool transaction; /* transaction started */
598 int changes; /* changes made to db */
599 POOLMEM *fname; /* Filename only */
600 POOLMEM *path; /* Path only */
601 POOLMEM *esc_name; /* Escaped file name */
602 POOLMEM *esc_path; /* Escaped path name */
603 int fnl; /* file name length */
604 int pnl; /* path name length */
607 void my_dbi_free_result(B_DB *mdb);
608 DBI_ROW my_dbi_fetch_row (B_DB *mdb);
609 int my_dbi_query (B_DB *mdb, const char *query);
610 void my_dbi_data_seek (B_DB *mdb, int row);
611 void my_dbi_field_seek (B_DB *mdb, int row);
612 DBI_FIELD * my_dbi_fetch_field(B_DB *mdb);
613 const char * my_dbi_strerror (B_DB *mdb);
614 int my_dbi_getisnull (dbi_result *result, int row_number, int column_number);
615 char * my_dbi_getvalue (dbi_result *result, int row_number, unsigned int column_number);
616 //int my_dbi_getvalue (dbi_result *result, int row_number, unsigned int column_number, char *value);
617 int my_dbi_sql_insert_id(B_DB *mdb, char *table_name);
619 int my_dbi_batch_start(JCR *jcr, B_DB *mdb);
620 int my_dbi_batch_end(JCR *jcr, B_DB *mdb, const char *error);
621 typedef struct ATTR_DBR ATTR_DBR;
622 int my_dbi_batch_insert(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
623 char *my_postgresql_copy_escape(char *dest, char *src, size_t len);
624 // typedefs for libdbi work with postgresql copy insert
625 typedef int (*custom_function_insert_t)(void*, const char*, int);
626 typedef char* (*custom_function_error_t)(void*);
627 typedef int (*custom_function_end_t)(void*, const char*);
629 extern const char* my_dbi_batch_lock_path_query[3];
630 extern const char* my_dbi_batch_lock_filename_query[3];
631 extern const char* my_dbi_batch_unlock_tables_query[3];
632 extern const char* my_dbi_batch_fill_filename_query[3];
633 extern const char* my_dbi_batch_fill_path_query[3];
635 /* "Generic" names for easier conversion */
636 #define sql_store_result(x) (x)->result
637 #define sql_free_result(x) my_dbi_free_result(x)
638 #define sql_fetch_row(x) my_dbi_fetch_row(x)
639 #define sql_query(x, y) my_dbi_query((x), (y))
640 #define sql_close(x) dbi_conn_close((x)->db)
641 #define sql_strerror(x) my_dbi_strerror(x)
642 #define sql_num_rows(x) dbi_result_get_numrows((x)->result)
643 #define sql_data_seek(x, i) my_dbi_data_seek((x), (i))
644 /* #define sql_affected_rows(x) dbi_result_get_numrows_affected((x)->result) */
645 #define sql_affected_rows(x) 1
646 #define sql_insert_id(x,y) my_dbi_sql_insert_id((x), (y))
647 #define sql_field_seek(x, y) my_dbi_field_seek((x), (y))
648 #define sql_fetch_field(x) my_dbi_fetch_field(x)
649 #define sql_num_fields(x) ((x)->num_fields)
650 #define sql_batch_start(x,y) my_dbi_batch_start(x,y)
651 #define sql_batch_end(x,y,z) my_dbi_batch_end(x,y,z)
652 #define sql_batch_insert(x,y,z) my_dbi_batch_insert(x,y,z)
653 #define sql_batch_lock_path_query my_dbi_batch_lock_path_query[db_type]
654 #define sql_batch_lock_filename_query my_dbi_batch_lock_filename_query[db_type]
655 #define sql_batch_unlock_tables_query my_dbi_batch_unlock_tables_query[db_type]
656 #define sql_batch_fill_filename_query my_dbi_batch_fill_filename_query[db_type]
657 #define sql_batch_fill_path_query my_dbi_batch_fill_path_query[db_type]
659 #define SQL_ROW DBI_ROW
660 #define SQL_FIELD DBI_FIELD
663 #else /* USE BACULA DB routines */
665 #define HAVE_BACULA_DB 1
667 /* Change this each time there is some incompatible
668 * file format change!!!!
670 #define BDB_VERSION 13 /* file version number */
673 int bdb_version; /* Version number */
674 uint32_t JobId; /* next Job Id */
675 uint32_t PoolId; /* next Pool Id */
676 uint32_t MediaId; /* next Media Id */
677 uint32_t JobMediaId; /* next JobMedia Id */
678 uint32_t ClientId; /* next Client Id */
679 uint32_t FileSetId; /* nest FileSet Id */
680 time_t time; /* time file written */
684 /* This is the REAL definition for using the
688 BQUEUE bq; /* queue control */
689 /* pthread_mutex_t mutex; */ /* single thread lock */
690 brwlock_t lock; /* transaction lock */
691 int ref_count; /* number of times opened */
692 struct s_control control; /* control file structure */
693 int cfd; /* control file device */
694 FILE *jobfd; /* Jobs records file descriptor */
695 FILE *poolfd; /* Pool records fd */
696 FILE *mediafd; /* Media records fd */
697 FILE *jobmediafd; /* JobMedia records fd */
698 FILE *clientfd; /* Client records fd */
699 FILE *filesetfd; /* FileSet records fd */
700 char *db_name; /* name of database */
701 POOLMEM *errmsg; /* nicely edited error message */
702 POOLMEM *cmd; /* Command string */
703 POOLMEM *cached_path;
704 int cached_path_len; /* length of cached path */
705 uint32_t cached_path_id;
708 #endif /* HAVE_SQLITE3 */
709 #endif /* HAVE_MYSQL */
710 #endif /* HAVE_SQLITE */
711 #endif /* HAVE_POSTGRESQL */
712 #endif /* HAVE_DBI */
715 /* Use for better error location printing */
716 #define UPDATE_DB(jcr, db, cmd) UpdateDB(__FILE__, __LINE__, jcr, db, cmd)
717 #define INSERT_DB(jcr, db, cmd) InsertDB(__FILE__, __LINE__, jcr, db, cmd)
718 #define QUERY_DB(jcr, db, cmd) QueryDB(__FILE__, __LINE__, jcr, db, cmd)
719 #define DELETE_DB(jcr, db, cmd) DeleteDB(__FILE__, __LINE__, jcr, db, cmd)
722 #else /* not __SQL_C */
724 /* This is a "dummy" definition for use outside of sql.c
727 int dummy; /* for SunOS compiler */
732 /* ==============================================================
734 * What follows are definitions that are used "globally" for all
735 * the different SQL engines and both inside and external to the
739 extern uint32_t bacula_db_version;
742 * These are the sizes of the current definitions of database
743 * Ids. In general, FileId_t can be set to uint64_t and it
744 * *should* work. Users have reported back that it does work
745 * for PostgreSQL. For the other types, all places in Bacula
746 * have been converted, but no one has actually tested it.
747 * In principle, the only field that really should need to be
748 * 64 bits is the FileId_t
750 typedef uint32_t FileId_t;
751 typedef uint32_t DBId_t; /* general DB id type */
752 typedef uint32_t JobId_t;
757 * Structure used when calling db_get_query_ids()
758 * allows the subroutine to return a list of ids.
760 class dbid_list : public SMARTALLOC {
762 DBId_t *DBId; /* array of DBIds */
763 char *PurgedFiles; /* Array of PurgedFile flags */
764 int num_ids; /* num of ids actually stored */
765 int max_ids; /* size of id array */
766 int num_seen; /* number of ids processed */
767 int tot_ids; /* total to process */
769 dbid_list(); /* in sql.c */
770 ~dbid_list(); /* in sql.c */
776 /* Job information passed to create job record and update
777 * job record at end of job. Note, although this record
778 * contains all the fields found in the Job database record,
779 * it also contains fields found in the JobMedia record.
784 char Job[MAX_NAME_LENGTH]; /* Job unique name */
785 char Name[MAX_NAME_LENGTH]; /* Job base name */
786 int JobType; /* actually char(1) */
787 int JobLevel; /* actually char(1) */
788 int JobStatus; /* actually char(1) */
789 DBId_t ClientId; /* Id of client */
790 DBId_t PoolId; /* Id of pool */
791 DBId_t FileSetId; /* Id of FileSet */
792 DBId_t PriorJobId; /* Id of migrated (prior) job */
793 time_t SchedTime; /* Time job scheduled */
794 time_t StartTime; /* Job start time */
795 time_t EndTime; /* Job termination time of orig job */
796 time_t RealEndTime; /* Job termination time of this job */
797 utime_t JobTDate; /* Backup time/date in seconds */
798 uint32_t VolSessionId;
799 uint32_t VolSessionTime;
802 uint32_t JobMissingFiles;
807 /* Note, FirstIndex, LastIndex, Start/End File and Block
808 * are only used in the JobMedia record.
810 uint32_t FirstIndex; /* First index this Volume */
811 uint32_t LastIndex; /* Last index this Volume */
817 char cSchedTime[MAX_TIME_LENGTH];
818 char cStartTime[MAX_TIME_LENGTH];
819 char cEndTime[MAX_TIME_LENGTH];
820 char cRealEndTime[MAX_TIME_LENGTH];
821 /* Extra stuff not in DB */
822 int limit; /* limit records to display */
826 /* Job Media information used to create the media records
827 * for each Volume used for the job.
829 /* JobMedia record */
830 struct JOBMEDIA_DBR {
831 DBId_t JobMediaId; /* record id */
832 JobId_t JobId; /* JobId */
833 DBId_t MediaId; /* MediaId */
834 uint32_t FirstIndex; /* First index this Volume */
835 uint32_t LastIndex; /* Last index this Volume */
836 uint32_t StartFile; /* File for start of data */
837 uint32_t EndFile; /* End file on Volume */
838 uint32_t StartBlock; /* start block on tape */
839 uint32_t EndBlock; /* last block */
840 uint32_t Copy; /* identical copy */
844 /* Volume Parameter structure */
846 char VolumeName[MAX_NAME_LENGTH]; /* Volume name */
847 char MediaType[MAX_NAME_LENGTH]; /* Media Type */
848 char Storage[MAX_NAME_LENGTH]; /* Storage name */
849 uint32_t VolIndex; /* Volume seqence no. */
850 uint32_t FirstIndex; /* First index this Volume */
851 uint32_t LastIndex; /* Last index this Volume */
852 uint32_t StartFile; /* File for start of data */
853 uint32_t EndFile; /* End file on Volume */
854 uint32_t StartBlock; /* start block on tape */
855 uint32_t EndBlock; /* last block */
856 int32_t Slot; /* Slot */
857 // uint32_t Copy; /* identical copy */
858 // uint32_t Stripe; /* RAIT strip number */
862 /* Attributes record -- NOT same as in database because
863 * in general, this "record" creates multiple database
864 * records (e.g. pathname, filename, fileattributes).
867 char *fname; /* full path & filename */
868 char *link; /* link if any */
869 char *attr; /* attributes statp */
882 /* File record -- same format as database */
891 char Digest[BASE64_SIZE(CRYPTO_DIGEST_MAX_SIZE)];
892 int DigestType; /* NO_SIG/MD5_SIG/SHA1_SIG */
895 /* Pool record -- same format as database */
898 char Name[MAX_NAME_LENGTH]; /* Pool name */
899 uint32_t NumVols; /* total number of volumes */
900 uint32_t MaxVols; /* max allowed volumes */
901 int32_t LabelType; /* Bacula/ANSI/IBM */
902 int32_t UseOnce; /* set to use once only */
903 int32_t UseCatalog; /* set to use catalog */
904 int32_t AcceptAnyVolume; /* set to accept any volume sequence */
905 int32_t AutoPrune; /* set to prune automatically */
906 int32_t Recycle; /* default Vol recycle flag */
907 utime_t VolRetention; /* retention period in seconds */
908 utime_t VolUseDuration; /* time in secs volume can be used */
909 uint32_t MaxVolJobs; /* Max Jobs on Volume */
910 uint32_t MaxVolFiles; /* Max files on Volume */
911 uint64_t MaxVolBytes; /* Max bytes on Volume */
912 DBId_t RecyclePoolId; /* RecyclePool destination when media is purged */
913 char PoolType[MAX_NAME_LENGTH];
914 char LabelFormat[MAX_NAME_LENGTH];
915 /* Extra stuff not in DB */
922 char Name[MAX_NAME_LENGTH]; /* Device name */
923 DBId_t MediaTypeId; /* MediaType */
924 DBId_t StorageId; /* Storage id if autochanger */
925 uint32_t DevMounts; /* Number of times mounted */
926 uint32_t DevErrors; /* Number of read/write errors */
927 uint64_t DevReadBytes; /* Number of bytes read */
928 uint64_t DevWriteBytes; /* Number of bytew written */
929 uint64_t DevReadTime; /* time spent reading volume */
930 uint64_t DevWriteTime; /* time spent writing volume */
931 uint64_t DevReadTimeSincCleaning; /* read time since cleaning */
932 uint64_t DevWriteTimeSincCleaning; /* write time since cleaning */
933 time_t CleaningDate; /* time last cleaned */
934 utime_t CleaningPeriod; /* time between cleanings */
940 char Name[MAX_NAME_LENGTH]; /* Device name */
941 int AutoChanger; /* Set if autochanger */
943 /* Not in database */
944 bool created; /* set if created by db_create ... */
947 class MEDIATYPE_DBR {
950 char MediaType[MAX_NAME_LENGTH]; /* MediaType string */
951 int ReadOnly; /* Set if read-only */
955 /* Media record -- same as the database */
957 DBId_t MediaId; /* Unique volume id */
958 char VolumeName[MAX_NAME_LENGTH]; /* Volume name */
959 char MediaType[MAX_NAME_LENGTH]; /* Media type */
960 DBId_t PoolId; /* Pool id */
961 time_t FirstWritten; /* Time Volume first written this usage */
962 time_t LastWritten; /* Time Volume last written */
963 time_t LabelDate; /* Date/Time Volume labeled */
964 time_t InitialWrite; /* Date/Time Volume first written */
965 int32_t LabelType; /* Label (Bacula/ANSI/IBM) */
966 uint32_t VolJobs; /* number of jobs on this medium */
967 uint32_t VolFiles; /* Number of files */
968 uint32_t VolBlocks; /* Number of blocks */
969 uint32_t VolMounts; /* Number of times mounted */
970 uint32_t VolErrors; /* Number of read/write errors */
971 uint32_t VolWrites; /* Number of writes */
972 uint32_t VolReads; /* Number of reads */
973 uint64_t VolBytes; /* Number of bytes written */
974 uint32_t VolParts; /* Number of parts written */
975 uint64_t MaxVolBytes; /* Max bytes to write to Volume */
976 uint64_t VolCapacityBytes; /* capacity estimate */
977 uint64_t VolReadTime; /* time spent reading volume */
978 uint64_t VolWriteTime; /* time spent writing volume */
979 utime_t VolRetention; /* Volume retention in seconds */
980 utime_t VolUseDuration; /* time in secs volume can be used */
981 uint32_t MaxVolJobs; /* Max Jobs on Volume */
982 uint32_t MaxVolFiles; /* Max files on Volume */
983 int32_t Recycle; /* recycle yes/no */
984 int32_t Slot; /* slot in changer */
985 int32_t Enabled; /* 0=disabled, 1=enabled, 2=archived */
986 int32_t InChanger; /* Volume currently in changer */
987 DBId_t StorageId; /* Storage record Id */
988 uint32_t EndFile; /* Last file on volume */
989 uint32_t EndBlock; /* Last block on volume */
990 uint32_t RecycleCount; /* Number of times recycled */
991 char VolStatus[20]; /* Volume status */
992 DBId_t DeviceId; /* Device where Vol last written */
993 DBId_t LocationId; /* Where Volume is -- user defined */
994 DBId_t ScratchPoolId; /* Where to move if scratch */
995 DBId_t RecyclePoolId; /* Where to move when recycled */
996 /* Extra stuff not in DB */
997 faddr_t rec_addr; /* found record address */
998 /* Since the database returns times as strings, this is how we pass
1001 char cFirstWritten[MAX_TIME_LENGTH]; /* FirstWritten returned from DB */
1002 char cLastWritten[MAX_TIME_LENGTH]; /* LastWritten returned from DB */
1003 char cLabelDate[MAX_TIME_LENGTH]; /* LabelData returned from DB */
1004 char cInitialWrite[MAX_TIME_LENGTH]; /* InitialWrite returned from DB */
1005 bool set_first_written;
1006 bool set_label_date;
1009 /* Client record -- same as the database */
1011 DBId_t ClientId; /* Unique Client id */
1013 utime_t FileRetention;
1014 utime_t JobRetention;
1015 char Name[MAX_NAME_LENGTH]; /* Client name */
1016 char Uname[256]; /* Uname for client */
1019 /* Counter record as in database */
1020 struct COUNTER_DBR {
1021 char Counter[MAX_NAME_LENGTH];
1024 int32_t CurrentValue;
1025 char WrapCounter[MAX_NAME_LENGTH];
1029 /* FileSet record -- same as the database */
1030 struct FILESET_DBR {
1031 DBId_t FileSetId; /* Unique FileSet id */
1032 char FileSet[MAX_NAME_LENGTH]; /* FileSet name */
1033 char MD5[50]; /* MD5 signature of include/exclude */
1034 time_t CreateTime; /* date created */
1036 * This is where we return CreateTime
1038 char cCreateTime[MAX_TIME_LENGTH]; /* CreateTime as returned from DB */
1039 /* Not in DB but returned by db_create_fileset() */
1040 bool created; /* set when record newly created */
1043 /* Call back context for getting a 32/64 bit value from the database */
1044 struct db_int64_ctx {
1045 int64_t value; /* value returned */
1046 int count; /* number of values seen */
1052 #include "sql_cmds.h"
1055 * Exported globals from sql.c
1057 extern int DLL_IMP_EXP db_type; /* SQL engine type index */
1060 * Some functions exported by sql.c for use within the
1063 void list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type type);
1064 void list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx);
1065 int get_sql_record_max(JCR *jcr, B_DB *mdb);
1066 bool check_tables_version(JCR *jcr, B_DB *mdb);
1067 void _db_unlock(const char *file, int line, B_DB *mdb);
1068 void _db_lock(const char *file, int line, B_DB *mdb);
1069 const char *db_get_type(void);
1071 void print_dashes(B_DB *mdb);
1072 void print_result(B_DB *mdb);
1073 int QueryDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
1074 int InsertDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
1075 int DeleteDB(const char *file, int line, JCR *jcr, B_DB *db, char *delete_cmd);
1076 int UpdateDB(const char *file, int line, JCR *jcr, B_DB *db, char *update_cmd);
1077 void split_path_and_file(JCR *jcr, B_DB *mdb, const char *fname);
1078 #endif /* __SQL_H_ */