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
39 /* -----------------------------------------------------------------------
41 * Generic Routines (or almost generic)
43 * -----------------------------------------------------------------------
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);
52 * Submit general SQL query
54 int db_list_sql_query(void *jcr, B_DB *mdb, char *query, DB_LIST_HANDLER *sendit,
55 void *ctx, int verbose, int full)
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(mdb, sendit, ctx, full);
78 db_list_pool_records(void *jcr, B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx, int 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");
86 Mmsg(&mdb->cmd, "SELECT PoolId,Name,NumVols,MaxVols,PoolType,LabelFormat "
87 "FROM Pool ORDER BY PoolId");
91 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
96 list_result(mdb, sendit, ctx, full);
103 db_list_client_records(void *jcr, B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx, int full)
106 Mmsg(&mdb->cmd, "SELECT ClientId,Name,Uname,AutoPrune,FileRetention,"
107 "FileRetention,JobRetention "
108 "FROM Client ORDER BY ClientId");
110 Mmsg(&mdb->cmd, "SELECT ClientId,Name,FileRetention,JobRetention "
111 "FROM Client ORDER BY ClientId");
115 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
120 list_result(mdb, sendit, ctx, full);
122 sql_free_result(mdb);
128 db_list_media_records(void *jcr, B_DB *mdb, MEDIA_DBR *mdbr,
129 DB_LIST_HANDLER *sendit, void *ctx, int 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);
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);
145 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
150 list_result(mdb, sendit, ctx, full);
152 sql_free_result(mdb);
156 void db_list_jobmedia_records(void *jcr, B_DB *mdb, uint32_t JobId,
157 DB_LIST_HANDLER *sendit, void *ctx, int 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);
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");
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);
177 Mmsg(&mdb->cmd, "SELECT JobId,Media.VolumeName,FirstIndex,LastIndex "
178 "FROM JobMedia,Media WHERE Media.MediaId=JobMedia.MediaId");
182 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
187 list_result(mdb, sendit, ctx, full);
189 sql_free_result(mdb);
196 * List Job record(s) that match JOB_DBR
198 * Currently, we return all jobs or if jr->JobId is set,
199 * only the job with the specified id.
202 db_list_job_records(void *jcr, B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit,
206 if (jr->JobId == 0 && jr->Job[0] == 0) {
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 */
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);
228 if (jr->JobId == 0 && jr->Job[0] == 0) {
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);
238 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
243 list_result(mdb, sendit, ctx, full);
245 sql_free_result(mdb);
254 db_list_job_totals(void *jcr, B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, void *ctx)
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");
262 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
267 list_result(mdb, sendit, ctx, 0);
269 sql_free_result(mdb);
272 Mmsg(&mdb->cmd, "SELECT count(*) AS Jobs,sum(JobFiles) \
273 AS Files,sum(JobBytes) As Bytes FROM Job");
275 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
280 list_result(mdb, sendit, ctx, 0);
282 sql_free_result(mdb);
288 db_list_files_for_job(void *jcr, B_DB *mdb, uint32_t jobid, DB_LIST_HANDLER *sendit, void *ctx)
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",
297 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
302 list_result(mdb, sendit, ctx, 0);
304 sql_free_result(mdb);
309 #endif /* HAVE_MYSQL || HAVE_SQLITE */