]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/mysql.c
Add second COALESCE to sql_find -- thanks to JML
[bacula/bacula] / bacula / src / cats / mysql.c
1 /*
2  * Bacula Catalog Database routines specific to MySQL
3  *   These are MySQL specific routines -- hopefully all
4  *    other files are generic.
5  *
6  *    Kern Sibbald, March 2000
7  *
8  *    Version $Id$
9  */
10
11 /*
12    Copyright (C) 2000-2004 Kern Sibbald and John Walker
13
14    This program is free software; you can redistribute it and/or
15    modify it under the terms of the GNU General Public License as
16    published by the Free Software Foundation; either version 2 of
17    the License, or (at your option) any later version.
18
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22    General Public License for more details.
23
24    You should have received a copy of the GNU General Public
25    License along with this program; if not, write to the Free
26    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27    MA 02111-1307, USA.
28
29  */
30
31
32 /* The following is necessary so that we do not include
33  * the dummy external definition of DB.
34  */
35 #define __SQL_C                       /* indicate that this is sql.c */
36
37 #include "bacula.h"
38 #include "cats.h"
39
40 #ifdef HAVE_MYSQL
41
42 /* -----------------------------------------------------------------------
43  *
44  *   MySQL dependent defines and subroutines
45  *
46  * -----------------------------------------------------------------------
47  */
48
49 /* List of open databases */
50 static BQUEUE db_list = {&db_list, &db_list};
51
52 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
53
54 /*
55  * Initialize database data structure. In principal this should
56  * never have errors, or it is really fatal.
57  */
58 B_DB *
59 db_init_database(JCR *jcr, char *db_name, char *db_user, char *db_password, 
60                  char *db_address, int db_port, char *db_socket) 
61 {
62    B_DB *mdb;
63
64    if (!db_user) {              
65       Jmsg(jcr, M_FATAL, 0, _("A user name for MySQL must be supplied.\n"));
66       return NULL;
67    }
68    P(mutex);                          /* lock DB queue */
69    /* Look to see if DB already open */
70    for (mdb=NULL; (mdb=(B_DB *)qnext(&db_list, &mdb->bq)); ) {
71       if (strcmp(mdb->db_name, db_name) == 0) {
72          Dmsg2(100, "DB REopen %d %s\n", mdb->ref_count, db_name);
73          mdb->ref_count++;
74          V(mutex);
75          return mdb;                  /* already open */
76       }
77    }
78    Dmsg0(100, "db_open first time\n");
79    mdb = (B_DB *) malloc(sizeof(B_DB));
80    memset(mdb, 0, sizeof(B_DB));
81    mdb->db_name = bstrdup(db_name);
82    mdb->db_user = bstrdup(db_user);
83    if (db_password) {
84       mdb->db_password = bstrdup(db_password);
85    }
86    if (db_address) {
87       mdb->db_address = bstrdup(db_address);
88    }
89    if (db_socket) {
90       mdb->db_socket = bstrdup(db_socket);
91    }
92    mdb->db_port = db_port;
93    mdb->have_insert_id = TRUE;
94    mdb->errmsg = get_pool_memory(PM_EMSG); /* get error message buffer */
95    *mdb->errmsg = 0;
96    mdb->cmd = get_pool_memory(PM_EMSG);    /* get command buffer */
97    mdb->cached_path = get_pool_memory(PM_FNAME);
98    mdb->cached_path_id = 0;
99    mdb->ref_count = 1;
100    mdb->fname = get_pool_memory(PM_FNAME);
101    mdb->path = get_pool_memory(PM_FNAME);
102    mdb->esc_name = get_pool_memory(PM_FNAME);
103    qinsert(&db_list, &mdb->bq);            /* put db in list */
104    V(mutex);
105    return mdb;
106 }
107
108 /*
109  * Now actually open the database.  This can generate errors,
110  *  which are returned in the errmsg
111  *
112  * DO NOT close the database or free(mdb) here !!!!
113  */
114 int
115 db_open_database(JCR *jcr, B_DB *mdb)
116 {
117    int errstat;
118
119    P(mutex);
120    if (mdb->connected) {
121       V(mutex);
122       return 1;
123    }
124    mdb->connected = FALSE;
125
126    if ((errstat=rwl_init(&mdb->lock)) != 0) {
127       Mmsg1(&mdb->errmsg, _("Unable to initialize DB lock. ERR=%s\n"), 
128             strerror(errstat));
129       V(mutex);
130       return 0;
131    }
132
133    /* connect to the database */
134 #ifdef HAVE_EMBEDDED_MYSQL
135    mysql_server_init(0, NULL, NULL);
136 #endif
137    mysql_init(&(mdb->mysql));
138    Dmsg0(50, "mysql_init done\n");
139    mdb->db = mysql_real_connect(
140         &(mdb->mysql),                /* db */
141         mdb->db_address,              /* default = localhost */
142         mdb->db_user,                 /*  login name */
143         mdb->db_password,             /*  password */
144         mdb->db_name,                 /* database name */
145         mdb->db_port,                 /* default port */
146         mdb->db_socket,               /* default = socket */
147         CLIENT_FOUND_ROWS);           /* flags */
148
149    /* If no connect, try once more in case it is a timing problem */
150    if (mdb->db == NULL) {
151       mdb->db = mysql_real_connect(
152            &(mdb->mysql),                /* db */
153            mdb->db_address,              /* default = localhost */
154            mdb->db_user,                 /*  login name */
155            mdb->db_password,             /*  password */
156            mdb->db_name,                 /* database name */
157            mdb->db_port,                 /* default port */
158            mdb->db_socket,               /* default = socket */
159            CLIENT_FOUND_ROWS);           /* flags */
160    }
161     
162    Dmsg0(50, "mysql_real_connect done\n");
163    Dmsg3(50, "db_user=%s db_name=%s db_password=%s\n", mdb->db_user, mdb->db_name, 
164             mdb->db_password==NULL?"(NULL)":mdb->db_password);
165   
166    if (mdb->db == NULL) {
167       Mmsg2(&mdb->errmsg, _("Unable to connect to MySQL server. \n\
168 Database=%s User=%s\n\
169 It is probably not running or your password is incorrect.\n"), 
170          mdb->db_name, mdb->db_user);
171       V(mutex);
172       return 0;
173    }
174
175    if (!check_tables_version(jcr, mdb)) {
176       V(mutex);
177       return 0;
178    }
179
180 #ifdef HAVE_TREAD_SAFE_MYSQL
181    my_thread_init();
182 #endif
183
184    mdb->connected = TRUE;
185    V(mutex);
186    return 1;
187 }
188
189 void
190 db_close_database(JCR *jcr, B_DB *mdb)
191 {
192    if (!mdb) {
193       return;
194    }
195    P(mutex);
196    mdb->ref_count--;
197 #ifdef HAVE_TREAD_SAFE_MYSQL
198    my_thread_end();
199 #endif
200    if (mdb->ref_count == 0) {
201       qdchain(&mdb->bq);
202       if (mdb->connected && mdb->db) {
203          sql_close(mdb);
204 #ifdef HAVE_EMBEDDED_MYSQL
205          mysql_server_end();
206 #endif
207       }
208       rwl_destroy(&mdb->lock);       
209       free_pool_memory(mdb->errmsg);
210       free_pool_memory(mdb->cmd);
211       free_pool_memory(mdb->cached_path);
212       free_pool_memory(mdb->fname);
213       free_pool_memory(mdb->path);
214       free_pool_memory(mdb->esc_name);
215       if (mdb->db_name) {
216          free(mdb->db_name);
217       }
218       if (mdb->db_user) {
219          free(mdb->db_user);
220       }
221       if (mdb->db_password) {
222          free(mdb->db_password);
223       }
224       if (mdb->db_address) {
225          free(mdb->db_address);
226       }
227       if (mdb->db_socket) {
228          free(mdb->db_socket);
229       }
230       free(mdb);
231    }
232    V(mutex);
233 }
234
235 /*
236  * Return the next unique index (auto-increment) for
237  * the given table.  Return NULL on error.
238  *  
239  * For MySQL, NULL causes the auto-increment value
240  *  to be updated.
241  */
242 int db_next_index(JCR *jcr, B_DB *mdb, char *table, char *index)
243 {
244    strcpy(index, "NULL");
245    return 1;
246 }   
247
248
249 /*
250  * Escape strings so that MySQL is happy
251  *
252  *   NOTE! len is the length of the old string. Your new
253  *         string must be long enough (max 2*old+1) to hold
254  *         the escaped output.
255  */
256 void
257 db_escape_string(char *snew, char *old, int len)
258 {
259    mysql_escape_string(snew, old, len);
260
261 #ifdef DO_IT_MYSELF
262
263 /* Should use mysql_real_escape_string ! */
264 unsigned long mysql_real_escape_string(MYSQL *mysql, char *to, const char *from, unsigned long length);
265
266    char *n, *o;
267
268    n = snew;
269    o = old;
270    while (len--) {
271       switch (*o) {
272       case 0:
273          *n++= '\\';
274          *n++= '0';
275          o++;
276          break;
277       case '\n':
278          *n++= '\\';
279          *n++= 'n';
280          o++;
281          break;
282       case '\r':
283          *n++= '\\';
284          *n++= 'r';
285          o++;
286          break;
287       case '\\':
288          *n++= '\\';
289          *n++= '\\';
290          o++;
291          break;
292       case '\'':
293          *n++= '\\';
294          *n++= '\'';
295          o++;
296          break;
297       case '"':
298          *n++= '\\';
299          *n++= '"';
300          o++;
301          break;
302       case '\032':
303          *n++= '\\';
304          *n++= 'Z';
305          o++;
306          break;
307       default:
308          *n++= *o++;
309       }
310    }
311    *n = 0;
312 #endif
313 }
314
315 /*
316  * Submit a general SQL command (cmd), and for each row returned,
317  *  the sqlite_handler is called with the ctx.
318  */
319 int db_sql_query(B_DB *mdb, char *query, DB_RESULT_HANDLER *result_handler, void *ctx)
320 {
321    SQL_ROW row;
322   
323    db_lock(mdb);
324    if (sql_query(mdb, query) != 0) {
325       Mmsg(&mdb->errmsg, _("Query failed: %s: ERR=%s\n"), query, sql_strerror(mdb));
326       db_unlock(mdb);
327       return 0;
328    }
329    if (result_handler != NULL) {
330       if ((mdb->result = sql_store_result(mdb)) != NULL) {
331          int num_fields = sql_num_fields(mdb);
332
333          while ((row = sql_fetch_row(mdb)) != NULL) {
334             if (result_handler(ctx, num_fields, row))
335                break;
336          }
337
338          sql_free_result(mdb);
339       }
340    }
341    db_unlock(mdb);
342    return 1;
343
344 }
345
346 #endif /* HAVE_MYSQL */