(*****************************************************************************)
(* MODULE: BSESTMT^                                                          *)
(* RIGHTS: Reggatta Systems, Inc. - COPYRIGHT 1995 - 1998                    *)
(* DESCR.: This module contains the base statement class                     *)
(*****************************************************************************)
Unit BseStmt;
{$I BSE.INC}
{$I BAPI.INC}
Interface
Uses
  Classes,
  DB,
  Windows,
  BDE,

  BseBapi,
  BseColl,
  BseTypes,
  BseTable;

{...$DEFINE TABLE_LINKED}

Type
  TSqlStmt =
  Class( TBapiListItem )
    Protected
      FUnidirectional : Bool;
      Procedure SetSQLText( P : PChar ); Virtual;

    Public
      FSQLText  : PChar;
      FPropBits : Word;

      Property SQLText : PChar
        Read  FSQLText
        Write SetSqlText;

      Constructor Create( P : TObject; Container : TTitanCollection; SQL : PChar; PropBits : Word );
        Virtual;
      Destructor  Destroy; Override;

      Procedure   OpenStatement; Virtual;
      Procedure   CloseStatement; Virtual;

      Procedure   QSetParams (                              { Set query options }
                    uFldDescs     : Word;                   { Number of parameter field descriptors }
                    paFldDescs    : pFLDDesc;               { Array of parameter field descriptors }
                    pRecBuff      : Pointer                 { Record buffer }
                  ); Virtual; Abstract;

      Procedure   QExec (                                   { Execute prepared query }
                    phCur         : phDBICur                { Returned handle on result set }
                  ); Virtual; Abstract;

      Procedure   QSetProcParams (                          { Set procedure params }
                    uParamDescs   : Word;                   { Number of parameter descriptors }
                    paParamDescs  : pSPParamDesc;           { Array of parameter descriptors }
                    pRecBuff      : Pointer                 { Record buffer }
                  ); Virtual; Abstract;

      Procedure   QInstantiateAnswer (                      { Create answer table }
                    hCur          : hDBICur;                { Cursor Handle }
                    pszAnswerName : PChar;                  { Answer Table Name/NULL }
                    pszAnswerType : PChar;                  { Answer Table Type/NULL }
                    bOverWrite    : Bool;                   { Overwrite Flag }
                    phCur         : phDBICur                { cursor to instantiated table (output)(optional) }
                  ); Virtual; Abstract;

      Procedure   GetProp (                                 { Get property }
                    iProp         : Longint;                { Property to retrieve }
                    PropValue     : Pointer;                { == NULL, validate iProp for getting }
                    iMaxLen       : Word;                   { Length of buffer pPropValue }
                    var   iLen    : Word                    { Returns required length }
                  ); Override;

      Procedure   SetProp (                                 { Get property }
                    iProp         : Longint;                { Property to retrieve }
                    PropValue     : Longint                 { == NULL, validate iProp for getting }
                  ); Override;

      Procedure   ValidateProp (                            { Validate a property }
                    iProp         : Longint;                { property to validate }
                    bSetting      : Bool                    { TRUE:setting, FALSE:getting }
                  ); Override;
  end;


Implementation
Uses
  SysUtils,

  BseUtils,
  BseErrs;

Const
  BDE_FIELD_CHAR          = '_';
  BDE_PARAM_CHAR1         = ':';
  BDE_PARAM_CHAR2         = '?';
  BDE_CR                  = #13;
  BDE_LF                  = #10;


Constructor TSqlStmt.Create( P : TObject; Container : TTitanCollection;
              SQL : PChar; PropBits : Word );
begin
  Inherited Create( P, Container );
  SQLText   := SQL;
  FPropBits := PropBits;
  OpenStatement;
end;


Destructor TSqlStmt.Destroy;
begin
  CloseStatement;
  SQLText := NIL;
  Inherited Destroy;
end;


Procedure TSqlStmt.SetSQLText( P : PChar );
var
  i : Cardinal;
begin
  if Assigned( FSQLText ) then
    StrDispose( FSQLText );
  If ( P <> NIL ) then
  begin
    i := Succ( StrLen( P ) );
    FSqlText := StrAlloc( i );
    CharToOEMBuff( P, FSQLText, i );
  end  
  else
    FSQLText := NIL;
end;


Procedure TSqlStmt.OpenStatement;
begin
end;


Procedure TSqlStmt.CloseStatement;
begin
end;


Procedure TSqlStmt.GetProp (                                 { Get property }
           iProp         : Longint;                         { Property to retrieve }
           PropValue     : Pointer;                         { == NULL, validate iProp for getting }
           iMaxLen       : Word;                            { Length of buffer pPropValue }
           var   iLen    : Word                             { Returns required length }
         );
begin
  iLen := 0;
  Case TPropertyRec( iProp ).Prop of
    Word( stmtMAXPROPS ):                                   { ro UINT16      Number of defined properties }
      begin
        iLen := Sizeof( Word );
        Word( PropValue^ ) := maxstmtProps;
      end;
(*
    Word( stmtPARAMETERCOUNT ):                             { ro UINT16      Count of parameters }
    Word( stmtPARAMETERDESCS ):                             { ro aFLDDesc    Array of parameters }
*)
    Word( stmtLANGDRVNAME ):                                { ro pCHAR       Symbolic language drv. name }
      begin
        iLen := 1;
        PChar( PropValue^ ) := #0;
      end;
    Word( stmtUNIDIRECTIONAL ):                             { rw BOOL        Cursor Unidirectional }
      begin
        iLen := Sizeof( Bool );
        Bool( PropValue^ ) := FUnidirectional;
      end;
(*
    Word( stmtCURSORNAME ):                                 { rw pCHAR       Cursor name }
    Word( stmtNEWCONNECT ):                                 { rw BOOL        Stmt on new connection }
    Word( stmtNATIVEHNDL ):                                 { ro pBYTE       Native statement handle }
*)
  else
    Inherited GetProp( iProp, PropValue, iMaxLen, iLen );
  end;
end;


Procedure TSqlStmt.SetProp (                                 { Get property }
           iProp         : Longint;                         { Property to retrieve }
           PropValue     : Longint                          { == NULL, validate iProp for getting }
         );
begin
  Case TPropertyRec( iProp ).Prop of
    Word( stmtUNIDIRECTIONAL ):                             { rw BOOL        Cursor Unidirectional }
      FUnidirectional := Bool( PropValue );
(*
    Word( stmtCURSORNAME ):                                 { rw pCHAR       Cursor name }
    Word( stmtNEWCONNECT ):                                 { rw BOOL        Stmt on new connection }
*)
  else
    Inherited SetProp( iProp, PropValue );
  end;
end;


Procedure TSqlStmt.ValidateProp (                            { Validate a property }
           iProp         : Longint;                         { property to validate }
           bSetting      : Bool                             { TRUE:setting, FALSE:getting }
         );
begin
  Case TPropertyRec( iProp ).Prop of
    Word( stmtMAXPROPS ),                                   { ro UINT16      Number of defined properties }
    Word( stmtPARAMETERCOUNT ),                             { ro UINT16      Count of parameters }
    Word( stmtPARAMETERDESCS ),                             { ro aFLDDesc    Array of parameters }
    Word( stmtLANGDRVNAME ),                                { ro pCHAR       Symbolic language drv. name }
    Word( stmtNATIVEHNDL ):                                 { ro pBYTE       Native statement handle }
      If bSetting then
        Raise ETitanEngineError.Create( DBIERR_NOTSUPPORTED );
  else
    Inherited ValidateProp( iProp, bSetting );
  end;
end;


end.
