]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_list.c
Fix NumVols in Pools, implement full listing of DB tables
[bacula/bacula] / bacula / src / cats / sql_list.c
1 /*
2  * Bacula Catalog Database List records 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 list_result(B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx, int full_list);
48 extern int QueryDB(char *file, int line, void *jcr, B_DB *db, char *select_cmd);
49
50
51 /* 
52  * Submit general SQL query
53  */
54 int db_list_sql_query(void *jcr, B_DB *mdb, char *query, DB_LIST_HANDLER *sendit, 
55                       void *ctx, int verbose, int full)
56 {
57    db_lock(mdb);
58    if (sql_query(mdb, query) != 0) {
59       Mmsg(&mdb->errmsg, _("Query failed: %s\n"), sql_strerror(mdb));
60       if (verbose) {
61          sendit(ctx, mdb->errmsg);
62       }
63       db_unlock(mdb);
64       return 0;
65    }
66
67    mdb->result = sql_store_result(mdb);
68
69    if (mdb->result) {
70       list_result(mdb, sendit, ctx, full);
71       sql_free_result(mdb);
72    }
73    db_unlock(mdb);
74    return 1;
75 }
76
77 void
78 db_list_pool_records(void *jcr, B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx, int full) 
79 {
80    if (full) {
81       Mmsg(&mdb->cmd, "SELECT PoolId,Name,NumVols,MaxVols,UseOnce,UseCatalog,"
82          "AcceptAnyVolume,VolRetention,VolUseDuration,MaxVolJobs,MaxVolBytes,"
83          "AutoPrune,Recycle,PoolType,LabelFormat "
84           "FROM Pool ORDER BY PoolId");
85    } else {
86       Mmsg(&mdb->cmd, "SELECT PoolId,Name,NumVols,MaxVols,PoolType,LabelFormat "
87         "FROM Pool ORDER BY PoolId");
88    }
89
90    db_lock(mdb);
91    if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
92       db_unlock(mdb);
93       return;
94    }
95
96    list_result(mdb, sendit, ctx, full);
97    
98    sql_free_result(mdb);
99    db_unlock(mdb);
100 }
101
102 void
103 db_list_client_records(void *jcr, B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx, int full)
104 {
105    if (full) {
106       Mmsg(&mdb->cmd, "SELECT ClientId,Name,Uname,AutoPrune,FileRetention,"
107          "FileRetention,JobRetention "
108          "FROM Client ORDER BY ClientId");
109    } else {
110       Mmsg(&mdb->cmd, "SELECT ClientId,Name,FileRetention,JobRetention "
111          "FROM Client ORDER BY ClientId");
112    }
113
114    db_lock(mdb);
115    if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
116       db_unlock(mdb);
117       return;
118    }
119
120    list_result(mdb, sendit, ctx, full);
121    
122    sql_free_result(mdb);
123    db_unlock(mdb);
124 }
125
126
127 void
128 db_list_media_records(void *jcr, B_DB *mdb, MEDIA_DBR *mdbr, 
129                       DB_LIST_HANDLER *sendit, void *ctx, int full)
130 {
131    if (full) {
132       Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,Slot,PoolId,"
133          "MediaType,FirstWritten,LastWritten,LabelDate,VolJobs,"
134          "VolFiles,VolBlocks,VolMounts,VolBytes,VolErrors,VolWrites,"
135          "VolCapacityBytes,VolStatus,Recycle,VolRetention,"
136          "VolUseDuration,MaxVolJobs,MaxVolFiles,MaxVolBytes "
137          "FROM Media WHERE Media.PoolId=%u ORDER BY MediaId", mdbr->PoolId);
138    } else {
139       Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,MediaType,VolStatus,"
140          "VolBytes,LastWritten,VolRetention,Recycle,Slot "
141          "FROM Media WHERE Media.PoolId=%u ORDER BY MediaId", mdbr->PoolId);
142    }
143
144    db_lock(mdb);
145    if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
146       db_unlock(mdb);
147       return;
148    }
149
150    list_result(mdb, sendit, ctx, full);
151    
152    sql_free_result(mdb);
153    db_unlock(mdb);
154 }
155
156 void db_list_jobmedia_records(void *jcr, B_DB *mdb, uint32_t JobId, 
157                               DB_LIST_HANDLER *sendit, void *ctx, int full)
158 {
159    if (full) {
160       if (JobId > 0) {                   /* do by JobId */
161          Mmsg(&mdb->cmd, "SELECT JobMediaId,JobId,MediaId,Media.VolumeName,"
162             "FirstIndex,LastIndex,StartFile,EndFile,StartBlock,EndBlock "
163             "FROM JobMedia,Media WHERE Media.MediaId=JobMedia.MediaId "
164             "AND JobMedia.JobId=%u", JobId);
165       } else {
166          Mmsg(&mdb->cmd, "SELECT JobMediaId,JobId,MediaId,Media.VolumeName,"
167             "FirstIndex,LastIndex,StartFile,EndFile,StartBlock,EndBlock "
168             "FROM JobMedia,Media WHERE Media.MediaId=JobMedia.MediaId");
169       }
170
171    } else {
172       if (JobId > 0) {                   /* do by JobId */
173          Mmsg(&mdb->cmd, "SELECT JobId,Media.VolumeName,FirstIndex,LastIndex"
174             "FROM JobMedia,Media WHERE Media.MediaId=JobMedia.MediaId "
175             "AND JobMedia.JobId=%u", JobId);
176       } else {
177          Mmsg(&mdb->cmd, "SELECT JobId,Media.VolumeName,FirstIndex,LastIndex "
178             "FROM JobMedia,Media WHERE Media.MediaId=JobMedia.MediaId");
179       }
180    }
181    db_lock(mdb);
182    if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
183       db_unlock(mdb);
184       return;
185    }
186
187    list_result(mdb, sendit, ctx, full);
188    
189    sql_free_result(mdb);
190    db_unlock(mdb);
191 }
192
193
194
195 /*
196  * List Job record(s) that match JOB_DBR
197  *
198  *  Currently, we return all jobs or if jr->JobId is set,
199  *  only the job with the specified id.
200  */
201 void
202 db_list_job_records(void *jcr, B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, 
203                     void *ctx, int full)
204 {
205    if (full) {
206       if (jr->JobId == 0 && jr->Job[0] == 0) {
207          Mmsg(&mdb->cmd, 
208             "SELECT JobId,Job,Job.Name,PurgedFiles,Type,Level,"
209             "Job.ClientId,Client.Name,JobStatus,SchedTime,"
210             "StartTime,EndTime,JobTDate,"
211             "VolSessionId,VolSessionTime,JobFiles,JobErrors,"
212             "JobMissingFiles,Job.PoolId,Pool.Name,Job.FileSetId,FileSet.FileSet "
213             "FROM Job,Client,Pool,FileSet WHERE "
214             "Client.ClientId=Job.ClientId AND Pool.PoolId=Job.PoolId "
215             "AND FileSet.FileSetId=Job.FileSetId  ORDER BY JobId");
216       } else {                           /* single record */
217          Mmsg(&mdb->cmd, 
218             "SELECT JobId,Job,Job.Name,PurgedFiles,Type,Level,"
219             "Job.ClientId,Client.Name,JobStatus,SchedTime,"
220             "StartTime,EndTime,JobTDate,"
221             "VolSessionId,VolSessionTime,JobFiles,JobErrors,"
222             "JobMissingFiles,Job.PoolId,Pool.Name,Job.FileSetId,FileSet.FileSet "
223             "FROM Job,Client,Pool,FileSet WHERE Job.JobId=%u AND "
224             "Client.ClientId=Job.ClientId AND Pool.PoolId=Job.PoolId "
225             "AND FileSet.FileSetId=Job.FileSetId", jr->JobId);
226       }
227    } else {
228       if (jr->JobId == 0 && jr->Job[0] == 0) {
229          Mmsg(&mdb->cmd, 
230            "SELECT JobId,Name,StartTime,Type,Level,JobFiles,JobBytes,JobStatus "
231            "FROM Job ORDER BY JobId");
232       } else {                           /* single record */
233          Mmsg(&mdb->cmd, "SELECT JobId,Name,StartTime,Type,Level,"
234             "JobFiles,JobBytes,JobStatus FROM Job WHERE JobId=%u", jr->JobId);
235       }
236    }
237    db_lock(mdb);
238    if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
239       db_unlock(mdb);
240       return;
241    }
242
243    list_result(mdb, sendit, ctx, full);
244    
245    sql_free_result(mdb);
246    db_unlock(mdb);
247 }
248
249 /*
250  * List Job totals
251  *
252  */
253 void
254 db_list_job_totals(void *jcr, B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, void *ctx)
255 {
256    db_lock(mdb);
257
258    /* List by Job */
259    Mmsg(&mdb->cmd, "SELECT  count(*) AS Jobs, sum(JobFiles) \
260 AS Files, sum(JobBytes) AS Bytes, Name AS Job FROM Job GROUP BY Name");
261
262    if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
263       db_unlock(mdb);
264       return;
265    }
266
267    list_result(mdb, sendit, ctx, 0);
268    
269    sql_free_result(mdb);
270
271    /* Do Grand Total */
272    Mmsg(&mdb->cmd, "SELECT count(*) AS Jobs,sum(JobFiles) \
273 AS Files,sum(JobBytes) As Bytes FROM Job");
274
275    if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
276       db_unlock(mdb);
277       return;
278    }
279
280    list_result(mdb, sendit, ctx, 0);
281    
282    sql_free_result(mdb);
283    db_unlock(mdb);
284 }
285
286
287 void
288 db_list_files_for_job(void *jcr, B_DB *mdb, uint32_t jobid, DB_LIST_HANDLER *sendit, void *ctx)
289 {
290
291    Mmsg(&mdb->cmd, "SELECT Path.Path,Filename.Name FROM File,\
292 Filename,Path WHERE File.JobId=%u AND Filename.FilenameId=File.FilenameId \
293 AND Path.PathId=File.PathId",
294       jobid);
295
296    db_lock(mdb);
297    if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
298       db_unlock(mdb);
299       return;
300    }
301
302    list_result(mdb, sendit, ctx, 0);
303    
304    sql_free_result(mdb);
305    db_unlock(mdb);
306 }
307
308
309 #endif /* HAVE_MYSQL || HAVE_SQLITE */