//Programma Realizzato dalla SOFTECH di Germinara Francesco (c) 2004 //www.germinara.it - info@germinara.it //Data Realizzazione: 24 maggio 2004 ///////////////////////////////////////////////////////////////////// // // FGODBCConnection.m // Commesse // #import "FGODBCConnection.h" @implementation FGODBCConnection -(id)init{ [super init]; return(self); } -(void) dealloc{ [strOdbcVersion release]; [super dealloc]; } //Apre la connessione con il data base - (int) Open:(NSString *)strOpenConnections { short buflen; char buf[257]; SQLCHAR dataSource[255]; SQLCHAR driverInfo[255]; SWORD len1; int status; connected=0; strOdbcVersion=[NSString alloc]; strcpy(dataSource,[strOpenConnections cString]); //"DSN=DBTEST;UID=SA;PWD=;" #if (ODBCVER < 0x0300) if (SQLAllocEnv (&henv) != SQL_SUCCESS) return -1; if (SQLAllocConnect (henv, &hdbc) != SQL_SUCCESS) return -1; #else if (SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv) != SQL_SUCCESS) return -1; SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_UINTEGER); if (SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc) != SQL_SUCCESS) return -1; #endif //Carico la versione del Driver ODBC e la memorizzo nella classe status = SQLGetInfo (hdbc, SQL_DM_VER, driverInfo, sizeof (driverInfo), &len1); if (status == SQL_SUCCESS){ NSLog(@"Driver Manager: %s", driverInfo); strOdbcVersion=[strOdbcVersion initWithCString:driverInfo]; } status = SQLDriverConnect (hdbc, 0, (UCHAR *) dataSource, SQL_NTS,(UCHAR *) buf, sizeof (buf), &buflen, SQL_DRIVER_COMPLETE); if (status != SQL_SUCCESS && status != SQL_SUCCESS_WITH_INFO) return -1; //Opzione per tracciare le chiamate SQL SQLSetConnectOption (hdbc, SQL_OPT_TRACEFILE, (UDWORD) "\\FGSQL.LOG"); connected = 1; //Carico la versione del Driver ODBC e la memorizzo nella classe status = SQLGetInfo (hdbc, SQL_DRIVER_VER, driverInfo, sizeof (driverInfo), &len1); if (status == SQL_SUCCESS) printf ("Driver: %s\n", driverInfo); return 0; } -(int)OpenCursor:(SQLHSTMT *)hstmt{ //Alloco Handle di CONNESSIONE #if (ODBCVER < 0x0300) if (SQLAllocStmt (hdbc, hstmt) != SQL_SUCCESS) return -1; #else if (SQLAllocHandle (SQL_HANDLE_STMT, hdbc, hstmt) != SQL_SUCCESS) return -1; #endif return 0; } //Visualizza il messaggio di errore - (int) ShowMessage:(NSString *) strDescription HandleStatMent:(SQLHSTMT *)hstmt { NSMutableArray *saStrError; //Array degli errori NSString *strError; //Messaggio di errore BOOL bError=FALSE; saStrError=[[NSMutableArray alloc]init]; unsigned char buf[250]; unsigned char sqlstate[15]; /* * Get statement errors */ while (SQLError (henv, hdbc, *hstmt, sqlstate, NULL, buf, sizeof(buf), NULL) == SQL_SUCCESS) { strError=[NSString stringWithFormat:@"%s, SQLSTATE=%s\n",buf,sqlstate]; [saStrError addObject:strError]; [strError release]; bError=TRUE; } /* * Get connection errors */ while (SQLError (henv, hdbc, SQL_NULL_HSTMT, sqlstate, NULL, buf, sizeof(buf), NULL) == SQL_SUCCESS) { strError=[NSString stringWithFormat:@"%s, SQLSTATE=%s\n",buf,sqlstate]; [saStrError addObject:strError]; [strError release]; bError=TRUE; } /* * Get environmental errors */ while (SQLError (henv, SQL_NULL_HDBC, SQL_NULL_HSTMT, sqlstate, NULL, buf, sizeof(buf), NULL) == SQL_SUCCESS) { strError=[NSString stringWithFormat:@"%s, SQLSTATE=%s\n",buf,sqlstate]; [saStrError addObject:strError]; [strError release]; bError=TRUE; } if(bError){//Solo se ho realmente un errore visualizzo messaggio //Costruisco messaggio da visualizzare NSMutableString *strTxtError; strTxtError=[[NSMutableString alloc]init]; [strTxtError appendString:strDescription]; int nIndex=0,nCount=0; nCount=[saStrError count]; for(nIndex=0; nIndex < nCount; nIndex++){ [strTxtError appendString: [saStrError objectAtIndex:nIndex]]; } NSRunAlertPanel(@"Errore SQL",strTxtError,@"OK",nil,nil); } return -1; } //Chiude la connessione ODBC - (int) Close { #if (ODBCVER < 0x0300) // if (*hstmt) // SQLFreeStmt (*hstmt, SQL_DROP); if (connected) SQLDisconnect (hdbc); if (hdbc) SQLFreeConnect (hdbc); if (henv) SQLFreeEnv (henv); #else // if (*hstmt) // { // int sts; // sts = SQLCloseCursor (*hstmt); // if (sts != SQL_ERROR) // [self ShowMessage:@"Chiusura Connessione"]; // SQLFreeHandle (SQL_HANDLE_STMT, *hstmt); // } if (connected) SQLDisconnect (hdbc); if (hdbc) SQLFreeHandle (SQL_HANDLE_DBC, hdbc); if (henv) SQLFreeHandle (SQL_HANDLE_ENV, henv); #endif connected=0; return 0; } //Retun Connection Status - (int) IsConnected{ return connected; } //GET info on ODBC Version -(NSString *) GetODBCVersion{ return strOdbcVersion; } //Esegue comando SQL - (int) ExecuteSQL:(NSString *) strQuery HandleStatMent:(SQLHSTMT *)hstmt{ if (SQLPrepare (*hstmt, (UCHAR *) [strQuery cString], SQL_NTS) != SQL_SUCCESS){ [self ShowMessage:@"ExecuteSQL: SQLPrepare" HandleStatMent:hstmt]; // return -1; } if (SQLExecute (*hstmt) != SQL_SUCCESS){ [self ShowMessage:@"ExecuteSQL: SQLExecute" HandleStatMent:hstmt]; return -1; } return 0; } //Chiudo il cursore - (int) CloseCursor:(SQLHSTMT *)hstmt{ int sts=0; if (*hstmt) { sts = SQLCloseCursor (*hstmt); SQLFreeHandle (SQL_HANDLE_STMT, *hstmt); (*hstmt)=0x00; } return sts; } //Stop Elaborate Query - (int) StopQuery:(SQLHSTMT *)hstmt{ int sts=0; if (*hstmt){ while (SQLMoreResults (*hstmt) == SQL_SUCCESS); } return sts; } //Leggo Nomi delle Colonne - (int) FillColName:(NSMutableDictionary *) fieldsDictionary HandleStatMent:(SQLHSTMT *)hstmt{ SQLCHAR colName[100]; //Nome della colonna short colNum; short colType; UDWORD colPrecision; short colScale; short colNullable; short numCols; numCols = 0; if( connected){ if(SQLNumResultCols (*hstmt, &numCols) != SQL_SUCCESS){ [self ShowMessage:@"FillColName: SQLNumResultCols" HandleStatMent:hstmt]; }else{ //Ricavo informazioni sul campo colNum=0; for(colNum = 1; colNum <= numCols; colNum++){ if (SQLDescribeCol (*hstmt, colNum, (UCHAR *) colName,sizeof (colName), NULL, &colType, &colPrecision,&colScale, &colNullable) != SQL_SUCCESS){ [self ShowMessage:@"FillColName: SQLNumResultCols" HandleStatMent:hstmt]; break; }else{ NSString *strTxtNomeCampo; strTxtNomeCampo=[[NSString alloc]initWithCString:colName]; FGODBCFieldInfo *myInfoField; myInfoField=[[FGODBCFieldInfo alloc]init]; [myInfoField SetValues:strTxtNomeCampo forType:colType forPrecision:colPrecision forScale:colScale forNullability:colNullable forCol:colNum-1]; [fieldsDictionary setObject:myInfoField forKey:(id)strTxtNomeCampo]; [strTxtNomeCampo release]; [myInfoField release]; } } } } return numCols; } //Return TRUE if the Select statment found at least one record -(BOOL) FindFirstRecord:(SQLHSTMT *)hstmt{ BOOL bFound=FALSE; int sts=0; sts=SQLFetch (*hstmt); if(sts == SQL_NO_DATA_FOUND) bFound=FALSE; if(sts != SQL_SUCCESS) [self ShowMessage:@"FillColName: SQLNumResultCols" HandleStatMent:hstmt]; else bFound=TRUE; return bFound; } //Return TRUE if the Select statment found at next record -(BOOL) MoveNextRecord:(SQLHSTMT *)hstmt{ return [self FindFirstRecord:hstmt]; } //Restituisco il contenuto del campo indicato -(NSString *) GetFieldData:(short) nCol HandleStatMent:(SQLHSTMT *)hstmt{ SQLINTEGER colIndicator=0; char fetchBuffer[4096]; if(fieldValue!=nil){ [fieldValue release]; } fieldValue=[NSString alloc]; nCol=nCol+1; //0 based if(SQLGetData (*hstmt, nCol, SQL_CHAR, fetchBuffer,sizeof (fetchBuffer), &colIndicator) != SQL_SUCCESS) [self ShowMessage:@"GetFieldData: GetFieldData" HandleStatMent:hstmt]; if(colIndicator != SQL_NULL_DATA) fieldValue=[fieldValue initWithCString:fetchBuffer]; if(colIndicator == SQL_NULL_DATA) fieldValue=[fieldValue init]; return fieldValue; } @end