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);
48 extern int QueryDB(char *file, int line, B_DB *db, char *select_cmd);
52 * Submit general SQL query
54 int db_list_sql_query(B_DB *mdb, char *query, DB_LIST_HANDLER *sendit, void *ctx)
57 if (sql_query(mdb, query) != 0) {
58 Mmsg(&mdb->errmsg, _("Query failed: %s\n"), sql_strerror(mdb));
59 sendit(ctx, mdb->errmsg);
64 mdb->result = sql_store_result(mdb);
67 list_result(mdb, sendit, ctx);
75 db_list_pool_records(B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx)
77 Mmsg(&mdb->cmd, "SELECT PoolId,Name,NumVols,MaxVols,PoolType,LabelFormat \
81 if (!QUERY_DB(mdb, mdb->cmd)) {
86 list_result(mdb, sendit, ctx);
93 db_list_media_records(B_DB *mdb, MEDIA_DBR *mdbr, DB_LIST_HANDLER *sendit, void *ctx)
96 Mmsg(&mdb->cmd, "SELECT VolumeName,MediaType,VolStatus,\
97 VolBytes,LastWritten,VolRetention,Recycle \
98 FROM Media WHERE Media.PoolId=%d ORDER BY MediaId", mdbr->PoolId);
101 if (!QUERY_DB(mdb, mdb->cmd)) {
106 list_result(mdb, sendit, ctx);
108 sql_free_result(mdb);
112 void db_list_jobmedia_records(B_DB *mdb, uint32_t JobId, DB_LIST_HANDLER *sendit, void *ctx)
114 if (JobId > 0) { /* do by JobId */
115 Mmsg(&mdb->cmd, "SELECT JobId, Media.VolumeName, FirstIndex, LastIndex \
116 FROM JobMedia, Media WHERE Media.MediaId=JobMedia.MediaId and JobMedia.JobId=%d",
119 Mmsg(&mdb->cmd, "SELECT JobId, Media.VolumeName, FirstIndex, LastIndex \
120 FROM JobMedia, Media WHERE Media.MediaId=JobMedia.MediaId");
124 if (!QUERY_DB(mdb, mdb->cmd)) {
129 list_result(mdb, sendit, ctx);
131 sql_free_result(mdb);
138 * List Job record(s) that match JOB_DBR
140 * Currently, we return all jobs or if jr->JobId is set,
141 * only the job with the specified id.
144 db_list_job_records(B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, void *ctx)
147 if (jr->JobId == 0 && jr->Job[0] == 0) {
148 Mmsg(&mdb->cmd, "SELECT JobId,Name,StartTime,Type,Level,\
149 JobFiles,JobBytes,JobStatus FROM Job");
150 } else { /* single record */
151 Mmsg(&mdb->cmd, "SELECT JobId,Name,StartTime,Type,Level,\
152 JobFiles,JobBytes,JobStatus FROM Job WHERE Job.JobId=%d", jr->JobId);
156 if (!QUERY_DB(mdb, mdb->cmd)) {
161 list_result(mdb, sendit, ctx);
163 sql_free_result(mdb);
172 db_list_job_totals(B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, void *ctx)
179 Mmsg(&mdb->cmd, "SELECT count(*) AS Jobs, sum(JobFiles) \
180 AS Files, sum(JobBytes) AS Bytes, Name AS Job FROM Job GROUP BY Name");
182 if (!QUERY_DB(mdb, mdb->cmd)) {
187 list_result(mdb, sendit, ctx);
189 sql_free_result(mdb);
192 Mmsg(&mdb->cmd, "SELECT count(*) AS Jobs,sum(JobFiles) \
193 AS Files,sum(JobBytes) As Bytes FROM Job");
195 if (!QUERY_DB(mdb, mdb->cmd)) {
200 list_result(mdb, sendit, ctx);
202 sql_free_result(mdb);
208 db_list_files_for_job(B_DB *mdb, uint32_t jobid, DB_LIST_HANDLER *sendit, void *ctx)
211 Mmsg(&mdb->cmd, "SELECT Path.Path,Filename.Name FROM File,\
212 Filename,Path WHERE File.JobId=%d and Filename.FilenameId=File.FilenameId \
213 and Path.PathId=File.PathId",
217 if (!QUERY_DB(mdb, mdb->cmd)) {
222 list_result(mdb, sendit, ctx);
224 sql_free_result(mdb);
229 #endif /* HAVE_MYSQL || HAVE_SQLITE */