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
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, void *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_MD5_to_file_record(void *jcr, B_DB *mdb, FileId_t FileId, char *MD5)
63 Mmsg(&mdb->cmd, "UPDATE File SET MD5='%s' WHERE FileId=%u", MD5, FileId);
64 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
69 /* Mark the file record as being visited during database
70 * verify compare. Stuff JobId into MarkedId field
72 int db_mark_file_record(void *jcr, B_DB *mdb, FileId_t FileId, JobId_t JobId)
77 Mmsg(&mdb->cmd, "UPDATE File SET MarkId=%u WHERE FileId=%u", JobId, FileId);
78 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
84 * Update the Job record at end of Job
86 * Returns: 0 on failure
90 db_update_job_start_record(void *jcr, B_DB *mdb, JOB_DBR *jr)
92 char dt[MAX_TIME_LENGTH];
99 stime = jr->StartTime;
100 localtime_r(&stime, &tm);
101 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
102 JobTDate = (btime_t)stime;
105 Mmsg(&mdb->cmd, "UPDATE Job SET Level='%c', StartTime='%s', \
106 ClientId=%u, JobTDate=%s WHERE JobId=%u",
107 (char)(jr->Level), dt, jr->ClientId, edit_uint64(JobTDate, ed1), jr->JobId);
108 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
117 * Update the Job record at end of Job
119 * Returns: 0 on failure
123 db_update_job_end_record(void *jcr, B_DB *mdb, JOB_DBR *jr)
125 char dt[MAX_TIME_LENGTH];
129 char ed1[30], ed2[30];
133 localtime_r(&ttime, &tm);
134 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
139 "UPDATE Job SET JobStatus='%c', EndTime='%s', \
140 ClientId=%u, JobBytes=%s, JobFiles=%u, JobErrors=%u, VolSessionId=%u, \
141 VolSessionTime=%u, PoolId=%u, FileSetId=%u, JobTDate=%s WHERE JobId=%u",
142 (char)(jr->JobStatus), dt, jr->ClientId, edit_uint64(jr->JobBytes, ed1),
143 jr->JobFiles, jr->JobErrors, jr->VolSessionId, jr->VolSessionTime,
144 jr->PoolId, jr->FileSetId, edit_uint64(JobTDate, ed2), jr->JobId);
146 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
153 db_update_pool_record(void *jcr, B_DB *mdb, POOL_DBR *pr)
156 char ed1[50], ed2[50], ed3[50];
160 "UPDATE Pool SET NumVols=%d,MaxVols=%d,UseOnce=%d,UseCatalog=%d,"
161 "AcceptAnyVolume=%d,VolRetention='%s',VolUseDuration='%s',"
162 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s,Recycle=%d,"
163 "AutoPrune=%d,LabelFormat='%s' WHERE PoolId=%u",
164 pr->NumVols, pr->MaxVols, pr->UseOnce, pr->UseCatalog,
165 pr->AcceptAnyVolume, edit_uint64(pr->VolRetention, ed1),
166 edit_uint64(pr->VolUseDuration, ed2),
167 pr->MaxVolJobs, pr->MaxVolFiles,
168 edit_uint64(pr->MaxVolBytes, ed3),
169 pr->Recycle, pr->AutoPrune,
170 pr->LabelFormat, pr->PoolId);
172 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
178 * Update the Media Record at end of Session
180 * Returns: 0 on failure
184 db_update_media_record(void *jcr, B_DB *mdb, MEDIA_DBR *mr)
186 char dt[MAX_TIME_LENGTH];
190 char ed1[30], ed2[30];
193 Dmsg1(100, "update_media: FirstWritten=%d\n", mr->FirstWritten);
195 if (mr->VolJobs == 1) {
196 Dmsg1(400, "Set FirstWritten Vol=%s\n", mr->VolumeName);
197 ttime = mr->FirstWritten;
198 localtime_r(&ttime, &tm);
199 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
200 Mmsg(&mdb->cmd, "UPDATE Media SET FirstWritten='%s'\
201 WHERE VolumeName='%s'", dt, mr->VolumeName);
202 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
203 Dmsg1(400, "Firstwritten stat=%d\n", stat);
206 ttime = mr->LastWritten;
207 localtime_r(&ttime, &tm);
208 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
210 Mmsg(&mdb->cmd, "UPDATE Media SET VolJobs=%u,\
211 VolFiles=%u,VolBlocks=%u,VolBytes=%s,VolMounts=%u,VolErrors=%u,\
212 VolWrites=%u,MaxVolBytes=%s,LastWritten='%s',VolStatus='%s',\
213 Slot=%d WHERE VolumeName='%s'",
214 mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
215 mr->VolMounts, mr->VolErrors, mr->VolWrites,
216 edit_uint64(mr->MaxVolBytes, ed2), dt,
217 mr->VolStatus, mr->Slot, mr->VolumeName);
219 Dmsg1(400, "%s\n", mdb->cmd);
221 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
226 #endif /* HAVE_MYSQL || HAVE_SQLITE */