]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/cats/sql.c
Misc
[bacula/bacula] / bacula / src / cats / sql.c
index 50b96467c799156d35af27248ec06191ebccc48e..ad7ce60caad1692216742c1b46d31ad7a1d9e280 100644 (file)
@@ -52,16 +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);
+   Dmsg1(800, "int_handler starts with row pointing at %x\n", row);
 
    if (row[0]) {
-      Dmsg1(200, "int_handler finds '%s'\n", row[0]);
+      Dmsg1(800, "int_handler finds '%s'\n", row[0]);
       *val = atoi(row[0]);
    } else {
-      Dmsg0(200, "int_handler finds zero\n");
+      Dmsg0(800, "int_handler finds zero\n");
       *val = 0;
    }
-   Dmsg0(200, "int_handler finishes\n");
+   Dmsg0(800, "int_handler finishes\n");
    return 0;
 }
        
@@ -74,7 +74,7 @@ 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(JCR *jcr, B_DB *mdb)
 {
-   char *query = "SELECT VersionId FROM Version";
+   const char *query = "SELECT VersionId FROM Version";
   
    bacula_db_version = 0;
    db_sql_query(mdb, query, int_handler, (void *)&bacula_db_version);
@@ -89,7 +89,7 @@ int check_tables_version(JCR *jcr, B_DB *mdb)
 
 /* Utility routine for queries. The database MUST be locked before calling here. */
 int
-QueryDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
+QueryDB(const char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
 {
    int status;
    if ((status=sql_query(mdb, cmd)) != 0) {
@@ -112,7 +112,7 @@ QueryDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
  *         1 on success
  */
 int
-InsertDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
+InsertDB(const 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));
@@ -145,7 +145,7 @@ InsertDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
  *          1 on success  
  */
 int
-UpdateDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
+UpdateDB(const char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
 {
 
    if (sql_query(mdb, cmd)) {
@@ -157,12 +157,12 @@ UpdateDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
       return 0;
    }
    mdb->num_rows = sql_affected_rows(mdb);
-   if (mdb->num_rows != 1) {
+   if (mdb->num_rows < 1) {
       char ed1[30];
       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);
+//       j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
       }
       return 0;
    }
@@ -176,7 +176,7 @@ UpdateDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
  *          n number of rows affected
  */
 int
-DeleteDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
+DeleteDB(const char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
 {
 
    if (sql_query(mdb, cmd)) {
@@ -232,7 +232,7 @@ char *db_strerror(B_DB *mdb)
  *   thread without blocking, but must be unlocked the number of
  *   times it was locked.
  */
-void _db_lock(char *file, int line, B_DB *mdb)
+void _db_lock(const char *file, int line, B_DB *mdb)
 {
    int errstat;
    if ((errstat=rwl_writelock(&mdb->lock)) != 0) {
@@ -246,7 +246,7 @@ void _db_lock(char *file, int line, B_DB *mdb)
  *   same thread up to the number of times that thread called
  *   db_lock()/
  */
-void _db_unlock(char *file, int line, B_DB *mdb)
+void _db_unlock(const char *file, int line, B_DB *mdb)
 {
    int errstat;
    if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) {
@@ -276,6 +276,23 @@ void db_start_transaction(JCR *jcr, B_DB *mdb)
    db_unlock(mdb);
 #endif
 
+/*
+ * This is turned off because transactions break
+ * if multiple simultaneous jobs are run.    
+ */
+#ifdef xAVE_POSTGRESQL
+   db_lock(mdb);
+   /* Allow only 25,000 changes per transaction */
+   if (mdb->transaction && mdb->changes > 25000) {
+      db_end_transaction(jcr, mdb);
+   }
+   if (!mdb->transaction) {   
+      db_sql_query(mdb, "BEGIN", NULL, NULL);  /* begin transaction */
+      Dmsg0(400, "Start PosgreSQL transaction\n");
+      mdb->transaction = 1;
+   }
+   db_unlock(mdb);
+#endif
 }
 
 void db_end_transaction(JCR *jcr, B_DB *mdb)
@@ -290,6 +307,17 @@ void db_end_transaction(JCR *jcr, B_DB *mdb)
    mdb->changes = 0;
    db_unlock(mdb);
 #endif
+
+#ifdef xAVE_POSTGRESQL
+   db_lock(mdb);
+   if (mdb->transaction) {
+      db_sql_query(mdb, "COMMIT", NULL, NULL); /* end transaction */
+      mdb->transaction = 0;
+      Dmsg1(400, "End PostgreSQL transaction changes=%d\n", mdb->changes);
+   }
+   mdb->changes = 0;
+   db_unlock(mdb);
+#endif
 }
 
 /*
@@ -299,20 +327,55 @@ void db_end_transaction(JCR *jcr, B_DB *mdb)
  */
 void split_path_and_file(JCR *jcr, B_DB *mdb, const char *fname)
 {
-   split_path_and_filename(fname, &mdb->path, &mdb->pnl, &mdb->fname, &mdb->fnl);
-   if (mdb->fnl == 0) {
+   const char *p, *f;
+
+   /* Find path without the filename.  
+    * I.e. everything after the last / is a "filename".
+    * OK, maybe it is a directory name, but we treat it like
+    * a filename. If we don't find a / then the whole name
+    * must be a path name (e.g. c:).
+    */
+   for (p=f=fname; *p; p++) {
+      if (*p == '/') {
+        f = p;                       /* set pos of last slash */
+      }
+   }
+   if (*f == '/') {                   /* did we find a slash? */
+      f++;                           /* yes, point to filename */
+   } else {                          /* no, whole thing must be path name */
+      f = p;
+   }
+
+   /* If filename doesn't exist (i.e. root directory), we
+    * simply create a blank name consisting of a single 
+    * space. This makes handling zero length filenames
+    * easier.
+    */
+   mdb->fnl = p - f;
+   if (mdb->fnl > 0) {
+      mdb->fname = check_pool_memory_size(mdb->fname, mdb->fnl+1);
+      memcpy(mdb->fname, f, mdb->fnl);   /* copy filename */
+      mdb->fname[mdb->fnl] = 0;
+   } else {
       mdb->fname[0] = ' ';            /* blank filename */
       mdb->fname[1] = 0;
       mdb->fnl = 1;
    }
-   if (mdb->pnl == 0) {   
+
+   mdb->pnl = f - fname;    
+   if (mdb->pnl > 0) {
+      mdb->path = check_pool_memory_size(mdb->path, mdb->pnl+1);
+      memcpy(mdb->path, fname, mdb->pnl);
+      mdb->path[mdb->pnl] = 0;
+   } else {
       Mmsg1(&mdb->errmsg, _("Path length is zero. File=%s\n"), fname);
       Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
       mdb->path[0] = ' ';
       mdb->path[1] = 0;
       mdb->pnl = 1;
    }
-   Dmsg2(400, "split path=%s file=%s\n", mdb->path, mdb->fname);
+
+   Dmsg2(500, "split path=%s file=%s\n", mdb->path, mdb->fname);
 }
 
 /*
@@ -348,17 +411,17 @@ list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type t
    int i, col_len, max_len = 0;
    char buf[2000], ewc[30];
 
-   Dmsg0(200, "list_result starts\n");
+   Dmsg0(800, "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));
+   Dmsg1(800, "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);
+      Dmsg1(800, "list_result processing field %d\n", i);
       field = sql_fetch_field(mdb);
       col_len = strlen(field->name);
       if (type == VERT_LIST) {
@@ -379,17 +442,17 @@ list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type t
       }
    }
 
-   Dmsg0(200, "list_result finished first loop\n");
+   Dmsg0(800, "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));
+   Dmsg1(800, "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);
+      Dmsg1(800, "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);
@@ -397,7 +460,7 @@ list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type t
    send(ctx, "\n");
    list_dashes(mdb, send, ctx);
 
-   Dmsg1(200, "list_result starts third loop looking at %d fields\n", sql_num_fields(mdb));
+   Dmsg1(800, "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, "|");
@@ -405,7 +468,7 @@ list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type t
         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) {
+        } else if (IS_NUM(field->type) && !jcr->gui && is_an_integer(row[i])) {
             bsnprintf(buf, sizeof(buf), " %*s |", (int)field->max_length,       
                      add_commas(row[i], ewc));
         } else {
@@ -420,14 +483,14 @@ list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type t
 
 vertical_list:
    
-   Dmsg1(200, "list_result starts vertical list at %d fields\n", sql_num_fields(mdb));
+   Dmsg1(800, "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) {
+        } else if (IS_NUM(field->type) && !jcr->gui && is_an_integer(row[i])) {
             bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, 
                add_commas(row[i], ewc));
         } else {