]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/mysql.c
This commit was manufactured by cvs2svn to create tag
[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
9 /*
10    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29
30 /* The following is necessary so that we do not include
31  * the dummy external definition of DB.
32  */
33 #define __SQL_C                       /* indicate that this is sql.c */
34
35 #include "bacula.h"
36 #include "cats.h"
37
38 #ifdef HAVE_MYSQL
39
40 /* -----------------------------------------------------------------------
41  *
42  *   MySQL dependent defines and subroutines
43  *
44  * -----------------------------------------------------------------------
45  */
46
47 /* List of open databases */
48 static BQUEUE db_list = {&db_list, &db_list};
49
50 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
51
52 /*
53  * Initialize database data structure. In principal this should
54  * never have errors, or it is really fatal.
55  */
56 B_DB *
57 db_init_database(char *db_name, char *db_user, char *db_password)
58 {
59    B_DB *mdb;
60
61    P(mutex);                          /* lock DB queue */
62    /* Look to see if DB already open */
63    for (mdb=NULL; (mdb=(B_DB *)qnext(&db_list, &mdb->bq)); ) {
64       if (strcmp(mdb->db_name, db_name) == 0) {
65          Dmsg2(100, "DB REopen %d %s\n", mdb->ref_count, db_name);
66          mdb->ref_count++;
67          V(mutex);
68          return mdb;                  /* already open */
69       }
70    }
71    Dmsg0(100, "db_open first time\n");
72    mdb = (B_DB *) malloc(sizeof(B_DB));
73    memset(mdb, 0, sizeof(B_DB));
74    mdb->db_name = bstrdup(db_name);
75    mdb->db_user = bstrdup(db_user);
76    mdb->db_password = bstrdup(db_password);
77    mdb->have_insert_id = TRUE;
78    mdb->errmsg = (char *) get_pool_memory(PM_EMSG); /* get error message buffer */
79    *mdb->errmsg = 0;
80    mdb->cmd = (char *) get_pool_memory(PM_EMSG);    /* get command buffer */
81    mdb->ref_count = 1;
82    qinsert(&db_list, &mdb->bq);            /* put db in list */
83    V(mutex);
84    return mdb;
85 }
86
87 /*
88  * Now actually open the database.  This can generate errors,
89  * which are returned in the errmsg
90  */
91 int
92 db_open_database(B_DB *mdb)
93 {
94    P(mutex);
95    if (mdb->connected) {
96       V(mutex);
97       return 1;
98    }
99    mdb->connected = FALSE;
100    if (pthread_mutex_init(&mdb->mutex, NULL) != 0) {
101       Mmsg1(&mdb->errmsg, "Unable to initialize DB mutex. ERR=%s\n", strerror(errno));
102       V(mutex);
103       return 0;
104    }
105
106    /* connect to the database */
107    mysql_init(&(mdb->mysql));
108    Dmsg0(50, "mysql_init done\n");
109    mdb->db = mysql_real_connect(
110         &(mdb->mysql),                /* db */
111         NULL,                         /* default = localhost */
112         mdb->db_user,                 /*  login name */
113         mdb->db_password,             /*  password */
114         mdb->db_name,                 /* database name */
115         0,                            /* default port */
116         NULL,                         /* default = socket */
117         0);                           /* flags = none */
118
119    /* If no connect, try once more incase it is a timing problem */
120    if (mdb->db == NULL) {
121       mdb->db = mysql_real_connect(
122            &(mdb->mysql),                /* db */
123            NULL,                         /* default = localhost */
124            mdb->db_user,                 /*  login name */
125            mdb->db_password,             /*  password */
126            mdb->db_name,                 /* database name */
127            0,                            /* default port */
128            NULL,                         /* default = socket */
129            0);                           /* flags = none */
130    }
131     
132    Dmsg0(50, "mysql_real_connect done\n");
133    Dmsg3(50, "db_user=%s db_name=%s db_password=%s\n", mdb->db_user, mdb->db_name, 
134             mdb->db_password==NULL?"(NULL)":mdb->db_password);
135   
136    if (mdb->db == NULL) {
137       Mmsg2(&mdb->errmsg, _("Unable to connect to MySQL server. \n\
138 Database=%s User=%s\n\
139 It is probably not running or your password is incorrect.\n"), 
140          mdb->db_name, mdb->db_user);
141       V(mutex);
142       return 0;
143    }
144
145    if (!check_tables_version(mdb)) {
146       V(mutex);
147       return 0;
148    }
149
150    mdb->connected = TRUE;
151    V(mutex);
152    return 1;
153 }
154
155 void
156 db_close_database(B_DB *mdb)
157 {
158    P(mutex);
159    mdb->ref_count--;
160    if (mdb->ref_count == 0) {
161       qdchain(&mdb->bq);
162       if (mdb->connected && mdb->db) {
163          sql_close(mdb);
164       }
165       pthread_mutex_destroy(&mdb->mutex);
166       free_pool_memory(mdb->errmsg);
167       free_pool_memory(mdb->cmd);
168       if (mdb->db_name) {
169          free(mdb->db_name);
170       }
171       if (mdb->db_user) {
172          free(mdb->db_user);
173       }
174       if (mdb->db_password) {
175          free(mdb->db_password);
176       }
177       free(mdb);
178    }
179    V(mutex);
180 }
181
182 /*
183  * Return the next unique index (auto-increment) for
184  * the given table.  Return NULL on error.
185  *  
186  * For MySQL, NULL causes the auto-increment value
187  *  to be updated.
188  */
189 char *db_next_index(B_DB *mdb, char *table)
190 {
191    return "NULL";
192 }   
193
194
195
196 void
197 db_escape_string(char *snew, char *old, int len)
198 {
199    mysql_escape_string(snew, old, len);
200 }
201
202 /*
203  * Submit a general SQL command (cmd), and for each row returned,
204  *  the sqlite_handler is called with the ctx.
205  */
206 int db_sql_query(B_DB *mdb, char *query, DB_RESULT_HANDLER *result_handler, void *ctx)
207 {
208    SQL_ROW row;
209   
210    P(mdb->mutex);
211    if (sql_query(mdb, query) != 0) {
212       Mmsg(&mdb->errmsg, _("Query failed: %s: ERR=%s\n"), query, sql_strerror(mdb));
213       V(mdb->mutex);
214       return 0;
215    }
216    if (result_handler != NULL) {
217       if ((mdb->result = sql_store_result(mdb)) != NULL) {
218          int num_fields = sql_num_fields(mdb);
219
220          while ((row = sql_fetch_row(mdb)) != NULL) {
221             if (result_handler(ctx, num_fields, row))
222                break;
223          }
224
225          sql_free_result(mdb);
226       }
227    }
228    V(mdb->mutex);
229    return 1;
230
231 }
232
233
234 static void
235 list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx)
236 {
237    SQL_FIELD  *field;
238    unsigned int i, j;
239
240    sql_field_seek(mdb, 0);
241    send(ctx, "+");
242    for (i = 0; i < sql_num_fields(mdb); i++) {
243       field = sql_fetch_field(mdb);
244       for (j = 0; j < field->max_length + 2; j++)
245               send(ctx, "-");
246       send(ctx, "+");
247    }
248    send(ctx, "\n");
249 }
250
251 void
252 list_result(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx)
253 {
254    SQL_FIELD *field;
255    SQL_ROW    row;
256    unsigned int i, col_len;
257    char buf[2000], ewc[30];
258
259    if (mdb->result == NULL) {
260       return;
261    }
262    /* determine column display widths */
263    sql_field_seek(mdb, 0);
264    for (i = 0; i < sql_num_fields(mdb); i++) {
265       field = sql_fetch_field(mdb);
266       if (IS_NUM(field->type) && field->max_length > 0) { /* fixup for commas */
267          field->max_length += (field->max_length - 1) / 3;
268       }
269       col_len = strlen(field->name);
270       if (col_len < field->max_length)
271               col_len = field->max_length;
272       if (col_len < 4 && !IS_NOT_NULL(field->flags))
273               col_len = 4;    /* 4 = length of the word "NULL" */
274       field->max_length = col_len;    /* reset column info */
275    }
276
277    list_dashes(mdb, send, ctx);
278    send(ctx, "|");
279    sql_field_seek(mdb, 0);
280    for (i = 0; i < sql_num_fields(mdb); i++) {
281       field = sql_fetch_field(mdb);
282       sprintf(buf, " %-*s |", field->max_length, field->name);
283       send(ctx, buf);
284    }
285    send(ctx, "\n");
286    list_dashes(mdb, send, ctx);
287
288    while ((row = sql_fetch_row(mdb)) != NULL) {
289       sql_field_seek(mdb, 0);
290       send(ctx, "|");
291       for (i = 0; i < sql_num_fields(mdb); i++) {
292          field = sql_fetch_field(mdb);
293          if (row[i] == NULL) {
294             sprintf(buf, " %-*s |", field->max_length, "NULL");
295          } else if (IS_NUM(field->type)) {
296             sprintf(buf, " %*s |", field->max_length,       
297                add_commas(row[i], ewc));
298          } else {
299             sprintf(buf, " %-*s |", field->max_length, row[i]);
300          }
301          send(ctx, buf);
302       }
303       send(ctx, "\n");
304    }
305    list_dashes(mdb, send, ctx);
306 }
307
308
309 #endif /* HAVE_MYSQL */