]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql.c
213cc4acf8f7c37b01eef178d9aa5f5c06f0634d
[bacula/bacula] / bacula / src / cats / sql.c
1 /*
2  * Bacula Catalog Database interface routines
3  * 
4  *     Almost generic set of SQL database interface routines
5  *      (with a little more work)
6  *
7  *    Kern Sibbald, March 2000
8  */
9
10 /*
11    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
12
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.
17
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.
22
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,
26    MA 02111-1307, USA.
27
28  */
29
30 /* The following is necessary so that we do not include
31  * the dummy external definition of B_DB.
32  */
33 #define __SQL_C                       /* indicate that this is sql.c */
34
35 #include "bacula.h"
36 #include "cats.h"
37
38 #if    HAVE_MYSQL | HAVE_SQLITE
39
40 /* Forward referenced subroutines */
41 void print_dashes(B_DB *mdb);
42 void print_result(B_DB *mdb);
43
44
45 /* NOTE!!! The following routines expect that the
46  *  calling subroutine sets and clears the mutex
47  */
48
49 /* Utility routine for queries */
50 int
51 QueryDB(char *file, int line, B_DB *mdb, char *cmd)
52 {
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);
56       return 0;
57    }
58    mdb->result = sql_store_result(mdb);
59    
60    return mdb->result != NULL;
61 }
62
63 /* 
64  * Utility routine to do inserts   
65  * Returns: 0 on failure      
66  *          1 on success
67  */
68 int
69 InsertDB(char *file, int line, B_DB *mdb, char *cmd)
70 {
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);
74       return 0;
75    }
76    if (mdb->have_insert_id) {
77       mdb->num_rows = sql_affected_rows(mdb);
78    } else {
79       mdb->num_rows = 1;
80    }
81    if (mdb->num_rows != 1) {
82       char ed1[30];
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 */
86       return 0;
87    }
88    return 1;
89 }
90
91 /* Utility routine for updates.
92  *  Returns: 0 on failure
93  *           1 on success  
94  */
95 int
96 UpdateDB(char *file, int line, B_DB *mdb, char *cmd)
97 {
98
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);
103       return 0;
104    }
105    mdb->num_rows = sql_affected_rows(mdb);
106    if (mdb->num_rows != 1) {
107       char ed1[30];
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);
112       return 0;
113    }
114    return 1;
115 }
116
117 /* Utility routine for deletes   
118  *
119  * Returns: -1 on error
120  *           n number of rows affected
121  */
122 int
123 DeleteDB(char *file, int line, B_DB *mdb, char *cmd)
124 {
125
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);
129       return -1;
130    }
131    return sql_affected_rows(mdb);
132 }
133
134
135 /*
136  * Get record max. Query is already in mdb->cmd
137  *  No locking done
138  *
139  * Returns: -1 on failure
140  *          count on success
141  */
142 int get_sql_record_max(B_DB *mdb)
143 {
144    SQL_ROW row;
145    int stat = 0;
146
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));
150          stat = -1;
151       } else {
152          stat = atoi(row[0]);
153       }
154       sql_free_result(mdb);
155    } else {
156       Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
157       stat = -1;
158    }
159    return stat;
160 }
161
162 char *db_strerror(B_DB *mdb)
163 {
164    return mdb->errmsg;
165 }
166
167 #endif /* HAVE_MYSQL | HAVE_SQLITE */