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

📄 daovalueobjectunit.pas

📁 随便说说最近项目中的三层架构吧。讲点实际的东西。我最讨厌空讲道理。网上讲道理的太多了
💻 PAS
字号:
//------------------------------------------------------------------------------
//                           DAO数据存储基类
//1.所有的数据库操作,只有通过DAO类进行存取。
//2.不允许其他模块进行数据存取操作。
//
//------------------------------------------------------------------------------
unit DAOValueObjectUnit;

interface

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

type

  TDAOValueObject= class(TPersistent)
    private
      sSQLSelect: String;
      sSQLMax: String;
      sSQLCount: String;
      sSQLInsert: String;
      sSQLUpdate: String;
      sSQLDelete: String;
      sSQL: String;
      sSQLCode: WideString;

      //主键生成机制:
      maxKey: Integer;
      countKey: Integer;
      realKey: Integer;

      sVOClass: String;
      VO: TValueObject;
      VOList: TValueObjectList;

      RDMDataSet: TRDMDataSet;
      function getSQL: WideString;
      procedure setSQL(tmpSql: WideString);
      function getSQLSelect: WideString;
      procedure setSQLSelect(tmpSql: WideString);
      function getSQLMax: WideString;
      procedure setSQLMax(tmpSql: WideString);
      function getSQLCount: WideString;
      procedure setSQLCount(tmpSql: WideString);
      function getSQLInsert: WideString;
      procedure setSQLInsert(tmpSql: WideString);
      function getSQLUpdate: WideString;
      procedure setSQLUpdate(tmpSql: WideString);
      function getSQLDelete: WideString;
      procedure setSQLDelete(tmpSql: WideString);
      procedure setVOClass(sClass :String);
      function getVOClass: String;
      function getVO: TValueObject;
      function getVOList: TValueObjectList;
      procedure setVOList(tmpVOList: TValueObjectList);
      function getSQLCode: WideString;

    published
      property PVOClass: String read getVOClass write setVOClass;
      property PVOList: TValueObjectList read getVOList write setVOList;
      property PSQL: WideString read getSQL write setSQL;
      property PSQLSelect: WideString read getSQLSelect write setSQLSelect;
      property PSQLMax: WideString read getSQLMax write setSQLMax;
      property PSQLCount: WideString read getSQLCount write setSQLCount;
      property PSQLInsert: WideString read getSQLInsert write setSQLInsert;
      property PSQLUpdate: WideString read getSQLUpdate write setSQLUpdate;
      property PSQLDelete: WideString read getSQLDelete write setSQLDelete;

    public
      constructor Create(VOClass: String); overload;
      function getPrimaryKey(const sSQLMax: String; sSQLCount: String):String;  virtual;
      function createDAOValue: TValueObject; virtual;
      function insertDAOValue(pValueObject: TValueObject): Integer; virtual;
      function updateDAOValue(pValueObject: TValueObject): Integer; virtual;
      function deleteDAOValue(pValueObject: TValueObject): Integer; virtual;
      property pSQLCode: WideString read getSQLCode;
  end;

implementation
  uses DMFrm;

function TDAOValueObject.getSQL: WideString;
begin
  Result := sSQL;
end;

procedure TDAOValueObject.setSQL(tmpSql: WideString);
begin
  sSQL := tmpSql;
end;

function TDAOValueObject.getSQLSelect: WideString;
begin
  Result := sSQLSelect;
end;

procedure TDAOValueObject.setSQLSelect(tmpSql: WideString);
begin
  sSQLSelect := tmpSql;
end;

function TDAOValueObject.getSQLMax: WideString;
begin
  Result := sSQLMax; 
end;

procedure TDAOValueObject.setSQLMax(tmpSql: WideString);
begin
  sSQLMax := tmpSql;
end;

function TDAOValueObject.getSQLCount: WideString;
begin
  Result := sSQLCount;
end;

procedure TDAOValueObject.setSQLCount(tmpSql: WideString);
begin
  sSQLCount := tmpSql;
end;

function TDAOValueObject.getSQLInsert: WideString;
begin
  Result := sSQLInsert;
end;

