]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/cats/sql.c
Improve debug output scheduler+jobq+tracing
[bacula/bacula] / bacula / src / cats / sql.c
index 408a3f6900a64dafae7ff1a55a895dbea969c13e..58d331037be1ff53f24a5044c1960a071ff0b68a 100644 (file)
@@ -10,7 +10,7 @@
  */
 
 /*
-   Copyright (C) 2000-2003 Kern Sibbald and John Walker
+   Copyright (C) 2000-2004 Kern Sibbald and John Walker
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
@@ -37,7 +37,9 @@
 #include "bacula.h"
 #include "cats.h"
 
-#if    HAVE_MYSQL | HAVE_SQLITE
+#if    HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL
+
+uint32_t bacula_db_version = 0;
 
 /* Forward referenced subroutines */
 void print_dashes(B_DB *mdb);
@@ -50,11 +52,16 @@ static int int_handler(void *ctx, int num_fields, char **row)
 {
    uint32_t *val = (uint32_t *)ctx;
 
+   Dmsg1(200, "int_handler starts with row pointing at %x\n", row);
+
    if (row[0]) {
+      Dmsg1(200, "int_handler finds '%s'\n", row[0]);
       *val = atoi(row[0]);
    } else {
+      Dmsg0(200, "int_handler finds zero\n");
       *val = 0;
    }
+   Dmsg0(200, "int_handler finishes\n");
    return 0;
 }
        
@@ -65,33 +72,37 @@ static int int_handler(void *ctx, int num_fields, char **row)
  */
 
 /* Check that the tables correspond to the version we want */
-int check_tables_version(void *jcr, B_DB *mdb)
+int check_tables_version(JCR *jcr, B_DB *mdb)
 {
-   uint32_t version;
    char *query = "SELECT VersionId FROM Version";
   
-   version = 0;
-   db_sql_query(mdb, query, int_handler, (void *)&version);
-   if (version != BDB_VERSION) {
+   bacula_db_version = 0;
+   db_sql_query(mdb, query, int_handler, (void *)&bacula_db_version);
+   if (bacula_db_version != BDB_VERSION) {
       Mmsg(&mdb->errmsg, "Version error for database \"%s\". Wanted %d, got %d\n",
-        mdb->db_name, BDB_VERSION, version);
+         mdb->db_name, BDB_VERSION, bacula_db_version);
       Jmsg(jcr, M_FATAL, 0, "%s", mdb->errmsg);
       return 0;
    }
    return 1;
 }
 
-/* Utility routine for queries */
+/* Utility routine for queries. The database MUST be locked before calling here. */
 int
