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