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

📄 companyvo.pas

📁 随便说说最近项目中的三层架构吧。讲点实际的东西。我最讨厌空讲道理。网上讲道理的太多了
💻 PAS
字号:
unit CompanyVO;

interface

uses
  Windows, Classes, SysUtils,  Dialogs, DBClient, DB, Contnrs, Variants, ValueObjectUnit,
  TValueObjectListUnit, DataPersistentUnit;

type
  PTCompanyVO =^ TCompanyVO;
  TCompanyVO = class(TValueObject)
    public
      id: string;
      name: string;
      address: string;
      tel: string;
  end;

  PTCompanyVOList =^ TCompanyVOList;
  TCompanyVOList = Class(TValueObjectList)
    private
      sSQLMax: String;
      sSQLCount: String;
      sSQLInsert: String;
      sSQLUpdate: String;
      sSQLDelete: String;
      sSQL: String;
      rdmDS: TRDMDataSet;
      {
      tmpCDS: TClientDataSet;
      function getDataSet: TClientDataSet;
      procedure SetDataSet(tmpDS: TClientDataSet);  }

    published
     // property pCLientDS: TClientDataSet read getDataSet write SetDataSet;

    public      constructor Create(tmpDS: TRDMDataSet);      procedure AfterConstruction; override;
      destructor Destroy; override;      procedure GetDatabase(tmpCDS: TClientDataSet);
      function InsertToBase(comVO: TCompanyVO): boolean;
      function UpdateDatabase(comVO: TCompanyVO): boolean;
      function DeleteForBase(comVO: TCompanyVO): boolean;

   end;


implementation

constructor TCompanyVOList.Create(tmpDS: TRDMDataSet);
begin
  inherited Create;
  rdmDS := tmpDS;
  sSQL := 'select guid as 主键, Dno as 公司ID号, Dname as 公司名称, Daddr as 公司地址, DTel as 公司电话, RowVer as 版本号 from Company';
  sSQLMax := 'select isnull(convert(char(10), max(convert(int, guid))), count(*)) from Company';
  sSQLCount :='select count(*) from Company';
  sSQLInsert :='Insert into Users (guid, Dno, Dname, Daddr, DTel, RowVer) Values (''%s'', ''%s'',''%s'',''%s'',''%s'',''%s'')';
  sSQLUpdate :='update Company set Dno=''%s'', Dname=''%s'', Daddr=''%s'', DTel=''%s'' where guid =''%s'' and RowVer=''%s'' ';
  sSQLDelete :='Delete Company where guid =''%s'' and RowVer=''%s'' ';
end;

procedure TCompanyVOList.AfterConstruction;
begin
  inherited;
end;

destructor TCompanyVOList.Destroy;
begin
  inherited;
end;

procedure TCompanyVOList.GetDatabase(tmpCDS: TClientDataSet);
var
  i_i: Integer;
  wsSQL: WideString;
begin

  wsSQL := sSQL;
  try
    rdmDS.SelectRDMDS(wsSQL, tmpCDS);
  except
    tmpCDS := nil;
    ShowMessage('数据存取失败!');
  end;

  //添加到voList中
  for i_i:=0 to tmpCDS.RecordCount-1 do
  begin
    PValueObject := TCompanyVO.Create;
    (pValueObject as TCompanyVO).pPrimarykey := tmpCDS.Fields[0].Value;
    (pValueObject as TCompanyVO).id:= tmpCDS.Fields[1].Value;
    (pValueObject as TCompanyVO).name := tmpCDS.Fields[2].Value;
    (pValueObject as TCompanyVO).address := tmpCDS.Fields[3].Value;
    (pValueObject as TCompanyVO).tel:= tmpCDS.Fields[4].Value;
    (pValueObject as TCompanyVO).pRowVersion := tmpCDS.Fields[5].Value;
    AddVO(PValueObject);
    tmpCDS.Next;

  end;

end;

function TCompanyVOList.InsertToBase(comVO: TCompanyVO): boolean;
var
  i_i: Integer;
  wsSQL: WideString;
  wsMsg: WideString;
begin
  wsSQL := ForMat(sSQLInsert, [comVO.pPrimarykey, comVO.id, comVO.name, comVO.address, comVO.tel, comVo.pRowVersion]);

  try
    if not rdmDS.InsertRDMDS(wsSQL, wsMsg) then
    begin
      ShowMessage(wsMsg);
      exit;
    end;
  except
    ShowMessage('数据存取失败!');
    exit;
  end;

  AddVO(comVO);

end;

function TCompanyVOList.UpdateDatabase(comVO: TCompanyVO): boolean;
var
  i_i: Integer;
  wsSQL: WideString;
  wsMsg: WideString;
begin
  wsSQL := ForMat(sSQLUpdate, [comVO.id, comVO.name, comVO.address, comVO.tel, comVO.pPrimarykey, comVo.pRowVersion]);

  try
    if not rdmDS.UpdateRDMDS(wsSQL, wsMsg) then
    begin
      ShowMessage(wsMsg);
      exit;
    end;
  except
    ShowMessage('数据存取失败!');
    exit;
  end;

  if findVO(comVO)<>-1 then
     SetVo(comVO);
end;

function TCompanyVOList.DeleteForBase(comVO: TCompanyVO): boolean;
var
  i_i: Integer;
  wsSQL: WideString;
  wsMsg: WideString;
  i_rtn: Integer;
begin
  wsSQL := ForMat(sSQLDelete, [comVO.pPrimarykey, comVo.pRowVersion]);

  try
    if not rdmDS.DeleteRDMDS(wsSQL, wsMsg) then
    begin
      ShowMessage(wsMsg);
      exit;
    end;
  except
    ShowMessage('数据存取失败!');
    exit;
  end;

  i_rtn := findVO(comVO);
  if i_rtn<>-1 then
     DelVO(i_rtn, comVO);

end;



end.

⌨️ 快捷键说明

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