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);
119 * Update the Job record at end of Job
121 * Returns: 0 on failure
125 db_update_job_end_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
127 char dt[MAX_TIME_LENGTH];
131 char ed1[30], ed2[30];
135 localtime_r(&ttime, &tm);
136 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
141 "UPDATE Job SET JobStatus='%c', EndTime='%s', \
142 ClientId=%u, JobBytes=%s, JobFiles=%u, JobErrors=%u, VolSessionId=%u, \
143 VolSessionTime=%u, PoolId=%u, FileSetId=%u, JobTDate=%s WHERE JobId=%u",
144 (char)(jr->JobStatus), dt, jr->ClientId, edit_uint64(jr->JobBytes, ed1),
145 jr->JobFiles, jr->JobErrors, jr->VolSessionId, jr->VolSessionTime,
146 jr->PoolId, jr->FileSetId, edit_uint64(JobTDate, ed2), jr->JobId);
148 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
155 * Update Client record
156 * Returns: 0 on failure
160 db_update_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
163 char ed1[50], ed2[50];
167 memcpy(&tcr, cr, sizeof(tcr));
168 if (!db_create_client_record(jcr, mdb, &tcr)) {
174 "UPDATE Client SET AutoPrune=%d,FileRetention=%s,JobRetention=%s,"
175 "Uname='%s' WHERE Name='%s'",
177 edit_uint64(cr->FileRetention, ed1),
178 edit_uint64(cr->JobRetention, ed2),
179 cr->Uname, cr->Name);
181 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
188 * Update Counters record
189 * Returns: 0 on failure
192 int db_update_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
197 "UPDATE Counters SET MinValue=%d,MaxValue=%d,CurrentValue=%d,"
198 "WrapCounter='%s' WHERE Counter='%s'",
199 cr->MinValue, cr->MaxValue, cr->CurrentValue,
200 cr->WrapCounter, cr->Counter);
202 int stat = UPDATE_DB(jcr, mdb, mdb->cmd);
209 db_update_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
212 char ed1[50], ed2[50], ed3[50];
216 "UPDATE Pool SET NumVols=%u,MaxVols=%u,UseOnce=%d,UseCatalog=%d,"
217 "AcceptAnyVolume=%d,VolRetention='%s',VolUseDuration='%s',"
218 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s,Recycle=%d,"
219 "AutoPrune=%d,LabelFormat='%s' WHERE PoolId=%u",
220 pr->NumVols, pr->MaxVols, pr->UseOnce, pr->UseCatalog,
221 pr->AcceptAnyVolume, edit_uint64(pr->VolRetention, ed1),
222 edit_uint64(pr->VolUseDuration, ed2),
223 pr->MaxVolJobs, pr->MaxVolFiles,
224 edit_uint64(pr->MaxVolBytes, ed3),
225 pr->Recycle, pr->AutoPrune,
226 pr->LabelFormat, pr->PoolId);
228 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
234 * Update the Media Record at end of Session
236 * Returns: 0 on failure
240 db_update_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
242 char dt[MAX_TIME_LENGTH];
246 char ed1[30], ed2[30];
249 Dmsg1(100, "update_media: FirstWritten=%d\n", mr->FirstWritten);
251 if (mr->VolJobs == 1) {
252 Dmsg1(400, "Set FirstWritten Vol=%s\n", mr->VolumeName);
253 ttime = mr->FirstWritten;
254 localtime_r(&ttime, &tm);
255 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
256 Mmsg(&mdb->cmd, "UPDATE Media SET FirstWritten='%s'\
257 WHERE VolumeName='%s'", dt, mr->VolumeName);
258 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
259 Dmsg1(400, "Firstwritten stat=%d\n", stat);
262 /* Label just done? */
263 if (mr->VolBytes == 1) {
264 ttime = mr->LabelDate;
268 localtime_r(&ttime, &tm);
269 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
270 Mmsg(&mdb->cmd, "UPDATE Media SET LabelDate='%s' "
271 "WHERE VolumeName='%s'", dt, mr->VolumeName);
272 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
275 /* Make sure InChanger is 0 for any record having the same Slot */
276 db_make_inchanger_unique(jcr, mdb, mr);
278 ttime = mr->LastWritten;
279 localtime_r(&ttime, &tm);
280 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
282 Mmsg(&mdb->cmd, "UPDATE Media SET VolJobs=%u,"
283 "VolFiles=%u,VolBlocks=%u,VolBytes=%s,VolMounts=%u,VolErrors=%u,"
284 "VolWrites=%u,MaxVolBytes=%s,LastWritten='%s',VolStatus='%s',"
285 "Slot=%d,Drive=%d,InChanger=%d WHERE VolumeName='%s'",
286 mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
287 mr->VolMounts, mr->VolErrors, mr->VolWrites,
288 edit_uint64(mr->MaxVolBytes, ed2), dt,
289 mr->VolStatus, mr->Slot, mr->Drive, mr->InChanger, mr->VolumeName);
291 Dmsg1(400, "%s\n", mdb->cmd);
293 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
299 * If we have a non-zero InChanger, ensure that no other Media
300 * record in this Pool has InChanger set on the same Slot.
302 * This routine assumes the database is already locked.
305 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
307 if (mr->InChanger != 0 && mr->Slot != 0) {
308 Mmsg(&mdb->cmd, "UPDATE Media SET InChanger=0 WHERE PoolId=%u "
309 "AND Slot=%d\n", mr->PoolId, mr->Slot);
310 Dmsg1(400, "%s\n", mdb->cmd);
311 UPDATE_DB(jcr, mdb, mdb->cmd);
315 #endif /* HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL*/