]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_find.c
d49efab49af48f533586e3993f09be9811d185cf
[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
11 /*
12    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
13
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.
18
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.
23
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,
27    MA 02111-1307, USA.
28
29  */
30
31 /* *****FIXME**** fix fixed length of select_cmd[] and insert_cmd[] */
32
33 /* The following is necessary so that we do not include
34  * the dummy external definition of DB.
35  */
36 #define __SQL_C                       /* indicate that this is sql.c */
37
38 #include "bacula.h"
39 #include "cats.h"
40
41 #if    HAVE_MYSQL || HAVE_SQLITE
42
43 /* -----------------------------------------------------------------------
44  *
45  *   Generic Routines (or almost generic)
46  *
47  * -----------------------------------------------------------------------
48  */
49
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);
53
54
55 /* Find job start time. Used to find last full save
56  * for Incremental and Differential saves.
57  *      
58  * Returns: 0 on failure
59  *          1 on success, jr is unchanged, but stime is set
60  */
61 int
62 db_find_job_start_time(B_DB *mdb, JOB_DBR *jr, char *stime)
63 {
64    SQL_ROW row;
65    int JobId;
66
67    strcpy(stime, "0000-00-00 00:00:00");   /* default */
68    P(mdb->mutex);
69    /* If no Id given, we must find corresponding job */
70    if (jr->JobId == 0) {
71       /* Differential is since last Full backup */
72       if (jr->Level == L_DIFFERENTIAL) {
73          Mmsg(&mdb->cmd, 
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) {
80          Mmsg(&mdb->cmd, 
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,
85            jr->ClientId);
86       } else {
87          Mmsg1(&mdb->errmsg, _("Unknown level=%d\n"), jr->Level);
88          Emsg0(M_ERROR, 0, mdb->errmsg);
89          V(mdb->mutex);
90          return 0;
91       }
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);
95          V(mdb->mutex);
96          return 0;
97       }
98       if ((row = sql_fetch_row(mdb)) == NULL) {
99          sql_free_result(mdb);
100          V(mdb->mutex);
101          return 0;
102       }
103       JobId = atoi(row[0]);
104       sql_free_result(mdb);
105    } else {
106       JobId = jr->JobId;              /* search for particular id */
107    }
108
109    Dmsg1(100, "Submitting: %s\n", mdb->cmd);
110    Mmsg(&mdb->cmd, "SELECT StartTime from Job WHERE Job.JobId=%d", JobId);
111
112    if (!QUERY_DB(mdb, mdb->cmd)) {
113       Emsg1(M_ERROR, 0, _("Query error for start time request: %s\n"), mdb->cmd);
114       V(mdb->mutex);
115       return 0;
116    }
117
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);
122       V(mdb->mutex);
123       return 0;
124    }
125    Dmsg1(100, "Got start time: %s\n", row[0]);
126    strcpy(stime, row[0]);
127
128    sql_free_result(mdb);
129
130    V(mdb->mutex);
131    return 1;
132 }
133
134 /* 
135  * Find JobId of last full verify 
136  * Returns: 1 on success
137  *          0 on failure
138  */
139 int
140 db_find_last_full_verify(B_DB *mdb, JOB_DBR *jr)
141 {
142    SQL_ROW row;
143
144    /* Find last full */
145    P(mdb->mutex);
146    if (jr->Level != L_VERIFY_CATALOG) {
147       Emsg2(M_FATAL, 0, _("Expecting Level=%c, got %c\n"), L_VERIFY_CATALOG, jr->Level);
148       return 0;
149    }
150    Mmsg(&mdb->cmd, 
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
155    if (!QUERY_DB(mdb, mdb->cmd)) {
156       V(mdb->mutex);
157       return 0;
158    }
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);
162       V(mdb->mutex);
163       return 0;
164    }
165
166    jr->JobId = atoi(row[0]);
167    sql_free_result(mdb);
168
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);
172       V(mdb->mutex);
173       return 0;
174    }
175
176    V(mdb->mutex);
177    return 1;
178 }
179
180 /* 
181  * Find Available Media (Volume) for Pool
182  *
183  * Find a Volume for a given PoolId, MediaType, and Status.
184  *
185  * Returns: 0 on failure
186  *          numrows on success
187  */
188 int
189 db_find_next_volume(B_DB *mdb, int item, MEDIA_DBR *mr) 
190 {
191    SQL_ROW row;
192    int numrows;
193
194    P(mdb->mutex);
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); 
199
200    if (!QUERY_DB(mdb, mdb->cmd)) {
201       V(mdb->mutex);
202       return 0;
203    }
204
205    numrows = sql_num_rows(mdb);
206    if (item > numrows) {
207       V(mdb->mutex);
208       return 0;
209    }
210    
211    /* Seek to desired item 
212     * Note, we use base 1; SQL uses base 0
213     */
214    sql_data_seek(mdb, item-1);
215
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);
219       V(mdb->mutex);
220       return 0;
221    }
222
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);
235
236    sql_free_result(mdb);
237
238    V(mdb->mutex);
239    return numrows;
240 }
241
242
243 #endif /* HAVE_MYSQL || HAVE_SQLITE */