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