]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql.c
647d4115c5430c3abe073d09352b7b9fb2858b32
[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  * Called here to retrieve an integer from the database
46  */
47 static int int_handler(void *ctx, int num_fields, char **row)
48 {
49    uint32_t *val = (uint32_t *)ctx;
50
51    if (row[0]) {
52       *val = atoi(row[0]);
53    } else {
54       *val = 0;
55    }
56    return 0;
57 }
58        
59
60
61 /* NOTE!!! The following routines expect that the
62  *  calling subroutine sets and clears the mutex
63  */
64
65 /* Check that the tables conrrespond to the version we want */
66 int check_tables_version(B_DB *mdb)
67 {
68    uint32_t version;
69    char *query = "SELECT VersionId FROM Version";
70   
71    version = 0;
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);
76       return 0;
77    }
78    return 1;
79 }
80
81 /* Utility routine for queries */
82 int
83 QueryDB(char *file, int line, B_DB *mdb, char *cmd)
84 {
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);
88       return 0;
89    }
90    mdb->result = sql_store_result(mdb);
91    
92    return mdb->result != NULL;
93 }
94
95 /* 
96  * Utility routine to do inserts   
97  * Returns: 0 on failure      
98  *          1 on success
99  */
100 int
101 InsertDB(char *file, int line, B_DB *mdb, char *cmd)
102 {
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);
106       return 0;
107    }
108    if (mdb->have_insert_id) {
109       mdb->num_rows = sql_affected_rows(mdb);
110    } else {
111       mdb->num_rows = 1;
112    }
113    if (mdb->num_rows != 1) {
114       char ed1[30];
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 */
118       return 0;
119    }
120    return 1;
121 }
122
123 /* Utility routine for updates.
124  *  Returns: 0 on failure
125  *           1 on success  
126  */
127 int
128 UpdateDB(char *file, int line, B_DB *mdb, char *cmd)
129 {
130
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);
135       return 0;
136    }
137    mdb->num_rows = sql_affected_rows(mdb);
138    if (mdb->num_rows != 1) {
139       char ed1[30];
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);
144       return 0;
145    }
146    return 1;
147 }
148
149 /* Utility routine for deletes   
150  *
151  * Returns: -1 on error
152  *           n number of rows affected
153  */
154 int
155 DeleteDB(char *file, int line, B_DB *mdb, char *cmd)
156 {
157
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);
161       return -1;
162    }
163    return sql_affected_rows(mdb);
164 }
165
166
167 /*
168  * Get record max. Query is already in mdb->cmd
169  *  No locking done
170  *
171  * Returns: -1 on failure
172  *          count on success
173  */
174 int get_sql_record_max(B_DB *mdb)
175 {
176    SQL_ROW row;
177    int stat = 0;
178
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));
182          stat = -1;
183       } else {
184          stat = atoi(row[0]);
185       }
186       sql_free_result(mdb);
187    } else {
188       Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
189       stat = -1;
190    }
191    return stat;
192 }
193
194 char *db_strerror(B_DB *mdb)
195 {
196    return mdb->errmsg;
197 }
198
199 #endif /* HAVE_MYSQL | HAVE_SQLITE */