midgard/select_db.h

00001 /* $Id: select_db.h,v 1.11 2005/04/07 09:15:55 piotras Exp $
00002  *
00003  * midgard/select_db.h: database switching helper routines
00004  *
00005  * Copyright (C) 1999 Jukka Zitting <jukka.zitting@iki.fi>
00006  * Copyright (C) 2000 The Midgard Project ry
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Library General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Library General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this library; see the file COPYING.  If not, write to
00020  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00021  * Boston, MA 02111-1307, USA.
00022  */
00023 #ifndef MGD_SELECT_DB_H
00024 #define MGD_SELECT_DB_H
00025 
00026 /* EEH placed here to avoid superlong log lines in apaches error log */
00027 #line 27 "select_db.h"
00028 #include "midgard/midgard.h"
00029 
00030 static int mgd_select_database(request_rec *r, midgard_database_connection *dbc,
00031       midgard_request_config * rcfg, int assert_conn)
00032 {
00033    if (dbc && dbc == rcfg->database.current && dbc->handle && dbc->handle->mgd) {
00034                 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, 
00035          "Midgard: select_database kept current database %s",
00036          dbc->name);
00037       rcfg->mgd = dbc->handle->mgd;
00038 
00039       return assert_conn
00040          ? mgd_assert_db_connection(rcfg->mgd,
00041             dbc->handle->hostname, dbc->name, dbc->handle->username, dbc->handle->password)
00042          : 1;
00043    }
00044 
00045    if (dbc->handle->current != dbc || dbc->handle->mgd == NULL) {
00046       if (dbc->handle->mgd == NULL) {
00047 //         dbc->handle->mgd = mgd_connect(
00048          dbc->handle->mgd = mgd_connect(dbc->handle->hostname,
00049             dbc->name, dbc->handle->username, dbc->handle->password);
00050 
00051          if (dbc->handle->mgd != NULL)  {
00052            g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
00053                "Midgard: select_database opened database %s",
00054                dbc->name);
00055          }
00056          else {
00057            g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
00058                "Midgard: failed to open database '%s' for user '%s'",
00059                   dbc->name, dbc->handle->username);
00060             return 0;
00061          }
00062       }
00063       else {
00064          if (mgd_select_db(dbc->handle->mgd, dbc->name)
00065                || (mgd_assert_db_connection(dbc->handle->mgd,
00066                      dbc->handle->hostname, dbc->name, dbc->handle->username, dbc->handle->password)
00067                   && mgd_select_db(dbc->handle->mgd, dbc->name))) {
00068            g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
00069                 "Midgard: switching to %s", dbc->name);
00070          }
00071          else {
00072            g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
00073                "Midgard: switching to %s failed", dbc->name);
00074             /* DONT GIVE UP try disconnect and reconnect!*/  
00075              mgd_close( dbc->handle->mgd);
00076              return assert_conn
00077                   ? mgd_assert_db_connection(rcfg->mgd,
00078                     dbc->handle->hostname, dbc->name, dbc->handle->username, dbc->handle->password)
00079                    : 1;
00080                         
00081               
00082              
00083          }
00084       }
00085       dbc->handle->current = dbc;
00086    }
00087    else {
00088      g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
00089          "Midgard: select_database reused open (%s)",
00090                               dbc->name);
00091    }
00092 
00093    rcfg->database.current = dbc;
00094    rcfg->mgd = dbc->handle->mgd;
00095    return assert_conn
00096       ? mgd_assert_db_connection(rcfg->mgd,
00097          dbc->handle->hostname, dbc->name, dbc->handle->username, dbc->handle->password)
00098       : 1;
00099 }
00100 
00101 #endif
00102 

Generated on Thu Feb 22 06:15:14 2007 for midgard-core by  doxygen 1.4.6