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

📄 abstempengine.pas

📁 Absolute Database 是来替代BDE[Borland数据库引擎]的用于Delphi 和 C++ Builder 开发用的数据库引擎. 它小巧, 高速, 健壮, 易于使用. 它能直接编译进
💻 PAS
📖 第 1 页 / 共 4 页
字号:
// AddIndex
//------------------------------------------------------------------------------
procedure TABSTemporaryTableData.AddIndex(IndexDef: TABSIndexDef; Cursor: TABSCursor);
begin
  Lock;
  try
    inherited AddIndex(IndexDef, Cursor);
    if (not Cursor.Session.InTransaction) then
      PageManager.ApplyChanges(Cursor.Session.SessionID);
  finally
    Unlock;
  end;
end;// AddIndex


//------------------------------------------------------------------------------
// DeleteIndex
//------------------------------------------------------------------------------
procedure TABSTemporaryTableData.DeleteIndex(IndexID: TABSObjectID; Cursor: TABSCursor);
begin
  Lock;
  try
    inherited DeleteIndex(IndexID, Cursor);
    if (Cursor <> nil) then
      begin
        if (not Cursor.Session.InTransaction) then
          PageManager.ApplyChanges(Cursor.Session.SessionID);
      end
    else
      PageManager.ApplyChanges(SYSTEM_SESSION_ID);
  finally
    Unlock;
  end;
end;// DeleteIndex


//------------------------------------------------------------------------------
// insert record
//------------------------------------------------------------------------------
function TABSTemporaryTableData.InsertRecord(var Cursor: TABSCursor): Boolean;
var RecordID: TABSRecordID;
begin
  if (FRecordManager = nil) then
   raise EABSException.Create(10134,ErrorLNilPointer);
  if (Cursor.CurrentRecordBuffer = nil) then
   raise EABSException.Create(10135,ErrorLNilPointer);
  // add record to first empty space
  try
    Result := FRecordManager.AddRecord(Cursor.Session.SessionID,
                                           Cursor.CurrentRecordBuffer,RecordID);
    Move(RecordID,Cursor.CurrentRecordID,sizeof(Cursor.CurrentRecordID));
  except
    Result := False;
  end;
  if (Result) then
   begin
     if (TABSRecordBitmap(Cursor.RecordBitmap).Active) then
      begin
        if (IsRecordVisible(Cursor)) then
         TABSRecordBitmap(Cursor.RecordBitmap).ShowRecord(Cursor.CurrentRecordID)
        else
         TABSRecordBitmap(Cursor.RecordBitmap).HideRecord(Cursor.CurrentRecordID);
      end;
     FIndexManager.InsertRecord(Cursor);
     Cursor.FirstPosition := False;
     Cursor.LastPosition := False;
     if (not Cursor.Session.InTransaction) then
      FPageManager.ApplyChanges(Cursor.Session.SessionID);
   end;
end; // InsertRecord;


//------------------------------------------------------------------------------
// delete record
//------------------------------------------------------------------------------
function TABSTemporaryTableData.DeleteRecord(Cursor: TABSCursor): Boolean;
begin
 Result := False;
end; // DeleteRecord


//------------------------------------------------------------------------------
// update record
//------------------------------------------------------------------------------
function TABSTemporaryTableData.UpdateRecord(Cursor: TABSCursor): Boolean;
begin
 Result := False;
end; // UpdateRecord


//------------------------------------------------------------------------------
// return record count
//------------------------------------------------------------------------------
function TABSTemporaryTableData.InternalGetRecordCount(Cursor: TABSCursor): TABSRecordNo;
begin
  if (FRecordManager = nil) then
   raise EABSException.Create(10133,ErrorLNilPointer);
  Result := FRecordManager.RecordCount;
end; // GetRecordCount


//------------------------------------------------------------------------------
// move cursor to specified position and set current record id in cursor
//------------------------------------------------------------------------------
procedure TABSTemporaryTableData.InternalSetRecNo(Cursor: TABSCursor; RecNo: TABSRecordNo);
var RecordID: TABSRecordNo;
begin
  if (FRecordManager = nil) then
   raise EABSException.Create(10136,ErrorLNilPointer);
  if (RecNo > 1) then
   RecordID := RecNo-1
  else
   RecordID := 0;
  Move(RecordID,Cursor.CurrentRecordID,sizeof(Cursor.CurrentRecordID));
end; // SetRecNo


//------------------------------------------------------------------------------
// get current record position from cursor
//------------------------------------------------------------------------------
function TABSTemporaryTableData.InternalGetRecNo(Cursor: TABSCursor): TABSRecordNo;
begin
  if (FRecordManager = nil) then
   raise EABSException.Create(10137,ErrorLNilPointer);
  Result := 0;
  Move(Cursor.CurrentRecordID,Result,sizeof(Cursor.CurrentRecordID));
  Inc(Result);
end; // GetRecNo


//---------------------------------------------------------------------------
// write BLOB field to record buffer
//---------------------------------------------------------------------------
procedure TABSTemporaryTableData.WriteBLOBFieldToRecordBuffer(
          Cursor:     TABSCursor;
          FieldNo:    Integer;
          BLOBStream: TABSStream
          );
var
    WriteSize,WriteBytes:     Integer;
    PartialBLOBDescriptor:    TABSPartialTemporaryBLOBDescriptor;
    DiskOffset:               Int64;
begin
 if (FBLOBFile = nil) then
  raise EABSException.Create(10144,ErrorLNilPointer);
 if (BLOBStream.Modified) then
  begin
   if (BLOBStream.Size = 0) then
    begin
     // empty stream
     SetNullFlag(True,FieldNo,Cursor.CurrentRecordBuffer);
    end
   else
    begin
     PartialBLOBDescriptor.NumBlocks := TABSCompressedBLOBStream(
        TABSLocalBLOBStream(BLOBStream).TemporaryStream).BLOBDescriptor.NumBlocks;
     PartialBLOBDescriptor.UncompressedSize := TABSCompressedBLOBStream(
        TABSLocalBLOBStream(BLOBStream).TemporaryStream).BLOBDescriptor.UncompressedSize;
     PartialBLOBDescriptor.CompressedSize := TABSCompressedBLOBStream(
        TABSLocalBLOBStream(BLOBStream).TemporaryStream).CompressedSize;
     SetStreamPosition(FBLOBFile,FBLOBFile.Size,10422);
     DiskOffset := FBLOBFile.Position;
     // save blob descriptor
     WriteSize := sizeof(PartialBLOBDescriptor);
     WriteBytes := FBLOBFile.Write(PartialBLOBDescriptor,WriteSize);
     if (WriteBytes <> WriteSize) then
      raise EABSException.Create(10145,ErrorLCannotWriteToStream,
        [DiskOffset,FBLOBFile.Size,WriteSize,WriteBytes]);
     // save compressed data to blob file
     TABSStream(TABSCompressedBLOBStream(TABSLocalBLOBStream(BLOBStream).TemporaryStream).
        CompressedStream).SaveToStream(FBLOBFile);
     // store offset of blob descriptor in blob file to record buffer
     Move(DiskOffset,PChar(Cursor.CurrentRecordBuffer +
      FieldManager.FieldDefs[FieldNo].MemoryOffset)^,sizeof(DiskOffset));
     SetNullFlag(False,FieldNo,Cursor.CurrentRecordBuffer);
    end; // not empty stream
  end; // Modified
end; // WriteBLOBFieldToRecordBuffer


//---------------------------------------------------------------------------
// create BLOB stream
//---------------------------------------------------------------------------
function TABSTemporaryTableData.InternalCreateBlobStream(
          Cursor:   TABSCursor;
          ToInsert: Boolean;
          FieldNo:  Integer;
          OpenMode: TABSBLOBOpenMode
          ): TABSStream;
