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);
83 /* Utility routine for queries */
85 QueryDB(char *file, int line, B_DB *mdb, char *cmd)
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);
92 mdb->result = sql_store_result(mdb);
94 return mdb->result != NULL;
98 * Utility routine to do inserts
99 * Returns: 0 on failure
103 InsertDB(char *file, int line, B_DB *mdb, char *cmd)
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);
110 if (mdb->have_insert_id) {
111 mdb->num_rows = sql_affected_rows(mdb);
115 if (mdb->num_rows != 1) {
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 */
125 /* Utility routine for updates.
126 * Returns: 0 on failure
130 UpdateDB(char *file, int line, B_DB *mdb, char *cmd)
133 if (sql_query(mdb, cmd)) {
134 m_msg(file, line, &mdb->errmsg, _("update %s failed:\n%s\n"), cmd, sql_strerror(mdb));
135 e_msg(file, line, M_ERROR, 0, mdb->errmsg);
136 e_msg(file, line, M_ERROR, 0, "%s\n", cmd);
139 mdb->num_rows = sql_affected_rows(mdb);
140 if (mdb->num_rows != 1) {
142 m_msg(file, line, &mdb->errmsg, _("Update problem: affect_rows=%s\n"),
143 edit_uint64(mdb->num_rows, ed1));
144 e_msg(file, line, M_ERROR, 0, mdb->errmsg);
145 e_msg(file, line, M_ERROR, 0, "%s\n", cmd);
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 e_msg(file, line, M_ERROR, 0, mdb->errmsg);
165 return sql_affected_rows(mdb);
170 * Get record max. Query is already in mdb->cmd
173 * Returns: -1 on failure
176 int get_sql_record_max(B_DB *mdb)
181 if (QUERY_DB(mdb, mdb->cmd)) {
182 if ((row = sql_fetch_row(mdb)) == NULL) {
183 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
188 sql_free_result(mdb);
190 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
196 char *db_strerror(B_DB *mdb)
201 void _db_lock(char *file, int line, B_DB *mdb)
204 if ((errstat=rwl_writelock(&mdb->lock)) != 0) {
205 e_msg(file, line, M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
210 void _db_unlock(char *file, int line, B_DB *mdb)
213 if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) {
214 e_msg(file, line, M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
220 #endif /* HAVE_MYSQL | HAVE_SQLITE */