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 /* Forward referenced subroutines */
43 void print_dashes(B_DB *mdb);
44 void print_result(B_DB *mdb);
47 * Called here to retrieve an integer from the database
49 static int int_handler(void *ctx, int num_fields, char **row)
51 uint32_t *val = (uint32_t *)ctx;
63 /* NOTE!!! The following routines expect that the
64 * calling subroutine sets and clears the mutex
67 /* Check that the tables correspond to the version we want */
68 int check_tables_version(void *jcr, B_DB *mdb)
71 char *query = "SELECT VersionId FROM Version";
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);
84 /* Utility routine for queries */
86 QueryDB(char *file, int line, void *jcr, B_DB *mdb, char *cmd)
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);
92 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
96 mdb->result = sql_store_result(mdb);
98 return mdb->result != NULL;
102 * Utility routine to do inserts
103 * Returns: 0 on failure
107 InsertDB(char *file, int line, void *jcr, B_DB *mdb, char *cmd)
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);
113 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
117 if (mdb->have_insert_id) {
118 mdb->num_rows = sql_affected_rows(mdb);
122 if (mdb->num_rows != 1) {
124 m_msg(file, line, &mdb->errmsg, _("Insertion problem: affected_rows=%s\n"),
125 edit_uint64(mdb->num_rows, ed1));
132 /* Utility routine for updates.
133 * Returns: 0 on failure
137 UpdateDB(char *file, int line, void *jcr, B_DB *mdb, char *cmd)
140 if (sql_query(mdb, cmd)) {
141 m_msg(file, line, &mdb->errmsg, _("update %s failed:\n%s\n"), cmd, sql_strerror(mdb));
142 j_msg(file, line, jcr, M_ERROR, 0, "%s", mdb->errmsg);
144 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
148 mdb->num_rows = sql_affected_rows(mdb);
149 if (mdb->num_rows != 1) {
151 m_msg(file, line, &mdb->errmsg, _("Update problem: affect_rows=%s\n"),
152 edit_uint64(mdb->num_rows, ed1));
159 /* Utility routine for deletes
161 * Returns: -1 on error
162 * n number of rows affected
165 DeleteDB(char *file, int line, void *jcr, B_DB *mdb, char *cmd)
168 if (sql_query(mdb, cmd)) {
169 m_msg(file, line, &mdb->errmsg, _("delete %s failed:\n%s\n"), cmd, sql_strerror(mdb));
170 j_msg(file, line, jcr, M_ERROR, 0, "%s", mdb->errmsg);
172 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
177 return sql_affected_rows(mdb);
182 * Get record max. Query is already in mdb->cmd
185 * Returns: -1 on failure
188 int get_sql_record_max(void *jcr, B_DB *mdb)
193 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
194 if ((row = sql_fetch_row(mdb)) == NULL) {
195 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
200 sql_free_result(mdb);
202 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
208 char *db_strerror(B_DB *mdb)
213 void _db_lock(char *file, int line, B_DB *mdb)
216 if ((errstat=rwl_writelock(&mdb->lock)) != 0) {
217 e_msg(file, line, M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
222 void _db_unlock(char *file, int line, B_DB *mdb)
225 if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) {
226 e_msg(file, line, M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
232 * Start a transaction. This groups inserts and makes things
233 * much more efficient. Usually started when inserting
236 void db_start_transaction(void *jcr, B_DB *mdb)
240 /* Allow only 10,000 changes per transaction */
241 if (mdb->transaction && mdb->changes > 10000) {
242 db_end_transaction(jcr, mdb);
244 if (!mdb->transaction) {
245 my_sqlite_query(mdb, "BEGIN"); /* begin transaction */
246 Dmsg0(400, "Start SQLite transaction\n");
247 mdb->transaction = 1;
254 void db_end_transaction(void *jcr, B_DB *mdb)
258 if (mdb->transaction) {
259 my_sqlite_query(mdb, "COMMIT"); /* end transaction */
260 mdb->transaction = 0;
261 Dmsg1(400, "End SQLite transaction changes=%d\n", mdb->changes);
269 * Given a full filename, split it into its path
270 * and filename parts. They are returned in pool memory
271 * in the mdb structure.
273 void split_path_and_filename(void *jcr, B_DB *mdb, char *fname)
277 /* Find path without the filename.
278 * I.e. everything after the last / is a "filename".
279 * OK, maybe it is a directory name, but we treat it like
280 * a filename. If we don't find a / then the whole name
281 * must be a path name (e.g. c:).
283 for (p=f=fname; *p; p++) {
285 f = p; /* set pos of last slash */
288 if (*f == '/') { /* did we find a slash? */
289 f++; /* yes, point to filename */
290 } else { /* no, whole thing must be path name */
294 /* If filename doesn't exist (i.e. root directory), we
295 * simply create a blank name consisting of a single
296 * space. This makes handling zero length filenames
301 mdb->fname = check_pool_memory_size(mdb->fname, mdb->fnl+1);
302 memcpy(mdb->fname, f, mdb->fnl); /* copy filename */
303 mdb->fname[mdb->fnl] = 0;
305 mdb->fname[0] = ' '; /* blank filename */
310 mdb->pnl = f - fname;
312 mdb->path = check_pool_memory_size(mdb->path, mdb->pnl+1);
313 memcpy(mdb->path, fname, mdb->pnl);
314 mdb->path[mdb->pnl] = 0;
316 Mmsg1(&mdb->errmsg, _("Path length is zero. File=%s\n"), fname);
317 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
323 Dmsg2(100, "sllit path=%s file=%s\n", mdb->path, mdb->fname);
326 #endif /* HAVE_MYSQL | HAVE_SQLITE */