2 * Bacula Catalog Database Find record interface routines
4 * Note, generally, these routines are more complicated
5 * that a simple search by name or id. Such simple
8 * Kern Sibbald, December 2000
14 Copyright (C) 2000-2003 Kern Sibbald and John Walker
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License as
18 published by the Free Software Foundation; either version 2 of
19 the License, or (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with this program; if not, write to the Free
28 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34 /* The following is necessary so that we do not include
35 * the dummy external definition of DB.
37 #define __SQL_C /* indicate that this is sql.c */
42 #if HAVE_MYSQL || HAVE_SQLITE
44 /* -----------------------------------------------------------------------
46 * Generic Routines (or almost generic)
48 * -----------------------------------------------------------------------
51 /* Imported subroutines */
52 extern void print_result(B_DB *mdb);
53 extern int QueryDB(char *file, int line, void *jcr, B_DB *db, char *select_cmd);
56 * Find job start time. Used to find last full save
57 * for Incremental and Differential saves.
59 * Returns: 0 on failure
60 * 1 on success, jr is unchanged, but stime is set
63 db_find_job_start_time(void *jcr, B_DB *mdb, JOB_DBR *jr, POOLMEM **stime)
68 pm_strcpy(stime, "0000-00-00 00:00:00"); /* default */
70 /* If no Id given, we must find corresponding job */
72 /* Differential is since last Full backup */
73 if (jr->Level == L_DIFFERENTIAL) {
75 "SELECT JobId FROM Job WHERE JobStatus='T' AND Type='%c' AND \
76 Level='%c' AND Name='%s' AND ClientId=%d AND FileSetId=%d \
77 ORDER BY StartTime DESC LIMIT 1",
78 jr->Type, L_FULL, jr->Name, jr->ClientId, jr->FileSetId);
79 /* Incremental is since last Full, Incremental, or Differential */
80 } else if (jr->Level == L_INCREMENTAL) {
82 "SELECT JobId FROM Job WHERE JobStatus='T' AND Type='%c' AND \
83 Level IN ('%c','%c','%c') AND Name='%s' AND ClientId=%d \
84 ORDER BY StartTime DESC LIMIT 1",
85 jr->Type, L_INCREMENTAL, L_DIFFERENTIAL, L_FULL, jr->Name,
88 Mmsg1(&mdb->errmsg, _("Unknown level=%d\n"), jr->Level);
92 Dmsg1(100, "Submitting: %s\n", mdb->cmd);
93 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
94 Mmsg1(&mdb->errmsg, _("Query error for start time request: %s\n"), mdb->cmd);
98 if ((row = sql_fetch_row(mdb)) == NULL) {
103 JobId = atoi(row[0]);
104 sql_free_result(mdb);
106 JobId = jr->JobId; /* search for particular id */
109 Dmsg1(100, "Submitting: %s\n", mdb->cmd);
110 Mmsg(&mdb->cmd, "SELECT StartTime FROM Job WHERE Job.JobId=%d", JobId);
112 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
113 Mmsg1(&mdb->errmsg, _("Query error for start time request: %s\n"), mdb->cmd);
118 if ((row = sql_fetch_row(mdb)) == NULL) {
119 pm_strcpy(stime, ""); /* set EOS */
120 Mmsg2(&mdb->errmsg, _("No Job found for JobId=%d: %s\n"), JobId, sql_strerror(mdb));
121 sql_free_result(mdb);
125 Dmsg1(100, "Got start time: %s\n", row[0]);
126 pm_strcpy(stime, row[0]);
128 sql_free_result(mdb);
135 * Find JobId of last job that ran. E.g. for
136 * VERIFY_CATALOG we want the JobId of the last INIT.
137 * For VERIFY_VOLUME_TO_CATALOG, we want the JobId of the last Job.
139 * Returns: 1 on success
143 db_find_last_jobid(void *jcr, B_DB *mdb, JOB_DBR *jr)
149 if (jr->Level == L_VERIFY_CATALOG) {
151 "SELECT JobId FROM Job WHERE Type='%c' AND Level='%c' AND Name='%s' AND \
152 ClientId=%d ORDER BY StartTime DESC LIMIT 1",
153 JT_VERIFY, L_VERIFY_INIT, jr->Name, jr->ClientId);
154 } else if (jr->Level == L_VERIFY_VOLUME_TO_CATALOG) {
156 "SELECT JobId FROM Job WHERE Type='%c' AND \
157 ClientId=%d ORDER BY StartTime DESC LIMIT 1",
158 JT_BACKUP, jr->ClientId);
160 Mmsg1(&mdb->errmsg, _("Unknown Job level=%c\n"), jr->Level);
165 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
169 if ((row = sql_fetch_row(mdb)) == NULL) {
170 Mmsg1(&mdb->errmsg, _("No Job found for: %s.\n"), mdb->cmd);
171 sql_free_result(mdb);
176 jr->JobId = atoi(row[0]);
177 sql_free_result(mdb);
179 Dmsg1(100, "db_get_last_jobid: got JobId=%d\n", jr->JobId);
180 if (jr->JobId <= 0) {
181 Mmsg1(&mdb->errmsg, _("No Job found for: %s\n"), mdb->cmd);
191 * Find Available Media (Volume) for Pool
193 * Find a Volume for a given PoolId, MediaType, and Status.
195 * Returns: 0 on failure
199 db_find_next_volume(void *jcr, B_DB *mdb, int item, MEDIA_DBR *mr)
205 if (item == -1) { /* find oldest volume */
206 /* Find oldest volume */
207 Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,\
208 VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,\
209 VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,Recycle,Slot,\
210 FirstWritten,LastWritten \
211 FROM Media WHERE PoolId=%d AND MediaType='%s' AND VolStatus IN ('Full',\
212 'Recycle','Purged','Used') \
213 ORDER BY FirstWritten", mr->PoolId, mr->MediaType);
216 /* Find next available volume */
217 Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,\
218 VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,\
219 VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,Recycle,Slot,\
220 FirstWritten,LastWritten \
221 FROM Media WHERE PoolId=%d AND MediaType='%s' AND VolStatus='%s' \
222 ORDER BY MediaId", mr->PoolId, mr->MediaType, mr->VolStatus);
224 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
229 numrows = sql_num_rows(mdb);
230 if (item > numrows) {
231 Mmsg2(&mdb->errmsg, _("Request for Volume item %d greater than max %d\n"),
237 /* Seek to desired item
238 * Note, we use base 1; SQL uses base 0
240 sql_data_seek(mdb, item-1);
242 if ((row = sql_fetch_row(mdb)) == NULL) {
243 Mmsg1(&mdb->errmsg, _("No Volume record found for item %d.\n"), item);
244 sql_free_result(mdb);
249 /* Return fields in Media Record */
250 mr->MediaId = str_to_int64(row[0]);
251 bstrncpy(mr->VolumeName, row[1], sizeof(mr->VolumeName));
252 mr->VolJobs = str_to_int64(row[2]);
253 mr->VolFiles = str_to_int64(row[3]);
254 mr->VolBlocks = str_to_int64(row[4]);
255 mr->VolBytes = str_to_uint64(row[5]);
256 mr->VolMounts = str_to_int64(row[6]);
257 mr->VolErrors = str_to_int64(row[7]);
258 mr->VolWrites = str_to_int64(row[8]);
259 mr->MaxVolBytes = str_to_uint64(row[9]);
260 mr->VolCapacityBytes = str_to_uint64(row[10]);
261 mr->VolRetention = str_to_uint64(row[11]);
262 mr->VolUseDuration = str_to_uint64(row[12]);
263 mr->MaxVolJobs = str_to_int64(row[13]);
264 mr->MaxVolFiles = str_to_int64(row[14]);
265 mr->Recycle = str_to_int64(row[15]);
266 mr->Slot = str_to_int64(row[16]);
267 bstrncpy(mr->cFirstWritten, row[17]!=NULL?row[17]:"", sizeof(mr->cFirstWritten));
268 mr->FirstWritten = (time_t)str_to_utime(mr->cFirstWritten);
269 bstrncpy(mr->cLastWritten, row[18]!=NULL?row[18]:"", sizeof(mr->cLastWritten));
270 mr->LastWritten = (time_t)str_to_utime(mr->cLastWritten);
272 sql_free_result(mdb);
279 #endif /* HAVE_MYSQL || HAVE_SQLITE */