-QueryDB(char *file, int line, void *jcr, B_DB *mdb, char *cmd)
+QueryDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
 {
-   if (sql_query(mdb, cmd)) {
+   int status;
+   if ((status=sql_query(mdb, cmd)) != 0) {
       m_msg(file, line, &mdb->errmsg, _("query %s failed:\n%s\n"), cmd, sql_strerror(mdb));
       j_msg(file, line, jcr, M_FATAL, 0, "%s", mdb->errmsg);
+      if (verbose) {
+         j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
+      }
       return 0;
    }
+
    mdb->result = sql_store_result(mdb);
-   
+
    return mdb->result != NULL;
 }
 
@@ -101,11 +112,14 @@ QueryDB(char *file, int line, void *jcr, B_DB *mdb, char *cmd)
  *         1 on success
  */
 int
-InsertDB(char *file, int line, void *jcr, B_DB *mdb, char *cmd)
+InsertDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
 {
    if (sql_query(mdb, cmd)) {
       m_msg(file, line, &mdb->errmsg,  _("insert %s failed:\n%s\n"), cmd, sql_strerror(mdb));
       j_msg(file, line, jcr, M_FATAL, 0, "%s", mdb->errmsg);
+      if (verbose) {
+         j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
+      }
       return 0;
    }
    if (mdb->have_insert_id) {
@@ -115,8 +129,11 @@ InsertDB(char *file, int line, void *jcr, B_DB *mdb, char *cmd)
    }
    if (mdb->num_rows != 1) {
       char ed1[30];
-      m_msg(file, line, &mdb->errmsg, _("Insertion problem: affect_rows=%s\n"), 
+      m_msg(file, line, &mdb->errmsg, _("Insertion problem: affected_rows=%s\n"), 
         edit_uint64(mdb->num_rows, ed1));
+      if (verbose) {
+         j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
+      }
       return 0;
    }
    mdb->changes++;
@@ -128,20 +145,25 @@ InsertDB(char *file, int line, void *jcr, B_DB *mdb, char *cmd)
  *          1 on success  
  */
 int
-UpdateDB(char *file, int line, void *jcr, B_DB *mdb, char *cmd)
+UpdateDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
 {
 
    if (sql_query(mdb, cmd)) {
       m_msg(file, line, &mdb->errmsg, _("update %s failed:\n%s\n"), cmd, sql_strerror(mdb));
       j_msg(file, line, jcr, M_ERROR, 0, "%s", mdb->errmsg);
-      j_msg(file, line, jcr, M_ERROR, 0, "%s\n", cmd);
+      if (verbose) {
+         j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
+      }
       return 0;
    }
    mdb->num_rows = sql_affected_rows(mdb);
    if (mdb->num_rows != 1) {
       char ed1[30];
-      m_msg(file, line, &mdb->errmsg, _("Update problem: affect_rows=%s\n"), 
+      m_msg(file, line, &mdb->errmsg, _("Update problem: affected_rows=%s\n"), 
         edit_uint64(mdb->num_rows, ed1));
+      if (verbose) {
+         j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
+      }
       return 0;
    }
    mdb->changes++;
@@ -154,12 +176,15 @@ UpdateDB(char *file, int line, void *jcr, B_DB *mdb, char *cmd)
  *          n number of rows affected
  */
 int
-DeleteDB(char *file, int line, void *jcr, B_DB *mdb, char *cmd)
+DeleteDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
 {
 
    if (sql_query(mdb, cmd)) {
       m_msg(file, line, &mdb->errmsg, _("delete %s failed:\n%s\n"), cmd, sql_strerror(mdb));
       j_msg(file, line, jcr, M_ERROR, 0, "%s", mdb->errmsg);
+      if (verbose) {
+         j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
+      }
       return -1;
    }
    mdb->changes++;
@@ -174,7 +199,7 @@ DeleteDB(char *file, int line, void *jcr, B_DB *mdb, char *cmd)
  * Returns: -1 on failure
  *         count on success
  */
-int get_sql_record_max(void *jcr, B_DB *mdb)
+int get_sql_record_max(JCR *jcr, B_DB *mdb)
 {
    SQL_ROW row;
    int stat = 0;
@@ -194,11 +219,19 @@ int get_sql_record_max(void *jcr, B_DB *mdb)
    return stat;
 }
 
+/*
+ * Return pre-edited error message
+ */
 char *db_strerror(B_DB *mdb)
 {
    return mdb->errmsg;
 }
 
+/*
+ * Lock database, this can be called multiple times by the same
+ *   thread without blocking, but must be unlocked the number of
+ *   times it was locked.
+ */
 void _db_lock(char *file, int line, B_DB *mdb)
 {
    int errstat;
@@ -208,6 +241,11 @@ void _db_lock(char *file, int line, B_DB *mdb)
    }
 }    
 
+/*
+ * Unlock the database. This can be called multiple times by the
+ *   same thread up to the number of times that thread called
+ *   db_lock()/
+ */
 void _db_unlock(char *file, int line, B_DB *mdb)
 {
    int errstat;
@@ -222,7 +260,7 @@ void _db_unlock(char *file, int line, B_DB *mdb)
  *  much more efficient. Usually started when inserting 
  *  file attributes.
  */
-void db_start_transaction(void *jcr, B_DB *mdb)
+void db_start_transaction(JCR *jcr, B_DB *mdb)
 {
 #ifdef xAVE_SQLITE
    db_lock(mdb);
@@ -240,7 +278,7 @@ void db_start_transaction(void *jcr, B_DB *mdb)
 
 }
 
-void db_end_transaction(void *jcr, B_DB *mdb)
+void db_end_transaction(JCR *jcr, B_DB *mdb)
 {
 #ifdef xAVE_SQLITE
    db_lock(mdb);
@@ -259,7 +297,7 @@ void db_end_transaction(void *jcr, B_DB *mdb)
  *  and filename parts. They are returned in pool memory
  *  in the mdb structure.
  */
-void split_path_and_filename(void *jcr, B_DB *mdb, char *fname)
+void split_path_and_filename(JCR *jcr, B_DB *mdb, char *fname)
 {
    char *p, *f;
 
@@ -312,4 +350,130 @@ void split_path_and_filename(void *jcr, B_DB *mdb, char *fname)
    Dmsg2(100, "sllit path=%s file=%s\n", mdb->path, mdb->fname);
 }
 
-#endif /* HAVE_MYSQL | HAVE_SQLITE */
+/*
+ * List dashes as part of header for listing SQL results in a table
+ */
+void
+list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx)
+{
+   SQL_FIELD  *field;
+   int i, j;
+
+   sql_field_seek(mdb, 0);
+   send(ctx, "+");
+   for (i = 0; i < sql_num_fields(mdb); i++) {
+      field = sql_fetch_field(mdb);
+      for (j = 0; j < (int)field->max_length + 2; j++) {
+         send(ctx, "-");
+      }
+      send(ctx, "+");
+   }
+   send(ctx, "\n");
+}
+
+/*
+ * If full_list is set, we list vertically, otherwise, we 
+ * list on one line horizontally.      
+ */
+void
+list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type type)
+{
+   SQL_FIELD *field;
+   SQL_ROW row;
+   int i, col_len, max_len = 0;
+   char buf[2000], ewc[30];
+
+   Dmsg0(200, "list_result starts\n");
+   if (mdb->result == NULL || sql_num_rows(mdb) == 0) {
+      send(ctx, _("No results to list.\n"));
+      return;
+   }
+
+   Dmsg1(200, "list_result starts looking at %d fields\n", sql_num_fields(mdb));
+   /* determine column display widths */
+   sql_field_seek(mdb, 0);
+   for (i = 0; i < sql_num_fields(mdb); i++) {
+      Dmsg1(200, "list_result processing field %d\n", i);
+      field = sql_fetch_field(mdb);
+      col_len = strlen(field->name);
+      if (type == VERT_LIST) {
+        if (col_len > max_len) {
+           max_len = col_len;
+        }
+      } else {
+        if (IS_NUM(field->type) && (int)field->max_length > 0) { /* fixup for commas */
+           field->max_length += (field->max_length - 1) / 3;
+        }
+        if (col_len < (int)field->max_length) {
+           col_len = field->max_length;
+        }
+        if (col_len < 4 && !IS_NOT_NULL(field->flags)) {
+            col_len = 4;                 /* 4 = length of the word "NULL" */
+        }
+        field->max_length = col_len;    /* reset column info */
+      }
+   }
+
+   Dmsg0(200, "list_result finished first loop\n");
+   if (type == VERT_LIST) {
+      goto vertical_list;
+   }
+
+   Dmsg1(200, "list_result starts second loop looking at %d fields\n", sql_num_fields(mdb));
+   list_dashes(mdb, send, ctx);
+   send(ctx, "|");
+   sql_field_seek(mdb, 0);
+   for (i = 0; i < sql_num_fields(mdb); i++) {
+      Dmsg1(200, "list_result looking at field %d\n", i);
+      field = sql_fetch_field(mdb);
+      bsnprintf(buf, sizeof(buf), " %-*s |", (int)field->max_length, field->name);
+      send(ctx, buf);
+   }
+   send(ctx, "\n");
+   list_dashes(mdb, send, ctx);
+
+   Dmsg1(200, "list_result starts third loop looking at %d fields\n", sql_num_fields(mdb));
+   while ((row = sql_fetch_row(mdb)) != NULL) {
+      sql_field_seek(mdb, 0);
+      send(ctx, "|");
+      for (i = 0; i < sql_num_fields(mdb); i++) {
+        field = sql_fetch_field(mdb);
+        if (row[i] == NULL) {
+            bsnprintf(buf, sizeof(buf), " %-*s |", (int)field->max_length, "NULL");
+        } else if (IS_NUM(field->type) && !jcr->gui) {
+            bsnprintf(buf, sizeof(buf), " %*s |", (int)field->max_length,       
+                     add_commas(row[i], ewc));
+        } else {
+            bsnprintf(buf, sizeof(buf), " %-*s |", (int)field->max_length, row[i]);
+        }
+        send(ctx, buf);
+      }
+      send(ctx, "\n");
+   }
+   list_dashes(mdb, send, ctx);
+   return;
+
+vertical_list:
+   
+   Dmsg1(200, "list_result starts vertical list at %d fields\n", sql_num_fields(mdb));
+   while ((row = sql_fetch_row(mdb)) != NULL) {
+      sql_field_seek(mdb, 0);
+      for (i = 0; i < sql_num_fields(mdb); i++) {
+        field = sql_fetch_field(mdb);
+        if (row[i] == NULL) {
+            bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, "NULL");
+        } else if (IS_NUM(field->type) && !jcr->gui) {
+            bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, 
+               add_commas(row[i], ewc));
+        } else {
+            bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, row[i]);
+        }
+        send(ctx, buf);
+      }
+      send(ctx, "\n");
+   }
+   return;
+}
+
+
+#endif /* HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL */