📄 jvqmemorydataset.pas
字号:
if NewChange then
Inc(FRowsChanged)
//---------------------------------------------------------------------------
end;
//----------------- Added by CFZ -------------------------------
procedure TJvMemoryData.Open;
begin
try
if FDataSet <> nil then
begin
if FLoadStructure then
CopyStructure(FDataSet, FAutoIncAsInteger)
else
if FApplyMode <> amNone then // Added 2004/10/25 (CFZ)
begin
AddStatusField;
// Removed (2004/10/19) becuase all fields are included in Design Time (CFZ)
(* CheckStructure(FAutoIncAsInteger); *)
HideStatusField;
end;
end;
inherited;
except
SysUtils.Abort;
Exit;
end;
if (FDataSet <> nil) and FLoadRecords then
begin
if not FDataSet.Active then
FDataSet.Open;
FRowsOriginal := CopyFromDataSet;
if FRowsOriginal > 0 then
begin
if FKeyFieldNames <> '' then
SortOnFields(KeyFieldNames);
if FApplyMode = amAppend then
Last
else
First;
end;
if FDataSet.Active And FDataSetClosed Then
FDataSet.Close;
end;
end;
//--------------------------------------------------------------
procedure TJvMemoryData.OpenCursor(InfoQuery: Boolean);
begin
if not InfoQuery then
begin
if FieldCount > 0 then
FieldDefs.Clear;
InitFieldDefsFromFields;
end;
FActive := True;
inherited OpenCursor(InfoQuery);
end;
procedure TJvMemoryData.InternalOpen;
begin
BookmarkSize := SizeOf(TBookmarkData);
if DefaultFields then
CreateFields;
BindFields(True);
InitBufferPointers(True);
InternalFirst;
end;
procedure TJvMemoryData.InternalClose;
begin
ClearRecords;
FAutoInc := 1;
BindFields(False);
if DefaultFields then
DestroyFields;
FreeIndexList;
FActive := False;
end;
procedure TJvMemoryData.InternalHandleException;
begin
Application.HandleException(Self);
end;
procedure TJvMemoryData.InternalInitFieldDefs;
begin
// InitFieldDefsFromFields
end;
function TJvMemoryData.IsCursorOpen: Boolean;
begin
Result := FActive;
end;
function TJvMemoryData.GetRecordCount: Integer;
begin
Result := FRecords.Count;
end;
function TJvMemoryData.GetRecNo: Integer;
begin
CheckActive;
UpdateCursorPos;
if (FRecordPos = -1) and (RecordCount > 0) then
Result := 1
else
Result := FRecordPos + 1;
end;
procedure TJvMemoryData.SetRecNo(Value: Integer);
begin
if (Value > 0) and (Value <= FRecords.Count) then
begin
FRecordPos := Value - 1;
Resync([]);
end;
end;
function TJvMemoryData.IsSequenced: Boolean;
begin
Result := not Filtered;
end;
function TJvMemoryData.Locate(const KeyFields: string;
const KeyValues: Variant; Options: TLocateOptions): Boolean;
begin
DoBeforeScroll;
Result := DataSetLocateThrough(Self, KeyFields, KeyValues, Options);
if Result then
begin
DataEvent(deDataSetChange, 0);
DoAfterScroll;
end;
end;
procedure TJvMemoryData.EmptyTable;
begin
if Active then
begin
CheckBrowseMode;
ClearRecords;
ClearBuffers;
DataEvent(deDataSetChange, 0);
end;
end;
//------------------------------ Added by CFZ --------------------------------------------
procedure TJvMemoryData.AddStatusField;
begin
// Check if FieldStatus not exists in FieldDefs Added 2004/10/19 (CFZ)
if (FieldDefs.Count > 0) and not (FieldDefs[FieldDefs.Count - 1].Name = FStatusName) then
FieldDefs.Add(FStatusName, ftSmallint);
end;
procedure TJvMemoryData.HideStatusField;
begin
// Check if FieldStatus already exists in FieldDefs Added 2004/10/25 (CFZ)
if (FieldDefs.Count > 0) and (FieldDefs[FieldDefs.Count - 1].Name = FStatusName) then
begin
FieldDefs[FieldDefs.Count - 1].Attributes := [faHiddenCol]; // Hide in FieldDefs
// Check if FieldStatus not exists in Fields Added 2004/10/25 (CFZ)
if not (Fields[Fields.Count - 1].FieldName = FStatusName) then
FieldDefs[FieldDefs.Count - 1].CreateField(Self);
Fields[Fields.Count - 1].Visible := False; // Hide in Fields
end;
end;
procedure TJvMemoryData.CheckStructure(UseAutoIncAsInteger: Boolean);
var
I: Integer;
procedure CheckDataTypes(FieldDefs: TFieldDefs);
var
J: Integer;
begin
for J := FieldDefs.Count - 1 downto 0 do
begin
if (FieldDefs.Items[J].DataType = ftAutoInc) and UseAutoIncAsInteger then
FieldDefs.Items[J].DataType := ftInteger;
if not (FieldDefs.Items[J].DataType in ftSupported) then
FieldDefs.Items[J].Free;
end;
end;
begin
CheckDataTypes(FieldDefs);
for I := 0 to FieldDefs.Count - 1 do
if (csDesigning in ComponentState) and (Owner <> nil) then
FieldDefs.Items[I].CreateField(Owner)
else
FieldDefs.Items[I].CreateField(Self);
end;
procedure TJvMemoryData.SetDataSet(ADataSet: TDataSet);
begin
FDataSet := ADataSet;
end;
procedure TJvMemoryData.SetDataSetClosed(Value: Boolean);
begin
if (csDesigning in ComponentState) and (FDataSet = nil) then
FDataSetClosed := True
else
FDataSetClosed := Value;
end;
procedure TJvMemoryData.SetLoadStructure(Value: Boolean);
begin
if (csDesigning in ComponentState) and (FDataSet = nil) then
FLoadStructure := False
else
FLoadStructure := Value;
end;
procedure TJvMemoryData.SetLoadRecords(Value: Boolean);
begin
if (csDesigning in ComponentState) and (FDataSet = nil) then
FLoadRecords := False
else
FLoadRecords := Value;
end;
procedure TJvMemoryData.SetApplyMode(Value: TApplyMode);
begin
if (csDesigning in ComponentState) and (FDataSet = nil) then
FApplyMode := amNone
else
FApplyMode := Value;
end;
procedure TJvMemoryData.SetExactApply(Value: Boolean);
begin
if (csDesigning in ComponentState) and (FDataSet = nil) then
FExactApply := False
else
FExactApply := Value;
end;
//----------------------------------------------------------------------------------------
procedure TJvMemoryData.FixReadOnlyFields(MakeReadOnly: Boolean);
var
I: Integer;
begin
if MakeReadOnly then
for I := 0 to FieldCount - 1 do
Fields[I].ReadOnly := (Fields[I].Tag = 1)
else
for I := 0 to FieldCount - 1 do
begin
Fields[I].Tag := Ord(Fields[I].ReadOnly);
Fields[I].ReadOnly := False;
if Fields[I].DataType = ftAutoInc then
FAutoIncField := Fields[I];
end;
end;
procedure TJvMemoryData.CopyStructure(Source: TDataSet; UseAutoIncAsInteger: Boolean);
var
I: Integer;
begin
if Source = nil then
Exit;
CheckInactive;
for I := FieldCount - 1 downto 0 do
Fields[I].Free;
Source.FieldDefs.Update;
FieldDefs := Source.FieldDefs;
if FApplyMode <> amNone then
AddStatusField;
CheckStructure(UseAutoIncAsInteger);
if FApplyMode <> amNone then
HideStatusField;
end;
function TJvMemoryData.LoadFromDataSet(Source: TDataSet; RecordCount: Integer;
Mode: TLoadMode; DisableAllControls: Boolean = True): Integer;
var
SourceActive: Boolean;
MovedCount, I: Integer;
SB, DB: TBookmark;
begin
Result := 0;
if Source = Self then
Exit;
FSaveLoadState := slsLoading;
SourceActive := Source.Active;
if DisableAllControls then
Source.DisableControls;
SB := Source.GetBookmark;
try
if DisableAllControls then
Self.DisableControls;
DB := GetBookmark;
try
Filtered := False;
with Source do
begin
Open;
CheckBrowseMode;
UpdateCursorPos;
end;
if Mode = lmCopy then
begin
Close;
CopyStructure(Source, AutoIncAsInteger);
end;
FreeIndexList;
if not Active then
Open;
CheckBrowseMode;
if RecordCount > 0 then
MovedCount := RecordCount
else
begin
Source.First;
MovedCount := MaxInt;
end;
FAutoIncField := nil;
// FixReadOnlyFields also sets FAutoIncField if there is any
FixReadOnlyFields(False);
// find first source autoinc field
FSrcAutoIncField := nil;
if Mode = lmCopy then
for I := 0 to Source.FieldCount - 1 do
if Source.Fields[I].DataType = ftAutoInc then
begin
FSrcAutoIncField := Source.Fields[I];
Break;
end;
try
while not Source.Eof do
begin
Append;
AssignRecord(Source, Self, True);
// assign AutoInc value manually (make user keep largest if source isn't sorted by autoinc field)
if (FAutoIncField <> nil) and (FSrcAutoIncField <> nil) then
FAutoInc := Max(FAutoInc, FSrcAutoIncField.AsInteger);
Post;
Inc(Result);
if Result >= MovedCount then
Break;
Source.Next;
end;
finally
FixReadOnlyFields(True);
FAutoIncField := nil;
FSrcAutoIncField := nil;
First;
end;
// move back to where we started from
if (DB <> nil) and BookmarkValid(DB) then
begin
GotoBookmark(DB);
FreeBookmark(DB);
end;
finally
if DisableAllControls then
EnableControls;
end;
finally
// move back to where we started from
if (SB <> nil) and Source.BookmarkValid(SB) then
begin
Source.GotoBookmark(SB);
Source.FreeBookmark(SB);
end;
if not SourceActive then
Source.Close;
if DisableAllControls then
Source.EnableControls;
FSaveLoadState := slsNone;
end;
end;
function TJvMemoryData.SaveToDataSet(Dest: TDataSet; RecordCount: Integer; DisableAllControls: Boolean = True): Integer;
var
MovedCount: Integer;
SB, DB: TBookmark;
begin
Result := 0;
if Dest = Self then
Exit;
CheckBrowseMode;
UpdateCursorPos;
if DisableAllControls then
begin
DisableControls;
Dest.DisableControls;
end;
FSaveLoadState := slsSaving;
try
SB := GetBookmark;
DB := Dest.GetBookmark;
try
if not Dest.Active then
Dest.Open
else
Dest.CheckBrowseMode;
if RecordCount > 0 then
MovedCount := RecordCount
else
begin
First;
MovedCount := MaxInt;
end;
try
while not Eof do
begin
Dest.Append;
AssignRecord(Self, Dest, True);
Dest.Post;
Inc(Result);
if Result >= MovedCount then
Break;
Next;
end;
finally
Dest.First;
end;
finally
if (SB <> nil) and BookmarkValid(SB) then
begin
GotoBookmark(SB);
FreeBookmark(SB);
end;
if (DB <> nil) and Dest.BookmarkValid(DB) then
begin
Dest.GotoBookmark(DB);
Dest.FreeBookmark(DB);
end;
end;
finally
if DisableAllControls then
begin
EnableControls;
Dest.EnableControls;
end;
FSaveLoadState := slsNone;
end;
end;
procedure TJvMemoryData.SortOnFields(const FieldNames: string;
CaseInsensitive: Boolean = True; Descending: Boolean = False);
begin
CreateIndexList(FieldNames);
FCaseInsensitiveSort := CaseInsensitive;
FDescendingSort := Descending;
try
Sort;
except
FreeIndexList;
raise;
end;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -