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
11 Bacula® - The Network Backup Solution
13 Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
15 The main author of Bacula is Kern Sibbald, with contributions from
16 many others, a complete list can be found in the file AUTHORS.
17 This program is Free Software; you can redistribute it and/or
18 modify it under the terms of version two of the GNU General Public
19 License as published by the Free Software Foundation plus additions
20 that are listed in the file LICENSE.
22 This program is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 Bacula® is a registered trademark of John Walker.
33 The licensor of Bacula is the Free Software Foundation Europe
34 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
35 Switzerland, email:ftf@fsfeurope.org.
39 /* The following is necessary so that we do not include
40 * the dummy external definition of DB.
42 #define __SQL_C /* indicate that this is sql.c */
49 /* -----------------------------------------------------------------------
51 * MySQL dependent defines and subroutines
53 * -----------------------------------------------------------------------
56 /* List of open databases */
57 static BQUEUE db_list = {&db_list, &db_list};
59 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
62 * Retrieve database type
71 * Initialize database data structure. In principal this should
72 * never have errors, or it is really fatal.
75 db_init_database(JCR *jcr, const char *db_name, const char *db_user, const char *db_password,
76 const char *db_address, int db_port, const char *db_socket,
77 int mult_db_connections)
82 Jmsg(jcr, M_FATAL, 0, _("A user name for MySQL must be supplied.\n"));
85 P(mutex); /* lock DB queue */
86 /* Look to see if DB already open */
87 if (!mult_db_connections) {
88 for (mdb=NULL; (mdb=(B_DB *)qnext(&db_list, &mdb->bq)); ) {
89 if (bstrcmp(mdb->db_name, db_name) &&
90 bstrcmp(mdb->db_address, db_address) &&
91 mdb->db_port == db_port) {
92 Dmsg2(100, "DB REopen %d %s\n", mdb->ref_count, db_name);
95 return mdb; /* already open */
99 Dmsg0(100, "db_open first time\n");
100 mdb = (B_DB *) malloc(sizeof(B_DB));
101 memset(mdb, 0, sizeof(B_DB));
102 mdb->db_name = bstrdup(db_name);
103 mdb->db_user = bstrdup(db_user);
105 mdb->db_password = bstrdup(db_password);
108 mdb->db_address = bstrdup(db_address);
111 mdb->db_socket = bstrdup(db_socket);
113 mdb->db_port = db_port;
114 mdb->have_insert_id = TRUE;
115 mdb->errmsg = get_pool_memory(PM_EMSG); /* get error message buffer */
117 mdb->cmd = get_pool_memory(PM_EMSG); /* get command buffer */
118 mdb->cached_path = get_pool_memory(PM_FNAME);
119 mdb->cached_path_id = 0;
121 mdb->fname = get_pool_memory(PM_FNAME);
122 mdb->path = get_pool_memory(PM_FNAME);
123 mdb->esc_name = get_pool_memory(PM_FNAME);
124 qinsert(&db_list, &mdb->bq); /* put db in list */
130 * Now actually open the database. This can generate errors,
131 * which are returned in the errmsg
133 * DO NOT close the database or free(mdb) here !!!!
136 db_open_database(JCR *jcr, B_DB *mdb)
141 if (mdb->connected) {
145 mdb->connected = FALSE;
147 if ((errstat=rwl_init(&mdb->lock)) != 0) {
148 Mmsg1(&mdb->errmsg, _("Unable to initialize DB lock. ERR=%s\n"),
154 /* connect to the database */
155 #ifdef HAVE_EMBEDDED_MYSQL
156 mysql_server_init(0, NULL, NULL);
158 mysql_init(&(mdb->mysql));
159 Dmsg0(50, "mysql_init done\n");
160 /* If connection fails, try at 5 sec intervals for 30 seconds. */
161 for (int retry=0; retry < 6; retry++) {
162 mdb->db = mysql_real_connect(
163 &(mdb->mysql), /* db */
164 mdb->db_address, /* default = localhost */
165 mdb->db_user, /* login name */
166 mdb->db_password, /* password */
167 mdb->db_name, /* database name */
168 mdb->db_port, /* default port */
169 mdb->db_socket, /* default = socket */
170 CLIENT_FOUND_ROWS); /* flags */
172 /* If no connect, try once more in case it is a timing problem */
173 if (mdb->db != NULL) {
179 mdb->mysql.reconnect = 1; /* so connection does not timeout */
180 Dmsg0(50, "mysql_real_connect done\n");
181 Dmsg3(50, "db_user=%s db_name=%s db_password=%s\n", mdb->db_user, mdb->db_name,
182 mdb->db_password==NULL?"(NULL)":mdb->db_password);
184 if (mdb->db == NULL) {
185 Mmsg2(&mdb->errmsg, _("Unable to connect to MySQL server. \n"
186 "Database=%s User=%s\n"
187 "It is probably not running or your password is incorrect.\n"),
188 mdb->db_name, mdb->db_user);
193 if (!check_tables_version(jcr, mdb)) {
198 #ifdef HAVE_THREAD_SAFE_MYSQL
202 mdb->connected = TRUE;
208 db_close_database(JCR *jcr, B_DB *mdb)
213 db_end_transaction(jcr, mdb);
216 #if defined(HAVE_THREAD_SAFE_MYSQL)
219 if (mdb->ref_count == 0) {
221 if (mdb->connected && mdb->db) {
223 #ifdef HAVE_EMBEDDED_MYSQL
227 rwl_destroy(&mdb->lock);
228 free_pool_memory(mdb->errmsg);
229 free_pool_memory(mdb->cmd);
230 free_pool_memory(mdb->cached_path);
231 free_pool_memory(mdb->fname);
232 free_pool_memory(mdb->path);
233 free_pool_memory(mdb->esc_name);
240 if (mdb->db_password) {
241 free(mdb->db_password);
243 if (mdb->db_address) {
244 free(mdb->db_address);
246 if (mdb->db_socket) {
247 free(mdb->db_socket);
255 * Return the next unique index (auto-increment) for
256 * the given table. Return NULL on error.
258 * For MySQL, NULL causes the auto-increment value
261 int db_next_index(JCR *jcr, B_DB *mdb, char *table, char *index)
263 strcpy(index, "NULL");
269 * Escape strings so that MySQL is happy
271 * NOTE! len is the length of the old string. Your new
272 * string must be long enough (max 2*old+1) to hold
273 * the escaped output.
276 db_escape_string(char *snew, char *old, int len)
278 mysql_escape_string(snew, old, len);
282 /* Should use mysql_real_escape_string ! */
283 unsigned long mysql_real_escape_string(MYSQL *mysql, char *to, const char *from, unsigned long length);
335 * Submit a general SQL command (cmd), and for each row returned,
336 * the sqlite_handler is called with the ctx.
338 int db_sql_query(B_DB *mdb, const char *query, DB_RESULT_HANDLER *result_handler, void *ctx)
344 if (sql_query(mdb, query) != 0) {
345 Mmsg(mdb->errmsg, _("Query failed: %s: ERR=%s\n"), query, sql_strerror(mdb));
349 if (result_handler != NULL) {
350 if ((mdb->result = sql_use_result(mdb)) != NULL) {
353 /* We *must* fetch all rows */
354 while ((row = sql_fetch_row(mdb)) != NULL) {
356 /* the result handler returns 1 when it has
357 * seen all the data it wants. However, we
358 * loop to the end of the data.
361 if (result_handler(ctx, num_fields, row)) {
367 sql_free_result(mdb);
375 #endif /* HAVE_MYSQL */