]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/cats/sql_update.c
Fix bug #1504 -- Error when creating tables in MySQL 5.5
[bacula/bacula] / bacula / src / cats / sql_update.c
index fa605998f7c20e5d08bb62f90f7252c177e93b5a..5ac95e5fbf4bb2f980aa1afa85914e4553734238 100644 (file)
@@ -1,12 +1,12 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
+   Copyright (C) 2000-2008 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
+   modify it under the terms of version three of the GNU Affero General Public
    License as published by the Free Software Foundation and included
    in the file LICENSE.
 
    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
+   You should have received a copy of the GNU Affero 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 John Walker.
+   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.
@@ -30,7 +30,7 @@
  *
  *    Kern Sibbald, March 2000
  *
- *    Version $Id$
+ *    Version $Id: sql_update.c 8478 2009-02-18 20:11:55Z kerns $
  */
 
 /* The following is necessary so that we do not include
@@ -41,7 +41,7 @@
 #include "bacula.h"
 #include "cats.h"
 
-#if    HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL || HAVE_DBI
+#if    HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL || HAVE_INGRES || HAVE_DBI
 
 /* -----------------------------------------------------------------------
  *
@@ -102,7 +102,7 @@ db_update_job_start_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
    struct tm tm;
    btime_t JobTDate;
    int stat;
-   char ed1[50], ed2[50], ed3[50], ed4[50];
+   char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50];
 
    stime = jr->StartTime;
    (void)localtime_r(&stime, &tm);
@@ -111,13 +111,14 @@ db_update_job_start_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
 
    db_lock(mdb);
    Mmsg(mdb->cmd, "UPDATE Job SET JobStatus='%c',Level='%c',StartTime='%s',"
-"ClientId=%s,JobTDate=%s,PoolId=%s WHERE JobId=%s",
+"ClientId=%s,JobTDate=%s,PoolId=%s,FileSetId=%s WHERE JobId=%s",
       (char)(jcr->JobStatus),
       (char)(jr->JobLevel), dt, 
       edit_int64(jr->ClientId, ed1),
       edit_uint64(JobTDate, ed2), 
       edit_int64(jr->PoolId, ed3),
-      edit_int64(jr->JobId, ed4));
+      edit_int64(jr->FileSetId, ed4),
+      edit_int64(jr->JobId, ed5));
 
    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
    mdb->changes = 0;
@@ -126,14 +127,20 @@ db_update_job_start_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
 }
 
 /*
- * Given an incoming integer, set the string buffer to either NULL or the value
- *
+ * Update Long term statistics with all jobs that were run before
+ * age seconds
  */
-static void edit_num_or_null(char *s, size_t n, uint64_t id) {
-   char ed1[50];
-   bsnprintf(s, n, id ? "%s" : "NULL", edit_int64(id, ed1));
-}
+int
+db_update_stats(JCR *jcr, B_DB *mdb, utime_t age)
+{
+   char ed1[30];
+   utime_t now = (utime_t)time(NULL);
+   edit_uint64(now - age, ed1);
 
+   Mmsg(mdb->cmd, fill_jobhisto, ed1);
+   QUERY_DB(jcr, mdb, mdb->cmd); /* TODO: get a message ? */
+   return sql_affected_rows(mdb);
+}
 
 /*
  * Update the Job record at end of Job
@@ -142,22 +149,16 @@ static void edit_num_or_null(char *s, size_t n, uint64_t id) {
  *           1 on success
  */
 int
-db_update_job_end_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr, bool stats_enabled)
+db_update_job_end_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
 {
    char dt[MAX_TIME_LENGTH];
    char rdt[MAX_TIME_LENGTH];
    time_t ttime;
    struct tm tm;
    int stat;
-   char ed1[30], ed2[30], ed3[50];
+   char ed1[30], ed2[30], ed3[50], ed4[50];
    btime_t JobTDate;
-   char PoolId[50], FileSetId[50], ClientId[50], PriorJobId[50];
-
-
-   /* some values are set to zero, which translates to NULL in SQL */
-   edit_num_or_null(PoolId,    sizeof(PoolId),    jr->PoolId);
-   edit_num_or_null(FileSetId, sizeof(FileSetId), jr->FileSetId);
-   edit_num_or_null(ClientId,  sizeof(ClientId),  jr->ClientId);
+   char PriorJobId[50];
 
    if (jr->PriorJobId) {
       bstrncpy(PriorJobId, edit_int64(jr->PriorJobId, ed1), sizeof(PriorJobId));
@@ -181,24 +182,20 @@ db_update_job_end_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr, bool stats_enabled)
    db_lock(mdb);
    Mmsg(mdb->cmd,
       "UPDATE Job SET JobStatus='%c',EndTime='%s',"
-"ClientId=%s,JobBytes=%s,JobFiles=%u,JobErrors=%u,VolSessionId=%u,"
-"VolSessionTime=%u,PoolId=%s,FileSetId=%s,JobTDate=%s,"
-"RealEndTime='%s',PriorJobId=%s WHERE JobId=%s",
-      (char)(jr->JobStatus), dt, ClientId, edit_uint64(jr->JobBytes, ed1),
+"ClientId=%u,JobBytes=%s,ReadBytes=%s,JobFiles=%u,JobErrors=%u,VolSessionId=%u,"
+"VolSessionTime=%u,PoolId=%u,FileSetId=%u,JobTDate=%s,"
+"RealEndTime='%s',PriorJobId=%s,HasBase=%u WHERE JobId=%s",
+      (char)(jr->JobStatus), dt, jr->ClientId, edit_uint64(jr->JobBytes, ed1),
+      edit_uint64(jr->ReadBytes, ed4),
       jr->JobFiles, jr->JobErrors, jr->VolSessionId, jr->VolSessionTime,
-      PoolId, FileSetId, edit_uint64(JobTDate, ed2), 
+      jr->PoolId, jr->FileSetId, edit_uint64(JobTDate, ed2), 
       rdt,
       PriorJobId,
+      jr->HasBase,
       edit_int64(jr->JobId, ed3));
 
    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
 
-   if (stat && stats_enabled) {
-      Mmsg(mdb->cmd,
-          "INSERT INTO JobStat (SELECT * FROM Job WHERE JobId=%s)",
-          edit_int64(jr->JobId, ed3));
-      INSERT_DB(jcr, mdb, mdb->cmd); /* TODO: get a message ? */
-   }
    db_unlock(mdb);
    return stat;
 }
@@ -246,7 +243,7 @@ int db_update_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
    db_lock(mdb);
 
    Mmsg(mdb->cmd,
-"UPDATE Counters SET MinValue=%d,MaxValue=%d,CurrentValue=%d,"
+"UPDATE Counters SET \"MinValue\"=%d,\"MaxValue\"=%d,CurrentValue=%d,"
 "WrapCounter='%s' WHERE Counter='%s'",
       cr->MinValue, cr->MaxValue, cr->CurrentValue,
       cr->WrapCounter, cr->Counter);
@@ -260,7 +257,7 @@ int db_update_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
 int db_update_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
 {
    int stat;
-   char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50];
+   char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50], ed6[50];
 
    db_lock(mdb);
    Mmsg(mdb->cmd, "SELECT count(*) from Media WHERE PoolId=%s",
@@ -272,7 +269,8 @@ int db_update_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
 "UPDATE Pool SET NumVols=%u,MaxVols=%u,UseOnce=%d,UseCatalog=%d,"
 "AcceptAnyVolume=%d,VolRetention='%s',VolUseDuration='%s',"
 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s,Recycle=%d,"
-"AutoPrune=%d,LabelType=%d,LabelFormat='%s',RecyclePoolId=%s WHERE PoolId=%s",
+"AutoPrune=%d,LabelType=%d,LabelFormat='%s',RecyclePoolId=%s,"
+"ScratchPoolId=%s,ActionOnPurge=%d WHERE PoolId=%s",
       pr->NumVols, pr->MaxVols, pr->UseOnce, pr->UseCatalog,
       pr->AcceptAnyVolume, edit_uint64(pr->VolRetention, ed1),
       edit_uint64(pr->VolUseDuration, ed2),
@@ -280,8 +278,9 @@ int db_update_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
       edit_uint64(pr->MaxVolBytes, ed3),
       pr->Recycle, pr->AutoPrune, pr->LabelType,
       pr->LabelFormat, edit_int64(pr->RecyclePoolId,ed5),
+      edit_int64(pr->ScratchPoolId,ed6),
+      pr->ActionOnPurge,
       ed4);
-
    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
    db_unlock(mdb);
    return stat;
