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 * Update the Media Record at end of Session
264 * Returns: 0 on failure
268 db_update_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
270 char dt[MAX_TIME_LENGTH];
274 char ed1[30], ed2[30], ed3[30], ed4[30];
277 Dmsg1(100, "update_media: FirstWritten=%d\n", mr->FirstWritten);
279 if (mr->set_first_written) {
280 Dmsg1(400, "Set FirstWritten Vol=%s\n", mr->VolumeName);
281 ttime = mr->FirstWritten;
282 localtime_r(&ttime, &tm);
283 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
284 Mmsg(mdb->cmd, "UPDATE Media SET FirstWritten='%s'"
285 " WHERE VolumeName='%s'", dt, mr->VolumeName);
286 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
287 Dmsg1(400, "Firstwritten=%d\n", mr->FirstWritten);
290 /* Label just done? */
291 if (mr->set_label_date) {
292 ttime = mr->LabelDate;
296 localtime_r(&ttime, &tm);
297 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
298 Mmsg(mdb->cmd, "UPDATE Media SET LabelDate='%s' "
299 "WHERE VolumeName='%s'", dt, mr->VolumeName);
300 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
303 if (mr->LastWritten != 0) {
304 ttime = mr->LastWritten;
305 localtime_r(&ttime, &tm);
306 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
308 Mmsg(mdb->cmd, "UPDATE Media SET VolJobs=%u,"
309 "VolFiles=%u,VolBlocks=%u,VolBytes=%s,VolMounts=%u,VolErrors=%u,"
310 "VolWrites=%u,MaxVolBytes=%s,LastWritten='%s',VolStatus='%s',"
311 "Slot=%d,InChanger=%d,VolReadTime=%s,VolWriteTime=%s,VolParts=%d,"
313 " WHERE VolumeName='%s'",
314 mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
315 mr->VolMounts, mr->VolErrors, mr->VolWrites,
316 edit_uint64(mr->MaxVolBytes, ed2), dt,
317 mr->VolStatus, mr->Slot, mr->InChanger,
318 edit_uint64(mr->VolReadTime, ed3),
319 edit_uint64(mr->VolWriteTime, ed4),
324 Mmsg(mdb->cmd, "UPDATE Media SET VolJobs=%u,"
325 "VolFiles=%u,VolBlocks=%u,VolBytes=%s,VolMounts=%u,VolErrors=%u,"
326 "VolWrites=%u,MaxVolBytes=%s,VolStatus='%s',"
327 "Slot=%d,InChanger=%d,VolReadTime=%s,VolWriteTime=%s,VolParts=%d,"
329 " WHERE VolumeName='%s'",
330 mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
331 mr->VolMounts, mr->VolErrors, mr->VolWrites,
332 edit_uint64(mr->MaxVolBytes, ed2),
333 mr->VolStatus, mr->Slot, mr->InChanger,
334 edit_uint64(mr->VolReadTime, ed3),
335 edit_uint64(mr->VolWriteTime, ed4),
341 Dmsg1(400, "%s\n", mdb->cmd);
343 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
345 /* Make sure InChanger is 0 for any record having the same Slot */
346 db_make_inchanger_unique(jcr, mdb, mr);
353 * Update the Media Record Default values from Pool
355 * Returns: 0 on failure
359 db_update_media_defaults(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
362 char ed1[50], ed2[50], ed3[50], ed4[50];
366 if (mr->VolumeName[0]) {
367 Mmsg(mdb->cmd, "UPDATE Media SET "
368 "Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
369 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s"
370 " WHERE VolumeName='%s'",
371 mr->Recycle,edit_uint64(mr->VolRetention, ed1),
372 edit_uint64(mr->VolUseDuration, ed2),
373 mr->MaxVolJobs, mr->MaxVolFiles,
374 edit_uint64(mr->VolBytes, ed3),
377 Mmsg(mdb->cmd, "UPDATE Media SET "
378 "Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
379 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s"
381 mr->Recycle,edit_uint64(mr->VolRetention, ed1),
382 edit_uint64(mr->VolUseDuration, ed2),
383 mr->MaxVolJobs, mr->MaxVolFiles,
384 edit_uint64(mr->VolBytes, ed3),
385 edit_int64(mr->PoolId, ed4));
388 Dmsg1(400, "%s\n", mdb->cmd);
390 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
398 * If we have a non-zero InChanger, ensure that no other Media
399 * record has InChanger set on the same Slot.
401 * This routine assumes the database is already locked.
404 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
406 char ed1[50], ed2[50];
407 if (mr->InChanger != 0 && mr->Slot != 0) {
408 Mmsg(mdb->cmd, "UPDATE Media SET InChanger=0 WHERE "
409 "Slot=%d AND PoolId=%s AND MediaId!=%s",
411 edit_int64(mr->PoolId, ed1), edit_int64(mr->MediaId, ed2));
412 Dmsg1(400, "%s\n", mdb->cmd);
413 UPDATE_DB(jcr, mdb, mdb->cmd);
420 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
422 /* DUMMY func for Bacula_DB */
426 #endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL*/