]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/cats/sql_create.c
Tweak insert_autokey to use unsigned int64 instead of signed int64.
[bacula/bacula] / bacula / src / cats / sql_create.c
index 44e016c46549b292cc0261c3b6df9fd8e7873377..3dcc13c81e5707f5020157a784c0be3289fb7488 100644 (file)
@@ -6,7 +6,7 @@
    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.
 
@@ -15,7 +15,7 @@
    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.
@@ -625,7 +625,7 @@ int db_create_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
    }
 
    /* Must create it */
-   Mmsg(mdb->cmd, "INSERT INTO Counters (Counter,MinValue,MaxValue,CurrentValue,"
+   Mmsg(mdb->cmd, "INSERT INTO Counters (Counter,\"MinValue\",\"MaxValue\",CurrentValue,"
       "WrapCounter) VALUES ('%s','%d','%d','%d','%s')",
       cr->Counter, cr->MinValue, cr->MaxValue, cr->CurrentValue,
       cr->WrapCounter);
@@ -818,11 +818,12 @@ bool my_batch_end(JCR *jcr, B_DB *mdb, const char *error)
 }
 
 /* 
- * Returns 1 if OK
- *         0 if failed
+ * Returns true if OK
+ *         false if failed
  */
 bool db_write_batch_file_records(JCR *jcr)
 {
+   bool retval = false;
    int JobStatus = jcr->JobStatus;
 
    if (!jcr->batch_started) {         /* no files to backup ? */
@@ -830,7 +831,7 @@ bool db_write_batch_file_records(JCR *jcr)
       return true;
    }
    if (job_canceled(jcr)) {
-      return false;
+      goto bail_out;
    }
 
    Dmsg1(50,"db_create_file_record changes=%u\n",jcr->db_batch->changes);
@@ -838,49 +839,52 @@ bool db_write_batch_file_records(JCR *jcr)
    jcr->JobStatus = JS_AttrInserting;
    if (!sql_batch_end(jcr, jcr->db_batch, NULL)) {
       Jmsg1(jcr, M_FATAL, 0, "Batch end %s\n", jcr->db_batch->errmsg);
-      return false;
+      goto bail_out;
    }
    if (job_canceled(jcr)) {
-      return false;
+      goto bail_out;
    }
 
-
-   /* we have to lock tables */
+   /*
+    * We have to lock tables
+    */
    if (!db_sql_query(jcr->db_batch, sql_batch_lock_path_query, NULL, NULL)) {
       Jmsg1(jcr, M_FATAL, 0, "Lock Path table %s\n", jcr->db_batch->errmsg);
-      return false;
+      goto bail_out;
    }
 
    if (!db_sql_query(jcr->db_batch, sql_batch_fill_path_query, NULL, NULL)) {
       Jmsg1(jcr, M_FATAL, 0, "Fill Path table %s\n",jcr->db_batch->errmsg);
       db_sql_query(jcr->db_batch, sql_batch_unlock_tables_query, NULL, NULL);
-      return false;
+      goto bail_out;
    }
    
    if (!db_sql_query(jcr->db_batch, sql_batch_unlock_tables_query,NULL,NULL)) {
       Jmsg1(jcr, M_FATAL, 0, "Unlock Path table %s\n", jcr->db_batch->errmsg);
-      return false;      
+      goto bail_out;
    }
 
-   /* we have to lock tables */
+   /*
+    * We have to lock tables
+    */
    if (!db_sql_query(jcr->db_batch,sql_batch_lock_filename_query,NULL, NULL)) {
       Jmsg1(jcr, M_FATAL, 0, "Lock Filename table %s\n", jcr->db_batch->errmsg);
-      return false;
+      goto bail_out;
    }
    
    if (!db_sql_query(jcr->db_batch,sql_batch_fill_filename_query, NULL,NULL)) {
       Jmsg1(jcr,M_FATAL,0,"Fill Filename table %s\n",jcr->db_batch->errmsg);
       db_sql_query(jcr->db_batch, sql_batch_unlock_tables_query, NULL, NULL);
-      return false;            
+      goto bail_out;
    }
 
    if (!db_sql_query(jcr->db_batch, sql_batch_unlock_tables_query,NULL,NULL)) {
       Jmsg1(jcr, M_FATAL, 0, "Unlock Filename table %s\n", jcr->db_batch->errmsg);
-      return false;
+      goto bail_out;
    }
    
    if (!db_sql_query(jcr->db_batch, 
-       "INSERT INTO File (FileIndex, JobId, PathId, FilenameId, LStat, MD5)"
+       "INSERT INTO File (FileIndex, JobId, PathId, FilenameId, LStat, MD5) "
          "SELECT batch.FileIndex, batch.JobId, Path.PathId, "
                 "Filename.FilenameId,batch.LStat, batch.MD5 "
            "FROM batch "
@@ -889,13 +893,16 @@ bool db_write_batch_file_records(JCR *jcr)
                      NULL,NULL))
    {
       Jmsg1(jcr, M_FATAL, 0, "Fill File table %s\n", jcr->db_batch->errmsg);
-      return false;
+      goto bail_out;
    }
 
+   jcr->JobStatus = JobStatus;         /* reset entry status */
+   retval = true;
+
+bail_out:
    db_sql_query(jcr->db_batch, "DROP TABLE batch", NULL,NULL);
 
-   jcr->JobStatus = JobStatus;         /* reset entry status */
-   return true;
+   return retval;
 }
 
 /**
@@ -1083,38 +1090,6 @@ bool db_write_batch_file_records(JCR *jcr)
 
 #endif /* ! HAVE_BATCH_FILE_INSERT */
 