procedure TDAOValueObject.setSQLInsert(tmpSql: WideString);
begin
  sSQLInsert := tmpSql;
end;

function TDAOValueObject.getSQLUpdate: WideString;
begin
  Result := sSQLUpdate;
end;

procedure TDAOValueObject.setSQLUpdate(tmpSql: WideString);
begin
  sSQLUpdate := tmpSql;
end;

function TDAOValueObject.getSQLDelete: WideString;
begin
  Result := sSQLDelete;
end;

procedure TDAOValueObject.setSQLDelete(tmpSql: WideString);
begin
  sSQLDelete := tmpSql;
end;

procedure TDAOValueObject.setVOClass(sClass :String);
begin
  sVOClass := sClass;
end;

function TDAOValueObject.getVOClass: String;
begin
  Result :=sVOClass;
end;

function TDAOValueObject.getVO: TValueObject;
begin
  Result := VO;
end;

function TDAOValueObject.getVOList: TValueObjectList;
begin
  Result := VOList;
end;

procedure TDAOValueObject.setVOList(tmpVOList: TValueObjectList);
begin
  VOList := tmpVOList;
end;

constructor TDAOValueObject.Create(VOClass: String);
var
  tmpPer: TPersistent;
begin
  inherited Create;
  PVOClass := VOClass;

 // sqlAnySrvObj := TSQLAnalyzeService.Create;
  if not assigned(VOList) then
    PVOList := TValueObjectList.Create;

  if not assigned(RDMDataSet) then
    RDMDataSet := TRDMDataSet.Create(dm.conn);

  VOList.pVOClass := VOClass;

end;

procedure TDAOValueObject.CreateDSAndVOList(tmpDS: TDataSet);
var
  i_i, i_j : integer;
  voParam: Olevariant;
  voFlag: OleVariant;
  voObj: OleVariant;
  procedure InitDataSet(tmpDS: TDataSet);
  begin
    if tmpDS.Fields[i_j].Value = null then
    begin
      tmpDS.Edit;
      if tmpDS.Fields[i_j].DataType in [ftSmallint, ftInteger, ftWord, ftFloat, ftBytes, ftLargeint] then
        tmpDS.Fields[i_j].Value := 0;
      if tmpDS.Fields[i_j].DataType in [ftString, ftWideString] then
        tmpDS.Fields[i_j].Value := '';
      if tmpDS.Fields[i_j].DataType in [ftBoolean] then
        tmpDS.Fields[i_j].Value := false;
      if tmpDS.Fields[i_j].DataType in [ftDate] then
        tmpDS.Fields[i_j].Value := Date;
      if tmpDS.Fields[i_j].DataType in [ftTime] then
        tmpDS.Fields[i_j].Value := time;
      if tmpDS.Fields[i_j].DataType in [ftDateTime] then
        tmpDS.Fields[i_j].Value := Date;
    end;
  end;

begin

  voFlag := VarArrayCreate([0,2], varVariant);   //VO原始数据:
  voFlag[0] := false;
  voFlag[1] := false;
  voFlag[2] := false;
  voParam := VarArrayCreate([0,tmpDS.FieldCount-1],varVariant);     //VO字段数据:
  voObj := VarArrayCreate([0,1],varVariant);   //真正的VO:


  for i_i := 1 to tmpDS.RecordCount do
  begin
    VO := PVOList.NewVO;

    with (PVOList as TDBVOList).pDataSet do
    begin
      Append;

      for i_j :=0 to FieldCount-1 do
      begin
        InitDataSet(tmpDS);   //数据预处理:
        voParam[i_j] := tmpDS.Fields[i_j].Value;
      end;
      voObj[0] := voFlag;
      voObj[1] := voParam;   //数据打包:
      VO.POLEData := voObj;

      post;
    end;

    VOList.AddVO(VO);
    tmpDS.Next;
  end;
end;

function TDAOValueObject.getPrimaryKey(const sSQLMax: String; sSQLCount: String): String;
var
  tmpDS: TDataSet;
  wsPrv: WideString;
