]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql.c
Implement Auto Prune and Auto Recycle
[bacula/bacula] / bacula / src / cats / sql.c
1 /*
2  * Bacula Catalog Database interface routines
3  * 
4  *     Almost generic set of SQL database interface routines
5  *      (with a little more work)
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 /* The following is necessary so that we do not include
33  * the dummy external definition of B_DB.
34  */
35 #define __SQL_C                       /* indicate that this is sql.c */
36
37 #include "bacula.h"
38 #include "cats.h"
39
40 #if    HAVE_MYSQL | HAVE_SQLITE
41
42 /* Forward referenced subroutines */
43 void print_dashes(B_DB *mdb);
44 void print_result(B_DB *mdb);
45
46 /*
47  * Called here to retrieve an integer from the database
48  */
49 static int int_handler(void *ctx, int num_fields, char **row)
50 {
51    uint32_t *val = (uint32_t *)ctx;
52
53    if (row[0]) {
54       *val = atoi(row[0]);
55    } else {
56       *val = 0;
57    }
58    return 0;
59 }
60        
61
62
63 /* NOTE!!! The following routines expect that the
64  *  calling subroutine sets and clears the mutex
65  */
66
67 /* Check that the tables conrrespond to the version we want */
68 int check_tables_version(B_DB *mdb)
69 {
70    uint32_t version;
71    char *query = "SELECT VersionId FROM Version";
72   
73    version = 0;
74    db_sql_query(mdb, query, int_handler, (void *)&version);
75    if (version != BDB_VERSION) {
76       Mmsg(&mdb->errmsg, "Database version mismatch. Wanted %d, got %d\n",
77          BDB_VERSION, version);
78       return 0;
79    }
80    return 1;
81 }
82
83 /* Utility routine for queries */
84 int
85 QueryDB(char *file, int line, B_DB *mdb, char *cmd)
86 {
87    if (sql_query(mdb, cmd)) {
88       m_msg(file, line, &mdb->errmsg, _("query %s failed:\n%s\n"), cmd, sql_strerror(mdb));
89       e_msg(file, line, M_FATAL, 0, mdb->errmsg);
90       return 0;
91    }
92    mdb->result = sql_store_result(mdb);
93    
94    return mdb->result != NULL;
95 }
96
97 /* 
98  * Utility routine to do inserts   
99  * Returns: 0 on failure      
100  *          1 on success
101  */
102 int
103 InsertDB(char *file, int line, B_DB *mdb, char *cmd)
104 {
105    if (sql_query(mdb, cmd)) {
106       m_msg(file, line, &mdb->errmsg,  _("insert %s failed:\n%s\n"), cmd, sql_strerror(mdb));
107       e_msg(file, line, M_FATAL, 0, mdb->errmsg);
108       return 0;
109    }
110    if (mdb->have_insert_id) {
111       mdb->num_rows = sql_affected_rows(mdb);
112    } else {
113       mdb->num_rows = 1;
114    }
115    if (mdb->num_rows != 1) {
116       char ed1[30];
117       m_msg(file, line, &mdb->errmsg, _("Insertion problem: affect_rows=%s\n"), 
118          edit_uint64(mdb->num_rows, ed1));
119       e_msg(file, line, M_FATAL, 0, mdb->errmsg);  /* ***FIXME*** remove me */
120       return 0;
121    }
122    return 1;
123 }
124
125 /* Utility routine for updates.
126  *  Returns: 0 on failure
127  *           1 on success  
128  */
129 int
130 UpdateDB(char *file, int line, B_DB *mdb, char *cmd)
131 {
132
133    if (sql_query(mdb, cmd)) {
134       m_msg(file, line, &mdb->errmsg, _("update %s failed:\n%s\n"), cmd, sql_strerror(mdb));
135       e_msg(file, line, M_ERROR, 0, mdb->errmsg);
136       e_msg(file, line, M_ERROR, 0, "%s\n", cmd);
137       return 0;
138    }
139    mdb->num_rows = sql_affected_rows(mdb);
140    if (mdb->num_rows != 1) {
141       char ed1[30];
142       m_msg(file, line, &mdb->errmsg, _("Update problem: affect_rows=%s\n"), 
143          edit_uint64(mdb->num_rows, ed1));
144       e_msg(file, line, M_ERROR, 0, mdb->errmsg);
145       e_msg(file, line, M_ERROR, 0, "%s\n", cmd);
146       return 0;
147    }
148    return 1;
149 }
150
151 /* Utility routine for deletes   
152  *
153  * Returns: -1 on error
154  *           n number of rows affected
155  */
156 int
157 DeleteDB(char *file, int line, B_DB *mdb, char *cmd)
158 {
159
160    if (sql_query(mdb, cmd)) {
161       m_msg(file, line, &mdb->errmsg, _("delete %s failed:\n%s\n"), cmd, sql_strerror(mdb));
162       e_msg(file, line, M_ERROR, 0, mdb->errmsg);
163       return -1;
164    }
165    return sql_affected_rows(mdb);
166 }
167
168
169 /*
170  * Get record max. Query is already in mdb->cmd
171  *  No locking done
172  *
173  * Returns: -1 on failure
174  *          count on success
175  */
176 int get_sql_record_max(B_DB *mdb)
177 {
178    SQL_ROW row;
179    int stat = 0;
180
181    if (QUERY_DB(mdb, mdb->cmd)) {
182       if ((row = sql_fetch_row(mdb)) == NULL) {
183          Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
184          stat = -1;
185       } else {
186          stat = atoi(row[0]);
187       }
188       sql_free_result(mdb);
189    } else {
190       Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
191       stat = -1;
192    }
193    return stat;
194 }
195
196 char *db_strerror(B_DB *mdb)
197 {
198    return mdb->errmsg;
199 }
200
201 void _db_lock(char *file, int line, B_DB *mdb)
202 {
203    int errstat;
204    if ((errstat=rwl_writelock(&mdb->lock)) != 0) {
205       e_msg(file, line, M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
206            strerror(errstat));
207    }
208 }    
209
210 void _db_unlock(char *file, int line, B_DB *mdb)
211 {
212    int errstat;
213    if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) {
214       e_msg(file, line, M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
215            strerror(errstat));
216    }
217 }    
218
219
220 #endif /* HAVE_MYSQL | HAVE_SQLITE */