-
-/* List of SQL commands to create temp table and indicies  */
-const char *create_temp_basefile[5] = {
-   /* MySQL */
-   "CREATE TEMPORARY TABLE basefile%lld ("
-//   "CREATE TABLE basefile%lld ("
-   "Path BLOB NOT NULL,"
-   "Name BLOB NOT NULL)",
-
-   /* Postgresql */
-   "CREATE TEMPORARY TABLE basefile%lld (" 
-//   "CREATE TABLE basefile%lld (" 
-   "Path TEXT,"
-   "Name TEXT)",
-
-   /* SQLite */
-   "CREATE TEMPORARY TABLE basefile%lld (" 
-   "Path TEXT,"
-   "Name TEXT)",
-
-   /* SQLite3 */
-   "CREATE TEMPORARY TABLE basefile%lld (" 
-   "Path TEXT,"
-   "Name TEXT)",
-
-   /* Ingres */
-   "DECLARE GLOBAL TEMPORARY TABLE basefile%lld (" 
-   "Path TEXT NOT NULL,"
-   "Name TEXT NOT NULL)"
-   "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
-};
-
 /** 
  * Create file attributes record, or base file attributes record
  */
@@ -1238,17 +1213,7 @@ bool db_create_base_file_list(JCR *jcr, B_DB *mdb, char *jobids)
       goto bail_out;
    }
    Mmsg(buf, select_recent_version[db_type], jobids, jobids);
-   Mmsg(mdb->cmd,
-"CREATE TEMPORARY TABLE new_basefile%lld AS  "
-//"CREATE TABLE new_basefile%lld AS "
-  "SELECT Path.Path AS Path, Filename.Name AS Name, Temp.FileIndex AS FileIndex,"
-         "Temp.JobId AS JobId, Temp.LStat AS LStat, Temp.FileId AS FileId, "
-         "Temp.MD5 AS MD5 "
-  "FROM ( %s ) AS Temp "
-  "JOIN Filename ON (Filename.FilenameId = Temp.FilenameId) "
-  "JOIN Path ON (Path.PathId = Temp.PathId) "
- "WHERE Temp.FileIndex > 0",
-        (uint64_t)jcr->JobId, buf.c_str());
+   Mmsg(mdb->cmd, create_temp_new_basefile[db_type], (uint64_t)jcr->JobId, buf.c_str());
 
    ret = db_sql_query(mdb, mdb->cmd, NULL, NULL);
 bail_out:
@@ -1256,7 +1221,6 @@ bail_out:
    return ret;
 }
 
-
 /**
  * Create Restore Object record in B_DB
  *
@@ -1264,7 +1228,9 @@ bail_out:
 bool db_create_restore_object_record(JCR *jcr, B_DB *mdb, ROBJECT_DBR *ro)
 {
    bool stat;
-   POOLMEM *esc_obj = get_pool_memory(PM_MESSAGE); 
+   int plug_name_len;
+   POOLMEM *esc_plug_name = get_pool_memory(PM_MESSAGE);
+
    db_lock(mdb);
 
    Dmsg1(dbglevel, "Oname=%s\n", ro->object_name);
@@ -1274,15 +1240,20 @@ bool db_create_restore_object_record(JCR *jcr, B_DB *mdb, ROBJECT_DBR *ro)
    mdb->esc_name = check_pool_memory_size(mdb->esc_name, mdb->fnl*2+1);
    db_escape_string(jcr, mdb, mdb->esc_name, ro->object_name, mdb->fnl);
    
-   esc_obj = check_pool_memory_size(esc_obj, ro->object_len*2+1);
-   db_escape_string(jcr, mdb, esc_obj, ro->object, ro->object_len);
+   db_escape_object(jcr, mdb, ro->object, ro->object_len);
+
+   plug_name_len = strlen(ro->plugin_name);
+   esc_plug_name = check_pool_memory_size(esc_plug_name, plug_name_len*2+1);
+   db_escape_string(jcr, mdb, esc_plug_name, ro->plugin_name, plug_name_len);
 
    Mmsg(mdb->cmd,
-        "INSERT INTO RestoreObject (Fname,RestoreObject,"
-        "ObjectLength,ObjectIndex,ObjectType,FileIndex,JobId) VALUES"
-        "('%s','%s',%d,%d,%d,%d,%u)",
-        mdb->esc_name, esc_obj, ro->object_len,
-        ro->ObjectIndex, FT_RESTORE_FIRST, ro->FileIndex, ro->JobId);
+        "INSERT INTO RestoreObject (ObjectName,PluginName,RestoreObject,"
+        "ObjectLength,ObjectFullLength,ObjectIndex,ObjectType,"
+        "ObjectCompression,FileIndex,JobId) "
+        "VALUES ('%s','%s','%s',%d,%d,%d,%d,%d,%d,%u)",
+        mdb->esc_name, esc_plug_name, mdb->esc_obj,
+        ro->object_len, ro->object_full_len, ro->object_index, 
+        FT_RESTORE_FIRST, ro->object_compression, ro->FileIndex, ro->JobId);
 
    ro->RestoreObjectId = sql_insert_autokey_record(mdb, mdb->cmd, NT_("RestoreObject"));
    if (ro->RestoreObjectId == 0) {
@@ -1294,7 +1265,7 @@ bool db_create_restore_object_record(JCR *jcr, B_DB *mdb, ROBJECT_DBR *ro)
       stat = true;
    }
    db_unlock(mdb);
-   free_pool_memory(esc_obj);
+   free_pool_memory(esc_plug_name);
    return stat;
 }