]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/cats/sql_get.c
Fix and document new queries
[bacula/bacula] / bacula / src / cats / sql_get.c
index 0509a8fd6b0d07c05d8a39d590a5f2c755493414..6da66a75bdfc184e81b91290029e62cc285daca3 100644 (file)
@@ -1,7 +1,7 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
+   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.
@@ -57,7 +57,6 @@
 /* Forward referenced functions */
 static int db_get_file_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr, FILE_DBR *fdbr);
 static int db_get_filename_record(JCR *jcr, B_DB *mdb);
-static int db_get_path_record(JCR *jcr, B_DB *mdb);
 
 
 /*
@@ -229,7 +228,7 @@ static int db_get_filename_record(JCR *jcr, B_DB *mdb)
  *
  *   DO NOT use Jmsg in this routine (see notes for get_file_record)
  */
-static int db_get_path_record(JCR *jcr, B_DB *mdb)
+int db_get_path_record(JCR *jcr, B_DB *mdb)
 {
    SQL_ROW row;
    uint32_t PathId = 0;
@@ -297,13 +296,13 @@ bool db_get_job_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
       Mmsg(mdb->cmd, "SELECT VolSessionId,VolSessionTime,"
 "PoolId,StartTime,EndTime,JobFiles,JobBytes,JobTDate,Job,JobStatus,"
 "Type,Level,ClientId,Name,PriorJobId,RealEndTime,JobId,FileSetId,"
-"SchedTime,RealEndTime,ReadBytes "
+"SchedTime,RealEndTime,ReadBytes,HasBase "
 "FROM Job WHERE Job='%s'", jr->Job);
     } else {
       Mmsg(mdb->cmd, "SELECT VolSessionId,VolSessionTime,"
 "PoolId,StartTime,EndTime,JobFiles,JobBytes,JobTDate,Job,JobStatus,"
 "Type,Level,ClientId,Name,PriorJobId,RealEndTime,JobId,FileSetId,"
-"SchedTime,RealEndTime,ReadBytes "
+"SchedTime,RealEndTime,ReadBytes,HasBase "
 "FROM Job WHERE JobId=%s", 
           edit_int64(jr->JobId, ed1));
     }
@@ -346,6 +345,7 @@ bool db_get_job_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
    jr->SchedTime = str_to_utime(jr->cSchedTime);
    jr->EndTime = str_to_utime(jr->cEndTime);
    jr->RealEndTime = str_to_utime(jr->cRealEndTime);
+   jr->HasBase = str_to_int64(row[21]);
    sql_free_result(mdb);
 
    db_unlock(mdb);
@@ -1044,10 +1044,12 @@ bool db_get_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
 }
 
 /*
- * Find the last "accurate" backup state (that can take deleted files in account)
+ * Find the last "accurate" backup state (that can take deleted files in
+ * account)
  * 1) Get all files with jobid in list (F subquery)
  *    Get all files in BaseFiles with jobid in list
- * 2) Take only the last version of each file (Temp subquery) => accurate list is ok
+ * 2) Take only the last version of each file (Temp subquery) => accurate list
+ *    is ok
  * 3) Join the result to file table to get fileindex, jobid and lstat information
  *
  * TODO: See if we can do the SORT only if needed (as an argument)
@@ -1065,30 +1067,23 @@ bool db_get_file_list(JCR *jcr, B_DB *mdb, char *jobids,
          
 #define new_db_get_file_list
 #ifdef new_db_get_file_list
-   /* This is broken, at least if called from ua_restore.c */
+   POOL_MEM buf2(PM_MESSAGE);
+   Mmsg(buf2, select_recent_version_with_basejob[db_type], 
+        jobids, jobids, jobids);
    Mmsg(buf,
- "SELECT Path.Path, Filename.Name, File.FileIndex, File.JobId, File.LStat "
- "FROM ( "
-  "SELECT max(FileId) as FileId, PathId, FilenameId "
-    "FROM (SELECT FileId, PathId, FilenameId FROM File WHERE JobId IN (%s) "
-           "UNION "
-          "SELECT File.FileId, PathId, FilenameId "
-            "FROM BaseFiles JOIN File USING (FileId) "
-           "WHERE BaseFiles.JobId IN (%s) "
-          ") AS F "
-   "GROUP BY PathId, FilenameId "
-  ") AS Temp "
+ "SELECT Path.Path, Filename.Name, FileIndex, JobId, "
+        "LStat, MD5 "
+ "FROM ( %s ) AS Temp "
  "JOIN Filename ON (Filename.FilenameId = Temp.FilenameId) "
  "JOIN Path ON (Path.PathId = Temp.PathId) "
- "JOIN File ON (File.FileId = Temp.FileId) "
-"WHERE File.FileIndex > 0 ORDER BY JobId, FileIndex ASC",/* Return sorted by JobId, */
-                                                         /* FileIndex for restore code */ 
-        jobids, jobids);
-   Dmsg1(0, "q=%s\n", buf.c_str());
+"WHERE FileIndex > 0 ORDER BY JobId, FileIndex ASC",/* Return sorted by JobId, */
+                                                    /* FileIndex for restore code */ 
+        buf2.c_str());
+   Dmsg1(0, "sql=%s\n", buf.c_str());
 #else
    /*  
-    * I am not sure that this works the same as the code in ua_restore.c
-    *  but it is very similar. The accurate-test fails in a restore. Bad file count.
+    * I am not sure that this works the same as the code in ua_restore.c but it
+    *  is very similar. The accurate-test fails in a restore. Bad file count.
     */
    Mmsg(buf, uar_sel_files, jobids);
 #endif
@@ -1096,6 +1091,21 @@ bool db_get_file_list(JCR *jcr, B_DB *mdb, char *jobids,
    return db_sql_query(mdb, buf.c_str(), result_handler, ctx);
 }
 
+/*
+ * This procedure gets the base jobid list used by jobids,
+ */
+bool db_get_used_base_jobids(JCR *jcr, B_DB *mdb, 
+                             POOLMEM *jobids, db_list_ctx *result)
+{
+   POOL_MEM buf;
+   Mmsg(buf,
+ "SELECT DISTINCT BaseJobId "
+ "  FROM Job JOIN BaseFiles USING (JobId) "
+ " WHERE Job.HasBase = 1 "
+ "   AND JobId IN (%s) ", jobids);
+   return db_sql_query(mdb, buf.c_str(), db_list_handler, result);
+}
+
 /* The decision do change an incr/diff was done before
  * Full : do nothing
  * Differential : get the last full id
@@ -1107,7 +1117,7 @@ bool db_get_file_list(JCR *jcr, B_DB *mdb, char *jobids,
  * TODO: look and merge from ua_restore.c
  */
 bool db_accurate_get_jobids(JCR *jcr, B_DB *mdb, 
-                            JOB_DBR *jr, POOLMEM *jobids)
+                            JOB_DBR *jr, db_list_ctx *jobids)
 {
    bool ret=false;
    char clientid[50], jobid[50], filesetid[50];
@@ -1118,7 +1128,8 @@ bool db_accurate_get_jobids(JCR *jcr, B_DB *mdb,
    utime_t StartTime = (jr->StartTime)?jr->StartTime:time(NULL);
 
    bstrutime(date, sizeof(date),  StartTime + 1);
-   jobids[0]='\0';
+   jobids->list[0] = 0;
+   jobids->count = 0;
 
    /* First, find the last good Full backup for this job/client/fileset */
    Mmsg(query, 
@@ -1184,8 +1195,8 @@ bool db_accurate_get_jobids(JCR *jcr, B_DB *mdb,
 
    /* build a jobid list ie: 1,2,3,4 */
    Mmsg(query, "SELECT JobId FROM btemp3%s ORDER by JobTDate", jobid);
-   db_sql_query(mdb, query.c_str(), db_get_int_handler, jobids);
-   Dmsg1(1, "db_accurate_get_jobids=%s\n", jobids);
+   db_sql_query(mdb, query.c_str(), db_list_handler, jobids);
+   Dmsg1(1, "db_accurate_get_jobids=%s\n", jobids->list);
    ret = true;
 
 bail_out:
@@ -1195,6 +1206,19 @@ bail_out:
    return ret;
 }
 
+bool db_get_base_file_list(JCR *jcr, B_DB *mdb,
+                           DB_RESULT_HANDLER *result_handler, void *ctx)
+{
+   POOL_MEM buf(PM_MESSAGE);
+         
+   Mmsg(buf,
+ "SELECT Path, Name, FileIndex, JobId, LStat, MD5 "
+   "FROM new_basefile%lld ORDER BY JobId, FileIndex ASC",
+        (uint64_t) jcr->JobId);
+
+   return db_sql_query(mdb, buf.c_str(), result_handler, ctx);
+}
+
 bool db_get_base_jobid(JCR *jcr, B_DB *mdb, JOB_DBR *jr, JobId_t *jobid)
 {
    char date[MAX_TIME_LENGTH];
@@ -1223,13 +1247,13 @@ bool db_get_base_jobid(JCR *jcr, B_DB *mdb, JOB_DBR *jr, JobId_t *jobid)
 //      edit_uint64(jr->FileSetId, filesetid));
         date);
 
-   Dmsg1(1, "db_get_base_jobid q=%s\n", query.c_str());
+   Dmsg1(10, "db_get_base_jobid q=%s\n", query.c_str());
    if (!db_sql_query(mdb, query.c_str(), db_int64_handler, &id)) {
       goto bail_out;
    }
    *jobid = (JobId_t) id;
 
-   Dmsg1(1, "db_get_base_jobid=%lld\n", id);
+   Dmsg1(10, "db_get_base_jobid=%lld\n", id);
    return true;
 
 bail_out: