(*****************************************************************************)
(* MODULE: SQLTABLE                                                          *)
(* RIGHTS: Reggatta Systems - COPYRIGHT 1995 - 2000                          *)
(* DESCR.: This module contains the Scalable SQL Class                       *)
(*****************************************************************************)
Unit Sqlapi;
Interface
{$I BSE.INC}
{$I BAPI.INC}
Uses
  BseTypes;

Type
  SQL_YIELD_T = 
  Record
     iSessionID : Word;
  end;

  BTRV_YIELD_T = 
  Record
     iOpCode           : Word;
     bClientIDlastFour : ARRAY[ 1..4 ] OF BYTE;
  end;

  BTI_CB_INFO_T = 
  Record
     typex : Word;
     size  : Word;
     case u: Boolean of
       False: ( sYield : SQL_YIELD_T );
       True:  ( bYield : BTRV_YIELD_T );
  end;

  BTI_CB_FUNC_PTR_T = Function
    (
	  VAR bCallbackInfo : BTI_CB_INFO_T;
      VAR bUserData
    )   : Word;

  BTI_CB_FUNC_PTR_PTR_T = ^BTI_CB_FUNC_PTR_T;


Type
  TxGetSessionID = Function
    (
      Var iSessionID : SmallInt
    ) : SmallInt;
    StdCall;


  TxPutSessionID = Function
    ( 
      iSessionID : SmallInt
    ) : SmallInt;
    StdCall;


  TXQLCallBack = Function
    (
      iAction                   : Word;
      iOption                   : Word;
      fCallBackFunction         : BTI_CB_FUNC_PTR_T;
      fPreviousCallBackFunction : BTI_CB_FUNC_PTR_PTR_T;
      VAR bUserData;
      VAR bPreviousUserData     : POINTER
    ) : SmallInt;
    StdCall;


  TxShareSessionID = Function
    (
      VAR tChangeCount : SmallInt
    ) : SmallInt;
    StdCall;


  TSQLGetCountDatabaseNames = Function
    (
	  VAR tCount : SmallInt
    ) : SmallInt;
    StdCall;


  TSQLGetDatabaseNames = Function
    (
      VAR tBufLen  : SmallInt;
      VAR bDataBuf : CHAR
    ) : SmallInt;
    StdCall;


  TSQLUnloadDBNAMES = Function
    (
	iReserved : SmallInt
    ) : SmallInt;


 TXQLCursor = Function
   (
     VAR iCursorID : SmallInt
    ) : SmallInt;
    StdCall;



 TXQLCompile = Function
   (
     iCursorID     : SmallInt;
     VAR tStatementLen : SmallInt;
     VAR sStatement
    ) : SmallInt;
    StdCall;


 TXQLDescribe = Function
   (
     iCursorID   : SmallInt;
     tPosition   : SmallInt;
     VAR iDataType   : SmallInt;
     VAR tSize       : SmallInt;
     VAR tDecPlaces  : SmallInt;
     VAR tDisplayLen : SmallInt;
     VAR tNameLen    : SmallInt;
     VAR sName       : CHAR
    ) : SmallInt;
    StdCall;


 TXQLExec = Function
   (
     iCursorID : SmallInt
   ) : SmallInt;
   StdCall;



 TXQLFetch = Function
   (
     iCursorID  : SmallInt;
     iOption    : SmallInt;
     VAR tBufLen    : SmallInt;
     VAR bDataBuf;
     VAR lCount     : LONGINT;
     iASCIIFlag : SmallInt;
     iSpacing   : SmallInt
   ) : SmallInt;
   StdCall;



 TXQLFormat = Function
   (
     iCursorID : SmallInt;
     tPosition : SmallInt;
     tMaskLen  : SmallInt;
     VAR sMask
   ) : SmallInt;
   StdCall;

 TXQLFree = TXQLExec;

 TXQLLogin = Function
   (
     VAR sUser;
     VAR sPassWord;
     VAR sDDPath;
     VAR sDataPath;
     VAR sReserved;
     iFeaturesUsed : SmallInt
   ) : SmallInt;
   StdCall;


 TXQLLogout = Function : SmallInt;
   StdCall;


 TXQLSPUtility = Function
   (
     iCursorID           : SmallInt;
     iOption             : SmallInt;
     VAR tStatementCount : SmallInt;
     VAR tStatementExec  : SmallInt;
     VAR tBufLen         : SmallInt;
     VAR bDataBuf
   ) : SmallInt;
   StdCall;


 TXQLStatus = Function
   (
     iCursor : SmallInt;
     iOption : SmallInt;
     VAR sStatBuf
   ) : SmallInt;
   StdCall;

 TXQLStop = TXQLLogout;

 TXQLSubst = Function
   (
     iCursorID : SmallInt;
     tCount    : SmallInt;
     tNameLen  : SmallInt;
     VAR sVarNames;
     tTextLen  : SmallInt;
     VAR sValueText
   ) : SmallInt;
   StdCall;


 TXQLVersion = Function
   (
     VAR sVersion
   ) : SmallInt;
   StdCall;


 TXQLConvert = Function
   (
     iOption  : SmallInt;
     iType    : SmallInt;
     tSize    : SmallInt;
     tDec     : SmallInt;
     tdSize   : SmallInt;
     VAR sValue;
     VAR sRetVal;
     VAR sMask;
     sJustify : SmallInt
   ) : SmallInt;
   StdCall;


 TXQLValidate = Function
   (
     VAR tCount     : SmallInt;
     VAR sFieldName;
     tBufLen    : SmallInt;
     VAR bDataBuf
   ) : SmallInt;
   StdCall;


 TXQLMask = Function
   (
     iOption   : SmallInt;
     iType     : SmallInt;
     tSize     : SmallInt;
     tDec      : SmallInt;
     VAR tLen  : SmallInt;
     VAR sMask
   ) : SmallInt;
   StdCall;


 TxCompute = Function
   (
     iCursorID  : SmallInt;
     VAR sFldName;
     iFldType   : SmallInt;
     tFldLen    : SmallInt;
     tDecPlaces : SmallInt;
     VAR tExpLen    : SmallInt;
     VAR sExpression
   ) : SmallInt;
   StdCall;


 TxDescribe = Function
   (
     iCursorID  : SmallInt;
     iOption    : SmallInt;
     VAR tBufLen    : SmallInt;
     tPosition  : SmallInt;
     VAR tCount     : SmallInt;
     VAR bDataBuf
   ) : SmallInt;
   StdCall;


 TxMovefld = Function
   (
     iCursorID     : SmallInt;
     tFromPosition : SmallInt;
     tToPosition   : SmallInt
   ) : SmallInt;
   StdCall;


 TxFetch = Function
   (
     iCursorID    : SmallInt;
     VAR tBufLen      : SmallInt;
     iOption      : SmallInt;
     VAR lRecordCount : LONGINT;
     VAR lRejectCount : LONGINT;
     VAR bDataBuf
   ) : SmallInt;
   StdCall;


 TxField = Function
   (
     iCursorID   : SmallInt;
     iOption     : SmallInt;
     tPosition   : SmallInt;
     VAR tCount  : SmallInt;
     VAR sFldNames
   ) : SmallInt;
   StdCall;

 TxFree = TXQLExec;

 TxInsert = Function
   (
     iCursorID    : SmallInt;
     VAR tFileCount   : SmallInt;
     VAR sFileNames;
     VAR lRecordCount : LONGINT;
     VAR bDataBuf
   ) : SmallInt;
   StdCall;


 TxJoin = Function
   (
     iCursorID    : SmallInt;
     VAR sSecFile;
     VAR sOwner;
     iOption      : SmallInt;
     tPriFldCount : SmallInt;
     VAR sPriFlds;
     tSecFldCount : SmallInt;
     VAR sSecFlds
   ) : SmallInt;
   StdCall;


 TxLogin = Function
   (
     VAR sUser;
     VAR sPassWord;
     VAR sDDPath;
     VAR sDataPath;
     VAR sReserved;
     iFeaturesUsed : SmallInt
   ) : SmallInt;
   StdCall;

 TxLogout = TXQLLogout;

 TxNew = Function
   (
     VAR iCursorID : SmallInt;
     VAR sFileName;
     VAR sOwner;
     iOpenMode : SmallInt
   ) : SmallInt;
   StdCall;


 TxOrder = Function
   (
     iCursorID : SmallInt;
     tCount    : SmallInt;
     VAR sOrder
   ) : SmallInt;
   StdCall;


 TxRemall = Function
   (
     iCursorID    : SmallInt;
     VAR tFileCount   : SmallInt;
     VAR sFileNames;
     iOption      : SmallInt;
     VAR lRecordCount : LONGINT;
     lRejectCount : LONGINT
   ) : SmallInt;
   StdCall;


 TxRemove = Function
   (
     iCursorID    : SmallInt;
     VAR tFileCount   : SmallInt;
     VAR sFileNames;
     VAR lRecordCount : LONGINT
   ) : SmallInt;
   StdCall;


 TxReset = Function
   (
     VAR sReserved
   ) : SmallInt;
   StdCall;


 TxRestrict = Function
   (
     iCursorID : SmallInt;
     iOption   : SmallInt;
     VAR tExpLen   : SmallInt;
     VAR sExpression
   ) : SmallInt;
   StdCall;

 TxStop  = TXQLLogout;

 TxTrans = TXQLExec;

 TxStore = Function
   (
     iCursorID : SmallInt;
     VAR sViewName;
     tTextLen  : SmallInt;
     VAR sText
   ) : SmallInt;
   StdCall;


 TxRecall = Function
   (
     VAR iCursorID   : SmallInt;
     VAR sViewName;
     tOwnerCount : SmallInt;
     VAR sOwner;
     iOpenMode   : SmallInt;
     VAR tTextLen    : SmallInt;
     VAR sText
   ) : SmallInt;
   StdCall;


 TxUpdate = Function
   (
     iCursorID    : SmallInt;
     VAR tFileCount   : SmallInt;
     VAR sFileNames;
     VAR lRecordCount : LONGINT;
     VAR bDataBuf
   ) : SmallInt;
   StdCall;


 TxUpdall = Function
   (
     iCursorID    : SmallInt;
     VAR tFileCount   : SmallInt;
     VAR sFileNames;
     iOption      : SmallInt;
     VAR lRecordCount : LONGINT;
     VAR lRejectCount : LONGINT;
     tFldCount    : SmallInt;
     VAR sUpdateFld;
     VAR sReplaceFld
   ) : SmallInt;
   StdCall;


 TxDD = Function
   (
     VAR sPathName;
     iOption : SmallInt
   ) : SmallInt;
   StdCall;


 TxDDAttr = Function
   (
     iOption    : SmallInt;
     VAR sFldName;
     iAttrType  : SmallInt;
     VAR tBufLen    : SmallInt;
     VAR sAttrBuf
   ) : SmallInt;
   StdCall;


 TxDDModify = Function
   (
     iOption     : SmallInt;
     VAR sFileName;
     iCreate    : SmallInt;
     VAR sPathName;
     VAR sOwner;
     iOwnerFlag : SmallInt;
     tFldCount  : SmallInt;
     VAR bFldBuf;
     tIndxCount : SmallInt;
     VAR bIndxBuf
   ) : SmallInt;
   StdCall;


 TxDDCreate = Function
   (
     iOption           : SmallInt;
     VAR sFileName;
     iCreate           : SmallInt;
     VAR sPathName;
     VAR sOwner;
     iOwnerFlag        : SmallInt;
     tFldCount         : SmallInt;
     VAR bFldBuf;
     tIndxCount        : SmallInt;
     VAR bIndxBuf;
     tBufLen           : SmallInt;
     VAR bCreateParmsBuf
   ) : SmallInt;
   StdCall;


 TxDDDrop = Function
   (
     VAR sName;
     iType   : SmallInt;
     iDelete : SmallInt
   ) : SmallInt;
   StdCall;


 TxDDField = Function
   (
     iOption    : SmallInt;
     VAR tCount     : SmallInt;
     VAR sFldNames;
     VAR tBufLen    : SmallInt;
     VAR bDataBuf
   ) : SmallInt;
   StdCall;
  

 TxDDFile = Function
   (
     iOption    : SmallInt;
     VAR tCount     : SmallInt;
     VAR sFileNames;
     VAR tBufLen    : SmallInt;
     VAR bDataBuf
   ) : SmallInt;
   StdCall;


 TxDDIndex = Function
   (
     iOption    : SmallInt;
     VAR tCount     : SmallInt;
     VAR sIndexName;
     VAR tBufLen    : SmallInt;
     VAR bDataBuf
   ) : SmallInt;
   StdCall;


 TxDDPath = Function
   (
     iOption     : SmallInt;
     VAR sPathName
   ) : SmallInt;
   StdCall;


 TxDDView = Function
   (
     VAR tCount     : SmallInt;
     VAR sViewName;
     VAR tBufLen    : SmallInt;
     VAR bDataBuf
   ) : SmallInt;
   StdCall;


 TxAccess = Function
   (
     VAR sMstrPswd;
     VAR sUser;
     iOption    : SmallInt;
     iAccRights : SmallInt;
     VAR sFileName;
     VAR tCount     : SmallInt;
     VAR sFldNames;
     VAR tBufLen    : SmallInt;
     VAR bDataBuf
   ) : SmallInt;
   StdCall;


 TxPassWord = Function
   (
     VAR sUser;
     VAR sPassWord
   ) : SmallInt;
   StdCall;


 TxSecurity = Function
   (
     VAR sMstrPswd;
     iOption : SmallInt
   ) : SmallInt;
   StdCall;


 TxUser = Function
   (
     VAR sMstPswd;
     iOption    : SmallInt;
     VAR sUser;
     VAR sPassWord;
     iFlags     : SmallInt;
     VAR tCount     : SmallInt;
     VAR tBufLen    : SmallInt;
     VAR bDataBuf
   ) : SmallInt;
   StdCall;


 TxChar = Function
   (
     iOption : SmallInt;
     iType   : SmallInt;
     VAR cCharacter
   ) : SmallInt;
   StdCall;


 TxVersion = Function
   (
     VAR sVersion
   ) : SmallInt;
   StdCall;


 TxStatus = Function
   (
     iCursorID  : SmallInt;
     iOption    : SmallInt;
     VAR tLen       : SmallInt;
     VAR sStatBuf 
   ) : SmallInt;
   StdCall;


 TxConvert = Function
   (
     iOption  : SmallInt;
     iType    : SmallInt;
     tSize    : SmallInt;
     tDec     : SmallInt;
     tDSize   : SmallInt;
     VAR sValue;
     VAR sRetVal;
     VAR sMask;
     iJustify : SmallInt
   ) : SmallInt;
   StdCall;



 TxValidate = Function
   (
     VAR tCount     : SmallInt;
     VAR sFieldName;
     tBufLen    : SmallInt;
     VAR bDataBuf
   ) : SmallInt;
   StdCall;


 TxMask = Function
   (
     iOption : SmallInt;
     iType   : SmallInt;
     tSize   : SmallInt;
     iDec    : SmallInt;
     VAR tLen    : SmallInt;
     VAR sMask
   ) : SmallInt;
   StdCall;


Var
  xGetSessionID                  : TxGetSessionID;
  xPutSessionID                  : TxPutSessionID;
  XQLCallback                    : TXQLCallback;
  xShareSessionID	         : TxShareSessionID;
  SQLGetCountDatabaseNames	 : TSQLGetCountDatabaseNames;
  SQLGetCountRemoteDatabaseNames : TSQLGetCountDatabaseNames;
  SQLGetDatabaseNames		 : TSQLGetDatabaseNames;
  SQLGetRemoteDatabaseNames	 : TSQLGetDatabaseNames;
  SQLUnloadDBNAMES		 : TSQLUnloadDBNAMES;
  XQLCursor			 : TXQLCursor;
  XQLCompile			 : TXQLCompile;
  XQLDescribe			 : TXQLDescribe;
  XQLExec			 : TXQLExec;
  XQLFetch			 : TXQLFetch;
  XQLFormat			 : TXQLFormat;
  XQLFree			 : TXQLFree;
  XQLLogin			 : TXQLLogin;
  XQLLogout			 : TXQLLogout;
  XQLSPUtility			 : TXQLSPUtility;
  XQLStatus			 : TXQLStatus;
  XQLStop			 : TXQLStop;
  XQLSubst			 : TXQLSubst;
  XQLVersion			 : TXQLVersion;
  XQLConvert			 : TXQLConvert;
  XQLValidate			 : TXQLValidate;
  XQLMask			 : TXQLMask;
  xCompute			 : TxCompute;
  xDescribe			 : TxDescribe;
  xMovefld			 : TxMovefld;
  xFetch			 : TxFetch;
  xField			 : TxField;
  xFree				 : TxFree;
  xInsert			 : TxInsert;
  xJoin				 : TxJoin;
  xLogin			 : TxLogin;
  xLogout			 : TxLogout;
  xNew				 : TxNew;
  xOrder			 : TxOrder;
  xRemall			 : TxRemall;
  xRemove			 : TxRemove;
  xReset			 : TxReset;
  xRestrict			 : TxRestrict;
  xStop				 : TxStop;
  xTrans			 : TxTrans;
  xStore			 : TxStore;
  xRecall			 : TxRecall;
  xUpdate			 : TxUpdate;
  xUpdall			 : TxUpdall;
  xDD				 : TxDD;
  xDDAttr			 : TxDDAttr;
  xDDModify			 : TxDDModify;
  xDDCreate			 : TxDDCreate;
  xDDDrop			 : TxDDDrop;
  xDDField			 : TxDDField;
  xDDFile			 : TxDDFile;
  xDDIndex			 : TxDDIndex;
  xDDPath			 : TxDDPath;
  xDDView			 : TxDDView;
  xAccess			 : TxAccess;
  xPassWord			 : TxPassWord;
  xSecurity			 : TxSecurity;
  xUser				 : TxUser;
  xChar				 : TxChar;
  xVersion			 : TxVersion;
  xStatus			 : TxStatus;
  xConvert			 : TxConvert;
  xValidate			 : TxValidate;
  xMask				 : TxMask;

Function SQLInit  : SmallInt;
Function SQLStop  : SmallInt;
Function IsSSQL40 : Boolean;

Function SQLInterfaceDLLLoaded : Boolean;
Function DBNamesInterfaceDLLLoaded : Boolean;

Implementation
Uses
  Forms,
  Dialogs,
  SysUtils,
  Windows,

  BseUtils,
  BtrConv;


Const
  SQLDLLNAME     = 'WSSQL32.DLL';
  DBNAMESDLLNAME = 'WDBNM32.DLL';

Const
  SQLLibraryHandle     : THandle = HINSTANCE_ERROR;
  DBNamesLibraryHandle : THandle = HINSTANCE_ERROR;

Type
  TFarProc = FARPROC;


Function SQLInterfaceDLLLoaded : Boolean;
begin
  Result := ( SQLLibraryHandle > HINSTANCE_ERROR );
end;


Function DBNamesInterfaceDLLLoaded : Boolean;
begin
  Result := ( DBNamesLibraryHandle > HINSTANCE_ERROR );
end;


Function SQLInit : SmallInt;
Const
  SQL_ERROR = 200; {The Scalable SQL local engine or Requester is not loaded.}
Var
  FatalError : Boolean;
  FCSect   : TRTLCriticalSection;

  Function GetSQLProc( ProcName : PChar ) : TFarProc;
  begin
    If Not FatalError then
    begin
      Result := GetProcAddress( SQLLibraryHandle, ProcName );
      If ( Result = NIL ) then
      begin
        FatalError := TRUE;
        MessageDlg( 'Error Loading Dynamic Function ' + StrPas( ProcName ) + ' from ' + SQLDLLNAME, mtError, [mbOk], 0 );
      end;
    end
    else
      Result := NIL;
  end;

  Function GetDBNamesProc( ProcName : PChar ) : TFarProc;
  begin
    If Not FatalError then
    begin
      Result := GetProcAddress( DBNamesLibraryHandle, ProcName );
      If ( Result = NIL ) then
      begin
        FatalError := TRUE;
        MessageDlg( 'Error Loading Dynamic Function ' + StrPas( ProcName ) + ' from ' + DBNAMESDLLNAME, mtError, [mbOk], 0 );
      end;
    end
    else
      Result := NIL;
  end;

begin
  Result     := SQL_ERROR;
  FatalError := FALSE;
  If Not SQLInterfaceDLLLoaded then
  begin
    InitializeCriticalSection( FCSect );
    Try
      EnterCriticalSection( FCSect );
      Try
        SQLLibraryHandle := LoadLibrary( SQLDLLNAME );
        If SQLInterfaceDLLLoaded then
        begin
          @xGetSessionID      := GetSQLProc( '_xGetSessionID'  );
          @xPutSessionID      := GetSQLProc( '_xPutSessionID'  );
          @XQLCallback        := GetSQLProc( '_XQLCallback'    );
          @xShareSessionID    := GetSQLProc( '_xShareSessionID');
          @XQLCursor	      := GetSQLProc( '_XQLCursor'      );
          @XQLCompile	      := GetSQLProc( '_XQLCompile'     );
          @XQLDescribe	      := GetSQLProc( 'XQLDescribe'     );
          @XQLExec	      := GetSQLProc( 'XQLExec'         );
          @XQLFetch	      := GetSQLProc( '_XQLFetch'       );
          @XQLFormat	      := GetSQLProc( 'XQLFormat'       );
          @XQLFree	      := GetSQLProc( '_XQLFree'        );
          @XQLLogin	      := GetSQLProc( '_XQLLogin'       );
          @XQLLogout	      := GetSQLProc( '_XQLLogout'      );
          @XQLSPUtility	      := GetSQLProc( 'XQLSPUtility'    );
          @XQLStatus	      := GetSQLProc( 'XQLStatus'       );
          @XQLStop	      := GetSQLProc( '_XQLStop'        );
          @XQLSubst	      := GetSQLProc( 'XQLSubst'	       );
          @XQLVersion	      := GetSQLProc( 'XQLVersion'      );
          @XQLConvert	      := GetSQLProc( 'XQLConvert'      );
          @XQLValidate	      := GetSQLProc( 'XQLValidate'     );
          @XQLMask	      := GetSQLProc( 'XQLMask'	       );
          @xCompute	      := GetSQLProc( 'xCompute'	       );
          @xDescribe	      := GetSQLProc( 'xDescribe'       );
          @xMovefld	      := GetSQLProc( 'xMovefld'	       );
          @xFetch	      := GetSQLProc( 'xFetch'	       );
          @xField	      := GetSQLProc( 'xField'	       );
          @xFree	      := GetSQLProc( 'xFree'	       );
          @xInsert	      := GetSQLProc( 'xInsert'	       );
          @xJoin	      := GetSQLProc( 'xJoin'	       );
          @xLogin	      := GetSQLProc( 'xLogin'	       );
          @xLogout	      := GetSQLProc( 'xLogout'	       );
          @xNew		      := GetSQLProc( 'xNew'	       );
          @xOrder	      := GetSQLProc( 'xOrder'	       );
          @xRemall	      := GetSQLProc( 'xRemall'	       );
          @xRemove	      := GetSQLProc( 'xRemove'	       );
          @xReset	      := GetSQLProc( 'xReset'	       );
          @xRestrict	      := GetSQLProc( 'xRestrict'       );
          @xStop	      := GetSQLProc( 'xStop'	       );
          @xTrans	      := GetSQLProc( 'xTrans'	       );
          @xStore	      := GetSQLProc( 'xStore'	       );
          @xRecall	      := GetSQLProc( 'xRecall'	       );
          @xUpdate	      := GetSQLProc( 'xUpdate'	       );
          @xUpdall	      := GetSQLProc( 'xUpdall'	       );
          @xDD		      := GetSQLProc( 'xDD'	       );
          @xDDAttr	      := GetSQLProc( 'xDDAttr'	       );
          @xDDModify	      := GetSQLProc( 'xDDModify'       );
          @xDDCreate	      := GetSQLProc( 'xDDCreate'       );
          @xDDDrop	      := GetSQLProc( 'xDDDrop'	       );
          @xDDField	      := GetSQLProc( 'xDDField'	       );
          @xDDFile	      := GetSQLProc( 'xDDFile'	       );
          @xDDIndex	      := GetSQLProc( 'xDDIndex'	       );
          @xDDPath	      := GetSQLProc( 'xDDPath'	       );
          @xDDView	      := GetSQLProc( 'xDDView'	       );
          @xAccess	      := GetSQLProc( 'xAccess'	       );
          @xPassWord	      := GetSQLProc( 'xPassword'       );
          @xSecurity	      := GetSQLProc( 'xSecurity'       );
          @xUser	      := GetSQLProc( 'xUser'	       );
          @xChar	      := GetSQLProc( 'xChar'	       );
          @xVersion	      := GetSQLProc( 'xVersion'	       );
          @xStatus	      := GetSQLProc( 'xStatus'	       );
          @xConvert	      := GetSQLProc( 'xConvert'	       );
          @xValidate	      := GetSQLProc( 'xValidate'       );
          @xMask	      := GetSQLProc( 'xMask'	       );
          Result              := 0;
        end
        else
          FatalError := TRUE;
        If Not FatalError and Not IGNORESQLDATABASES then
        begin
          If Not DBNamesInterfaceDLLLoaded then
          begin
            DBNamesLibraryHandle := LoadLibrary( DBNAMESDLLNAME );
            If DBNamesInterfaceDLLLoaded then
            begin
              @SQLGetCountDatabaseNames	      := GetDBNamesProc( 'SQLGetCountDatabaseNames'       );
              @SQLGetCountRemoteDatabaseNames := GetDBNamesProc( 'SQLGetCountRemoteDatabaseNames' );
              @SQLGetDatabaseNames	      := GetDBNamesProc( 'SQLGetDatabaseNames'            );
              @SQLGetRemoteDatabaseNames      := GetDBNamesProc( 'SQLGetRemoteDatabaseNames'      );
              @SQLUnloadDBNames               := GetDBNamesProc( 'SQLUnloadDBNames'               );
            end
            else
              IGNORESQLDATABASES := TRUE;
          end;
        end;
      Finally
        LeaveCriticalSection( FCSect );
      end;
    Finally
      DeleteCriticalSection( FCSect );
    end;
  end
  else
    Result := 0;
end;


Function SQLStop : SmallInt;
Var
  FCSect : TRTLCriticalSection;
begin
  Result := 0;
  InitializeCriticalSection( FCSect );
  Try
    EnterCriticalSection( FCSect );
    Try
      If SQLInterfaceDLLLoaded then
        FreeLibrary( SQLLibraryHandle );
      SQLLibraryHandle := HINSTANCE_ERROR;
      If DBNamesInterfaceDLLLoaded then
        FreeLibrary( DBNamesLibraryHandle );
      DBNamesLibraryHandle := HINSTANCE_ERROR;
    Finally
      LeaveCriticalSection( FCSect );
    end;
  Finally
    DeleteCriticalSection( FCSect );
  end;
end;


Function IsSSQL40 : Boolean;
Type
  TXQLVersionRec =
  packed Record
    Signature      : Array[1..4] of Char;
    BufferSize     : SmallInt;
    ioption        : SmallInt;
    iOptionParam   : Longint;
    VersionNumber  : SmallInt;
    RevisionNumber : SmallInt;
    UpdateNumber   : SmallInt;
    TargetCode     : Array[1..4] of Char;
  end;

Const
  Is40   : Boolean = FALSE;
  Tested : Boolean = FALSE;
var
  Buffer    : TXQLVersionRec;
  sReserved : Char;
begin
  If Not ( Is40 or Tested ) then
  begin
    Tested := TRUE;
    Result := SQLInterfaceDLLLoaded;
    If Not Result then
    begin
      SQLInit;
      Result := SQLInterfaceDLLLoaded;
      If Not Result then
        Exit;
    end;
    MoveZero( Buffer, SizeOf( Buffer ) );
    Buffer.Signature  := 'XVER';
    Buffer.BufferSize := SizeOf( Buffer );
    If ( XQLVersion( Buffer ) = 0 ) then
    begin
      If ( Buffer.VersionNumber = 4 ) then
      begin
        Is40 := TRUE;
        sReserved := #0;
        xReset( sReserved );
      end;
    end;
  end;
  Result := Is40;
end;



Initialization

end.

