2 * Bacula Catalog Database interface routines
4 * Almost generic set of SQL database interface routines
5 * (with a little more work)
7 * Kern Sibbald, March 2000
13 Copyright (C) 2000-2005 Kern Sibbald
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation; either version 2 of
18 the License, or (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received a copy of the GNU General Public
26 License along with this program; if not, write to the Free
27 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32 /* The following is necessary so that we do not include
33 * the dummy external definition of B_DB.
35 #define __SQL_C /* indicate that this is sql.c */
40 #if HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL
42 uint32_t bacula_db_version = 0;
44 /* Forward referenced subroutines */
45 void print_dashes(B_DB *mdb);
46 void print_result(B_DB *mdb);
49 * Called here to retrieve an integer from the database
51 static int int_handler(void *ctx, int num_fields, char **row)
53 uint32_t *val = (uint32_t *)ctx;
55 Dmsg1(800, "int_handler starts with row pointing at %x\n", row);
58 Dmsg1(800, "int_handler finds '%s'\n", row[0]);
59 *val = str_to_int64(row[0]);
61 Dmsg0(800, "int_handler finds zero\n");
64 Dmsg0(800, "int_handler finishes\n");
70 /* NOTE!!! The following routines expect that the
71 * calling subroutine sets and clears the mutex
74 /* Check that the tables correspond to the version we want */
75 int check_tables_version(JCR *jcr, B_DB *mdb)
77 const char *query = "SELECT VersionId FROM Version";
79 bacula_db_version = 0;
80 db_sql_query(mdb, query, int_handler, (void *)&bacula_db_version);
81 if (bacula_db_version != BDB_VERSION) {
82 Mmsg(mdb->errmsg, "Version error for database \"%s\". Wanted %d, got %d\n",
83 mdb->db_name, BDB_VERSION, bacula_db_version);
84 Jmsg(jcr, M_FATAL, 0, "%s", mdb->errmsg);
90 /* Utility routine for queries. The database MUST be locked before calling here. */
92 QueryDB(const char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
95 if ((status=sql_query(mdb, cmd)) != 0) {
96 m_msg(file, line, &mdb->errmsg, _("query %s failed:\n%s\n"), cmd, sql_strerror(mdb));
97 j_msg(file, line, jcr, M_FATAL, 0, "%s", mdb->errmsg);
99 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
104 mdb->result = sql_store_result(mdb);
106 return mdb->result != NULL;
110 * Utility routine to do inserts
111 * Returns: 0 on failure
115 InsertDB(const char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
117 if (sql_query(mdb, cmd)) {
118 m_msg(file, line, &mdb->errmsg, _("insert %s failed:\n%s\n"), cmd, sql_strerror(mdb));
119 j_msg(file, line, jcr, M_FATAL, 0, "%s", mdb->errmsg);
121 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
125 if (mdb->have_insert_id) {
126 mdb->num_rows = sql_affected_rows(mdb);
130 if (mdb->num_rows != 1) {
132 m_msg(file, line, &mdb->errmsg, _("Insertion problem: affected_rows=%s\n"),
133 edit_uint64(mdb->num_rows, ed1));
135 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
143 /* Utility routine for updates.
144 * Returns: 0 on failure
148 UpdateDB(const char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
151 if (sql_query(mdb, cmd)) {
152 m_msg(file, line, &mdb->errmsg, _("update %s failed:\n%s\n"), cmd, sql_strerror(mdb));
153 j_msg(file, line, jcr, M_ERROR, 0, "%s", mdb->errmsg);
155 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
159 mdb->num_rows = sql_affected_rows(mdb);
160 if (mdb->num_rows < 1) {
162 m_msg(file, line, &mdb->errmsg, _("Update problem: affected_rows=%s\n"),
163 edit_uint64(mdb->num_rows, ed1));
165 // j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
173 /* Utility routine for deletes
175 * Returns: -1 on error
176 * n number of rows affected
179 DeleteDB(const char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
182 if (sql_query(mdb, cmd)) {
183 m_msg(file, line, &mdb->errmsg, _("delete %s failed:\n%s\n"), cmd, sql_strerror(mdb));
184 j_msg(file, line, jcr, M_ERROR, 0, "%s", mdb->errmsg);
186 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
191 return sql_affected_rows(mdb);
196 * Get record max. Query is already in mdb->cmd
199 * Returns: -1 on failure
202 int get_sql_record_max(JCR *jcr, B_DB *mdb)
207 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
208 if ((row = sql_fetch_row(mdb)) == NULL) {
209 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
212 stat = str_to_int64(row[0]);
214 sql_free_result(mdb);
216 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
223 * Return pre-edited error message
225 char *db_strerror(B_DB *mdb)
231 * Lock database, this can be called multiple times by the same
232 * thread without blocking, but must be unlocked the number of
233 * times it was locked.
235 void _db_lock(const char *file, int line, B_DB *mdb)
238 if ((errstat=rwl_writelock(&mdb->lock)) != 0) {
240 e_msg(file, line, M_FATAL, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
241 errstat, be.strerror(errstat));
246 * Unlock the database. This can be called multiple times by the
247 * same thread up to the number of times that thread called
250 void _db_unlock(const char *file, int line, B_DB *mdb)
253 if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) {
255 e_msg(file, line, M_FATAL, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
256 errstat, be.strerror(errstat));
261 * Start a transaction. This groups inserts and makes things
262 * much more efficient. Usually started when inserting
265 void db_start_transaction(JCR *jcr, B_DB *mdb)
268 jcr->attr = get_pool_memory(PM_FNAME);
271 jcr->ar = (ATTR_DBR *)malloc(sizeof(ATTR_DBR));
275 if (!mdb->allow_transactions) {
279 /* Allow only 10,000 changes per transaction */
280 if (mdb->transaction && mdb->changes > 10000) {
281 db_end_transaction(jcr, mdb);
283 if (!mdb->transaction) {
284 my_sqlite_query(mdb, "BEGIN"); /* begin transaction */
285 Dmsg0(400, "Start SQLite transaction\n");
286 mdb->transaction = 1;
292 * This is turned off because transactions break
293 * if multiple simultaneous jobs are run.
295 #ifdef HAVE_POSTGRESQL
296 if (!mdb->allow_transactions) {
300 /* Allow only 25,000 changes per transaction */
301 if (mdb->transaction && mdb->changes > 25000) {
302 db_end_transaction(jcr, mdb);
304 if (!mdb->transaction) {
305 db_sql_query(mdb, "BEGIN", NULL, NULL); /* begin transaction */
306 Dmsg0(400, "Start PosgreSQL transaction\n");
307 mdb->transaction = 1;
313 void db_end_transaction(JCR *jcr, B_DB *mdb)
316 * This can be called during thread cleanup and
317 * the db may already be closed. So simply return.
323 if (jcr && jcr->cached_attribute) {
324 Dmsg0(400, "Flush last cached attribute.\n");
325 if (!db_create_file_attributes_record(jcr, mdb, jcr->ar)) {
326 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
328 jcr->cached_attribute = false;
332 if (!mdb->allow_transactions) {
336 if (mdb->transaction) {
337 my_sqlite_query(mdb, "COMMIT"); /* end transaction */
338 mdb->transaction = 0;
339 Dmsg1(400, "End SQLite transaction changes=%d\n", mdb->changes);
345 #ifdef HAVE_POSTGRESQL
346 if (!mdb->allow_transactions) {
350 if (mdb->transaction) {
351 db_sql_query(mdb, "COMMIT", NULL, NULL); /* end transaction */
352 mdb->transaction = 0;
353 Dmsg1(400, "End PostgreSQL transaction changes=%d\n", mdb->changes);
361 * Given a full filename, split it into its path
362 * and filename parts. They are returned in pool memory
363 * in the mdb structure.
365 void split_path_and_file(JCR *jcr, B_DB *mdb, const char *fname)
369 /* Find path without the filename.
370 * I.e. everything after the last / is a "filename".
371 * OK, maybe it is a directory name, but we treat it like
372 * a filename. If we don't find a / then the whole name
373 * must be a path name (e.g. c:).
375 for (p=f=fname; *p; p++) {
377 f = p; /* set pos of last slash */
380 if (*f == '/') { /* did we find a slash? */
381 f++; /* yes, point to filename */
382 } else { /* no, whole thing must be path name */
386 /* If filename doesn't exist (i.e. root directory), we
387 * simply create a blank name consisting of a single
388 * space. This makes handling zero length filenames
393 mdb->fname = check_pool_memory_size(mdb->fname, mdb->fnl+1);
394 memcpy(mdb->fname, f, mdb->fnl); /* copy filename */
395 mdb->fname[mdb->fnl] = 0;
401 mdb->pnl = f - fname;
403 mdb->path = check_pool_memory_size(mdb->path, mdb->pnl+1);
404 memcpy(mdb->path, fname, mdb->pnl);
405 mdb->path[mdb->pnl] = 0;
407 Mmsg1(&mdb->errmsg, _("Path length is zero. File=%s\n"), fname);
408 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
413 Dmsg2(500, "split path=%s file=%s\n", mdb->path, mdb->fname);
417 * List dashes as part of header for listing SQL results in a table
420 list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx)
425 sql_field_seek(mdb, 0);
427 for (i = 0; i < sql_num_fields(mdb); i++) {
428 field = sql_fetch_field(mdb);
429 for (j = 0; j < (int)field->max_length + 2; j++) {
438 * If full_list is set, we list vertically, otherwise, we
439 * list on one line horizontally.
442 list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type type)
446 int i, col_len, max_len = 0;
447 char buf[2000], ewc[30];
449 Dmsg0(800, "list_result starts\n");
450 if (mdb->result == NULL || sql_num_rows(mdb) == 0) {
451 send(ctx, _("No results to list.\n"));
455 Dmsg1(800, "list_result starts looking at %d fields\n", sql_num_fields(mdb));
456 /* determine column display widths */
457 sql_field_seek(mdb, 0);
458 for (i = 0; i < sql_num_fields(mdb); i++) {
459 Dmsg1(800, "list_result processing field %d\n", i);
460 field = sql_fetch_field(mdb);
461 col_len = cstrlen(field->name);
462 if (type == VERT_LIST) {
463 if (col_len > max_len) {
467 if (IS_NUM(field->type) && (int)field->max_length > 0) { /* fixup for commas */
468 field->max_length += (field->max_length - 1) / 3;
470 if (col_len < (int)field->max_length) {
471 col_len = field->max_length;
473 if (col_len < 4 && !IS_NOT_NULL(field->flags)) {
474 col_len = 4; /* 4 = length of the word "NULL" */
476 field->max_length = col_len; /* reset column info */
480 Dmsg0(800, "list_result finished first loop\n");
481 if (type == VERT_LIST) {
485 Dmsg1(800, "list_result starts second loop looking at %d fields\n", sql_num_fields(mdb));
486 list_dashes(mdb, send, ctx);
488 sql_field_seek(mdb, 0);
489 for (i = 0; i < sql_num_fields(mdb); i++) {
490 Dmsg1(800, "list_result looking at field %d\n", i);
491 field = sql_fetch_field(mdb);
492 bsnprintf(buf, sizeof(buf), " %-*s |", (int)field->max_length, field->name);
496 list_dashes(mdb, send, ctx);
498 Dmsg1(800, "list_result starts third loop looking at %d fields\n", sql_num_fields(mdb));
499 while ((row = sql_fetch_row(mdb)) != NULL) {
500 sql_field_seek(mdb, 0);
502 for (i = 0; i < sql_num_fields(mdb); i++) {
503 field = sql_fetch_field(mdb);
504 if (row[i] == NULL) {
505 bsnprintf(buf, sizeof(buf), " %-*s |", (int)field->max_length, "NULL");
506 } else if (IS_NUM(field->type) && !jcr->gui && is_an_integer(row[i])) {
507 bsnprintf(buf, sizeof(buf), " %*s |", (int)field->max_length,
508 add_commas(row[i], ewc));
510 bsnprintf(buf, sizeof(buf), " %-*s |", (int)field->max_length, row[i]);
516 list_dashes(mdb, send, ctx);
521 Dmsg1(800, "list_result starts vertical list at %d fields\n", sql_num_fields(mdb));
522 while ((row = sql_fetch_row(mdb)) != NULL) {
523 sql_field_seek(mdb, 0);
524 for (i = 0; i < sql_num_fields(mdb); i++) {
525 field = sql_fetch_field(mdb);
526 if (row[i] == NULL) {
527 bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, "NULL");
528 } else if (IS_NUM(field->type) && !jcr->gui && is_an_integer(row[i])) {
529 bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name,
530 add_commas(row[i], ewc));
532 bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, row[i]);
542 #endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL*/