Exemple #1
0
/* ****************************************************************************
 * Global Function
 * SQL Database Connection
 */
bool SetSqlConnectionPool(std::string domainName)
{
    // myConnectionPool.find(domainName) == myConnectionPool.end() ~ this is defined inside the function: myConnectionPool[domainName] = dbConnection_;
    // myDomainHost.find(domainName) != myDomainHost.end() ~ make sure the connection is defined
    if (myConnectionPool.find(domainName) == myConnectionPool.end() && myDomainHost.find(domainName) != myDomainHost.end())
    {
        Wt::log("start") << " *** SetSqlConnectionPool(" << domainName << ")  myDbUser = "******" | myDbPort = " << myDbPort[domainName] << " |  myDbName = "  <<  myDbName[domainName];
        try
        {
            Wt::Dbo::SqlConnection *dbConnection;
            #ifdef POSTGRES
                dbConnection = new Wt::Dbo::backend::Postgres("user="******" password="******" port=" + myDbPort[domainName] + " dbname=" + myDbName[domainName]);
            #elif SQLITE3
                Wt::Dbo::backend::Sqlite3 *sqlite3 = new Wt::Dbo::backend::Sqlite3(std::string(path.c_str()) + "app_root/" + std::string(myDbName[domainName]));
                sqlite3->setDateTimeStorage(Wt::Dbo::SqlDateTime, Wt::Dbo::backend::Sqlite3::PseudoISO8601AsText);
                dbConnection = sqlite3;
            #elif MYSQL
                dbConnection = new Wt::Dbo::backend::MySQL(myDbName[domainName], myDbUser[domainName], myDbPassword[domainName], "localhost");
            #elif FIREBIRD
                #ifdef WIN32
                    myFile = "C:\\opt\\db\\firebird\\" + myDbName[domainName];
                #else
                    myFile = "/opt/db/firebird/" + myDbName[domainName];
                #endif
                dbConnection = new Wt::Dbo::backend::Firebird("localhost", myFile.c_str(), myDbUser[domainName], myDbPassword[domainName], "", "", ""); // Server:localhost, Path:File, user, password
            #endif // FIREBIRD
            dbConnection->setProperty("show-queries", "true");
            // We need to convert it FixedSqlConnectionPool to SqlConnectionPool, not sure if I should just refactor to use FixedSqlConnectionPool
            Wt::Dbo::SqlConnectionPool *dbConnection_ = new Wt::Dbo::FixedSqlConnectionPool(dbConnection, 10);
            myConnectionPool[domainName] = dbConnection_;
        }
        catch (std::exception& e)
        {
            std::cerr << e.what() << std::endl;
            std::cerr << "Error Connecting to domains database: " << domainName;
            Wt::log("error") << "WittyWizard::SetSqlConnectionPool()  Failed reading cookie: " << domainName;
            return false;
        }
    }
    else
    {
        Wt::log("start") << " *** SetSqlConnectionPool(" << domainName << ") reuse Connection";
    }
    return true;
}
Exemple #2
0
void WServer::initialize()
{
	//Start time
	_ptBeforeLoad = boost::posix_time::microsec_clock::local_time();

	/* *************************************************************************
	 * ***********************  Connect to SQL Server  *************************
	 * *************************************************************************/
	try
	{
		log("info") << "Connecting to database backend";

		//Wt::Dbo::SqlConnection *sqlConnection = new Wt::Dbo::backend::MySQL("wt", "root", "", "127.0.0.1");
		Wt::Dbo::SqlConnection *sqlConnection = new Wt::Dbo::backend::Sqlite3(":memory:");
		sqlConnection->setProperty("show-queries", "true");
		_sqlPool = new Wt::Dbo::FixedSqlConnectionPool(sqlConnection, 1);

		log("success") << "Successfully connected to database";
	}
	catch(Wt::Dbo::Exception &e)
	{
		log("fatal") << "Database error connecting to database: " <<  e.what();
		throw e;
	}
	catch(std::exception &e)
	{
		log("fatal") << "Error connecting to database: " << e.what();
		throw e;
	}

	/* *************************************************************************
	 * ***********************  Initialize Databases  **************************
	 * *************************************************************************/
	try
	{
		_dboManager = new DboDatabaseManager(this, _sqlPool);
		_modules = new ModuleDatabase(_dboManager);
		_configurations = new ConfigurationsDatabase(_dboManager);
		_languages = new LanguageDatabase(_dboManager);
		_styles = new StyleDatabase(_dboManager);
		_pages = new PageDatabase(_dboManager);
		_accessPaths = new AccessPathDatabase(_dboManager);
		_navigationMenus = new NavigationMenuDatabase(_dboManager);
	}
	catch(std::exception &e)
	{
		log("fatal") << "Error initializing databases: " << e.what();
		throw e;
	}

	/* *************************************************************************
	 * ***************************  Create Tables  *****************************
	 * *************************************************************************/
#define REINSTALL_DBO
#ifdef REINSTALL_DBO
	//Drop
	try
	{
		_installer = new Installer::DboInstaller(*_sqlPool);
		_installer->dropTables();
	}
	catch(Wt::Dbo::Exception &e)
	{
		log("error") << "Database error dropping tables: " <<  e.what();
	}
	catch(std::exception &e)
	{
		log("error") << "Error dropping tables: " << e.what();
	}

	//Create
	try
	{
		_installer->createTables();
	}
	catch(Wt::Dbo::Exception &e)
	{
		log("fatal") << "Database error creating tables: " <<  e.what();
		throw e;
	}
	catch(std::exception &e)
	{
		log("fatal") << "Error creating tables: " << e.what();
		throw e;
	}

	//Insert
// 	try
// 	{
		_installer->insertRows();
// 	}
// 	catch(Wt::Dbo::Exception &e)
// 	{
// 		log("fatal") << "Database error inserting data: " <<  e.what();
// 		throw e;
// 	}
// 	catch(std::exception &e)
// 	{
// 		log("fatal") << "Error inserting data: " << e.what();
// 		throw e;
// 	}
#endif

	/* *************************************************************************
	 * *************************  Load DboDatabases  ***************************
	 * *************************************************************************/
// 	try
// 	{
		log("info") << "Loading DboDatabaseManager";
		_dboManager->load();
// 	}
// 	catch(Wt::Dbo::Exception &e)
// 	{
// 		log("fatal") << "Database error loading DboDatabaseManager: " << e.what();
// 		throw e;
// 	}
// 	catch(std::exception &e)
// 	{
// 		log("fatal") << "Error loading DboDatabaseManager: " << e.what();
// 		throw e;
// 	}

	//Server localized strings
	setLocalizedStrings(new DboLocalizedStrings(this));

	pages()->register404PageHandler(new Default404Page());
	pages()->registerPageHandler("home", ModuleDatabase::Navigation, new AnotherPage());
	pages()->registerPageHandler("sitemap", ModuleDatabase::Navigation, new AnotherPage());
	pages()->registerPageHandler("login", ModuleDatabase::Authentication, new LoginPage());
	pages()->registerPageHandler("register", ModuleDatabase::Authentication, new RegistrationPage());

	/* *************************************************************************
	 * *********************  Create temporary XML file  ***********************
	 * *************************************************************************/
	try
	{
		log("info") << "Writing XML Configuration file";
		createWtXmlConfiguration();
		log("success") << "XML Configuration file created";
	}
	catch(std::exception &e)
	{
		log("fatal") << "Error creating XML Configuration file: " << e.what();
		throw e;
	}

	//Configure authorization module
	configureAuth();
}
Exemple #3
0
void WServer::Initialize()
{
	//Start time
	PTBeforeLoad = boost::posix_time::microsec_clock::local_time();

	/* *************************************************************************
	 * ***********************  Connect to SQL Server  *************************
	 * *************************************************************************/
	try
	{
		log("info") << "Connecting to database backend";

		Wt::Dbo::SqlConnection *SQLConnection = new Wt::Dbo::backend::MySQL("wt", "root", "", "127.0.0.1");
		//Wt::Dbo::SqlConnection *SQLConnection = new Wt::Dbo::backend::Sqlite3(":memory:");
		SQLConnection->setProperty("show-queries", "true");
		SQLPool = new Wt::Dbo::FixedSqlConnectionPool(SQLConnection, 1);

		log("success") << "Successfully connected to database";
	}
	catch(Wt::Dbo::Exception &e)
	{
		log("fatal") << "Database error connecting to database: " <<  e.what();
		throw e;
	}
	catch(std::exception &e)
	{
		log("fatal") << "Error connecting to database: " << e.what();
		throw e;
	}

	/* *************************************************************************
	 * ***********************  Initialize Databases  **************************
	 * *************************************************************************/
	try
	{
		_DboManager = new DboDatabaseManager(this, SQLPool);
		_Modules = new ModulesDatabase(_DboManager);
		_Configurations = new ConfigurationsDatabase(_DboManager);
		_Languages = new LanguagesDatabase(_DboManager);
		_Styles = new StylesDatabase(_DboManager);
		_Pages = new PagesDatabase(_DboManager);
		_AccessPaths = new AccessPathsDatabase(_DboManager);
		_NavigationMenus = new NavigationMenusDatabase(_DboManager);
	}
	catch(std::exception &e)
	{
		log("fatal") << "Error initializing databases: " << e.what();
		throw e;
	}

	/* *************************************************************************
	 * ***************************  Create Tables  *****************************
	 * *************************************************************************/
#define REINSTALLDBO 0
#if REINSTALLDBO == 1
	//Drop
	try
	{
		_Installer = new DboInstaller(*SQLPool);
		_Installer->DropTables();
	}
	catch(Wt::Dbo::Exception &e)
	{
		log("error") << "Database error dropping tables: " <<  e.what();
	}
	catch(std::exception &e)
	{
		log("error") << "Error dropping tables: " << e.what();
	}

	//Create
	try
	{
		_Installer->CreateTables();
	}
	catch(Wt::Dbo::Exception &e)
	{
		log("fatal") << "Database error creating tables: " <<  e.what();
		throw e;
	}
	catch(std::exception &e)
	{
		log("fatal") << "Error creating tables: " << e.what();
		throw e;
	}

	//Insert
	try
	{
		_Installer->InsertRows();
	}
	catch(Wt::Dbo::Exception &e)
	{
		log("fatal") << "Database error inserting default data: " <<  e.what();
		throw e;
	}
	catch(std::exception &e)
	{
		log("fatal") << "Error inserting default data: " << e.what();
		throw e;
	}
#endif

	/* *************************************************************************
	 * *************************  Load DboDatabases  ***************************
	 * *************************************************************************/
// 	try
// 	{
		log("info") << "Loading DboDatabaseManager";
		_DboManager->Load();
// 	}
// 	catch(Wt::Dbo::Exception &e)
// 	{
// 		log("fatal") << "Database error loading DboDatabaseManager: " << e.what();
// 		throw e;
// 	}
// 	catch(std::exception &e)
// 	{
// 		log("fatal") << "Error loading DboDatabaseManager: " << e.what();
// 		throw e;
// 	}

	//Server localized strings
	setLocalizedStrings(new DboLocalizedStrings(this));

	Pages()->Register404PageHandler(new Default404Page());
	Pages()->RegisterPageHandler("home", ModulesDatabase::Navigation, new TestPage());
	Pages()->RegisterPageHandler("sitemap", ModulesDatabase::Navigation, new AnotherPage());

	/* *************************************************************************
	 * *********************  Create temporary XML file  ***********************
	 * *************************************************************************/
	try
	{
		log("info") << "Writing XML Configuration file";
		CreateWtXmlConfiguration();
		log("success") << "XML Configuration file created";
	}
	catch(std::exception &e)
	{
		log("fatal") << "Error creating XML Configuration file: " << e.what();
		throw e;
	}

	//Configure authorization module
	ConfigureAuth();
}