]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/cats/sql_find.c
Merge branch 'master' into basejobv3
[bacula/bacula] / bacula / src / cats / sql_find.c
index 641a1e77007f98aa913bfc76ee4b7c559d90cced..1ce51e3ff1191452325c4aea2bf63e0a4919f93d 100644 (file)
@@ -1,3 +1,30 @@
+/*
+   Bacula® - The Network Backup Solution
+
+   Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
+
+   The main author of Bacula is Kern Sibbald, with contributions from
+   many others, a complete list can be found in the file AUTHORS.
+   This program is Free Software; you can redistribute it and/or
+   modify it under the terms of version two of the GNU General Public
+   License as published by the Free Software Foundation and included
+   in the file LICENSE.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
+
+   Bacula® is a registered trademark of Kern Sibbald.
+   The licensor of Bacula is the Free Software Foundation Europe
+   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
+   Switzerland, email:ftf@fsfeurope.org.
+*/
 /*
  * Bacula Catalog Database Find record interface routines
  *
@@ -9,21 +36,6 @@
  *
  *    Version $Id$
  */
-/*
-   Copyright (C) 2000-2006 Kern Sibbald
-
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License
-   version 2 as amended with additional clauses defined in the
-   file LICENSE in the main source directory.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
-   the file LICENSE for additional details.
-
- */
-
 
 
 /* The following is necessary so that we do not include
@@ -34,7 +46,7 @@
 #include "bacula.h"
 #include "cats.h"
 
-#if    HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL
+#if    HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL || HAVE_DBI
 
 /* -----------------------------------------------------------------------
  *
  * -----------------------------------------------------------------------
  */
 
-/* Imported subroutines */
-extern void print_result(B_DB *mdb);
-extern int QueryDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
-
 /*
  * Find job start time if JobId specified, otherwise
- * find last full save for Incremental and Differential saves.
+ * find last Job start time Incremental and Differential saves.
  *
  *  StartTime is returned in stime
  *
@@ -68,8 +76,8 @@ db_find_job_start_time(JCR *jcr, B_DB *mdb, JOB_DBR *jr, POOLMEM **stime)
    /* If no Id given, we must find corresponding job */
    if (jr->JobId == 0) {
       /* Differential is since last Full backup */
-         Mmsg(mdb->cmd,
-"SELECT StartTime FROM Job WHERE JobStatus='T' AND Type='%c' AND "
+      Mmsg(mdb->cmd,
+"SELECT StartTime FROM Job WHERE JobStatus IN ('T','W') AND Type='%c' AND "
 "Level='%c' AND Name='%s' AND ClientId=%s AND FileSetId=%s "
 "ORDER BY StartTime DESC LIMIT 1",
            jr->JobType, L_FULL, jr->Name, 
@@ -99,7 +107,7 @@ db_find_job_start_time(JCR *jcr, B_DB *mdb, JOB_DBR *jr, POOLMEM **stime)
          sql_free_result(mdb);
          /* Now edit SQL command for Incremental Job */
          Mmsg(mdb->cmd,
-"SELECT StartTime FROM Job WHERE JobStatus='T' AND Type='%c' AND "
+"SELECT StartTime FROM Job WHERE JobStatus IN ('T','W') AND Type='%c' AND "
 "Level IN ('%c','%c','%c') AND Name='%s' AND ClientId=%s "
 "AND FileSetId=%s ORDER BY StartTime DESC LIMIT 1",
             jr->JobType, L_INCREMENTAL, L_DIFFERENTIAL, L_FULL, jr->Name,
@@ -140,6 +148,52 @@ bail_out:
    return false;
 }
 
+
+/*
+ * Find the last job start time for the specified JobLevel
+ *
+ *  StartTime is returned in stime
+ *
+ * Returns: false on failure
+ *          true  on success, jr is unchanged, but stime is set
+ */
+bool
+db_find_last_job_start_time(JCR *jcr, B_DB *mdb, JOB_DBR *jr, POOLMEM **stime, int JobLevel)
+{
+   SQL_ROW row;
+   char ed1[50], ed2[50];
+
+   db_lock(mdb);
+
+   pm_strcpy(stime, "0000-00-00 00:00:00");   /* default */
+
+   Mmsg(mdb->cmd,
+"SELECT StartTime FROM Job WHERE JobStatus IN ('T','W') AND Type='%c' AND "
+"Level='%c' AND Name='%s' AND ClientId=%s AND FileSetId=%s "
+"ORDER BY StartTime DESC LIMIT 1",
+      jr->JobType, JobLevel, jr->Name, 
+      edit_int64(jr->ClientId, ed1), edit_int64(jr->FileSetId, ed2));
+   if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
+      Mmsg2(&mdb->errmsg, _("Query error for start time request: ERR=%s\nCMD=%s\n"),
+         sql_strerror(mdb), mdb->cmd);
+      goto bail_out;
+   }
+   if ((row = sql_fetch_row(mdb)) == NULL) {
+      sql_free_result(mdb);
+      Mmsg(mdb->errmsg, _("No prior Full backup Job record found.\n"));
+      goto bail_out;
+   }
+   Dmsg1(100, "Got start time: %s\n", row[0]);
+   pm_strcpy(stime, row[0]);
+   sql_free_result(mdb);
+   db_unlock(mdb);
+   return true;
+
+bail_out:
+   db_unlock(mdb);
+   return false;
+}
+
 /*
  * Find last failed job since given start-time
  *   it must be either Full or Diff.
@@ -203,7 +257,7 @@ db_find_last_jobid(JCR *jcr, B_DB *mdb, const char *Name, JOB_DBR *jr)
    if (jr->JobLevel == L_VERIFY_CATALOG) {
       Mmsg(mdb->cmd,
 "SELECT JobId FROM Job WHERE Type='V' AND Level='%c' AND "
-" JobStatus='T' AND Name='%s' AND "
+" JobStatus IN ('T','W') AND Name='%s' AND "
 "ClientId=%s ORDER BY StartTime DESC LIMIT 1",
            L_VERIFY_INIT, jr->Name, 
            edit_int64(jr->ClientId, ed1));
@@ -212,11 +266,11 @@ db_find_last_jobid(JCR *jcr, B_DB *mdb, const char *Name, JOB_DBR *jr)
               jr->JobType == JT_BACKUP) {
       if (Name) {
          Mmsg(mdb->cmd,
-"SELECT JobId FROM Job WHERE Type='B' AND JobStatus='T' AND "
+"SELECT JobId FROM Job WHERE Type='B' AND JobStatus IN ('T','W') AND "
 "Name='%s' ORDER BY StartTime DESC LIMIT 1", Name);
       } else {
          Mmsg(mdb->cmd,
-"SELECT JobId FROM Job WHERE Type='B' AND JobStatus='T' AND "
+"SELECT JobId FROM Job WHERE Type='B' AND JobStatus IN ('T','W') AND "
 "ClientId=%s ORDER BY StartTime DESC LIMIT 1", 
            edit_int64(jr->ClientId, ed1));
       }
@@ -262,7 +316,7 @@ db_find_last_jobid(JCR *jcr, B_DB *mdb, const char *Name, JOB_DBR *jr)
 int
 db_find_next_volume(JCR *jcr, B_DB *mdb, int item, bool InChanger, MEDIA_DBR *mr)
 {
-   SQL_ROW row;
+   SQL_ROW row = NULL;
    int numrows;
    const char *order;
 
@@ -272,69 +326,78 @@ db_find_next_volume(JCR *jcr, B_DB *mdb, int item, bool InChanger, MEDIA_DBR *mr
    if (item == -1) {       /* find oldest volume */
       /* Find oldest volume */
       Mmsg(mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,"
-          "VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,"
-          "VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,Recycle,Slot,"
-          "FirstWritten,LastWritten,VolStatus,InChanger,VolParts,"
-          "LabelType "
-          "FROM Media WHERE PoolId=%s AND MediaType='%s' AND VolStatus IN ('Full',"
-          "'Recycle','Purged','Used','Append') "
-          "ORDER BY LastWritten LIMIT 1", 
-          edit_int64(mr->PoolId, ed1), mr->MediaType);
+         "VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,"
+         "MediaType,VolStatus,PoolId,VolRetention,VolUseDuration,MaxVolJobs,"
+         "MaxVolFiles,Recycle,Slot,FirstWritten,LastWritten,InChanger,"
+         "EndFile,EndBlock,VolParts,LabelType,LabelDate,StorageId,"
+         "Enabled,LocationId,RecycleCount,InitialWrite,"
+         "ScratchPoolId,RecyclePoolId,VolReadTime,VolWriteTime "
+         "FROM Media WHERE PoolId=%s AND MediaType='%s' AND VolStatus IN ('Full',"
+         "'Recycle','Purged','Used','Append') AND Enabled=1 "
+         "ORDER BY LastWritten LIMIT 1", 
+         edit_int64(mr->PoolId, ed1), mr->MediaType);
      item = 1;
    } else {
-      char changer[100];
+      POOL_MEM changer(PM_FNAME);
       /* Find next available volume */
       if (InChanger) {
-         bsnprintf(changer, sizeof(changer), "AND InChanger=1 AND StorageId=%s",
-            edit_int64(mr->StorageId, ed1));
-      } else {
-         changer[0] = 0;
+         Mmsg(changer, "AND InChanger=1 AND StorageId=%s",
+              edit_int64(mr->StorageId, ed1));
       }
-      if (strcmp(mr->VolStatus, "Recycled") == 0 ||
+      if (strcmp(mr->VolStatus, "Recycle") == 0 ||
           strcmp(mr->VolStatus, "Purged") == 0) {
-         order = "ORDER BY LastWritten ASC,MediaId";  /* take oldest */
+         order = "AND Recycle=1 ORDER BY LastWritten ASC,MediaId";  /* take oldest that can be recycled */
       } else {
          order = "ORDER BY LastWritten IS NULL,LastWritten DESC,MediaId";   /* take most recently written */
       }
       Mmsg(mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,"
-          "VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,"
-          "VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,Recycle,Slot,"
-          "FirstWritten,LastWritten,VolStatus,InChanger,VolParts,"
-          "LabelType "
-          "FROM Media WHERE PoolId=%s AND MediaType='%s' AND VolStatus='%s' "
-          "%s "
-          "%s LIMIT %d",
-          edit_int64(mr->PoolId, ed1), mr->MediaType,
-          mr->VolStatus, changer, order, item);
+         "VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,"
+         "MediaType,VolStatus,PoolId,VolRetention,VolUseDuration,MaxVolJobs,"
+         "MaxVolFiles,Recycle,Slot,FirstWritten,LastWritten,InChanger,"
+         "EndFile,EndBlock,VolParts,LabelType,LabelDate,StorageId,"
+         "Enabled,LocationId,RecycleCount,InitialWrite,"
+         "ScratchPoolId,RecyclePoolId,VolReadTime,VolWriteTime "
+         "FROM Media WHERE PoolId=%s AND MediaType='%s' AND Enabled=1 "
+         "AND VolStatus='%s' "
+         "%s "
+         "%s LIMIT %d",
+         edit_int64(mr->PoolId, ed1), mr->MediaType,
+         mr->VolStatus, changer.c_str(), order, item);
    }
