]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_list.c
1.19 24Apr02
[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
7 /*
8    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
9
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.
14
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.
19
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,
23    MA 02111-1307, USA.
24
25  */
26
27 /* The following is necessary so that we do not include
28  * the dummy external definition of DB.
29  */
30 #define __SQL_C                       /* indicate that this is sql.c */
31
32 #include "bacula.h"
33 #include "cats.h"
34
35 #if    HAVE_MYSQL || HAVE_SQLITE
36
37 /* -----------------------------------------------------------------------
38  *
39  *   Generic Routines (or almost generic)
40  *
41  * -----------------------------------------------------------------------
42  */
43
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);
47
48
49 /* 
50  * Submit general SQL query
51  */
52 int db_list_sql_query(B_DB *mdb, char *query, DB_LIST_HANDLER *sendit, void *ctx)
53 {
54    P(mdb->mutex);
55    if (sql_query(mdb, query) != 0) {
56       Mmsg(&mdb->errmsg, _("Query failed: %s\n"), sql_strerror(mdb));
57       sendit(ctx, mdb->errmsg);
58       V(mdb->mutex);
59       return 0;
60    }
61
62    mdb->result = sql_store_result(mdb);
63
64    if (mdb->result) {
65       list_result(mdb, sendit, ctx);
66       sql_free_result(mdb);
67    }
68    V(mdb->mutex);
69    return 1;
70 }
71
72 void
73 db_list_pool_records(B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx) 
74 {
75    Mmsg(&mdb->cmd, "SELECT PoolId,Name,NumVols,MaxVols,PoolType,LabelFormat \
76 FROM Pool");
77
78    P(mdb->mutex);
79    if (!QUERY_DB(mdb, mdb->cmd)) {
80       V(mdb->mutex);
81       return;
82    }
83
84    list_result(mdb, sendit, ctx);
85    
86    sql_free_result(mdb);
87    V(mdb->mutex);
88 }
89
90 void
91 db_list_media_records(B_DB *mdb, MEDIA_DBR *mdbr, DB_LIST_HANDLER *sendit, void *ctx)
92 {
93
94    Mmsg(&mdb->cmd, "SELECT VolumeName,MediaType,VolStatus,\
95 VolBytes,LastWritten \
96 FROM Media WHERE Media.PoolId=%d ORDER BY MediaId", mdbr->PoolId);
97
98    P(mdb->mutex);
99    if (!QUERY_DB(mdb, mdb->cmd)) {
100       V(mdb->mutex);
101       return;
102    }
103
104    list_result(mdb, sendit, ctx);
105    
106    sql_free_result(mdb);
107    V(mdb->mutex);
108 }
109
110 void db_list_jobmedia_records(B_DB *mdb, uint32_t JobId, DB_LIST_HANDLER *sendit, void *ctx)
111 {
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", 
115            JobId);
116    } else {
117       Mmsg(&mdb->cmd, "SELECT JobId, Media.VolumeName, FirstIndex, LastIndex \
118 FROM JobMedia, Media WHERE Media.MediaId=JobMedia.MediaId");
119    }
120
121    P(mdb->mutex);
122    if (!QUERY_DB(mdb, mdb->cmd)) {
123       V(mdb->mutex);
124       return;
125    }
126
127    list_result(mdb, sendit, ctx);
128    
129    sql_free_result(mdb);
130    V(mdb->mutex);
131 }
132
133
134
135 /*
136  * List Job record(s) that match JOB_DBR
137  *
138  *  Currently, we return all jobs or if jr->JobId is set,
139  *  only the job with the specified id.
140  */
141 void
142 db_list_job_records(B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, void *ctx)
143 {
144
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);
151    }
152
153    P(mdb->mutex);
154    if (!QUERY_DB(mdb, mdb->cmd)) {
155       V(mdb->mutex);
156       return;
157    }
158
159    list_result(mdb, sendit, ctx);
160    
161    sql_free_result(mdb);
162    V(mdb->mutex);
163 }
164
165 /*
166  * List Job totals
167  *
168  */
169 void
170 db_list_job_totals(B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, void *ctx)
171 {
172
173
174    P(mdb->mutex);
175
176    /* List by Job */
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");
179
180    if (!QUERY_DB(mdb, mdb->cmd)) {
181       V(mdb->mutex);
182       return;
183    }
184
185    list_result(mdb, sendit, ctx);
186    
187    sql_free_result(mdb);
188
189    /* Do Grand Total */
190    Mmsg(&mdb->cmd, "SELECT count(*) AS Jobs,sum(JobFiles) \
191 AS Files,sum(JobBytes) As Bytes FROM Job");
192
193    if (!QUERY_DB(mdb, mdb->cmd)) {
194       V(mdb->mutex);
195       return;
196    }
197
198    list_result(mdb, sendit, ctx);
199    
200    sql_free_result(mdb);
201    V(mdb->mutex);
202 }
203
204
205 void
206 db_list_files_for_job(B_DB *mdb, uint32_t jobid, DB_LIST_HANDLER *sendit, void *ctx)
207 {
208
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",
212       jobid);
213
214    P(mdb->mutex);
215    if (!QUERY_DB(mdb, mdb->cmd)) {
216       V(mdb->mutex);
217       return;
218    }
219
220    list_result(mdb, sendit, ctx);
221    
222    sql_free_result(mdb);
223    V(mdb->mutex);
224 }
225
226
227 #endif /* HAVE_MYSQL || HAVE_SQLITE */