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