extern int RelationalUpdatePlayerDetails(const char* oldName, const char* newName, const char* newNotes)
{
	int ret = FALSE;
	int exist_id, player_id;
	DBProvider *pdb;
	if ((pdb = ConnectToDB(dbProviderType)) == NULL)
		return FALSE;

	player_id = GetPlayerId(pdb, oldName);
	exist_id = GetPlayerId(pdb, newName);
	if (exist_id != player_id && exist_id != -1)
	{	/* Can't change the name to an existing one */
		outputerrf( _("New player name already exists.") );
	}
	else
	{
		char *buf = g_strdup_printf("UPDATE player SET name = '%s', notes = '%s' WHERE player_id = %d",
									newName, newNotes, player_id);
		if (pdb->UpdateCommand(buf))
		{
			ret = 1;
			pdb->Commit();
		}
		else
			outputerrf( _("Error running database command") );
		g_free(buf);
	}
	pdb->Disconnect();
	return ret;
}
CString CIntOptionDAO::GetCityID()
{
	if(!ConnectToDB("root","","localhost","ddm"))
	{
		return NULL;
	}
	


	CStringA cstrSelectQuery("SELECT Value FROM intoptiontb where OptionName = 'mcl';");

	MYSQL_RES *pMySQLQueryResult =  m_pSQLDataAccessHelper->ExecuteQuery(cstrSelectQuery);

	if(NULL == pMySQLQueryResult)
	{
		return FALSE;
	}

	MYSQL_ROW mySQLResultRow;
	if((mySQLResultRow = mysql_fetch_row(pMySQLQueryResult)))
	{
		CStringA strTemp(mySQLResultRow[0]);
		CString strCityID(strTemp);
		m_pSQLDataAccessHelper->FreeQueryResult(pMySQLQueryResult);
		return strCityID;
	}

	m_pSQLDataAccessHelper->FreeQueryResult(pMySQLQueryResult);
	return NULL;
}
Exemple #3
0
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormShow(TObject *Sender)
{
	PATH = SettingsFile->ReadString("MainForm","PATH",GetCurrentDir()+"\\cmdata.mdb");

	if (!FileExists(PATH)) {
		 OpenDialog->FileName = PATH;
		 if (OpenDialog->Execute()) {
			PATH = OpenDialog->FileName;
			ConnectToDB();
		 } else {
			MessageDlg("Файл '" + PATH + "' не найден!", mtError, TMsgDlgButtons() << mbOK, 0);
            Application->Terminate();
		 }
	} else {
		ConnectToDB();
	}
	ToolBarPager->Caption->Caption = "ChurchManager 4.0 [" + PATH + "]";

	if (SettingsFile->ReadBoolean("MainForm", "WindowStateMaximized", 0))
		WindowState = wsMaximized;
	else {
		Left = SettingsFile->ReadInteger("MainForm", "Left", Screen->WorkAreaWidth - Width);
		Top = SettingsFile->ReadInteger("MainForm", "Top", Screen->WorkAreaHeight - Height);
	}
	styleIndex = SettingsFile->ReadInteger("MainForm", "StyleIndex", 1);
	if (styleIndex == 1) {
		LunaMenuItemClick(this);
		LunaMenuItem->Checked = true;
	}
	else if (styleIndex == 2) {
		SilverMenuItemClick(this);
		SilverMenuItem->Checked = true;
	}
	else if (styleIndex == 3) {
		BlueMenuItemClick(this);
		BlueMenuItem->Checked = true;
	}
	else if (styleIndex == 4) {
		OliveMenuItemClick(this);
		OliveMenuItem->Checked = true;
	}
	else {
		Silver2MenuItemClick(this);
		Silver2MenuItem->Checked = true;
	}
}
extern RowSet* RunQuery(const char *sz)
{
	DBProvider *pdb;
	if ((pdb = ConnectToDB(dbProviderType)) != NULL)
	{
		RowSet *rs = pdb->Select(sz);
		pdb->Disconnect();
		return rs;
	}
	return NULL;
}
extern void CommandRelationalErase(char *sz)
{
	char *mq, *gq, buf[1024];
	DBProvider *pdb;
	char *player_name;
	int player_id;
	if (!sz || !*sz || ((player_name = NextToken(&sz)) == NULL))
	{
		outputl( _("You must specify a player name to remove "
		"(see `help relational erase player').") );
		return;
	}
	if ((pdb = ConnectToDB(dbProviderType)) == NULL)
		return;

	player_id = GetPlayerId(pdb, player_name);
	if (player_id == -1)
	{
		outputl (_("Player not found or player stats empty"));
		return;
	}
	/* Get all matches involving player */
	mq = g_strdup_printf ("FROM session WHERE player_id0 = %d OR player_id1 = %d", player_id, player_id);

	/* first remove any gamestats and games */
	gq = g_strdup_printf ("FROM game WHERE session_id in (select session_id %s)", mq);

	sprintf(buf, "DELETE FROM gamestat WHERE game_id in (select game_id %s)", gq);
	pdb->UpdateCommand(buf);
	sprintf(buf, "DELETE %s", gq);
	pdb->UpdateCommand(buf);

	/* Now remove any matchstats */
	sprintf(buf, "DELETE FROM matchstat WHERE session_id in (select session_id %s)", mq);
	pdb->UpdateCommand(buf);

	/* then remove any matches */
	sprintf(buf, "DELETE %s", mq);
	pdb->UpdateCommand(buf);

	/* then the player */
	sprintf(buf, "DELETE FROM player WHERE player_id = %d", player_id);
	pdb->UpdateCommand(buf);

	g_free(mq);
	g_free(gq);
	pdb->Commit();
	pdb->Disconnect();
}
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setCentralWidget(ui->centralWidget);
    ui->centralWidget->layout();
    connectSignals();
    
    db = QSqlDatabase::addDatabase("QIBASE");
    tablemodel = NULL;
    fieldsmodel = NULL;

    ConnectToDB();

    ReadTables();
}
Exemple #7
0
DWORD CDBPSServer::InitDBCQueue(DWORD dwNumberOfConnection)
{
	extern CDBCQueue* g_pCDBCQueue;
	g_pCDBCQueue = (new CDBCQueue())->getQueueInstance();

	for (unsigned int i = 0; i < dwNumberOfConnection; i++) {
		ST_DBConnection stDBConnection;
		HANDLE hDataBase = CreateDBInstance(E_DB_POSTGRES);
		ConnectToDB(hDataBase, m_stDBLoginToken);
		stDBConnection.hDataBase = hDataBase;

		DebugLog("Create DB Connection [%d]", i + 1);
		g_pCDBCQueue->pushToQueue(stDBConnection);
		::Sleep(1000);
	}
	return E_RET_SUCCESS;
}
const char *
TestDB(DBProviderType dbType)
{
    char *ret = NULL;
    DBProvider *pdb;
    RowSet *rs;
    if ((pdb = ConnectToDB(dbType)) == NULL)
        return _("Database connection test failed, installation problem!\n");

    /* Check main table is present */
    rs = pdb->Select("COUNT(*) from session");
    if (rs) {                   /* Success */
        FreeRowset(rs);
    } else
        ret = (_("Database table check failed!\n" "The session table is missing."));

    pdb->Disconnect();
    return ret;
}
int MainWindow::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    _id = QMainWindow::qt_metacall(_c, _id, _a);
    if (_id < 0)
        return _id;
    if (_c == QMetaObject::InvokeMetaMethod) {
        switch (_id) {
        case 0: ConnectToDB(); break;
        case 1: ReadTables(); break;
        case 2: ReadFields((*reinterpret_cast< const QModelIndex(*)>(_a[1]))); break;
        case 3: on_pushButton_5_clicked(); break;
        case 4: on_pushButton_4_clicked(); break;
        case 5: on_pushButton_3_clicked(); break;
        case 6: on_pushButton_2_clicked(); break;
        case 7: on_pushButton_clicked(); break;
        default: ;
        }
        _id -= 8;
    }
    return _id;
}
extern void CommandRelationalEraseAll(char *UNUSED(sz))
{
	DBProvider *pdb;

	if( fConfirmSave && !GetInputYN( _("Are you sure you want to erase all "
				       "player records?") ) )
		return;

	if ((pdb = ConnectToDB(dbProviderType)) == NULL)
		return;

	/* first remove all matchstats */
	pdb->UpdateCommand("DELETE FROM matchstat");

	/* then remove all matches */
	pdb->UpdateCommand("DELETE FROM session");
      
	/* finally remove the players */
	pdb->UpdateCommand("DELETE FROM player");

	pdb->Commit();
	pdb->Disconnect();
}
extern statcontext *relational_player_stats_get(const char *player0, const char *player1)
{
	int id0 = -1;
	int id1 = -1;
	DBProvider *pdb = NULL;
	char *query[2];
	int i;
	char *buf;
	RowSet *rs;
	statcontext *psc;

	g_return_val_if_fail(player0, NULL);

	if ((pdb = ConnectToDB(dbProviderType)) == NULL)
		return NULL;

	id0 = GetPlayerId(pdb, player0);
	if (player1)
		id1 = GetPlayerId(pdb, player1);
	if (id0 == -1 || (player1 && id1 == -1))
		return NULL;

	psc = g_new0(statcontext, 1);

	if (!player1) {
		query[0] =
		    g_strdup_printf("where matchstat.player_id = %d", id0);
		query[1] =
		    g_strdup_printf("NATURAL JOIN session WHERE "
				    "(session.player_id0 = %d OR session.player_id1 = %d) "
				    "AND matchstat.player_id != %d", id0, id0,
				    id0);
	} else {
		query[0] = g_strdup_printf("NATURAL JOIN session WHERE "
					   "((session.player_id0 = %d OR session.player_id1 = %d) "
					   " AND "
					   " (session.player_id0 = %d OR session.player_id1 = %d))"
					   "AND matchstat.player_id = %d", id0,
					   id0, id1, id1, id0);
		query[1] =
		    g_strdup_printf("NATURAL JOIN session WHERE "
				    "((session.player_id0 = %d OR session.player_id1 = %d) "
				    " AND "
				    " (session.player_id0 = %d OR session.player_id1 = %d))"
				    "AND matchstat.player_id = %d", id0, id0,
				    id1, id1, id1);
	}

	IniStatcontext(psc);
	for (i = 0; i < 2; ++i) {
		buf = g_strdup_printf("SUM(total_moves),"
				      "SUM(unforced_moves),"
				      "SUM(total_cube_decisions),"
				      "SUM(close_cube_decisions),"
				      "SUM(doubles),"
				      "SUM(takes),"
				      "SUM(passes),"
				      "SUM(very_bad_moves),"
				      "SUM(bad_moves),"
				      "SUM(doubtful_moves),"
				      "SUM(unmarked_moves),"
				      "SUM(very_unlucky_rolls),"
				      "SUM(unlucky_rolls),"
				      "SUM(unmarked_rolls),"
				      "SUM(lucky_rolls),"
				      "SUM(very_lucky_rolls),"
				      "SUM(missed_doubles_below_cp),"
				      "SUM(missed_doubles_above_cp),"
				      "SUM(wrong_doubles_below_dp),"
				      "SUM(wrong_doubles_above_tg),"
				      "SUM(wrong_takes),"
				      "SUM(wrong_passes),"
				      "SUM(chequer_error_total_normalised),"
				      "SUM(error_missed_doubles_below_cp_normalised),"
				      "SUM(error_missed_doubles_above_cp_normalised),"
				      "SUM(error_wrong_doubles_below_dp_normalised),"
				      "SUM(error_wrong_doubles_above_tg_normalised),"
				      "SUM(error_wrong_takes_normalised),"
				      "SUM(error_wrong_passes_normalised),"
				      "SUM(luck_total_normalised)"
				      "from matchstat " "%s", query[i]);
		rs = pdb->Select(buf);
		g_free(buf);
		if ((!rs) || !strtol(rs->data[1][0], NULL, 0))
			return NULL;
		psc->anTotalMoves[i] = strtol(rs->data[1][0], NULL, 0);
		psc->anUnforcedMoves[i] = strtol(rs->data[1][1], NULL, 0);
		psc->anTotalCube[i] = strtol(rs->data[1][2], NULL, 0);
		psc->anCloseCube[i] = strtol(rs->data[1][3], NULL, 0);
		psc->anDouble[i] = strtol(rs->data[1][4], NULL, 0);
		psc->anTake[i] = strtol(rs->data[1][5], NULL, 0);
		psc->anPass[i] = strtol(rs->data[1][6], NULL, 0);
		psc->anMoves[i][SKILL_VERYBAD] =
		    strtol(rs->data[1][7], NULL, 0);
		psc->anMoves[i][SKILL_BAD] = strtol(rs->data[1][8], NULL, 0);
		psc->anMoves[i][SKILL_DOUBTFUL] =
		    strtol(rs->data[1][9], NULL, 0);
		psc->anMoves[i][SKILL_NONE] = strtol(rs->data[1][10], NULL, 0);
		psc->anLuck[i][LUCK_VERYBAD] = strtol(rs->data[1][11], NULL, 0);
		psc->anLuck[i][LUCK_BAD] = strtol(rs->data[1][12], NULL, 0);
		psc->anLuck[i][LUCK_NONE] = strtol(rs->data[1][13], NULL, 0);
		psc->anLuck[i][LUCK_GOOD] = strtol(rs->data[1][14], NULL, 0);
		psc->anLuck[i][LUCK_VERYGOOD] =
		    strtol(rs->data[1][15], NULL, 0);
		psc->anCubeMissedDoubleDP[i] = strtol(rs->data[1][16], NULL, 0);
		psc->anCubeMissedDoubleTG[i] = strtol(rs->data[1][17], NULL, 0);
		psc->anCubeWrongDoubleDP[i] = strtol(rs->data[1][18], NULL, 0);
		psc->anCubeWrongDoubleTG[i] = strtol(rs->data[1][19], NULL, 0);
		psc->anCubeWrongTake[i] = strtol(rs->data[1][20], NULL, 0);
		psc->anCubeWrongPass[i] = strtol(rs->data[1][21], NULL, 0);
		psc->arErrorCheckerplay[i][0] =
		    (float)g_ascii_strtod(rs->data[1][22], NULL);
		psc->arErrorMissedDoubleDP[i][0] =
		    (float)g_ascii_strtod(rs->data[1][23], NULL);
		psc->arErrorMissedDoubleTG[i][0] =
		    (float)g_ascii_strtod(rs->data[1][24], NULL);
		psc->arErrorWrongDoubleDP[i][0] =
		    (float)g_ascii_strtod(rs->data[1][25], NULL);
		psc->arErrorWrongDoubleTG[i][0] =
		    (float)g_ascii_strtod(rs->data[1][26], NULL);
		psc->arErrorWrongTake[i][0] =
		    (float)g_ascii_strtod(rs->data[1][27], NULL);
		psc->arErrorWrongPass[i][0] =
		    (float)g_ascii_strtod(rs->data[1][28], NULL);
		psc->arLuck[i][0] =
		    (float)g_ascii_strtod(rs->data[1][29], NULL);
		FreeRowset(rs);
	}
	psc->fMoves = 1;
	psc->fCube = 1;
	psc->fDice = 1;

	return psc;
}
extern void CommandRelationalAddMatch(char *sz)
{
	DBProvider *pdb;
	char *buf, *buf2, *date;
	char warnings[1024] = "";
	int session_id, existing_id, player_id0, player_id1;
	char *arg = NULL;
	gboolean quiet = FALSE;

	arg = NextToken(&sz);
	if (arg)
		quiet = !strcmp(arg, "quiet");

	if (ListEmpty(&lMatch))
	{
		outputl( _("No match is being played.") );
		return;
	}

	/* Warn if match is not finished or fully analyzed */
	if (!quiet && !GameOver())
		strcat(warnings, _("The match is not finished\n"));
	if (!quiet && !MatchAnalysed())
		strcat(warnings, _("All of the match is not analyzed\n"));

	if (*warnings)
	{
		strcat(warnings, _("\nAdd match anyway?"));
		if (!GetInputYN(warnings))
			return;
	}

	if ((pdb = ConnectToDB(dbProviderType)) == NULL)
		return;

	existing_id = RelationalMatchExists(pdb);
	if (existing_id != -1)
	{
		if (!quiet && !GetInputYN(_("Match exists, overwrite?")))
			return;

		/* Remove any game stats and games */
		buf2 = g_strdup_printf("FROM game WHERE session_id = %d", existing_id);
		buf = g_strdup_printf("DELETE FROM gamestat WHERE game_id in (SELECT game_id %s)", buf2);
		pdb->UpdateCommand(buf);
		g_free(buf);
		buf = g_strdup_printf("DELETE %s", buf2);
		pdb->UpdateCommand(buf);
		g_free(buf);
		g_free(buf2);

		/* Remove any match stats and session */
		buf = g_strdup_printf("DELETE FROM matchstat WHERE session_id = %d", existing_id);
		pdb->UpdateCommand(buf);
		g_free(buf);
		buf = g_strdup_printf("DELETE FROM session WHERE session_id = %d", existing_id);
		pdb->UpdateCommand(buf);
		g_free(buf);
	}

	session_id = GetNextId(pdb, "session");
	player_id0 = AddPlayer(pdb, ap[0].szName);
	player_id1 = AddPlayer(pdb, ap[1].szName);
	if (session_id == -1 || player_id0 == -1 || player_id1 == -1)
	{
		outputl( _("Error adding match.") );
		return;
	}

	if( mi.nYear )
		date = g_strdup_printf("%04d-%02d-%02d", mi.nYear, mi.nMonth, mi.nDay);
	else
		date = NULL;

	buf = g_strdup_printf("INSERT INTO session(session_id, checksum, player_id0, player_id1, "
              "result, length, added, rating0, rating1, event, round, place, annotator, comment, date) "
              "VALUES (%d, '%s', %d, %d, %d, %d, CURRENT_TIMESTAMP, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
				session_id, GetMatchCheckSum(), player_id0, player_id1,
				MatchResult(ms.nMatchTo), ms.nMatchTo, NS(mi.pchRating[0]), NS(mi.pchRating[1]),
				NS(mi.pchEvent), NS(mi.pchRound), NS(mi.pchPlace), NS(mi.pchAnnotator), NS(mi.pchComment), NS(date));

	updateStatisticsMatch ( &lMatch );

	if (pdb->UpdateCommand(buf))
	{
		if (AddStats(pdb, session_id, player_id0, 0, "matchstat", ms.nMatchTo, &scMatch) &&
			AddStats(pdb, session_id, player_id1, 1, "matchstat", ms.nMatchTo, &scMatch))
		{
			if (storeGameStats)
				AddGames(pdb, session_id, player_id0, player_id1);
			pdb->Commit();
		}
	}
	g_free(buf);
	g_free(date);
	pdb->Disconnect();
}
Exemple #13
0
TEST(DATABASE, SELECT_SQL)
{
	ST_DB_LOGIN_TOKEN stDBLoginToken;
	stDBLoginToken.strDatabaseName = "postgres";
	stDBLoginToken.strDatabaseIP = "165.132.120.155";
	stDBLoginToken.strUserName = "******";
	stDBLoginToken.strPassword = "******";
	stDBLoginToken.strPort = "5432";

	HANDLE hDataBase = NULL;
	hDataBase = CreateDBInstance(E_DB_POSTGRES);
	ASSERT_TRUE(hDataBase != NULL);

	DWORD dwRet;
	dwRet = ConnectToDB(hDataBase, stDBLoginToken);
	ASSERT_TRUE(dwRet == E_RET_SUCCESS);

	/*
		Client Table
	*/
	ST_DB_SQL stDBQuerySQLFromClient;
	stDBQuerySQLFromClient.strSQL = "SELECT clientid, clientmail, clientphonenumber, \"clientNumber\" FROM \"client\" WHERE clientid = \'test\'";
	ST_DB_RESULT stDBResult;
	dwRet = QueryFromDB(hDataBase, stDBQuerySQLFromClient, stDBResult);
	ASSERT_TRUE(dwRet == E_RET_SUCCESS);

	if (stDBResult.vecstrResult.size() < 1) {
		return;
	}

	/*
		Comment Table
	*/
	ST_DB_SQL stDBQuerySQLFromComment;
	stDBQuerySQLFromComment.strSQL = "SELECT commentnumber, clientid, commenttime, commentcotent, storenumber FROM \"comment\" WHERE clientid = \'test\'";
	stDBResult.vecstrResult.clear();
	dwRet = QueryFromDB(hDataBase, stDBQuerySQLFromComment, stDBResult);
	ASSERT_TRUE(dwRet == E_RET_SUCCESS);

	if (stDBResult.vecstrResult.size() < 1) {
		return;
	}

	/*
		Menu Table
	*/
	ST_DB_SQL stDBQuerySQLFromMenu;
	stDBQuerySQLFromMenu.strSQL = "SELECT menunumber, menuname, menuprice, storename FROM \"menu\" WHERE menuname = \'test\'";
	stDBResult.vecstrResult.clear();
	dwRet = QueryFromDB(hDataBase, stDBQuerySQLFromMenu, stDBResult);
	ASSERT_TRUE(dwRet == E_RET_SUCCESS);

	/*
		Shop Table
	*/
	ST_DB_SQL stDBQuerySQLFromShop;
	stDBQuerySQLFromShop.strSQL = "SELECT shopnumber, shopname, shopaddress, shopphonenumber, shopcategory, \
		shophompage, shoprunningtime, shopholiday, shopetc, shopsmoking,\
		shopparking, shopalcohol, shopfacility, shopintroduction FROM \"shop\" WHERE shopname = \'test\'";
	stDBResult.vecstrResult.clear();
	dwRet = QueryFromDB(hDataBase, stDBQuerySQLFromShop, stDBResult);
	ASSERT_TRUE(dwRet == E_RET_SUCCESS);

	dwRet = QuitDB(hDataBase);
	ASSERT_TRUE(dwRet == E_RET_SUCCESS);
}