2 * Bacula Catalog Database interface routines
4 * Almost generic set of SQL database interface routines
5 * (with a little more work)
7 * Kern Sibbald, March 2000
13 Copyright (C) 2000-2003 Kern Sibbald and John Walker
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.
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.
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,
32 /* The following is necessary so that we do not include
33 * the dummy external definition of B_DB.
35 #define __SQL_C /* indicate that this is sql.c */
40 #if HAVE_MYSQL | HAVE_SQLITE
42 uint32_t bacula_db_version = 0;
44 /* Forward referenced subroutines */
45 void print_dashes(B_DB *mdb);
46 void print_result(B_DB *mdb);
49 * Called here to retrieve an integer from the database
51 static int int_handler(void *ctx, int num_fields, char **row)
53 uint32_t *val = (uint32_t *)ctx;
65 /* NOTE!!! The following routines expect that the
66 * calling subroutine sets and clears the mutex
69 /* Check that the tables correspond to the version we want */
70 int check_tables_version(JCR *jcr, B_DB *mdb)
72 char *query = "SELECT VersionId FROM Version";
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);
85 /* Utility routine for queries. The database MUST be locked before calling here. */
87 QueryDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
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);
94 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
99 mdb->result = sql_store_result(mdb);
101 return mdb->result != NULL;
105 * Utility routine to do inserts
106 * Returns: 0 on failure
110 InsertDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
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);
116 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
120 if (mdb->have_insert_id) {
121 mdb->num_rows = sql_affected_rows(mdb);
125 if (mdb->num_rows != 1) {
127 m_msg(file, line, &mdb->errmsg, _("Insertion problem: affected_rows=%s\n"),
128 edit_uint64(mdb->num_rows, ed1));
130 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
138 /* Utility routine for updates.
139 * Returns: 0 on failure
143 UpdateDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
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);
150 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
154 mdb->num_rows = sql_affected_rows(mdb);
155 if (mdb->num_rows != 1) {
157 m_msg(file, line, &mdb->errmsg, _("Update problem: affected_rows=%s\n"),
158 edit_uint64(mdb->num_rows, ed1));
160 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
168 /* Utility routine for deletes
170 * Returns: -1 on error
171 * n number of rows affected
174 DeleteDB(char *file, int line, JCR *jcr, B_DB *mdb, char *cmd)
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);
181 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
186 return sql_affected_rows(mdb);
191 * Get record max. Query is already in mdb->cmd
194 * Returns: -1 on failure
197 int get_sql_record_max(JCR *jcr, B_DB *mdb)
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));
209 sql_free_result(mdb);
211 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
217 char *db_strerror(B_DB *mdb)
222 void _db_lock(char *file, int line, B_DB *mdb)
225 if ((errstat=rwl_writelock(&mdb->lock)) != 0) {
226 e_msg(file, line, M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
231 void _db_unlock(char *file, int line, B_DB *mdb)
234 if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) {
235 e_msg(file, line, M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
241 * Start a transaction. This groups inserts and makes things
242 * much more efficient. Usually started when inserting
245 void db_start_transaction(JCR *jcr, B_DB *mdb)
249 /* Allow only 10,000 changes per transaction */
250 if (mdb->transaction && mdb->changes > 10000) {
251 db_end_transaction(jcr, mdb);
253 if (!mdb->transaction) {
254 my_sqlite_query(mdb, "BEGIN"); /* begin transaction */
255 Dmsg0(400, "Start SQLite transaction\n");
256 mdb->transaction = 1;
263 void db_end_transaction(JCR *jcr, B_DB *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);
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.
282 void split_path_and_filename(JCR *jcr, B_DB *mdb, char *fname)
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:).
292 for (p=f=fname; *p; p++) {
294 f = p; /* set pos of last slash */
297 if (*f == '/') { /* did we find a slash? */
298 f++; /* yes, point to filename */
299 } else { /* no, whole thing must be path name */
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
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;
314 mdb->fname[0] = ' '; /* blank filename */
319 mdb->pnl = f - fname;
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;
325 Mmsg1(&mdb->errmsg, _("Path length is zero. File=%s\n"), fname);
326 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
332 Dmsg2(100, "sllit path=%s file=%s\n", mdb->path, mdb->fname);
335 #endif /* HAVE_MYSQL | HAVE_SQLITE */