📄 s2.model.ts2datamodel.pas
字号:
unit S2.Model.TS2DataModel;
interface
uses
Windows, Messages, DB, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Types, S2.Model.TS2Model, S2.Tools.TS2Field, S2.Tools.TS2Record,
S2.Model.IS2DataObject, S2.Tools.TS2RecordList;
type
TS2DataModel = class(TS2Model, IS2DataObject)
public
function GetDataSet: TDataSet; virtual;
function IsActive: Boolean; override;
function IsReadOnly: Boolean; override;
function IsValid: Boolean; override;
protected
function LocateByKey(Data: TS2Record; const IsPartialKey: Boolean): Boolean;
procedure BindingData(Data: TS2Field); overload;
procedure BindingData(Data: TS2Record); overload;
public // S2.Model.IS2DataObject
function GetCaption: string; override;
function GetCount: Integer; virtual;
function GetData: TS2Record; virtual;
function GetDataByChoose: TS2RecordList; virtual;
function GetDataByFieldName(const FieldName: string): TS2Field; overload; virtual;
function GetDataByFieldName(FieldName: TStringDynArray): TS2Record; overload; virtual;
function GetDataByKey(Data: TS2Record): TS2Record; virtual;
function GetPrimaryKey: TS2Record; virtual;
function ValidateData(Data: TS2Record): Boolean; overload; virtual;
function ValidateData: Boolean; overload; virtual;
private
function FindField(const FieldName: string): Boolean;
function Check(Field: TField): Boolean;
end;
implementation
uses
Dialogs,
S2.Error.S2AbstractException, S2.Error.S2DataBindingException, S2.Tools.TS2FieldFactory,
S2.Error.S2Exception, S2.Error.S2ParameterNullException, S2.Form.TS2DialogFactory,
S2.Tools.TS2StringField, S2.Tools.TS2MoneyField, S2.Tools.TS2IntegerField, S2.Tools.TS2DoubleField,
S2.Tools.TS2DateField, S2.Error.S2FieldTypeError, S2.Error.S2FieldNotFound;
{$R *.dfm}
{ TS2DataModel }
procedure TS2DataModel.BindingData(Data: TS2Record);
var
I: Integer;
begin
for I := 0 to Data.GetFieldCount - 1 do
BindingData(Data.GetField(Data.GetFieldName(I)));
end;
procedure TS2DataModel.BindingData(Data: TS2Field);
var
Field: TS2Field;
begin
Field := TS2Field(Data);
if not FindField(Data.GetFieldName) then
raise S2FieldNotFound.Create(ClassName, 'BindingData', Data.GetFieldName);
if Field is TS2StringField then
GetDataSet.FieldByName(Field.GetFieldName).AsString := TS2StringField(Field).GetValue
else if Field is TS2MoneyField then
GetDataSet.FieldByName(Field.GetFieldName).AsCurrency := TS2MoneyField(Field).GetValue.GetValue
else if Field is TS2IntegerField then
GetDataSet.FieldByName(Field.GetFieldName).AsInteger := TS2IntegerField(Field).GetValue
else if Field is TS2DoubleField then
GetDataSet.FieldByName(Field.GetFieldName).AsFloat := TS2DoubleField(Field).GetValue
else if Field is TS2DateField then
GetDataSet.FieldByName(Field.GetFieldName).AsDateTime := TS2DateField(Field).GetValue.GetValue
else
raise S2FieldTypeError.Create(ClassName, 'BindingData');
end;
function TS2DataModel.GetCaption: string;
begin
raise S2AbstractException.Create(ClassName, 'GetCaption');
end;
function TS2DataModel.GetCount: Integer;
begin
Result := GetDataSet.RecordCount;
end;
function TS2DataModel.GetData: TS2Record;
var
I: Integer;
begin
if IsValid then
begin
Result := TS2Record.Create;
for I := 0 to GetDataSet.FieldCount - 1 do
Result.AddField(TS2FieldFactory.GetField(GetDataSet.Fields[I]));
end;
end;
function TS2DataModel.GetDataByChoose: TS2RecordList;
begin
raise S2AbstractException.Create(ClassName, 'GetDataByChoose');
end;
function TS2DataModel.GetDataByFieldName(FieldName: TStringDynArray): TS2Record;
var
I: Integer;
begin
if Length(FieldName) < 1 then raise S2ParameterNullException.Create(ClassName, 'GetDataByFieldName', 'FieldName');
try
Result := TS2Record.Create;
for I := Low(FieldName) to High(FieldName) do
Result.AddField(GetDataByFieldName(FieldName[0]));
except
FreeAndNil(Result);
raise;
end;
end;
function TS2DataModel.GetDataByFieldName(const FieldName: string): TS2Field;
begin
Result := TS2FieldFactory.GetField(GetDataSet.FieldByName(FieldName));
end;
function TS2DataModel.GetDataByKey(Data: TS2Record): TS2Record;
begin
Result := nil;
if LocateByKey(Data, False) then
Result := GetData;
end;
function TS2DataModel.GetDataSet: TDataSet;
begin
raise S2AbstractException.Create(ClassName, 'GetDataSet');
end;
function TS2DataModel.GetPrimaryKey: TS2Record;
begin
raise S2AbstractException.Create(ClassName, 'GetPrimaryKey');
end;
function TS2DataModel.IsActive: Boolean;
begin
Result := GetDataSet.Active;
end;
function TS2DataModel.IsReadOnly: Boolean;
begin
Result := False;
end;
function TS2DataModel.ValidateData: Boolean;
var
Index: Integer;
begin
Result := False;
with GetDataSet do
begin
First;
for Index := 0 to Fields.Count - 1 do
if Fields[Index].CurValue <> Fields[Index].NewValue then
if not Check(Fields[Index]) then Exit;
Next;
end;
Result := True;
end;
function TS2DataModel.ValidateData(Data: TS2Record): Boolean;
begin
raise S2AbstractException.Create(ClassName, 'ValidateData(Data: IS2Record)');
end;
function TS2DataModel.IsValid: Boolean;
begin
Result := IsActive and not GetDataSet.IsEmpty;
end;
function TS2DataModel.LocateByKey(Data: TS2Record; const IsPartialKey: Boolean): Boolean;
begin
raise S2AbstractException.Create(ClassName, 'LocateByKey');
end;
function TS2DataModel.FindField(const FieldName: string): Boolean;
begin
Result := GetDataSet.FindField(FieldName) <> nil;
end;
function TS2DataModel.Check(Field: TField): Boolean;
begin
Result := False;
if Field.Required and Field.IsNull then
begin
TS2DialogFactory.ShowMessage('请给''' + Field.DisplayName + '''指定一个有效值.');
Exit;
end;
if (Field.DataType = ftString) and (Length(Field.AsString) > Field.Size) then
begin
TS2DialogFactory.ShowMessage('''' + Field.DisplayName + '''的长度应该小于或等于' + IntToStr(Field.Size) + '位.');
Exit;
end;
Result := True;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -