]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_delete.c
1.19 24Apr02
[bacula/bacula] / bacula / src / cats / sql_delete.c
1 /*
2  * Bacula Catalog Database Delete record interface routines
3  * 
4  *    Kern Sibbald, December 2000
5  */
6
7 /*
8    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2 of
13    the License, or (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public
21    License along with this program; if not, write to the Free
22    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23    MA 02111-1307, USA.
24
25  */
26
27 /* *****FIXME**** fix fixed length of select_cmd[] and insert_cmd[] */
28
29 /* The following is necessary so that we do not include
30  * the dummy external definition of DB.
31  */
32 #define __SQL_C                       /* indicate that this is sql.c */
33
34 #include "bacula.h"
35 #include "cats.h"
36
37
38 #if    HAVE_MYSQL || HAVE_SQLITE
39 /* -----------------------------------------------------------------------
40  *
41  *   Generic Routines (or almost generic)
42  *
43  * -----------------------------------------------------------------------
44  */
45
46 /* Imported subroutines */
47 extern void print_dashes(B_DB *mdb);
48 extern void print_result(B_DB *mdb);
49 extern int QueryDB(char *file, int line, B_DB *db, char *select_cmd);
50 extern int DeleteDB(char *file, int line, B_DB *db, char *delete_cmd);
51        
52 /*
53  * Delete Pool record, must also delete all associated
54  *  Media records.
55  *
56  *  Returns: 0 on error
57  *           1 on success
58  *           PoolId = number of Pools deleted (should be 1)
59  *           NumVols = number of Media records deleted
60  */
61 int
62 db_delete_pool_record(B_DB *mdb, POOL_DBR *pr)
63 {
64    SQL_ROW row;
65
66    P(mdb->mutex);
67    Mmsg(&mdb->cmd, "SELECT PoolId FROM Pool WHERE Name=\"%s\"", pr->Name);
68    Dmsg1(10, "selectpool: %s\n", mdb->cmd);
69
70    pr->PoolId = pr->NumVols = 0;
71
72    if (QUERY_DB(mdb, mdb->cmd)) {
73
74       mdb->num_rows = sql_num_rows(mdb);
75    
76       if (mdb->num_rows == 0) {
77          Mmsg(&mdb->errmsg, _("No pool record %s exists\n"), pr->Name);
78          sql_free_result(mdb);
79          V(mdb->mutex);
80          return 0;
81       } else if (mdb->num_rows != 1) {
82          Mmsg(&mdb->errmsg, _("Expecting one pool record, got %d\n"), mdb->num_rows);
83          sql_free_result(mdb);
84          V(mdb->mutex);
85          return 0;
86       }
87       if ((row = sql_fetch_row(mdb)) == NULL) {
88          V(mdb->mutex);
89          Emsg1(M_ABORT, 0, _("Error fetching row %s\n"), sql_strerror(mdb));
90       }
91       pr->PoolId = atoi(row[0]);
92       sql_free_result(mdb);
93    }
94
95    /* Delete Media owned by this pool */
96    Mmsg(&mdb->cmd,
97 "DELETE FROM Media WHERE Media.PoolId = %d", pr->PoolId);
98
99    pr->NumVols = DELETE_DB(mdb, mdb->cmd);
100    Dmsg1(200, "Deleted %d Media records\n", pr->NumVols);
101
102    /* Delete Pool */
103    Mmsg(&mdb->cmd,
104 "DELETE FROM Pool WHERE Pool.PoolId = %d", pr->PoolId);
105    pr->PoolId = DELETE_DB(mdb, mdb->cmd);
106    Dmsg1(200, "Deleted %d Pool records\n", pr->PoolId);
107
108    V(mdb->mutex);
109    return 1;
110 }
111
112
113 /* Delete Media record */
114 int db_delete_media_record(B_DB *mdb, MEDIA_DBR *mr)
115 {
116
117    P(mdb->mutex);
118    if (mr->MediaId == 0) {
119       Mmsg(&mdb->cmd, "DELETE FROM Media WHERE VolumeName=\"%s\"", 
120            mr->VolumeName);
121    } else {
122       Mmsg(&mdb->cmd, "DELETE FROM Media WHERE MediaId=%d", 
123            mr->MediaId);
124    }
125
126    mr->MediaId = DELETE_DB(mdb, mdb->cmd);
127
128    V(mdb->mutex);
129    return 1;
130 }
131
132 #endif /* HAVE_MYSQL || HAVE_SQLITE */