示例#1
0
bool DBManager::init(std::tstring dbfile)
{
    std::tstring aofolder = AOManager::instance().getAOFolder();
    if (aofolder.empty())
    {
        LOG("DBManager::init: Not a valid AO folder.");
        return false;
    }

    if (dbfile.empty())
    {
        dbfile = _T("ItemAssistant.db");
    }

    bool dbfileExists = bfs::exists(bfs::tpath(dbfile));

    if (!m_db->Init(dbfile))
    {
        LOG("DBManager::init: Unable to " << (dbfileExists ? "open" : "create") << " database. [" << dbfile << "]");
        return false;
    }

    if (!dbfileExists)
    {
        createDBScheme();
    }

    unsigned int dbVersion = getDBVersion();
    if (dbVersion < CURRENT_DB_VERSION)
    {
        if (IDOK != MessageBox(NULL, _T("AO Item Assistant needs to update its database file to a newer version."),
                               _T("Question - AO Item Assistant++"), MB_OKCANCEL | MB_ICONQUESTION))
        {
            return false;
        }
        updateDBVersion(dbVersion);
    }
    else if (dbVersion > CURRENT_DB_VERSION)
    {
        MessageBox(NULL,
                   _T("AO Item Assistant has detected a too new version of its database file. You should upgrade the software to continue."),
                   _T("Error - AO Item Assistant++"), MB_OK | MB_ICONERROR);
        return false;
    }

    if (!syncLocalItemsDB(_T("aoitems.db"), aofolder))
    {
        MessageBox(NULL, _T("AO Item Assistant cannot start without a valid item database."),
                   _T("Error - AO Item Assistant++"), MB_OK | MB_ICONERROR);
        return false;
    }

    m_db->Exec(_T("ATTACH DATABASE \"aoitems.db\" AS aodb"));

    return true;
}
示例#2
0
// Try to open an existent database
aRTcomponent* aRTconn::OpenDb(SEXP data)
{
	bool silent = Silent; // **

    string dbname = GET_STRING_ELEMENT(data, "dbname");
	bool update   = GET_BOOL_ELEMENT(data,   "update");

	PrintSilent("Connecting to database \'%s\' ... ", dbname.c_str());
	
	Silent = false; // **
	TeDatabase* database = NewTeDatabase();

	// ** things doesn't work fine if this line executes with Silent == true
	// ** strange... I've already tried to change the name of this variable.
    if (database -> connect(Host, User, Password, dbname, Port))
	{
		Silent = silent; // **
		PrintSilentYes;

		string DBversion;
	    TeDatabasePortal* portal = database->getPortal();
	    if(!portal)
	        return false;

	    string sql = " SELECT db_version FROM te_database ";
	    //The database does not have the te_database connection
	    if(!portal->query(sql))
	    {
	        DBversion = "";
	        delete portal;
	        return NULL;
	    }

	    if(!portal->fetchRow())
	    {
	        DBversion = "";
	        delete portal;
	        return NULL;
	    }

	    DBversion = portal->getData(0);

		PrintSilent("Connected to database version %s\n", DBversion.c_str());

		string version, error_msg;
		if( needUpdateDB(database, version) )
		{
			if(update)
			{
				PrintSilent("Updating database \'%s\' to version %s ... ", dbname.c_str(), TeDBVERSION.c_str());
				if( updateDBVersion(database, version, error_msg) )
					{ PrintSilentYes; }
				else
					error("Could not update the database version: %s\n", error_msg.c_str());
			}
			else warning("Database needs to be updated to version %s. Call this function again with update=TRUE, otherwise things may not work correctly\n", TeDBVERSION.c_str());
		}
	}
	else
	{
		Silent = silent; // **
		PrintSilentNo;
		error("Database \'%s\'does not exist.", dbname.c_str());
	}
	
	aRTdb* root = new aRTdb(database);
	return root;
}