123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464 |
- 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.
|