1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
// ////////////////////////////////////////////////
// INITIALIZE GRID 4 : TABLE ACCES //
////////////////////////////////////////////////////
SQLHDBC EU_Create_HdbcACCES(LPSTR pstrFileName )
{ CDaoDatabase m_database;
CString m_strDatabase;
CString m_strQuery;
CDaoTableDefInfo tdi;
int i, NumDial,NumWin,y, Result,nRows ;
char *szBuffer ;
SQLHDBC hdbc ; //hdbc
SQLRETURN nResult; //result code
SQLHSTMT hstmt ;
HENV henv;
RETCODE retcode;
BOOL IiSheet=FALSE;
CHAR szDSNName[SQL_MAX_DSN_LENGTH + 1];
CHAR szDSNNameDrivers[SQL_MAX_DSN_LENGTH + 1];
CHAR szLegalDSNName[SQL_MAX_DSN_LENGTH + 1];
CHAR szFileDSNName[1024];
char *pvaleur;
UWORD rgbValue;
HCURSOR hOldCursor;
y= SearchDialAndWin( hWndTBtnQuery4 ,&NumDial,&NumWin,2);
if(!y) { TRACEEU("Error Creation Window Query Load Table xls/mdb "); return 0;}
for(i=0;i < 40;i++)//init GRID
{
*(int *)Dialog[NumDial].TabCoordWinGrid[NumWin].InfoTable[i][1] =0;
AllocAddItemGridSql(2,i,NumDial,NumWin, "\0");
AllocAddItemGridSql(3,i,NumDial,NumWin, "mdb");
}
nRows=0;
try {
// non exclusif, lecture seule
m_database.Open(pstrFileName, FALSE, TRUE);
}
catch (CDaoException* e) {
::DaoErrorMsg(e);
e->Delete(); AfxDaoTerm(); // hyper important
return 0;
}
m_strDatabase = m_database.GetName();
// TRACEEU("Nom BDD = %s, \n",
// (const char*) m_strDatabase );
int nTables = m_database.GetTableDefCount();
for (int n = 0; n < nTables; n++) {
m_database.GetTableDefInfo(n, tdi);
if (tdi.m_strName.Left(4) != "MSys") {
szBuffer = strdup( tdi.m_strName);
AllocAddItemGridSql(2,nRows,NumDial,NumWin,(char *) szBuffer);
AllocAddItemGridSql(3,nRows,NumDial,NumWin, (char *)"MDB");
nRows++;
// TRACEEU("TABLE GRID4...%s ",(const char*) szBuffer);
}
}
m_database.Close(); // route ECHAP
AfxDaoTerm(); // hyper important
// Attributes & Operations
InvalidateRect(hWndTBtnQuery4,0,0);
ShowWindow(HwndTQueryChild4,SW_RESTORE);
SetForegroundWindow((HWND)HwndTQueryChild4);
hOldCursor = SetCursor(LoadCursor((HINSTANCE)NULL, IDC_WAIT));
SetCursor((LoadCursor((HINSTANCE)NULL, IDC_WAIT)));
ShowCursor(TRUE);
strcpy(SQLInfoDriver[5].EnvDatabaseSql,pstrFileName);
strcpy(SQLInfoDriver[5].VerSql,"MS Access Driver");
strcpy(SQLInfoDriver[5].VerDriver ,"25");
Format_Drivers_MDB();
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
(void) SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER);
retcode = SQLDrivers(henv,SQL_FETCH_FIRST,(unsigned char *) szDSNNameDrivers,
sizeof(szDSNNameDrivers), NULL, NULL, 0, NULL);
// recherche du driver
while(SQL_SUCCEEDED(retcode))
{
if(pvaleur = strstr( szDSNNameDrivers,"Access Driver"))
{
G_IncrDataSource++;
memcpy(szFileDSNName,"\0",MAXBUFLEN+1);
sprintf(szLegalDSNName,"%05d",G_IncrDataSource);
strcpy(szDSNName,"EndUser4gl");strcat(szDSNName,szLegalDSNName);
strcat(szFileDSNName,"DSN=");
strcat(szFileDSNName,szDSNName);strcat(szFileDSNName,";");
strcat(szFileDSNName,G_szFileDSNName);
y= strlen(szFileDSNName);
for(i = 0;i < y + 1 ;i++) {
if(!strncmp((char *) szFileDSNName+i,(char *)";",1))
szFileDSNName[i] ='\0' ;
}
strcat(szDSNName,"\0");
SQLRemoveDSNFromIni(szDSNName);
SQLConfigDataSource(NULL,ODBC_REMOVE_DSN, szDSNNameDrivers,szFileDSNName);
if(! SQLWriteDSNToIni(szDSNName, szDSNNameDrivers))
{ TRACEEU("1 Erreur SQLWriteDSNToIni %s", szDSNNameDrivers);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
return 0;
}
if(!SQLConfigDataSource(NULL,ODBC_ADD_DSN, szDSNNameDrivers,szFileDSNName))
{ TRACEEU("4 Erreur SQLConfigDataSource %s", szDSNNameDrivers);
TRACEEU("4 Erreur SQLConfigDataSource %s", szFileDSNName);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
SQLRemoveDSNFromIni(szDSNName);
return 0;
}
if ((nResult = SQLAllocHandle(SQL_HANDLE_DBC,henv, (SQLHDBC FAR *)&hdbc)) != SQL_SUCCESS) {
DisplayError(nResult, 0, SQL_HANDLE_ENV, henv);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
SQLRemoveDSNFromIni(szDSNName);
SQLConfigDataSource(NULL,ODBC_REMOVE_DSN, szDSNNameDrivers,szFileDSNName);
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
return 0;
}
//test SQL_AUTOCOMMIT
nResult = SQLSetConnectAttr(hdbc,SQL_AUTOCOMMIT,(void *)SQL_AUTOCOMMIT_ON,SQL_IS_INTEGER);
if((nResult != SQL_SUCCESS ) )
{TRACEEU("PB03%d",nResult);
DisplayError(nResult, 0, SQL_HANDLE_STMT, hdbc);
return 0;
}
nResult = SQLSetConnectAttr(hdbc,SQL_ACCESS_MODE,(void *)SQL_MODE_READ_ONLY,SQL_IS_INTEGER);
if((nResult != SQL_SUCCESS ) )
{TRACEEU("PB02%d",nResult);
DisplayError(nResult, 0, SQL_HANDLE_STMT, hdbc);
return 0;
}
//test SQL_LOGIN_TIMEOUT
//the choice of 15 seconds for timeout is arbitrary
rgbValue = 15;
nResult = SQLSetConnectAttr(hdbc,SQL_LOGIN_TIMEOUT,(void *)&rgbValue ,SQL_IS_INTEGER);
if((nResult != SQL_SUCCESS ) )
{TRACEEU("PB04%d",nResult);
DisplayError(nResult, 0, SQL_HANDLE_STMT, hdbc);
return 0;
}
// CONNECTION ODBC/MDB BY TRANSPARENCY ******************************
nResult = SQLConnect(hdbc,(unsigned char *)szDSNName,SQL_NTS,(unsigned char *)"",\
SQL_NTS,(unsigned char *)"",SQL_NTS);
if(nResult == SQL_SUCCESS || nResult == SQL_SUCCESS_WITH_INFO)
{ // try to get a list of columns for the table
nResult = SQLAllocHandle(SQL_HANDLE_STMT,hdbc, (SQLHSTMT FAR *)&hstmt);
Result=0;
if(nResult == SQL_SUCCESS || nResult == SQL_SUCCESS_WITH_INFO)
{
G_hdbc = hdbc; // tres important
G_hstmt = hstmt; // tres important
strcpy( G_szDSNName,szDSNName);
strcpy( G_szDSNNameDrivers,szDSNNameDrivers) ;
strcpy( G_szFileDSNNameODBC,szFileDSNName) ;
G_henv = henv;
ExecuteSQLDialog( hdbc, hstmt,pstrFileName) ;
return hdbc;
}//if(nResult == SQL_SUCCESS
else
{
DisplayError(nResult, 0, SQL_HANDLE_STMT, hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
SQLRemoveDSNFromIni(szDSNName);
SQLConfigDataSource(NULL,ODBC_REMOVE_DSN, szDSNNameDrivers,szFileDSNName);
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
MessageBox(0, INITERROR, EXECERROR, MB_OK|MB_ICONHAND);
return 0;
}
}// nResult = SQLConnect
else
{
DisplayError(nResult, 0, SQL_HANDLE_STMT, hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
SQLRemoveDSNFromIni(szDSNName);
SQLConfigDataSource(NULL,ODBC_REMOVE_DSN, szDSNNameDrivers,szFileDSNName);
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
MessageBox(0, INITERROR, EXECERROR, MB_OK|MB_ICONHAND);
}
break;
} //if Excel driver
retcode = SQLDrivers(henv,SQL_FETCH_NEXT,(unsigned char *) szDSNNameDrivers,
sizeof(szDSNNameDrivers), NULL, NULL, 0, NULL);
}// while
return 0 ;
}
int FAR PASCAL ExecuteSQLDialog(SQLHDBC hdbc, SQLHSTMT hstmt,char *szBuffer)
{
SWORD nCols = 0; // # of result columns
SWORD nCount; // index
SWORD swColType; // column data type
SWORD swColScale; // column scale
SWORD swColNull; // nullable column ?
SWORD swColLength = MAXDATALEN; // column data length
UDWORD udwColDef; // precision on the column
SQLRETURN nReturn; // return code
HCURSOR hOldCursor; // default cursor handle
int nRows,i=0,y,NumDial,NumWin; // # of result rows
MSG msg;
RECT rc;
// strcpy(szBuffer," SELECT * FROM clients WHERE clients.CODE_CLIEN like '*A*' ");
// change cursor shape to hour glass
//TRACEEU("%d Wait..........Please %s",strlen(szBuffer),szBuffer);
hOldCursor = SetCursor(LoadCursor((HINSTANCE)NULL, IDC_WAIT));
SetCursor((LoadCursor((HINSTANCE)NULL, IDC_WAIT)));
ShowCursor(TRUE);
nReturn =SQLPrepare(hstmt,(unsigned char *)szBuffer,strlen(szBuffer));
GlobalDoubleValResult = (double)nReturn;
if( nReturn != SQL_SUCCESS )
{
DisplayError(nReturn, hWndFrameSQL, SQL_HANDLE_STMT, hstmt);
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))// hyper important :nettoyer
DispatchMessage (&msg) ;
SQLCloseCursor(hstmt);
SQLFreeStmt(hstmt, SQL_CLOSE);
GlobalDoubleValResult = -1;// no select
return EUError;
}
nReturn = SQLExecDirect(hstmt, (unsigned char *)szBuffer,strlen(szBuffer) );
GlobalDoubleValResult = (double)nReturn;
if(nReturn != SQL_SUCCESS )
{
DisplayError(nReturn, hWndFrameSQL, SQL_HANDLE_STMT, hstmt);
// if the return value was just information, carry on
// Close the open result set.
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))// hyper important :nettoyer
DispatchMessage (&msg) ;
SetCursor(hOldCursor);
DestroyWindow(hWndFrameSQL) ;
SQLCloseCursor(hstmt);
SQLFreeStmt(hstmt, SQL_CLOSE);
// restore the cursor to default value
SetCursor(hOldCursor);
GlobalDoubleValResult = 1;
return EUError; ;
}
// call SQLNumResultCols to calculate the number of columns in
// the result set, if more than the MAX_COL (the array boundary)
// limit the number to MAX_COL and display truncation warning.
// if it is 0, the statement probably was a non-SELECT simply return
nReturn = SQLNumResultCols(hstmt, &nCols);
//printfEU(" nCols %d",nCols);STOP();STOP();STOP();
if ((nCols == 0) )
{ // if "INTO" in szBuffer
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))// hyper important :nettoyer
DispatchMessage (&msg) ;
SQLCloseCursor(hstmt);
SetCursor(hOldCursor);
// DestroyWindow(hWndFrameSQL) ;
// restore the cursor to default value
SetCursor(hOldCursor);
if((strstr(szBuffer,"into")) || (strstr(szBuffer,"INTO")))
{ //printfEU("Find INTO............");
return GlobalDoubleValResult=0;//normal
}
else return GlobalDoubleValResult = 1;// not find
}
// TRACEEU("1 Cols.......... :%d",nCols);
y= SearchDialAndWin( hWndBtnSQL ,&NumDial,&NumWin,2);
if(!y) { TRACEEU("Error Creation Window Sql "); return EUError;}
AllocGridSql(2,nCols,64,26,NumDial,NumWin);
GetWindowRect(hWndFrameSQL,&rc);
y =((nCols + 1) * 160) +70 ;
if( y < rc.right){
SetWindowPos(hWndFrameSQL, NULL,\
rc.left, rc.top, y, rc.bottom,
SWP_NOZORDER | SWP_DRAWFRAME );
// Ascenseur horizontal : en bas, nouvelles dimensions
GetClientRect(hWndFrameSQL,&rc);
MoveWindow(Dialog[NumDial].TabCoordWinGrid[NumWin].hwndTableur[1],0,rc.bottom-96,\
rc.right-86,16,0);
// Ascenseur vertical : à droite, nouvelles dimensions
MoveWindow(Dialog[NumDial].TabCoordWinGrid[NumWin].hwndTableur[2],rc.right-86,\
Dialog[NumDial].TabCoordWinGrid[NumWin].HeightGrid,16,rc.bottom-96-Dialog[NumDial].TabCoordWinGrid[NumWin].HeightGrid,0);
}
// call SQLBindCol to bind column data array and column data length
// array to the result set
SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_BIND_TYPE, SQL_BIND_BY_COLUMN, 0);
for(nCount=1, szBuffer[0]='\0'; nCount <= nCols; nCount++) {
SQLDescribeCol(hstmt, nCount,(unsigned char *) szBuffer, MAXDATALEN, &swColLength, &swColType, &udwColDef,
&swColScale, &swColNull);
SQLBindCol(hstmt, (UWORD)(nCount), SQL_C_CHAR,(void *)szBuffer, MAXDATALEN, &dwDataLen);
AllocColumnGridSqlPUSHBUTTON(NumDial,NumWin);
AllocColumnGridSql(nCount,NumDial,NumWin,szBuffer);
}
// call SQLFetch to fetch each row of the result set. Extract data
// value and store it in the display buffer, separated by tabs.
// continue until SQLFetch fails.
// CenterWindow(hWndFrameSQL);
y=0;
// SQLFECTHSCROLL ***************************************************************
for(nRows = 0; (nReturn = SQLFetchScroll(hstmt,SQL_FETCH_NEXT,0))==SQL_SUCCESS \
|| nReturn==SQL_SUCCESS_WITH_INFO;) {
if(nRows >1 ){
Dialog[NumDial].TabCoordWinGrid[NumWin].LigneAColler =nRows ;
Dialog[NumDial].TabCoordWinGrid[NumWin].LigneClicDroit =nRows ;
CopierLigne(NumDial, NumWin);
CollerLigne(NumDial, NumWin);
}
else
AllocMallocItemGridSql(nRows,NumDial,NumWin);
for(nCount=1; szBuffer[0]='\0', nCount<= nCols+1 ; nCount++) {
SQLGetData(hstmt, (UWORD)(nCount), SQL_C_CHAR,(unsigned char *) szBuffer,\
MAXDATALEN, &dwDataLen );
AllocAddItemGridSql(nCount,nRows,NumDial,NumWin, (char *)szBuffer);
y++;
}
if(nRows<10) //visual express
{ Dialog[NumDial].TabCoordWinGrid[NumWin].NbLines =nRows +1;
Dialog[NumDial].TabCoordWinGrid[NumWin].StructureNbLignes=nRows +1;//tres important
CenterWindow(hWndFrameSQL);
ShowWindow(hWndFrameSQL, SW_SHOWNORMAL) ;
UpdateWindow(hWndFrameSQL);
}
nRows++;
}
Dialog[NumDial].TabCoordWinGrid[NumWin].NbLines =nRows ;
Dialog[NumDial].TabCoordWinGrid[NumWin].StructureNbLignes=nRows ;//tres important
CenterWindow(hWndFrameSQL);
ShowWindow(hWndFrameSQL, SW_SHOWNORMAL) ;
i= UpdateWindow(hWndFrameSQL);
SQLCloseCursor(hstmt); //important laisser en commentaire voir Query.cpp
SQLFreeStmt(hstmt, SQL_UNBIND);
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
DispatchMessage (&msg) ;
// restore the cursor to default value
SetCursor(hOldCursor);
GlobalDoubleValResult = 0;
return msg.wParam;
} |