Esempio n. 1
0
void DataBaseMgr::Disconnect()
{
	SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
	SQLDisconnect(hdbc);
	HandleDiagnosticRecord(hdbc, SQL_HANDLE_DBC, retcode);

	SQLFreeHandle(SQL_HANDLE_DBC, hdbc);

	SQLFreeHandle(SQL_HANDLE_ENV, henv);
}
Esempio n. 2
0
SQLRETURN DataBaseMgr::Query(std::string str)
{
	int char_id = 0;
	wchar_t char_name[100] = { 0, };
	int char_level = 0;
	float posX = 0, posY = 0;
	int exp = 0;
	int atk = 0;
	int def = 0;
	int hp = 0;

	mNodata = true;

	SQLINTEGER  pIndicators[10];

	retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);

	// Process data
	if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {

		retcode = SQLExecDirect(hstmt,
			// ID -> 1, Name -> 2, CLevel -> 3 column
			//(SQLCHAR*)"INSERT INTO dbo.exp_user_table VALUES (2, 'Hello', 1, 2.2, 3.3, 3, 5, 8)",
			//(SQLCHAR*)"SELECT ID, Nick, CLevel, XPos, YPos, EXP, Attack, Defence FROM dbo.exp_user_table",
			//(SQLCHAR*)"SELECT ID, Nick, CLevel, XPos, YPos, EXP, Attack, Defence FROM dbo.exp_user_table",
			//(SQLCHAR*)"EXEC dbo.SELECT_HIGHLEVEL 10",
			//(SQLCHAR*)"SELECT * FROM dbo.user_table WHERE Nick = 'Hello'",
			(SQLCHAR*)str.c_str(),
			SQL_NTS);

		if (retcode != SQL_ERROR)
		{
			if ((retcode == SQL_NO_DATA) && mNodata)
			{
				std::cout << "NO DATA" << std::endl;
				return retcode;
			}
		}
		else
		{
			// Error Handle
			HandleDiagnosticRecord(hstmt, SQL_HANDLE_STMT, retcode);
		}
	}
	SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
	return SQL_SUCCESS;
}
Esempio n. 3
0
void DataBaseMgr::Init()
{
	int char_id = 0;
	wchar_t char_name[100] = { 0, };
	int char_level = 0;
	float posX = 0, posY = 0;
	int exp = 0;
	int atk = 0;
	int def = 0;
	int hp = 0;

	SQLINTEGER  pIndicators[8];

	//setlocale(LC_ALL, "korean");

	SQLCHAR * OutConnStr = (SQLCHAR *)malloc(255);
	SQLSMALLINT * OutConnStrLen = (SQLSMALLINT *)malloc(255);

	// Allocate environment handle
	retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);

	// Set the ODBC version environment attribute
	if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
		retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);

		// Allocate connection handle
		if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
			retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);

			// Set login timeout to 5 seconds
			if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
				SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0);

				// Connect to data source
				retcode = SQLConnect(hdbc, (SQLCHAR*)"2010180020", SQL_NTS, (SQLCHAR*)"sa", SQL_NTS, (SQLCHAR*)"gamedb", SQL_NTS);

				// Allocate statement handle
				if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
					retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);

					// Process data
					if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {

						std::cout << "Connection Success \n";

						//retcode = SQLExecDirect(hstmt,
						//	// ID -> 1, Name -> 2, CLevel -> 3 column
						//	//(SQLCHAR*)"INSERT INTO dbo.exp_user_table VALUES (2, 'Hello', 1, 2.2, 3.3, 3, 5, 8)",
						//	//(SQLCHAR*)"SELECT ID, Nick, CLevel, XPos, YPos, EXP, Attack, Defence FROM dbo.exp_user_table",
						//	//(SQLCHAR*)"SELECT ID, Nick, CLevel, XPos, YPos, EXP, Attack, Defence FROM dbo.exp_user_table",
						//	//(SQLCHAR*)"EXEC dbo.SELECT_HIGHLEVEL 10",
						//	(SQLCHAR*)"SELECT * FROM dbo.exp_user_table WHERE Nick = 'Hello'",
						//	SQL_NTS);



						//if (retcode != SQL_ERROR)
						//{

						//	// Retrieve data from row set.
						//	SQLBindCol(hstmt, 1, SQL_C_LONG, (SQLPOINTER)&char_id, sizeof(char_id),
						//		&pIndicators[0]);

						//	// Retrieve data from row set.
						//	SQLBindCol(hstmt, 2, SQL_C_WCHAR, (SQLPOINTER)char_name, sizeof(char_name),
						//		&pIndicators[1]);

						//	// Retrieve data from row set.
						//	SQLBindCol(hstmt, 3, SQL_C_LONG, (SQLPOINTER)&char_level, sizeof(char_level),
						//		&pIndicators[2]);

						//	SQLBindCol(hstmt, 4, SQL_C_FLOAT, (SQLPOINTER)&posX, sizeof(posX),
						//		&pIndicators[3]);

						//	SQLBindCol(hstmt, 5, SQL_C_FLOAT, (SQLPOINTER)&posY, sizeof(posY),
						//		&pIndicators[4]);

						//	SQLBindCol(hstmt, 6, SQL_C_LONG, (SQLPOINTER)&exp, sizeof(exp),
						//		&pIndicators[5]);

						//	SQLBindCol(hstmt, 7, SQL_C_LONG, (SQLPOINTER)&atk, sizeof(atk),
						//		&pIndicators[6]);

						//	SQLBindCol(hstmt, 8, SQL_C_LONG, (SQLPOINTER)&def, sizeof(def),
						//		&pIndicators[7]);

						//	while (SQLFetch(hstmt) == SQL_SUCCESS)
						//	{
						//		std::wcout << "char_id: " << char_id << "\n";
						//		std::wcout << "char_name: " << char_name << "\n";
						//		std::wcout << "pos: " << posX << ", " << posY << "\n";
						//		std::wcout << "exp: " << exp << "\n";
						//		std::wcout << "atk: " << atk << "\n";
						//		std::wcout << "def: " << def << "\n";
						//	}
						//}
						if (retcode == SQL_ERROR)
						{
							// Error Handle
							HandleDiagnosticRecord(hstmt, SQL_HANDLE_STMT, retcode);
						}

						//SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
					}

					//SQLDisconnect(hdbc);
				}
				else
				{
					HandleDiagnosticRecord(hdbc, SQL_HANDLE_DBC, retcode);
				}

				//SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
			}
		}
		//SQLFreeHandle(SQL_HANDLE_ENV, henv);
	}
}
Esempio n. 4
0
SQLRETURN DataBaseMgr::Query(std::string str, unsigned int index)
{
	int char_id = 0;
	wchar_t char_name[100] = { 0, };
	int char_level = 0;
	float posX = 0, posY = 0;
	int exp = 0;
	int atk = 0;
	int def = 0;
	int hp = 0;

	mNodata = true;

	SQLINTEGER  pIndicators[10];

	retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);

	// Process data
	if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {

		retcode = SQLExecDirect(hstmt,
			// ID -> 1, Name -> 2, CLevel -> 3 column
			//(SQLCHAR*)"INSERT INTO dbo.exp_user_table VALUES (2, 'Hello', 1, 2.2, 3.3, 3, 5, 8)",
			//(SQLCHAR*)"SELECT ID, Nick, CLevel, XPos, YPos, EXP, Attack, Defence FROM dbo.exp_user_table",
			//(SQLCHAR*)"SELECT ID, Nick, CLevel, XPos, YPos, EXP, Attack, Defence FROM dbo.exp_user_table",
			//(SQLCHAR*)"EXEC dbo.SELECT_HIGHLEVEL 10",
			//(SQLCHAR*)"SELECT * FROM dbo.user_table WHERE Nick = 'Hello'",
			(SQLCHAR*)str.c_str(),
			SQL_NTS);

		if (retcode != SQL_ERROR)
		{

			// Retrieve data from row set.
			SQLBindCol(hstmt, 1, SQL_C_LONG, (SQLPOINTER)&char_id, sizeof(char_id),
				&pIndicators[0]);

			// Retrieve data from row set.
			SQLBindCol(hstmt, 2, SQL_C_WCHAR, (SQLPOINTER)char_name, sizeof(char_name),
				&pIndicators[1]);

			// Retrieve data from row set.
			SQLBindCol(hstmt, 3, SQL_C_LONG, (SQLPOINTER)&char_level, sizeof(char_level),
				&pIndicators[2]);

			SQLBindCol(hstmt, 4, SQL_C_FLOAT, (SQLPOINTER)&posX, sizeof(posX),
				&pIndicators[3]);

			SQLBindCol(hstmt, 5, SQL_C_FLOAT, (SQLPOINTER)&posY, sizeof(posY),
				&pIndicators[4]);

			SQLBindCol(hstmt, 6, SQL_C_LONG, (SQLPOINTER)&exp, sizeof(exp),
				&pIndicators[5]);

			SQLBindCol(hstmt, 7, SQL_C_LONG, (SQLPOINTER)&atk, sizeof(atk),
				&pIndicators[6]);

			SQLBindCol(hstmt, 8, SQL_C_LONG, (SQLPOINTER)&def, sizeof(def),
				&pIndicators[7]);

			SQLBindCol(hstmt, 9, SQL_C_LONG, (SQLPOINTER)&hp, sizeof(hp),
				&pIndicators[8]);

			while ((retcode = SQLFetch(hstmt)) == SQL_SUCCESS)
			{
				CLIENT(index).info.mPos.x = posX;
				CLIENT(index).info.mPos.y = posY;
				CLIENT(index).info.hp = hp;
				CLIENT(index).info.atk = atk;
				CLIENT(index).info.def = def;
				CLIENT(index).info.exp = exp;
				CLIENT(index).info.id = char_id;
				CLIENT(index).info.level = char_level;
				wcscpy_s(CLIENT(index).info.char_id, char_name);


				std::wcout << "char_id: " << char_id << "\n";
				std::wcout << "char_name: " << char_name << "\n";
				std::wcout << "pos: " << posX << ", " << posY << "\n";
				std::wcout << "exp: " << exp << "\n";
				std::wcout << "atk: " << atk << "\n";
				std::wcout << "def: " << def << "\n";
				std::wcout << "hp: " << hp << "\n";

				mNodata = false;
			}

			if ((retcode == SQL_NO_DATA) && mNodata)
			{
				std::cout << "NO DATA" << std::endl;
				return retcode;
			}
		}
		else
		{
			// Error Handle
			HandleDiagnosticRecord(hstmt, SQL_HANDLE_STMT, retcode);
		}
	}
	SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
	return SQL_SUCCESS;
}
Esempio n. 5
0
int main(int argc, char **argv)
{
  SQLHENV     hEnv = NULL;
  SQLHDBC     hDbc = NULL;
  SQLHSTMT    hStmt = NULL;
  char*       pConnStr;
  char        pQuery[1000];
  bool        silent = true;

  if (argc != 5) {
    fprintf(stderr, "Usage: %s <ConnString> <pkey range> <ccol range> <rand seed>\n", argv[0]);
    return 1;
  }
  pConnStr = argv[1];
  char *endptr;
  long long numkeys = strtoll(argv[2], &endptr, 10);
  long long rowsperkey = strtoll(argv[3], &endptr, 10);
  int seed = atoi(argv[4]);
  struct drand48_data lcg;
  srand48_r(seed, &lcg);

  // Allocate an environment
  if (!silent)
    fprintf(stderr, "Allocating Handle Enviroment\n");
  if (SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv) == SQL_ERROR)
    {
      fprintf(stderr, "Unable to allocate an environment handle\n");
      exit(-1);
    }

  // Register this as an application that expects 3.x behavior,
  // you must register something if you use AllocHandle
  if (!silent)
    fprintf(stderr, "Setting to ODBC3\n");
  TRYODBC(hEnv,
	  SQL_HANDLE_ENV,
	  SQLSetEnvAttr(hEnv,
			SQL_ATTR_ODBC_VERSION,
			(SQLPOINTER)SQL_OV_ODBC3,
			0));

  // Allocate a connection
  if (!silent)
    fprintf(stderr, "Allocating Handle\n");
  TRYODBC(hEnv,
	  SQL_HANDLE_ENV,
	  SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc));

  // Connect to the driver.  Use the connection string if supplied
  // on the input, otherwise let the driver manager prompt for input.
  if (!silent)
    fprintf(stderr, "Connecting to driver\n");
  TRYODBC(hDbc,
	  SQL_HANDLE_DBC,
	  SQLDriverConnect(hDbc,
			   NULL,
			   pConnStr,
			   SQL_NTS,
			   NULL,
			   0,
			   NULL,
			   SQL_DRIVER_COMPLETE));

  fprintf(stderr, "Connected!\n");

  if (!silent)
    fprintf(stderr, "Allocating statement\n");
  TRYODBC(hDbc,
	  SQL_HANDLE_DBC,
	  SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStmt));

  RETCODE     RetCode;
  SQLSMALLINT sNumResults;

  // Execute the query
  if (!silent)
    fprintf(stderr, "Executing query\n");
  long long i;
  double rval;
  for (i = 0; i < 100000; i++) {
    drand48_r(&lcg, &rval);
    sprintf(pQuery, "SELECT MAX(col1) FROM otest.test10 WHERE ccol = %lld", (long long)(rval * numkeys));
    RetCode = SQLExecDirect(hStmt, pQuery, SQL_NTS);

    switch(RetCode)
      {
      case SQL_SUCCESS_WITH_INFO:
	{
	  HandleDiagnosticRecord(hStmt, SQL_HANDLE_STMT, RetCode);
	  // fall through
	}
      case SQL_SUCCESS:
	{
	  // If this is a row-returning query, display
	  // results
	  TRYODBC(hStmt,
		  SQL_HANDLE_STMT,
		  SQLNumResultCols(hStmt,&sNumResults));
	  
	  if (sNumResults > 0)
	    {
	      DisplayResults(hStmt,sNumResults, silent);
	    } 
	  else
	    {
	      SQLLEN cRowCount;
	      
	      TRYODBC(hStmt,
		      SQL_HANDLE_STMT,
		      SQLRowCount(hStmt,&cRowCount));
	      
	      if (cRowCount >= 0)
		{
		  printf("%d %s returned\n",
			 (int)cRowCount,
			 (cRowCount == 1) ? "row" : "rows");
		}
	    }
	  break;
	}
	
      case SQL_ERROR:
	{
	  HandleDiagnosticRecord(hStmt, SQL_HANDLE_STMT, RetCode);
	  break;
	}
	
      default:
	fprintf(stderr, "Unexpected return code %hd!\n", RetCode);
	
      }
  }

  TRYODBC(hStmt,
	  SQL_HANDLE_STMT,
	  SQLFreeStmt(hStmt, SQL_CLOSE));

 Exit:

  // Free ODBC handles and exit

  if (hStmt)
    {
      SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
    }

  if (hDbc)
    {
      SQLDisconnect(hDbc);
      SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
    }

  if (hEnv)
    {
      SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
    }

  return 0;

}
Esempio n. 6
0
//int __cdecl wmain(int argc, _In_reads_(argc) WCHAR **argv)
//int __cdecl wmain(int argc, WCHAR **argv)
int __cdecl wmain2(int argc, WCHAR **argv)
{
    SQLHENV     hEnv = NULL;
    SQLHDBC     hDbc = NULL;
    SQLHSTMT    hStmt = NULL;
    WCHAR*      pwszConnStr;
    WCHAR       wszInput[SQL_QUERY_SIZE];

    // Allocate an environment

    if (SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv) == SQL_ERROR)
    {
        fwprintf(stderr, L"Unable to allocate an environment handle\n");
        exit(-1);
    }

    // Register this as an application that expects 3.x behavior,
    // you must register something if you use AllocHandle

    TRYODBC(hEnv,
            SQL_HANDLE_ENV,
            SQLSetEnvAttr(hEnv,
                SQL_ATTR_ODBC_VERSION,
                (SQLPOINTER)SQL_OV_ODBC3,
                0));

    // Allocate a connection
    TRYODBC(hEnv,
            SQL_HANDLE_ENV,
            SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc));

    if (argc > 1)
    {
        pwszConnStr = *++argv;
    } 
    else
    {
        pwszConnStr = L"";
    }

    // Connect to the driver.  Use the connection string if supplied
    // on the input, otherwise let the driver manager prompt for input.

    TRYODBC(hDbc,
        SQL_HANDLE_DBC,
        SQLDriverConnect(hDbc,
                         GetDesktopWindow(),
                         pwszConnStr,
                         SQL_NTS,
                         NULL,
                         0,
                         NULL,
                         SQL_DRIVER_COMPLETE));

    fwprintf(stderr, L"Connected!\n");

    TRYODBC(hDbc,
            SQL_HANDLE_DBC,
            SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStmt));

    wprintf(L"Enter SQL commands, type (control)Z to exit\nSQL COMMAND>");

    // Loop to get input and execute queries

    while(_fgetts(wszInput, SQL_QUERY_SIZE-1, stdin))
    {
        RETCODE     RetCode;
        SQLSMALLINT sNumResults;

        // Execute the query

        if (!(*wszInput))
        {
            wprintf(L"SQL COMMAND>");
            continue;
        }
        RetCode = SQLExecDirect(hStmt,wszInput, SQL_NTS);

        switch(RetCode)
        {
        case SQL_SUCCESS_WITH_INFO:
            {
                HandleDiagnosticRecord(hStmt, SQL_HANDLE_STMT, RetCode);
                // fall through
            }
        case SQL_SUCCESS:
            {
                // If this is a row-returning query, display
                // results
                TRYODBC(hStmt,
                        SQL_HANDLE_STMT,
                        SQLNumResultCols(hStmt,&sNumResults));

                if (sNumResults > 0)
                {
                    DisplayResults(hStmt,sNumResults);
                } 
                else
                {
                    SQLLEN cRowCount;

                    TRYODBC(hStmt,
                        SQL_HANDLE_STMT,
                        SQLRowCount(hStmt,&cRowCount));

                    if (cRowCount >= 0)
                    {
                        wprintf(L"%Id %s affected\n",
                                 cRowCount,
                                 cRowCount == 1 ? L"row" : L"rows");
                    }
                }
                break;
            }

        case SQL_ERROR:
            {
                HandleDiagnosticRecord(hStmt, SQL_HANDLE_STMT, RetCode);
                break;
            }

        default:
            fwprintf(stderr, L"Unexpected return code %hd!\n", RetCode);

        }
        TRYODBC(hStmt,
                SQL_HANDLE_STMT,
                SQLFreeStmt(hStmt, SQL_CLOSE));

        wprintf(L"SQL COMMAND>");
    }

Exit:

    // Free ODBC handles and exit

    if (hStmt)
    {
        SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
    }

    if (hDbc)
    {
        SQLDisconnect(hDbc);
        SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
    }

    if (hEnv)
    {
        SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
    }

    wprintf(L"\nDisconnected.");

    return 0;

}