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