⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myldbbaseengine.pas

📁 一个本地database引擎,支持中文T_Sql查询,兼容DELPHI标准数据库控件
💻 PAS
📖 第 1 页 / 共 5 页
字号:
                    SessionID:      TMYLDBSessionID;
                    RecordBuffer:   TMYLDBRecordBuffer
                            ): Boolean; virtual; abstract;
    function CanUpdateRecord(
                    SessionID:      TMYLDBSessionID;
                    OldRecordBuffer, NewRecordBuffer: TMYLDBRecordBuffer
                            ): Boolean; virtual; abstract;
    procedure InsertRecord(Cursor: TMYLDBCursor); virtual; abstract;
    procedure DeleteRecord(Cursor: TMYLDBCursor); virtual; abstract;
    procedure UpdateRecord(Cursor: TMYLDBCursor; BeforeNewRecordIsStored: Boolean); virtual; abstract;

    function GetMaxEntriesPerPage: Integer; virtual; abstract;
    procedure GetRecordIDByIndexPosition(SessionID: TMYLDBSessionID; IndexPos: TMYLDBIndexPosition; var RecordID: TMYLDBRecordID); virtual; abstract;

    property IndexDef: TMYLDBIndexDef read FIndexDef;
    property IndexManager: TMYLDBBaseIndexManager read FIndexManager;
  end; // TMYLDBIndex




////////////////////////////////////////////////////////////////////////////////
//
// TMYLDBBaseConstraintManager
//
////////////////////////////////////////////////////////////////////////////////


  TMYLDBBaseConstraintManager = class (TObject)
   private
    FConstraintDefs:    TMYLDBConstraintDefs;
    FTableData:         TMYLDBTableData;
   public
    procedure LoadMetadata(Stream: TStream);
    procedure SaveMetadata(Stream: TStream);
    constructor Create(aTableData: TMYLDBTableData); //virtual;
    destructor Destroy; override;

    // check constraint conditions
    function CheckConstraints(
                                SessionID:       TMYLDBSessionID;
                                NewRecordBuffer: TMYLDBRecordBuffer;
                                OldRecordBuffer: TMYLDBRecordBuffer;
                                ToInsert:        Boolean;
                                CurrentRecordID: TMYLDBRecordID;
                                RaiseException:  Boolean
                              ): Boolean;

    // Link object id
    procedure LinkObjectId(ConstraintDef: TMYLDBConstraintDef);
    // Link object ids between constraints and fields
    procedure LinkObjectIds;
    // Generate Constraint Names for empty constraints
    procedure FillConstraintAutoNames;

    // add constraint for primary or Unique index
    procedure AddConstraintFromIndex(IndexDef: TMYLDBIndexDef);
    // Delete Constraint Linked with deleted index
    procedure DeleteConstraintForIndexID(IndexObjectID: TMYLDBObjectID);

   public
    property ConstraintDefs: TMYLDBConstraintDefs read FConstraintDefs write FConstraintDefs;

  end; // TMYLDBBaseConstraintManager


////////////////////////////////////////////////////////////////////////////////
//
// TMYLDBRecordBitmap
//
////////////////////////////////////////////////////////////////////////////////

  TMYLDBRecordBitmap = class (TObject)
  private
   FBitmap:     TMYLDBBitsArray;
   FTableData:  TMYLDBTableData;
   FActive:     Boolean;
   FIndexBased: Boolean;
   FTableState: Integer;
  protected
   function GetSize: TMYLDBRecordNo;
   procedure SetSize(NewSize: TMYLDBRecordNo);
   function GetOutdated: Boolean;
  public
   constructor Create(aTableData: TMYLDBTableData);
   destructor Destroy; override;
   function GetRecordCount: TMYLDBRecordNo;
   function GetRecNoByRecordID(RecordID: TMYLDBRecordID): TMYLDBRecordNo;
   function GetRecNoByIndexPos(IndexID: TMYLDBObjectID; IndexPos: TMYLDBIndexPosition): TMYLDBRecordNo;
   function GetRecordIDByRecNo(RecNo: TMYLDBRecordNo): TMYLDBRecordID;
   procedure GetIndexPosByRecNo(IndexID: TMYLDBObjectID; RecNo: TMYLDBRecordNo; IndexPos: TMYLDBIndexPosition);
   function IsRecordVisible(RecordID: TMYLDBRecordID): Boolean; overload;
   function IsRecordVisible(IndexID: TMYLDBObjectID; IndexPos: TMYLDBIndexPosition): Boolean; overload;
   procedure ShowRecord(RecordID: TMYLDBRecordID); overload;
   procedure ShowRecord(IndexID: TMYLDBObjectID; IndexPos: TMYLDBIndexPosition); overload;
   procedure HideRecord(RecordID: TMYLDBRecordID); overload;
   procedure HideRecord(IndexID: TMYLDBObjectID; IndexPos: TMYLDBIndexPosition); overload;
   procedure SetAllBits;
   procedure Clear;
   function FindRecord(Restart, GoForward: Boolean; CurRecordID: TMYLDBRecordID;
                       var FoundRecordID: TMYLDBRecordID): Boolean; overload;
   function FindRecord(Restart, GoForward: Boolean; CurIndexPos, FoundRecordIndexPos: TMYLDBIndexPosition;
                       IndexID: TMYLDBObjectID): Boolean; overload;
  public
   property Active: Boolean read FActive write FActive;
   property Size: TMYLDBRecordNo read GetSize write SetSize;
   property Outdated: Boolean read GetOutdated;
   property IndexBased: Boolean read FIndexBased write FIndexBased;
   property TableState: Integer read FTableState write FTableState;
  end; // TMYLDBRecordBitmap



////////////////////////////////////////////////////////////////////////////////
//
// TMYLDBTableData
//
////////////////////////////////////////////////////////////////////////////////


  TMYLDBTableData = class (TObject)
  private
    FCSect:                        TRTLCriticalSection;
    FIsRepairing:                  Boolean;
  protected
    FPageManager:                  TMYLDBPageManager;
    FDatabaseData:                 TMYLDBDatabaseData;
    FTableName:                    String;
    FBLOBCompression:              TMYLDBCompression;
    FActive:                       Boolean;
    FFieldManager:                 TMYLDBBaseFieldManager;
    FConstraintManager:            TMYLDBBaseConstraintManager;
    FIndexManager:                 TMYLDBBaseIndexManager;
    FRecordManager:                TMYLDBBaseRecordManager;
    FCursorList:                   TList;
    FDisableTempFiles:             Boolean;

    // Fill New ObjectID for all defs
    procedure FillDefsByObjectId(Defs: TMYLDBMetaObjectDefs);
    function GetRecordBufferSize: Integer;
    procedure CreateRecordManager; virtual; abstract;
    procedure CreateFieldManager(FieldDefs: TMYLDBFieldDefs); virtual;
    procedure CreateIndexManager(IndexDefs: TMYLDBIndexDefs); virtual;
    procedure CreateConstraintManager(ConstraintDefs: TMYLDBConstraintDefs); virtual;
    procedure InitCursor(Cursor: TMYLDBCursor); virtual;
  public
    constructor Create(aDatabaseData: TMYLDBDatabaseData);
    destructor Destroy; override;

    // lock
    procedure Lock;
    // unlock
    procedure Unlock;

    procedure ApplyChanges(SessionID: TMYLDBSessionID; InTransaction: Boolean); virtual;
    procedure CancelChanges(SessionID: TMYLDBSessionID; InTransaction: Boolean); virtual;

    // table operations
    procedure CreateTable(
                          Cursor: TMYLDBCursor;
                          FieldDefs: TMYLDBFieldDefs;
                          IndexDefs: TMYLDBIndexDefs;
                          ConstraintDefs: TMYLDBConstraintDefs
                         ); virtual; abstract;
    procedure DeleteTable(Cursor: TMYLDBCursor; DesignMode: Boolean = False); virtual;
    procedure EmptyTable(Cursor: TMYLDBCursor); virtual;
    procedure RenameTable(NewTableName: String; Cursor: TMYLDBCursor); virtual;

    procedure OpenTable(Cursor: TMYLDBCursor); virtual;
    procedure CloseTable(Cursor: TMYLDBCursor); virtual;

    // Rename Field by Field Index in FieldDefs
    procedure RenameField(FieldName, NewFieldName: String); virtual;

    procedure AddIndex(IndexDef: TMYLDBIndexDef; Cursor: TMYLDBCursor); virtual;
    procedure DeleteIndex(IndexID: TMYLDBObjectID; Cursor: TMYLDBCursor); virtual;
    procedure EmptyIndex(IndexID: TMYLDBObjectID; SessionID: TMYLDBSessionID);
    procedure DeleteAllIndexes(Cursor: TMYLDBCursor);
    procedure EmptyAllIndexes(SessionID: TMYLDBSessionID);
    // return index name
    function FindOrCreateIndex(
                                Cursor: TMYLDBCursor;
                                FieldNamesList,
                                AscDescList,
                                CaseSensitivityList: TStringList;
                                var IsCreated: Boolean
                              ): String;
    function IndexExists(Cursor: TMYLDBCursor;
        FieldNamesList, AscDescList, CaseSensitivityList: TStringList): Boolean;


    // return true if Unique Constraint Failed
    function IsUniqueConstraintFailed(
                                        SessionID:        TMYLDBSessionID;
                                        IndexID:          TMYLDBObjectID;
                                        NewRecordBuffer:  TMYLDBRecordBuffer;
                                        OldRecordBuffer:  TMYLDBRecordBuffer;
                                        ToInsert:         Boolean;
                                        CurrentRecordID:  TMYLDBRecordID
                                     ): Boolean;

    //---------------- Search and navigation methods ---------------------------
   protected
    // clear bitmap and set state and indexbased
    procedure InitRecordBitmap(RecordBitmap: TMYLDBRecordBitmap; IndexID: TMYLDBObjectID);
    // if necessary - build record bitmap for cursor
    procedure RefreshCursorRecordBitmap(Cursor: TMYLDBCursor);
    // build record bitmap for cursor
    procedure BuildCursorRecordBitmap(Cursor: TMYLDBCursor);
    // return filter bitmap rec count
    function GetBitmapRecordCount(SessionID: TMYLDBSessionID): TMYLDBRecordNo; virtual; abstract;
    // return filter bitmap rec no by record id
    function GetBitmapRecNoByRecordID(RecordID: TMYLDBRecordID): TMYLDBRecordNo; virtual; abstract;
    // return filter bitmap rec no by record id
    function GetRecordIDByBitmapRecNo(RecordNo: TMYLDBRecordNo): TMYLDBRecordID; virtual; abstract;
    // return filter bitmap rec no by record id
    function GetBitmapRecNoByIndexPosition(IndexID: TMYLDBObjectID; IndexPos: TMYLDBIndexPosition): TMYLDBRecordNo;
    // return filter bitmap rec no by record id
    procedure GetIndexPositionByBitmapRecNo(IndexID: TMYLDBObjectID; RecordNo: TMYLDBRecordNo; IndexPos: TMYLDBIndexPosition);
    // return index position by record id
    function GetIndexPositionByRecordID(SessionID: TMYLDBSessionID; IndexID: TMYLDBObjectID; RecordID: TMYLDBRecordID; IndexPos: TMYLDBIndexPosition): Boolean;
    // return record id by index position
    procedure GetRecordIDByIndexPosition(SessionID: TMYLDBSessionID; IndexID: TMYLDBObjectID; IndexPos: TMYLDBIndexPosition; var RecordID: TMYLDBRecordID);

    // return true if record is in specified range
    function IsRecordInRange(Cursor: TMYLDBCursor): Boolean;
    // return true if record is not equal to the previous record
    function IsRecordInDistinct(Cursor: TMYLDBCursor): Boolean;
    // return true if record is visible (with applied filters, ranges, OnFilterRecord)
    function IsRecordVisible(Cursor: TMYLDBCursor): Boolean;

    procedure MergeAndCheckSearchConditionsCompatibility(
                                var Condition1:            TMYLDBScanSearchCondition;
                                var Condition2:            TMYLDBScanSearchCondition;
                                out Incompatible:          Boolean;
                                out HaveBeenMerged:        Boolean
                                           );
    // returns true if conditions are equal
    function IsEqualConditions(
                                var Condition1:            TMYLDBScanSearchCondition;
                                var Condition2:            TMYLDBScanSearchCondition
                                           ): Boolean;
    // removes duplicate conditions
    procedure RemoveDuplicateConditions(Conditions: TMYLDBScanSearchConditionArray);
    // sorts Conditions array and removes unnecessary conditions
    procedure OptimizeSearchConditions(
                                var Conditions:               TMYLDBScanSearchConditionArray;
                                out NonCompatibleConditions:  Boolean
                                     );
    // prepare conditions array
    procedure PrepareConditions(
                           Cursor:            TMYLDBCursor;
                           Conditions:        TMYLDBScanSearchConditionArray;
                           KeyCondition:      TMYLDBScanSearchCondition;
                           SearchExpression:  TMYLDBExpression;
                           GoForward:         Boolean
                               );
    // try to find the best scan condition with min range record count
    function ChooseScanConditionsWithMinScanCost(
                           SessionID:              TMYLDBSessionID;
                           Conditions:             TMYLDBScanSearchConditionArray;
                           CurrentIndexID:         TMYLDBObjectID;
                           CurrentRecordID:        TMYLDBRecordID;
                           FilterRecordPtr:        Pointer;
                           var ScanConditionNo:    Integer;
                           var ScanEndConditionNo: Integer
                                  ): Boolean;
    // if index is defined as unique or contain unique field
    function IsIndexUnique(IndexID: TMYLDBObjectID): Boolean;
    // try to find the best scan condition using heuristics
    function ChooseScanConditionsByHeuristsics(
                           Conditions:             TMYLDBScanSearchConditionArray;
                           CurrentIndexID:         TMYLDBObjectID;
                           var ScanConditionNo:    Integer;
                           var ScanEndConditionNo: Integer
                                  ): Boolean;
    // try to find the best scan condition
    procedure ChooseScanConditions(
                           SessionID:              TMYLDBSessionID;
                           Conditions:             TMYLDBScanSearchConditionArray;
                           CurrentIndexID:         TMYLDBObjectID;
                           CurrentRecordID:        TMYLDBRecordID;
                           GoForward:              Boolean;
                           FilterRecordPtr:        Pointer;
                           var ScanConditionNo:    Integer;
                           var ScanEndConditionNo: Integer
                                  );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -