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
11 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of
16 the License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public
24 License along with this program; if not, write to the Free
25 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /* The following is necessary so that we do not include
31 * the dummy external definition of B_DB.
33 #define __SQL_C /* indicate that this is sql.c */
38 #if HAVE_MYSQL | HAVE_SQLITE
40 /* Forward referenced subroutines */
41 void print_dashes(B_DB *mdb);
42 void print_result(B_DB *mdb);
45 * Called here to retrieve an integer from the database
47 static int int_handler(void *ctx, int num_fields, char **row)
49 uint32_t *val = (uint32_t *)ctx;
61 /* NOTE!!! The following routines expect that the
62 * calling subroutine sets and clears the mutex
65 /* Check that the tables conrrespond to the version we want */
66 int check_tables_version(B_DB *mdb)
69 char *query = "SELECT VersionId FROM Version";
72 db_sql_query(mdb, query, int_handler, (void *)&version);
73 if (version != BDB_VERSION) {
74 Mmsg(&mdb->errmsg, "Database version mismatch. Wanted %d, got %d\n",
75 BDB_VERSION, version);
81 /* Utility routine for queries */
83 QueryDB(char *file, int line, B_DB *mdb, char *cmd)
85 if (sql_query(mdb, cmd)) {
86 m_msg(file, line, &mdb->errmsg, _("query %s failed:\n%s\n"), cmd, sql_strerror(mdb));
87 e_msg(file, line, M_FATAL, 0, mdb->errmsg);
90 mdb->result = sql_store_result(mdb);
92 return mdb->result != NULL;
96 * Utility routine to do inserts
97 * Returns: 0 on failure
101 InsertDB(char *file, int line, B_DB *mdb, char *cmd)
103 if (sql_query(mdb, cmd)) {
104 m_msg(file, line, &mdb->errmsg, _("insert %s failed:\n%s\n"), cmd, sql_strerror(mdb));
105 e_msg(file, line, M_FATAL, 0, mdb->errmsg);
108 if (mdb->have_insert_id) {
109 mdb->num_rows = sql_affected_rows(mdb);
113 if (mdb->num_rows != 1) {
115 m_msg(file, line, &mdb->errmsg, _("Insertion problem: affect_rows=%s\n"),
116 edit_uint64(mdb->num_rows, ed1));
117 e_msg(file, line, M_FATAL, 0, mdb->errmsg); /* ***FIXME*** remove me */
123 /* Utility routine for updates.
124 * Returns: 0 on failure
128 UpdateDB(char *file, int line, B_DB *mdb, char *cmd)
131 if (sql_query(mdb, cmd)) {
132 m_msg(file, line, &mdb->errmsg, _("update %s failed:\n%s\n"), cmd, sql_strerror(mdb));
133 e_msg(file, line, M_ERROR, 0, mdb->errmsg);
134 e_msg(file, line, M_ERROR, 0, "%s\n", cmd);
137 mdb->num_rows = sql_affected_rows(mdb);
138 if (mdb->num_rows != 1) {
140 m_msg(file, line, &mdb->errmsg, _("Update problem: affect_rows=%s\n"),
141 edit_uint64(mdb->num_rows, ed1));
142 e_msg(file, line, M_ERROR, 0, mdb->errmsg);
143 e_msg(file, line, M_ERROR, 0, "%s\n", cmd);
149 /* Utility routine for deletes
151 * Returns: -1 on error
152 * n number of rows affected
155 DeleteDB(char *file, int line, B_DB *mdb, char *cmd)
158 if (sql_query(mdb, cmd)) {
159 m_msg(file, line, &mdb->errmsg, _("delete %s failed:\n%s\n"), cmd, sql_strerror(mdb));
160 e_msg(file, line, M_ERROR, 0, mdb->errmsg);
163 return sql_affected_rows(mdb);
168 * Get record max. Query is already in mdb->cmd
171 * Returns: -1 on failure
174 int get_sql_record_max(B_DB *mdb)
179 if (QUERY_DB(mdb, mdb->cmd)) {
180 if ((row = sql_fetch_row(mdb)) == NULL) {
181 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
186 sql_free_result(mdb);
188 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
194 char *db_strerror(B_DB *mdb)
199 #endif /* HAVE_MYSQL | HAVE_SQLITE */