//////////////////////////////////////////////////////////////////// // // FGODBCRecord.m // Commesse // // Created by Francesco Germinara on Mon Jun 07 2004. // // Copyright (c) 2004 SofTech di F.Germinara - www.germinara.it. All rights reserved. // // For information please contact me at info@germinara.it //////////////////////////////////////////////////////////////////// // History: // // Description: // //////////////////////////////////////////////////////////////////// #import "FGODBCRecord.h" #import "FGODBCFieldInfo.h" @implementation FGODBCRecord -(id)init{ self=[super init]; FGODBCFieldsInfo=[[NSMutableDictionary alloc]init]; return self; } //Create Object: Default Constructor -(id)initWithDataBase:(FGODBCConnection *) dbName forSQLTable:(NSString*) strName{ self=[super init]; nFields=0; if(dbName==nil){ NSException* aException = [NSException exceptionWithName:@"FGODBCRecord" reason:@"FGODBCRecord->:DataBase non valido" userInfo:(id)self]; [aException raise]; } database=dbName; if(strName==nil){ NSException* aException = [NSException exceptionWithName:@"FGODBCRecord" reason:@"FGODBCRecord->:Tabella non valida" userInfo:(id)self]; [aException raise]; } strTableName=[[NSString alloc]initWithString:strName]; FGODBCFieldsInfo=[[NSMutableDictionary alloc]init]; //Alloco Handle di Istruzioni int sts=[database OpenCursor:&hstmt]; if(sts!=0 || hstmt==0x00){ NSException* aException = [NSException exceptionWithName:@"FGODBCRecord" reason:@"FGODBCRecord->:hstmt non valido!" userInfo:(id)self]; [aException raise]; } nFields=0; return self; } //Return Table Name -(NSString *)TableName{ return strTableName; } //Create Object copying data from another object -(id)initWithRecordData:(FGODBCRecord *) record{ self=[super init]; database=record->database; strTableName=[[NSString alloc]initWithString:[record TableName]]; nFields=record->nFields; hstmt=record->hstmt; bIsEOF=record->bIsEOF; bIsBOF=record->bIsBOF; FGODBCFieldsInfo=[[NSMutableDictionary alloc]init]; FGODBCFieldInfo *aFieldInfo; NSString *strKey; NSArray *arr=[record->FGODBCFieldsInfo allKeys]; int nIndex=0; for(nIndex=0; nIndex<[arr count];nIndex++){ strKey=[arr objectAtIndex:nIndex]; aFieldInfo=[record FieldInfo:strKey]; FGODBCFieldInfo *tmpFieldInfo=[[FGODBCFieldInfo alloc]init]; [tmpFieldInfo SetValues:[[NSString alloc]initWithString:[aFieldInfo name]] forType:[aFieldInfo Type] forPrecision:[aFieldInfo Precision] forScale:[aFieldInfo Scale] forNullability:[aFieldInfo Nullability] forCol:[aFieldInfo ColNum]]; [tmpFieldInfo SetValue:[[NSString alloc]initWithString:[aFieldInfo GetValue]]]; [FGODBCFieldsInfo setObject:tmpFieldInfo forKey:strKey]; [tmpFieldInfo release]; } /* NSEnumerator *aEnum=[record->FGODBCFieldsInfo objectEnumerator]; FGODBCFieldInfo *tmpFieldInfo; while((tmpFieldInfo = [aEnum nextObject])!=nil){ } */ return(self); } //Close database handle and free resource, call before release -(void)CloseCursor{ [database CloseCursor:&hstmt]; //Close Communications Handle } //Standard destructor -(void)dealloc{ [FGODBCFieldsInfo removeAllObjects];//Libera anche gli oggetti allocati [FGODBCFieldsInfo release]; [strTableName release]; [super dealloc]; } //Load all fields Informations and set the nFields value -(void)FillFieldInfo{ if(nFields!=0) return; //Already Done if(database==nil){ NSException* aException = [NSException exceptionWithName:@"FGODBCRecord" reason:@"FGODBCRecord->:FillFieldInfo:DataBase non valido" userInfo:(id)self]; [aException raise]; } [database FillColName:FGODBCFieldsInfo HandleStatMent:&hstmt]; nFields= [FGODBCFieldsInfo count]; } //Return number of fields -(int)GetNumFields{ return nFields; } //return a Field Info Object -(FGODBCFieldInfo *)FieldInfo:(NSString*)strName{ FGODBCFieldInfo *aObjField=[FGODBCFieldsInfo objectForKey:strName]; return(aObjField); } //return a Field Info Object -(FGODBCFieldInfo *)FieldInfoByCol:(int)Col{ FGODBCFieldInfo *aFieldInfo; NSString *strKey; NSArray *arr=[FGODBCFieldsInfo allKeys]; int nIndex=0; for(nIndex=0; nIndex<[arr count];nIndex++){ strKey=[arr objectAtIndex:nIndex]; aFieldInfo=[self FieldInfo:strKey]; if([aFieldInfo ColNum]==Col) return(aFieldInfo); } return(nil); } //Return Field Name of col (0 based) -(NSString *)FieldName:(int)col{ FGODBCFieldInfo *aFieldInfo; NSString *strKey; NSArray *arr=[FGODBCFieldsInfo allKeys]; int nIndex=0; for(nIndex=0; nIndex<[arr count];nIndex++){ strKey=[arr objectAtIndex:nIndex]; aFieldInfo=[self FieldInfo:strKey]; if([aFieldInfo ColNum]==col) return([aFieldInfo name]); } return(nil); } //Return Field Length of col (0 based) -(int)FieldLegth:(int)col{ FGODBCFieldInfo *aFieldInfo; NSString *strKey; NSArray *arr=[FGODBCFieldsInfo allKeys]; int nIndex=0; for(nIndex=0; nIndex<[arr count];nIndex++){ strKey=[arr objectAtIndex:nIndex]; aFieldInfo=[self FieldInfo:strKey]; if([aFieldInfo ColNum]==col) return([aFieldInfo length]); } return(0); } //Count record Items -(long int)Count:(NSString *) strFilter{ long int nRecords=0; //Number of Records int sts=0; NSMutableString *strQuery; strQuery=[[NSMutableString alloc] init]; [strQuery appendString:@"select count(*) from "]; [strQuery appendString:strTableName]; [strQuery appendString:@" "]; if([strFilter length]!=0) [strQuery appendString:strFilter]; sts=[database ExecuteSQL:strQuery HandleStatMent:&hstmt]; if(sts==0){//Ok, sts=[database FindFirstRecord:&hstmt]; if(sts==TRUE){ nRecords=[[database GetFieldData:0 HandleStatMent:&hstmt] intValue]; } } [database StopQuery:&hstmt]; [strQuery release]; return nRecords; } //Load First Row of Data -(int)LoadDati:(NSString *) strFilter{ int sts=0; int nIndex=0; NSMutableString *strQuery; strQuery=[[NSMutableString alloc] init]; [strQuery appendString:@"select * from "]; [strQuery appendString:strTableName]; [strQuery appendString:@" "]; if([strFilter length]!=0) [strQuery appendString:strFilter]; sts=[database ExecuteSQL:strQuery HandleStatMent:&hstmt]; if(sts==0){//Ok, //Carico i dati nei vari campi [self FillFieldInfo]; sts=[database FindFirstRecord:&hstmt]; if(sts==TRUE){ //Carico i dati nei vari campi //[self FillFieldInfo]; for(nIndex=0; nIndex