2 * Bacula Catalog Database List records interface routines
4 * Kern Sibbald, March 2000
8 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public
21 License along with this program; if not, write to the Free
22 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 /* The following is necessary so that we do not include
28 * the dummy external definition of DB.
30 #define __SQL_C /* indicate that this is sql.c */
35 #if HAVE_MYSQL || HAVE_SQLITE
37 /* -----------------------------------------------------------------------
39 * Generic Routines (or almost generic)
41 * -----------------------------------------------------------------------
44 /* Imported subroutines */
45 extern void list_result(B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx);
46 extern int QueryDB(char *file, int line, B_DB *db, char *select_cmd);
50 * Submit general SQL query
52 int db_list_sql_query(B_DB *mdb, char *query, DB_LIST_HANDLER *sendit, void *ctx)
55 if (sql_query(mdb, query) != 0) {
56 Mmsg(&mdb->errmsg, _("Query failed: %s\n"), sql_strerror(mdb));
57 sendit(ctx, mdb->errmsg);
62 mdb->result = sql_store_result(mdb);
65 list_result(mdb, sendit, ctx);
73 db_list_pool_records(B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx)
75 Mmsg(&mdb->cmd, "SELECT PoolId,Name,NumVols,MaxVols,PoolType,LabelFormat \
79 if (!QUERY_DB(mdb, mdb->cmd)) {
84 list_result(mdb, sendit, ctx);
91 db_list_media_records(B_DB *mdb, MEDIA_DBR *mdbr, DB_LIST_HANDLER *sendit, void *ctx)
94 Mmsg(&mdb->cmd, "SELECT VolumeName,MediaType,VolStatus,\
95 VolBytes,LastWritten,VolRetention,Recycle \
96 FROM Media WHERE Media.PoolId=%d ORDER BY MediaId", mdbr->PoolId);
99 if (!QUERY_DB(mdb, mdb->cmd)) {
104 list_result(mdb, sendit, ctx);
106 sql_free_result(mdb);
110 void db_list_jobmedia_records(B_DB *mdb, uint32_t JobId, DB_LIST_HANDLER *sendit, void *ctx)
112 if (JobId > 0) { /* do by JobId */
113 Mmsg(&mdb->cmd, "SELECT JobId, Media.VolumeName, FirstIndex, LastIndex \
114 FROM JobMedia, Media WHERE Media.MediaId=JobMedia.MediaId and JobMedia.JobId=%d",
117 Mmsg(&mdb->cmd, "SELECT JobId, Media.VolumeName, FirstIndex, LastIndex \
118 FROM JobMedia, Media WHERE Media.MediaId=JobMedia.MediaId");
122 if (!QUERY_DB(mdb, mdb->cmd)) {
127 list_result(mdb, sendit, ctx);
129 sql_free_result(mdb);
136 * List Job record(s) that match JOB_DBR
138 * Currently, we return all jobs or if jr->JobId is set,
139 * only the job with the specified id.
142 db_list_job_records(B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, void *ctx)
145 if (jr->JobId == 0 && jr->Job[0] == 0) {
146 Mmsg(&mdb->cmd, "SELECT JobId,Name,StartTime,Type,Level,\
147 JobFiles,JobBytes,JobStatus FROM Job");
148 } else { /* single record */
149 Mmsg(&mdb->cmd, "SELECT JobId,Name,StartTime,Type,Level,\
150 JobFiles,JobBytes,JobStatus FROM Job WHERE Job.JobId=%d", jr->JobId);
154 if (!QUERY_DB(mdb, mdb->cmd)) {
159 list_result(mdb, sendit, ctx);
161 sql_free_result(mdb);
170 db_list_job_totals(B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, void *ctx)
177 Mmsg(&mdb->cmd, "SELECT count(*) AS Jobs, sum(JobFiles) \
178 AS Files, sum(JobBytes) AS Bytes, Name AS Job FROM Job GROUP BY Name");
180 if (!QUERY_DB(mdb, mdb->cmd)) {
185 list_result(mdb, sendit, ctx);
187 sql_free_result(mdb);
190 Mmsg(&mdb->cmd, "SELECT count(*) AS Jobs,sum(JobFiles) \
191 AS Files,sum(JobBytes) As Bytes FROM Job");
193 if (!QUERY_DB(mdb, mdb->cmd)) {
198 list_result(mdb, sendit, ctx);
200 sql_free_result(mdb);
206 db_list_files_for_job(B_DB *mdb, uint32_t jobid, DB_LIST_HANDLER *sendit, void *ctx)
209 Mmsg(&mdb->cmd, "SELECT Path.Path,Filename.Name FROM File,\
210 Filename,Path WHERE File.JobId=%d and Filename.FilenameId=File.FilenameId \
211 and Path.PathId=File.PathId",
215 if (!QUERY_DB(mdb, mdb->cmd)) {
220 list_result(mdb, sendit, ctx);
222 sql_free_result(mdb);
227 #endif /* HAVE_MYSQL || HAVE_SQLITE */