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 /* NOTE!!! The following routines expect that the
46 * calling subroutine sets and clears the mutex
49 /* Utility routine for queries */
51 QueryDB(char *file, int line, B_DB *mdb, char *cmd)
53 if (sql_query(mdb, cmd)) {
54 m_msg(file, line, &mdb->errmsg, _("query %s failed:\n%s\n"), cmd, sql_strerror(mdb));
55 e_msg(file, line, M_FATAL, 0, mdb->errmsg);
58 mdb->result = sql_store_result(mdb);
60 return mdb->result != NULL;
64 * Utility routine to do inserts
65 * Returns: 0 on failure
69 InsertDB(char *file, int line, B_DB *mdb, char *cmd)
71 if (sql_query(mdb, cmd)) {
72 m_msg(file, line, &mdb->errmsg, _("insert %s failed:\n%s\n"), cmd, sql_strerror(mdb));
73 e_msg(file, line, M_FATAL, 0, mdb->errmsg);
76 if (mdb->have_insert_id) {
77 mdb->num_rows = sql_affected_rows(mdb);
81 if (mdb->num_rows != 1) {
82 m_msg(file, line, &mdb->errmsg, _("Insertion problem: affect_rows=%" lld "\n"), mdb->num_rows);
83 e_msg(file, line, M_FATAL, 0, mdb->errmsg); /* ***FIXME*** remove me */
89 /* Utility routine for updates.
90 * Returns: 0 on failure
94 UpdateDB(char *file, int line, B_DB *mdb, char *cmd)
97 if (sql_query(mdb, cmd)) {
98 m_msg(file, line, &mdb->errmsg, _("update %s failed:\n%s\n"), cmd, sql_strerror(mdb));
99 e_msg(file, line, M_ERROR, 0, mdb->errmsg);
100 e_msg(file, line, M_ERROR, 0, "%s\n", cmd);
103 mdb->num_rows = sql_affected_rows(mdb);
104 if (mdb->num_rows != 1) {
105 m_msg(file, line, &mdb->errmsg, _("Update problem: affect_rows=%" lld "\n"), mdb->num_rows);
106 e_msg(file, line, M_ERROR, 0, mdb->errmsg);
107 e_msg(file, line, M_ERROR, 0, "%s\n", cmd);
113 /* Utility routine for deletes
115 * Returns: -1 on error
116 * n number of rows affected
119 DeleteDB(char *file, int line, B_DB *mdb, char *cmd)
122 if (sql_query(mdb, cmd)) {
123 m_msg(file, line, &mdb->errmsg, _("delete %s failed:\n%s\n"), cmd, sql_strerror(mdb));
124 e_msg(file, line, M_ERROR, 0, mdb->errmsg);
127 return sql_affected_rows(mdb);
132 * Get record max. Query is already in mdb->cmd
135 * Returns: -1 on failure
138 int get_sql_record_max(B_DB *mdb)
143 if (QUERY_DB(mdb, mdb->cmd)) {
144 if ((row = sql_fetch_row(mdb)) == NULL) {
145 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
150 sql_free_result(mdb);
152 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
158 char *db_strerror(B_DB *mdb)
163 #endif /* HAVE_MYSQL | HAVE_SQLITE */