ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/branches/udr/client/FBAttachment.pas
(Generate patch)

Comparing:
ibx/branches/journaling/fbintf/client/FBAttachment.pas (file contents), Revision 362 by tony, Tue Dec 7 13:27:39 2021 UTC vs.
ibx/branches/udr/client/FBAttachment.pas (file contents), Revision 387 by tony, Wed Jan 19 13:34:42 2022 UTC

# Line 16 | Line 16
16   *
17   *  The Initial Developer of the Original Code is Tony Whyman.
18   *
19 < *  The Original Code is (C) 2016 Tony Whyman, MWA Software
19 > *  The Original Code is (C) 2016-2021 Tony Whyman, MWA Software
20   *  (http://www.mwasoftware.co.uk).
21   *
22   *  All Rights Reserved.
# Line 39 | Line 39 | interface
39  
40   uses
41    Classes, SysUtils, {$IFDEF WINDOWS} windows, {$ENDIF} IB,  FBParamBlock,
42 <  FBActivityMonitor, FBClientAPI;
42 >  FBActivityMonitor, FBClientAPI, IBUtils;
43  
44   const
45    DefaultMaxInlineBlobLimit = 8192;
# Line 53 | Line 53 | type
53      AllowReverseLookup: boolean; {used to ensure that lookup of CP_UTF* does not return UNICODE_FSS}
54    end;
55  
56 +  { Database Journalling.
57 +
58 +    This class is intended to support a client side journal of all database
59 +    updates, inserts and deletes made by the client during a session. It also records
60 +    the transaction each update was made under.
61 +
62 +    The database schema is required to include a control table "IBX$JOURNALS" and
63 +    an SQL Sequence IBX$SESSIONS. These are created by the class when the
64 +    database is opened, if they are not already present. However, it is recommended
65 +    that they are created as an orginal part of the database schema in order to
66 +    unnecessarily avoid each user being given sufficient priviledge to create tables
67 +    and Sequences.
68 +
69 +    Syntax:
70 +
71 +    Transaction Start:
72 +    *S:<date/time>,<attachmentid>,<session id>,<transaction no.>,<string length>:<transaction Name>,<string length>:<TPB>,<default Completion>
73 +
74 +    Transaction Commit:
75 +    *C:<date/time>,<attachmentid>,<session id>,<transaction no.>
76 +
77 +    Transaction Commit retaining :
78 +    *c:<date/time>,<attachmentid>,<session id>,<transaction no.><old transaction no.>
79 +
80 +    Transaction Rollback:
81 +    *R:<date/time>,<attachmentid>,<session id>,<transaction no.>
82 +
83 +    Transaction Rollback retaining:
84 +    *r:<date/time>,<attachmentid>,<session id>,<transaction no.><old transaction no.>
85 +
86 +    Update/Insert/Delete
87 +    *Q:<date/time>,<attachmentid>,<session id>,<transaction no.>,<length of query text in bytes>:<query text>
88 +
89 +  }
90 +
91 +  { TFBJournaling }
92 +
93 +  TFBJournaling = class(TActivityHandler, IJournallingHook)
94 +  private
95 +    {Logfile}
96 +    const sQueryJournal          = '*Q:''%s'',%d,%d,%d,%d:%s' + LineEnding;
97 +    const sTransStartJnl         = '*S:''%s'',%d,%d,%d,%d:%s,%d:%s,%d' + LineEnding;
98 +    const sTransCommitJnl        = '*C:''%s'',%d,%d,%d' + LineEnding;
99 +    const sTransCommitFailJnl    = '*F:''%s'',%d,%d,%d' + LineEnding;
100 +    const sTransCommitRetJnl     = '*c:''%s'',%d,%d,%d,%d' + LineEnding;
101 +    const sTransRollBackJnl      = '*R:''%s'',%d,%d,%d' + LineEnding;
102 +    const sTransRollBackFailJnl  = '*f:''%s'',%d,%d,%d' + LineEnding;
103 +    const sTransRollBackRetJnl   = '*r:''%s'',%d,%d,%d,%d' + LineEnding;
104 +  private
105 +    FOptions: TJournalOptions;
106 +    FJournalFilePath: string;
107 +    FJournalFileStream: TStream;
108 +    FSessionID: integer;
109 +    FDoNotJournal: boolean;
110 +    function GetDateTimeFmt: AnsiString;
111 +  protected
112 +    procedure EndSession(RetainJournal: boolean);
113 +    function GetAttachment: IAttachment; virtual; abstract;
114 +  public
115 +    {IAttachment}
116 +    procedure Disconnect(Force: boolean=false); virtual;
117 +  public
118 +    {IJournallingHook}
119 +    procedure TransactionStart(Tr: ITransaction);
120 +    function TransactionEnd( TransactionID: integer; Completion: TTrCompletionState): boolean;
121 +    procedure TransactionRetained(Tr: ITransaction; OldTransactionID: integer;
122 +      Action: TTransactionAction);
123 +    procedure ExecQuery(Stmt: IStatement);
124 +    procedure ExecImmediateJnl(sql: AnsiString; tr: ITransaction);
125 +  public
126 +    {Client side Journaling}
127 +    function JournalingActive: boolean;
128 +    function GetJournalOptions: TJournalOptions;
129 +    function StartJournaling(aJournalLogFile: AnsiString): integer; overload;
130 +    function StartJournaling(aJournalLogFile: AnsiString; Options: TJournalOptions): integer; overload;
131 +    function StartJournaling(S: TStream; Options: TJournalOptions): integer; overload;
132 +    procedure StopJournaling(RetainJournal: boolean);
133 +  end;
134 +
135    { TFBAttachment }
136  
137 <  TFBAttachment = class(TActivityHandler)
137 >  TFBAttachment = class(TFBJournaling)
138    private
139      FDPB: IDPB;
140      FFirebirdAPI: IFirebirdAPI;
# Line 64 | Line 143 | type
143      FUserCharSetMap: array of TCharSetMap;
144      FSecDatabase: AnsiString;
145      FInlineBlobLimit: integer;
146 <  protected
68 <    FDatabaseName: AnsiString;
69 <    FRaiseExceptionOnConnectError: boolean;
146 >    FAttachmentID: integer;
147      FSQLDialect: integer;
148      FHasDefaultCharSet: boolean;
149      FCharSetID: integer;
150      FCodePage: TSystemCodePage;
151      FRemoteProtocol: AnsiString;
152      FAuthMethod: AnsiString;
153 +    FHasConnectionInfo: boolean;
154 +    procedure NeedDBInfo;
155 +    procedure NeedConnectionInfo;
156 +  protected
157 +    FDatabaseName: AnsiString;
158 +    FRaiseExceptionOnConnectError: boolean;
159      constructor Create(api: TFBClientAPI; DatabaseName: AnsiString; DPB: IDPB;
160        RaiseExceptionOnConnectError: boolean);
161      procedure CheckHandle; virtual; abstract;
162 +    procedure ClearCachedInfo;
163      function GenerateCreateDatabaseSQL(DatabaseName: AnsiString; aDPB: IDPB): AnsiString;
80    procedure GetODSAndConnectionInfo;
164      function GetDBInfo(ReqBuffer: PByte; ReqBufLen: integer): IDBInformation; virtual; abstract;
165      function IsConnected: boolean; virtual; abstract;
166      procedure EndAllTransactions;
167      procedure DPBFromCreateSQL(CreateSQL: AnsiString);
168      procedure SetParameters(SQLParams: ISQLParams; params: array of const);
169 +    procedure SetSQLDialect(aValue: integer);
170      procedure UseServerICUChanged; virtual;
171    public
172      destructor Destroy; override;
173 +    procedure Disconnect(Force: boolean); override;
174      function getFirebirdAPI: IFirebirdAPI;
175      function getDPB: IDPB;
176      function AllocateBPB: IBPB;
177      function AllocateDIRB: IDIRB;
178 <    function StartTransaction(TPB: array of byte; DefaultCompletion: TTransactionCompletion): ITransaction; overload; virtual; abstract;
179 <    function StartTransaction(TPB: ITPB; DefaultCompletion: TTransactionCompletion): ITransaction; overload; virtual; abstract;
180 <    procedure Disconnect(Force: boolean=false); virtual; abstract;
178 >    function StartTransaction(TPB: array of byte;
179 >      DefaultCompletion: TTransactionCompletion;
180 >      aName: AnsiString=''): ITransaction; overload; virtual; abstract;
181 >    function StartTransaction(TPB: ITPB; DefaultCompletion: TTransactionCompletion;
182 >      aName: AnsiString=''): ITransaction; overload; virtual; abstract;
183      procedure ExecImmediate(transaction: ITransaction; sql: AnsiString; aSQLDialect: integer); overload; virtual; abstract;
184      procedure ExecImmediate(TPB: array of byte; sql: AnsiString; aSQLDialect: integer); overload;
185      procedure ExecImmediate(transaction: ITransaction; sql: AnsiString); overload;
# Line 140 | Line 227 | type
227      function GetEventHandler(Event: AnsiString): IEvents; overload;
228  
229      function GetSQLDialect: integer;
230 +    function GetAttachmentID: integer;
231      function CreateBlob(transaction: ITransaction; RelationName, ColumnName: AnsiString; BPB: IBPB=nil): IBlob; overload;
232      function CreateBlob(transaction: ITransaction; BlobMetaData: IBlobMetaData; BPB: IBPB=nil): IBlob; overload; virtual; abstract;
233      function OpenBlob(transaction: ITransaction; BlobMetaData: IBlobMetaData; BlobID: TISC_QUAD; BPB: IBPB=nil): IBlob; overload; virtual; abstract;
# Line 150 | Line 238 | type
238      function CreateArray(transaction: ITransaction; ArrayMetaData: IArrayMetaData): IArray; overload; virtual; abstract;
239      function OpenArray(transaction: ITransaction; RelationName, ColumnName: AnsiString; ArrayID: TISC_QUAD): IArray; overload;
240      function OpenArray(transaction: ITransaction; ArrayMetaData: IArrayMetaData; ArrayID: TISC_QUAD): IArray; overload; virtual; abstract;
241 <    property SQLDialect: integer read FSQLDialect;
241 >    property SQLDialect: integer read GetSQLDialect;
242      property DPB: IDPB read FDPB;
243    public
244      function GetDBInformation(Requests: array of byte): IDBInformation; overload;
# Line 162 | Line 250 | type
250      function GetSecurityDatabase: AnsiString;
251      function GetODSMajorVersion: integer;
252      function GetODSMinorVersion: integer;
253 +    function GetCharSetID: integer;
254      function HasDecFloatSupport: boolean; virtual;
255      function GetInlineBlobLimit: integer;
256      procedure SetInlineBlobLimit(limit: integer);
257      function HasBatchMode: boolean; virtual;
258 +    function HasTable(aTableName: AnsiString): boolean;
259 +    function HasFunction(aFunctionName: AnsiString): boolean;
260 +    function HasProcedure(aProcName: AnsiString): boolean;
261  
262    public
263      {Character Sets}
# Line 180 | Line 272 | type
272        AllowReverseLookup:boolean; out CharSetID: integer);
273      function GetBlobMetaData(Transaction: ITransaction; tableName, columnName: AnsiString): IBlobMetaData; virtual; abstract;
274      function GetArrayMetaData(Transaction: ITransaction; tableName, columnName: AnsiString): IArrayMetaData; virtual; abstract;
275 <    property CharSetID: integer read FCharSetID;
275 >    property CharSetID: integer read GetCharSetID;
276      property CodePage: TSystemCodePage read FCodePage;
277  
278    public
# Line 214 | Line 306 | type
306  
307   implementation
308  
309 < uses FBMessages, IBUtils, FBTransaction {$IFDEF HASREQEX}, RegExpr{$ENDIF};
309 > uses FBMessages, IBErrorCodes, FBTransaction {$IFDEF HASREQEX}, RegExpr{$ENDIF};
310 >
311 > const
312 >  {Journaling}
313 >  sJournalTableName = 'IBX$JOURNALS';
314 >  sSequenceName = 'IBX$SESSIONS';
315 >
316 >  sqlCreateJournalTable =
317 >    'Create Table ' + sJournalTableName + '(' +
318 >    '  IBX$SessionID Integer not null, '+
319 >    '  IBX$TransactionID Integer not null, '+
320 >    '  IBX$OldTransactionID Integer, '+
321 >    '  IBX$USER VarChar(32) Default CURRENT_USER, '+
322 >    '  IBX$CREATED TIMESTAMP Default CURRENT_TIMESTAMP, '+
323 >    '  Primary Key(IBX$SessionID,IBX$TransactionID)' +
324 >    ')';
325 >
326 >  sqlCreateSequence = 'CREATE SEQUENCE ' + sSequenceName;
327 >
328 >  sqlGetNextSessionID = 'Select Gen_ID(' + sSequenceName + ',1) as SessionID From RDB$DATABASE';
329 >
330 >  sqlRecordJournalEntry = 'Insert into ' + sJournalTableName + '(IBX$SessionID,IBX$TransactionID,IBX$OldTransactionID) '+
331 >                        'Values(?,?,?)';
332 >
333 >  sqlCleanUpSession = 'Delete From ' + sJournalTableName + ' Where IBX$SessionID = ?';
334  
335   const
336    CharSetMap: array [0..69] of TCharsetMap = (
# Line 370 | Line 486 | const
486      'process_name',
487      'trusted_role',
488      'org_filename',
489 <    'utf8_ilename',
489 >    'utf8_filename',
490      'ext_call_depth',
491      'auth_block',
492      'client_version',
# Line 391 | Line 507 | const
507      'decfloat_traps'
508      );
509  
510 + type
511 +
512 +  { TQueryProcessor }
513 +
514 +  TQueryProcessor=class(TSQLTokeniser)
515 +  private
516 +    FInString: AnsiString;
517 +    FIndex: integer;
518 +    FStmt: IStatement;
519 +    function DoExecute: AnsiString;
520 +    function GetParamValue(ParamIndex: integer): AnsiString;
521 +  protected
522 +    function GetChar: AnsiChar; override;
523 +  public
524 +    class function Execute(Stmt: IStatement): AnsiString;
525 +  end;
526 +
527 +  { TQueryProcessor }
528 +
529 + function TQueryProcessor.DoExecute: AnsiString;
530 + var token: TSQLTokens;
531 +    ParamIndex: integer;
532 + begin
533 +  Result := '';
534 +  ParamIndex := 0;
535 +
536 +  while not EOF do
537 +  begin
538 +    token := GetNextToken;
539 +    case token of
540 +    sqltPlaceHolder:
541 +      begin
542 +        Result := Result + GetParamValue(ParamIndex);
543 +        Inc(ParamIndex);
544 +      end;
545 +    else
546 +      Result := Result + TokenText;
547 +    end;
548 +  end;
549 + end;
550 +
551 + function TQueryProcessor.GetParamValue(ParamIndex: integer): AnsiString;
552 + begin
553 +  with FStmt.SQLParams[ParamIndex] do
554 +  begin
555 +    if IsNull then
556 +      Result := 'NULL'
557 +    else
558 +    case GetSQLType of
559 +    SQL_BLOB:
560 +      if getSubType = 1 then {string}
561 +        Result := '''' + SQLSafeString(GetAsString) + ''''
562 +      else
563 +        Result := TSQLXMLReader.FormatBlob(GetAsString,getSubType);
564 +
565 +    SQL_ARRAY:
566 +        Result := TSQLXMLReader.FormatArray(getAsArray);
567 +
568 +    SQL_VARYING,
569 +    SQL_TEXT,
570 +    SQL_TIMESTAMP,
571 +    SQL_TYPE_DATE,
572 +    SQL_TYPE_TIME,
573 +    SQL_TIMESTAMP_TZ_EX,
574 +    SQL_TIME_TZ_EX,
575 +    SQL_TIMESTAMP_TZ,
576 +    SQL_TIME_TZ:
577 +      Result := '''' + SQLSafeString(GetAsString) + '''';
578 +    else
579 +      Result := GetAsString;
580 +    end;
581 +  end;
582 + end;
583 +
584 + function TQueryProcessor.GetChar: AnsiChar;
585 + begin
586 +  if FIndex <= Length(FInString) then
587 +  begin
588 +    Result := FInString[FIndex];
589 +    Inc(FIndex);
590 +  end
591 +  else
592 +    Result := #0;
593 + end;
594 +
595 + class function TQueryProcessor.Execute(Stmt: IStatement): AnsiString;
596 + begin
597 +  if not Stmt.IsPrepared then
598 +    IBError(ibxeSQLClosed,[]);
599 +  with self.Create do
600 +  try
601 +    FStmt := Stmt;
602 +    FInString := Stmt.GetProcessedSQLText;
603 +    FIndex := 1;
604 +    Result := Trim(DoExecute);
605 +  finally
606 +    Free;
607 +  end;
608 + end;
609 +
610 + { TFBJournaling }
611 +
612 + function TFBJournaling.GetDateTimeFmt: AnsiString;
613 + begin
614 +  {$IF declared(DefaultFormatSettings)}
615 +  with DefaultFormatSettings do
616 +  {$ELSE}
617 +  {$IF declared(FormatSettings)}
618 +  with FormatSettings do
619 +  {$IFEND}
620 +  {$IFEND}
621 +  Result := ShortDateFormat + ' ' + LongTimeFormat + '.zzzz'
622 + end;
623 +
624 + procedure TFBJournaling.EndSession(RetainJournal: boolean);
625 + begin
626 +  if JournalingActive and (FJournalFilePath <> '') then
627 +  begin
628 +    FreeAndNil(FJournalFileStream);
629 +    if not (joNoServerTable in FOptions) and not RetainJournal then
630 +    try
631 +        GetAttachment.ExecuteSQL([isc_tpb_write,isc_tpb_wait,isc_tpb_consistency],
632 +             sqlCleanUpSession,[FSessionID]);
633 +        sysutils.DeleteFile(FJournalFilePath);
634 +    except On E: EIBInterBaseError do
635 +      if E.IBErrorCode <> isc_lost_db_connection then
636 +        raise;
637 +      {ignore - do not delete journal if database gone away}
638 +    end;
639 +    FSessionID := -1;
640 +  end;
641 + end;
642 +
643 + procedure TFBJournaling.Disconnect(Force: boolean);
644 + begin
645 +  if JournalingActive then
646 +    EndSession(Force);
647 + end;
648 +
649 + procedure TFBJournaling.TransactionStart(Tr: ITransaction);
650 + var LogEntry: AnsiString;
651 +    TPBText: AnsiString;
652 + begin
653 +  if not (joNoServerTable in FOptions) then
654 +  try
655 +    FDoNotJournal := true;
656 +    GetAttachment.ExecuteSQL(Tr,sqlRecordJournalEntry,[FSessionID,Tr.GetTransactionID,NULL]);
657 +  finally
658 +    FDoNotJournal := false;
659 +  end;
660 +  TPBText := Tr.getTPB.AsText;
661 +  LogEntry := Format(sTransStartJnl,[FBFormatDateTime(GetDateTimeFmt,Now),
662 +                                     GetAttachment.GetAttachmentID,
663 +                                     FSessionID,
664 +                                     Tr.GetTransactionID,
665 +                                     Length(Tr.TransactionName),
666 +                                     Tr.TransactionName,
667 +                                     Length(TPBText),TPBText,
668 +                                     ord(tr.GetDefaultCompletion)]);
669 +  if assigned(FJournalFileStream) then
670 +    FJournalFileStream.Write(LogEntry[1],Length(LogEntry));
671 + end;
672 +
673 + function TFBJournaling.TransactionEnd(TransactionID: integer;
674 +  Completion: TTrCompletionState): boolean;
675 +
676 + var LogEntry: AnsiString;
677 + begin
678 +  Result := false;
679 +    case Completion of
680 +    trRolledback:
681 +      begin
682 +        LogEntry := Format(sTransRollbackJnl,[FBFormatDateTime(GetDateTimeFmt,Now),
683 +                                              GetAttachment.GetAttachmentID,
684 +                                              FSessionID,TransactionID]);
685 +        Result := true;
686 +      end;
687 +
688 +    trRollbackFailed:
689 +      begin
690 +        LogEntry := Format(sTransRollbackFailJnl,[FBFormatDateTime(GetDateTimeFmt,Now),
691 +                                              GetAttachment.GetAttachmentID,
692 +                                              FSessionID,TransactionID]);
693 +        Result := true;
694 +      end;
695 +
696 +    trCommitted:
697 +      begin
698 +        LogEntry := Format(sTransCommitJnl,[FBFormatDateTime(GetDateTimeFmt,Now),
699 +                                            GetAttachment.GetAttachmentID,
700 +                                            FSessionID,TransactionID]);
701 +        Result := true;
702 +      end;
703 +
704 +    trCommitFailed:
705 +      begin
706 +        LogEntry := Format(sTransCommitFailJnl,[FBFormatDateTime(GetDateTimeFmt,Now),
707 +                                            GetAttachment.GetAttachmentID,
708 +                                            FSessionID,TransactionID]);
709 +        Result := true;
710 +      end;
711 +    end;
712 +    if assigned(FJournalFileStream) then
713 +      FJournalFileStream.Write(LogEntry[1],Length(LogEntry));
714 + end;
715 +
716 + procedure TFBJournaling.TransactionRetained(Tr: ITransaction;
717 +  OldTransactionID: integer; Action: TTransactionAction);
718 + var LogEntry: AnsiString;
719 + begin
720 +    case Action of
721 +      TACommitRetaining:
722 +          LogEntry := Format(sTransCommitRetJnl,[FBFormatDateTime(GetDateTimeFmt,Now),
723 +                                  GetAttachment.GetAttachmentID,
724 +                                  FSessionID,Tr.GetTransactionID,OldTransactionID]);
725 +      TARollbackRetaining:
726 +          LogEntry := Format(sTransRollbackRetJnl,[FBFormatDateTime(GetDateTimeFmt,Now),
727 +                                      GetAttachment.GetAttachmentID,
728 +                                      FSessionID,Tr.GetTransactionID,OldTransactionID]);
729 +    end;
730 +    if assigned(FJournalFileStream) then
731 +      FJournalFileStream.Write(LogEntry[1],Length(LogEntry));
732 +
733 +    if not (joNoServerTable in FOptions) then
734 +    try
735 +      FDoNotJournal := true;
736 +      GetAttachment.ExecuteSQL(Tr,sqlRecordJournalEntry,[FSessionID,Tr.GetTransactionID,OldTransactionID]);
737 +    finally
738 +      FDoNotJournal := false;
739 +   end;
740 + end;
741 +
742 + procedure TFBJournaling.ExecQuery(Stmt: IStatement);
743 + var SQL: AnsiString;
744 +    LogEntry: AnsiString;
745 + begin
746 +  SQL := TQueryProcessor.Execute(Stmt);
747 +  LogEntry := Format(sQueryJournal,[FBFormatDateTime(GetDateTimeFmt,Now),
748 +                                      GetAttachment.GetAttachmentID,
749 +                                      FSessionID,
750 +                                      Stmt.GetTransaction.GetTransactionID,
751 +                                      Length(SQL),SQL]);
752 +  if assigned(FJournalFileStream) then
753 +    FJournalFileStream.Write(LogEntry[1],Length(LogEntry));
754 + end;
755 +
756 + procedure TFBJournaling.ExecImmediateJnl(sql: AnsiString; tr: ITransaction);
757 + var LogEntry: AnsiString;
758 + begin
759 +  LogEntry := Format(sQueryJournal,[FBFormatDateTime(GetDateTimeFmt,Now),
760 +                                      GetAttachment.GetAttachmentID,
761 +                                      FSessionID,
762 +                                      tr.GetTransactionID,
763 +                                      Length(sql),sql]);
764 +  if assigned(FJournalFileStream) then
765 +    FJournalFileStream.Write(LogEntry[1],Length(LogEntry));
766 + end;
767 +
768 + function TFBJournaling.JournalingActive: boolean;
769 + begin
770 +  Result := (FJournalFileStream <> nil) and not FDoNotJournal;
771 + end;
772 +
773 + function TFBJournaling.GetJournalOptions: TJournalOptions;
774 + begin
775 +  Result := FOptions;
776 + end;
777 +
778 + function TFBJournaling.StartJournaling(aJournalLogFile: AnsiString): integer;
779 + begin
780 +  Result := StartJournaling(aJournalLogFile,[joReadWriteTransactions,joModifyQueries]);
781 + end;
782 +
783 + function TFBJournaling.StartJournaling(aJournalLogFile: AnsiString;
784 +  Options: TJournalOptions): integer;
785 + begin
786 +  try
787 +    StartJournaling(TFileStream.Create(aJournalLogFile,fmCreate),Options);
788 +  finally
789 +    FJournalFilePath := aJournalLogFile;
790 +  end;
791 + end;
792 +
793 + function TFBJournaling.StartJournaling(S: TStream; Options: TJournalOptions
794 +  ): integer;
795 + begin
796 +  FOptions := Options;
797 +  if not (joNoServerTable in FOptions) then
798 +  with GetAttachment do
799 +  begin
800 +    if  not HasTable(sJournalTableName) then
801 +    begin
802 +      ExecImmediate([isc_tpb_write,isc_tpb_wait,isc_tpb_consistency],sqlCreateJournalTable);
803 +      ExecImmediate([isc_tpb_write,isc_tpb_wait,isc_tpb_consistency],sqlCreateSequence);
804 +    end;
805 +    FSessionID := OpenCursorAtStart(sqlGetNextSessionID)[0].AsInteger;
806 +  end;
807 +  FJournalFileStream := S;
808 +  Result := FSessionID;
809 + end;
810  
811 + procedure TFBJournaling.StopJournaling(RetainJournal: boolean);
812 + begin
813 +  EndSession(RetainJournal);
814 + end;
815  
816   { TFBAttachment }
817  
818 < procedure TFBAttachment.GetODSAndConnectionInfo;
819 < var DBInfo: IDBInformation;
400 <    i: integer;
401 <    Stmt: IStatement;
818 > procedure TFBAttachment.NeedConnectionInfo;
819 > var Stmt: IStatement;
820      ResultSet: IResultSet;
821      Param: IDPBItem;
822   begin
823 <  if not IsConnected then Exit;
824 <  DBInfo := GetDBInformation([isc_info_db_id,isc_info_ods_version,isc_info_ods_minor_version,
407 <                               isc_info_db_SQL_Dialect]);
408 <  for i := 0 to DBInfo.GetCount - 1 do
409 <    with DBInfo[i] do
410 <      case getItemType of
411 <      isc_info_ods_minor_version:
412 <        FODSMinorVersion := getAsInteger;
413 <      isc_info_ods_version:
414 <        FODSMajorVersion := getAsInteger;
415 <      isc_info_db_SQL_Dialect:
416 <        FSQLDialect := getAsInteger;
417 <      end;
418 <
823 >  if not IsConnected or FHasConnectionInfo then Exit;
824 >  NeedDBInfo;
825    FCharSetID := 0;
826    FRemoteProtocol := '';
827    FAuthMethod := 'Legacy_Auth';
# Line 461 | Line 867 | begin
867      end;
868    end;
869    FHasDefaultCharSet := CharSetID2CodePage(FCharSetID,FCodePage) and (FCharSetID > 1);
870 +  FHasConnectionInfo := true;
871 + end;
872 +
873 + procedure TFBAttachment.NeedDBInfo;
874 + var DBInfo: IDBInformation;
875 +    i: integer;
876 + begin
877 +  if not IsConnected or (FAttachmentID > 0) then Exit;
878 +  DBInfo := GetDBInformation([isc_info_db_id,isc_info_ods_version,isc_info_ods_minor_version,
879 +                               isc_info_db_SQL_Dialect, isc_info_attachment_id]);
880 +  for i := 0 to DBInfo.GetCount - 1 do
881 +    with DBInfo[i] do
882 +      case getItemType of
883 +      isc_info_ods_minor_version:
884 +        FODSMinorVersion := getAsInteger;
885 +      isc_info_ods_version:
886 +        FODSMajorVersion := getAsInteger;
887 +      isc_info_db_SQL_Dialect:
888 +        FSQLDialect := getAsInteger;
889 +      isc_info_attachment_id:
890 +        FAttachmentID := getAsInteger;
891 +      end;
892   end;
893  
894   constructor TFBAttachment.Create(api: TFBClientAPI; DatabaseName: AnsiString;
# Line 470 | Line 898 | begin
898    FFirebirdAPI := api.GetAPI; {Keep reference to interface}
899    FSQLDialect := 3;
900    FDatabaseName := DatabaseName;
473  FDPB := DPB;
901    SetLength(FUserCharSetMap,0);
902 +  ClearCachedInfo;
903 +  FInlineBlobLimit := DefaultMaxInlineBlobLimit;
904 +  FDPB := DPB;
905    FRaiseExceptionOnConnectError := RaiseExceptionOnConnectError;
906 + end;
907 +
908 + procedure TFBAttachment.ClearCachedInfo;
909 + begin
910 +  FHasDefaultCharSet := false;
911 +  FAttachmentID := 0;
912    FODSMajorVersion := 0;
913    FODSMinorVersion := 0;
914 <  FInlineBlobLimit := DefaultMaxInlineBlobLimit;
914 >  FCodePage := CP_NONE;
915 >  FCharSetID := 0;
916 >  FRemoteProtocol := '';
917 >  FAuthMethod := '';
918 >  FSecDatabase := '';
919 >  FHasConnectionInfo := false;
920   end;
921  
922   function TFBAttachment.GenerateCreateDatabaseSQL(DatabaseName: AnsiString;  aDPB: IDPB): AnsiString;
# Line 609 | Line 1050 | begin
1050    end;
1051   end;
1052  
1053 + procedure TFBAttachment.SetSQLDialect(aValue: integer);
1054 + begin
1055 +  FSQLDialect := aValue;
1056 + end;
1057 +
1058   procedure TFBAttachment.UseServerICUChanged;
1059   begin
1060    // Do nothing by default
# Line 620 | Line 1066 | begin
1066    inherited Destroy;
1067   end;
1068  
1069 + procedure TFBAttachment.Disconnect(Force: boolean);
1070 + begin
1071 +  inherited Disconnect(Force);
1072 +  ClearCachedInfo;
1073 + end;
1074 +
1075   function TFBAttachment.getFirebirdAPI: IFirebirdAPI;
1076   begin
1077    Result := FFirebirdAPI;
# Line 642 | Line 1094 | end;
1094  
1095   procedure TFBAttachment.ExecImmediate(TPB: array of byte; sql: AnsiString;
1096    aSQLDialect: integer);
1097 + var tr: ITransaction;
1098   begin
1099 <  ExecImmediate(StartTransaction(TPB,taCommit),sql,aSQLDialect);
1099 >  tr := StartTransaction(TPB,taCommit);
1100 >  try
1101 >    ExecImmediate(tr,sql,aSQLDialect);
1102 >  except
1103 >    tr.Rollback(true);
1104 >    raise;
1105 >  end;
1106   end;
1107  
1108   procedure TFBAttachment.ExecImmediate(transaction: ITransaction; sql: AnsiString);
# Line 653 | Line 1112 | end;
1112  
1113   procedure TFBAttachment.ExecImmediate(TPB: array of byte; sql: AnsiString);
1114   begin
1115 <  ExecImmediate(StartTransaction(TPB,taCommit),sql,FSQLDialect);
1115 >  ExecImmediate(TPB,sql,FSQLDialect);
1116   end;
1117  
1118   function TFBAttachment.ExecuteSQL(TPB: array of byte; sql: AnsiString;
1119    SQLDialect: integer; params: array of const): IResults;
1120 + var tr: ITransaction;
1121   begin
1122 <  Result := ExecuteSQL(StartTransaction(TPB,taCommit),sql,SQLDialect,params);
1122 >  tr := StartTransaction(TPB,taCommit);
1123 >  try
1124 >    Result := ExecuteSQL(tr,sql,SQLDialect,params);
1125 >  except
1126 >    tr.Rollback(true);
1127 >    raise;
1128 >  end;
1129   end;
1130  
1131   function TFBAttachment.ExecuteSQL(transaction: ITransaction; sql: AnsiString;
# Line 675 | Line 1141 | end;
1141   function TFBAttachment.ExecuteSQL(TPB: array of byte; sql: AnsiString;
1142    params: array of const): IResults;
1143   begin
1144 <   Result := ExecuteSQL(StartTransaction(TPB,taCommit),sql,params);
1144 >   Result := ExecuteSQL(TPB,sql,FSQLDialect,params);
1145   end;
1146  
1147   function TFBAttachment.ExecuteSQL(transaction: ITransaction; sql: AnsiString;
1148    params: array of const): IResults;
1149   begin
1150 <  with Prepare(transaction,sql,FSQLDialect) do
685 <  begin
686 <    SetParameters(SQLParams,params);
687 <    Result := Execute;
688 <  end;
1150 >  Result := ExecuteSQL(transaction,sql,FSQLDialect,params);
1151   end;
1152  
1153   function TFBAttachment.OpenCursor(transaction: ITransaction; sql: AnsiString;
# Line 778 | Line 1240 | end;
1240  
1241   function TFBAttachment.OpenCursorAtStart(sql: AnsiString; Scrollable: boolean;
1242    params: array of const): IResultSet;
1243 + var tr: ITransaction;
1244   begin
1245 <  Result := OpenCursorAtStart(StartTransaction([isc_tpb_read,isc_tpb_wait,isc_tpb_concurrency],taCommit),sql,FSQLDialect,
1246 <                   Scrollable,params);
1245 >  tr := StartTransaction([isc_tpb_read,isc_tpb_wait,isc_tpb_concurrency],taCommit);
1246 >  try
1247 >    Result := OpenCursorAtStart(tr,sql,FSQLDialect,Scrollable,params);
1248 >  except
1249 >    tr.Rollback(true);
1250 >    raise;
1251 >  end;
1252   end;
1253  
1254   function TFBAttachment.OpenCursorAtStart(sql: AnsiString;
1255    params: array of const): IResultSet;
1256   begin
1257 <  Result := OpenCursorAtStart(StartTransaction([isc_tpb_read,isc_tpb_wait,isc_tpb_concurrency],taCommit),sql,FSQLDialect,
790 <                   false,params);
1257 >  Result := OpenCursorAtStart(sql,false,params);
1258   end;
1259  
1260   function TFBAttachment.Prepare(transaction: ITransaction; sql: AnsiString;
# Line 817 | Line 1284 | end;
1284  
1285   function TFBAttachment.GetSQLDialect: integer;
1286   begin
1287 +  NeedDBInfo;
1288    Result := FSQLDialect;
1289   end;
1290  
1291 + function TFBAttachment.GetAttachmentID: integer;
1292 + begin
1293 +  NeedDBInfo;
1294 +  Result := FAttachmentID;
1295 + end;
1296 +
1297   function TFBAttachment.CreateBlob(transaction: ITransaction; RelationName,
1298    ColumnName: AnsiString; BPB: IBPB): IBlob;
1299   begin
# Line 896 | Line 1370 | end;
1370  
1371   function TFBAttachment.GetRemoteProtocol: AnsiString;
1372   begin
1373 +  NeedConnectionInfo;
1374    Result := FRemoteProtocol;
1375   end;
1376  
1377   function TFBAttachment.GetAuthenticationMethod: AnsiString;
1378   begin
1379 +  NeedConnectionInfo;
1380    Result := FAuthMethod;
1381   end;
1382  
1383   function TFBAttachment.GetSecurityDatabase: AnsiString;
1384   begin
1385 +  NeedConnectionInfo;
1386    Result := FSecDatabase;
1387   end;
1388  
1389   function TFBAttachment.GetODSMajorVersion: integer;
1390   begin
1391 +  NeedDBInfo;
1392    Result := FODSMajorVersion;
1393   end;
1394  
1395   function TFBAttachment.GetODSMinorVersion: integer;
1396   begin
1397 +  NeedDBInfo;
1398    Result := FODSMinorVersion;
1399   end;
1400  
1401 + function TFBAttachment.GetCharSetID: integer;
1402 + begin
1403 +  NeedConnectionInfo;
1404 +  Result := FCharSetID;
1405 + end;
1406 +
1407   function TFBAttachment.HasDecFloatSupport: boolean;
1408   begin
1409    Result := false;
# Line 942 | Line 1427 | begin
1427    Result := false;
1428   end;
1429  
1430 + function TFBAttachment.HasTable(aTableName: AnsiString): boolean;
1431 + begin
1432 +  Result := OpenCursorAtStart(
1433 +       'Select count(*) From RDB$RELATIONS Where RDB$RELATION_NAME = ?',
1434 +          [aTableName])[0].AsInteger > 0;
1435 + end;
1436 +
1437 + function TFBAttachment.HasFunction(aFunctionName: AnsiString): boolean;
1438 + begin
1439 +  Result := OpenCursorAtStart(
1440 +       'Select count(*) From RDB$FUNCTIONS Where RDB$FUNCTION_NAME = ?',
1441 +          [aFunctionName])[0].AsInteger > 0;
1442 + end;
1443 +
1444 + function TFBAttachment.HasProcedure(aProcName: AnsiString): boolean;
1445 + begin
1446 +  Result := OpenCursorAtStart(
1447 +       'Select count(*) From RDB$PROCEDURES Where RDB$PROCEDURE_NAME = ?',
1448 +          [aProcName])[0].AsInteger > 0;
1449 + end;
1450 +
1451   function TFBAttachment.HasDefaultCharSet: boolean;
1452   begin
1453 +  NeedConnectionInfo;
1454    Result := FHasDefaultCharSet
1455   end;
1456  
1457   function TFBAttachment.GetDefaultCharSetID: integer;
1458   begin
1459 +  NeedConnectionInfo;
1460    Result := FCharsetID;
1461   end;
1462  

Comparing:
ibx/branches/journaling/fbintf/client/FBAttachment.pas (property svn:eol-style), Revision 362 by tony, Tue Dec 7 13:27:39 2021 UTC vs.
ibx/branches/udr/client/FBAttachment.pas (property svn:eol-style), Revision 387 by tony, Wed Jan 19 13:34:42 2022 UTC

# Line 0 | Line 1
1 + native

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines