2 * Bacula Catalog Database List records interface routines
4 * Kern Sibbald, March 2000
10 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
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 || HAVE_POSTGRESQL
39 /* -----------------------------------------------------------------------
41 * Generic Routines (or almost generic)
43 * -----------------------------------------------------------------------
46 /* Imported subroutines */
47 extern void list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type);
48 extern int QueryDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
52 * Submit general SQL query
54 int db_list_sql_query(JCR *jcr, B_DB *mdb, char *query, DB_LIST_HANDLER *sendit,
55 void *ctx, int verbose, e_list_type type)
58 if (sql_query(mdb, query) != 0) {
59 Mmsg(mdb->errmsg, _("Query failed: %s\n"), sql_strerror(mdb));
61 sendit(ctx, mdb->errmsg);
67 mdb->result = sql_store_result(mdb);
70 list_result(jcr, mdb, sendit, ctx, type);
78 db_list_pool_records(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type)
81 if (type == VERT_LIST) {
82 Mmsg(mdb->cmd, "SELECT PoolId,Name,NumVols,MaxVols,UseOnce,UseCatalog,"
83 "AcceptAnyVolume,VolRetention,VolUseDuration,MaxVolJobs,MaxVolBytes,"
84 "AutoPrune,Recycle,PoolType,LabelFormat,Enabled,ScratchPoolId,"
86 "FROM Pool ORDER BY PoolId");
88 Mmsg(mdb->cmd, "SELECT PoolId,Name,NumVols,MaxVols,PoolType,LabelFormat "
89 "FROM Pool ORDER BY PoolId");
92 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
97 list_result(jcr, mdb, sendit, ctx, type);
104 db_list_client_records(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type)
107 if (type == VERT_LIST) {
108 Mmsg(mdb->cmd, "SELECT ClientId,Name,Uname,AutoPrune,FileRetention,"
109 "FileRetention,JobRetention "
110 "FROM Client ORDER BY ClientId");
112 Mmsg(mdb->cmd, "SELECT ClientId,Name,FileRetention,JobRetention "
113 "FROM Client ORDER BY ClientId");
116 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
121 list_result(jcr, mdb, sendit, ctx, type);
123 sql_free_result(mdb);
129 * If VolumeName is non-zero, list the record for that Volume
130 * otherwise, list the Volumes in the Pool specified by PoolId
133 db_list_media_records(JCR *jcr, B_DB *mdb, MEDIA_DBR *mdbr,
134 DB_LIST_HANDLER *sendit, void *ctx, e_list_type type)
137 if (type == VERT_LIST) {
138 if (mdbr->VolumeName[0] != 0) {
139 Mmsg(mdb->cmd, "SELECT MediaId,VolumeName,Slot,PoolId,"
140 "MediaType,FirstWritten,LastWritten,LabelDate,VolJobs,"
141 "VolFiles,VolBlocks,VolMounts,VolBytes,VolErrors,VolWrites,"
142 "VolCapacityBytes,VolStatus,Recycle,VolRetention,"
143 "VolUseDuration,MaxVolJobs,MaxVolFiles,MaxVolBytes,InChanger,"
145 "FROM Media WHERE Media.VolumeName='%s'", mdbr->VolumeName);
147 Mmsg(mdb->cmd, "SELECT MediaId,VolumeName,Slot,PoolId,"
148 "MediaType,FirstWritten,LastWritten,LabelDate,VolJobs,"
149 "VolFiles,VolBlocks,VolMounts,VolBytes,VolErrors,VolWrites,"
150 "VolCapacityBytes,VolStatus,Recycle,VolRetention,"
151 "VolUseDuration,MaxVolJobs,MaxVolFiles,MaxVolBytes,InChanger,"
153 "FROM Media WHERE Media.PoolId=%u ORDER BY MediaId", mdbr->PoolId);
156 if (mdbr->VolumeName[0] != 0) {
157 Mmsg(mdb->cmd, "SELECT MediaId,VolumeName,VolStatus,"
158 "VolBytes,VolFiles,VolRetention,Recycle,Slot,InChanger,MediaType,LastWritten "
159 "FROM Media WHERE Media.VolumeName='%s'", mdbr->VolumeName);
161 Mmsg(mdb->cmd, "SELECT MediaId,VolumeName,VolStatus,"
162 "VolBytes,VolFiles,VolRetention,Recycle,Slot,InChanger,MediaType,LastWritten "
163 "FROM Media WHERE Media.PoolId=%u ORDER BY MediaId", mdbr->PoolId);
167 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
172 list_result(jcr, mdb, sendit, ctx, type);
174 sql_free_result(mdb);
178 void db_list_jobmedia_records(JCR *jcr, B_DB *mdb, uint32_t JobId,
179 DB_LIST_HANDLER *sendit, void *ctx, e_list_type type)
182 if (type == VERT_LIST) {
183 if (JobId > 0) { /* do by JobId */
184 Mmsg(mdb->cmd, "SELECT JobMediaId,JobId,Media.MediaId,Media.VolumeName,"
185 "FirstIndex,LastIndex,StartFile,JobMedia.EndFile,StartBlock,"
187 "FROM JobMedia,Media WHERE Media.MediaId=JobMedia.MediaId "
188 "AND JobMedia.JobId=%u", JobId);
190 Mmsg(mdb->cmd, "SELECT JobMediaId,JobId,Media.MediaId,Media.VolumeName,"
191 "FirstIndex,LastIndex,StartFile,JobMedia.EndFile,StartBlock,"
193 "FROM JobMedia,Media WHERE Media.MediaId=JobMedia.MediaId");
197 if (JobId > 0) { /* do by JobId */
198 Mmsg(mdb->cmd, "SELECT JobId,Media.VolumeName,FirstIndex,LastIndex "
199 "FROM JobMedia,Media WHERE Media.MediaId=JobMedia.MediaId "
200 "AND JobMedia.JobId=%u", JobId);
202 Mmsg(mdb->cmd, "SELECT JobId,Media.VolumeName,FirstIndex,LastIndex "
203 "FROM JobMedia,Media WHERE Media.MediaId=JobMedia.MediaId");
206 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
211 list_result(jcr, mdb, sendit, ctx, type);
213 sql_free_result(mdb);
220 * List Job record(s) that match JOB_DBR
222 * Currently, we return all jobs or if jr->JobId is set,
223 * only the job with the specified id.
226 db_list_job_records(JCR *jcr, B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit,
227 void *ctx, e_list_type type)
230 if (type == VERT_LIST) {
231 if (jr->JobId == 0 && jr->Job[0] == 0) {
233 "SELECT JobId,Job,Job.Name,PurgedFiles,Type,Level,"
234 "Job.ClientId,Client.Name,JobStatus,SchedTime,"
235 "StartTime,EndTime,JobTDate,"
236 "VolSessionId,VolSessionTime,JobFiles,JobErrors,"
237 "JobMissingFiles,Job.PoolId,Pool.Name,Job.FileSetId,FileSet.FileSet "
238 "FROM Job,Client,Pool,FileSet WHERE "
239 "Client.ClientId=Job.ClientId AND Pool.PoolId=Job.PoolId "
240 "AND FileSet.FileSetId=Job.FileSetId ORDER BY StartTime");
241 } else { /* single record */
243 "SELECT JobId,Job,Job.Name,PurgedFiles,Type,Level,"
244 "Job.ClientId,Client.Name,JobStatus,SchedTime,"
245 "StartTime,EndTime,JobTDate,"
246 "VolSessionId,VolSessionTime,JobFiles,JobErrors,"
247 "JobMissingFiles,Job.PoolId,Pool.Name,Job.FileSetId,FileSet.FileSet "
248 "FROM Job,Client,Pool,FileSet WHERE Job.JobId=%u AND "
249 "Client.ClientId=Job.ClientId AND Pool.PoolId=Job.PoolId "
250 "AND FileSet.FileSetId=Job.FileSetId", jr->JobId);
253 if (jr->JobId == 0 && jr->Job[0] == 0) {
255 "SELECT JobId,Name,StartTime,Type,Level,JobFiles,JobBytes,JobStatus "
256 "FROM Job ORDER BY StartTime");
257 } else { /* single record */
258 Mmsg(mdb->cmd, "SELECT JobId,Name,StartTime,Type,Level,"
259 "JobFiles,JobBytes,JobStatus FROM Job WHERE JobId=%u", jr->JobId);
262 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
266 list_result(jcr, mdb, sendit, ctx, type);
268 sql_free_result(mdb);
277 db_list_job_totals(JCR *jcr, B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, void *ctx)
282 Mmsg(mdb->cmd, "SELECT count(*) AS Jobs,sum(JobFiles) "
283 "AS Files,sum(JobBytes) AS Bytes,Name AS Job FROM Job GROUP BY Name");
285 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
290 list_result(jcr, mdb, sendit, ctx, HORZ_LIST);
292 sql_free_result(mdb);
295 Mmsg(mdb->cmd, "SELECT count(*) AS Jobs,sum(JobFiles) "
296 "AS Files,sum(JobBytes) As Bytes FROM Job");
298 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
303 list_result(jcr, mdb, sendit, ctx, HORZ_LIST);
305 sql_free_result(mdb);
310 * Stupid MySQL is NON-STANDARD !
313 #define FN "CONCAT(Path.Path,Filename.Name)"
315 #define FN "Path.Path||Filename.Name"
319 db_list_files_for_job(JCR *jcr, B_DB *mdb, uint32_t jobid, DB_LIST_HANDLER *sendit, void *ctx)
323 Mmsg(mdb->cmd, "SELECT " FN " AS Filename FROM File,"
324 "Filename,Path WHERE File.JobId=%u AND Filename.FilenameId=File.FilenameId "
325 "AND Path.PathId=File.PathId",
328 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
333 list_result(jcr, mdb, sendit, ctx, HORZ_LIST);
335 sql_free_result(mdb);
340 #endif /* HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL */