]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/mysql.c
a35e21d6c8c73d9d95489481bdf870be8645473a
[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, 2001, 2002 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(char *db_name, char *db_user, char *db_password)
60 {
61    B_DB *mdb;
62
63    P(mutex);                          /* lock DB queue */
64    /* Look to see if DB already open */
65    for (mdb=NULL; (mdb=(B_DB *)qnext(&db_list, &mdb->bq)); ) {
66       if (strcmp(mdb->db_name, db_name) == 0) {
67          Dmsg2(100, "DB REopen %d %s\n", mdb->ref_count, db_name);
68          mdb->ref_count++;
69          V(mutex);
70          return mdb;                  /* already open */
71       }
72    }
73    Dmsg0(100, "db_open first time\n");
74    mdb = (B_DB *) malloc(sizeof(B_DB));
75    memset(mdb, 0, sizeof(B_DB));
76    mdb->db_name = bstrdup(db_name);
77    mdb->db_user = bstrdup(db_user);
78    mdb->db_password = bstrdup(db_password);
79    mdb->have_insert_id = TRUE;
80    mdb->errmsg = get_pool_memory(PM_EMSG); /* get error message buffer */
81    *mdb->errmsg = 0;
82    mdb->cmd = get_pool_memory(PM_EMSG);    /* get command buffer */
83    mdb->ref_count = 1;
84    qinsert(&db_list, &mdb->bq);            /* put db in list */
85    V(mutex);
86    return mdb;
87 }
88
89 /*
90  * Now actually open the database.  This can generate errors,
91  * which are returned in the errmsg
92  */
93 int
94 db_open_database(B_DB *mdb)
95 {
96    P(mutex);
97    if (mdb->connected) {
98       V(mutex);
99       return 1;
100    }
101    mdb->connected = FALSE;
102 #ifdef needed
103    if (pthread_mutex_init(&mdb->mutex, NULL) != 0) {
104       Mmsg1(&mdb->errmsg, "Unable to initialize DB mutex. ERR=%s\n", strerror(errno));
105       V(mutex);
106       return 0;
107    }
108 #endif
109
110    if (rwl_init(&mdb->lock) != 0) {
111       Mmsg1(&mdb->errmsg, "Unable to initialize DB lock. ERR=%s\n", strerror(errno));
112       V(mutex);
113       return 0;
114    }
115
116    /* connect to the database */
117    mysql_init(&(mdb->mysql));
118    Dmsg0(50, "mysql_init done\n");
119    mdb->db = mysql_real_connect(
120         &(mdb->mysql),                /* db */
121         NULL,                         /* default = localhost */
122         mdb->db_user,                 /*  login name */
123         mdb->db_password,             /*  password */
124         mdb->db_name,                 /* database name */
125         0,                            /* default port */
126         NULL,                         /* default = socket */
127         0);                           /* flags = none */
128
129    /* If no connect, try once more incase it is a timing problem */
130    if (mdb->db == NULL) {
131       mdb->db = mysql_real_connect(
132            &(mdb->mysql),                /* db */
133            NULL,                         /* default = localhost */
134            mdb->db_user,                 /*  login name */
135            mdb->db_password,             /*  password */
136            mdb->db_name,                 /* database name */
137            0,                            /* default port */
138            NULL,                         /* default = socket */
139            0);                           /* flags = none */
140    }
141     
142    Dmsg0(50, "mysql_real_connect done\n");
143    Dmsg3(50, "db_user=%s db_name=%s db_password=%s\n", mdb->db_user, mdb->db_name, 
144             mdb->db_password==NULL?"(NULL)":mdb->db_password);
145   
146    if (mdb->db == NULL) {
147       Mmsg2(&mdb->errmsg, _("Unable to connect to MySQL server. \n\
148 Database=%s User=%s\n\
149 It is probably not running or your password is incorrect.\n"), 
150          mdb->db_name, mdb->db_user);
151       V(mutex);
152       return 0;
153    }
154
155    if (!check_tables_version(mdb)) {
156       V(mutex);
157       return 0;
158    }
159
160    mdb->connected = TRUE;
161    V(mutex);
162    return 1;
163 }
164
165 void
166 db_close_database(B_DB *mdb)
167 {
168    P(mutex);
169    mdb->ref_count--;
170    if (mdb->ref_count == 0) {
171       qdchain(&mdb->bq);
172       if (mdb->connected && mdb->db) {
173          sql_close(mdb);
174       }
175 /*    pthread_mutex_destroy(&mdb->mutex); */
176       rwl_destroy(&mdb->lock);       
177       free_pool_memory(mdb->errmsg);
178       free_pool_memory(mdb->cmd);
179       if (mdb->db_name) {
180          free(mdb->db_name);
181       }
182       if (mdb->db_user) {
183          free(mdb->db_user);
184       }
185       if (mdb->db_password) {
186          free(mdb->db_password);
187       }
188       free(mdb);
189    }
190    V(mutex);
191 }
192
193 /*
194  * Return the next unique index (auto-increment) for
195  * the given table.  Return NULL on error.
196  *  
197  * For MySQL, NULL causes the auto-increment value
198  *  to be updated.
199  */
200 char *db_next_index(B_DB *mdb, char *table)
201 {
202    return "NULL";
203 }   
204
205
206
207 void
208 db_escape_string(char *snew, char *old, int len)
209 {
210    mysql_escape_string(snew, old, len);
211 }
212
213 /*
214  * Submit a general SQL command (cmd), and for each row returned,
215  *  the sqlite_handler is called with the ctx.
216  */
217 int db_sql_query(B_DB *mdb, char *query, DB_RESULT_HANDLER *result_handler, void *ctx)
218 {
219    SQL_ROW row;
220   
221    db_lock(mdb);
222    if (sql_query(mdb, query) != 0) {
223       Mmsg(&mdb->errmsg, _("Query failed: %s: ERR=%s\n"), query, sql_strerror(mdb));
224       db_unlock(mdb);
225       return 0;
226    }
227    if (result_handler != NULL) {
228       if ((mdb->result = sql_store_result(mdb)) != NULL) {
229          int num_fields = sql_num_fields(mdb);
230
231          while ((row = sql_fetch_row(mdb)) != NULL) {
232             if (result_handler(ctx, num_fields, row))
233                break;
234          }
235
236          sql_free_result(mdb);
237       }
238    }
239    db_unlock(mdb);
240    return 1;
241
242 }
243
244
245 static void
246 list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx)
247 {
248    SQL_FIELD  *field;
249    unsigned int i, j;
250
251    sql_field_seek(mdb, 0);
252    send(ctx, "+");
253    for (i = 0; i < sql_num_fields(mdb); i++) {
254       field = sql_fetch_field(mdb);
255       for (j = 0; j < field->max_length + 2; j++)
256               send(ctx, "-");
257       send(ctx, "+");
258    }
259    send(ctx, "\n");
260 }
261
262 void
263 list_result(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx)
264 {
265    SQL_FIELD *field;
266    SQL_ROW    row;
267    unsigned int i, col_len;
268    char buf[2000], ewc[30];
269
270    if (mdb->result == NULL) {
271       return;
272    }
273    /* determine column display widths */
274    sql_field_seek(mdb, 0);
275    for (i = 0; i < sql_num_fields(mdb); i++) {
276       field = sql_fetch_field(mdb);
277       if (IS_NUM(field->type) && field->max_length > 0) { /* fixup for commas */
278          field->max_length += (field->max_length - 1) / 3;
279       }
280       col_len = strlen(field->name);
281       if (col_len < field->max_length)
282               col_len = field->max_length;
283       if (col_len < 4 && !IS_NOT_NULL(field->flags))
284               col_len = 4;    /* 4 = length of the word "NULL" */
285       field->max_length = col_len;    /* reset column info */
286    }
287
288    list_dashes(mdb, send, ctx);
289    send(ctx, "|");
290    sql_field_seek(mdb, 0);
291    for (i = 0; i < sql_num_fields(mdb); i++) {
292       field = sql_fetch_field(mdb);
293       sprintf(buf, " %-*s |", field->max_length, field->name);
294       send(ctx, buf);
295    }
296    send(ctx, "\n");
297    list_dashes(mdb, send, ctx);
298
299    while ((row = sql_fetch_row(mdb)) != NULL) {
300       sql_field_seek(mdb, 0);
301       send(ctx, "|");
302       for (i = 0; i < sql_num_fields(mdb); i++) {
303          field = sql_fetch_field(mdb);
304          if (row[i] == NULL) {
305             sprintf(buf, " %-*s |", field->max_length, "NULL");
306          } else if (IS_NUM(field->type)) {
307             sprintf(buf, " %*s |", field->max_length,       
308                add_commas(row[i], ewc));
309          } else {
310             sprintf(buf, " %-*s |", field->max_length, row[i]);
311          }
312          send(ctx, buf);
313       }
314       send(ctx, "\n");
315    }
316    list_dashes(mdb, send, ctx);
317 }
318
319
320 #endif /* HAVE_MYSQL */