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) {
83 m_msg(file, line, &mdb->errmsg, _("Insertion problem: affect_rows=%s\n"),
84 edit_uint64(mdb->num_rows, ed1));
85 e_msg(file, line, M_FATAL, 0, mdb->errmsg); /* ***FIXME*** remove me */
91 /* Utility routine for updates.
92 * Returns: 0 on failure
96 UpdateDB(char *file, int line, B_DB *mdb, char *cmd)
99 if (sql_query(mdb, cmd)) {
100 m_msg(file, line, &mdb->errmsg, _("update %s failed:\n%s\n"), cmd, sql_strerror(mdb));
101 e_msg(file, line, M_ERROR, 0, mdb->errmsg);
102 e_msg(file, line, M_ERROR, 0, "%s\n", cmd);
105 mdb->num_rows = sql_affected_rows(mdb);
106 if (mdb->num_rows != 1) {
108 m_msg(file, line, &mdb->errmsg, _("Update problem: affect_rows=%s\n"),
109 edit_uint64(mdb->num_rows, ed1));
110 e_msg(file, line, M_ERROR, 0, mdb->errmsg);
111 e_msg(file, line, M_ERROR, 0, "%s\n", cmd);
117 /* Utility routine for deletes
119 * Returns: -1 on error
120 * n number of rows affected
123 DeleteDB(char *file, int line, B_DB *mdb, char *cmd)
126 if (sql_query(mdb, cmd)) {
127 m_msg(file, line, &mdb->errmsg, _("delete %s failed:\n%s\n"), cmd, sql_strerror(mdb));
128 e_msg(file, line, M_ERROR, 0, mdb->errmsg);
131 return sql_affected_rows(mdb);
136 * Get record max. Query is already in mdb->cmd
139 * Returns: -1 on failure
142 int get_sql_record_max(B_DB *mdb)
147 if (QUERY_DB(mdb, mdb->cmd)) {
148 if ((row = sql_fetch_row(mdb)) == NULL) {
149 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
154 sql_free_result(mdb);
156 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
162 char *db_strerror(B_DB *mdb)
167 #endif /* HAVE_MYSQL | HAVE_SQLITE */