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
12 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of
17 the License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public
25 License along with this program; if not, write to the Free
26 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31 /* *****FIXME**** fix fixed length of select_cmd[] and insert_cmd[] */
33 /* The following is necessary so that we do not include
34 * the dummy external definition of DB.
36 #define __SQL_C /* indicate that this is sql.c */
41 #if HAVE_MYSQL || HAVE_SQLITE
43 /* -----------------------------------------------------------------------
45 * Generic Routines (or almost generic)
47 * -----------------------------------------------------------------------
50 /* Imported subroutines */
51 extern void print_result(B_DB *mdb);
52 extern int QueryDB(char *file, int line, B_DB *db, char *select_cmd);
55 /* Find job start time. Used to find last full save
56 * for Incremental and Differential saves.
58 * Returns: 0 on failure
59 * 1 on success, jr is unchanged, but stime is set
62 db_find_job_start_time(B_DB *mdb, JOB_DBR *jr, char *stime)
67 strcpy(stime, "0000-00-00 00:00:00"); /* default */
69 /* If no Id given, we must find corresponding job */
71 /* Differential is since last Full backup */
72 if (jr->Level == L_DIFFERENTIAL) {
74 "SELECT JobId from Job WHERE JobStatus='T' and Type='%c' and \
75 Level='%c' and Name=\"%s\" and ClientId=%d and FileSetId=%d \
76 ORDER by StartTime DESC LIMIT 1",
77 jr->Type, L_FULL, jr->Name, jr->ClientId, jr->FileSetId);
78 /* Incremental is since last Full, Incremental, or Differential */
79 } else if (jr->Level == L_INCREMENTAL) {
81 "SELECT JobId from Job WHERE JobStatus='T' and Type='%c' and \
82 (Level='%c' or Level='%c' or Level='%c') and Name=\"%s\" and ClientId=%d \
83 ORDER by StartTime DESC LIMIT 1",
84 jr->Type, L_INCREMENTAL, L_DIFFERENTIAL, L_FULL, jr->Name,
87 Mmsg1(&mdb->errmsg, _("Unknown level=%d\n"), jr->Level);
88 Emsg0(M_ERROR, 0, mdb->errmsg);
92 Dmsg1(100, "Submitting: %s\n", mdb->cmd);
93 if (!QUERY_DB(mdb, mdb->cmd)) {
94 Emsg1(M_ERROR, 0, _("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(mdb, mdb->cmd)) {
113 Emsg1(M_ERROR, 0, _("Query error for start time request: %s\n"), mdb->cmd);
118 if ((row = sql_fetch_row(mdb)) == NULL) {
119 *stime = 0; /* set EOS */
120 Emsg2(M_ERROR, 0, _("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 strcpy(stime, row[0]);
128 sql_free_result(mdb);
135 * Find JobId of last full verify
136 * Returns: 1 on success
140 db_find_last_full_verify(B_DB *mdb, JOB_DBR *jr)
146 if (jr->Level != L_VERIFY_CATALOG) {
147 Emsg2(M_FATAL, 0, _("Expecting Level=%c, got %c\n"), L_VERIFY_CATALOG, jr->Level);
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);
155 if (!QUERY_DB(mdb, mdb->cmd)) {
159 if ((row = sql_fetch_row(mdb)) == NULL) {
160 Emsg0(M_FATAL, 0, _("No Job found for last full verify.\n"));
161 sql_free_result(mdb);
166 jr->JobId = atoi(row[0]);
167 sql_free_result(mdb);
169 Dmsg1(100, "db_get_last_full_verify: got JobId=%d\n", jr->JobId);
170 if (jr->JobId <= 0) {
171 Emsg1(M_FATAL, 0, _("No Verify Job found for: %s\n"), mdb->cmd);
181 * Find Available Media (Volume) for Pool
183 * Find a Volume for a given PoolId, MediaType, and Status.
185 * Returns: 0 on failure
189 db_find_next_volume(B_DB *mdb, int item, MEDIA_DBR *mr)
195 Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,\
196 VolBytes,VolMounts,VolErrors,VolWrites,VolMaxBytes,VolCapacityBytes \
197 FROM Media WHERE PoolId=%d AND MediaType=\"%s\" AND VolStatus=\"%s\" \
198 ORDER BY MediaId", mr->PoolId, mr->MediaType, mr->VolStatus);
200 if (!QUERY_DB(mdb, mdb->cmd)) {
205 numrows = sql_num_rows(mdb);
206 if (item > numrows) {
211 /* Seek to desired item
212 * Note, we use base 1; SQL uses base 0
214 sql_data_seek(mdb, item-1);
216 if ((row = sql_fetch_row(mdb)) == NULL) {
217 Emsg1(M_ERROR, 0, _("No media record found for item %d.\n"), item);
218 sql_free_result(mdb);
223 /* Return fields in Media Record */
224 mr->MediaId = atoi(row[0]);
225 strcpy(mr->VolumeName, row[1]);
226 mr->VolJobs = atoi(row[2]);
227 mr->VolFiles = atoi(row[3]);
228 mr->VolBlocks = atoi(row[4]);
229 mr->VolBytes = (uint64_t)strtod(row[5], NULL);
230 mr->VolMounts = atoi(row[6]);
231 mr->VolErrors = atoi(row[7]);
232 mr->VolWrites = atoi(row[8]);
233 mr->VolMaxBytes = (uint64_t)strtod(row[9], NULL);
234 mr->VolCapacityBytes = (uint64_t)strtod(row[10], NULL);
236 sql_free_result(mdb);
243 #endif /* HAVE_MYSQL || HAVE_SQLITE */