+
+#include "LiveSupport/Core/Configurable.h"
+#include "LiveSupport/Db/ConnectionManagerInterface.h"
+
+
+namespace LiveSupport {
+namespace Db {
+
+using namespace LiveSupport;
+using namespace LiveSupport::Core;
+
+/* ================================================================ constants */
+
+
+/* =================================================================== macros */
+
+
+/* =============================================================== data types */
+
+/**
+ * The factory to create appropriate ConnectionManager objects.
+ * This singleton class has to be configured with an XML element,
+ * describing the ConnectionManagerInterface that it should build
+ * and maintain. This is done by including the configuration element
+ * for the desired type of connection manager inside the configuration
+ * element for the factory.
+ *
+ * Currently only the SimpleConnectionManager is supported, thus a
+ * configuration file may look like this:
+ *
+ *
+ * <connectionManagerFactory>
+ *
+ * </connectionManagerFactory>
+ *
+ *
+ * The DTD for the above XML structure is:
+ *
+ *
+ *
+ *
+ *
+ * For the DTD and details of the simpleConnectionManager configuration
+ * element, see the SimpleConnectionManager documentation.
+ *
+ * @author $Author: maroy $
+ * @version $Revision: 1.1 $
+ * @see SimpleConnectionManager
+ */
+class ConnectionManagerFactory :
+ virtual public Configurable
+{
+ private:
+ /**
+ * The name of the configuration XML elmenent used by this object.
+ */
+ static const std::string configElementNameStr;
+
+ /**
+ * The singleton instance of this object.
+ */
+ static Ptr::Ref singleton;
+
+ /**
+ * The connection manager created by this factory.
+ */
+ Ptr::Ref connectionManager;
+
+ /**
+ * The default constructor.
+ */
+ ConnectionManagerFactory(void) throw()
+ {
+ }
+
+
+ public:
+ /**
+ * A virtual destructor, as this class has virtual functions.
+ */
+ virtual
+ ~ConnectionManagerFactory(void) throw ()
+ {
+ }
+
+ /**
+ * Return the name of the XML element this object expects
+ * to be sent to a call to configure().
+ *
+ * @return the name of the expected XML configuration element.
+ */
+ static const std::string
+ configElementName(void) throw ()
+ {
+ return configElementNameStr;
+ }
+
+ /**
+ * Returns the singleton instance of this object.
+ *
+ * @return the singleton instance of this object.
+ */
+ static Ptr::Ref
+ getInstance() throw ();
+
+ /**
+ * Configure the object based on the XML element supplied.
+ *
+ * @param element the XML element to configure the object from.
+ * @exception std::invalid_argument if the supplied XML element
+ * contains bad configuraiton information
+ * @exception std::logic_error if the object has already
+ * been configured, and can not be reconfigured.
+ */
+ virtual void
+ configure(const xmlpp::Element & element)
+ throw (std::invalid_argument,
+ std::logic_error);
+
+ /**
+ * Return a connection manager.
+ *
+ * @return the appropriate connection manager, according to the
+ * configuration of this factory.
+ */
+ Ptr::Ref
+ getConnectionManager(void) throw ()
+ {
+ return connectionManager;
+ }
+};
+
+
+/* ================================================= external data structures */
+
+
+/* ====================================================== function prototypes */
+
+
+} // namespace Db
+} // namespace LiveSupport
+
+#endif // LiveSupport_Db_ConnectionManagerFactory_h
+
diff --git a/livesupport/modules/db/include/LiveSupport/Db/ConnectionManagerInterface.h b/livesupport/modules/db/include/LiveSupport/Db/ConnectionManagerInterface.h
new file mode 100644
index 000000000..4272fbbc1
--- /dev/null
+++ b/livesupport/modules/db/include/LiveSupport/Db/ConnectionManagerInterface.h
@@ -0,0 +1,107 @@
+/*------------------------------------------------------------------------------
+
+ Copyright (c) 2004 Media Development Loan Fund
+
+ This file is part of the LiveSupport project.
+ http://livesupport.campware.org/
+ To report bugs, send an e-mail to bugs@campware.org
+
+ LiveSupport is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ LiveSupport is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with LiveSupport; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+ Author : $Author: maroy $
+ Version : $Revision: 1.1 $
+ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/db/include/LiveSupport/Db/ConnectionManagerInterface.h,v $
+
+------------------------------------------------------------------------------*/
+#ifndef LiveSupport_Db_ConnectionManagerInterface_h
+#define LiveSupport_Db_ConnectionManagerInterface_h
+
+#ifndef __cplusplus
+#error This is a C++ include file
+#endif
+
+
+/* ============================================================ include files */
+
+#ifdef HAVE_CONFIG_H
+#include "configure.h"
+#endif
+
+#include
+
+
+#include "LiveSupport/Core/Ptr.h"
+
+namespace LiveSupport {
+namespace Db {
+
+using namespace LiveSupport::Core;
+
+/* ================================================================ constants */
+
+
+/* =================================================================== macros */
+
+
+/* =============================================================== data types */
+
+/**
+ * The interface for handing out and managing database connections.
+ *
+ * @author $Author: maroy $
+ * @version $Revision: 1.1 $
+ */
+class ConnectionManagerInterface
+{
+ public:
+ /**
+ * Get a database connection from the manager.
+ * After use, the connection must be returned by calling
+ * returnConnection().
+ *
+ * @return a database connection to the database this manager
+ * serves.
+ * @see #returnConnection
+ */
+ virtual Ptr::Ref
+ getConnection(void) throw (std::runtime_error)
+ = 0;
+
+ /**
+ * Return a database connection previously aquired by a call to
+ * getConnection(), after it is not needed anymore.
+ *
+ * @param connection the connection to return.
+ * @see #getConnection
+ */
+ virtual void
+ returnConnection(Ptr::Ref connection)
+ throw (std::runtime_error)
+ = 0;
+};
+
+
+/* ================================================= external data structures */
+
+
+/* ====================================================== function prototypes */
+
+
+} // namespace Db
+} // namespace LiveSupport
+
+#endif // LiveSupport_Db_ConnectionManagerInterface_h
+
diff --git a/livesupport/modules/db/include/LiveSupport/Db/Conversion.h b/livesupport/modules/db/include/LiveSupport/Db/Conversion.h
new file mode 100644
index 000000000..0a898f295
--- /dev/null
+++ b/livesupport/modules/db/include/LiveSupport/Db/Conversion.h
@@ -0,0 +1,106 @@
+/*------------------------------------------------------------------------------
+
+ Copyright (c) 2004 Media Development Loan Fund
+
+ This file is part of the LiveSupport project.
+ http://livesupport.campware.org/
+ To report bugs, send an e-mail to bugs@campware.org
+
+ LiveSupport is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ LiveSupport is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with LiveSupport; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+ Author : $Author: maroy $
+ Version : $Revision: 1.1 $
+ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/db/include/LiveSupport/Db/Conversion.h,v $
+
+------------------------------------------------------------------------------*/
+#ifndef LiveSupport_Db_Conversion_h
+#define LiveSupport_Db_Conversion_h
+
+#ifndef __cplusplus
+#error This is a C++ include file
+#endif
+
+
+/* ============================================================ include files */
+
+#ifdef HAVE_CONFIG_H
+#include "configure.h"
+#endif
+
+#include
+#include
+
+#include "LiveSupport/Core/Ptr.h"
+
+
+namespace LiveSupport {
+namespace Db {
+
+using namespace boost;
+
+using namespace LiveSupport;
+using namespace LiveSupport::Core;
+
+
+/* ================================================================ constants */
+
+
+/* =================================================================== macros */
+
+
+/* =============================================================== data types */
+
+/**
+ * A helper object holding static conversion functions, that are
+ * helpful when accessing databases.
+ *
+ * @author $Author: maroy $
+ * @version $Revision: 1.1 $
+ */
+class Conversion
+{
+ private:
+ /**
+ * The default constructor.
+ */
+ Conversion(void) throw()
+ {
+ }
+
+
+ public:
+ /**
+ * Convert a boost::ptime to a odbc::Timestamp.
+ *
+ * @param ptime the boost ptime to convert.
+ * @return an odbc::Timestamp, holding the same time.
+ */
+ static Ptr::Ref
+ ptimeToTimestamp(Ptr::Ref ptime) throw();
+};
+
+
+/* ================================================= external data structures */
+
+
+/* ====================================================== function prototypes */
+
+
+} // namespace Db
+} // namespace LiveSupport
+
+#endif // LiveSupport_Db_Conversion_h
+
diff --git a/livesupport/modules/db/lib/.keepme b/livesupport/modules/db/lib/.keepme
new file mode 100644
index 000000000..e0808fa16
--- /dev/null
+++ b/livesupport/modules/db/lib/.keepme
@@ -0,0 +1 @@
+keep me
diff --git a/livesupport/modules/db/src/ConnectionManagerFactory.cxx b/livesupport/modules/db/src/ConnectionManagerFactory.cxx
new file mode 100644
index 000000000..50ae73829
--- /dev/null
+++ b/livesupport/modules/db/src/ConnectionManagerFactory.cxx
@@ -0,0 +1,111 @@
+/*------------------------------------------------------------------------------
+
+ Copyright (c) 2004 Media Development Loan Fund
+
+ This file is part of the LiveSupport project.
+ http://livesupport.campware.org/
+ To report bugs, send an e-mail to bugs@campware.org
+
+ LiveSupport is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ LiveSupport is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with LiveSupport; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+ Author : $Author: maroy $
+ Version : $Revision: 1.1 $
+ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/db/src/ConnectionManagerFactory.cxx,v $
+
+------------------------------------------------------------------------------*/
+
+/* ============================================================ include files */
+
+#ifdef HAVE_CONFIG_H
+#include "configure.h"
+#endif
+
+#include "LiveSupport/Db/ConnectionManagerFactory.h"
+#include "SimpleConnectionManager.h"
+
+
+using namespace LiveSupport::Core;
+using namespace LiveSupport::Db;
+
+/* =================================================== local data structures */
+
+
+/* ================================================ local constants & macros */
+
+/*------------------------------------------------------------------------------
+ * The name of the config element for this class
+ *----------------------------------------------------------------------------*/
+const std::string ConnectionManagerFactory::configElementNameStr =
+ "connectionManagerFactory";
+
+/*------------------------------------------------------------------------------
+ * The singleton instance of ConnectionManagerFactory
+ *----------------------------------------------------------------------------*/
+Ptr::Ref ConnectionManagerFactory::singleton;
+
+
+/* =============================================== local function prototypes */
+
+
+/* ============================================================= module code */
+
+/*------------------------------------------------------------------------------
+ * Return the singleton instance to ConnectionManagerFactory
+ *----------------------------------------------------------------------------*/
+Ptr::Ref
+ConnectionManagerFactory :: getInstance(void) throw ()
+{
+ if (!singleton.get()) {
+ singleton.reset(new ConnectionManagerFactory());
+ }
+
+ return singleton;
+}
+
+
+/*------------------------------------------------------------------------------
+ * Configure the connection manager factory.
+ *----------------------------------------------------------------------------*/
+void
+ConnectionManagerFactory :: configure(const xmlpp::Element & element)
+ throw (std::invalid_argument,
+ std::logic_error)
+{
+ if (element.get_name() != configElementNameStr) {
+ std::string eMsg = "Bad configuration element ";
+ eMsg += element.get_name();
+ throw std::invalid_argument(eMsg);
+ }
+
+ connectionManager.reset();
+
+ // try to look for a SimpleConnectionManager configuration element
+ xmlpp::Node::NodeList nodes = element.get_children(
+ SimpleConnectionManager::getConfigElementName());
+ if (nodes.size() >= 1) {
+ const xmlpp::Element * configElement =
+ dynamic_cast (*(nodes.begin()));
+ Ptr::Ref scm(new SimpleConnectionManager());
+ scm->configure(*configElement);
+ connectionManager = scm;
+ }
+
+ if (!connectionManager) {
+ throw std::invalid_argument("no storage client factories to configure");
+ }
+}
+
+
diff --git a/livesupport/modules/db/src/ConnectionManagerFactoryTest.cxx b/livesupport/modules/db/src/ConnectionManagerFactoryTest.cxx
new file mode 100644
index 000000000..87e5cfda7
--- /dev/null
+++ b/livesupport/modules/db/src/ConnectionManagerFactoryTest.cxx
@@ -0,0 +1,126 @@
+/*------------------------------------------------------------------------------
+
+ Copyright (c) 2004 Media Development Loan Fund
+
+ This file is part of the LiveSupport project.
+ http://livesupport.campware.org/
+ To report bugs, send an e-mail to bugs@campware.org
+
+ LiveSupport is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ LiveSupport is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with LiveSupport; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+ Author : $Author: maroy $
+ Version : $Revision: 1.1 $
+ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/db/src/ConnectionManagerFactoryTest.cxx,v $
+
+------------------------------------------------------------------------------*/
+
+/* ============================================================ include files */
+
+#ifdef HAVE_CONFIG_H
+#include "configure.h"
+#endif
+
+#if HAVE_UNISTD_H
+#include
+#else
+#error "Need unistd.h"
+#endif
+
+
+#include
+#include
+#include
+
+#include "LiveSupport/Db/ConnectionManagerFactory.h"
+#include "ConnectionManagerFactoryTest.h"
+
+
+using namespace odbc;
+using namespace LiveSupport::Db;
+
+/* =================================================== local data structures */
+
+
+/* ================================================ local constants & macros */
+
+CPPUNIT_TEST_SUITE_REGISTRATION(ConnectionManagerFactoryTest);
+
+/**
+ * The name of the configuration file for the connection manager factory.
+ */
+static const std::string configFileName = "etc/connectionManagerFactory.xml";
+
+
+/* =============================================== local function prototypes */
+
+
+/* ============================================================= module code */
+
+/*------------------------------------------------------------------------------
+ * Set up the test environment
+ *----------------------------------------------------------------------------*/
+void
+ConnectionManagerFactoryTest :: setUp(void) throw ()
+{
+}
+
+
+/*------------------------------------------------------------------------------
+ * Clean up the test environment
+ *----------------------------------------------------------------------------*/
+void
+ConnectionManagerFactoryTest :: tearDown(void) throw ()
+{
+}
+
+
+/*------------------------------------------------------------------------------
+ * Test to see if the singleton Hello object is accessible
+ *----------------------------------------------------------------------------*/
+void
+ConnectionManagerFactoryTest :: firstTest(void)
+ throw (CPPUNIT_NS::Exception)
+{
+ try {
+ Ptr::Ref parser(
+ new xmlpp::DomParser(configFileName, true));
+ const xmlpp::Document * document = parser->get_document();
+ const xmlpp::Element * root = document->get_root_node();
+ Ptr::Ref cmf =
+ ConnectionManagerFactory::getInstance();
+
+ cmf->configure(*root);
+
+ Ptr::Ref cm = cmf->getConnectionManager();
+ CPPUNIT_ASSERT(cm);
+
+ Ptr::Ref connection = cm->getConnection();
+ CPPUNIT_ASSERT(connection);
+
+ // so far, so good. now simply execute "SELECT 1", and see if
+ // it works
+ Ptr::Ref stmt(connection->createStatement());
+ Ptr::Ref rs(stmt->executeQuery("SELECT 1"));
+ CPPUNIT_ASSERT(rs->next());
+ CPPUNIT_ASSERT(rs->getInt(1) == 1);
+
+ } catch (std::invalid_argument &e) {
+ CPPUNIT_FAIL("semantic error in configuration file");
+ } catch (xmlpp::exception &e) {
+ CPPUNIT_FAIL(e.what());
+ }
+}
+
diff --git a/livesupport/modules/db/src/ConnectionManagerFactoryTest.h b/livesupport/modules/db/src/ConnectionManagerFactoryTest.h
new file mode 100644
index 000000000..ffbdd0dbd
--- /dev/null
+++ b/livesupport/modules/db/src/ConnectionManagerFactoryTest.h
@@ -0,0 +1,106 @@
+/*------------------------------------------------------------------------------
+
+ Copyright (c) 2004 Media Development Loan Fund
+
+ This file is part of the LiveSupport project.
+ http://livesupport.campware.org/
+ To report bugs, send an e-mail to bugs@campware.org
+
+ LiveSupport is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ LiveSupport is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with LiveSupport; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+ Author : $Author: maroy $
+ Version : $Revision: 1.1 $
+ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/db/src/ConnectionManagerFactoryTest.h,v $
+
+------------------------------------------------------------------------------*/
+#ifndef ConnectionManagerFactoryTest_h
+#define ConnectionManagerFactoryTest_h
+
+#ifndef __cplusplus
+#error This is a C++ include file
+#endif
+
+
+/* ============================================================ include files */
+
+#ifdef HAVE_CONFIG_H
+#include "configure.h"
+#endif
+
+#include
+
+
+namespace LiveSupport {
+namespace Db {
+
+/* ================================================================ constants */
+
+
+/* =================================================================== macros */
+
+
+/* =============================================================== data types */
+
+/**
+ * Unit test for the ConnectionManagerFactory class.
+ *
+ * @author $Author: maroy $
+ * @version $Revision: 1.1 $
+ * @see ConnectionManagerFactory
+ */
+class ConnectionManagerFactoryTest : public CPPUNIT_NS::TestFixture
+{
+ CPPUNIT_TEST_SUITE(ConnectionManagerFactoryTest);
+ CPPUNIT_TEST(firstTest);
+ CPPUNIT_TEST_SUITE_END();
+
+ protected:
+
+ /**
+ * A simple test.
+ *
+ * @exception CPPUNIT_NS::Exception on test failures.
+ */
+ void
+ firstTest(void) throw (CPPUNIT_NS::Exception);
+
+ public:
+
+ /**
+ * Set up the environment for the test case.
+ */
+ void
+ setUp(void) throw ();
+
+ /**
+ * Clean up the environment after the test case.
+ */
+ void
+ tearDown(void) throw ();
+};
+
+
+/* ================================================= external data structures */
+
+
+/* ====================================================== function prototypes */
+
+
+} // namespace Db
+} // namespace LiveSupport
+
+#endif // ConnectionManagerFactoryTest_h
+
diff --git a/livesupport/modules/db/src/Conversion.cxx b/livesupport/modules/db/src/Conversion.cxx
new file mode 100644
index 000000000..76ef535c7
--- /dev/null
+++ b/livesupport/modules/db/src/Conversion.cxx
@@ -0,0 +1,73 @@
+/*------------------------------------------------------------------------------
+
+ Copyright (c) 2004 Media Development Loan Fund
+
+ This file is part of the LiveSupport project.
+ http://livesupport.campware.org/
+ To report bugs, send an e-mail to bugs@campware.org
+
+ LiveSupport is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ LiveSupport is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with LiveSupport; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+ Author : $Author: maroy $
+ Version : $Revision: 1.1 $
+ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/db/src/Conversion.cxx,v $
+
+------------------------------------------------------------------------------*/
+
+/* ============================================================ include files */
+
+#ifdef HAVE_CONFIG_H
+#include "configure.h"
+#endif
+
+#include "LiveSupport/Db/Conversion.h"
+
+
+using namespace boost;
+
+using namespace LiveSupport::Core;
+using namespace LiveSupport::Db;
+
+/* =================================================== local data structures */
+
+
+/* ================================================ local constants & macros */
+
+
+/* =============================================== local function prototypes */
+
+
+/* ============================================================= module code */
+
+/*------------------------------------------------------------------------------
+ * Convert a boost::ptime to an odbc::Timestamp
+ *----------------------------------------------------------------------------*/
+Ptr::Ref
+Conversion :: ptimeToTimestamp(Ptr::Ref ptime)
+ throw ()
+{
+ gregorian::date date = ptime->date();
+ posix_time::time_duration hours = ptime->time_of_day();
+
+ Ptr::Ref timestamp(new odbc::Timestamp(date.year(),
+ date.month(),
+ date.day(),
+ hours.hours(),
+ hours.minutes(),
+ hours.seconds()));
+ return timestamp;
+}
+
diff --git a/livesupport/modules/db/src/SimpleConnectionManager.cxx b/livesupport/modules/db/src/SimpleConnectionManager.cxx
new file mode 100644
index 000000000..d222abdc8
--- /dev/null
+++ b/livesupport/modules/db/src/SimpleConnectionManager.cxx
@@ -0,0 +1,148 @@
+/*------------------------------------------------------------------------------
+
+ Copyright (c) 2004 Media Development Loan Fund
+
+ This file is part of the LiveSupport project.
+ http://livesupport.campware.org/
+ To report bugs, send an e-mail to bugs@campware.org
+
+ LiveSupport is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ LiveSupport is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with LiveSupport; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+ Author : $Author: maroy $
+ Version : $Revision: 1.1 $
+ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/db/src/SimpleConnectionManager.cxx,v $
+
+------------------------------------------------------------------------------*/
+
+/* ============================================================ include files */
+
+#ifdef HAVE_CONFIG_H
+#include "configure.h"
+#endif
+
+#include
+
+#include "SimpleConnectionManager.h"
+
+
+using namespace LiveSupport::Core;
+using namespace LiveSupport::Db;
+
+/* =================================================== local data structures */
+
+
+/* ================================================ local constants & macros */
+
+/*------------------------------------------------------------------------------
+ * The name of the config element for this class
+ *----------------------------------------------------------------------------*/
+const std::string SimpleConnectionManager::configElementNameStr =
+ "simpleConnectionManager";
+
+/**
+ * The name of the attribute to get the dsn for the connection.
+ */
+static const std::string dsnAttrName = "dsn";
+
+/**
+ * The name of the attribute to get the userName for the connection.
+ */
+static const std::string userNameAttrName = "userName";
+
+/**
+ * The name of the attribute to get the password for the connection.
+ */
+static const std::string passwordAttrName = "password";
+
+
+/* =============================================== local function prototypes */
+
+
+/* ============================================================= module code */
+
+/*------------------------------------------------------------------------------
+ * Configure the connection manager factory.
+ *----------------------------------------------------------------------------*/
+void
+SimpleConnectionManager :: configure(const xmlpp::Element & element)
+ throw (std::invalid_argument,
+ std::logic_error)
+{
+ if (element.get_name() != configElementNameStr) {
+ std::string eMsg = "Bad configuration element ";
+ eMsg += element.get_name();
+ throw std::invalid_argument(eMsg);
+ }
+
+ const xmlpp::Attribute * attribute;
+
+ if (!(attribute = element.get_attribute(dsnAttrName))) {
+ std::string eMsg = "Missing attribute ";
+ eMsg += dsnAttrName;
+ throw std::invalid_argument(eMsg);
+ }
+ dsn = attribute->get_value();
+
+ if (!(attribute = element.get_attribute(userNameAttrName))) {
+ std::string eMsg = "Missing attribute ";
+ eMsg += userNameAttrName;
+ throw std::invalid_argument(eMsg);
+ }
+ userName = attribute->get_value();
+
+ if (!(attribute = element.get_attribute(passwordAttrName))) {
+ std::string eMsg = "Missing attribute ";
+ eMsg += passwordAttrName;
+ throw std::invalid_argument(eMsg);
+ }
+ password = attribute->get_value();
+}
+
+
+/*------------------------------------------------------------------------------
+ * Give out a connection.
+ *----------------------------------------------------------------------------*/
+Ptr::Ref
+SimpleConnectionManager :: getConnection(void)
+ throw (std::runtime_error)
+{
+ odbc::Connection * conn = odbc::DriverManager::getConnection(dsn,
+ userName,
+ password);
+ if (!conn) {
+ std::string eMsg = "unable to option ODBC connection for DSN ";
+ eMsg += dsn;
+ throw std::runtime_error(eMsg);
+ }
+
+ Ptr::Ref connection(conn);
+ return connection;
+}
+
+
+/*------------------------------------------------------------------------------
+ * Receive a connection back.
+ *----------------------------------------------------------------------------*/
+void
+SimpleConnectionManager :: returnConnection(
+ Ptr::Ref connection)
+ throw (std::runtime_error)
+{
+ // nothing to do here...
+ // we could save the outgoing connections to a set in getConnection()
+ // and check here to see if the returned one is contained there
+}
+
diff --git a/livesupport/modules/db/src/SimpleConnectionManager.h b/livesupport/modules/db/src/SimpleConnectionManager.h
new file mode 100644
index 000000000..0965eb5eb
--- /dev/null
+++ b/livesupport/modules/db/src/SimpleConnectionManager.h
@@ -0,0 +1,193 @@
+/*------------------------------------------------------------------------------
+
+ Copyright (c) 2004 Media Development Loan Fund
+
+ This file is part of the LiveSupport project.
+ http://livesupport.campware.org/
+ To report bugs, send an e-mail to bugs@campware.org
+
+ LiveSupport is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ LiveSupport is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with LiveSupport; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+ Author : $Author: maroy $
+ Version : $Revision: 1.1 $
+ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/db/src/SimpleConnectionManager.h,v $
+
+------------------------------------------------------------------------------*/
+#ifndef SimpleConnectionManager_h
+#define SimpleConnectionManager_h
+
+#ifndef __cplusplus
+#error This is a C++ include file
+#endif
+
+
+/* ============================================================ include files */
+
+#ifdef HAVE_CONFIG_H
+#include "configure.h"
+#endif
+
+#include
+
+#include
+#include
+
+
+namespace LiveSupport {
+namespace Db {
+
+using namespace LiveSupport;
+using namespace LiveSupport::Core;
+
+/* ================================================================ constants */
+
+
+/* =================================================================== macros */
+
+
+/* =============================================================== data types */
+
+/**
+ * A simple connection manager, basically a facade to the underlying
+ * odbc::DriverManager class.
+ * This class can be configured with the following XML element, containing
+ * the ODBC Data Source Name (DSN), ODBC user name and ODBC password
+ * the manager will connect with to the ODBC source. The XML element looks
+ * as follows:
+ *
+ *
+ *
+ *
+ *
+ * The DTD for the above XML structure is:
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ * @author $Author: maroy $
+ * @version $Revision: 1.1 $
+ */
+class SimpleConnectionManager :
+ virtual public Configurable,
+ virtual public ConnectionManagerInterface
+{
+ private:
+ /**
+ * The name of the configuration XML elmenent used by this object.
+ */
+ static const std::string configElementNameStr;
+
+ /**
+ * The ODBC Data Source Name this manager connects to.
+ */
+ std::string dsn;
+
+ /**
+ * The user name to use when connecting to the ODBC DSN.
+ */
+ std::string userName;
+
+ /**
+ * The password to use when connecting to the ODBC DSN.
+ */
+ std::string password;
+
+
+ public:
+ /**
+ * The default constructor.
+ */
+ SimpleConnectionManager(void) throw ()
+ {
+ }
+
+ /**
+ * A virtual destructor, as this class has virtual functions.
+ */
+ virtual
+ ~SimpleConnectionManager(void) throw ()
+ {
+ }
+
+ /**
+ * Return the name of the XML element this object expects
+ * to be sent to a call to configure().
+ *
+ * @return the name of the expected XML configuration element.
+ */
+ static const std::string
+ getConfigElementName(void) throw ()
+ {
+ return configElementNameStr;
+ }
+
+ /**
+ * Configure the object based on the XML element supplied.
+ *
+ * @param element the XML element to configure the object from.
+ * @exception std::invalid_argument if the supplied XML element
+ * contains bad configuraiton information
+ * @exception std::logic_error if the object has already
+ * been configured, and can not be reconfigured.
+ */
+ virtual void
+ configure(const xmlpp::Element & element)
+ throw (std::invalid_argument,
+ std::logic_error);
+
+ /**
+ * Get a database connection from the manager.
+ * After use, the connection must be returned by calling
+ * returnConnection().
+ *
+ * @return a database connection to the database this manager
+ * serves.
+ * @see #returnConnection
+ */
+ virtual Ptr::Ref
+ getConnection(void) throw (std::runtime_error);
+
+ /**
+ * Return a database connection previously aquired by a call to
+ * getConnection(), after it is not needed anymore.
+ *
+ * @param connection the connection to return.
+ * @see #getConnection
+ */
+ virtual void
+ returnConnection(Ptr::Ref connection)
+ throw (std::runtime_error);
+};
+
+
+/* ================================================= external data structures */
+
+
+/* ====================================================== function prototypes */
+
+
+} // namespace Db
+} // namespace LiveSupport
+
+#endif // SimpleConnectionManager_h
+
diff --git a/livesupport/modules/db/src/SimpleConnectionManagerTest.cxx b/livesupport/modules/db/src/SimpleConnectionManagerTest.cxx
new file mode 100644
index 000000000..131883c8f
--- /dev/null
+++ b/livesupport/modules/db/src/SimpleConnectionManagerTest.cxx
@@ -0,0 +1,122 @@
+/*------------------------------------------------------------------------------
+
+ Copyright (c) 2004 Media Development Loan Fund
+
+ This file is part of the LiveSupport project.
+ http://livesupport.campware.org/
+ To report bugs, send an e-mail to bugs@campware.org
+
+ LiveSupport is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ LiveSupport is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with LiveSupport; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+ Author : $Author: maroy $
+ Version : $Revision: 1.1 $
+ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/db/src/SimpleConnectionManagerTest.cxx,v $
+
+------------------------------------------------------------------------------*/
+
+/* ============================================================ include files */
+
+#ifdef HAVE_CONFIG_H
+#include "configure.h"
+#endif
+
+#if HAVE_UNISTD_H
+#include
+#else
+#error "Need unistd.h"
+#endif
+
+
+#include
+#include
+#include
+
+#include "SimpleConnectionManager.h"
+#include "SimpleConnectionManagerTest.h"
+
+
+using namespace odbc;
+using namespace LiveSupport::Db;
+
+/* =================================================== local data structures */
+
+
+/* ================================================ local constants & macros */
+
+CPPUNIT_TEST_SUITE_REGISTRATION(SimpleConnectionManagerTest);
+
+/**
+ * The name of the configuration file for the connection manager.
+ */
+static const std::string configFileName = "etc/simpleConnectionManager.xml";
+
+
+/* =============================================== local function prototypes */
+
+
+/* ============================================================= module code */
+
+/*------------------------------------------------------------------------------
+ * Set up the test environment
+ *----------------------------------------------------------------------------*/
+void
+SimpleConnectionManagerTest :: setUp(void) throw ()
+{
+}
+
+
+/*------------------------------------------------------------------------------
+ * Clean up the test environment
+ *----------------------------------------------------------------------------*/
+void
+SimpleConnectionManagerTest :: tearDown(void) throw ()
+{
+}
+
+
+/*------------------------------------------------------------------------------
+ * Test to see if the singleton Hello object is accessible
+ *----------------------------------------------------------------------------*/
+void
+SimpleConnectionManagerTest :: firstTest(void)
+ throw (CPPUNIT_NS::Exception)
+{
+ try {
+ Ptr::Ref parser(
+ new xmlpp::DomParser(configFileName, true));
+ const xmlpp::Document * document = parser->get_document();
+ const xmlpp::Element * root = document->get_root_node();
+ Ptr::Ref scm(new SimpleConnectionManager());
+
+ scm->configure(*root);
+
+ Ptr::Ref connection = scm->getConnection();
+ CPPUNIT_ASSERT(connection);
+
+ // so far, so good. now simply execute "SELECT 1", and see if
+ // it works
+ Ptr::Ref stmt(connection->createStatement());
+ Ptr::Ref rs(stmt->executeQuery("SELECT 1"));
+ CPPUNIT_ASSERT(rs->next());
+ CPPUNIT_ASSERT(rs->getInt(1) == 1);
+
+ } catch (std::invalid_argument &e) {
+ CPPUNIT_FAIL("semantic error in configuration file");
+ } catch (xmlpp::exception &e) {
+ CPPUNIT_FAIL(e.what());
+ }
+}
+
diff --git a/livesupport/modules/db/src/SimpleConnectionManagerTest.h b/livesupport/modules/db/src/SimpleConnectionManagerTest.h
new file mode 100644
index 000000000..64887a1bc
--- /dev/null
+++ b/livesupport/modules/db/src/SimpleConnectionManagerTest.h
@@ -0,0 +1,106 @@
+/*------------------------------------------------------------------------------
+
+ Copyright (c) 2004 Media Development Loan Fund
+
+ This file is part of the LiveSupport project.
+ http://livesupport.campware.org/
+ To report bugs, send an e-mail to bugs@campware.org
+
+ LiveSupport is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ LiveSupport is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with LiveSupport; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+ Author : $Author: maroy $
+ Version : $Revision: 1.1 $
+ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/db/src/SimpleConnectionManagerTest.h,v $
+
+------------------------------------------------------------------------------*/
+#ifndef SimpleConnectionManagerTest_h
+#define SimpleConnectionManagerTest_h
+
+#ifndef __cplusplus
+#error This is a C++ include file
+#endif
+
+
+/* ============================================================ include files */
+
+#ifdef HAVE_CONFIG_H
+#include "configure.h"
+#endif
+
+#include
+
+
+namespace LiveSupport {
+namespace Db {
+
+/* ================================================================ constants */
+
+
+/* =================================================================== macros */
+
+
+/* =============================================================== data types */
+
+/**
+ * Unit test for the SimpleConnectionManager class.
+ *
+ * @author $Author: maroy $
+ * @version $Revision: 1.1 $
+ * @see SimpleConnectionManager
+ */
+class SimpleConnectionManagerTest : public CPPUNIT_NS::TestFixture
+{
+ CPPUNIT_TEST_SUITE(SimpleConnectionManagerTest);
+ CPPUNIT_TEST(firstTest);
+ CPPUNIT_TEST_SUITE_END();
+
+ protected:
+
+ /**
+ * A simple test.
+ *
+ * @exception CPPUNIT_NS::Exception on test failures.
+ */
+ void
+ firstTest(void) throw (CPPUNIT_NS::Exception);
+
+ public:
+
+ /**
+ * Set up the environment for the test case.
+ */
+ void
+ setUp(void) throw ();
+
+ /**
+ * Clean up the environment after the test case.
+ */
+ void
+ tearDown(void) throw ();
+};
+
+
+/* ================================================= external data structures */
+
+
+/* ====================================================== function prototypes */
+
+
+} // namespace Db
+} // namespace LiveSupport
+
+#endif // SimpleConnectionManagerTest_h
+
diff --git a/livesupport/modules/db/src/TestRunner.cxx b/livesupport/modules/db/src/TestRunner.cxx
new file mode 100644
index 000000000..94ab09046
--- /dev/null
+++ b/livesupport/modules/db/src/TestRunner.cxx
@@ -0,0 +1,85 @@
+/*------------------------------------------------------------------------------
+
+ Copyright (c) 2004 Media Development Loan Fund
+
+ This file is part of the LiveSupport project.
+ http://livesupport.campware.org/
+ To report bugs, send an e-mail to bugs@campware.org
+
+ LiveSupport is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ LiveSupport is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with LiveSupport; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+ Author : $Author: maroy $
+ Version : $Revision: 1.1 $
+ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/db/src/TestRunner.cxx,v $
+
+------------------------------------------------------------------------------*/
+
+/* ============================================================ include files */
+
+#ifdef HAVE_CONFIG_H
+#include "configure.h"
+#endif
+
+#include
+#include
+#include
+#include
+#include
+#include
+
+
+/* =================================================== local data structures */
+
+
+/* ================================================ local constants & macros */
+
+
+/* =============================================== local function prototypes */
+
+
+/* ============================================================= module code */
+
+/*------------------------------------------------------------------------------
+ * Run all tests
+ *----------------------------------------------------------------------------*/
+int
+main( int argc,
+ char * argv[] ) throw ()
+{
+ // Create the event manager and test controller
+ CPPUNIT_NS::TestResult controller;
+
+ // Add a listener that colllects test result
+ CPPUNIT_NS::TestResultCollector result;
+ controller.addListener( &result );
+
+ // Add a listener that print dots as test run.
+ CPPUNIT_NS::BriefTestProgressListener progress;
+ controller.addListener( &progress );
+
+ // Add the top suite to the test runner
+ CPPUNIT_NS::TestRunner runner;
+ runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
+ runner.run( controller );
+
+ // Print test in a compiler compatible format.
+ CPPUNIT_NS::CompilerOutputter outputter( &result, std::cerr );
+ outputter.setLocationFormat("%p:%l:");
+ outputter.write();
+
+ return result.wasSuccessful() ? 0 : 1;
+}
+
diff --git a/livesupport/modules/db/tmp/.keepme b/livesupport/modules/db/tmp/.keepme
new file mode 100644
index 000000000..e0808fa16
--- /dev/null
+++ b/livesupport/modules/db/tmp/.keepme
@@ -0,0 +1 @@
+keep me