]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_get.c
Vacation updates -- see kes06Oct02
[bacula/bacula] / bacula / src / cats / sql_get.c
1 /*
2  * Bacula Catalog Database Get record interface routines
3  *  Note, these routines generally get a record by id or
4  *        by name.  If more logic is involved, the routine
5  *        should be in find.c 
6  *
7  *    Kern Sibbald, March 2000
8  *
9  *    Version $Id$
10  */
11
12 /*
13    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
14
15    This program is free software; you can redistribute it and/or
16    modify it under the terms of the GNU General Public License as
17    published by the Free Software Foundation; either version 2 of
18    the License, or (at your option) any later version.
19
20    This program is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23    General Public License for more details.
24
25    You should have received a copy of the GNU General Public
26    License along with this program; if not, write to the Free
27    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28    MA 02111-1307, USA.
29
30  */
31
32 /* *****FIXME**** fix fixed length of select_cmd[] and insert_cmd[] */
33
34 /* The following is necessary so that we do not include
35  * the dummy external definition of DB.
36  */
37 #define __SQL_C                       /* indicate that this is sql.c */
38
39 #include "bacula.h"
40 #include "cats.h"
41
42 #if    HAVE_MYSQL || HAVE_SQLITE
43
44 /* -----------------------------------------------------------------------
45  *
46  *   Generic Routines (or almost generic)
47  *
48  * -----------------------------------------------------------------------
49  */
50
51 /* Forward referenced functions */
52 static int db_get_file_record(B_DB *mdb, FILE_DBR *fdbr);
53 static int db_get_filename_record(B_DB *mdb, char *fname);
54 static int db_get_path_record(B_DB *mdb, char *path);
55
56
57 /* Imported subroutines */
58 extern void print_result(B_DB *mdb);
59 extern int QueryDB(char *file, int line, B_DB *db, char *select_cmd);
60
61
62 /*
63  * Given a full filename (with path), look up the File record
64  * (with attributes) in the database.
65  *
66  *  Returns: 0 on failure
67  *           1 on success with the File record in FILE_DBR
68  */
69 int db_get_file_attributes_record(B_DB *mdb, char *fname, FILE_DBR *fdbr)
70 {
71    int fnl, pnl;
72    char *l, *p;
73    int stat;
74    char file[MAXSTRING];
75    char spath[MAXSTRING];
76    char buf[MAXSTRING];
77    Dmsg1(20, "Enter get_file_from_catalog fname=%s \n", fname);
78
79    /* Find path without the filename.  
80     * I.e. everything after the last / is a "filename".
81     * OK, maybe it is a directory name, but we treat it like
82     * a filename. If we don't find a / then the whole name
83     * must be a path name (e.g. c:).
84     */
85    for (p=l=fname; *p; p++) {
86       if (*p == '/') {
87          l = p;
88       }
89    }
90    if (*l == '/') {                   /* did we find a slash? */
91       l++;                            /* yes, point to filename */
92    } else {                           /* no, whole thing must be path name */
93       l = p;
94    }
95
96    /* If filename doesn't exist (i.e. root directory), we
97     * simply create a blank name consisting of a single 
98     * space. This makes handling zero length filenames
99     * easier.
100     */
101    fnl = p - l;
102    if (fnl > 255) {
103       Jmsg1(mdb->jcr, M_WARNING, 0, _("Filename truncated to 255 chars: %s\n"), l);
104       fnl = 255;
105    }
106    if (fnl > 0) {
107       strncpy(file, l, fnl);          /* copy filename */
108       file[fnl] = 0;
109    } else {
110       file[0] = ' ';                  /* blank filename */
111       file[1] = 0;
112       fnl = 1;
113    }
114
115    pnl = l - fname;    
116    if (pnl > 255) {
117       Jmsg1(mdb->jcr, M_WARNING, 0, _("Path name truncated to 255 chars: %s\n"), fname);
118       pnl = 255;
119    }
120    strncpy(spath, fname, pnl);
121    spath[pnl] = 0;
122
123    if (pnl == 0) {
124       Mmsg1(&mdb->errmsg, _("Path length is zero. File=%s\n"), fname);
125       Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
126       spath[0] = ' ';
127       spath[1] = 0;
128       pnl = 1;
129    }
130
131    Dmsg1(100, "spath=%s\n", spath);
132    Dmsg1(100, "file=%s\n", file);
133
134    db_escape_string(buf, file, fnl);
135    fdbr->FilenameId = db_get_filename_record(mdb, buf);
136    Dmsg2(100, "db_get_filename_record FilenameId=%u file=%s\n", fdbr->FilenameId, buf);
137
138    db_escape_string(buf, spath, pnl);
139    fdbr->PathId = db_get_path_record(mdb, buf);
140    Dmsg2(100, "db_get_path_record PathId=%u path=%s\n", fdbr->PathId, buf);
141
142    stat = db_get_file_record(mdb, fdbr);
143
144    return stat;
145 }
146
147  
148 /*
149  * Get a File record   
150  * Returns: 0 on failure
151  *          1 on success
152  *
153  *  DO NOT use Jmsg in this routine.
154  *
155  *  Note in this routine, we do not use Jmsg because it may be
156  *    called to get attributes of a non-existent file, which is
157  *    "normal" if a new file is found during Verify.
158  */
159 static
160 int db_get_file_record(B_DB *mdb, FILE_DBR *fdbr)
161 {
162    SQL_ROW row;
163    int stat = 0;
164
165    db_lock(mdb);
166    Mmsg(&mdb->cmd, 
167 "SELECT FileId, LStat, MD5 from File where File.JobId=%u and File.PathId=%u and \
168 File.FilenameId=%u", fdbr->JobId, fdbr->PathId, fdbr->FilenameId);
169
170    Dmsg3(050, "Get_file_record JobId=%u FilenameId=%u PathId=%u\n",
171       fdbr->JobId, fdbr->FilenameId, fdbr->PathId);
172       
173    Dmsg1(100, "Query=%s\n", mdb->cmd);
174
175    if (QUERY_DB(mdb, mdb->cmd)) {
176
177       mdb->num_rows = sql_num_rows(mdb);
178       Dmsg1(050, "get_file_record num_rows=%d\n", (int)mdb->num_rows);
179
180       if (mdb->num_rows > 1) {
181          Mmsg1(&mdb->errmsg, _("get_file_record want 1 got rows=%d\n"),
182             mdb->num_rows);
183       }
184       if (mdb->num_rows >= 1) {
185          if ((row = sql_fetch_row(mdb)) == NULL) {
186             Mmsg1(&mdb->errmsg, _("Error fetching row: %s\n"), sql_strerror(mdb));
187          } else {
188             fdbr->FileId = (FileId_t)strtod(row[0], NULL);
189             strncpy(fdbr->LStat, row[1], sizeof(fdbr->LStat));
190             fdbr->LStat[sizeof(fdbr->LStat)] = 0;
191             strncpy(fdbr->MD5, row[2], sizeof(fdbr->MD5));
192             fdbr->MD5[sizeof(fdbr->MD5)] = 0;
193             stat = 1;
194          }
195       }
196       sql_free_result(mdb);
197    }
198    db_unlock(mdb);
199    return stat;
200
201 }
202
203 /* Get Filename record   
204  * Returns: 0 on failure
205  *          FilenameId on success
206  *
207  *   DO NOT use Jmsg in this routine (see notes for get_file_record)
208  */
209 static int db_get_filename_record(B_DB *mdb, char *fname) 
210 {
211    SQL_ROW row;
212    int FilenameId = 0;
213
214    if (*fname == 0) {
215       Mmsg0(&mdb->errmsg, _("Null name given to db_get_filename_record\n"));
216       Emsg0(M_ABORT, 0, mdb->errmsg);
217    }
218
219    db_lock(mdb);
220    Mmsg(&mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name='%s'", fname);
221    if (QUERY_DB(mdb, mdb->cmd)) {
222
223       mdb->num_rows = sql_num_rows(mdb);
224
225       if (mdb->num_rows > 1) {
226          Mmsg1(&mdb->errmsg, _("More than one Filename!: %d\n"), (int)(mdb->num_rows));
227       }
228       if (mdb->num_rows >= 1) {
229          if ((row = sql_fetch_row(mdb)) == NULL) {
230             Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
231          } else {
232             FilenameId = atoi(row[0]);
233             if (FilenameId <= 0) {
234                Mmsg2(&mdb->errmsg, _("Get DB Filename record %s found bad record: %d\n"),
235                   mdb->cmd, FilenameId); 
236                FilenameId = 0;
237             }
238          }
239       }
240       sql_free_result(mdb);
241    }
242    db_unlock(mdb);
243    return FilenameId;
244 }
245
246 /* Get path record   
247  * Returns: 0 on failure
248  *          PathId on success
249  *
250  *   DO NOT use Jmsg in this routine (see notes for get_file_record)
251  */
252 static int db_get_path_record(B_DB *mdb, char *path)
253 {
254    SQL_ROW row;
255    uint32_t PathId = 0;
256
257    if (*path == 0) {
258       Emsg0(M_ABORT, 0, _("Null path given to db_get_path_record\n"));
259    }
260
261    db_lock(mdb);
262
263    if (mdb->cached_path_id != 0 && strcmp(mdb->cached_path, path) == 0) {
264       db_unlock(mdb);
265       return mdb->cached_path_id;
266    }          
267
268    Mmsg(&mdb->cmd, "SELECT PathId FROM Path WHERE Path='%s'", path);
269
270    if (QUERY_DB(mdb, mdb->cmd)) {
271       char ed1[30];
272       mdb->num_rows = sql_num_rows(mdb);
273
274       if (mdb->num_rows > 1) {
275          Mmsg1(&mdb->errmsg, _("More than one Path!: %s\n"), 
276             edit_uint64(mdb->num_rows, ed1));
277          Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
278       } else if (mdb->num_rows == 1) {
279          if ((row = sql_fetch_row(mdb)) == NULL) {
280             Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
281          } else {
282             PathId = atoi(row[0]);
283             if (PathId <= 0) {
284                Mmsg2(&mdb->errmsg, _("Get DB path record %s found bad record: %u\n"),
285                   mdb->cmd, PathId); 
286                PathId = 0;
287             } else {
288                /* Cache path */
289                if (PathId != mdb->cached_path_id) {
290                   mdb->cached_path_id = PathId;
291                   mdb->cached_path = check_pool_memory_size(mdb->cached_path,
292                      strlen(path)+1);
293                   strcpy(mdb->cached_path, path);
294                }
295             }
296          }
297       }
298       sql_free_result(mdb);
299    }
300    db_unlock(mdb);
301    return PathId;
302 }
303
304
305 /* 
306  * Get Job record for given JobId or Job name
307  * Returns: 0 on failure
308  *          1 on success
309  */
310 int db_get_job_record(B_DB *mdb, JOB_DBR *jr)
311 {
312    SQL_ROW row;
313
314    db_lock(mdb);
315    if (jr->JobId == 0) {
316       Mmsg(&mdb->cmd, "SELECT VolSessionId,VolSessionTime,\
317 PoolId,StartTime,EndTime,JobFiles,JobBytes,JobTDate,Job,JobStatus,\
318 Type,Level \
319 FROM Job WHERE Job='%s'", jr->Job);
320     } else {
321       Mmsg(&mdb->cmd, "SELECT VolSessionId,VolSessionTime,\
322 PoolId,StartTime,EndTime,JobFiles,JobBytes,JobTDate,Job,JobStatus,\
323 Type,Level \
324 FROM Job WHERE JobId=%u", jr->JobId);
325     }
326
327    if (!QUERY_DB(mdb, mdb->cmd)) {
328       db_unlock(mdb);
329       return 0;                       /* failed */
330    }
331    if ((row = sql_fetch_row(mdb)) == NULL) {
332       Mmsg1(&mdb->errmsg, _("No Job found for JobId %u\n"), jr->JobId);
333       sql_free_result(mdb);
334       db_unlock(mdb);
335       return 0;                       /* failed */
336    }
337
338    jr->VolSessionId = atol(row[0]);
339    jr->VolSessionTime = atol(row[1]);
340    jr->PoolId = atoi(row[2]);
341    strcpy(jr->cStartTime, row[3]);
342    strcpy(jr->cEndTime, row[4]);
343    jr->JobFiles = atol(row[5]);
344    jr->JobBytes = (uint64_t)strtod(row[6], NULL);
345    jr->JobTDate = (btime_t)strtod(row[7], NULL);
346    strcpy(jr->Job, row[8]);
347    jr->JobStatus = (int)*row[9];
348    jr->Type = (int)*row[10];
349    jr->Level = (int)*row[11];
350    sql_free_result(mdb);
351
352    db_unlock(mdb);
353    return 1;
354 }
355
356 /*
357  * Find VolumeNames for a give JobId
358  *  Returns: 0 on error or no Volumes found
359  *           number of volumes on success
360  *              Volumes are concatenated in VolumeNames
361  *              separated by a vertical bar (|).
362  *
363  *  Returns: number of volumes on success
364  */
365 int db_get_job_volume_names(B_DB *mdb, uint32_t JobId, POOLMEM **VolumeNames)
366 {
367    SQL_ROW row;
368    int stat = 0;
369    int i;
370
371    db_lock(mdb);
372    Mmsg(&mdb->cmd, 
373 "SELECT VolumeName FROM JobMedia,Media WHERE JobMedia.JobId=%u \
374 AND JobMedia.MediaId=Media.MediaId", JobId);
375
376    Dmsg1(130, "VolNam=%s\n", mdb->cmd);
377    *VolumeNames[0] = 0;
378    if (QUERY_DB(mdb, mdb->cmd)) {
379       mdb->num_rows = sql_num_rows(mdb);
380       Dmsg1(130, "Num rows=%d\n", mdb->num_rows);
381       if (mdb->num_rows <= 0) {
382          Mmsg1(&mdb->errmsg, _("No volumes found for JobId=%d\n"), JobId);
383          stat = 0;
384       } else {
385          stat = mdb->num_rows;
386          for (i=0; i < stat; i++) {
387             if ((row = sql_fetch_row(mdb)) == NULL) {
388                Mmsg2(&mdb->errmsg, _("Error fetching row %d: ERR=%s\n"), i, sql_strerror(mdb));
389                Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
390                stat = 0;
391                break;
392             } else {
393                if (*VolumeNames[0] != 0) {
394                   pm_strcat(VolumeNames, "|");
395                }
396                pm_strcat(VolumeNames, row[0]);
397             }
398          }
399       }
400       sql_free_result(mdb);
401    }
402    db_unlock(mdb);
403    return stat;
404 }
405
406
407 /* 
408  * Get the number of pool records
409  *
410  * Returns: -1 on failure
411  *          number on success
412  */
413 int db_get_num_pool_records(B_DB *mdb)
414 {
415    int stat = 0;
416
417    db_lock(mdb);
418    Mmsg(&mdb->cmd, "SELECT count(*) from Pool");
419    stat = get_sql_record_max(mdb);
420    db_unlock(mdb);
421    return stat;
422 }
423
424 /*
425  * This function returns a list of all the Pool record ids.
426  *  The caller must free ids if non-NULL.
427  *
428  *  Returns 0: on failure
429  *          1: on success
430  */
431 int db_get_pool_ids(B_DB *mdb, int *num_ids, uint32_t *ids[])
432 {
433    SQL_ROW row;
434    int stat = 0;
435    int i = 0;
436    uint32_t *id;
437
438    db_lock(mdb);
439    *ids = NULL;
440    Mmsg(&mdb->cmd, "SELECT PoolId FROM Pool");
441    if (QUERY_DB(mdb, mdb->cmd)) {
442       *num_ids = sql_num_rows(mdb);
443       if (*num_ids > 0) {
444          id = (uint32_t *)malloc(*num_ids * sizeof(uint32_t));
445          while ((row = sql_fetch_row(mdb)) != NULL) {
446             id[i++] = (uint32_t)atoi(row[0]);
447          }
448          *ids = id;
449       }
450       sql_free_result(mdb);
451       stat = 1;
452    } else {
453       Mmsg(&mdb->errmsg, _("Pool id select failed: ERR=%s\n"), sql_strerror(mdb));
454       Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
455       stat = 0;
456    }
457    db_unlock(mdb);
458    return stat;
459 }
460
461
462 /* Get Pool Record   
463  * If the PoolId is non-zero, we get its record,
464  *  otherwise, we search on the PoolName
465  *
466  * Returns: 0 on failure
467  *          id on success 
468  */
469 int db_get_pool_record(B_DB *mdb, POOL_DBR *pdbr)
470 {
471    SQL_ROW row;
472    int stat = 0;
473
474    db_lock(mdb);
475    if (pdbr->PoolId != 0) {               /* find by id */
476       Mmsg(&mdb->cmd, 
477 "SELECT PoolId, Name, NumVols, MaxVols, UseOnce, UseCatalog, AcceptAnyVolume, \
478 AutoPrune, Recycle, VolRetention, \
479 PoolType, LabelFormat FROM Pool WHERE Pool.PoolId=%d", pdbr->PoolId);
480    } else {                           /* find by name */
481       Mmsg(&mdb->cmd, 
482 "SELECT PoolId, Name, NumVols, MaxVols, UseOnce, UseCatalog, AcceptAnyVolume, \
483 AutoPrune, Recycle, VolRetention, \
484 PoolType, LabelFormat FROM Pool WHERE Pool.Name='%s'", pdbr->Name);
485    }  
486
487    if (QUERY_DB(mdb, mdb->cmd)) {
488       mdb->num_rows = sql_num_rows(mdb);
489       if (mdb->num_rows > 1) {
490          char ed1[30];
491          Mmsg1(&mdb->errmsg, _("More than one Pool!: %s\n"), 
492             edit_uint64(mdb->num_rows, ed1));
493          Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
494       } else if (mdb->num_rows == 1) {
495          if ((row = sql_fetch_row(mdb)) == NULL) {
496             Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
497             Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
498          } else {
499             pdbr->PoolId = atoi(row[0]);
500             strcpy(pdbr->Name, row[1]);
501             pdbr->NumVols = atoi(row[2]);
502             pdbr->MaxVols = atoi(row[3]);
503             pdbr->UseOnce = atoi(row[4]);
504             pdbr->UseCatalog = atoi(row[5]);
505             pdbr->AcceptAnyVolume = atoi(row[6]);
506             pdbr->AutoPrune = atoi(row[7]);
507             pdbr->Recycle = atoi(row[8]);
508             pdbr->VolRetention = (btime_t)strtod(row[9], NULL);
509             strcpy(pdbr->PoolType, row[10]);
510             if (row[11]) {
511                strcpy(pdbr->LabelFormat, row[11]);
512             } else {
513                pdbr->LabelFormat[0] = 0;
514             }
515             stat = pdbr->PoolId;
516          }
517       }
518       sql_free_result(mdb);
519    }
520    db_unlock(mdb);
521    return stat;
522 }
523
524 /* Get FileSet Record   
525  * If the FileSetId is non-zero, we get its record,
526  *  otherwise, we search on the name
527  *
528  * Returns: 0 on failure
529  *          id on success 
530  */
531 int db_get_fileset_record(B_DB *mdb, FILESET_DBR *fsr)
532 {
533    SQL_ROW row;
534    int stat = 0;
535
536    db_lock(mdb);
537    if (fsr->FileSetId != 0) {               /* find by id */
538       Mmsg(&mdb->cmd, 
539            "SELECT FileSetId, FileSet, MD5 FROM FileSet "
540            "WHERE FileSetId=%u", fsr->FileSetId);
541    } else {                           /* find by name */
542       Mmsg(&mdb->cmd, 
543            "SELECT FileSetId, FileSet, MD5 FROM FileSet "
544            "WHERE FileSet='%s'", fsr->FileSet);
545    }  
546
547    if (QUERY_DB(mdb, mdb->cmd)) {
548       mdb->num_rows = sql_num_rows(mdb);
549       if (mdb->num_rows > 1) {
550          char ed1[30];
551          Mmsg1(&mdb->errmsg, _("Got %s FileSets expected only one!\n"), 
552             edit_uint64(mdb->num_rows, ed1));
553          sql_data_seek(mdb, mdb->num_rows-1);
554       }
555       if ((row = sql_fetch_row(mdb)) == NULL) {
556          Mmsg1(&mdb->errmsg, _("Error fetching row get_fileset: %s\n"), sql_strerror(mdb));
557          Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
558       } else {
559          fsr->FileSetId = atoi(row[0]);
560          strcpy(fsr->FileSet, row[1]);
561          strcpy(fsr->MD5, row[2]);
562          stat = fsr->FileSetId;
563       }
564       sql_free_result(mdb);
565    }
566    db_unlock(mdb);
567    return stat;
568 }
569
570
571 /* 
572  * Get the number of Media records
573  *
574  * Returns: -1 on failure
575  *          number on success
576  */
577 int db_get_num_media_records(B_DB *mdb)
578 {
579    int stat = 0;
580
581    db_lock(mdb);
582    Mmsg(&mdb->cmd, "SELECT count(*) from Media");
583    stat = get_sql_record_max(mdb);
584    db_unlock(mdb);
585    return stat;
586 }
587
588
589 /*
590  * This function returns a list of all the Media record ids.
591  *  The caller must free ids if non-NULL.
592  *
593  *  Returns 0: on failure
594  *          1: on success
595  */
596 int db_get_media_ids(B_DB *mdb, int *num_ids, uint32_t *ids[])
597 {
598    SQL_ROW row;
599    int stat = 0;
600    int i = 0;
601    uint32_t *id;
602
603    db_lock(mdb);
604    *ids = NULL;
605    Mmsg(&mdb->cmd, "SELECT MediaId FROM Media");
606    if (QUERY_DB(mdb, mdb->cmd)) {
607       *num_ids = sql_num_rows(mdb);
608       if (*num_ids > 0) {
609          id = (uint32_t *)malloc(*num_ids * sizeof(uint32_t));
610          while ((row = sql_fetch_row(mdb)) != NULL) {
611             id[i++] = (uint32_t)atoi(row[0]);
612          }
613          *ids = id;
614       }
615       sql_free_result(mdb);
616       stat = 1;
617    } else {
618       Mmsg(&mdb->errmsg, _("Media id select failed: ERR=%s\n"), sql_strerror(mdb));
619       Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
620       stat = 0;
621    }
622    db_unlock(mdb);
623    return stat;
624 }
625
626
627 /* Get Media Record   
628  *
629  * Returns: 0 on failure
630  *          id on success 
631  */
632 int db_get_media_record(B_DB *mdb, MEDIA_DBR *mr)
633 {
634    SQL_ROW row;
635    int stat = 0;
636
637    db_lock(mdb);
638    if (mr->MediaId == 0 && mr->VolumeName[0] == 0) {
639       Mmsg(&mdb->cmd, "SELECT count(*) from Media");
640       mr->MediaId = get_sql_record_max(mdb);
641       db_unlock(mdb);
642       return 1;
643    }
644    if (mr->MediaId != 0) {               /* find by id */
645       Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,\
646 VolBytes,VolMounts,VolErrors,VolWrites,VolMaxBytes,VolCapacityBytes,\
647 MediaType,VolStatus,PoolId,VolRetention,Recycle,Slot \
648 FROM Media WHERE MediaId=%d", mr->MediaId);
649    } else {                           /* find by name */
650       Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,\
651 VolBytes,VolMounts,VolErrors,VolWrites,VolMaxBytes,VolCapacityBytes,\
652 MediaType,VolStatus,PoolId,VolRetention,Recycle,Slot \
653 FROM Media WHERE VolumeName='%s'", mr->VolumeName);
654    }  
655
656    if (QUERY_DB(mdb, mdb->cmd)) {
657       mdb->num_rows = sql_num_rows(mdb);
658       if (mdb->num_rows > 1) {
659          char ed1[30];
660          Mmsg1(&mdb->errmsg, _("More than one Volume!: %s\n"), 
661             edit_uint64(mdb->num_rows, ed1));
662          Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
663       } else if (mdb->num_rows == 1) {
664          if ((row = sql_fetch_row(mdb)) == NULL) {
665             Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
666             Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
667          } else {
668             /* return values */
669             mr->MediaId = atoi(row[0]);
670             strcpy(mr->VolumeName, row[1]);
671             mr->VolJobs = atoi(row[2]);
672             mr->VolFiles = atoi(row[3]);
673             mr->VolBlocks = atoi(row[4]);
674             mr->VolBytes = (uint64_t)strtod(row[5], NULL);
675             mr->VolMounts = atoi(row[6]);
676             mr->VolErrors = atoi(row[7]);
677             mr->VolWrites = atoi(row[8]);
678             mr->VolMaxBytes = (uint64_t)strtod(row[9], NULL);
679             mr->VolCapacityBytes = (uint64_t)strtod(row[10], NULL);
680             strcpy(mr->MediaType, row[11]);
681             strcpy(mr->VolStatus, row[12]);
682             mr->PoolId = atoi(row[13]);
683             mr->VolRetention = (btime_t)strtod(row[14], NULL);
684             mr->Recycle = atoi(row[15]);
685             mr->Slot = atoi(row[16]);
686             stat = mr->MediaId;
687          }
688       } else {
689          Mmsg0(&mdb->errmsg, _("Media record not found.\n"));
690       }
691       sql_free_result(mdb);
692    }
693    db_unlock(mdb);
694    return stat;
695 }
696
697
698 #endif /* HAVE_MYSQL || HAVE_SQLITE */