]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql.c
Optimize tree.c a bit, take Scott's spec & patch
[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-2003 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 uint32_t bacula_db_version = 0;
43
44 /* Forward referenced subroutines */
45 void print_dashes(B_DB *mdb);
46 void print_result(B_DB *mdb);
47
48 /*
49  * Called here to retrieve an integer from the database
50  */
51 static int int_handler(void *ctx, int num_fields, char **row)
52 {
53    uint32_t *val = (uint32_t *)ctx;
54
55    if (row[0]) {
56       *val = atoi(row[0]);
57    } else {
58       *val = 0;
59    }
60    return 0;
61 }
62        
63
64
65 /* NOTE!!! The following routines expect that the
66  *  calling subroutine sets and clears the mutex
67  */
68
69 /* Check that the tables correspond to the version we want */
70 int check_tables_version(JCR *jcr, B_DB *mdb)
71 {
72    char *query = "SELECT VersionId FROM Version";
73   
74    bacula_db_version = 0;
75    db_sql_query(mdb, query, int_handler, (void *)&bacula_db_version);
76    if (bacula_db_version != BDB_VERSION) {
77       Mmsg(&mdb->errmsg, "Version error for database \"%s\". Wanted %d, got %d\n",
78          mdb->db_name, BDB_VERSION, bacula_db_version);
79       Jmsg(jcr, M_FATAL, 0, "%s", mdb->errmsg);
80       return 0;
81    }
82    return 1;
83 }
84
85 /* Utility routine for queries. The database MUST be locked before calling here. */
86 int
87 QueryDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
88 {
89    int status;
90    if ((status=sql_query(mdb, cmd)) != 0) {
91       m_msg(file, line, &mdb->errmsg, _("query %s failed:\n%s\n"), cmd, sql_strerror(mdb));
92       j_msg(file, line, jcr, M_FATAL, 0, "%s", mdb->errmsg);
93       if (verbose) {
94          j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
95       }
96       return 0;
97    }
98
99    mdb->result = sql_store_result(mdb);
100
101    return mdb->result != NULL;
102 }
103
104 /* 
105  * Utility routine to do inserts   
106  * Returns: 0 on failure      
107  *          1 on success
108  */
109 int
110 InsertDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
111 {
112    if (sql_query(mdb, cmd)) {
113       m_msg(file, line, &mdb->errmsg,  _("insert %s failed:\n%s\n"), cmd, sql_strerror(mdb));
114       j_msg(file, line, jcr, M_FATAL, 0, "%s", mdb->errmsg);
115       if (verbose) {
116          j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
117       }
118       return 0;
119    }
120    if (mdb->have_insert_id) {
121       mdb->num_rows = sql_affected_rows(mdb);
122    } else {
123       mdb->num_rows = 1;
124    }
125    if (mdb->num_rows != 1) {
126       char ed1[30];
127       m_msg(file, line, &mdb->errmsg, _("Insertion problem: affected_rows=%s\n"), 
128          edit_uint64(mdb->num_rows, ed1));
129       if (verbose) {
130          j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
131       }
132       return 0;
133    }
134    mdb->changes++;
135    return 1;
136 }
137
138 /* Utility routine for updates.
139  *  Returns: 0 on failure
140  *           1 on success  
141  */
142 int
143 UpdateDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
144 {
145
146    if (sql_query(mdb, cmd)) {
147       m_msg(file, line, &mdb->errmsg, _("update %s failed:\n%s\n"), cmd, sql_strerror(mdb));
148       j_msg(file, line, jcr, M_ERROR, 0, "%s", mdb->errmsg);
149       if (verbose) {
150          j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
151       }
152       return 0;
153    }
154    mdb->num_rows = sql_affected_rows(mdb);
155    if (mdb->num_rows != 1) {
156       char ed1[30];
157       m_msg(file, line, &mdb->errmsg, _("Update problem: affected_rows=%s\n"), 
158          edit_uint64(mdb->num_rows, ed1));
159       if (verbose) {
160          j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
161       }
162       return 0;
163    }
164    mdb->changes++;
165    return 1;
166 }
167
168 /* Utility routine for deletes   
169  *
170  * Returns: -1 on error
171  *           n number of rows affected
172  */
173 int
174 DeleteDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
175 {
176
177    if (sql_query(mdb, cmd)) {
178       m_msg(file, line, &mdb->errmsg, _("delete %s failed:\n%s\n"), cmd, sql_strerror(mdb));
179       j_msg(file, line, jcr, M_ERROR, 0, "%s", mdb->errmsg);
180       if (verbose) {
181          j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
182       }
183       return -1;
184    }
185    mdb->changes++;
186    return sql_affected_rows(mdb);
187 }
188
189
190 /*
191  * Get record max. Query is already in mdb->cmd
192  *  No locking done
193  *
194  * Returns: -1 on failure
195  *          count on success
196  */
197 int get_sql_record_max(JCR *jcr, B_DB *mdb)
198 {
199    SQL_ROW row;
200    int stat = 0;
201
202    if (QUERY_DB(jcr, mdb, mdb->cmd)) {
203       if ((row = sql_fetch_row(mdb)) == NULL) {
204          Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
205          stat = -1;
206       } else {
207          stat = atoi(row[0]);
208       }
209       sql_free_result(mdb);
210    } else {
211       Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
212       stat = -1;
213    }
214    return stat;
215 }
216
217 char *db_strerror(B_DB *mdb)
218 {
219    return mdb->errmsg;
220 }
221
222 void _db_lock(char *file, int line, B_DB *mdb)
223 {
224    int errstat;
225    if ((errstat=rwl_writelock(&mdb->lock)) != 0) {
226       e_msg(file, line, M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
227            strerror(errstat));
228    }
229 }    
230
231 void _db_unlock(char *file, int line, B_DB *mdb)
232 {
233    int errstat;
234    if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) {
235       e_msg(file, line, M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
236            strerror(errstat));
237    }
238 }    
239
240 /*
241  * Start a transaction. This groups inserts and makes things
242  *  much more efficient. Usually started when inserting 
243  *  file attributes.
244  */
245 void db_start_transaction(JCR *jcr, B_DB *mdb)
246 {
247 #ifdef xAVE_SQLITE
248    db_lock(mdb);
249    /* Allow only 10,000 changes per transaction */
250    if (mdb->transaction && mdb->changes > 10000) {
251       db_end_transaction(jcr, mdb);
252    }
253    if (!mdb->transaction) {   
254       my_sqlite_query(mdb, "BEGIN");  /* begin transaction */
255       Dmsg0(400, "Start SQLite transaction\n");
256       mdb->transaction = 1;
257    }
258    db_unlock(mdb);
259 #endif
260
261 }
262
263 void db_end_transaction(JCR *jcr, B_DB *mdb)
264 {
265 #ifdef xAVE_SQLITE
266    db_lock(mdb);
267    if (mdb->transaction) {
268       my_sqlite_query(mdb, "COMMIT"); /* end transaction */
269       mdb->transaction = 0;
270       Dmsg1(400, "End SQLite transaction changes=%d\n", mdb->changes);
271    }
272    mdb->changes = 0;
273    db_unlock(mdb);
274 #endif
275 }
276
277 /*
278  * Given a full filename, split it into its path
279  *  and filename parts. They are returned in pool memory
280  *  in the mdb structure.
281  */
282 void split_path_and_filename(JCR *jcr, B_DB *mdb, char *fname)
283 {
284    char *p, *f;
285
286    /* Find path without the filename.  
287     * I.e. everything after the last / is a "filename".
288     * OK, maybe it is a directory name, but we treat it like
289     * a filename. If we don't find a / then the whole name
290     * must be a path name (e.g. c:).
291     */
292    for (p=f=fname; *p; p++) {
293       if (*p == '/') {
294          f = p;                       /* set pos of last slash */
295       }
296    }
297    if (*f == '/') {                   /* did we find a slash? */
298       f++;                            /* yes, point to filename */
299    } else {                           /* no, whole thing must be path name */
300       f = p;
301    }
302
303    /* If filename doesn't exist (i.e. root directory), we
304     * simply create a blank name consisting of a single 
305     * space. This makes handling zero length filenames
306     * easier.
307     */
308    mdb->fnl = p - f;
309    if (mdb->fnl > 0) {
310       mdb->fname = check_pool_memory_size(mdb->fname, mdb->fnl+1);
311       memcpy(mdb->fname, f, mdb->fnl);    /* copy filename */
312       mdb->fname[mdb->fnl] = 0;
313    } else {
314       mdb->fname[0] = ' ';            /* blank filename */
315       mdb->fname[1] = 0;
316       mdb->fnl = 1;
317    }
318
319    mdb->pnl = f - fname;    
320    if (mdb->pnl > 0) {
321       mdb->path = check_pool_memory_size(mdb->path, mdb->pnl+1);
322       memcpy(mdb->path, fname, mdb->pnl);
323       mdb->path[mdb->pnl] = 0;
324    } else {
325       Mmsg1(&mdb->errmsg, _("Path length is zero. File=%s\n"), fname);
326       Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
327       mdb->path[0] = ' ';
328       mdb->path[1] = 0;
329       mdb->pnl = 1;
330    }
331
332    Dmsg2(100, "sllit path=%s file=%s\n", mdb->path, mdb->fname);
333 }
334
335 #endif /* HAVE_MYSQL | HAVE_SQLITE */