@@ -292,7 +291,6 @@ db_update_storage_record(JCR *jcr, B_DB *mdb, STORAGE_DBR *sr)
 {
    int stat;
    char ed1[50];
-
    db_lock(mdb);
    Mmsg(mdb->cmd, "UPDATE Storage SET AutoChanger=%d WHERE StorageId=%s", 
       sr->AutoChanger, edit_int64(sr->StorageId, ed1));
@@ -370,7 +368,7 @@ db_update_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
         "Slot=%d,InChanger=%d,VolReadTime=%s,VolWriteTime=%s,VolParts=%d,"
         "LabelType=%d,StorageId=%s,PoolId=%s,VolRetention=%s,VolUseDuration=%s,"
         "MaxVolJobs=%d,MaxVolFiles=%d,Enabled=%d,LocationId=%s,"
-        "ScratchPoolId=%s,RecyclePoolId=%s,RecycleCount=%d"
+        "ScratchPoolId=%s,RecyclePoolId=%s,RecycleCount=%d,Recycle=%d,ActionOnPurge=%d"
         " WHERE VolumeName='%s'",
         mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
         mr->VolMounts, mr->VolErrors, mr->VolWrites,
@@ -388,7 +386,7 @@ db_update_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
         mr->Enabled, edit_uint64(mr->LocationId, ed9),
         edit_uint64(mr->ScratchPoolId, ed10),
         edit_uint64(mr->RecyclePoolId, ed11),
-        mr->RecycleCount,
+        mr->RecycleCount,mr->Recycle, mr->ActionOnPurge,
         mr->VolumeName);
 
    Dmsg1(400, "%s\n", mdb->cmd);
@@ -418,10 +416,10 @@ db_update_media_defaults(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
    db_lock(mdb);
    if (mr->VolumeName[0]) {
       Mmsg(mdb->cmd, "UPDATE Media SET "
-           "Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
+           "ActionOnPurge=%d, Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
            "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s,RecyclePoolId=%s"
            " WHERE VolumeName='%s'",
-           mr->Recycle,edit_uint64(mr->VolRetention, ed1),
+           mr->ActionOnPurge, mr->Recycle,edit_uint64(mr->VolRetention, ed1),
            edit_uint64(mr->VolUseDuration, ed2),
            mr->MaxVolJobs, mr->MaxVolFiles,
            edit_uint64(mr->MaxVolBytes, ed3),
@@ -429,10 +427,10 @@ db_update_media_defaults(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
            mr->VolumeName);
    } else {
       Mmsg(mdb->cmd, "UPDATE Media SET "
-           "Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
+           "ActionOnPurge=%d, Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
            "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s,RecyclePoolId=%s"
            " WHERE PoolId=%s",
-           mr->Recycle,edit_uint64(mr->VolRetention, ed1),
+           mr->ActionOnPurge, mr->Recycle,edit_uint64(mr->VolRetention, ed1),
            edit_uint64(mr->VolUseDuration, ed2),
            mr->MaxVolJobs, mr->MaxVolFiles,
            edit_uint64(mr->MaxVolBytes, ed3),
@@ -459,14 +457,29 @@ void
 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
 {
    char ed1[50], ed2[50];
-   if (mr->InChanger != 0 && mr->Slot != 0) {
-      Mmsg(mdb->cmd, "UPDATE Media SET InChanger=0 WHERE "
-           "Slot=%d AND StorageId=%s AND MediaId!=%s",
-            mr->Slot, 
-            edit_int64(mr->StorageId, ed1), edit_int64(mr->MediaId, ed2));
-      Dmsg1(400, "%s\n", mdb->cmd);
-      UPDATE_DB(jcr, mdb, mdb->cmd);
+   if (mr->InChanger != 0 && mr->Slot != 0 && mr->StorageId != 0) {
+
+       if (mr->MediaId != 0) {
+          Mmsg(mdb->cmd, "UPDATE Media SET InChanger=0, Slot=0 WHERE "
+               "Slot=%d AND StorageId=%s AND MediaId!=%s",
+               mr->Slot, 
+               edit_int64(mr->StorageId, ed1), edit_int64(mr->MediaId, ed2));
+
+       } else if (*mr->VolumeName) {
+          Mmsg(mdb->cmd, "UPDATE Media SET InChanger=0, Slot=0 WHERE "
+               "Slot=%d AND StorageId=%s AND VolumeName!='%s'",
+               mr->Slot, 
+               edit_int64(mr->StorageId, ed1), mr->VolumeName);
+
+       } else {  /* used by ua_label to reset all volume with this slot */
+          Mmsg(mdb->cmd, "UPDATE Media SET InChanger=0, Slot=0 WHERE "
+               "Slot=%d AND StorageId=%s",
+               mr->Slot, 
+               edit_int64(mr->StorageId, ed1), mr->VolumeName);          
+       }
+       Dmsg1(100, "%s\n", mdb->cmd);
+       UPDATE_DB(jcr, mdb, mdb->cmd);
    }
 }
 
-#endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL*/
+#endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL || HAVE_INGRES */