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
128 static void edit_num_or_null(char *s, size_t n, uint64_t id) {
130 bsnprintf(s, n, id ? "%s" : "NULL", edit_int64(id, ed1));
135 * Update the Job record at end of Job
137 * Returns: 0 on failure
141 db_update_job_end_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
143 char dt[MAX_TIME_LENGTH];
147 char ed1[30], ed2[30], ed3[50];
154 /* some values are set to zero, which translates to NULL in SQL */
155 edit_num_or_null(PoolId, sizeof(PoolId), jr->PoolId);
156 edit_num_or_null(FileSetId, sizeof(FileSetId), jr->FileSetId);
157 edit_num_or_null(ClientId, sizeof(ClientId), jr->ClientId);
160 localtime_r(&ttime, &tm);
161 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
166 "UPDATE Job SET JobStatus='%c', EndTime='%s', "
167 "ClientId=%s, JobBytes=%s, JobFiles=%u, JobErrors=%u, VolSessionId=%u, "
168 "VolSessionTime=%u, PoolId=%s, FileSetId=%s, JobTDate=%s WHERE JobId=%s",
169 (char)(jr->JobStatus), dt, ClientId, edit_uint64(jr->JobBytes, ed1),
170 jr->JobFiles, jr->JobErrors, jr->VolSessionId, jr->VolSessionTime,
171 PoolId, FileSetId, edit_uint64(JobTDate, ed2),
172 edit_int64(jr->JobId, ed3));
174 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
181 * Update Client record
182 * Returns: 0 on failure
186 db_update_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
189 char ed1[50], ed2[50];
193 memcpy(&tcr, cr, sizeof(tcr));
194 if (!db_create_client_record(jcr, mdb, &tcr)) {
200 "UPDATE Client SET AutoPrune=%d,FileRetention=%s,JobRetention=%s,"
201 "Uname='%s' WHERE Name='%s'",
203 edit_uint64(cr->FileRetention, ed1),
204 edit_uint64(cr->JobRetention, ed2),
205 cr->Uname, cr->Name);
207 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
214 * Update Counters record
215 * Returns: 0 on failure
218 int db_update_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
223 "UPDATE Counters SET MinValue=%d,MaxValue=%d,CurrentValue=%d,"
224 "WrapCounter='%s' WHERE Counter='%s'",
225 cr->MinValue, cr->MaxValue, cr->CurrentValue,
226 cr->WrapCounter, cr->Counter);
228 int stat = UPDATE_DB(jcr, mdb, mdb->cmd);
235 db_update_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
238 char ed1[50], ed2[50], ed3[50], ed4[50];
241 Mmsg(mdb->cmd, "SELECT count(*) from Pool");
242 pr->NumVols = get_sql_record_max(jcr, mdb);
245 "UPDATE Pool SET NumVols=%u,MaxVols=%u,UseOnce=%d,UseCatalog=%d,"
246 "AcceptAnyVolume=%d,VolRetention='%s',VolUseDuration='%s',"
247 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s,Recycle=%d,"
248 "AutoPrune=%d,LabelType=%d,LabelFormat='%s' WHERE PoolId=%s",
249 pr->NumVols, pr->MaxVols, pr->UseOnce, pr->UseCatalog,
250 pr->AcceptAnyVolume, edit_uint64(pr->VolRetention, ed1),
251 edit_uint64(pr->VolUseDuration, ed2),
252 pr->MaxVolJobs, pr->MaxVolFiles,
253 edit_uint64(pr->MaxVolBytes, ed3),
254 pr->Recycle, pr->AutoPrune, pr->LabelType,
256 edit_int64(pr->PoolId, ed4));
258 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
264 db_update_storage_record(JCR *jcr, B_DB *mdb, STORAGE_DBR *sr)
270 Mmsg(mdb->cmd, "UPDATE Storage SET AutoChanger=%d WHERE StorageId=%s",
271 sr->AutoChanger, edit_int64(sr->StorageId, ed1));
273 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
280 * Update the Media Record at end of Session
282 * Returns: 0 on failure
286 db_update_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
288 char dt[MAX_TIME_LENGTH];
292 char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50];
295 Dmsg1(100, "update_media: FirstWritten=%d\n", mr->FirstWritten);
297 if (mr->set_first_written) {
298 Dmsg1(400, "Set FirstWritten Vol=%s\n", mr->VolumeName);
299 ttime = mr->FirstWritten;
300 localtime_r(&ttime, &tm);
301 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
302 Mmsg(mdb->cmd, "UPDATE Media SET FirstWritten='%s'"
303 " WHERE VolumeName='%s'", dt, mr->VolumeName);
304 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
305 Dmsg1(400, "Firstwritten=%d\n", mr->FirstWritten);
308 /* Label just done? */
309 if (mr->set_label_date) {
310 ttime = mr->LabelDate;
314 localtime_r(&ttime, &tm);
315 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
316 Mmsg(mdb->cmd, "UPDATE Media SET LabelDate='%s' "
317 "WHERE VolumeName='%s'", dt, mr->VolumeName);
318 UPDATE_DB(jcr, mdb, mdb->cmd);
321 if (mr->LastWritten != 0) {
322 ttime = mr->LastWritten;
323 localtime_r(&ttime, &tm);
324 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
325 Mmsg(mdb->cmd, "UPDATE Media Set LastWritten='%s' "
326 "WHERE VolumeName='%s'", dt, mr->VolumeName);
327 UPDATE_DB(jcr, mdb, mdb->cmd);
330 Mmsg(mdb->cmd, "UPDATE Media SET VolJobs=%u,"
331 "VolFiles=%u,VolBlocks=%u,VolBytes=%s,VolMounts=%u,VolErrors=%u,"
332 "VolWrites=%u,MaxVolBytes=%s,VolStatus='%s',"
333 "Slot=%d,InChanger=%d,VolReadTime=%s,VolWriteTime=%s,VolParts=%d,"
334 "LabelType=%d,StorageId=%s"
335 " WHERE VolumeName='%s'",
336 mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
337 mr->VolMounts, mr->VolErrors, mr->VolWrites,
338 edit_uint64(mr->MaxVolBytes, ed2),
339 mr->VolStatus, mr->Slot, mr->InChanger,
340 edit_uint64(mr->VolReadTime, ed3),
341 edit_uint64(mr->VolWriteTime, ed4),
344 edit_int64(mr->StorageId, ed5),
347 Dmsg1(400, "%s\n", mdb->cmd);
349 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
351 /* Make sure InChanger is 0 for any record having the same Slot */
352 db_make_inchanger_unique(jcr, mdb, mr);
359 * Update the Media Record Default values from Pool
361 * Returns: 0 on failure
365 db_update_media_defaults(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
368 char ed1[50], ed2[50], ed3[50], ed4[50];
372 if (mr->VolumeName[0]) {
373 Mmsg(mdb->cmd, "UPDATE Media SET "
374 "Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
375 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s"
376 " WHERE VolumeName='%s'",
377 mr->Recycle,edit_uint64(mr->VolRetention, ed1),
378 edit_uint64(mr->VolUseDuration, ed2),
379 mr->MaxVolJobs, mr->MaxVolFiles,
380 edit_uint64(mr->VolBytes, ed3),
383 Mmsg(mdb->cmd, "UPDATE Media SET "
384 "Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
385 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s"
387 mr->Recycle,edit_uint64(mr->VolRetention, ed1),
388 edit_uint64(mr->VolUseDuration, ed2),
389 mr->MaxVolJobs, mr->MaxVolFiles,
390 edit_uint64(mr->VolBytes, ed3),
391 edit_int64(mr->PoolId, ed4));
394 Dmsg1(400, "%s\n", mdb->cmd);
396 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
404 * If we have a non-zero InChanger, ensure that no other Media
405 * record has InChanger set on the same Slot.
407 * This routine assumes the database is already locked.
410 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
412 char ed1[50], ed2[50];
413 if (mr->InChanger != 0 && mr->Slot != 0) {
414 Mmsg(mdb->cmd, "UPDATE Media SET InChanger=0 WHERE "
415 "Slot=%d AND StorageId=%s AND MediaId!=%s",
417 edit_int64(mr->StorageId, ed1), edit_int64(mr->MediaId, ed2));
418 Dmsg1(400, "%s\n", mdb->cmd);
419 UPDATE_DB(jcr, mdb, mdb->cmd);
426 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
428 /* DUMMY func for Bacula_DB */
432 #endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL*/