1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465 |
- unit fpodbc;
- {$mode objfpc}
- {$h+}
- interface
- uses odbcsql,SysUtils,Classes;
- Type
- TDSNTypes = (dtUser,dtSystem,dtBoth);
- TODBCParamType = (ptUnknown,ptInput,ptInputOutput,ptResult,ptOutput,ptRetVal);
- TODBCParamTypes = Set of TODBCParamType;
- TODBCObject = Class(TComponent)
- Private
- FHandle : SQLHandle;
- FHandleType : SQLSmallint;
- Function GetHandle : SQLHandle;
- function GetHandleAllocated: Boolean;
- function GetExtendedErrorInfo: String;
- Protected
- Function CreateHandle : SQLHandle; Virtual;
- Function ParentHandle : SQLHandle; Virtual;
- Procedure FreeHandle;
- Function CheckODBC(Res : Integer;Msg : String) : Integer;
- Public
- Destructor Destroy; override;
- Property Handle : SQLHandle Read GetHandle;
- Property HandleAllocated : Boolean Read GetHandleAllocated;
- end;
- TODBCEnvironment = Class(TODBCObject)
- Private
- FODBCBehaviour : Integer;
- procedure SetODBCbehaviour(const Value: Integer);
- function GetNullTerminate: Boolean;
- procedure SetNullTerminate(const Value: Boolean);
- protected
- function CreateHandle: SQLHandle; override;
- Procedure SetIntAttribute(Const Attr,Value : Integer);
- Procedure SetStringAttribute(Const Attr: Integer; Value : String);
- Function GetIntAttribute(Const Attr : Integer) : Integer;
- Function GetStringAttribute(Const Attr : Integer) : String;
- Public
- Constructor Create(Aowner : TComponent);override;
- Function GetDriverNames(List : Tstrings) : Integer;
- Function GetDataSourceNames(List : Tstrings; Types : TDSNTypes;Descriptions : Boolean) : Integer;
- function GetDriverOptions(Driver: String; Options: TStrings): Integer;
- Property ODBCBehaviour : Integer Read FODBCBehaviour Write SetODBCbehaviour;
- Property NullTerminateStrings : Boolean Read GetNullTerminate Write SetNullTerminate;
- end;
- TConnectionBrowseEvent = Procedure (Sender : TObject;InParams,OutParams : Tstrings) of Object;
- TODBCConnection = Class(TODBCObject)
- Private
- FActive : Boolean;
- FDriverParams : TStrings;
- FDSN,
- FDriverName,
- FUserName,
- FPassword : String;
- FEnvironMent : TODBCEnvironment;
- FOnBrowseConnection : TConnectionBrowseEvent;
- FWindowHandle : integer;
- FDriverCOmpletion: SQLUSmallInt;
- function GetDriverName: String;
- function GetDriverParams: TStrings;
- procedure SetActive(const Value: Boolean);
- procedure SetDriverName(const Value: String);
- procedure SetDriverParams(const Value: TStrings);
- procedure SetDSN(const Value: String);
- function GetEnvironment: TODBCEnvironMent;
- procedure SetEnvironment(const Value: TODBCEnvironMent);
- Protected
- procedure ConnectToDriver;
- procedure ConnectToDSN;
- Procedure ConnectBrowsing;
- Function ParentHandle : SQLHandle; override;
- Procedure CheckActive;
- Procedure CheckInActive;
- Public
- Constructor Create(Aowner : TComponent);override;
- Destructor Destroy; override;
- Procedure Connect;
- Procedure Disconnect;
- Procedure GetTableNames(S : TStrings; SystemTables : Boolean);
- Procedure GetFieldNames(TableName : String; S : TStrings);
- Procedure GetPrimaryKeyFields(TableName : String; S : TStrings);
- procedure GetProcedureNames(S : TStrings);
- procedure GetProcedureParams(ProcName : String;ParamTypes : TODBCParamTypes; S : TStrings);
- Property DSN : String Read FDSN Write SetDSN;
- Property DriverName : String Read GetDriverName Write SetDriverName;
- Property DriverCompletion : SQLUSmallInt Read FDriverCOmpletion Write FDriverCompletion;
- Property DriverParams : TStrings Read GetDriverParams Write SetDriverParams;
- Property Active : Boolean Read FActive Write SetActive;
- Property Environment : TODBCEnvironMent Read GetEnvironment Write SetEnvironment;
- Property UserName : String Read FUserName Write FUserName;
- Property Password : string Read FPassword Write FPassword;
- Property OnBrowseConnection : TConnectionBrowseEvent Read FonBrowseConnection Write FOnBrowseConnection;
- Property WindowHandle : integer Read FWindowHandle Write FWindowHandle;
- end;
- TODBCStatement = Class;
- TODBCFieldList = Class(TCollection)
- Private
- FStatement : TODBCStatement;
- Public
- Constructor Create(Statement : TODBCStatement);
- end;
- {
- TODBCStatement allocates 1 big data buffer. For each bound field
- two things are allocated in the buffer:
- - Size of fetched data as filled in by fetch.
- - data. (may be zero for blobs etc)
- The FBuffOffset contains the offset in the buffer of the size field.
- Data immediatly follows the size.
- }
- TODBCField = Class(TCollectionItem)
- Private
- FDecimalDigits,
- FPosition : SQLSmallInt;
- FName : String;
- FSize : SQLUInteger; // Declared size, as returned by DescribeCol
- FNullable : Boolean;
- FDataType : SQLSmallInt; // Declared type, as returned by DescribeCol
- FBuffOffSet : SQLInteger; // Offset in data buffer.
- FBuffer : Pointer; // Pointer to data.
- FBufSize : SQLInteger; // Allocated buffer size.
- FBufType : SQLSmallInt; // Allocated buffer type
- function GetAsString: String;
- function GetData : PChar;
- Function GetIsNull : Boolean;
- Function GetAsInteger : Integer;
- Function GetAsBoolean : Boolean;
- Function GetAsDouble : Double;
- Function GetAsDateTime : TDateTime;
- Public
- Property Position : SQLSmallint Read FPosition;
- Property Name : String read FName;
- Property DataType : SQLSmallInt read FDatatype;
- Property Size : SQLUinteger read FSize;
- property DecimalDigits : SQLSmallInt read FDecimalDigits;
- Property Nullable : Boolean Read FNullable;
- Property Data : Pchar Read GetData;
- Property BufType : SQLSmallInt Read FBufType;
- Property BufSize : SQLInteger Read FBufSize;
- Property IsNull : Boolean Read GetIsNull;
- Property AsString : String Read GetAsString;
- Property AsInteger : Integer Read GetAsInteger;
- Property AsBoolean : Boolean Read GetAsBoolean;
- Property AsDouble : Double Read GetAsDouble;
- Property AsDateTime : TDateTime Read GetAsDateTime;
- end;
- TODBCStatement = Class(TODBCObject)
- Private
- FBOF,FEOF : Boolean;
- FConnection: TODBCConnection;
- FFields : TODBCFieldList;
- FBuffer : Pointer;
- Protected
- Function ParentHandle : SQLHandle; override;
- procedure SetConnection(const Value: TODBCConnection);
- procedure AllocBuffers;
- Public
- Constructor Create(Aowner : TComponent);override;
- Destructor Destroy; override;
- Procedure BindFields(RestrictList : TStrings);virtual;
- Procedure ClearFields;virtual;
- Function Fetch : Boolean;
- Property Connection : TODBCConnection Read FConnection Write SetConnection;
- Property BOF : Boolean read FBOF;
- Property EOF : Boolean read FEOF;
- Property Fields : TODBCFieldList Read FFields;
- end;
- TODBCTableList = Class(TODBCStatement)
- Public
- Procedure GetTableNames(S : TStrings; SystemTables : Boolean);
- end;
- TODBCFieldNamesList = Class(TODBCStatement)
- Public
- Procedure GetFieldNames(TableName : String;S : TStrings);
- end;
- TODBCPrimaryKeyFieldsList = Class(TODBCStatement)
- Public
- Procedure GetPrimaryKeyFields(TableName : String;S : TStrings);
- end;
- TODBCProcedureList = Class(TODBCStatement)
- Public
- Procedure GetProcedureList(S : TStrings);
- end;
- TODBCProcedureParams = Class(TODBCStatement)
- Procedure GetProcedureParams(ProcName: String; ParamTypes: TODBCParamTypes; S: TStrings);
- end;
- TStatementState = (ssInactive,ssPrepared,ssBound,ssOpen);
- TODBCSQLStatement = Class(TODBCStatement)
- Private
- FSQL : TStrings;
- FState : TStatementState;
- function GetActive: Boolean;
- procedure SetActive(const Value: Boolean);
- Protected
- procedure FreeStatement(Option: SQLUSMALLINT);
- procedure ExecuteDirect;
- procedure ExecutePrepared;
- Procedure SetSQL(const Value: TStrings);
- Public
- Constructor Create(Aowner : TComponent);override;
- Destructor Destroy; override;
- procedure Prepare;
- procedure Unprepare;
- Procedure BindFields(RestrictList : TStrings);override;
- procedure ExecSQL;
- Procedure Open;
- Procedure Close;
- procedure GetFieldList(List: TStrings);
- Property Active : Boolean Read GetActive Write SetActive;
- Property SQL : TStrings Read FSQL Write SetSQL;
- end;
- EODBCError = Class(Exception);
- Const
- ODBCParamTypeNames : Array [TODBCParamType] of string
- = ('Unknown','Input','Input/Output','Result','Output','RetVal');
- Function DefaultEnvironment : TODBCEnvironment;
- implementation
- { TODBCObject }
- resourcestring
- SErrUnexpected = 'Unexpected ODBC error:';
- SErrEnvironmentHandle = 'Cannot allocate environment handle:';
- SErrInvalidBehaviour = 'Invalid value for ODBC behaviour: %d';
- SErrNotConnected = 'Operation invalid when not connected.';
- SErrConnected = 'Operation invalid when connected.';
- SNeedDSNOrDriver = 'Cannot connect with empty DSN and driver names.';
- SErrGettingDataSources = 'Error getting datasources:';
- SErrGettingDriverNames = 'Error getting driver names:';
- SErrGettingDriverOptions = 'Error getting driver options:';
- SErrSettingEnvAttribute = 'Error setting environment attribute:';
- SErrGettingEnvAttribute = 'Error Getting environment attribute:';
- SErrBrowseConnecting = 'Error connecting to datasource via browse:';
- SErrDSNConnect = 'Error connecting to DSN:';
- SErrDriverConnect = 'Error connecting to driver:';
- SErrDisconnecting = 'Error disconnecting:';
- SErrNoConnectionForStatement = 'Missing connection for statement.';
- SErrNoSQLStatement = 'Missing SQL statement.';
- SErrPreparing = 'Error preparing statement:';
- SErrGettingTableNames = 'Error getting table names:';
- SErrFetchingData = 'Error fetching data:';
- SErrFieldNames = 'Error getting field names:';
- SErrPrimaryKeys = 'Error getting primary key names:';
- SErrProcedureNames = 'Error getting procedure names:';
- SErrExecuting = 'Error while executing statement:';
- SErrExecutingPrepared = 'Error while executing prepared statement:';
- SErrNotPrepared = 'Statement is not prepared';
- SErrNotInactive = 'Statement is already prepared or executed.';
- SErrStatementActive = 'A statement is still active';
- SErrColumnCount = 'Error retrieving cilumn count:';
- SErrColDescription = 'Error retrieving column description';
- SErrInvalidConversion = 'invalid type conversion';
- SErrBindCol = 'Error binding column';
- Const
- ODBCSuccess = [SQL_SUCCESS,SQL_SUCCESS_WITH_INFO];
- Procedure ODBCError (Msg : String);
- begin
- Raise EODBCError.Create(Msg);
- end;
- Procedure ODBCErrorFmt (Fmt : String;Args : Array of const);
- begin
- Raise EODBCError.CreateFmt(Fmt,Args);
- end;
- Function CheckODBC(Res : Integer;Msg : String) : Integer;
- begin
- Result:=Res;
- if not Res in [SQL_SUCCESS,SQL_SUCCESS_WITH_INFO] then
- begin
- If MSG='' then
- MSG:=SErrUnexpected;
- ODBCErrorFmt(msg,[res]);
- end;
- end;
- function TODBCObject.CheckODBC(Res: Integer; Msg: String): Integer;
- Var S : String;
- begin
- Result:=Res;
- if not Res in [SQL_SUCCESS,SQL_SUCCESS_WITH_INFO] then
- begin
- If MSG='' then
- MSG:=SErrUnexpected;
- S:=GetExtendedErrorInfo;
- If S<>'' then
- Msg:=Msg+LineEnding+S;
- ODBCError(msg);
- end;
- end;
- function TODBCObject.GetExtendedErrorInfo : String;
- Var
- Res : SQLreturn;
- I,MsgLen : SQLSmallInt;
- SQLState : Array[0..6] of Char;
- NativeError : SQLInteger;
- MSg : Array[0..SQL_MAX_MESSAGE_LENGTH] of Char;
- SState,SMsg : String;
- begin
- I:=0;
- Result:='';
- Repeat
- Inc(i);
- Res:=SQLGetDiagRec(FhandleType, FHandle, i, SqlState, NativeError,
- Msg, sizeof(Msg), MsgLen);
- If Res<>SQL_NO_DATA then
- begin
- SState:=SQLState;
- SMsg:=Msg;
- If Length(Result)>0 then
- Result:=Result+LineEnding;
- Result:=Result+Format('[%s] : %s (%d)',[SState,SMsg,NativeError]);
- end;
- Until (Res=SQL_NO_DATA);
- end;
- function TODBCObject.CreateHandle: SQLHandle;
- begin
- {$ifdef debug}
- Writeln(Classname,': Creating handle of type ',FHAndleType,' and parent ',ParentHandle);
- {$endif}
- CheckODBC(SQLAllocHandle(FHandleType,ParentHandle,FHandle),SErrEnvironmentHandle);
- Result:=FHandle;
- end;
- destructor TODBCObject.Destroy;
- begin
- If FHandle<>0 then
- FreeHandle;
- inherited;
- end;
- procedure TODBCObject.FreeHandle;
- begin
- If FHandle<>0 then
- begin
- SQLFreeHandle(FHandleType,FHandle);
- FHandle:=0;
- end;
- end;
- function TODBCObject.GetHandle: SQLHandle;
- begin
- If FHandle=0 then
- CreateHandle;
- Result:=FHandle;
- end;
- function TODBCObject.GetHandleAllocated: Boolean;
- begin
- Result:=(FHandle<>0)
- end;
- function TODBCObject.ParentHandle: SQLHandle;
- begin
- Result:=SQL_NULL_HANDLE;
- end;
- { TODBCEnvironment }
- constructor TODBCEnvironment.Create(Aowner: TComponent);
- begin
- FHandleType:=SQL_HANDLE_ENV;
- inherited;
- end;
- function TODBCEnvironment.CreateHandle: SQLHandle;
- begin
- Result:=Inherited CreateHandle;
- ODBCbehaviour:=SQL_OV_ODBC3;
- end;
- function TODBCEnvironment.GetDataSourceNames(List: Tstrings;
- Types: TDSNTypes;Descriptions : Boolean): Integer;
- var
- DSNName,
- DSNDesc: array[0..SQL_MAX_OPTION_STRING_LENGTH] of Char;
- lenn,lend : SQLSmallInt;
- Dir : SQLSmallInt;
- Sn,SD : String;
- begin
- Case Types of
- dtSystem : Dir:=SQL_FETCH_FIRST_SYSTEM;
- dtUser : Dir:=SQL_FETCH_FIRST_USER;
- dtBoth : Dir:=SQL_FETCH_FIRST;
- end;
- List.Clear;
- CheckODBC(SQLDatasources(Handle, Dir,
- DSNName,SQL_MAX_OPTION_STRING_LENGTH, @lenn,
- DSNDesc,SQL_MAX_OPTION_STRING_LENGTH, @lend),SErrGettingDataSources);
- Repeat
- If Not Descriptions then
- List.Add(DSNName)
- else
- begin
- SN:=DSNName;
- SD:=DSNDesc;
- List.Add(SN+'='+SD);
- end;
- Until Not (SQLDataSources(Handle, SQL_FETCH_NEXT,
- DSNName, SQL_MAX_OPTION_STRING_LENGTH, @lenn,
- DSNDesc,SQL_MAX_OPTION_STRING_LENGTH, @lend) in ODBCSuccess);
- Result:=List.Count;
- end;
- function TODBCEnvironment.GetDriverNames(List : Tstrings): Integer;
- Var
- DriverName: array[0..SQL_MAX_OPTION_STRING_LENGTH] of Char;
- len : SQLSmallInt;
- begin
- List.Clear;
- CheckODBC(SQLDrivers(Handle, SQL_FETCH_FIRST, DriverName,
- SQL_MAX_OPTION_STRING_LENGTH, @len, Nil,0,Nil),SErrGettingDriverNames);
- Repeat
- List.Add(DriverName);
- Until Not (SQLDrivers(Handle, SQL_FETCH_NEXT, DriverName,
- SQL_MAX_OPTION_STRING_LENGTH, @len, Nil,0,Nil) in ODBCSuccess);
- Result:=List.Count;
- end;
- function TODBCEnvironment.GetDriverOptions(Driver : String;Options: Tstrings): Integer;
- Var
- DriverName,
- DriverOptions: array[0..SQL_MAX_OPTION_STRING_LENGTH] of Char;
- lenn,leno : SQLSmallInt;
- Found : Boolean;
- P : PChar;
- S : string;
- begin
- CheckODBC(SQLDrivers(Handle, SQL_FETCH_FIRST, DriverName,
- SQL_MAX_OPTION_STRING_LENGTH, @lenn, DriverOptions,
- SQL_MAX_OPTION_STRING_LENGTH,@Leno),SErrGettingDriverOptions);
- Result:=0;
- Options.Clear;
- Repeat
- Found:=CompareText(Driver,DriverName)=0;
- If Found then
- begin
- P:=@DriverOptions[0];
- While P[0]<>#0 do
- begin
- S:=StrPas(P);
- options.Add(S);
- Inc(P,Length(S)+1);
- end;
- end;
- Until Not (SQLDrivers(Handle, SQL_FETCH_NEXT, DriverName,
- SQL_MAX_OPTION_STRING_LENGTH, @lenn, DriverOptions,
- SQL_MAX_OPTION_STRING_LENGTH,@Leno) in ODBCSuccess) or Found;
- Result:=Options.Count;
- end;
- function TODBCEnvironment.GetIntAttribute(const Attr: Integer): Integer;
- begin
- CheckODBC(SQLSetEnvAttr(Handle,Attr,SQLPointer(@result),0),SErrSettingEnvAttribute);
- end;
- function TODBCEnvironment.GetNullTerminate: Boolean;
- begin
- Result:=(GetIntAttribute(SQL_ATTR_OUTPUT_NTS)=SQL_TRUE);
- end;
- function TODBCEnvironment.GetStringAttribute(const Attr: Integer): String;
- Var
- OldLen,Len: Integer;
- begin
- OldLen:=0;
- Repeat
- Inc(OldLen,255);
- SetLength(Result,OldLen);
- CheckODBC(SQLGetEnvAttr(Handle,Attr,SQLPointer(@result),OldLen,@Len),SErrGettingEnvAttribute);
- until (Len<=OldLen);
- SetLength(Result,Len);
- end;
- procedure TODBCEnvironment.SetIntAttribute(const Attr, Value: Integer);
- begin
- CheckODBC(SQLSetEnvAttr(Handle,Attr,SQLPointer(Value),0),SErrSettingEnvAttribute);
- end;
- procedure TODBCEnvironment.SetNullTerminate(const Value: Boolean);
- begin
- If Value then
- SetIntAttribute(SQL_ATTR_OUTPUT_NTS,SQL_TRUE)
- else
- SetIntAttribute(SQL_ATTR_OUTPUT_NTS,SQL_FALSE);
- end;
- procedure TODBCEnvironment.SetODBCbehaviour(const Value: Integer);
- begin
- If (Value<>FODBCBehaviour) then
- begin
- If Not (Value in [SQL_OV_ODBC3,SQL_OV_ODBC2]) Then
- ODBCErrorFmt(SErrInvalidBehaviour,[Value]);
- SetIntAttribute(SQL_ATTR_ODBC_VERSION,Value);
- FODBCBehaviour := Value;
- end;
- end;
- procedure TODBCEnvironment.SetStringAttribute(const Attr: Integer;
- Value: String);
- begin
- CheckODBC(SQLSetEnvAttr(Handle,Attr,SQLPointer(Value),Length(Value)),SErrSettingEnvAttribute);
- end;
- { TODBCConnection }
- procedure TODBCConnection.CheckActive;
- begin
- If Not FActive then
- ODBCError(SErrNotConnected);
- end;
- procedure TODBCConnection.CheckInActive;
- begin
- If FActive then
- ODBCError(SErrConnected);
- end;
- procedure TODBCConnection.Connect;
- begin
- If Not FActive then
- begin
- If Assigned (FonBrowseConnection) then
- ConnectBrowsing
- else If (FDSN<>'') then
- ConnectToDSN
- else if FDriverName<>'' then
- ConnectToDriver
- else
- ODBCError(SNeedDSNOrDriver);
- FActive:=True;
- end;
- end;
- Function ListToBuf(List : Tstrings; Buf : PChar; Sep : Char; MaxLen : Integer) : Boolean;
- Var
- P : PChar;
- S : String;
- I,Len : Integer;
- begin
- P:=Buf;
- I:=0;
- Result:=True;
- While Result and (I<List.Count) do
- begin
- S:=List[i];
- If I<List.Count-1 then
- S:=S+Sep;
- Len:=Length(S);
- Result:=(Longint(P-Buf)+Len)<=MaxLen;
- If Result then
- begin
- Move(S[1],P^,Len);
- Inc(P,Len);
- end;
- Inc(i);
- end;
- P[0]:=#0;
- end;
- Function BufToList(Buf : PChar;MaxLen : Integer;List : Tstrings;Sep : Char) : Integer;
- Var
- S : String;
- P : PChar;
- Totlen,Len : Integer;
- begin
- List.Clear;
- Result:=0;
- P:=Buf;
- TotLen:=0;
- While (P[0]<>#0) or (totlen<Maxlen) do
- begin
- Len:=0;
- While Not (P[len] in [Sep,#0]) do
- Inc(len);
- SetLength(S,Len);
- List.Add(S);
- Move(P[0],S[1],Len);
- Inc(P,Len);
- If P[0]<>#0 then
- Inc(P,1);
- inc(Totlen,Len+1);
- end;
- Result:=List.Count;
- end;
- Procedure TODBCConnection.ConnectBrowsing;
- Var
- Inlist,OutList : TStringList;
- InStr,
- OutStr: Array[0..SQL_MAX_OPTION_STRING_LENGTH] of Char;
- i,Res : Integer;
- olen : SQLSmallint;
- begin
- InList:=TStringList.Create;
- OutList:=TstringList.Create;
- try
- If FDSN<>'' then
- InList.Add('DSN='+FDSN)
- else If FDriverName<>'' then
- begin
- Inlist.Add('DRIVER='+FDriverName);
- For I:=0 to DriverParams.Count-1 do
- Inlist.Add(DriverParams[i]);
- end;
- Repeat
- ListToBuf(Inlist,Instr,';',SQL_MAX_OPTION_STRING_LENGTH);
- Res:=SQLBrowseConnect(Handle,Instr,SQL_NTS,Outstr,SQL_MAX_OPTION_STRING_LENGTH,Olen);
- If RES=SQL_NEED_DATA then
- begin
- OutList.Clear;
- BufToList(OutStr,Olen,OutList,';');
- FOnBrowseConnection(Self,InList,OutList);
- end
- Until (Res<>SQL_NEED_DATA);
- CheckODBC(Res,SErrBrowseConnecting);
- Finally
- Outlist.free;
- InList.Free;
- end;
- end;
- Procedure TODBCConnection.ConnectToDSN;
- begin
- CheckODBC(SQLConnect(Handle,PSQLChar(FDSN),SQL_NTS,
- PSQLChar(FUserName),SQL_NTS,
- PSQLChar(FPassword),SQL_NTS),SErrDSNConnect);
- end;
- Procedure TODBCConnection.ConnectToDriver;
- Var
- Instr,
- OutStr : Array[0..SQL_MAX_OPTION_STRING_LENGTH] of Char;
- OLen : SQLSmallint;
- InList : TStringList;
- begin
- InList:=TStringList.Create;
- Try
- Inlist.Assign(DriverParams);
- Inlist.Insert(0,'DRIVER={'+DRIVERNAME+'}');
- ListToBuf(Inlist,InStr,';',SQL_MAX_OPTION_STRING_LENGTH);
- Finally
- Inlist.Free;
- end;
- CheckODBC(SQLDriverConnect(Handle,FWindowHandle,
- Instr,SQL_NTS,
- OutStr,SQL_MAX_OPTION_STRING_LENGTH,
- Olen,FDriverCompletion),SErrDriverConnect);
- end;
- constructor TODBCConnection.Create(Aowner: TComponent);
- begin
- inherited;
- FHandleType:=SQL_HANDLE_DBC;
- FDriverParams:=TStringList.Create;
- FDriverCompletion:=SQL_DRIVER_NOPROMPT;
- end;
- destructor TODBCConnection.Destroy;
- begin
- Disconnect;
- inherited;
- end;
- procedure TODBCConnection.Disconnect;
- begin
- If FActive then
- begin
- CheckODBC(SQLDisconnect(Handle),SErrDisconnecting);
- Factive:=False;
- end;
- end;
- function TODBCConnection.GetDriverName: String;
- begin
- Result:=FDriverName;
- end;
- function TODBCConnection.GetDriverParams: TStrings;
- begin
- Result:=FDriverParams;
- end;
- function TODBCConnection.GetEnvironment: TODBCEnvironMent;
- begin
- If FEnvironment=Nil then
- result:=DefaultEnvironment
- else
- Result:=FEnvironment;
- end;
- procedure TODBCConnection.SetActive(const Value: Boolean);
- begin
- If Value then
- Connect
- else
- Disconnect;
- end;
- procedure TODBCConnection.SetDriverName(const Value: String);
- begin
- CheckInactive;
- FDSN:='';
- If CompareText(FDriverName,Value)<>0 then
- begin
- FDriverName:=Value;
- FDriverParams.Clear;
- end;
- end;
- procedure TODBCConnection.SetDriverParams(const Value: TStrings);
- begin
- CheckInactive;
- FDriverParams.Assign(Value);
- end;
- procedure TODBCConnection.SetDSN(const Value: String);
- begin
- CheckInactive;
- FDSN := Value;
- end;
- procedure TODBCConnection.SetEnvironment(const Value: TODBCEnvironMent);
- begin
- CheckInactive;
- If (Value<>Environment) then // !! may be defaultenvironment...
- begin
- If HandleAllocated then
- FreeHandle;
- FEnvironment:=Value
- end;
- end;
- function TODBCConnection.ParentHandle: SQLHandle;
- begin
- Result:=Environment.Handle
- end;
- Const
- DefEnv : Pointer = Nil;
- Function DefaultEnvironment : TODBCEnvironment;
- begin
- If DefEnv=Nil then
- DefEnv:=TODBCEnvironment.Create(Nil);
- Result:=TODBCEnvironment(DefEnv);
- end;
- procedure TODBCConnection.GetTableNames(S: TStrings;
- SystemTables: Boolean);
- begin
- With TODBCTableList.Create(Self) do
- try
- GetTableNames(S,SystemTables);
- finally
- Free;
- end;
- end;
- procedure TODBCConnection.GetFieldNames(TableName: String; S: TStrings);
- begin
- With TODBCFieldNamesList.Create(Self) do
- try
- GetFieldNames(TableName,S);
- finally
- Free;
- end;
- end;
- procedure TODBCConnection.GetPrimaryKeyFields(TableName: String;
- S: TStrings);
- begin
- With TODBCPrimaryKeyFieldsList.Create(Self) do
- try
- GetPrimaryKeyFields(TableName,S);
- finally
- Free;
- end;
- end;
- procedure TODBCConnection.GetProcedureNames(S: TStrings);
- begin
- With TODBCProcedureList.Create(Self) do
- try
- GetProcedureList(S);
- Finally
- Free;
- end;
- end;
- procedure TODBCConnection.GetProcedureParams(ProcName: String;
- ParamTypes: TODBCParamTypes; S: TStrings);
- begin
- With TODBCProcedureParams.Create(Self) do
- Try
- GetProcedureParams(ProcName,Paramtypes,S);
- finally
- Free;
- end;
- end;
- { TODBCStatement }
- Type
- TODBCFieldBufRec = Record
- T{ype} : SQlSmallint;
- B{ufsize} : SQLInteger;
- {Buftyp}e : SQLSmallint;
- end;
- Const
- BufDescrCount = 26;
- BufDescr : Array[1..BufDescrCount] of TODBCFieldBufRec =
- { Type Bufsize Buftype }
- (
- (T:SQL_CHAR ;b:-1 ;e: SQL_CHAR),
- (T:SQL_NUMERIC ;b:sizeof(SQLDouble) ;e: SQL_DOUBLE),
- (T:SQL_DECIMAL ;b:sizeof(SQLDouble) ;e: SQL_DOUBLE),
- (T:SQL_INTEGER ;b:sizeof(SQLInteger) ;e: SQL_INTEGER),
- (T:SQL_SMALLINT ;b:sizeof(SQLSmallInt) ;e: SQL_SMALLINT),
- (T:SQL_FLOAT ;b:sizeof(SQLDOUBLE) ;e: SQL_DOUBLE),
- (T:SQL_REAL ;b:sizeof(SQLDOUBLE) ;e: SQL_DOUBLE),
- (T:SQL_DOUBLE ;b:Sizeof(SQLDOUBLE) ;e: SQL_DOUBLE),
- (T:SQL_DATE ;b:Sizeof(SQL_DATE_STRUCT) ;e: SQL_DATE),
- (T:SQL_TIME ;b:sizeof(SQL_TIME_STRUCT) ;e: SQL_TIME),
- (T:SQL_TIMESTAMP ;b:sizeof(SQL_TIMESTAMP_STRUCT) ;e: SQL_TIMESTAMP),
- (T:SQL_VARCHAR ;b:-1 ;e: SQL_CHAR),
- (T:SQL_UNKNOWN_TYPE ;b:0 ;e: SQL_UNKNOWN_TYPE),
- (T:SQL_LONGVARCHAR ;b:-1 ;e: SQL_CHAR),
- (T:SQL_BINARY ;b:-2 ;e: SQL_BINARY),
- (T:SQL_VARBINARY ;b:-2 ;e: SQL_BINARY),
- (T:SQL_LONGVARBINARY ;b:-2 ;e: SQL_BINARY),
- (T:SQL_BIGINT ;b:sizeOf(SQLDOUBLE) ;e: SQL_DOUBLE),
- (T:SQL_TINYINT ;b:Sizeof(SQLSMALLINT) ;e: SQL_SMALLINT),
- (T:SQL_BIT ;b:sizeof(SQL_CHAR) ;e: SQL_BIT),
- (T:SQL_WCHAR ;b:-1 ;e: SQL_CHAR),
- (T:SQL_WVARCHAR ;b:-1 ;e: SQL_CHAR),
- (T:SQL_WLONGVARCHAR ;b:-1 ;e: SQL_CHAR),
- (T:SQL_TYPE_DATE ;b:sizeof(SQL_DATE_STRUCT) ;e: SQL_TYPE_DATE),
- (T:SQL_TYPE_TIME ;b:sizeof(SQL_TIME_STRUCT) ;e: SQL_TYPE_TIME),
- (T:SQL_TYPE_TIMESTAMP;b:sizeof(SQL_TIMESTAMP_STRUCT) ;e: SQL_TYPE_TIMESTAMP)
- );
- { // template
- (T: ;b: ;e: ),
- }
- Function GetColSizeBufType (Coltype: SQLSmallint;
- Var BufSize : SQLInteger;
- Var BufType : SQLSmallInt) : Boolean;
- Var
- I : Integer;
- begin
- I:=0;
- BufSize:=0;
- BufType:=0;
- While (I<=BufDescrCount) and (BufDescr[i].t<>Coltype) do
- Inc(i);
- Result:=(i<=BufDescrCount);
- If Result then
- begin
- BufSize:=BufDescr[i].b;
- BufType:=BufDescr[i].e;
- end;
- end;
- procedure TODBCStatement.BindFields(RestrictList : TStrings);
- Var
- Count: SQLSmallInt;
- CName : Array[0..SQL_NAME_LEN] of Char;
- CSize : SQLUINTEGER;
- CDataType,CDecimals,CNullable,CNameLen: SQLSmallInt;
- I : integer;
- begin
- CheckODBC(SQLNumResultCols(Handle,Count),SErrColumnCount);
- For I:=1 to Count do
- begin
- CheckODBC(SQLDescribeCol(Handle,i,CName,SQL_NAME_LEN,CNameLen,
- CdataType,CSize, CDecimals,CNullable)
- ,SErrColDescription);
- If Not Assigned(RestrictList) or (RestrictList.IndexOf(Cname)<>-1) then
- With FFields.Add as TODBCField do
- begin
- FPosition:=I;
- FName:=Cname;
- FDataType:=CDataType;
- FSize:=CSize;
- FDecimalDigits:=CDecimals;
- FNullable:=(CNullable=SQL_TRUE);
- GetColsizeBufType(FDataType,FBufSize,FBufType);
- If FBufSize=-1 then
- FBufSize:=FSize;
- end;
- end;
- AllocBuffers;
- For I:=0 to Count-1 do
- With FFields.Items[i] as TODBCField do
- CheckODBC(SQLBindCol(Handle,FPosition,FBufType,GetData,FBufSize,FBuffer+FBuffOffset)
- ,SErrBindCol);
- end;
- procedure TODBCStatement.ClearFields;
- begin
- FFields.Clear;
- end;
- constructor TODBCStatement.Create(Aowner: TComponent);
- begin
- FHandleType:=SQL_HANDLE_STMT;
- inherited;
- If AOwner is TODBCConnection then
- Connection:=TODBCConnection(Aowner);
- FFields:=TODBCFieldList.Create(Self);
- end;
- function TODBCStatement.ParentHandle: SQLHandle;
- begin
- If (Connection=Nil) then
- ODBCError(SErrNoConnectionForStatement);
- Result:=Connection.Handle;
- end;
- procedure TODBCStatement.SetConnection(const Value: TODBCConnection);
- begin
- If Value<>FConnection then
- begin
- If HandleAllocated then
- FreeHandle;
- FConnection := Value;
- end;
- end;
- Function TODBCStatement.fetch : Boolean;
- Var
- res : SQLReturn;
- begin
- Res:=SQLFetch(Handle);
- Result:=(Res=SQL_SUCCESS);
- If Not Result and (Res<>SQL_NO_DATA) then
- CheckODBC(Res,SErrFetchingData);
- FBof:=False;
- If (Res=SQL_NO_DATA) then
- FEOF:=True;
- end;
- destructor TODBCStatement.Destroy;
- begin
- FFields.Free;
- inherited;
- end;
- { TODBCSQLStatement }
- procedure TODBCSQLStatement.GetFieldList(List : TStrings);
- Var
- Count: SQLSmallInt;
- CName : Array[0..SQL_NAME_LEN] of Char;
- CSize : SQLUINTEGER;
- CDataType,CDecimals,CNullable,CNameLen: SQLSmallInt;
- I : integer;
- begin
- if Not (FState in [ssPrepared,ssBound,ssOpen]) then
- ODBCError(SErrNotPrepared);
- List.Clear;
- CheckODBC(SQLNumResultCols(Handle,Count),SErrColumnCount);
- For I:=1 to Count do
- begin
- CheckODBC(SQLDescribeCol(Handle,i,CName,SQL_NAME_LEN,CNameLen,
- CdataType,CSize, CDecimals,CNullable)
- ,SErrColDescription);
- List.Add(CName);
- end;
- end;
- procedure TODBCSQLStatement.Unprepare;
- begin
- Case FState of
- ssBound,ssOpen :
- begin
- ClearFields;
- FreeStatement(SQL_CLOSE);
- end;
- ssPrepared : begin
- FreeStatement(SQL_CLOSE);
- end;
- end;
- FState:=ssInactive;
- end;
- procedure TODBCSQLStatement.FreeStatement(Option : SQLUSMALLINT);
- begin
- SQLFreeStmt(Handle,SQL_CLOSE);
- end;
- procedure TODBCSQLStatement.Close;
- begin
- if FState<>ssInactive then
- begin
- Unprepare;
- FreeStatement(SQL_CLOSE);
- FState:=ssInactive;
- end;
- end;
- constructor TODBCSQLStatement.Create(Aowner: TComponent);
- begin
- inherited;
- FSQL:=TStringList.Create;
- end;
- destructor TODBCSQLStatement.Destroy;
- begin
- if FState=ssOpen then
- Close
- else If FState<>ssInactive then
- Unprepare;
- FSQL.Free;
- inherited;
- end;
- procedure TODBCSQLStatement.ExecSQL;
- begin
- Case FState of
- ssPrepared,ssBound : ExecutePrepared;
- ssInactive : ExecuteDirect;
- else
- Raise Exception.Create(SErrStatementActive)
- end;
- end;
- procedure TODBCSQLStatement.ExecuteDirect;
- Var
- S : String;
- begin
- if FState<>ssInactive then
- ODBCError(SErrStatementActive);
- S:=SQL.Text;
- CheckODBC(SQLExecDirect(Handle,PChar(S),SQL_NTS),SErrExecuting);
- end;
- procedure TODBCSQLStatement.ExecutePrepared;
- begin
- if Not (FState in [ssPrepared,ssBound]) then
- ODBCError(SErrNotPrepared);
- CheckODBC(SQLExecute(Handle),SErrExecutingPrepared);
- end;
- function TODBCSQLStatement.GetActive: Boolean;
- begin
- Result:=(FState=ssOpen);
- end;
- procedure TODBCSQLStatement.Open;
- begin
- if (FState<>ssOpen) then
- begin
- Writeln('Preparing');
- If FState=ssInactive then
- Prepare;
- Writeln('Bind fields');
- if FState=ssPrepared then
- BindFields(Nil);
- Writeln('Executing');
- ExecSQL;
- Writeln('Fetching');
- If FState=ssBound then
- Fetch;
- FState:=ssOpen;
- FBOF:=True;
- end;
- end;
- procedure TODBCSQLStatement.Prepare;
- Var
- S : String;
- begin
- If FState<>ssInactive then
- ODBCError(SErrNotInactive);
- If (FSQL.Count=0) then
- ODBCError(SErrNoSQLStatement);
- S:=FSQL.Text;
- CheckODBC(SQLPrepare(Handle,PChar(S),SQL_NTS),SErrPreparing);
- FState:=ssPrepared;
- end;
- procedure TODBCSQLStatement.SetActive(const Value: Boolean);
- begin
- If Value then
- Open
- else
- Close;
- end;
- procedure TODBCSQLStatement.SetSQL(const Value: TStrings);
- begin
- FSQL.Assign(Value);
- end;
- procedure TODBCSQLStatement.BindFields(RestrictList: TStrings);
- begin
- inherited;
- FState:=ssBound;
- end;
- procedure TODBCStatement.AllocBuffers;
- Var
- I,TotalSize,AddSize : Integer;
- begin
- TotalSize:=0;
- For i:=0 to FFields.Count-1 do
- With (FFields.Items[i] as TODBCField) do
- begin
- AddSize:=FBufSize;
- If FBufSize=-2 then // Blob.
- AddSize:=0
- else if FBufSize=-1 then
- AddSize:=FSize+1; // some Char variant.
- // Store offset temporarily in FData
- FBuffOffset:=TotalSize;
- Inc(TotalSize,AddSize+SizeOf(SQLinteger));
- end;
- FBuffer:=GetMem(TotalSize);
- TotalSize:=0;
- For i:=0 to FFields.Count-1 do
- With (FFields.Items[i] as TODBCField) do
- FBuffer:=Self.FBuffer;
- end;
- { TODBCTableList }
- procedure TODBCTableList.GetTableNames(S: TStrings; SystemTables : Boolean);
- var
- TName,
- TType: array[0..SQL_NAME_LEN+1] of char;
- NL,TL: SQLINTEGER;
- Res: SQLRETURN;
- begin
- S.Clear;
- Res:=CheckODBC(SQLTables(handle, nil,0,nil,0,nil,0,nil,0),SErrGettingTableNames);
- if Res=SQL_SUCCESS then
- begin
- // Must bind by colno, because names changed between ODBC 2.0 and 3.0 !!
- SQLBindCol(handle,3,SQL_CHAR,@TName,SQL_NAME_LEN,@NL);
- SQLBindCol(handle,4,SQL_CHAR,@TType,SQL_NAME_LEN,@TL);
- While Fetch do
- if SystemTables or (CompareText(TType,'SYSTEM TABLE')<>0) then
- S.Add(TName);
- end;
- end;
- { TODBCFieldNamesList }
- procedure TODBCFieldNamesList.GetFieldNames(TableName: String;
- S: TStrings);
- var
- FName : array[0..SQL_NAME_LEN+1] of char;
- NF : SQLINTEGER;
- Res: SQLRETURN;
- begin
- S.Clear;
- Res:=CheckODBC(SQLColumns(handle, nil, 0, nil, 0, pchar(TableName), SQL_NTS, nil, 0),SErrFieldNames);
- if Res=SQL_SUCCESS then
- begin
- SQLBindCol(handle, 4, SQL_CHAR, @FNAme, SQL_NAME_LEN, @NF);
- While Fetch do
- S.Add(FName);
- end;
- end;
- { TODBCPrimaryKeyFieldsList }
- procedure TODBCPrimaryKeyFieldsList.GetPrimaryKeyFields(TableName: String;
- S: TStrings);
- var
- FName : array[0..SQL_NAME_LEN+1] of char;
- NF : SQLINTEGER;
- Res: SQLRETURN;
- begin
- S.Clear;
- Res:=CheckODBC(SQLPrimaryKeys(handle, nil, 0, nil, 0, pchar(TableName), SQL_NTS),SErrPrimaryKeys);
- if Res=SQL_SUCCESS then
- begin
- SQLBindCol(handle, 4, SQL_CHAR, @FNAme, SQL_NAME_LEN, @NF);
- While Fetch do
- S.Add(FName);
- end;
- end;
- { TODBCProcedureList }
- procedure TODBCProcedureList.GetProcedureList(S: TStrings);
- var
- PName : array[0..SQL_NAME_LEN+1] of char;
- NP : SQLINTEGER;
- Res: SQLRETURN;
- begin
- S.Clear;
- Res:=CheckODBC(SQLProcedures(handle, nil, 0, nil, 0, Nil, 0),SErrProcedureNames);
- if Res=SQL_SUCCESS then
- begin
- SQLBindCol(handle, 3, SQL_CHAR, @PNAme, SQL_NAME_LEN, @NP);
- While Fetch do
- S.Add(PName);
- end;
- end;
- { TODBCProcedureParams }
- procedure TODBCProcedureParams.GetProcedureParams(ProcName: String;
- ParamTypes: TODBCParamTypes; S: TStrings);
- var
- PName : array[0..SQL_NAME_LEN+1] of char;
- NP,NT : SQLINTEGER;
- Ptype : SQLSmallInt;
- Res: SQLRETURN;
- begin
- S.Clear;
- Res:=CheckODBC(SQLProcedureColumns(handle, nil, 0, nil, 0, PChar(ProcName),SQL_NTS,Nil, 0),SErrProcedureNames);
- if Res=SQL_SUCCESS then
- begin
- SQLBindCol(handle, 4, SQL_CHAR, @PName, SQL_NAME_LEN, @NP);
- SQLBindCol(handle, 5, SQL_SMALLINT, @PType, SizeOf(SQLSmallInt), @NT);
- While Fetch do
- begin
- If TODBCParamType(PType) in ParamTypes then
- S.Add(PName);
- end;
- end;
- end;
- { TODBCFieldList }
- constructor TODBCFieldList.Create(Statement: TODBCStatement);
- begin
- FStatement:=Statement;
- Inherited Create(TODBCField);
- end;
- { TODBCField }
- function TODBCField.GetAsString: String;
- begin
- If IsNull then
- Result:=''
- else
- Case FBufType of
- SQL_Smallint : Result:=IntToStr(PSQLSmallInt(Data)^);
- SQL_Integer : Result:=IntToStr(PSQLINTEGER(Data)^);
- SQL_BIT : Result:=IntToStr(PByte(Data)^);
- SQL_CHAR : Result:=StrPas(Data);
- SQL_DOUBLE : Result:=FloatToStr(GetAsDouble);
- SQL_DATE : result:=DateToStr(AsDateTime);
- SQL_TIME : Result:=TimeToStr(AsDateTime);
- SQL_TIMESTAMP : result:=datetimeToStr(AsDateTime);
- SQL_TYPE_DATE : result:=dateToStr(AsDateTime);
- SQL_TYPE_TIMESTAMP : result:=datetimeToStr(AsDateTime);
- SQL_TYPE_TIME : Result:=TimeToStr(AsDateTime);
- else
- ODBCError(SErrInvalidConversion)
- end;
- end;
- function TODBCField.GetData : Pchar;
- begin
- Result:=FBuffer+FBuffOffset+SizeOf(SQLinteger);
- end;
- function TODBCField.GetIsNull : boolean;
- begin
- Result:=PSQLinteger(FBuffer+FBuffOffset)^=SQL_NULL_DATA;
- end;
- Function TODBCField.GetAsInteger : Integer;
- begin
- If IsNull then
- Result:=0
- else
- Case FBufType of
- SQL_Smallint : Result:=PSQLSmallInt(Data)^;
- SQL_Integer : Result:=PSQLINTEGER(Data)^;
- SQL_BIT : Result:=PByte(Data)^;
- SQL_CHAR : Result:=StrToInt(GetAsString);
- SQL_DOUBLE : Result:=Round(GetAsDouble);
- SQL_DATE,
- SQL_TIME,
- SQL_TIMESTAMP,
- SQL_TYPE_DATE,
- SQL_TYPE_TIMESTAMP,
- SQL_TYPE_TIME : Result:=Round(AsDateTime);
- else
- ODBCError(SErrInvalidConversion)
- end;
- end;
- Function TODBCField.GetAsBoolean : Boolean;
- begin
- If IsNull then
- Result:=False
- else
- Case FBufType of
- SQL_Smallint : Result:=PSQLSmallInt(Data)^=0;
- SQL_Integer : Result:=PSQLINTEGER(Data)^=0;
- SQL_BIT : Result:=PBYTE(Data)^=0;
- SQL_CHAR : Result:=(StrToInt(GetAsString)=0);
- SQL_DOUBLE : Result:=Round(GetAsDouble)=0;
- SQL_DATE,
- SQL_TIME,
- SQL_TIMESTAMP,
- SQL_TYPE_DATE,
- SQL_TYPE_TIMESTAMP,
- SQL_TYPE_TIME : Result:=Round(AsDateTime)=0;
- else
- ODBCError(SErrInvalidConversion)
- end;
- end;
- Function TODBCField.GetAsDouble : Double;
- begin
- If IsNull then
- Result:=0
- else
- Case FBufType of
- SQL_Smallint : Result:=PSQLSmallInt(Data)^;
- SQL_Integer : Result:=PSQLINTEGER(Data)^;
- SQL_BIT : Result:=PBYTE(Data)^;
- SQL_CHAR : Result:=StrToFloat(GetAsString);
- SQL_DOUBLE : Result:=PSQLDOUBLE(GetData)^;
- SQL_DATE,
- SQL_TIME,
- SQL_TIMESTAMP,
- SQL_TYPE_DATE,
- SQL_TYPE_TIMESTAMP,
- SQL_TYPE_TIME : Result:=AsDateTime;
- else
- ODBCError(SErrInvalidConversion)
- end;
- end;
- {
- function DateStructToDateTime( b:PSQL_DATE_STRUCT):TDateTime;
- function DateTimeToDateStruct( b:TDateTime):SQL_DATE_STRUCT;
- procedure DateTime2TimeStampStruct( var Value:SQL_TIMESTAMP_STRUCT; b:TDateTime);
- }
- Function TODBCField.GetAsDateTime : TDateTime;
- begin
- If IsNull then
- Result:=0
- else
- Case FBufType of
- SQL_Smallint : Result:=PSQLSmallInt(Data)^;
- SQL_Integer : Result:=PSQLINTEGER(Data)^;
- SQL_BIT : Result:=PBYTE(Data)^;
- SQL_CHAR : Result:=StrToInt(GetAsString);
- SQL_DOUBLE : Result:=PSQLDOUBLE(GetData)^;
- SQL_DATE : Result:=DateStructToDateTime(PSQL_DATE_STRUCT(Data));
- SQL_TIME : Result:=TimeStructToDateTime(PSQL_TIME_STRUCT(Data));
- SQL_TIMESTAMP : Result:=TimeStampStructToDateTime(PSQL_TIMESTAMP_STRUCT(Data));
- SQL_TYPE_DATE : Result:=DateStructToDateTime(PSQL_DATE_STRUCT(Data));
- SQL_TYPE_TIMESTAMP : Result:=TimeStampStructToDateTime(PSQL_TIMESTAMP_STRUCT(Data));
- SQL_TYPE_TIME : Result:=TimeStructToDateTime(PSQL_TIME_STRUCT(Data));
- else
- ODBCError(SErrInvalidConversion)
- end;
- end;
- Finalization
- If Assigned(DefEnv) then
- TODBCEnvironment(DefEnv).Free;
- end.
|