+   Dmsg1(050, "fnextvol=%s\n", mdb->cmd);
    if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
       db_unlock(mdb);
       return 0;
    }
 
    numrows = sql_num_rows(mdb);
-   if (item > numrows) {
-      Mmsg2(&mdb->errmsg, _("Request for Volume item %d greater than max %d\n"),
+   if (item > numrows || item < 1) {
+      Dmsg2(050, "item=%d got=%d\n", item, numrows);
+      Mmsg2(&mdb->errmsg, _("Request for Volume item %d greater than max %d or less than 1\n"),
          item, numrows);
       db_unlock(mdb);
       return 0;
    }
 
-   /* Seek to desired item
-    * Note, we use base 1; SQL uses base 0
+   /* Note, we previously seeked to the row using:
+    *  sql_data_seek(mdb, item-1);
+    * but this failed on PostgreSQL, so now we loop
+    * over all the records.  This should not be too horrible since
+    * the maximum Volumes we look at in any case is 20.
     */
-   sql_data_seek(mdb, item-1);
-
-   if ((row = sql_fetch_row(mdb)) == NULL) {
-      Mmsg1(&mdb->errmsg, _("No Volume record found for item %d.\n"), item);
-      sql_free_result(mdb);
-      db_unlock(mdb);
-      return 0;
+   while (item-- > 0) {
+      if ((row = sql_fetch_row(mdb)) == NULL) {
+         Dmsg1(050, "Fail fetch item=%d\n", item+1);
+         Mmsg1(&mdb->errmsg, _("No Volume record found for item %d.\n"), item);
+         sql_free_result(mdb);
+         db_unlock(mdb);
+         return 0;
+      }
    }
 
    /* Return fields in Media Record */
    mr->MediaId = str_to_int64(row[0]);
-   bstrncpy(mr->VolumeName, row[1], sizeof(mr->VolumeName));
+   bstrncpy(mr->VolumeName, row[1]!=NULL?row[1]:"", sizeof(mr->VolumeName));
    mr->VolJobs = str_to_int64(row[2]);
    mr->VolFiles = str_to_int64(row[3]);
    mr->VolBlocks = str_to_int64(row[4]);
@@ -344,23 +407,41 @@ db_find_next_volume(JCR *jcr, B_DB *mdb, int item, bool InChanger, MEDIA_DBR *mr
    mr->VolWrites = str_to_int64(row[8]);
    mr->MaxVolBytes = str_to_uint64(row[9]);
    mr->VolCapacityBytes = str_to_uint64(row[10]);
-   mr->VolRetention = str_to_uint64(row[11]);
-   mr->VolUseDuration = str_to_uint64(row[12]);
-   mr->MaxVolJobs = str_to_int64(row[13]);
-   mr->MaxVolFiles = str_to_int64(row[14]);
-   mr->Recycle = str_to_int64(row[15]);
-   mr->Slot = str_to_int64(row[16]);
-   bstrncpy(mr->cFirstWritten, row[17]!=NULL?row[17]:"", sizeof(mr->cFirstWritten));
+   bstrncpy(mr->MediaType, row[11]!=NULL?row[11]:"", sizeof(mr->MediaType));
+   bstrncpy(mr->VolStatus, row[12]!=NULL?row[12]:"", sizeof(mr->VolStatus));
+   mr->PoolId = str_to_int64(row[13]);
+   mr->VolRetention = str_to_uint64(row[14]);
+   mr->VolUseDuration = str_to_uint64(row[15]);
+   mr->MaxVolJobs = str_to_int64(row[16]);
+   mr->MaxVolFiles = str_to_int64(row[17]);
+   mr->Recycle = str_to_int64(row[18]);
+   mr->Slot = str_to_int64(row[19]);
+   bstrncpy(mr->cFirstWritten, row[20]!=NULL?row[20]:"", sizeof(mr->cFirstWritten));
    mr->FirstWritten = (time_t)str_to_utime(mr->cFirstWritten);
-   bstrncpy(mr->cLastWritten, row[18]!=NULL?row[18]:"", sizeof(mr->cLastWritten));
+   bstrncpy(mr->cLastWritten, row[21]!=NULL?row[21]:"", sizeof(mr->cLastWritten));
    mr->LastWritten = (time_t)str_to_utime(mr->cLastWritten);
-   bstrncpy(mr->VolStatus, row[19], sizeof(mr->VolStatus));
-   mr->InChanger = str_to_int64(row[20]);
-   mr->VolParts = str_to_int64(row[21]);
-   mr->LabelType = str_to_int64(row[22]);
+   mr->InChanger = str_to_uint64(row[22]);
+   mr->EndFile = str_to_uint64(row[23]);
+   mr->EndBlock = str_to_uint64(row[24]);
+   mr->VolParts = str_to_int64(row[25]);
+   mr->LabelType = str_to_int64(row[26]);
+   bstrncpy(mr->cLabelDate, row[27]!=NULL?row[27]:"", sizeof(mr->cLabelDate));
+   mr->LabelDate = (time_t)str_to_utime(mr->cLabelDate);
+   mr->StorageId = str_to_int64(row[28]);
+   mr->Enabled = str_to_int64(row[29]);
+   mr->LocationId = str_to_int64(row[30]);
+   mr->RecycleCount = str_to_int64(row[31]);
+   bstrncpy(mr->cInitialWrite, row[32]!=NULL?row[32]:"", sizeof(mr->cInitialWrite));
+   mr->InitialWrite = (time_t)str_to_utime(mr->cInitialWrite);
+   mr->ScratchPoolId = str_to_int64(row[33]);
+   mr->RecyclePoolId = str_to_int64(row[34]);
+   mr->VolReadTime = str_to_int64(row[35]);
+   mr->VolWriteTime = str_to_int64(row[36]);
+
    sql_free_result(mdb);
 
    db_unlock(mdb);
+   Dmsg1(050, "Rtn numrows=%d\n", numrows);
    return numrows;
 }