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, 2001, 2002 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 conrrespond to the version we want */
68 int check_tables_version(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, "Database version mismatch. Wanted %d, got %d\n",
77 BDB_VERSION, version);
78 Jmsg(mdb->jcr, M_FATAL, 0, mdb->errmsg);
84 /* Utility routine for queries */
86 QueryDB(char *file, int line, 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, mdb->jcr, M_FATAL, 0, mdb->errmsg);
93 mdb->result = sql_store_result(mdb);
95 return mdb->result != NULL;
99 * Utility routine to do inserts
100 * Returns: 0 on failure
104 InsertDB(char *file, int line, B_DB *mdb, char *cmd)
106 if (sql_query(mdb, cmd)) {
107 m_msg(file, line, &mdb->errmsg, _("insert %s failed:\n%s\n"), cmd, sql_strerror(mdb));
108 j_msg(file, line, mdb->jcr, M_FATAL, 0, mdb->errmsg);
111 if (mdb->have_insert_id) {
112 mdb->num_rows = sql_affected_rows(mdb);
116 if (mdb->num_rows != 1) {
118 m_msg(file, line, &mdb->errmsg, _("Insertion problem: affect_rows=%s\n"),
119 edit_uint64(mdb->num_rows, ed1));
126 /* Utility routine for updates.
127 * Returns: 0 on failure
131 UpdateDB(char *file, int line, B_DB *mdb, char *cmd)
134 if (sql_query(mdb, cmd)) {
135 m_msg(file, line, &mdb->errmsg, _("update %s failed:\n%s\n"), cmd, sql_strerror(mdb));
136 j_msg(file, line, mdb->jcr, M_ERROR, 0, mdb->errmsg);
137 j_msg(file, line, mdb->jcr, M_ERROR, 0, "%s\n", cmd);
140 mdb->num_rows = sql_affected_rows(mdb);
141 if (mdb->num_rows != 1) {
143 m_msg(file, line, &mdb->errmsg, _("Update problem: affect_rows=%s\n"),
144 edit_uint64(mdb->num_rows, ed1));
151 /* Utility routine for deletes
153 * Returns: -1 on error
154 * n number of rows affected
157 DeleteDB(char *file, int line, B_DB *mdb, char *cmd)
160 if (sql_query(mdb, cmd)) {
161 m_msg(file, line, &mdb->errmsg, _("delete %s failed:\n%s\n"), cmd, sql_strerror(mdb));
162 j_msg(file, line, mdb->jcr, M_ERROR, 0, mdb->errmsg);
166 return sql_affected_rows(mdb);
171 * Get record max. Query is already in mdb->cmd
174 * Returns: -1 on failure
177 int get_sql_record_max(B_DB *mdb)
182 if (QUERY_DB(mdb, mdb->cmd)) {
183 if ((row = sql_fetch_row(mdb)) == NULL) {
184 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
189 sql_free_result(mdb);
191 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
197 char *db_strerror(B_DB *mdb)
202 void _db_lock(char *file, int line, B_DB *mdb)
205 if ((errstat=rwl_writelock(&mdb->lock)) != 0) {
206 j_msg(file, line, mdb->jcr, M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
211 void _db_unlock(char *file, int line, B_DB *mdb)
214 if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) {
215 j_msg(file, line, mdb->jcr, M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
221 * Start a transaction. This groups inserts and makes things
222 * much more efficient. Usually started when inserting
225 void db_start_transaction(B_DB *mdb)
229 /* Allow only 10,000 changes per transaction */
230 if (mdb->transaction && mdb->changes > 10000) {
231 db_end_transaction(mdb);
233 if (!mdb->transaction) {
234 my_sqlite_query(mdb, "BEGIN"); /* begin transaction */
235 Dmsg0(400, "Start SQLite transaction\n");
236 mdb->transaction = 1;
243 void db_end_transaction(B_DB *mdb)
247 if (mdb->transaction) {
248 my_sqlite_query(mdb, "COMMIT"); /* end transaction */
249 mdb->transaction = 0;
250 Dmsg1(400, "End SQLite transaction changes=%d\n", mdb->changes);
258 #endif /* HAVE_MYSQL | HAVE_SQLITE */