]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_update.c
Minor updates
[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 /* 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 /* -----------------------------------------------------------------------
51  *
52  *   Generic Routines (or almost generic)
53  *
54  * -----------------------------------------------------------------------
55  */
56 /* Update the attributes record by adding the MD5 signature */
57 int
58 db_add_MD5_to_file_record(B_DB *mdb, FileId_t FileId, char *MD5)
59 {
60    int stat;
61
62    db_lock(mdb);
63    Mmsg(&mdb->cmd, "UPDATE File SET MD5=\"%s\" WHERE FileId=%d", MD5, FileId);
64    stat = UPDATE_DB(mdb, mdb->cmd);
65    db_unlock(mdb);
66    return stat;
67 }
68
69 /* Mark the file record as being visited during database
70  * verify compare. Stuff JobId into MarkedId field
71  */
72 int db_mark_file_record(B_DB *mdb, FileId_t FileId, JobId_t JobId) 
73 {
74    int stat;
75
76    db_lock(mdb);
77    Mmsg(&mdb->cmd, "UPDATE File SET MarkId=%d WHERE FileId=%d", JobId, FileId);
78    stat = UPDATE_DB(mdb, mdb->cmd);
79    db_unlock(mdb);
80    return stat;
81 }
82
83 /*
84  * Update the Job record at end of Job
85  *
86  *  Returns: 0 on failure
87  *           1 on success
88  */
89 int
90 db_update_job_start_record(B_DB *mdb, JOB_DBR *jr)
91 {
92    char dt[MAX_TIME_LENGTH];
93    time_t stime;
94    struct tm tm;
95    btime_t JobTDate;
96    int stat;
97    char ed1[30];
98        
99    stime = jr->StartTime;
100    localtime_r(&stime, &tm);
101    strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
102    JobTDate = (btime_t)stime;
103
104    db_lock(mdb);
105    Mmsg(&mdb->cmd, "UPDATE Job SET Level='%c', StartTime=\"%s\", \
106 ClientId=%d, JobTDate=%s WHERE JobId=%d",
107       (char)(jr->Level), dt, jr->ClientId, edit_uint64(JobTDate, ed1), jr->JobId);
108    stat = UPDATE_DB(mdb, mdb->cmd);
109    db_unlock(mdb);
110 #ifdef HAVE_SQLITE
111    /******FIXME***** do this machine independently */
112    my_sqlite_query(mdb, "BEGIN");     /* begin transaction */
113 #endif
114    return stat;
115 }
116
117
118
119 /*
120  * Update the Job record at end of Job
121  *
122  *  Returns: 0 on failure
123  *           1 on success
124  */
125 int
126 db_update_job_end_record(B_DB *mdb, JOB_DBR *jr)
127 {
128    char dt[MAX_TIME_LENGTH];
129    time_t ttime;
130    struct tm tm;
131    int stat;
132    char ed1[30], ed2[30];
133    btime_t JobTDate;
134        
135    ttime = jr->EndTime;
136    localtime_r(&ttime, &tm);
137    strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
138    JobTDate = ttime;
139
140    db_lock(mdb);
141    Mmsg(&mdb->cmd,
142       "UPDATE Job SET JobStatus='%c', EndTime='%s', \
143 ClientId=%d, JobBytes=%s, JobFiles=%d, JobErrors=%d, VolSessionId=%d, \
144 VolSessionTime=%d, PoolId=%d, FileSetId=%d, JobTDate=%s WHERE JobId=%d",
145       (char)(jr->JobStatus), dt, jr->ClientId, edit_uint64(jr->JobBytes, ed1), 
146       jr->JobFiles, jr->JobErrors, jr->VolSessionId, jr->VolSessionTime, 
147       jr->PoolId, jr->FileSetId, edit_uint64(JobTDate, ed2), jr->JobId);
148
149    stat = UPDATE_DB(mdb, mdb->cmd);
150 #ifdef HAVE_SQLITE
151    my_sqlite_query(mdb, "COMMIT");    /* end transaction */
152 #endif
153    db_unlock(mdb);
154    return stat;
155 }
156
157
158 int
159 db_update_pool_record(B_DB *mdb, POOL_DBR *pr)
160 {
161    int stat;
162
163    db_lock(mdb);
164    Mmsg(&mdb->cmd,
165 "UPDATE Pool SET NumVols=%d, MaxVols=%d, UseOnce=%d, UseCatalog=%d, \
166 AcceptAnyVolume=%d, LabelFormat=\"%s\" WHERE PoolId=%d",
167       pr->NumVols, pr->MaxVols, pr->UseOnce, pr->UseCatalog,
168       pr->AcceptAnyVolume, pr->LabelFormat, pr->PoolId);
169
170    stat = UPDATE_DB(mdb, mdb->cmd);
171    db_unlock(mdb);
172    return stat;
173 }
174
175 /* 
176  * Update the Media Record at end of Session
177  *
178  * Returns: 0 on failure
179  *          numrows on success
180  */
181 int
182 db_update_media_record(B_DB *mdb, MEDIA_DBR *mr) 
183 {
184    char dt[MAX_TIME_LENGTH];
185    time_t ttime;
186    struct tm tm;
187    int stat;
188    char ed1[30], ed2[30];
189        
190    ttime = mr->LastWritten;
191    localtime_r(&ttime, &tm);
192    strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
193
194    Dmsg1(100, "update_media: FirstWritten=%d\n", mr->FirstWritten);
195    db_lock(mdb);
196    if (mr->VolMounts == 1) {
197       Mmsg(&mdb->cmd, "UPDATE Media SET FirstWritten=\"%s\"\
198  WHERE VolumeName=\"%s\"", dt, mr->VolumeName);
199       UPDATE_DB(mdb, mdb->cmd);
200    }
201
202    Mmsg(&mdb->cmd, "UPDATE Media SET VolJobs=%d,\
203  VolFiles=%d, VolBlocks=%d, VolBytes=%s, VolMounts=%d, VolErrors=%d,\
204  VolWrites=%d, VolMaxBytes=%s, LastWritten=\"%s\", VolStatus=\"%s\" \
205  WHERE VolumeName=\"%s\"",
206    mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
207    mr->VolMounts, mr->VolErrors, mr->VolWrites, 
208    edit_uint64(mr->VolMaxBytes, ed2), dt, 
209    mr->VolStatus, mr->VolumeName);
210
211    stat = UPDATE_DB(mdb, mdb->cmd);
212    db_unlock(mdb);
213    return stat;
214 }
215
216 #endif /* HAVE_MYSQL || HAVE_SQLITE */