begin
  tmpDS := nil;
  sSQLCode := sqlAnySrvObj.getSQLCode(sSQLMax);
  with  frmServer.pLdmDataSet do
  begin
    try
      tmpDS := SelectLDMDS(pSQLCode, wsPrv);
    except
      ShowMessage('数据存取失败!');
    end;
    if not tmpDS.IsEmpty then
      maxKey := tmpDS.Fields[0].Value;
    tmpDS.Close;
  end;

  sSQLCode := sqlAnySrvObj.getSQLCode(sSQLCount);
  with  frmServer.pLdmDataSet do
  begin
    try
      tmpDS := SelectLDMDS(pSQLCode, wsPrv);
    except
      ShowMessage('数据存取失败!');
    end;

    if not tmpDS.IsEmpty then
      countKey := tmpDS.Fields[0].Value;
    tmpDS.Close;
  end;

  with PVOList do
  begin
    if MaxKey > CountKey then
      RealKey := MaxKey
    else
      RealKey := CountKey;
  end;

end;

function TDAOValueObject.createDAOValue: TValueObject;
var
  tmpVO: TValueObject;
  tmpPer: TPersistent;
begin
  if FindClass(pVOClass) <> nil then
  begin
    tmpPer := TPersistentClass(FindClass(pVOClass)).Create;
    tmpVO := TValueObject(tmpPer);
  end
  else
    tmpVO := TValueObject.Create;

  tmpVO.pPrimarykey := IntTOStr(RealKey+1);
  tmpVO.pRowVersion := 0;
  Result := tmpVO;
end;

function TDAOValueObject.insertDAOValue(pValueObject: TValueObject): Integer;
var
  tmpOle: OleVariant;
  oParam: OleVariant;
  i_i: INteger;
begin
  tmpOle := pValueObject.POLEData;
  if not VarArrayHighBound(tmpOle,1)>0 then
    exit
  else
    oParam:= tmpOle[1];

  sSQLCode := sqlAnySrvObj.getSQLCode(PSQLInsert, oParam);
  with  frmServer.pLdmDataSet do
  begin
    try
      Result := InsertLDMDS(pSQLCode);
      getPrimaryKey(PSQLMax, PSQLCount);

      PVOList.AddVO(pValueObject);

    except
      Result := 0;
      ShowMessage('数据存取失败!');
    end;
  end;
end;

function TDAOValueObject.updateDAOValue(pValueObject: TValueObject): Integer;
var
  tmpOle, oParam, pVO: OleVariant;
  i_i: Integer;
begin
  tmpOle := pValueObject.POLEData;
  if not VarArrayHighBound(tmpOle,1)>0 then
    exit
  else
    oParam:= tmpOle[1];

  pVO := VarArrayCreate([0, VarArrayHighBound(oParam,1)], varVariant);
  for i_i:=1 to VarArrayHighBound(oParam,1)-1 do
    pVO[i_i-1] := oParam[i_i];

  pVO[VarArrayHighBound(oParam,1)-1] := oParam[0];
  pVO[VarArrayHighBound(oParam,1)] := oParam[VarArrayHighBound(oParam,1)];

  sSQLCode := sqlAnySrvObj.getSQLCode(PSQLUpdate, pVO);
  with  frmServer.pLdmDataSet do
  begin
    try
      Result := UpdateLDMDS(pSQLCode);
    except
      Result := -1;
      ShowMessage('数据存取失败!');
    end;
  end;

end;

function TDAOValueObject.deleteDAOValue(pValueObject: TValueObject): Integer; 
var
  tmpOle, pVO, oParam: OleVariant;
begin
  tmpOle := pValueObject.POLEData;
  if not VarArrayHighBound(tmpOle,1)>0 then
    exit
  else
    oParam:= tmpOle[1];

  pVO := VarArrayCreate([0, 1], varVariant);
  pVO[0] := oParam[0];
  pVO[1] := oParam[VarArrayHighBound(oParam,1)];

  sSQLCode := sqlAnySrvObj.getSQLCode(PSQLDelete, pVO);
  with  frmServer.pLdmDataSet do
  begin
    try
      Result := DeleteLDMDS(pSQLCode);
      getPrimaryKey(PSQLMax, PSQLCount);
    except
      Result := -1;
      ShowMessage('数据存取失败!');
    end;
  end;

end;

end.

⌨️ 快捷键说明

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