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, 2001, 2002 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(char *db_name, char *db_user, char *db_password)
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);
70 return mdb; /* already open */
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 */
82 mdb->cmd = get_pool_memory(PM_EMSG); /* get command buffer */
84 qinsert(&db_list, &mdb->bq); /* put db in list */
90 * Now actually open the database. This can generate errors,
91 * which are returned in the errmsg
94 db_open_database(B_DB *mdb)
101 mdb->connected = FALSE;
103 if (pthread_mutex_init(&mdb->mutex, NULL) != 0) {
104 Mmsg1(&mdb->errmsg, "Unable to initialize DB mutex. ERR=%s\n", strerror(errno));
110 if (rwl_init(&mdb->lock) != 0) {
111 Mmsg1(&mdb->errmsg, "Unable to initialize DB lock. ERR=%s\n", strerror(errno));
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 */
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 */
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);
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);
155 if (!check_tables_version(mdb)) {
160 mdb->connected = TRUE;
166 db_close_database(B_DB *mdb)
170 if (mdb->ref_count == 0) {
172 if (mdb->connected && mdb->db) {
175 /* pthread_mutex_destroy(&mdb->mutex); */
176 rwl_destroy(&mdb->lock);
177 free_pool_memory(mdb->errmsg);
178 free_pool_memory(mdb->cmd);
185 if (mdb->db_password) {
186 free(mdb->db_password);
194 * Return the next unique index (auto-increment) for
195 * the given table. Return NULL on error.
197 * For MySQL, NULL causes the auto-increment value
200 char *db_next_index(B_DB *mdb, char *table)
208 db_escape_string(char *snew, char *old, int len)
210 mysql_escape_string(snew, old, len);
214 * Submit a general SQL command (cmd), and for each row returned,
215 * the sqlite_handler is called with the ctx.
217 int db_sql_query(B_DB *mdb, char *query, DB_RESULT_HANDLER *result_handler, void *ctx)
222 if (sql_query(mdb, query) != 0) {
223 Mmsg(&mdb->errmsg, _("Query failed: %s: ERR=%s\n"), query, sql_strerror(mdb));
227 if (result_handler != NULL) {
228 if ((mdb->result = sql_store_result(mdb)) != NULL) {
229 int num_fields = sql_num_fields(mdb);
231 while ((row = sql_fetch_row(mdb)) != NULL) {
232 if (result_handler(ctx, num_fields, row))
236 sql_free_result(mdb);
246 list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx)
251 sql_field_seek(mdb, 0);
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++)
263 list_result(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx)
267 unsigned int i, col_len;
268 char buf[2000], ewc[30];
270 if (mdb->result == NULL) {
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;
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 */
288 list_dashes(mdb, 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);
297 list_dashes(mdb, send, ctx);
299 while ((row = sql_fetch_row(mdb)) != NULL) {
300 sql_field_seek(mdb, 0);
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));
310 sprintf(buf, " %-*s |", field->max_length, row[i]);
316 list_dashes(mdb, send, ctx);
320 #endif /* HAVE_MYSQL */