]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_find.c
b0dc4277f4bab372ec96229f590e665e807a17b9
[bacula/bacula] / bacula / src / cats / sql_find.c
1 /*
2  * Bacula Catalog Database Find record interface routines
3  *
4  *  Note, generally, these routines are more complicated
5  *        that a simple search by name or id. Such simple
6  *        request are in get.c
7  * 
8  *    Kern Sibbald, December 2000
9  *
10  *    Version $Id$
11  */
12
13 /*
14    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
15
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.
20
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.
25
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,
29    MA 02111-1307, USA.
30
31  */
32
33 /* *****FIXME**** fix fixed length of select_cmd[] and insert_cmd[] */
34
35 /* The following is necessary so that we do not include
36  * the dummy external definition of DB.
37  */
38 #define __SQL_C                       /* indicate that this is sql.c */
39
40 #include "bacula.h"
41 #include "cats.h"
42
43 #if    HAVE_MYSQL || HAVE_SQLITE
44
45 /* -----------------------------------------------------------------------
46  *
47  *   Generic Routines (or almost generic)
48  *
49  * -----------------------------------------------------------------------
50  */
51
52 /* Imported subroutines */
53 extern void print_result(B_DB *mdb);
54 extern int QueryDB(char *file, int line, B_DB *db, char *select_cmd);
55
56
57 /* Find job start time. Used to find last full save
58  * for Incremental and Differential saves.
59  *      
60  * Returns: 0 on failure
61  *          1 on success, jr is unchanged, but stime is set
62  */
63 int
64 db_find_job_start_time(B_DB *mdb, JOB_DBR *jr, POOLMEM **stime)
65 {
66    SQL_ROW row;
67    int JobId;
68
69    pm_strcpy(stime, "0000-00-00 00:00:00");   /* default */
70    db_lock(mdb);
71    /* If no Id given, we must find corresponding job */
72    if (jr->JobId == 0) {
73       /* Differential is since last Full backup */
74       if (jr->Level == L_DIFFERENTIAL) {
75          Mmsg(&mdb->cmd, 
76 "SELECT JobId from Job WHERE JobStatus='T' and Type='%c' and \
77 Level='%c' and Name='%s' and ClientId=%d and FileSetId=%d \
78 ORDER by StartTime DESC LIMIT 1",
79            jr->Type, L_FULL, jr->Name, jr->ClientId, jr->FileSetId);
80       /* Incremental is since last Full, Incremental, or Differential */
81       } else if (jr->Level == L_INCREMENTAL) {
82          Mmsg(&mdb->cmd, 
83 "SELECT JobId from Job WHERE JobStatus='T' and Type='%c' and \
84 (Level='%c' or Level='%c' or Level='%c') and Name='%s' and ClientId=%d \
85 ORDER by StartTime DESC LIMIT 1",
86            jr->Type, L_INCREMENTAL, L_DIFFERENTIAL, L_FULL, jr->Name,
87            jr->ClientId);
88       } else {
89          Mmsg1(&mdb->errmsg, _("Unknown level=%d\n"), jr->Level);
90          db_unlock(mdb);
91          return 0;
92       }
93       Dmsg1(100, "Submitting: %s\n", mdb->cmd);
94       if (!QUERY_DB(mdb, mdb->cmd)) {
95          Mmsg1(&mdb->errmsg, _("Query error for start time request: %s\n"), mdb->cmd);
96          db_unlock(mdb);
97          return 0;
98       }
99       if ((row = sql_fetch_row(mdb)) == NULL) {
100          sql_free_result(mdb);
101          db_unlock(mdb);
102          return 0;
103       }
104       JobId = atoi(row[0]);
105       sql_free_result(mdb);
106    } else {
107       JobId = jr->JobId;              /* search for particular id */
108    }
109
110    Dmsg1(100, "Submitting: %s\n", mdb->cmd);
111    Mmsg(&mdb->cmd, "SELECT StartTime from Job WHERE Job.JobId=%d", JobId);
112
113    if (!QUERY_DB(mdb, mdb->cmd)) {
114       Mmsg1(&mdb->errmsg, _("Query error for start time request: %s\n"), mdb->cmd);
115       db_unlock(mdb);
116       return 0;
117    }
118
119    if ((row = sql_fetch_row(mdb)) == NULL) {
120       pm_strcpy(stime, "");                   /* set EOS */
121       Mmsg2(&mdb->errmsg, _("No Job found for JobId=%d: %s\n"), JobId, sql_strerror(mdb));
122       sql_free_result(mdb);
123       db_unlock(mdb);
124       return 0;
125    }
126    Dmsg1(100, "Got start time: %s\n", row[0]);
127    pm_strcpy(stime, row[0]);
128
129    sql_free_result(mdb);
130
131    db_unlock(mdb);
132    return 1;
133 }
134
135 /* 
136  * Find JobId of last job that ran.  E.g. for
137  *   VERIFY_CATALOG we want the JobId of the last INIT.
138  *   For VERIFY_VOLUME_TO_CATALOG, we want the JobId of the last Job.
139  *
140  * Returns: 1 on success
141  *          0 on failure
142  */
143 int
144 db_find_last_jobid(B_DB *mdb, JOB_DBR *jr)
145 {
146    SQL_ROW row;
147
148    /* Find last full */
149    db_lock(mdb);
150    if (jr->Level == L_VERIFY_CATALOG) {
151       Mmsg(&mdb->cmd, 
152 "SELECT JobId FROM Job WHERE Type='%c' AND Level='%c' AND Name='%s' AND \
153 ClientId=%d ORDER BY StartTime DESC LIMIT 1",
154            JT_VERIFY, L_VERIFY_INIT, jr->Name, jr->ClientId);
155    } else if (jr->Level == L_VERIFY_VOLUME_TO_CATALOG) {
156       Mmsg(&mdb->cmd, 
157 "SELECT JobId FROM Job WHERE Type='%c' AND \
158 ClientId=%d ORDER BY StartTime DESC LIMIT 1",
159            JT_BACKUP, jr->ClientId);
160    } else {
161       Mmsg1(&mdb->errmsg, _("Unknown Job level=%c\n"), jr->Level);
162       db_unlock(mdb);
163       return 0;
164    }
165
166    if (!QUERY_DB(mdb, mdb->cmd)) {
167       db_unlock(mdb);
168       return 0;
169    }
170    if ((row = sql_fetch_row(mdb)) == NULL) {
171       Mmsg1(&mdb->errmsg, _("No Job found for: %s.\n"), mdb->cmd);
172       sql_free_result(mdb);
173       db_unlock(mdb);
174       return 0;
175    }
176
177    jr->JobId = atoi(row[0]);
178    sql_free_result(mdb);
179
180    Dmsg1(100, "db_get_last_jobid: got JobId=%d\n", jr->JobId);
181    if (jr->JobId <= 0) {
182       Mmsg1(&mdb->errmsg, _("No Job found for: %s\n"), mdb->cmd);
183       db_unlock(mdb);
184       return 0;
185    }
186
187    db_unlock(mdb);
188    return 1;
189 }
190
191 /* 
192  * Find Available Media (Volume) for Pool
193  *
194  * Find a Volume for a given PoolId, MediaType, and Status.
195  *
196  * Returns: 0 on failure
197  *          numrows on success
198  */
199 int
200 db_find_next_volume(B_DB *mdb, int item, MEDIA_DBR *mr) 
201 {
202    SQL_ROW row;
203    int numrows;
204
205    db_lock(mdb);
206    Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,\
207 VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,\
208 VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,Recycle,Slot,\
209 FirstWritten FROM Media WHERE PoolId=%d AND MediaType='%s' AND VolStatus='%s' \
210 ORDER BY MediaId", mr->PoolId, mr->MediaType, mr->VolStatus); 
211
212    if (!QUERY_DB(mdb, mdb->cmd)) {
213       db_unlock(mdb);
214       return 0;
215    }
216
217    numrows = sql_num_rows(mdb);
218    if (item > numrows) {
219       Mmsg2(&mdb->errmsg, _("Request for Volume item %d greater than max %d\n"),
220          item, numrows);
221       db_unlock(mdb);
222       return 0;
223    }
224    
225    /* Seek to desired item 
226     * Note, we use base 1; SQL uses base 0
227     */
228    sql_data_seek(mdb, item-1);
229
230    if ((row = sql_fetch_row(mdb)) == NULL) {
231       Mmsg1(&mdb->errmsg, _("No Volume record found for item %d.\n"), item);
232       sql_free_result(mdb);
233       db_unlock(mdb);
234       return 0;
235    }
236
237    /* Return fields in Media Record */
238    mr->MediaId = str_to_int64(row[0]);
239    bstrncpy(mr->VolumeName, row[1], sizeof(mr->VolumeName));
240    mr->VolJobs = str_to_int64(row[2]);
241    mr->VolFiles = str_to_int64(row[3]);
242    mr->VolBlocks = str_to_int64(row[4]);
243    mr->VolBytes = str_to_uint64(row[5]);
244    mr->VolMounts = str_to_int64(row[6]);
245    mr->VolErrors = str_to_int64(row[7]);
246    mr->VolWrites = str_to_int64(row[8]);
247    mr->MaxVolBytes = str_to_uint64(row[9]);
248    mr->VolCapacityBytes = str_to_uint64(row[10]);
249    mr->VolRetention = str_to_uint64(row[11]);
250    mr->VolUseDuration = str_to_uint64(row[12]);
251    mr->MaxVolJobs = str_to_int64(row[13]);
252    mr->MaxVolFiles = str_to_int64(row[14]);
253    mr->Recycle = str_to_int64(row[15]);
254    mr->Slot = str_to_int64(row[16]);
255    bstrncpy(mr->cFirstWritten, row[17]!=NULL?row[17]:"", sizeof(mr->cFirstWritten));
256
257    sql_free_result(mdb);
258
259    db_unlock(mdb);
260    return numrows;
261 }
262
263
264 #endif /* HAVE_MYSQL || HAVE_SQLITE */