var TempStream:             TABSTemporaryStream;
    CompressedStream:       TABSCompressedBLOBStream;
    Offset:                 Integer;
    DiskOffset,OldPos:      Int64;
    ReadBytes,ReadSize:     Integer;
    BLOBDescriptor:         TABSBLOBDescriptor;
    PartialBLOBDescriptor:  TABSPartialTemporaryBLOBDescriptor;
begin
 if (FBLOBFile = nil) then
  raise EABSException.Create(10140,ErrorLNilPointer);
 BLOBDescriptor.CompressionAlgorithm :=
     Byte(FieldManager.FieldDefs[FieldNo].BLOBCompressionAlgorithm);
 BLOBDescriptor.CompressionMode := FieldManager.FieldDefs[FieldNo].BLOBCompressionMode;
 BLOBDescriptor.BlockSize := FieldManager.FieldDefs[FieldNo].BLOBBlockSize;
 BLOBDescriptor.StartPosition := 0;
 TempStream := TABSTemporaryStream.Create(FDisableTempFiles);
 // create new compressed stream
 if ((ToInsert) or (OpenMode = bomWrite) or
     (CheckNullFlag(FieldNo,Cursor.CurrentRecordBuffer))) then
  begin
   // empty stream
   BLOBDescriptor.NumBlocks := 0;
   BLOBDescriptor.UncompressedSize := 0;
   CompressedStream := TABSCompressedBLOBStream.Create(TempStream,
    BLOBDescriptor,True);
   Result := TABSLocalBLOBStream.Create(CompressedStream,Cursor,OpenMode,FieldNo);
  end // empty stream
 else
  begin
   // copy value from TableData
   Offset := FieldManager.FieldDefs[FieldNo].MemoryOffset;
   // offset to blob field data
   Move(PChar(Cursor.CurrentRecordBuffer + Offset)^,DiskOffset,Sizeof(DiskOffset));
   OldPos := FBLOBFile.Position;

   // set position in BLOB file
   FBLOBFile.Position := DiskOffset;
   if (FBLOBFile.Position <> DiskOffset) then
     raise EABSException.Create(10141,ErrorLCannotSetPosition,
       [DiskOffset,OldPos,FBLOBFile.Size]);

   // read partial BLOB descriptor
   OldPos := FBLOBFile.Position;
   ReadSize := sizeof(TABSPartialTemporaryBLOBDescriptor);
   ReadBytes := FBLOBFile.Read(PartialBLOBDescriptor,ReadSize);
   if (ReadBytes <> ReadSize) then
    raise EABSException.Create(10142,ErrorLCannotReadFromStream,
      [OldPos,FBLOBFile.Size,ReadSize,ReadBytes]);

   // read compressed blob data
   TempStream.LoadFromStreamWithPosition(FBLOBFile,FBLOBFile.Position,
      PartialBLOBDescriptor.CompressedSize);

   // copy partial blob descriptor
   BLOBDescriptor.NumBlocks := PartialBLOBDescriptor.NumBlocks;
   BLOBDescriptor.UncompressedSize := PartialBLOBDescriptor.UncompressedSize;
   CompressedStream := TABSCompressedBLOBStream.Create(TempStream,
    BLOBDescriptor,False);
   Result := TABSLocalBLOBStream.Create(CompressedStream,Cursor,OpenMode,FieldNo);
  end; // copy value from TableData
end; // InternalCreateBlobStream


//------------------------------------------------------------------------------
// GetDirectBlobData
//------------------------------------------------------------------------------
procedure TABSTemporaryTableData.GetDirectBlobData(
              Cursor:     TABSCursor;
              FieldNo:    Integer;
              RecordBuffer: TABSRecordBuffer;
              var BLOBDescriptor: TABSPartialTemporaryBLOBDescriptor;
              var pBlobData: PChar);
var
    Offset:                 Integer;
    DiskOffset,OldPos:      Int64;
    ReadBytes,ReadSize:     Integer;
begin
 if (CheckNullFlag(FieldNo,RecordBuffer)) then
  begin
   // empty stream
   BLOBDescriptor.NumBlocks := 0;
   BLOBDescriptor.UncompressedSize := 0;
   BLOBDescriptor.CompressedSize := 0;
   pBlobData := nil;
  end // empty stream
 else
  begin
   // copy value from TableData
   Offset := FieldManager.FieldDefs[FieldNo].MemoryOffset;
   // offset to blob field data
   Move(PChar(RecordBuffer + Offset)^,DiskOffset,Sizeof(DiskOffset));
   OldPos := FBLOBFile.Position;

   // set position in BLOB file
   FBLOBFile.Position := DiskOffset;
   if (FBLOBFile.Position <> DiskOffset) then
     raise EABSException.Create(10509,ErrorLCannotSetPosition,
       [DiskOffset,OldPos,FBLOBFile.Size]);

   // read partial BLOB descriptor
   OldPos := FBLOBFile.Position;
   ReadSize := sizeof(TABSPartialTemporaryBLOBDescriptor);
   ReadBytes := FBLOBFile.Read(BLOBDescriptor,ReadSize);
   if (ReadBytes <> ReadSize) then
    raise EABSException.Create(10510,ErrorLCannotReadFromStream,
      [OldPos,FBLOBFile.Size,ReadSize,ReadBytes]);

   // read compressed blob data
   pBlobData := MemoryManager.GetMem(BLOBDescriptor.CompressedSize);
   FBLOBFile.ReadBuffer(pBlobData^, BLOBDescriptor.CompressedSize);
   FBLOBFile.Position := OldPos;
  end; // copy value from TableData
end;// GetDirectBlobData


//------------------------------------------------------------------------------
// SetDirectBlobData
//------------------------------------------------------------------------------
procedure TABSTemporaryTableData.SetDirectBlobData(
              Cursor:     TABSCursor;
              FieldNo:    Integer;
              RecordBuffer: TABSRecordBuffer;
              var BLOBDescriptor: TABSPartialTemporaryBLOBDescriptor;
              var pBlobData: PChar);
var
    WriteSize,WriteBytes:     Integer;
    DiskOffset:               Int64;
begin
 if (FBLOBFile = nil) then
  raise EABSException.Create(10506,ErrorLNilPointer);
 if (BLOBDescriptor.UncompressedSize = 0) then
   SetNullFlag(True,FieldNo,Cursor.CurrentRecordBuffer)
 else
    begin
     SetStreamPosition(FBLOBFile,FBLOBFile.Size,10507);
     DiskOffset := FBLOBFile.Position;
     // save blob descriptor
     WriteSize := sizeof(BLOBDescriptor);
     WriteBytes := FBLOBFile.Write(BLOBDescriptor,WriteSize);
     if (WriteBytes <> WriteSize) then
      raise EABSException.Create(10508,ErrorLCannotWriteToStream,
        [DiskOffset,FBLOBFile.Size,WriteSize,WriteBytes]);
     // save compressed data to blob file
     FBLOBFile.WriteBuffer(pBlobData^,BLOBDescriptor.CompressedSize);
     // store offset of blob descriptor in blob file to record buffer
     Move(DiskOffset,PChar(Cursor.CurrentRecordBuffer +
      FieldManager.FieldDefs[FieldNo].MemoryOffset)^,sizeof(DiskOffset));
     SetNullFlag(False,FieldNo,Cursor.CurrentRecordBuffer);
    end;
end;// SetDirectBlobData


//------------------------------------------------------------------------------
// FreeDirectBlobData
//------------------------------------------------------------------------------
procedure TABSTemporaryTableData.FreeDirectBlobData(
              Cursor:     TABSCursor;
              FieldNo:    Integer;
              RecordBuffer: TABSRecordBuffer;
              var BLOBDescriptor: TABSPartialTemporaryBLOBDescriptor;
              var pBlobData: PChar);
begin
  if (pBlobData <> nil) then
    MemoryManager.FreeAndNillMem(pBlobData);
end;// FreeDirectBlobData

end.

⌨️ 快捷键说明

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