2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2010 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
44 Here is how database versions work.
46 While I am working on a new release with database changes, the
47 update scripts are in the src/cats directory under the names
48 update_xxx_tables.in. Most of the time, I make database updates
49 in one go and immediately update the version, but not always. If
50 there are going to be several updates as is the case with version
51 1.37, then I will often forgo changing the version until the last
52 update otherwise I will end up with too many versions and a lot
55 When I am pretty sure there will be no more updates, I will
56 change the version from 8 to 9 (in the present case), and when I
57 am 100% sure there will be no more changes, the update script
58 will be copied to the updatedb directory with the correct name
59 (in the present case 8 to 9).
61 Now, in principle, each of the different DB implementations
62 can have a different version, but in practice they are all
63 the same (simplifies things). The exception is the internal
64 database, which is no longer used, and hence, no longer changes.
73 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)
90 #error "SQLite2 is now deprecated, use SQLite3 instead."
92 #define BDB_VERSION 12
96 /* Define opaque structure for sqlite */
101 #define IS_NUM(x) ((x) == 1)
102 #define IS_NOT_NULL(x) ((x) == 1)
104 typedef struct s_sql_field {
105 char *name; /* name of column */
106 int length; /* length */
107 int max_length; /* max length */
108 uint32_t type; /* type */
109 uint32_t flags; /* flags */
113 * This is the "real" definition that should only be
114 * used inside sql.c and associated database interface
119 dlink link; /* queue control */
120 brwlock_t lock; /* transaction lock */
124 int nrow; /* nrow returned from sqlite */
125 int ncolumn; /* ncolum returned from sqlite */
126 int num_rows; /* used by code */
127 int row; /* seek row */
128 int field; /* seek field */
129 SQL_FIELD **fields; /* defined fields */
133 char *db_address; /* host name address */
134 char *db_socket; /* socket for local access */
136 int db_port; /* port for host name address */
137 bool connected; /* connection made to db */
138 bool fields_defined; /* set when fields defined */
139 char *sqlite_errmsg; /* error message returned by sqlite */
140 POOLMEM *errmsg; /* nicely edited error message */
141 POOLMEM *cmd; /* SQL command string */
142 POOLMEM *cached_path; /* cached path name */
143 int cached_path_len; /* length of cached path */
144 uint32_t cached_path_id; /* cached path id */
145 bool allow_transactions; /* transactions allowed */
146 bool transaction; /* transaction started */
147 int changes; /* changes during transaction */
148 POOLMEM *fname; /* Filename only */
149 POOLMEM *path; /* Path only */
150 POOLMEM *esc_name; /* Escaped file name */
151 POOLMEM *esc_path; /* Escaped path name */
152 POOLMEM *esc_obj; /* Escaped restore object */
153 int fnl; /* file name length */
154 int pnl; /* path name length */
159 * "Generic" names for easier conversion
163 #define sql_store_result(x) (x)->result
164 #define sql_free_result(x) my_sqlite_free_table(x)
165 #define sql_fetch_row(x) my_sqlite_fetch_row(x)
166 #define sql_query(x, y) my_sqlite_query((x), (y))
167 #define sql_insert_autokey_record(x, y, z) my_sqlite_insert_autokey_record((x), (y), (z))
169 #define sql_close(x) sqlite3_close((x)->db)
170 #define sql_affected_rows(x) sqlite3_changes((x)->db)
172 #define sql_close(x) sqlite_close((x)->db)
173 #define sql_affected_rows(x) 1
175 #define sql_strerror(x) (x)->sqlite_errmsg?(x)->sqlite_errmsg:"unknown"
176 #define sql_num_rows(x) (x)->nrow
177 #define sql_data_seek(x, i) (x)->row = (i)
178 #define sql_field_seek(x, y) my_sqlite_field_seek((x), (y))
179 #define sql_fetch_field(x) my_sqlite_fetch_field(x)
180 #define sql_num_fields(x) ((x)->ncolumn)
181 #define SQL_ROW char**
182 #define SQL_MATCH "MATCH"
184 #define sql_batch_start(x,y) my_batch_start(x,y)
185 #define sql_batch_end(x,y,z) my_batch_end(x,y,z)
186 #define sql_batch_insert(x,y,z) my_batch_insert(x,y,z)
187 #define sql_batch_lock_path_query my_sqlite_batch_lock_query
188 #define sql_batch_lock_filename_query my_sqlite_batch_lock_query
189 #define sql_batch_unlock_tables_query my_sqlite_batch_unlock_query
190 #define sql_batch_fill_filename_query my_sqlite_batch_fill_filename_query
191 #define sql_batch_fill_path_query my_sqlite_batch_fill_path_query
193 /* In cats/sqlite.c */
194 void my_sqlite_free_table(B_DB *mdb);
195 SQL_ROW my_sqlite_fetch_row(B_DB *mdb);
196 int my_sqlite_query(B_DB *mdb, const char *cmd);
197 void my_sqlite_field_seek(B_DB *mdb, int field);
198 SQL_FIELD *my_sqlite_fetch_field(B_DB *mdb);
199 int my_sqlite_insert_autokey_record(B_DB *mdb, const char *query, const char *table_name);
200 extern const char* my_sqlite_batch_lock_query;
201 extern const char* my_sqlite_batch_unlock_query;
202 extern const char* my_sqlite_batch_fill_filename_query;
203 extern const char* my_sqlite_batch_fill_path_query;
214 #define BDB_VERSION 12
218 /* Define opaque structure for sqlite */
223 #define IS_NUM(x) ((x) == 1)
224 #define IS_NOT_NULL(x) ((x) == 1)
226 typedef struct s_sql_field {
227 char *name; /* name of column */
228 int length; /* length */
229 int max_length; /* max length */
230 uint32_t type; /* type */
231 uint32_t flags; /* flags */
235 * This is the "real" definition that should only be
236 * used inside sql.c and associated database interface
241 dlink link; /* queue control */
242 brwlock_t lock; /* transaction lock */
246 int nrow; /* nrow returned from sqlite */
247 int ncolumn; /* ncolum returned from sqlite */
248 int num_rows; /* used by code */
249 int row; /* seek row */
250 int field; /* seek field */
251 SQL_FIELD **fields; /* defined fields */
255 char *db_address; /* host name address */
256 char *db_socket; /* socket for local access */
258 int db_port; /* port for host name address */
259 bool connected; /* connection made to db */
260 bool fields_defined; /* set when fields defined */
261 char *sqlite_errmsg; /* error message returned by sqlite */
262 POOLMEM *errmsg; /* nicely edited error message */
263 POOLMEM *cmd; /* SQL command string */
264 POOLMEM *cached_path; /* cached path name */
265 int cached_path_len; /* length of cached path */
266 uint32_t cached_path_id; /* cached path id */
267 bool allow_transactions; /* transactions allowed */
268 bool transaction; /* transaction started */
269 int changes; /* changes during transaction */
270 POOLMEM *fname; /* Filename only */
271 POOLMEM *path; /* Path only */
272 POOLMEM *esc_name; /* Escaped file name */
273 POOLMEM *esc_path; /* Escaped path name */
274 POOLMEM *esc_obj; /* Escaped restore object */
275 int fnl; /* file name length */
276 int pnl; /* path name length */
280 * Conversion of sqlite 2 names to sqlite3
282 #define sqlite_last_insert_rowid sqlite3_last_insert_rowid
283 #define sqlite_open sqlite3_open
284 #define sqlite_close sqlite3_close
285 #define sqlite_result sqlite3_result
286 #define sqlite_exec sqlite3_exec
287 #define sqlite_get_table sqlite3_get_table
288 #define sqlite_free_table sqlite3_free_table
292 * "Generic" names for easier conversion
296 #define sql_store_result(x) (x)->result
297 #define sql_free_result(x) my_sqlite_free_table(x)
298 #define sql_fetch_row(x) my_sqlite_fetch_row(x)
299 #define sql_query(x, y) my_sqlite_query((x), (y))
300 #define sql_insert_autokey_record(x, y, z) my_sqlite_insert_autokey_record((x), (y), (z))
302 #define sql_close(x) sqlite3_close((x)->db)
304 #define sql_close(x) sqlite_close((x)->db)
306 #define sql_strerror(x) (x)->sqlite_errmsg?(x)->sqlite_errmsg:"unknown"
307 #define sql_num_rows(x) (x)->nrow
308 #define sql_data_seek(x, i) (x)->row = (i)
309 #define sql_affected_rows(x) sqlite3_changes((x)->db)
310 #define sql_field_seek(x, y) my_sqlite_field_seek((x), (y))
311 #define sql_fetch_field(x) my_sqlite_fetch_field(x)
312 #define sql_num_fields(x) ((x)->ncolumn)
313 #define sql_batch_start(x,y) my_batch_start(x,y)
314 #define sql_batch_end(x,y,z) my_batch_end(x,y,z)
315 #define sql_batch_insert(x,y,z) my_batch_insert(x,y,z)
316 #define SQL_ROW char**
317 #define SQL_MATCH "MATCH"
318 #define sql_batch_lock_path_query my_sqlite_batch_lock_query
319 #define sql_batch_lock_filename_query my_sqlite_batch_lock_query
320 #define sql_batch_unlock_tables_query my_sqlite_batch_unlock_query
321 #define sql_batch_fill_filename_query my_sqlite_batch_fill_filename_query
322 #define sql_batch_fill_path_query my_sqlite_batch_fill_path_query
324 /* In cats/sqlite.c */
325 void my_sqlite_free_table(B_DB *mdb);
326 SQL_ROW my_sqlite_fetch_row(B_DB *mdb);
327 int my_sqlite_query(B_DB *mdb, const char *cmd);
328 void my_sqlite_field_seek(B_DB *mdb, int field);
329 SQL_FIELD *my_sqlite_fetch_field(B_DB *mdb);
330 int my_sqlite_insert_autokey_record(B_DB *mdb, const char *query, const char *table_name);
331 extern const char* my_sqlite_batch_lock_query;
332 extern const char* my_sqlite_batch_unlock_query;
333 extern const char* my_sqlite_batch_fill_filename_query;
334 extern const char* my_sqlite_batch_fill_path_query;
341 #define BDB_VERSION 12
346 * This is the "real" definition that should only be
347 * used inside sql.c and associated database interface
353 dlink link; /* queue control */
354 brwlock_t lock; /* transaction lock */
359 my_ulonglong num_rows;
364 char *db_address; /* host address */
365 char *db_socket; /* socket for local access */
366 int db_port; /* port of host address */
368 POOLMEM *errmsg; /* nicely edited error message */
369 POOLMEM *cmd; /* SQL command string */
370 POOLMEM *cached_path;
371 int cached_path_len; /* length of cached path */
372 uint32_t cached_path_id;
373 bool allow_transactions; /* transactions allowed */
374 int changes; /* changes made to db */
375 POOLMEM *fname; /* Filename only */
376 POOLMEM *path; /* Path only */
377 POOLMEM *esc_name; /* Escaped file name */
378 POOLMEM *esc_path; /* Escaped path name */
379 POOLMEM *esc_obj; /* Escaped restore object */
380 int fnl; /* file name length */
381 int pnl; /* path name length */
384 #define DB_STATUS int
386 /* "Generic" names for easier conversion */
387 #define sql_store_result(x) mysql_store_result((x)->db)
388 #define sql_use_result(x) mysql_use_result((x)->db)
389 #define sql_free_result(x) my_mysql_free_result(x)
390 #define sql_fetch_row(x) mysql_fetch_row((x)->result)
391 #define sql_query(x, y) mysql_query((x)->db, (y))
392 #define sql_strerror(x) mysql_error((x)->db)
393 #define sql_num_rows(x) mysql_num_rows((x)->result)
394 #define sql_data_seek(x, i) mysql_data_seek((x)->result, (i))
395 #define sql_affected_rows(x) mysql_affected_rows((x)->db)
396 #define sql_insert_autokey_record(x, y, z) my_mysql_insert_autokey_record((x), (y), (z))
397 #define sql_field_seek(x, y) mysql_field_seek((x)->result, (y))
398 #define sql_fetch_field(x) mysql_fetch_field((x)->result)
399 #define sql_num_fields(x) (int)mysql_num_fields((x)->result)
400 #define SQL_ROW MYSQL_ROW
401 #define SQL_FIELD MYSQL_FIELD
402 #define SQL_MATCH "MATCH"
404 #define sql_batch_start(x,y) my_batch_start(x,y)
405 #define sql_batch_end(x,y,z) my_batch_end(x,y,z)
406 #define sql_batch_insert(x,y,z) my_batch_insert(x,y,z)
407 #define sql_batch_lock_path_query my_mysql_batch_lock_path_query
408 #define sql_batch_lock_filename_query my_mysql_batch_lock_filename_query
409 #define sql_batch_unlock_tables_query my_mysql_batch_unlock_tables_query
410 #define sql_batch_fill_filename_query my_mysql_batch_fill_filename_query
411 #define sql_batch_fill_path_query my_mysql_batch_fill_path_query
414 extern const char* my_mysql_batch_lock_path_query;
415 extern const char* my_mysql_batch_lock_filename_query;
416 extern const char* my_mysql_batch_unlock_tables_query;
417 extern const char* my_mysql_batch_fill_filename_query;
418 extern const char* my_mysql_batch_fill_path_query;
419 extern void my_mysql_free_result(B_DB *mdb);
420 extern int my_mysql_insert_autokey_record(B_DB *mdb, const char *query, const char *table_name);
424 #ifdef HAVE_POSTGRESQL
426 #define BDB_VERSION 12
428 #include <libpq-fe.h>
430 /* TEMP: the following is taken from select OID, typname from pg_type; */
431 #define IS_NUM(x) ((x) == 20 || (x) == 21 || (x) == 23 || (x) == 700 || (x) == 701)
432 #define IS_NOT_NULL(x) ((x) == 1)
434 typedef char **POSTGRESQL_ROW;
435 typedef struct pg_field {
439 unsigned int flags; // 1 == not null
444 * This is the "real" definition that should only be
445 * used inside sql.c and associated database interface
448 * P O S T G R E S Q L
451 dlink link; /* queue control */
452 brwlock_t lock; /* transaction lock */
457 POSTGRESQL_FIELD *fields;
459 int row_size; /* size of malloced rows */
461 int fields_size; /* size of malloced fields */
462 int row_number; /* row number from my_postgresql_data_seek */
463 int field_number; /* field number from my_postgresql_field_seek */
468 char *db_address; /* host address */
469 char *db_socket; /* socket for local access */
470 int db_port; /* port of host address */
472 POOLMEM *errmsg; /* nicely edited error message */
473 POOLMEM *cmd; /* SQL command string */
474 POOLMEM *cached_path;
475 int cached_path_len; /* length of cached path */
476 uint32_t cached_path_id;
477 bool allow_transactions; /* transactions allowed */
478 bool transaction; /* transaction started */
479 int changes; /* changes made to db */
480 POOLMEM *fname; /* Filename only */
481 POOLMEM *path; /* Path only */
482 POOLMEM *esc_name; /* Escaped file name */
483 POOLMEM *esc_path; /* Escaped path name */
484 unsigned char *esc_obj; /* Escaped restore object */
485 int fnl; /* file name length */
486 int pnl; /* path name length */
489 void my_postgresql_free_result(B_DB *mdb);
490 POSTGRESQL_ROW my_postgresql_fetch_row (B_DB *mdb);
491 int my_postgresql_query (B_DB *mdb, const char *query);
492 void my_postgresql_data_seek (B_DB *mdb, int row);
493 int my_postgresql_insert_autokey_record (B_DB *mdb, const char *query, const char *table_name);
494 void my_postgresql_field_seek (B_DB *mdb, int row);
495 POSTGRESQL_FIELD * my_postgresql_fetch_field(B_DB *mdb);
497 int my_postgresql_batch_start(JCR *jcr, B_DB *mdb);
498 int my_postgresql_batch_end(JCR *jcr, B_DB *mdb, const char *error);
499 typedef struct ATTR_DBR ATTR_DBR;
500 int my_postgresql_batch_insert(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
501 char *my_postgresql_copy_escape(char *dest, char *src, size_t len);
503 extern const char* my_pg_batch_lock_path_query;
504 extern const char* my_pg_batch_lock_filename_query;
505 extern const char* my_pg_batch_unlock_tables_query;
506 extern const char* my_pg_batch_fill_filename_query;
507 extern const char* my_pg_batch_fill_path_query;
509 /* "Generic" names for easier conversion */
510 #define sql_store_result(x) ((x)->result)
511 #define sql_free_result(x) my_postgresql_free_result(x)
512 #define sql_fetch_row(x) my_postgresql_fetch_row(x)
513 #define sql_query(x, y) my_postgresql_query((x), (y))
514 #define sql_close(x) PQfinish((x)->db)
515 #define sql_strerror(x) PQerrorMessage((x)->db)
516 #define sql_num_rows(x) ((unsigned) PQntuples((x)->result))
517 #define sql_data_seek(x, i) my_postgresql_data_seek((x), (i))
518 #define sql_affected_rows(x) ((unsigned) atoi(PQcmdTuples((x)->result)))
519 #define sql_insert_autokey_record(x, y, z) my_postgresql_insert_autokey_record((x), (y), (z))
520 #define sql_field_seek(x, y) my_postgresql_field_seek((x), (y))
521 #define sql_fetch_field(x) my_postgresql_fetch_field(x)
522 #define sql_num_fields(x) ((x)->num_fields)
524 #define sql_batch_start(x,y) my_postgresql_batch_start(x,y)
525 #define sql_batch_end(x,y,z) my_postgresql_batch_end(x,y,z)
526 #define sql_batch_insert(x,y,z) my_postgresql_batch_insert(x,y,z)
527 #define sql_batch_lock_path_query my_pg_batch_lock_path_query
528 #define sql_batch_lock_filename_query my_pg_batch_lock_filename_query
529 #define sql_batch_unlock_tables_query my_pg_batch_unlock_tables_query
530 #define sql_batch_fill_filename_query my_pg_batch_fill_filename_query
531 #define sql_batch_fill_path_query my_pg_batch_fill_path_query
533 #define SQL_ROW POSTGRESQL_ROW
534 #define SQL_FIELD POSTGRESQL_FIELD
535 #define SQL_MATCH "~"
541 #include "myingres.h"
543 #define BDB_VERSION 12
545 /* TEMP: the following is taken from $(II_SYSTEM)/ingres/files/eqsqlda.h IISQ_ types */
546 #define IS_NUM(x) ((x) == 10 || (x) == 30 || (x) == 31)
547 #define IS_NOT_NULL(x) ((x) == 1)
549 typedef char **INGRES_ROW;
552 * This is the "real" definition that should only be
553 * used inside sql.c and associated database interface
559 dlink link; /* queue control */
560 brwlock_t lock; /* transaction lock */
565 INGRES_FIELD *fields;
567 int row_size; /* size of malloced rows */
569 int fields_size; /* size of malloced fields */
570 int row_number; /* row number from my_ingres_data_seek */
571 int field_number; /* field number from my_ingres_field_seek */
576 char *db_address; /* host address */
577 char *db_socket; /* socket for local access */
578 int db_port; /* port of host address */
579 int session_id; /* unique session id */
581 POOLMEM *errmsg; /* nicely edited error message */
582 POOLMEM *cmd; /* SQL command string */
583 POOLMEM *cached_path;
584 int cached_path_len; /* length of cached path */
585 uint32_t cached_path_id;
586 bool allow_transactions; /* transactions allowed */
587 bool transaction; /* transaction started */
588 bool explicit_commit; /* do an explicit commit after each query */
589 int changes; /* changes made to db */
590 POOLMEM *fname; /* Filename only */
591 POOLMEM *path; /* Path only */
592 POOLMEM *esc_name; /* Escaped file name */
593 POOLMEM *esc_path; /* Escaped path name */
594 alist *query_filters; /* Filters to convert sql queries into supported Ingres SQL */
595 int fnl; /* file name length */
596 int pnl; /* path name length */
599 void my_ingres_free_result(B_DB *mdb);
600 INGRES_ROW my_ingres_fetch_row (B_DB *mdb);
601 int my_ingres_query (B_DB *mdb, const char *query);
602 void my_ingres_data_seek (B_DB *mdb, int row);
603 void my_ingres_field_seek (B_DB *mdb, int row);
604 INGRES_FIELD * my_ingres_fetch_field(B_DB *mdb);
605 void my_ingres_close (B_DB *mdb);
606 int my_ingres_insert_autokey_record (B_DB *mdb, const char *query, const char *table_name);
608 bool my_ingres_batch_start(JCR *jcr, B_DB *mdb);
609 bool my_ingres_batch_end(JCR *jcr, B_DB *mdb, const char *error);
610 typedef struct ATTR_DBR ATTR_DBR;
611 bool my_ingres_batch_insert(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
612 char *my_ingres_copy_escape(char *dest, char *src, size_t len);
614 extern const char* my_ingres_batch_lock_path_query;
615 extern const char* my_ingres_batch_lock_filename_query;
616 extern const char* my_ingres_batch_unlock_tables_query;
617 extern const char* my_ingres_batch_fill_filename_query;
618 extern const char* my_ingres_batch_fill_path_query;
620 /* "Generic" names for easier conversion */
621 #define sql_store_result(x) ((x)->result)
622 #define sql_free_result(x) my_ingres_free_result(x)
623 #define sql_fetch_row(x) my_ingres_fetch_row(x)
624 #define sql_query(x, y) my_ingres_query((x), (y))
625 #define sql_close(x) my_ingres_close(x)
626 #define sql_strerror(x) INGerrorMessage((x)->db)
627 #define sql_num_rows(x) ((unsigned) INGntuples((x)->result))
628 #define sql_data_seek(x, i) my_ingres_data_seek((x), (i))
629 #define sql_affected_rows(x) ((x)->num_rows)
630 #define sql_insert_autokey_record(x, y, z) my_ingres_insert_autokey_record((x), (y), (z))
631 #define sql_field_seek(x, y) my_ingres_field_seek((x), (y))
632 #define sql_fetch_field(x) my_ingres_fetch_field(x)
633 #define sql_num_fields(x) ((x)->num_fields)
635 #define sql_batch_start(x,y) my_ingres_batch_start(x,y)
636 #define sql_batch_end(x,y,z) my_ingres_batch_end(x,y,z)
637 #define sql_batch_insert(x,y,z) my_ingres_batch_insert(x,y,z)
638 #define sql_batch_lock_path_query my_ingres_batch_lock_path_query
639 #define sql_batch_lock_filename_query my_ingres_batch_lock_filename_query
640 #define sql_batch_unlock_tables_query my_ingres_batch_unlock_tables_query
641 #define sql_batch_fill_filename_query my_ingres_batch_fill_filename_query
642 #define sql_batch_fill_path_query my_ingres_batch_fill_path_query
644 #define SQL_ROW INGRES_ROW
645 #define SQL_FIELD INGRES_FIELD
646 #define SQL_MATCH "~"
652 #define BDB_VERSION 12
656 #ifdef HAVE_BATCH_FILE_INSERT
657 #include <dbi/dbi-dev.h>
658 #endif //HAVE_BATCH_FILE_INSERT
660 #define IS_NUM(x) ((x) == 1 || (x) == 2 )
661 #define IS_NOT_NULL(x) ((x) == (1 << 0))
663 typedef char **DBI_ROW;
664 typedef struct dbi_field {
668 unsigned int flags; // 1 == not null
671 typedef struct dbi_field_get {
677 * This is the "real" definition that should only be
678 * used inside sql.c and associated database interface
684 dlink link; /* queue control */
685 brwlock_t lock; /* transaction lock */
689 dbi_error_flag status;
692 DBI_FIELD_GET *field_get;
694 int row_size; /* size of malloced rows */
696 int fields_size; /* size of malloced fields */
697 int row_number; /* row number from my_postgresql_data_seek */
698 int field_number; /* field number from my_postgresql_field_seek */
700 int db_type; /* DBI driver defined */
701 char *db_driverdir ; /* DBI driver dir */
702 char *db_driver; /* DBI type database */
706 char *db_address; /* host address */
707 char *db_socket; /* socket for local access */
708 int db_port; /* port of host address */
710 POOLMEM *errmsg; /* nicely edited error message */
711 POOLMEM *cmd; /* SQL command string */
712 POOLMEM *cached_path;
713 int cached_path_len; /* length of cached path */
714 uint32_t cached_path_id;
715 bool allow_transactions; /* transactions allowed */
716 bool transaction; /* transaction started */
717 int changes; /* changes made to db */
718 POOLMEM *fname; /* Filename only */
719 POOLMEM *path; /* Path only */
720 POOLMEM *esc_name; /* Escaped file name */
721 POOLMEM *esc_path; /* Escaped path name */
722 POOLMEM *esc_obj; /* Escaped restore object */
723 int fnl; /* file name length */
724 int pnl; /* path name length */
727 void my_dbi_free_result(B_DB *mdb);
728 DBI_ROW my_dbi_fetch_row (B_DB *mdb);
729 int my_dbi_query (B_DB *mdb, const char *query);
730 void my_dbi_data_seek (B_DB *mdb, int row);
731 void my_dbi_field_seek (B_DB *mdb, int row);
732 DBI_FIELD * my_dbi_fetch_field(B_DB *mdb);
733 const char * my_dbi_strerror (B_DB *mdb);
734 int my_dbi_getisnull (dbi_result *result, int row_number, int column_number);
735 char * my_dbi_getvalue (dbi_result *result, int row_number, unsigned int column_number);
736 //int my_dbi_getvalue (dbi_result *result, int row_number, unsigned int column_number, char *value);
737 int my_dbi_insert_autokey_record(B_DB *mdb, const char *query, const char *table_name);
739 int my_dbi_batch_start(JCR *jcr, B_DB *mdb);
740 int my_dbi_batch_end(JCR *jcr, B_DB *mdb, const char *error);
741 typedef struct ATTR_DBR ATTR_DBR;
742 int my_dbi_batch_insert(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
743 char *my_postgresql_copy_escape(char *dest, char *src, size_t len);
744 // typedefs for libdbi work with postgresql copy insert
745 typedef int (*custom_function_insert_t)(void*, const char*, int);
746 typedef char* (*custom_function_error_t)(void*);
747 typedef int (*custom_function_end_t)(void*, const char*);
749 extern const char* my_dbi_batch_lock_path_query[5];
750 extern const char* my_dbi_batch_lock_filename_query[5];
751 extern const char* my_dbi_batch_unlock_tables_query[5];
752 extern const char* my_dbi_batch_fill_filename_query[5];
753 extern const char* my_dbi_batch_fill_path_query[5];
754 extern const char* my_dbi_match[5];
756 /* "Generic" names for easier conversion */
757 #define sql_store_result(x) (x)->result
758 #define sql_free_result(x) my_dbi_free_result(x)
759 #define sql_fetch_row(x) my_dbi_fetch_row(x)
760 #define sql_query(x, y) my_dbi_query((x), (y))
761 #define sql_close(x) dbi_conn_close((x)->db)
762 #define sql_strerror(x) my_dbi_strerror(x)
763 #define sql_num_rows(x) dbi_result_get_numrows((x)->result)
764 #define sql_data_seek(x, i) my_dbi_data_seek((x), (i))
765 #define SQL_MATCH my_dbi_match[db_type]
766 /* #define sql_affected_rows(x) dbi_result_get_numrows_affected((x)->result) */
767 #define sql_affected_rows(x) 1
768 #define sql_insert_autokey_record(x, y, z) my_dbi_insert_autokey_record((x), (y), (z))
769 #define sql_field_seek(x, y) my_dbi_field_seek((x), (y))
770 #define sql_fetch_field(x) my_dbi_fetch_field(x)
771 #define sql_num_fields(x) ((x)->num_fields)
772 #define sql_batch_start(x,y) my_dbi_batch_start(x,y)
773 #define sql_batch_end(x,y,z) my_dbi_batch_end(x,y,z)
774 #define sql_batch_insert(x,y,z) my_dbi_batch_insert(x,y,z)
775 #define sql_batch_lock_path_query my_dbi_batch_lock_path_query[db_type]
776 #define sql_batch_lock_filename_query my_dbi_batch_lock_filename_query[db_type]
777 #define sql_batch_unlock_tables_query my_dbi_batch_unlock_tables_query[db_type]
778 #define sql_batch_fill_filename_query my_dbi_batch_fill_filename_query[db_type]
779 #define sql_batch_fill_path_query my_dbi_batch_fill_path_query[db_type]
781 #define SQL_ROW DBI_ROW
782 #define SQL_FIELD DBI_FIELD
784 #endif /* HAVE_SQLITE3 */
785 #endif /* HAVE_MYSQL */
786 #endif /* HAVE_SQLITE */
787 #endif /* HAVE_POSTGRESQL */
788 #endif /* HAVE_INGRES */
789 #endif /* HAVE_DBI */
792 /* Use for better error location printing */
793 #define UPDATE_DB(jcr, db, cmd) UpdateDB(__FILE__, __LINE__, jcr, db, cmd)
794 #define INSERT_DB(jcr, db, cmd) InsertDB(__FILE__, __LINE__, jcr, db, cmd)
795 #define QUERY_DB(jcr, db, cmd) QueryDB(__FILE__, __LINE__, jcr, db, cmd)
796 #define DELETE_DB(jcr, db, cmd) DeleteDB(__FILE__, __LINE__, jcr, db, cmd)
799 #else /* not __SQL_C */
801 /* This is a "dummy" definition for use outside of sql.c
804 int dummy; /* for SunOS compiler */
809 /* ==============================================================
811 * What follows are definitions that are used "globally" for all
812 * the different SQL engines and both inside and external to the
816 extern uint32_t bacula_db_version;
821 * Structure used when calling db_get_query_ids()
822 * allows the subroutine to return a list of ids.
824 class dbid_list : public SMARTALLOC {
826 DBId_t *DBId; /* array of DBIds */
827 char *PurgedFiles; /* Array of PurgedFile flags */
828 int num_ids; /* num of ids actually stored */
829 int max_ids; /* size of id array */
830 int num_seen; /* number of ids processed */
831 int tot_ids; /* total to process */
833 dbid_list(); /* in sql.c */
834 ~dbid_list(); /* in sql.c */
840 /* Job information passed to create job record and update
841 * job record at end of job. Note, although this record
842 * contains all the fields found in the Job database record,
843 * it also contains fields found in the JobMedia record.
848 char Job[MAX_NAME_LENGTH]; /* Job unique name */
849 char Name[MAX_NAME_LENGTH]; /* Job base name */
850 int JobType; /* actually char(1) */
851 int JobLevel; /* actually char(1) */
852 int JobStatus; /* actually char(1) */
853 DBId_t ClientId; /* Id of client */
854 DBId_t PoolId; /* Id of pool */
855 DBId_t FileSetId; /* Id of FileSet */
856 DBId_t PriorJobId; /* Id of migrated (prior) job */
857 time_t SchedTime; /* Time job scheduled */
858 time_t StartTime; /* Job start time */
859 time_t EndTime; /* Job termination time of orig job */
860 time_t RealEndTime; /* Job termination time of this job */
861 utime_t JobTDate; /* Backup time/date in seconds */
862 uint32_t VolSessionId;
863 uint32_t VolSessionTime;
866 uint32_t JobMissingFiles;
872 /* Note, FirstIndex, LastIndex, Start/End File and Block
873 * are only used in the JobMedia record.
875 uint32_t FirstIndex; /* First index this Volume */
876 uint32_t LastIndex; /* Last index this Volume */
882 char cSchedTime[MAX_TIME_LENGTH];
883 char cStartTime[MAX_TIME_LENGTH];
884 char cEndTime[MAX_TIME_LENGTH];
885 char cRealEndTime[MAX_TIME_LENGTH];
886 /* Extra stuff not in DB */
887 int limit; /* limit records to display */
889 uint32_t FileIndex; /* added during Verify */
892 /* Job Media information used to create the media records
893 * for each Volume used for the job.
895 /* JobMedia record */
896 struct JOBMEDIA_DBR {
897 DBId_t JobMediaId; /* record id */
898 JobId_t JobId; /* JobId */
899 DBId_t MediaId; /* MediaId */
900 uint32_t FirstIndex; /* First index this Volume */
901 uint32_t LastIndex; /* Last index this Volume */
902 uint32_t StartFile; /* File for start of data */
903 uint32_t EndFile; /* End file on Volume */
904 uint32_t StartBlock; /* start block on tape */
905 uint32_t EndBlock; /* last block */
906 // uint32_t Copy; /* identical copy */
910 /* Volume Parameter structure */
912 char VolumeName[MAX_NAME_LENGTH]; /* Volume name */
913 char MediaType[MAX_NAME_LENGTH]; /* Media Type */
914 char Storage[MAX_NAME_LENGTH]; /* Storage name */
915 uint32_t VolIndex; /* Volume seqence no. */
916 uint32_t FirstIndex; /* First index this Volume */
917 uint32_t LastIndex; /* Last index this Volume */
918 int32_t Slot; /* Slot */
919 uint64_t StartAddr; /* Start address */
920 uint64_t EndAddr; /* End address */
921 int32_t InChanger; /* InChanger flag */
922 // uint32_t Copy; /* identical copy */
923 // uint32_t Stripe; /* RAIT strip number */
927 /* Attributes record -- NOT same as in database because
928 * in general, this "record" creates multiple database
929 * records (e.g. pathname, filename, fileattributes).
932 char *fname; /* full path & filename */
933 char *link; /* link if any */
934 char *attr; /* attributes statp */
952 uint32_t object_full_len;
953 uint32_t object_index;
954 int32_t object_compression;
959 DBId_t RestoreObjectId;
963 /* File record -- same format as database */
972 char Digest[BASE64_SIZE(CRYPTO_DIGEST_MAX_SIZE)];
973 int DigestType; /* NO_SIG/MD5_SIG/SHA1_SIG */
976 /* Pool record -- same format as database */
979 char Name[MAX_NAME_LENGTH]; /* Pool name */
980 uint32_t NumVols; /* total number of volumes */
981 uint32_t MaxVols; /* max allowed volumes */
982 int32_t LabelType; /* Bacula/ANSI/IBM */
983 int32_t UseOnce; /* set to use once only */
984 int32_t UseCatalog; /* set to use catalog */
985 int32_t AcceptAnyVolume; /* set to accept any volume sequence */
986 int32_t AutoPrune; /* set to prune automatically */
987 int32_t Recycle; /* default Vol recycle flag */
988 uint32_t ActionOnPurge; /* action on purge, e.g. truncate the disk volume */
989 utime_t VolRetention; /* retention period in seconds */
990 utime_t VolUseDuration; /* time in secs volume can be used */
991 uint32_t MaxVolJobs; /* Max Jobs on Volume */
992 uint32_t MaxVolFiles; /* Max files on Volume */
993 uint64_t MaxVolBytes; /* Max bytes on Volume */
994 DBId_t RecyclePoolId; /* RecyclePool destination when media is purged */
995 DBId_t ScratchPoolId; /* ScratchPool source when media is needed */
996 char PoolType[MAX_NAME_LENGTH];
997 char LabelFormat[MAX_NAME_LENGTH];
998 /* Extra stuff not in DB */
1005 char Name[MAX_NAME_LENGTH]; /* Device name */
1006 DBId_t MediaTypeId; /* MediaType */
1007 DBId_t StorageId; /* Storage id if autochanger */
1008 uint32_t DevMounts; /* Number of times mounted */
1009 uint32_t DevErrors; /* Number of read/write errors */
1010 uint64_t DevReadBytes; /* Number of bytes read */
1011 uint64_t DevWriteBytes; /* Number of bytew written */
1012 uint64_t DevReadTime; /* time spent reading volume */
1013 uint64_t DevWriteTime; /* time spent writing volume */
1014 uint64_t DevReadTimeSincCleaning; /* read time since cleaning */
1015 uint64_t DevWriteTimeSincCleaning; /* write time since cleaning */
1016 time_t CleaningDate; /* time last cleaned */
1017 utime_t CleaningPeriod; /* time between cleanings */
1023 char Name[MAX_NAME_LENGTH]; /* Device name */
1024 int AutoChanger; /* Set if autochanger */
1026 /* Not in database */
1027 bool created; /* set if created by db_create ... */
1030 class MEDIATYPE_DBR {
1033 char MediaType[MAX_NAME_LENGTH]; /* MediaType string */
1034 int ReadOnly; /* Set if read-only */
1038 /* Media record -- same as the database */
1040 DBId_t MediaId; /* Unique volume id */
1041 char VolumeName[MAX_NAME_LENGTH]; /* Volume name */
1042 char MediaType[MAX_NAME_LENGTH]; /* Media type */
1043 DBId_t PoolId; /* Pool id */
1044 time_t FirstWritten; /* Time Volume first written this usage */
1045 time_t LastWritten; /* Time Volume last written */
1046 time_t LabelDate; /* Date/Time Volume labeled */
1047 time_t InitialWrite; /* Date/Time Volume first written */
1048 int32_t LabelType; /* Label (Bacula/ANSI/IBM) */
1049 uint32_t VolJobs; /* number of jobs on this medium */
1050 uint32_t VolFiles; /* Number of files */
1051 uint32_t VolBlocks; /* Number of blocks */
1052 uint32_t VolMounts; /* Number of times mounted */
1053 uint32_t VolErrors; /* Number of read/write errors */
1054 uint32_t VolWrites; /* Number of writes */
1055 uint32_t VolReads; /* Number of reads */
1056 uint64_t VolBytes; /* Number of bytes written */
1057 uint32_t VolParts; /* Number of parts written */
1058 uint64_t MaxVolBytes; /* Max bytes to write to Volume */
1059 uint64_t VolCapacityBytes; /* capacity estimate */
1060 uint64_t VolReadTime; /* time spent reading volume */
1061 uint64_t VolWriteTime; /* time spent writing volume */
1062 utime_t VolRetention; /* Volume retention in seconds */
1063 utime_t VolUseDuration; /* time in secs volume can be used */
1064 uint32_t ActionOnPurge; /* action on purge, e.g. truncate the disk volume */
1065 uint32_t MaxVolJobs; /* Max Jobs on Volume */
1066 uint32_t MaxVolFiles; /* Max files on Volume */
1067 int32_t Recycle; /* recycle yes/no */
1068 int32_t Slot; /* slot in changer */
1069 int32_t Enabled; /* 0=disabled, 1=enabled, 2=archived */
1070 int32_t InChanger; /* Volume currently in changer */
1071 DBId_t StorageId; /* Storage record Id */
1072 uint32_t EndFile; /* Last file on volume */
1073 uint32_t EndBlock; /* Last block on volume */
1074 uint32_t RecycleCount; /* Number of times recycled */
1075 char VolStatus[20]; /* Volume status */
1076 DBId_t DeviceId; /* Device where Vol last written */
1077 DBId_t LocationId; /* Where Volume is -- user defined */
1078 DBId_t ScratchPoolId; /* Where to move if scratch */
1079 DBId_t RecyclePoolId; /* Where to move when recycled */
1080 /* Extra stuff not in DB */
1081 faddr_t rec_addr; /* found record address */
1082 /* Since the database returns times as strings, this is how we pass
1085 char cFirstWritten[MAX_TIME_LENGTH]; /* FirstWritten returned from DB */
1086 char cLastWritten[MAX_TIME_LENGTH]; /* LastWritten returned from DB */
1087 char cLabelDate[MAX_TIME_LENGTH]; /* LabelData returned from DB */
1088 char cInitialWrite[MAX_TIME_LENGTH]; /* InitialWrite returned from DB */
1089 bool set_first_written;
1090 bool set_label_date;
1093 /* Client record -- same as the database */
1095 DBId_t ClientId; /* Unique Client id */
1097 utime_t FileRetention;
1098 utime_t JobRetention;
1099 char Name[MAX_NAME_LENGTH]; /* Client name */
1100 char Uname[256]; /* Uname for client */
1103 /* Counter record as in database */
1104 struct COUNTER_DBR {
1105 char Counter[MAX_NAME_LENGTH];
1108 int32_t CurrentValue;
1109 char WrapCounter[MAX_NAME_LENGTH];
1113 /* FileSet record -- same as the database */
1114 struct FILESET_DBR {
1115 DBId_t FileSetId; /* Unique FileSet id */
1116 char FileSet[MAX_NAME_LENGTH]; /* FileSet name */
1117 char MD5[50]; /* MD5 signature of include/exclude */
1118 time_t CreateTime; /* date created */
1120 * This is where we return CreateTime
1122 char cCreateTime[MAX_TIME_LENGTH]; /* CreateTime as returned from DB */
1123 /* Not in DB but returned by db_create_fileset() */
1124 bool created; /* set when record newly created */
1127 /* Call back context for getting a 32/64 bit value from the database */
1128 struct db_int64_ctx {
1129 int64_t value; /* value returned */
1130 int count; /* number of values seen */
1133 /* Call back context for getting a list of comma separated strings from the
1138 POOLMEM *list; /* list */
1139 int count; /* number of values seen */
1141 db_list_ctx() { list = get_pool_memory(PM_FNAME); *list = 0; count = 0; }
1142 ~db_list_ctx() { free_pool_memory(list); list = NULL; }
1145 db_list_ctx(const db_list_ctx&); /* prohibit pass by value */
1146 db_list_ctx &operator=(const db_list_ctx&); /* prohibit class assignment */
1152 #include "sql_cmds.h"
1155 * Exported globals from sql.c
1157 extern int CATS_IMP_EXP db_type; /* SQL engine type index */
1160 * Some functions exported by sql.c for use within the
1163 void list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type type);
1164 void list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx);
1165 int get_sql_record_max(JCR *jcr, B_DB *mdb);
1166 bool check_tables_version(JCR *jcr, B_DB *mdb);
1167 bool db_check_max_connections(JCR *jcr, B_DB *mdb, uint32_t nb);
1168 void _db_unlock(const char *file, int line, B_DB *mdb);
1169 void _db_lock(const char *file, int line, B_DB *mdb);
1170 const char *db_get_type(void);
1172 void print_dashes(B_DB *mdb);
1173 void print_result(B_DB *mdb);
1174 int QueryDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
1175 int InsertDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
1176 int DeleteDB(const char *file, int line, JCR *jcr, B_DB *db, char *delete_cmd);
1177 int UpdateDB(const char *file, int line, JCR *jcr, B_DB *db, char *update_cmd);
1178 void split_path_and_file(JCR *jcr, B_DB *mdb, const char *fname);
1179 #endif /* __SQL_H_ */