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
43 char catalog_db[] = "MySQL";
45 char catalog_db[] = "SQLite";
48 /* Forward referenced subroutines */
49 void print_dashes(B_DB *mdb);
50 void print_result(B_DB *mdb);
53 * Called here to retrieve an integer from the database
55 static int int_handler(void *ctx, int num_fields, char **row)
57 uint32_t *val = (uint32_t *)ctx;
69 /* NOTE!!! The following routines expect that the
70 * calling subroutine sets and clears the mutex
73 /* Check that the tables correspond to the version we want */
74 int check_tables_version(void *jcr, B_DB *mdb)
77 char *query = "SELECT VersionId FROM Version";
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);
90 /* Utility routine for queries */
92 QueryDB(char *file, int line, void *jcr, B_DB *mdb, char *cmd)
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);
98 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
102 mdb->result = sql_store_result(mdb);
104 return mdb->result != NULL;
108 * Utility routine to do inserts
109 * Returns: 0 on failure
113 InsertDB(char *file, int line, void *jcr, B_DB *mdb, char *cmd)
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);
119 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
123 if (mdb->have_insert_id) {
124 mdb->num_rows = sql_affected_rows(mdb);
128 if (mdb->num_rows != 1) {
130 m_msg(file, line, &mdb->errmsg, _("Insertion problem: affected_rows=%s\n"),
131 edit_uint64(mdb->num_rows, ed1));
133 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
141 /* Utility routine for updates.
142 * Returns: 0 on failure
146 UpdateDB(char *file, int line, void *jcr, B_DB *mdb, char *cmd)
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);
153 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
157 mdb->num_rows = sql_affected_rows(mdb);
158 if (mdb->num_rows != 1) {
160 m_msg(file, line, &mdb->errmsg, _("Update problem: affected_rows=%s\n"),
161 edit_uint64(mdb->num_rows, ed1));
163 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
171 /* Utility routine for deletes
173 * Returns: -1 on error
174 * n number of rows affected
177 DeleteDB(char *file, int line, void *jcr, B_DB *mdb, char *cmd)
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);
184 j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd);
189 return sql_affected_rows(mdb);
194 * Get record max. Query is already in mdb->cmd
197 * Returns: -1 on failure
200 int get_sql_record_max(void *jcr, B_DB *mdb)
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));
212 sql_free_result(mdb);
214 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
220 char *db_strerror(B_DB *mdb)
225 void _db_lock(char *file, int line, B_DB *mdb)
228 if ((errstat=rwl_writelock(&mdb->lock)) != 0) {
229 e_msg(file, line, M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
234 void _db_unlock(char *file, int line, B_DB *mdb)
237 if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) {
238 e_msg(file, line, M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
244 * Start a transaction. This groups inserts and makes things
245 * much more efficient. Usually started when inserting
248 void db_start_transaction(void *jcr, B_DB *mdb)
252 /* Allow only 10,000 changes per transaction */
253 if (mdb->transaction && mdb->changes > 10000) {
254 db_end_transaction(jcr, mdb);
256 if (!mdb->transaction) {
257 my_sqlite_query(mdb, "BEGIN"); /* begin transaction */
258 Dmsg0(400, "Start SQLite transaction\n");
259 mdb->transaction = 1;
266 void db_end_transaction(void *jcr, B_DB *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);
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.
285 void split_path_and_filename(void *jcr, B_DB *mdb, char *fname)
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:).
295 for (p=f=fname; *p; p++) {
297 f = p; /* set pos of last slash */
300 if (*f == '/') { /* did we find a slash? */
301 f++; /* yes, point to filename */
302 } else { /* no, whole thing must be path name */
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
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;
317 mdb->fname[0] = ' '; /* blank filename */
322 mdb->pnl = f - fname;
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;
328 Mmsg1(&mdb->errmsg, _("Path length is zero. File=%s\n"), fname);
329 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
335 Dmsg2(100, "sllit path=%s file=%s\n", mdb->path, mdb->fname);
338 #endif /* HAVE_MYSQL | HAVE_SQLITE */