2 * Bacula Catalog Database routines specific to MySQL
3 * These are MySQL specific routines -- hopefully all
4 * other files are generic.
6 * Kern Sibbald, March 2000
12 Copyright (C) 2000-2003 Kern Sibbald and John Walker
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.
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.
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,
32 /* The following is necessary so that we do not include
33 * the dummy external definition of DB.
35 #define __SQL_C /* indicate that this is sql.c */
42 /* -----------------------------------------------------------------------
44 * MySQL dependent defines and subroutines
46 * -----------------------------------------------------------------------
49 /* List of open databases */
50 static BQUEUE db_list = {&db_list, &db_list};
52 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
55 * Initialize database data structure. In principal this should
56 * never have errors, or it is really fatal.
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)
64 P(mutex); /* lock DB queue */
65 /* Look to see if DB already open */
66 for (mdb=NULL; (mdb=(B_DB *)qnext(&db_list, &mdb->bq)); ) {
67 if (strcmp(mdb->db_name, db_name) == 0) {
68 Dmsg2(100, "DB REopen %d %s\n", mdb->ref_count, db_name);
71 return mdb; /* already open */
74 Dmsg0(100, "db_open first time\n");
75 mdb = (B_DB *) malloc(sizeof(B_DB));
76 memset(mdb, 0, sizeof(B_DB));
77 mdb->db_name = bstrdup(db_name);
78 mdb->db_user = bstrdup(db_user);
79 mdb->db_password = bstrdup(db_password);
81 mdb->db_address = bstrdup(db_address);
84 mdb->db_socket = bstrdup(db_socket);
86 mdb->db_port = db_port;
87 mdb->have_insert_id = TRUE;
88 mdb->errmsg = get_pool_memory(PM_EMSG); /* get error message buffer */
90 mdb->cmd = get_pool_memory(PM_EMSG); /* get command buffer */
91 mdb->cached_path = get_pool_memory(PM_FNAME);
92 mdb->cached_path_id = 0;
94 mdb->fname = get_pool_memory(PM_FNAME);
95 mdb->path = get_pool_memory(PM_FNAME);
96 mdb->esc_name = get_pool_memory(PM_FNAME);
97 qinsert(&db_list, &mdb->bq); /* put db in list */
103 * Now actually open the database. This can generate errors,
104 * which are returned in the errmsg
107 db_open_database(JCR *jcr, B_DB *mdb)
112 if (mdb->connected) {
116 mdb->connected = FALSE;
118 if ((errstat=rwl_init(&mdb->lock)) != 0) {
119 Mmsg1(&mdb->errmsg, _("Unable to initialize DB lock. ERR=%s\n"),
125 /* connect to the database */
126 #ifdef HAVE_EMBEDDED_MYSQL
127 mysql_server_init(0, NULL, NULL);
129 mysql_init(&(mdb->mysql));
130 Dmsg0(50, "mysql_init done\n");
131 mdb->db = mysql_real_connect(
132 &(mdb->mysql), /* db */
133 mdb->db_address, /* default = localhost */
134 mdb->db_user, /* login name */
135 mdb->db_password, /* password */
136 mdb->db_name, /* database name */
137 mdb->db_port, /* default port */
138 mdb->db_socket, /* default = socket */
139 CLIENT_FOUND_ROWS); /* flags */
141 /* If no connect, try once more in case it is a timing problem */
142 if (mdb->db == NULL) {
143 mdb->db = mysql_real_connect(
144 &(mdb->mysql), /* db */
145 mdb->db_address, /* default = localhost */
146 mdb->db_user, /* login name */
147 mdb->db_password, /* password */
148 mdb->db_name, /* database name */
149 mdb->db_port, /* default port */
150 mdb->db_socket, /* default = socket */
151 CLIENT_FOUND_ROWS); /* flags */
154 Dmsg0(50, "mysql_real_connect done\n");
155 Dmsg3(50, "db_user=%s db_name=%s db_password=%s\n", mdb->db_user, mdb->db_name,
156 mdb->db_password==NULL?"(NULL)":mdb->db_password);
158 if (mdb->db == NULL) {
159 Mmsg2(&mdb->errmsg, _("Unable to connect to MySQL server. \n\
160 Database=%s User=%s\n\
161 It is probably not running or your password is incorrect.\n"),
162 mdb->db_name, mdb->db_user);
167 if (!check_tables_version(jcr, mdb)) {
169 db_close_database(jcr, mdb);
175 mdb->connected = TRUE;
181 db_close_database(JCR *jcr, B_DB *mdb)
186 if (mdb->ref_count == 0) {
188 if (mdb->connected && mdb->db) {
190 #ifdef HAVE_EMBEDDED_MYSQL
194 rwl_destroy(&mdb->lock);
195 free_pool_memory(mdb->errmsg);
196 free_pool_memory(mdb->cmd);
197 free_pool_memory(mdb->cached_path);
198 free_pool_memory(mdb->fname);
199 free_pool_memory(mdb->path);
200 free_pool_memory(mdb->esc_name);
207 if (mdb->db_password) {
208 free(mdb->db_password);
210 if (mdb->db_address) {
211 free(mdb->db_address);
213 if (mdb->db_socket) {
214 free(mdb->db_socket);
222 * Return the next unique index (auto-increment) for
223 * the given table. Return NULL on error.
225 * For MySQL, NULL causes the auto-increment value
228 int db_next_index(JCR *jcr, B_DB *mdb, char *table, char *index)
230 strcpy(index, "NULL");
236 * Escape strings so that MySQL is happy
238 * NOTE! len is the length of the old string. Your new
239 * string must be long enough (max 2*old) to hold
240 * the escaped output.
243 db_escape_string(char *snew, char *old, int len)
245 mysql_escape_string(snew, old, len);
249 /* Should use mysql_real_escape_string ! */
250 unsigned long mysql_real_escape_string(MYSQL *mysql, char *to, const char *from, unsigned long length);
302 * Submit a general SQL command (cmd), and for each row returned,
303 * the sqlite_handler is called with the ctx.
305 int db_sql_query(B_DB *mdb, char *query, DB_RESULT_HANDLER *result_handler, void *ctx)
310 if (sql_query(mdb, query) != 0) {
311 Mmsg(&mdb->errmsg, _("Query failed: %s: ERR=%s\n"), query, sql_strerror(mdb));
315 if (result_handler != NULL) {
316 if ((mdb->result = sql_store_result(mdb)) != NULL) {
317 int num_fields = sql_num_fields(mdb);
319 while ((row = sql_fetch_row(mdb)) != NULL) {
320 if (result_handler(ctx, num_fields, row))
324 sql_free_result(mdb);
334 list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx)
339 sql_field_seek(mdb, 0);
341 for (i = 0; i < sql_num_fields(mdb); i++) {
342 field = sql_fetch_field(mdb);
343 for (j = 0; j < field->max_length + 2; j++)
351 * If full_list is set, we list vertically, otherwise, we
352 * list on one line horizontally.
355 list_result(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type type)
359 unsigned int i, col_len, max_len = 0;
360 char buf[2000], ewc[30];
362 if (mdb->result == NULL) {
363 send(ctx, _("No results to list.\n"));
366 /* determine column display widths */
367 sql_field_seek(mdb, 0);
368 for (i = 0; i < sql_num_fields(mdb); i++) {
369 field = sql_fetch_field(mdb);
370 col_len = strlen(field->name);
371 if (type == VERT_LIST) {
372 if (col_len > max_len) {
376 if (IS_NUM(field->type) && field->max_length > 0) { /* fixup for commas */
377 field->max_length += (field->max_length - 1) / 3;
379 if (col_len < field->max_length) {
380 col_len = field->max_length;
382 if (col_len < 4 && !IS_NOT_NULL(field->flags)) {
383 col_len = 4; /* 4 = length of the word "NULL" */
385 field->max_length = col_len; /* reset column info */
389 if (type == VERT_LIST) {
393 list_dashes(mdb, send, ctx);
395 sql_field_seek(mdb, 0);
396 for (i = 0; i < sql_num_fields(mdb); i++) {
397 field = sql_fetch_field(mdb);
398 bsnprintf(buf, sizeof(buf), " %-*s |", (int)field->max_length, field->name);
402 list_dashes(mdb, send, ctx);
404 while ((row = sql_fetch_row(mdb)) != NULL) {
405 sql_field_seek(mdb, 0);
407 for (i = 0; i < sql_num_fields(mdb); i++) {
408 field = sql_fetch_field(mdb);
409 if (row[i] == NULL) {
410 bsnprintf(buf, sizeof(buf), " %-*s |", (int)field->max_length, "NULL");
411 } else if (IS_NUM(field->type)) {
412 bsnprintf(buf, sizeof(buf), " %*s |", (int)field->max_length,
413 add_commas(row[i], ewc));
415 bsnprintf(buf, sizeof(buf), " %-*s |", (int)field->max_length, row[i]);
421 list_dashes(mdb, send, ctx);
426 while ((row = sql_fetch_row(mdb)) != NULL) {
427 sql_field_seek(mdb, 0);
428 for (i = 0; i < sql_num_fields(mdb); i++) {
429 field = sql_fetch_field(mdb);
430 if (row[i] == NULL) {
431 bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, "NULL");
432 } else if (IS_NUM(field->type)) {
433 bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name,
434 add_commas(row[i], ewc));
436 bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, row[i]);
446 #endif /* HAVE_MYSQL */