]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql.c
1.19 24Apr02
[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       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 */
84       return 0;
85    }
86    return 1;
87 }
88
89 /* Utility routine for updates.
90  *  Returns: 0 on failure
91  *           1 on success  
92  */
93 int
94 UpdateDB(char *file, int line, B_DB *mdb, char *cmd)
95 {
96
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);
101       return 0;
102    }
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);
108       return 0;
109    }
110    return 1;
111 }
112
113 /* Utility routine for deletes   
114  *
115  * Returns: -1 on error
116  *           n number of rows affected
117  */
118 int
119 DeleteDB(char *file, int line, B_DB *mdb, char *cmd)
120 {
121
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);
125       return -1;
126    }
127    return sql_affected_rows(mdb);
128 }
129
130
131 /*
132  * Get record max. Query is already in mdb->cmd
133  *  No locking done
134  *
135  * Returns: -1 on failure
136  *          count on success
137  */
138 int get_sql_record_max(B_DB *mdb)
139 {
140    SQL_ROW row;
141    int stat = 0;
142
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));
146          stat = -1;
147       } else {
148          stat = atoi(row[0]);
149       }
150       sql_free_result(mdb);
151    } else {
152       Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
153       stat = -1;
154    }
155    return stat;
156 }
157
158 char *db_strerror(B_DB *mdb)
159 {
160    return mdb->errmsg;
161 }
162
163 #endif /* HAVE_MYSQL | HAVE_SQLITE */