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