2 * Bacula Catalog Database Update record interface routines
4 * Kern Sibbald, March 2000
10 Copyright (C) 2000-2003 Kern Sibbald and John Walker
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_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(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,
64 Mmsg(&mdb->cmd, "UPDATE File SET MD5='%s' WHERE FileId=%u", SIG, FileId);
65 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
70 /* Mark the file record as being visited during database
71 * verify compare. Stuff JobId into MarkedId field
73 int db_mark_file_record(JCR *jcr, B_DB *mdb, FileId_t FileId, JobId_t JobId)
78 Mmsg(&mdb->cmd, "UPDATE File SET MarkId=%u WHERE FileId=%u", JobId, FileId);
79 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
85 * Update the Job record at end of Job
87 * Returns: 0 on failure
91 db_update_job_start_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
93 char dt[MAX_TIME_LENGTH];
100 stime = jr->StartTime;
101 localtime_r(&stime, &tm);
102 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
103 JobTDate = (btime_t)stime;
106 Mmsg(&mdb->cmd, "UPDATE Job SET Level='%c', StartTime='%s',"
107 "ClientId=%u, JobTDate=%s WHERE JobId=%u",
108 (char)(jr->Level), dt, jr->ClientId, edit_uint64(JobTDate, ed1), jr->JobId);
110 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
117 * Given an incoming integer, set the string buffer to either NULL or the value
121 void edit_num_or_null(char *s, size_t n, uint32_t id) {
122 bsnprintf(s, n, id ? "%u" : "NULL", id);
127 * Update the Job record at end of Job
129 * Returns: 0 on failure
133 db_update_job_end_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
135 char dt[MAX_TIME_LENGTH];
139 char ed1[30], ed2[30];
146 /* some values are set to zero, which translates to NULL in SQL */
147 edit_num_or_null(PoolId, sizeof(PoolId), jr->PoolId);
148 edit_num_or_null(FileSetId, sizeof(FileSetId), jr->FileSetId);
149 edit_num_or_null(ClientId, sizeof(ClientId), jr->ClientId);
152 localtime_r(&ttime, &tm);
153 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
158 "UPDATE Job SET JobStatus='%c', EndTime='%s', \
159 ClientId=%s, JobBytes=%s, JobFiles=%u, JobErrors=%u, VolSessionId=%u, \
160 VolSessionTime=%u, PoolId=%s, FileSetId=%s, JobTDate=%s WHERE JobId=%u",
161 (char)(jr->JobStatus), dt, ClientId, edit_uint64(jr->JobBytes, ed1),
162 jr->JobFiles, jr->JobErrors, jr->VolSessionId, jr->VolSessionTime,
163 PoolId, FileSetId, edit_uint64(JobTDate, ed2), jr->JobId);
165 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
172 * Update Client record
173 * Returns: 0 on failure
177 db_update_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
180 char ed1[50], ed2[50];
184 memcpy(&tcr, cr, sizeof(tcr));
185 if (!db_create_client_record(jcr, mdb, &tcr)) {
191 "UPDATE Client SET AutoPrune=%d,FileRetention=%s,JobRetention=%s,"
192 "Uname='%s' WHERE Name='%s'",
194 edit_uint64(cr->FileRetention, ed1),
195 edit_uint64(cr->JobRetention, ed2),
196 cr->Uname, cr->Name);
198 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
205 * Update Counters record
206 * Returns: 0 on failure
209 int db_update_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
214 "UPDATE Counters SET MinValue=%d,MaxValue=%d,CurrentValue=%d,"
215 "WrapCounter='%s' WHERE Counter='%s'",
216 cr->MinValue, cr->MaxValue, cr->CurrentValue,
217 cr->WrapCounter, cr->Counter);
219 int stat = UPDATE_DB(jcr, mdb, mdb->cmd);
226 db_update_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
229 char ed1[50], ed2[50], ed3[50];
233 "UPDATE Pool SET NumVols=%u,MaxVols=%u,UseOnce=%d,UseCatalog=%d,"
234 "AcceptAnyVolume=%d,VolRetention='%s',VolUseDuration='%s',"
235 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s,Recycle=%d,"
236 "AutoPrune=%d,LabelFormat='%s' WHERE PoolId=%u",
237 pr->NumVols, pr->MaxVols, pr->UseOnce, pr->UseCatalog,
238 pr->AcceptAnyVolume, edit_uint64(pr->VolRetention, ed1),
239 edit_uint64(pr->VolUseDuration, ed2),
240 pr->MaxVolJobs, pr->MaxVolFiles,
241 edit_uint64(pr->MaxVolBytes, ed3),
242 pr->Recycle, pr->AutoPrune,
243 pr->LabelFormat, pr->PoolId);
245 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
251 * Update the Media Record at end of Session
253 * Returns: 0 on failure
257 db_update_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
259 char dt[MAX_TIME_LENGTH];
263 char ed1[30], ed2[30], ed3[30], ed4[30];
266 Dmsg1(100, "update_media: FirstWritten=%d\n", mr->FirstWritten);
268 if (mr->VolJobs == 1) {
269 Dmsg1(400, "Set FirstWritten Vol=%s\n", mr->VolumeName);
270 ttime = mr->FirstWritten;
271 localtime_r(&ttime, &tm);
272 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
273 Mmsg(&mdb->cmd, "UPDATE Media SET FirstWritten='%s'\
274 WHERE VolumeName='%s'", dt, mr->VolumeName);
275 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
276 Dmsg1(400, "Firstwritten stat=%d\n", stat);
279 /* Label just done? */
280 if (mr->VolBytes == 1) {
281 ttime = mr->LabelDate;
285 localtime_r(&ttime, &tm);
286 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
287 Mmsg(&mdb->cmd, "UPDATE Media SET LabelDate='%s' "
288 "WHERE VolumeName='%s'", dt, mr->VolumeName);
289 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
292 /* Make sure InChanger is 0 for any record having the same Slot */
293 db_make_inchanger_unique(jcr, mdb, mr);
295 ttime = mr->LastWritten;
296 localtime_r(&ttime, &tm);
297 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
299 Mmsg(&mdb->cmd, "UPDATE Media SET VolJobs=%u,"
300 "VolFiles=%u,VolBlocks=%u,VolBytes=%s,VolMounts=%u,VolErrors=%u,"
301 "VolWrites=%u,MaxVolBytes=%s,LastWritten='%s',VolStatus='%s',"
302 "Slot=%d,InChanger=%d,VolReadTime=%s,VolWriteTime=%s "
303 " WHERE VolumeName='%s'",
304 mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
305 mr->VolMounts, mr->VolErrors, mr->VolWrites,
306 edit_uint64(mr->MaxVolBytes, ed2), dt,
307 mr->VolStatus, mr->Slot, mr->InChanger,
308 edit_uint64(mr->VolReadTime, ed3),
309 edit_uint64(mr->VolWriteTime, ed4),
313 Dmsg1(400, "%s\n", mdb->cmd);
315 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
321 * If we have a non-zero InChanger, ensure that no other Media
322 * record in this Pool has InChanger set on the same Slot.
324 * This routine assumes the database is already locked.
327 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
329 if (mr->InChanger != 0 && mr->Slot != 0) {
330 Mmsg(&mdb->cmd, "UPDATE Media SET InChanger=0 WHERE PoolId=%u "
331 "AND Slot=%d\n", mr->PoolId, mr->Slot);
332 Dmsg1(400, "%s\n", mdb->cmd);
333 UPDATE_DB(jcr, mdb, mdb->cmd);
337 #endif /* HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL*/