]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_update.c
DB changes for Retention Periods
[bacula/bacula] / bacula / src / cats / sql_update.c
1 /*
2  * Bacula Catalog Database Update record interface routines
3  * 
4  *    Kern Sibbald, March 2000
5  */
6
7 /*
8    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
9
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.
14
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.
19
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,
23    MA 02111-1307, USA.
24
25  */
26
27 /* *****FIXME**** fix fixed length of select_cmd[] and insert_cmd[] */
28
29 /* The following is necessary so that we do not include
30  * the dummy external definition of DB.
31  */
32 #define __SQL_C                       /* indicate that this is sql.c */
33
34 #include "bacula.h"
35 #include "cats.h"
36
37 #if    HAVE_MYSQL || HAVE_SQLITE
38
39 /* -----------------------------------------------------------------------
40  *
41  *   Generic Routines (or almost generic)
42  *
43  * -----------------------------------------------------------------------
44  */
45
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);
49
50 static int do_update(B_DB *mdb, char *cmd)
51 {
52    int stat;
53
54    stat = UPDATE_DB(mdb, cmd);
55    return stat;
56 }
57
58 /* -----------------------------------------------------------------------
59  *
60  *   Generic Routines (or almost generic)
61  *
62  * -----------------------------------------------------------------------
63  */
64 /* Update the attributes record by adding the MD5 signature */
65 int
66 db_add_MD5_to_file_record(B_DB *mdb, FileId_t FileId, char *MD5)
67 {
68    int stat;
69
70    P(mdb->mutex);
71    Mmsg(&mdb->cmd, "UPDATE File SET MD5=\"%s\" WHERE FileId=%d", MD5, FileId);
72    stat = UPDATE_DB(mdb, mdb->cmd);
73    V(mdb->mutex);
74    return stat;
75 }
76
77 /* Mark the file record as being visited during database
78  * verify compare. Stuff JobId into FileIndex field
79  */
80 int db_mark_file_record(B_DB *mdb, FileId_t FileId, int JobId) 
81 {
82    int stat;
83
84    P(mdb->mutex);
85    Mmsg(&mdb->cmd, "UPDATE File SET FileIndex=%d WHERE FileId=%d", JobId, FileId);
86    stat = UPDATE_DB(mdb, mdb->cmd);
87    V(mdb->mutex);
88    return stat;
89 }
90
91 /*
92  * Update the Job record at end of Job
93  *
94  *  Returns: 0 on failure
95  *           1 on success
96  */
97 int
98 db_update_job_start_record(B_DB *mdb, JOB_DBR *jr)
99 {
100    char dt[MAX_TIME_LENGTH];
101    time_t stime;
102    struct tm tm;
103    btime_t JobTDate;
104    int stat;
105    char ed1[30];
106        
107    stime = jr->StartTime;
108    localtime_r(&stime, &tm);
109    strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
110    JobTDate = (btime_t)stime;
111
112    P(mdb->mutex);
113    Mmsg(&mdb->cmd, "UPDATE Job SET Level='%c', StartTime=\"%s\", \
114 ClientId=%d, JobTDate=%s WHERE JobId=%d",
115       (char)(jr->Level), dt, jr->ClientId, edit_uint64(JobTDate, ed1), jr->JobId);
116    stat = UPDATE_DB(mdb, mdb->cmd);
117    V(mdb->mutex);
118    return stat;
119 }
120
121
122
123 /*
124  * Update the Job record at end of Job
125  *
126  *  Returns: 0 on failure
127  *           1 on success
128  */
129 int
130 db_update_job_end_record(B_DB *mdb, JOB_DBR *jr)
131 {
132    char dt[MAX_TIME_LENGTH];
133    time_t ttime;
134    struct tm tm;
135    int stat;
136    char ed1[30], ed2[30];
137    btime_t JobTDate;
138        
139    ttime = jr->EndTime;
140    localtime_r(&ttime, &tm);
141    strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
142    JobTDate = ttime;
143
144    P(mdb->mutex);
145    Mmsg(&mdb->cmd,
146       "UPDATE Job SET JobStatus='%c', EndTime='%s', \
147 ClientId=%d, JobBytes=%s, JobFiles=%d, JobErrors=%d, VolSessionId=%d, \
148 VolSessionTime=%d, PoolId=%d, FileSetId=%d, JobTDate=%s WHERE JobId=%d",
149       (char)(jr->JobStatus), dt, jr->ClientId, edit_uint64(jr->JobBytes, ed1), 
150       jr->JobFiles, jr->JobErrors, jr->VolSessionId, jr->VolSessionTime, 
151       jr->PoolId, jr->FileSetId, edit_uint64(JobTDate, ed2), jr->JobId);
152
153    stat = UPDATE_DB(mdb, mdb->cmd);
154    V(mdb->mutex);
155    return stat;
156 }
157
158
159 int
160 db_update_pool_record(B_DB *mdb, POOL_DBR *pr)
161 {
162    int stat;
163
164    P(mdb->mutex);
165    Mmsg(&mdb->cmd,
166 "UPDATE Pool SET NumVols=%d, MaxVols=%d, UseOnce=%d, UseCatalog=%d, \
167 AcceptAnyVolume=%d, LabelFormat=\"%s\" WHERE PoolId=%d",
168       pr->NumVols, pr->MaxVols, pr->UseOnce, pr->UseCatalog,
169       pr->AcceptAnyVolume, pr->LabelFormat, pr->PoolId);
170
171    stat = UPDATE_DB(mdb, mdb->cmd);
172    V(mdb->mutex);
173    return stat;
174 }
175
176 /* 
177  * Update the Media Record at end of Session
178  *
179  * Returns: 0 on failure
180  *          numrows on success
181  */
182 int
183 db_update_media_record(B_DB *mdb, MEDIA_DBR *mr) 
184 {
185    char dt[MAX_TIME_LENGTH];
186    time_t ttime;
187    struct tm tm;
188    int stat;
189    char ed1[30], ed2[30];
190        
191    ttime = mr->LastWritten;
192    localtime_r(&ttime, &tm);
193    strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
194
195    Dmsg1(000, "update_media: FirstWritte=%d\n", mr->FirstWritten);
196    P(mdb->mutex);
197    if (mr->VolMounts == 1) {
198       Mmsg(&mdb->cmd, "UPDATE Media SET FirstWritten=\"%s\"\
199  WHERE VolumeName=\"%s\"", dt, mr->VolumeName);
200       if (do_update(mdb, mdb->cmd) == 0) {
201          V(mdb->mutex);
202          return 0;
203       }
204    }
205
206    Mmsg(&mdb->cmd, "UPDATE Media SET VolJobs=%d,\
207  VolFiles=%d, VolBlocks=%d, VolBytes=%s, VolMounts=%d, VolErrors=%d,\
208  VolWrites=%d, VolMaxBytes=%s, LastWritten=\"%s\", VolStatus=\"%s\" \
209  WHERE VolumeName=\"%s\"",
210    mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
211    mr->VolMounts, mr->VolErrors, mr->VolWrites, 
212    edit_uint64(mr->VolMaxBytes, ed2), dt, 
213    mr->VolStatus, mr->VolumeName);
214
215    stat = UPDATE_DB(mdb, mdb->cmd);
216    V(mdb->mutex);
217    return stat;
218 }
219
220 #endif /* HAVE_MYSQL || HAVE_SQLITE */