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

📄 abstypes.pas

📁 Absolute Database 是来替代BDE[Borland数据库引擎]的用于Delphi 和 C++ Builder 开发用的数据库引擎. 它小巧, 高速, 健壮, 易于使用. 它能直接编译进
💻 PAS
📖 第 1 页 / 共 5 页
字号:
    procedure SetBit(BitNo: Integer; Value: Boolean);
    // returns number of bit = 1 in FBits array by bit position
    function GetBitNoByBitPosition(BitPosition: Integer): Integer;
    // returns position of bit = 1 by bit no in FBits array
    function GetBitPositionByBitNo(BitNo: Integer): Integer;
    procedure SetAllBits;
    function FindBit(Value: Boolean; var BitNo: Integer): Boolean;
    function Find(Restart,GoForward: Boolean; CurBitNo: Integer; var FoundBitNo: Integer): Boolean;
   public
    property Size: Integer read FBitCount write SetSize;
    property NonZeroBitCount: Integer read FNonZeroBitCount;
  end;


////////////////////////////////////////////////////////////////////////////////
//
// Bits functions
//
////////////////////////////////////////////////////////////////////////////////


  // return true if null flag is set (bit = 1)
  function CheckNullFlag(
          BitNo:       Integer; // number of bit to check
          NullFlags:   PChar // pointer to bits of null flags
                      ): Boolean;

  // set or clear a null flag
  procedure SetNullFlag(
          ToSet:       Boolean; // if true - set bit = 1, otherwise set bit = 0
          BitNo:       Integer; // number of bit to check
          NullFlags:   PChar // pointer to bits of null flags
                     );


////////////////////////////////////////////////////////////////////////////////
//
// Other functions
//
////////////////////////////////////////////////////////////////////////////////


  // returns temporary name
  function GetTemporaryName(Prefix: String): String;

  function BracketFieldName(name: String): String;

implementation

uses Math, ABSExpressions, ABSCompression, ABSBTree, ABSBase;


////////////////////////////////////////////////////////////////////////////////
//
// TABSIndexPositionCache
//
////////////////////////////////////////////////////////////////////////////////


//------------------------------------------------------------------------------
// Create
//------------------------------------------------------------------------------
constructor TABSIndexPositionCache.Create;
begin
  Position := TABSKeyPath.Create;
  IndexID := INVALID_OBJECT_ID;
  TableState := -1;
end;// Create


//------------------------------------------------------------------------------
// Destroy
//------------------------------------------------------------------------------
destructor TABSIndexPositionCache.Destroy;
begin
  TABSKeyPath(Position).Free;
end;// Destroy




////////////////////////////////////////////////////////////////////////////////
//
// TABSScanSearchCondition
//
////////////////////////////////////////////////////////////////////////////////


//------------------------------------------------------------------------------
// ClearKeyRecordBuffer
//------------------------------------------------------------------------------
procedure TABSScanSearchCondition.ClearKeyRecordBuffer;
begin
  if (not FExternalKeyRecordBuffer) then
    if (KeyRecordBuffer <> nil) then
      MemoryManager.FreeAndNillMem(KeyRecordBuffer);
end;// ClearKeyRecordBuffer


//------------------------------------------------------------------------------
// Create
//------------------------------------------------------------------------------
constructor TABSScanSearchCondition.Create(ExternalKeyRecordBuffer: Boolean);
begin
  FExternalKeyRecordBuffer := ExternalKeyRecordBuffer;
  KeyRecordBuffer := nil;
  KeyRecordBufferSize := 0;
  IndexID := INVALID_OBJECT_ID;
  Expression := nil;
  CreatedFromExpression := False;
  ParentExpressions := TList.Create;
  ParentExpressionNodes := TList.Create;
  PartialCompare := False;
end;// Create


//------------------------------------------------------------------------------
// Destroy
//------------------------------------------------------------------------------
destructor TABSScanSearchCondition.Destroy;
begin
  ClearKeyRecordBuffer;
  ParentExpressions.Free;
  ParentExpressionNodes.Free;
  inherited;
end;// Destroy


//------------------------------------------------------------------------------
// Assign
//------------------------------------------------------------------------------
procedure TABSScanSearchCondition.Assign(ScanSearchCondition: TABSScanSearchCondition);
var
  i: Integer;
begin
  Condition := ScanSearchCondition.Condition;
  ClearKeyRecordBuffer;
  if (ScanSearchCondition.KeyRecordBuffer <> nil) then
    begin
      KeyRecordBuffer := MemoryManager.AllocMem(ScanSearchCondition.KeyRecordBufferSize);
      Move(ScanSearchCondition.KeyRecordBuffer^, KeyRecordBuffer^, ScanSearchCondition.KeyRecordBufferSize);
    end
  else
    KeyRecordBuffer := nil;
  FExternalKeyRecordBuffer := False;
  KeyRecordBufferSize := ScanSearchCondition.KeyRecordBufferSize;
  KeyFieldCount := ScanSearchCondition.KeyFieldCount;
  IndexID := ScanSearchCondition.IndexID;
  Expression := ScanSearchCondition.Expression;
  CreatedFromExpression := ScanSearchCondition.CreatedFromExpression;
  PartialCompare := ScanSearchCondition.PartialCompare;

  // assign
  ParentExpressions.Clear;
  for i:=0 to ScanSearchCondition.ParentExpressions.Count-1 do
    ParentExpressions.Add(ScanSearchCondition.ParentExpressions[i]);
  // assign
  ParentExpressionNodes.Clear;
  for i:=0 to ScanSearchCondition.ParentExpressionNodes.Count-1 do
    ParentExpressionNodes.Add(ScanSearchCondition.ParentExpressionNodes[i]);
end;// Assign



////////////////////////////////////////////////////////////////////////////////
//
// TABSScanSearchConditionArray
//
////////////////////////////////////////////////////////////////////////////////

//------------------------------------------------------------------------------
// Create
//------------------------------------------------------------------------------
constructor TABSScanSearchConditionArray.Create;
begin
  Items := nil;
  Count := 0;
end;// Create


//------------------------------------------------------------------------------
// Destroy
//------------------------------------------------------------------------------
destructor TABSScanSearchConditionArray.Destroy;
var
  i: Integer;
begin
  for i := 0 to Count-1 do
    Items[i].Free;
  SetLength(Items, 0);
end;// Destroy


//------------------------------------------------------------------------------
// Delete
//------------------------------------------------------------------------------
procedure TABSScanSearchConditionArray.Delete(ItemNo: Integer);
begin
 if (ItemNo < 0) or (ItemNo >= Count) then
  raise EABSException.Create(10413,ErrorLInvalidItemNumber);
 if (Items[ItemNo] = nil) then
  raise EABSException.Create(10414,ErrorLNilPointer);
 Items[ItemNo].Free;
 if (ItemNo < Count - 1) then
  Move(Items[ItemNo+ 1 ],Items[ItemNo],
      (Count - ItemNo-1) * SizeOf(TABSScanSearchCondition));
 Dec(Count);
 SetLength(Items,Count);
end; // Delete


//------------------------------------------------------------------------------
// AddExpression
//------------------------------------------------------------------------------
procedure TABSScanSearchConditionArray.AddExpression(Expression: Pointer);
begin
  SetLength(Items, Count+1);
  Items[Count] := TABSScanSearchCondition.Create(False);
  Items[Count].Expression := Expression;
  Items[Count].Condition := scNone;
  Items[Count].KeyRecordBuffer := nil;
  Items[Count].KeyFieldCount := 0;
  Items[Count].IndexID := INVALID_OBJECT_ID;
  Inc(Count);
end;// AddExpression


//------------------------------------------------------------------------------
// AddCondition
//------------------------------------------------------------------------------
procedure TABSScanSearchConditionArray.AddCondition(
                  ScanSearchCondition:  TABSScanSearchCondition
                             );
begin
  if ((ScanSearchCondition.Expression = nil) and
      (ScanSearchCondition.IndexID = INVALID_ID4)) then
   raise EABSException.Create(10388,ErrorLInvalidScanConditionNoIndex,
      [ScanSearchCondition.IndexID,Count]);
  SetLength(Items, Count+1);
  Items[Count] := TABSScanSearchCondition.Create(False);
  Items[Count].Assign(ScanSearchCondition);
  Inc(Count);
end;// AddCondition


//------------------------------------------------------------------------------
// TryToExtendMultiFieldIndexCondition
//------------------------------------------------------------------------------
procedure TABSScanSearchConditionArray.TryToExtendMultiFieldIndexCondition(
              ConditionNo: Integer; IndexDef: Pointer; Expressions: TList);
var
  bExtendSucceeded: Boolean;
  i: integer;
begin
  if (TABSIndexDef(IndexDef).ColumnCount > 1) then
    begin
      bExtendSucceeded := False;
      repeat
        for i := 0 to Expressions.Count-1 do
          begin
            bExtendSucceeded := TABSExpression(Expressions[i]).ExtendMultiFieldIndexCondition(Items[ConditionNo], IndexDef);
            if (bExtendSucceeded) then
              break;
          end;
      until (not bExtendSucceeded);
    end;
end;// TryToExtendMultiFieldIndexCondition


//------------------------------------------------------------------------------
// CreateIndexScanConditionsFromExpressions
//------------------------------------------------------------------------------
procedure TABSScanSearchConditionArray.CreateIndexScanConditionsFromExpressions(IndexDefs: Pointer; SessionID: TABSSessionID);
var
  i: Integer;
  Expressions: TList;
begin
  // create all possible single-field index scan conditions
  for i := 0 to Count-1 do
    if (Items[i].Expression <> nil) then
      TABSExpression(Items[i].Expression).CreateIndexScanConditions(IndexDefs, Self, SessionID);

  Expressions := TList.Create;
  try
    // collect all expressions
    for i := 0 to Count-1 do
      if (Items[i].Expression <> nil) then
        Expressions.Add(Items[i].Expression);

    if (Expressions.Count > 0) then
      // try to extend multi-field index conditions
      for i := 0 to Count-1 do
        if (Items[i].Expression = nil) then
          TryToExtendMultiFieldIndexCondition(i, TABSIndexDefs(IndexDefs).GetDefByObjectId(Items[i].IndexID), Expressions);
  finally
    Expressions.Free;
  end;
end;// CreateIndexScanConditionsFromExpressions


//------------------------------------------------------------------------------
// RemoveUnusedConditionsCreatedFromExpressions
//------------------------------------------------------------------------------
procedure TABSScanSearchConditionArray.RemoveUnusedConditionsCreatedFromExpressions(
                               var ScanConditionNo,ScanEndConditionNo: Integer);
var
  i: integer;
begin
  i := 0;
  while (i < Count-1) do
    begin
      if not (i in [ScanConditionNo, ScanEndConditionNo]) then
        if (Items[i].CreatedFromExpression) then
          begin
            Delete(i);
            if (ScanConditionNo > i) then
              Dec(ScanConditionNo);
            if (ScanEndConditionNo > i) then
              Dec(ScanEndConditionNo);
            i := 0;
            continue;
          end;
      Inc(i);
    end;
end;// RemoveUnusedConditionsCreatedFromExpressions


//------------------------------------------------------------------------------
// ExtractChosenConditionsFromExpressions
//------------------------------------------------------------------------------
procedure TABSScanSearchConditionArray.ExtractChosenConditionsFromExpressions(ScanConditionNo, ScanEndConditionNo: Integer);
var
  i: Integer;
begin
  for i := 0 to Count-1 do
    if (Items[i].Expression <> nil) then
      TABSExpression(Items[i].Expression).RemoveExtractedNodes(Self,ScanConditionNo,ScanEndConditionNo);
end;// ExtractChosenConditionsFromExpressions


//------------------------------------------------------------------------------
// ReturnExtractedConditionsToExpressions
//------------------------------------------------------------------------------
procedure TABSScanSearchConditionArray.ReturnExtractedConditionsToExpressions(ScanConditionNo, ScanEndConditionNo: Integer);
var
  i,j: Integer;
begin
  for i := 0 to Count-1 do
    if (i in [ScanConditionNo, ScanEndConditionNo]) then
      for j := 0 to Items[i].ParentExpressions.Count-1 do
        TABSExpression(Items[i].ParentExpressions[j]).AddNode(Items[i].ParentExpressionNodes[j]);
end;// ReturnExtractedConditionsToExpressions



////////////////////////////////////////////////////////////////////////////////
//
// TABSIntegerArray
//
////////////////////////////////////////////////////////////////////////////////

//------------------------------------------------------------------------------
// Construct array of specified size
//------------------------------------------------------------------------------
constructor TABSIntegerArray.Create(
  size: Integer;
  DefaultAllocBy: Integer;
  MaximumAllocBy: Integer
  );
begin
 AllocBy := DefaultAllocBy; // default alloc
 deAllocBy := DefaultAllocBy; // default dealloc
 MaxAllocBy := MaximumAllocBy; // max alloc
 AllocItemCount := 0;
 SetSize(size);
end;//TABSIntegerArray.Create


//------------------------------------------------------------------------------
// Destruct array (free mem)
//------------------------------------------------------------------------------
destructor TABSIntegerArray.Destroy;
begin
 Items := nil;
 inherited Destroy;
end;//TABSIntegerArray.Destroy;


⌨️ 快捷键说明

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