]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql.c
21fdbe4436bae7a71d101d481a90e2b7e2062294
[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 /* Check that the tables conrrespond to the version we want */
50 int check_tables_version(B_DB *mdb)
51 {
52 /*****FIXME***** implement */
53    return 1;
54 }
55
56 /* Utility routine for queries */
57 int
58 QueryDB(char *file, int line, B_DB *mdb, char *cmd)
59 {
60    if (sql_query(mdb, cmd)) {
61       m_msg(file, line, &mdb->errmsg, _("query %s failed:\n%s\n"), cmd, sql_strerror(mdb));
62       e_msg(file, line, M_FATAL, 0, mdb->errmsg);
63       return 0;
64    }
65    mdb->result = sql_store_result(mdb);
66    
67    return mdb->result != NULL;
68 }
69
70 /* 
71  * Utility routine to do inserts   
72  * Returns: 0 on failure      
73  *          1 on success
74  */
75 int
76 InsertDB(char *file, int line, B_DB *mdb, char *cmd)
77 {
78    if (sql_query(mdb, cmd)) {
79       m_msg(file, line, &mdb->errmsg,  _("insert %s failed:\n%s\n"), cmd, sql_strerror(mdb));
80       e_msg(file, line, M_FATAL, 0, mdb->errmsg);
81       return 0;
82    }
83    if (mdb->have_insert_id) {
84       mdb->num_rows = sql_affected_rows(mdb);
85    } else {
86       mdb->num_rows = 1;
87    }
88    if (mdb->num_rows != 1) {
89       char ed1[30];
90       m_msg(file, line, &mdb->errmsg, _("Insertion problem: affect_rows=%s\n"), 
91          edit_uint64(mdb->num_rows, ed1));
92       e_msg(file, line, M_FATAL, 0, mdb->errmsg);  /* ***FIXME*** remove me */
93       return 0;
94    }
95    return 1;
96 }
97
98 /* Utility routine for updates.
99  *  Returns: 0 on failure
100  *           1 on success  
101  */
102 int
103 UpdateDB(char *file, int line, B_DB *mdb, char *cmd)
104 {
105
106    if (sql_query(mdb, cmd)) {
107       m_msg(file, line, &mdb->errmsg, _("update %s failed:\n%s\n"), cmd, sql_strerror(mdb));
108       e_msg(file, line, M_ERROR, 0, mdb->errmsg);
109       e_msg(file, line, M_ERROR, 0, "%s\n", cmd);
110       return 0;
111    }
112    mdb->num_rows = sql_affected_rows(mdb);
113    if (mdb->num_rows != 1) {
114       char ed1[30];
115       m_msg(file, line, &mdb->errmsg, _("Update problem: affect_rows=%s\n"), 
116          edit_uint64(mdb->num_rows, ed1));
117       e_msg(file, line, M_ERROR, 0, mdb->errmsg);
118       e_msg(file, line, M_ERROR, 0, "%s\n", cmd);
119       return 0;
120    }
121    return 1;
122 }
123
124 /* Utility routine for deletes   
125  *
126  * Returns: -1 on error
127  *           n number of rows affected
128  */
129 int
130 DeleteDB(char *file, int line, B_DB *mdb, char *cmd)
131 {
132
133    if (sql_query(mdb, cmd)) {
134       m_msg(file, line, &mdb->errmsg, _("delete %s failed:\n%s\n"), cmd, sql_strerror(mdb));
135       e_msg(file, line, M_ERROR, 0, mdb->errmsg);
136       return -1;
137    }
138    return sql_affected_rows(mdb);
139 }
140
141
142 /*
143  * Get record max. Query is already in mdb->cmd
144  *  No locking done
145  *
146  * Returns: -1 on failure
147  *          count on success
148  */
149 int get_sql_record_max(B_DB *mdb)
150 {
151    SQL_ROW row;
152    int stat = 0;
153
154    if (QUERY_DB(mdb, mdb->cmd)) {
155       if ((row = sql_fetch_row(mdb)) == NULL) {
156          Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
157          stat = -1;
158       } else {
159          stat = atoi(row[0]);
160       }
161       sql_free_result(mdb);
162    } else {
163       Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
164       stat = -1;
165    }
166    return stat;
167 }
168
169 char *db_strerror(B_DB *mdb)
170 {
171    return mdb->errmsg;
172 }
173
174 #endif /* HAVE_MYSQL | HAVE_SQLITE */