Merry Christmas

MFC - DB 클라이언트

시스템 2008. 11. 19. 14:07 |

typedef struct stSql_Connection{
 int    m_iState;
 SQLHENV   m_henv;
 SQLHDBC   m_hdbc;
 stSql_Connection(){
  m_iState = USING_FALSE;
  m_henv = NULL;
  m_hdbc = NULL;
 }
}stSQL_CONNECTION;


// Databae Connection Function
BOOL DBConnection(stSQL_CONNECTION *pSqlConnection){
 SQLRETURN SqlReturn;

 if(NULL != pSqlConnection){
  
  SqlReturn = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &pSqlConnection->m_henv);
  if(SQL_SUCCESS != SqlReturn && SQL_SUCCESS_WITH_INFO != SqlReturn){
   LogSender("DataBase", "ODBC 환경 설정을 위한 메모리 할당 실패-DBConnection", FALSE);
   return FALSE;
  }

  SqlReturn = SQLSetEnvAttr(pSqlConnection->m_henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
  if(SQL_SUCCESS != SqlReturn && SQL_SUCCESS_WITH_INFO != SqlReturn){
   LogSender("DataBase", "ODBC 환경 속성 설정 실패-DBConnection", FALSE);
   return FALSE;
  }

  SqlReturn = SQLAllocHandle(SQL_HANDLE_DBC, pSqlConnection->m_henv, &pSqlConnection->m_hdbc);
  if(SQL_SUCCESS != SqlReturn && SQL_SUCCESS_WITH_INFO != SqlReturn){
   return FALSE;
  }

  SQLSetConnectAttr(pSqlConnection->m_hdbc, 5, (void*)SQL_LOGIN_TIMEOUT, 0);
  SqlReturn = SQLConnect(pSqlConnection->m_hdbc, (SQLCHAR*)ODBCNAME, SQL_NTS, (SQLCHAR*)DBACCOUNT,
   SQL_NTS, (SQLCHAR*)DBPASSWORD, SQL_NTS);
  if(SQL_SUCCESS != SqlReturn && SQL_SUCCESS_WITH_INFO != SqlReturn){
   return FALSE;
  }
 }
 else
  return FALSE;

 return TRUE;
}

 

/* DB 연결 */
int DBDisConnection(stSQL_CONNECTION *pSqlConnection){
 if(NULL != pSqlConnection){
  SQLDisconnect(pSqlConnection->m_hdbc);
  SQLFreeHandle(SQL_HANDLE_DBC, pSqlConnection->m_hdbc);
  SQLFreeHandle(SQL_HANDLE_ENV, pSqlConnection->m_henv);
 }
 else
  return FALSE;

 return TRUE;
}

 

BOOL GetGroupInfo(pstPERUSERINFO pPerUserInfo){
 if(USING_TRUE == pPerUserInfo->m_iUsingState){
  SQLHSTMT hStmt;
  SQLRETURN sqlRtn;
  SQLINTEGER *sqlLen = NULL;
  char szSqlQuery[BUFSIZE_128];
  int iDBRtn = DBHandleRtn();
  int iRtn = FALSE;

  while(TRUE){
   ZeroMemory(szSqlQuery, BUFSIZE_128);
   sqlRtn = SQLAllocHandle(SQL_HANDLE_STMT, m_pSqlConnection[iDBRtn]->m_hdbc, &hStmt);
   if(SQL_SUCCESS != sqlRtn && SQL_SUCCESS_WITH_INFO != sqlRtn){
    LogSender("DataBase", "SQLAllocHandle() Error!! - GetGroupInfo()", sqlRtn);
    m_pSqlConnection[iDBRtn]->m_iState = USING_FALSE;
    
    return FALSE;
   }
   
   sprintf(szSqlQuery, "USP_CHAT_GetGroupInfo '%s'", pPerUserInfo->m_szNickName);
   sqlRtn = SQLExecDirect(hStmt, (SQLCHAR*)szSqlQuery, SQL_NTS);
   if(SQL_SUCCESS == sqlRtn || SQL_SUCCESS_WITH_INFO == sqlRtn){
    for(int i = 0; i < MAXGROUPCNT; i++){
     SQLBindCol(hStmt, COLUMNNUM_1, SQL_INTEGER, &iRtn, sizeof(int), sqlLen);
     SQLBindCol(hStmt, COLUMNNUM_2, SQL_C_CHAR, &pPerUserInfo->m_szGroupInfo[i], GROUPSIZE, sqlLen);

     sqlRtn = SQLFetch(hStmt);
     if(SQL_NO_DATA == sqlRtn)
      break;
     
     if(0 == strlen(pPerUserInfo->m_szGroupInfo[0])){
      CopyMemory(pPerUserInfo->m_szGroupInfo[i], "기타", 4);
      break;
     }

     if(-1 == iRtn || -2 == iRtn){
      SQLFreeStmt(hStmt, SQL_DROP);
      m_pSqlConnection[iDBRtn]->m_iState = USING_FALSE;
      return FALSE;
     }
    }
    break;
   }
   else{
    SQLFreeStmt(hStmt, SQL_DROP);
//    m_pSqlConnection[iDBRtn]->m_iState = USING_FALSE;
//    return FALSE;
    DBDisConnection(m_pSqlConnection[iDBRtn]);
    DBConnection(m_pSqlConnection[iDBRtn]);
   }
  }
  SQLFreeStmt(hStmt, SQL_DROP);
  m_pSqlConnection[iDBRtn]->m_iState = USING_FALSE;
 }

 return TRUE;
}

: