]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql.c
SQLite performance + misc -- see kes20Jul02
[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    mdb->changes++;
123    return 1;
124 }
125
126 /* Utility routine for updates.
127  *  Returns: 0 on failure
128  *           1 on success  
129  */
130 int
131 UpdateDB(char *file, int line, B_DB *mdb, char *cmd)
132 {
133
134    if (sql_query(mdb, cmd)) {
135       m_msg(file, line, &mdb->errmsg, _("update %s failed:\n%s\n"), cmd, sql_strerror(mdb));
136       e_msg(file, line, M_ERROR, 0, mdb->errmsg);
137       e_msg(file, line, M_ERROR, 0, "%s\n", cmd);
138       return 0;
139    }
140    mdb->num_rows = sql_affected_rows(mdb);
141    if (mdb->num_rows != 1) {
142       char ed1[30];
143       m_msg(file, line, &mdb->errmsg, _("Update problem: affect_rows=%s\n"), 
144          edit_uint64(mdb->num_rows, ed1));
145       e_msg(file, line, M_ERROR, 0, mdb->errmsg);
146       e_msg(file, line, M_ERROR, 0, "%s\n", cmd);
147       return 0;
148    }
149    mdb->changes++;
150    return 1;
151 }
152
153 /* Utility routine for deletes   
154  *
155  * Returns: -1 on error
156  *           n number of rows affected
157  */
158 int
159 DeleteDB(char *file, int line, B_DB *mdb, char *cmd)
160 {
161
162    if (sql_query(mdb, cmd)) {
163       m_msg(file, line, &mdb->errmsg, _("delete %s failed:\n%s\n"), cmd, sql_strerror(mdb));
164       e_msg(file, line, M_ERROR, 0, mdb->errmsg);
165       return -1;
166    }
167    mdb->changes++;
168    return sql_affected_rows(mdb);
169 }
170
171
172 /*
173  * Get record max. Query is already in mdb->cmd
174  *  No locking done
175  *
176  * Returns: -1 on failure
177  *          count on success
178  */
179 int get_sql_record_max(B_DB *mdb)
180 {
181    SQL_ROW row;
182    int stat = 0;
183
184    if (QUERY_DB(mdb, mdb->cmd)) {
185       if ((row = sql_fetch_row(mdb)) == NULL) {
186          Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
187          stat = -1;
188       } else {
189          stat = atoi(row[0]);
190       }
191       sql_free_result(mdb);
192    } else {
193       Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
194       stat = -1;
195    }
196    return stat;
197 }
198
199 char *db_strerror(B_DB *mdb)
200 {
201    return mdb->errmsg;
202 }
203
204 void _db_lock(char *file, int line, B_DB *mdb)
205 {
206    int errstat;
207    if ((errstat=rwl_writelock(&mdb->lock)) != 0) {
208       e_msg(file, line, M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
209            strerror(errstat));
210    }
211 }    
212
213 void _db_unlock(char *file, int line, B_DB *mdb)
214 {
215    int errstat;
216    if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) {
217       e_msg(file, line, M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
218            strerror(errstat));
219    }
220 }    
221
222
223 #endif /* HAVE_MYSQL | HAVE_SQLITE */