2 * Bacula Catalog Database Update record interface routines
4 * Kern Sibbald, March 2000
10 Copyright (C) 2000-2005 Kern Sibbald
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29 /* The following is necessary so that we do not include
30 * the dummy external definition of DB.
32 #define __SQL_C /* indicate that this is sql.c */
37 #if HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL
39 /* -----------------------------------------------------------------------
41 * Generic Routines (or almost generic)
43 * -----------------------------------------------------------------------
46 /* Imported subroutines */
47 extern void print_result(B_DB *mdb);
48 extern int UpdateDB(const char *file, int line, JCR *jcr, B_DB *db, char *update_cmd);
50 /* -----------------------------------------------------------------------
52 * Generic Routines (or almost generic)
54 * -----------------------------------------------------------------------
56 /* Update the attributes record by adding the MD5 signature */
58 db_add_SIG_to_file_record(JCR *jcr, B_DB *mdb, FileId_t FileId, char *SIG,
65 Mmsg(mdb->cmd, "UPDATE File SET MD5='%s' WHERE FileId=%s", SIG,
66 edit_int64(FileId, ed1));
67 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
72 /* Mark the file record as being visited during database
73 * verify compare. Stuff JobId into MarkedId field
75 int db_mark_file_record(JCR *jcr, B_DB *mdb, FileId_t FileId, JobId_t JobId)
78 char ed1[50], ed2[50];
81 Mmsg(mdb->cmd, "UPDATE File SET MarkId=%s WHERE FileId=%s",
82 edit_int64(JobId, ed1), edit_int64(FileId, ed2));
83 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
89 * Update the Job record at start of Job
91 * Returns: false on failure
95 db_update_job_start_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
97 char dt[MAX_TIME_LENGTH];
102 char ed1[50], ed2[50], ed3[50];
104 stime = jr->StartTime;
105 localtime_r(&stime, &tm);
106 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
107 JobTDate = (btime_t)stime;
110 Mmsg(mdb->cmd, "UPDATE Job SET JobStatus='%c',Level='%c',StartTime='%s',"
111 "ClientId=%s,JobTDate=%s WHERE JobId=%s",
112 (char)(jcr->JobStatus),
113 (char)(jr->JobLevel), dt,
114 edit_int64(jr->ClientId, ed1),
115 edit_uint64(JobTDate, ed2),
116 edit_int64(jr->JobId, ed3));
118 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
125 * Given an incoming integer, set the string buffer to either NULL or the value
129 static void edit_num_or_null(char *s, size_t n, uint64_t id) {
131 bsnprintf(s, n, id ? "%s" : "NULL", edit_int64(id, ed1));
136 * Update the Job record at end of Job
138 * Returns: 0 on failure
142 db_update_job_end_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
144 char dt[MAX_TIME_LENGTH];
148 char ed1[30], ed2[30], ed3[50];
155 /* some values are set to zero, which translates to NULL in SQL */
156 edit_num_or_null(PoolId, sizeof(PoolId), jr->PoolId);
157 edit_num_or_null(FileSetId, sizeof(FileSetId), jr->FileSetId);
158 edit_num_or_null(ClientId, sizeof(ClientId), jr->ClientId);
161 localtime_r(&ttime, &tm);
162 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
167 "UPDATE Job SET JobStatus='%c', EndTime='%s', "
168 "ClientId=%s, JobBytes=%s, JobFiles=%u, JobErrors=%u, VolSessionId=%u, "
169 "VolSessionTime=%u, PoolId=%s, FileSetId=%s, JobTDate=%s WHERE JobId=%s",
170 (char)(jr->JobStatus), dt, ClientId, edit_uint64(jr->JobBytes, ed1),
171 jr->JobFiles, jr->JobErrors, jr->VolSessionId, jr->VolSessionTime,
172 PoolId, FileSetId, edit_uint64(JobTDate, ed2),
173 edit_int64(jr->JobId, ed3));
175 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
182 * Update Client record
183 * Returns: 0 on failure
187 db_update_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
190 char ed1[50], ed2[50];
194 memcpy(&tcr, cr, sizeof(tcr));
195 if (!db_create_client_record(jcr, mdb, &tcr)) {
201 "UPDATE Client SET AutoPrune=%d,FileRetention=%s,JobRetention=%s,"
202 "Uname='%s' WHERE Name='%s'",
204 edit_uint64(cr->FileRetention, ed1),
205 edit_uint64(cr->JobRetention, ed2),
206 cr->Uname, cr->Name);
208 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
215 * Update Counters record
216 * Returns: 0 on failure
219 int db_update_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
224 "UPDATE Counters SET MinValue=%d,MaxValue=%d,CurrentValue=%d,"
225 "WrapCounter='%s' WHERE Counter='%s'",
226 cr->MinValue, cr->MaxValue, cr->CurrentValue,
227 cr->WrapCounter, cr->Counter);
229 int stat = UPDATE_DB(jcr, mdb, mdb->cmd);
236 db_update_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
239 char ed1[50], ed2[50], ed3[50], ed4[50];
243 "UPDATE Pool SET NumVols=%u,MaxVols=%u,UseOnce=%d,UseCatalog=%d,"
244 "AcceptAnyVolume=%d,VolRetention='%s',VolUseDuration='%s',"
245 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s,Recycle=%d,"
246 "AutoPrune=%d,LabelType=%d,LabelFormat='%s' WHERE PoolId=%s",
247 pr->NumVols, pr->MaxVols, pr->UseOnce, pr->UseCatalog,
248 pr->AcceptAnyVolume, edit_uint64(pr->VolRetention, ed1),
249 edit_uint64(pr->VolUseDuration, ed2),
250 pr->MaxVolJobs, pr->MaxVolFiles,
251 edit_uint64(pr->MaxVolBytes, ed3),
252 pr->Recycle, pr->AutoPrune, pr->LabelType,
254 edit_int64(pr->PoolId, ed4));
256 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
262 db_update_storage_record(JCR *jcr, B_DB *mdb, STORAGE_DBR *sr)
268 Mmsg(mdb->cmd, "UPDATE Storage SET AutoChanger=%d WHERE StorageId=%s",
269 sr->AutoChanger, edit_int64(sr->StorageId, ed1));
271 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
278 * Update the Media Record at end of Session
280 * Returns: 0 on failure
284 db_update_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
286 char dt[MAX_TIME_LENGTH];
290 char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50];
293 Dmsg1(100, "update_media: FirstWritten=%d\n", mr->FirstWritten);
295 if (mr->set_first_written) {
296 Dmsg1(400, "Set FirstWritten Vol=%s\n", mr->VolumeName);
297 ttime = mr->FirstWritten;
298 localtime_r(&ttime, &tm);
299 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
300 Mmsg(mdb->cmd, "UPDATE Media SET FirstWritten='%s'"
301 " WHERE VolumeName='%s'", dt, mr->VolumeName);
302 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
303 Dmsg1(400, "Firstwritten=%d\n", mr->FirstWritten);
306 /* Label just done? */
307 if (mr->set_label_date) {
308 ttime = mr->LabelDate;
312 localtime_r(&ttime, &tm);
313 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
314 Mmsg(mdb->cmd, "UPDATE Media SET LabelDate='%s' "
315 "WHERE VolumeName='%s'", dt, mr->VolumeName);
316 UPDATE_DB(jcr, mdb, mdb->cmd);
319 if (mr->LastWritten != 0) {
320 ttime = mr->LastWritten;
321 localtime_r(&ttime, &tm);
322 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
323 Mmsg(mdb->cmd, "UPDATE Media Set LastWritten='%s' "
324 "WHERE VolumeName='%s'", dt, mr->VolumeName);
325 UPDATE_DB(jcr, mdb, mdb->cmd);
328 Mmsg(mdb->cmd, "UPDATE Media SET VolJobs=%u,"
329 "VolFiles=%u,VolBlocks=%u,VolBytes=%s,VolMounts=%u,VolErrors=%u,"
330 "VolWrites=%u,MaxVolBytes=%s,VolStatus='%s',"
331 "Slot=%d,InChanger=%d,VolReadTime=%s,VolWriteTime=%s,VolParts=%d,"
332 "LabelType=%d,StorageId=%s"
333 " WHERE VolumeName='%s'",
334 mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
335 mr->VolMounts, mr->VolErrors, mr->VolWrites,
336 edit_uint64(mr->MaxVolBytes, ed2),
337 mr->VolStatus, mr->Slot, mr->InChanger,
338 edit_uint64(mr->VolReadTime, ed3),
339 edit_uint64(mr->VolWriteTime, ed4),
342 edit_int64(mr->StorageId, ed5),
345 Dmsg1(400, "%s\n", mdb->cmd);
347 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
349 /* Make sure InChanger is 0 for any record having the same Slot */
350 db_make_inchanger_unique(jcr, mdb, mr);
357 * Update the Media Record Default values from Pool
359 * Returns: 0 on failure
363 db_update_media_defaults(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
366 char ed1[50], ed2[50], ed3[50], ed4[50];
370 if (mr->VolumeName[0]) {
371 Mmsg(mdb->cmd, "UPDATE Media SET "
372 "Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
373 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s"
374 " WHERE VolumeName='%s'",
375 mr->Recycle,edit_uint64(mr->VolRetention, ed1),
376 edit_uint64(mr->VolUseDuration, ed2),
377 mr->MaxVolJobs, mr->MaxVolFiles,
378 edit_uint64(mr->VolBytes, ed3),
381 Mmsg(mdb->cmd, "UPDATE Media SET "
382 "Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
383 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s"
385 mr->Recycle,edit_uint64(mr->VolRetention, ed1),
386 edit_uint64(mr->VolUseDuration, ed2),
387 mr->MaxVolJobs, mr->MaxVolFiles,
388 edit_uint64(mr->VolBytes, ed3),
389 edit_int64(mr->PoolId, ed4));
392 Dmsg1(400, "%s\n", mdb->cmd);
394 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
402 * If we have a non-zero InChanger, ensure that no other Media
403 * record has InChanger set on the same Slot.
405 * This routine assumes the database is already locked.
408 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
410 char ed1[50], ed2[50];
411 if (mr->InChanger != 0 && mr->Slot != 0) {
412 Mmsg(mdb->cmd, "UPDATE Media SET InChanger=0 WHERE "
413 "Slot=%d AND StorageId=%s AND MediaId!=%s",
415 edit_int64(mr->StorageId, ed1), edit_int64(mr->MediaId, ed2));
416 Dmsg1(400, "%s\n", mdb->cmd);
417 UPDATE_DB(jcr, mdb, mdb->cmd);
424 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
426 /* DUMMY func for Bacula_DB */
430 #endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL*/