2 * Bacula Catalog Database Update record interface routines
4 * Kern Sibbald, March 2000
8 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public
21 License along with this program; if not, write to the Free
22 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 /* *****FIXME**** fix fixed length of select_cmd[] and insert_cmd[] */
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, B_DB *db, char *update_cmd);
50 static int do_update(B_DB *mdb, char *cmd)
54 stat = UPDATE_DB(mdb, cmd);
58 /* -----------------------------------------------------------------------
60 * Generic Routines (or almost generic)
62 * -----------------------------------------------------------------------
64 /* Update the attributes record by adding the MD5 signature */
66 db_add_MD5_to_file_record(B_DB *mdb, FileId_t FileId, char *MD5)
71 Mmsg(&mdb->cmd, "UPDATE File SET MD5=\"%s\" WHERE FileId=%d", MD5, FileId);
72 stat = UPDATE_DB(mdb, mdb->cmd);
77 /* Mark the file record as being visited during database
78 * verify compare. Stuff JobId into FileIndex field
80 int db_mark_file_record(B_DB *mdb, FileId_t FileId, int JobId)
85 Mmsg(&mdb->cmd, "UPDATE File SET FileIndex=%d WHERE FileId=%d", JobId, FileId);
86 stat = UPDATE_DB(mdb, mdb->cmd);
92 * Update the Job record at end of Job
94 * Returns: 0 on failure
98 db_update_job_start_record(B_DB *mdb, JOB_DBR *jr)
100 char dt[MAX_TIME_LENGTH];
106 stime = jr->StartTime;
107 localtime_r(&stime, &tm);
108 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
109 StartDay = (int32_t)(date_encode(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday) -
110 date_encode(2000, 1, 1));
113 Mmsg(&mdb->cmd, "UPDATE Job SET Level='%c', StartTime=\"%s\", \
114 ClientId=%d, StartDay=%d WHERE JobId=%d",
115 (char)(jr->Level), dt, jr->ClientId, StartDay, jr->JobId);
116 stat = UPDATE_DB(mdb, mdb->cmd);
124 * Update the Job record at end of Job
126 * Returns: 0 on failure
130 db_update_job_end_record(B_DB *mdb, JOB_DBR *jr)
132 char dt[MAX_TIME_LENGTH];
138 localtime_r(&ttime, &tm);
139 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
143 "UPDATE Job SET JobStatus='%c', EndTime='%s', \
144 ClientId=%d, JobBytes=%" lld ", JobFiles=%d, JobErrors=%d, VolSessionId=%d, \
145 VolSessionTime=%d, PoolId=%d, FileSetId=%d WHERE JobId=%d",
146 (char)(jr->JobStatus), dt, jr->ClientId, jr->JobBytes, jr->JobFiles,
147 jr->JobErrors, jr->VolSessionId, jr->VolSessionTime,
148 jr->PoolId, jr->FileSetId, jr->JobId);
150 stat = UPDATE_DB(mdb, mdb->cmd);
157 db_update_pool_record(B_DB *mdb, POOL_DBR *pr)
163 "UPDATE Pool SET NumVols=%d, MaxVols=%d, UseOnce=%d, UseCatalog=%d, \
164 AcceptAnyVolume=%d, LabelFormat=\"%s\" WHERE PoolId=%d",
165 pr->NumVols, pr->MaxVols, pr->UseOnce, pr->UseCatalog,
166 pr->AcceptAnyVolume, pr->LabelFormat, pr->PoolId);
168 stat = UPDATE_DB(mdb, mdb->cmd);
174 * Update the Media Record at end of Session
176 * Returns: 0 on failure
180 db_update_media_record(B_DB *mdb, MEDIA_DBR *mr)
182 char dt[MAX_TIME_LENGTH];
187 ttime = mr->LastWritten;
188 localtime_r(&ttime, &tm);
189 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
192 if (mr->VolMounts == 1) {
193 Mmsg(&mdb->cmd, "UPDATE Media SET FirstWritten=\"%s\" WHERE \
194 VolumeName=\"%s\"", dt, mr->VolumeName);
195 if (do_update(mdb, mdb->cmd) == 0) {
201 Mmsg(&mdb->cmd, "UPDATE Media SET VolJobs=%d,\
202 VolFiles=%d, VolBlocks=%d, VolBytes=%" lld ", VolMounts=%d, VolErrors=%d,\
203 VolWrites=%d, VolMaxBytes=%" lld ", LastWritten=\"%s\", VolStatus=\"%s\" \
204 WHERE VolumeName=\"%s\"",
205 mr->VolJobs, mr->VolFiles, mr->VolBlocks, mr->VolBytes, mr->VolMounts,
206 mr->VolErrors, mr->VolWrites, mr->VolMaxBytes, dt, mr->VolStatus,
209 stat = UPDATE_DB(mdb, mdb->cmd);
214 #endif /* HAVE_MYSQL || HAVE_SQLITE */