TPersonBasVO = class(TDataTransferObject)
private
FDno: string;
//单位编号
FSdno: String;
//售电点编号
FSecno: String;
//区域编号
FYhh: string;
//用户号
FYhbm: String;
//用户编码
FDjbm: String;
//多级编码
FDwdjbm: String;
//电网多级编码
FYhm: string;
//用户名
FAddr: String;//地址
FDeptName:string;//单位名称
FTel: String;
//电话
FPbbz: Integer;
//配表标志
FXgcs: Integer;
//限购次数
FGdsx: Integer;
//购电上限
Fstatues: Boolean;
//状态
FKhrq: TDateTime;
//开户日期
FFtlb: String;//分摊类别
FFtb: Real;//分摊比
FFtzp: Real;
//分摊电价
Fftlbname: String;
//分摊名称
published
Property PK_Dno : String index 0 Read FDno Write FDno;
Property PK_Sdno : String index 1 Read FSdno Write FSdno;
Property PK_Secno : String index 2 Read FSecno Write FSecno;
Property PK_Yhh : String index 3 Read FYhh Write FYhh;
Property Yhbm : String index 4 Read FYhbm Write FYhbm;
Property Djbm : String index 5 Read FDjbm Write FDjbm;
Property Dwdjbm : String index 6 Read FDwdjbm Write FDwdjbm;
Property Yhm : String index 7 Read FYhm Write FYhm;
Property Addr : String index 8 Read FAddr Write FAddr;
Property DeptName : String index 9 Read FDeptName Write FDeptName;
Property Tel : String index 10 Read FTel Write FTel;
Property Pbbz : Integer index 11 Read FPbbz Write FPbbz;
Property Xgcs : Integer index 12 Read FXgcs Write FXgcs;
Property Gdsx : Integer index 13 Read FGdsx Write FGdsx;
Property statues : Boolean index 14 Read Fstatues Write Fstatues;
Property Khrq : TDateTime index 15 Read FKhrq Write FKhrq;
Property Ftlb : String index 16 Read FFtlb Write FFtlb;
Property Ftb : Real index 17 Read FFtb Write FFtb;
Property Ftzp : Real index 18 Read FFtzp Write FFtzp;
Property ftlbname : String index 19 Read Fftlbname Write Fftlbname;
public
constructor Create;
end;
TCardBasVO = class(TDataTransferObject)
public
FYhh: string;
//用户号
FYhcard: String;
//用户卡号
FMid: String;
//表类型
FModel: String;
//表型号
FMeter_no: String;
//表编号
FMorder: Integer;
//表顺序号
FPno: String;// 价格编号
FBds:do
uble;
//表底度
FCnst: Integer;
//脉冲常数
FBb: Integer;
//变比
FGmodel: String;
//型号规格
FMdate: TDateTime;
//建档日期
FFtlb: String;//分摊类别
FFtb: Real;//分摊比
published
Property PK_Yhh : String index 0 Read FYhh Write FYhh;
Property PK_Yhcard : String index 1 Read FYhcard Write FYhcard;
Property Mid : String index 2 Read FMid Write FMid;
Property Model : String index 3 Read FModel Write FModel;
Property Meter_no : String index 4 Read FMeter_no Write FMeter_no;
Property PK_Morder : Integer index 5 Read FMorder Write FMorder;
Property Pno : String index 6 Read FPno Write FPno;
Property Bds :do
uble index 7 Read FBds Write FBds;
Property Cnst : Integer index 8 Read FCnst Write FCnst;
Property Bb : Integer index 9 Read FBb Write FBb;
Property Gmodel : String index 10 Read FGmodel Write FGmodel;
Property Mdate : TDateTime index 11 Read FMdate Write FMdate;
Property Ftlb : String index 12 Read FFtlb Write FFtlb;
Property Ftb : Real index 13 Read FFtb Write FFtb;
public
constructor Create;
end;
//明细表:
TDetail = class(TDataTransferObject)
public
person: TPersonBasVO;
card: TCardBasVO;
published
Property Fperson : TPersonBasVO index 0 Read person Write person;
Property Fcard : TCardBasVO index 1 Read card Write card;
public
constructor Create(tmpPer: TPersonBasVO;
tmpCard: TCardBasVO);
end;
//明细表测试
{
personVO.PK_Dno := '001';
Detail := TDetail.Create(personVO, CardVO);
rdmDS.SelectRDMDS(Detail, ClientDataSet2);
}
我的SQL是根据关系映射的。支持OO,多表,视图关联之内,解决很方便,喜欢的话和我联系。
来自:wu_yanan2003, 时间:2006-5-11 14:50:16, ID:3439345 | 编辑
感觉能从底层控制SQL语句是比较好的。
一个比较简单的O/P MAPING,有兴趣你可以扩充它哟。
主要的核心代码如下,DAOAdapter根据VO生成SQL语句。支持OO。DAO模式,不用我多介绍了吧。不了解可以去看看JAVA。另外通过AOP对 DAO模板进行拦截,依赖注入与控制反转,程序运行时,从一个数据库连接池获得一个连接,注入模板中。彻底实现数据库的移植问题。有兴趣可以和我讨论。
interface
Uses
Classes, DB, Contnrs, SysUtils, uDataTransferObject;
type
TDAOAdapter = class(TPersistent)
public
function GetFldName(tmpFld: String): String;
function IsKeyFld(tmpFld: String): Boolean;
function getKeySQLString(Const pTO: TDataTransferObject): String;
function getMaxCountString(Const pTO: TDataTransferObject): String;
function getSearchSQLString(Const pTO: TDataTransferObject): String;
function getInsertSQLString(Const pTO: TDataTransferObject): String;
function getUpdateSQLString(Const pTO: TDataTransferObject): String;
function getDeleteSQLString(Const pTO: TDataTransferObject): String;
constructor Create;
destructor Destroy;
override;
end;
implementation
uses uMPropList, TypInfo, dialogs;
constructor TDAOAdapter.Create;
begin
inherited Create;
end;
function TDAOAdapter.GetFldName(tmpFld: String): String;
begin
if Uppercase(copy(tmpFld, 1, 3)) = 'PK_' then
Result := Copy(tmpFld, 4, length(tmpFld))
else
if Uppercase(tmpFld) = 'TBNAME' then
Result := ''
else
Result := tmpFld;
end;
function TDAOAdapter.IsKeyFld(tmpFld: String): Boolean;
begin
if Uppercase(copy(tmpFld, 1, 3)) = 'PK_' then
Result := True
else
Result := False;
end;
function TDAOAdapter.getKeySQLString(Const pTO: TDataTransferObject): String;
var
i: Integer;
tmpSQL, tmpFid, tmpTbname: String;
tmpPropList: TMPropList;
filter: string;
begin
tmpSQL := 'select count(*) as mySum from ';
tmpPropList := pTO.FPropList;
tmpTbname := pTO.tbName;
tmpSQL := tmpSQL + tmpTbname+ ' where 1=1 and ';
for i:=0 to tmpPropList.PropCount-1do
begin
if (GetFldName(tmpPropList.PropNames)<>'') and (IsKeyFld(tmpPropList.PropNames) = true) then
filter := filter + GetFldName(tmpPropList.PropNames)+'='
else
Continue;
case tmpPropList.Props^.PropType^.Kind of
tkInteger:
if GetFldName(tmpPropList.PropNames)<>'' then
filter := filter +IntToStr( GetOrdProp(pTO, tmpPropList.Props) )+' and ';
tkInt64:
if GetFldName(tmpPropList.PropNames)<>'' then
filter := filter +IntToStr( GetInt64Prop(pTO, tmpPropList.Props) )+' and ';
tkChar, tkLString, tkString:
if GetFldName(tmpPropList.PropNames)<>'' then
filter := filter +QuotedStr( GetStrProp(pTO, tmpPropList.Props) )+' and ';
tkSet:
if GetFldName(tmpPropList.PropNames)<>'' then
filter := filter +QuotedStr( GetSetProp(pTO, tmpPropList.Props) )+' and ';
tkEnumeration:
if GetFldName(tmpPropList.PropNames)<>'' then
begin
if GetEnumProp(pTO, tmpPropList.Props) = 'True' then
filter := filter + QuotedStr('1')+' and ';
if GetEnumProp(pTO, tmpPropList.Props) = 'False' then
filter := filter + QuotedStr('0')+' and ';
if GetEnumProp(pTO, tmpPropList.Props) = '' then
filter := filter + QuotedStr('0')+' and ';
end;
tkFloat:
if GetFldName(tmpPropList.PropNames)<>'' then
filter := filter +FloatToStr( GetFloatProp(pTO, tmpPropList.Props) )+' and ';
tkWChar, tkWString:
if GetFldName(tmpPropList.PropNames)<>'' then
filter := filter +QuotedStr( GetWideStrProp(pTO, tmpPropList.Props) )+' and ';
end;
end;
Result := Copy(tmpSQL+filter,0, length(tmpSQL+filter)-4);
end;
function TDAOAdapter.getMaxCountString(Const pTO: TDataTransferObject): String;
begin
Result := 'select count(*) as mySum from '+pTO.tbName+ ' where 1=1 ';
end;
function TDAOAdapter.getSearchSQLString(Const pTO: TDataTransferObject): String;
var
i,j: Integer;
tmpSQL, tmpFid, tmpTbname: String;
tmpPropList, tmpProp2: TMPropList;
tmpObj: TObject;
tmpInfo: TTypeInfo;
begin
tmpSQL := 'select ';
if not pTO.isDetail then
begin
//单表情况:
tmpPropList := pTO.FPropList;
tmpTbname := pTO.tbName;
for i:=0 to tmpPropList.PropCount-1do
begin
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL + GetFldName(tmpPropList.PropNames) +','
end;
tmpSQL := Copy(tmpSQL,0, length(tmpSQL)-1);
tmpSQL := tmpSQL +' FROM '+tmpTbname+' where 1=1';
Result := tmpSQL;
end
else
begin
//明细表情况:
tmpPropList := pTO.FPropList;
tmpSQL := 'select ';
for i:=0 to tmpPropList.PropCount-1do
begin
case tmpPropList.Props^.PropType^.Kind of
tkClass:
begin
tmpObj := GetObjectProp(pTO, tmpPropList.Props);
tmpProp2 := TDataTransferObject(tmpObj).FPropList;
tmpTbname := TDataTransferObject(tmpObj).tbName;
for j:=0 to tmpProp2.PropCount-1do
begin
if GetFldName(tmpProp2.PropNames[j])<>'' then
tmpSQL := tmpSQL + tmpTbname +'.'+GetFldName(tmpProp2.PropNames[j]) +','
end;
end;
end;
end;
tmpSQL := Copy(tmpSQL,0, length(tmpSQL)-1);
tmpSQL := tmpSQL + ' FROM ';
for i:=0 to tmpPropList.PropCount-1do
begin
case tmpPropList.Props^.PropType^.Kind of
tkClass:
begin
tmpObj := GetObjectProp(pTO, tmpPropList.Props);
tmpProp2 := TDataTransferObject(tmpObj).FPropList;
tmpTbname := TDataTransferObject(tmpObj).tbName;
tmpSQL := tmpSQL + tmpTbname+',';
end;
end;
end;
tmpSQL := Copy(tmpSQL,0, length(tmpSQL)-1);
tmpSQL := tmpSQL + ' Where '+pTO.IndexInfo;
Result := tmpSQL;
end;
end;
function TDAOAdapter.getInsertSQLString(Const pTO: TDataTransferObject): String;
var
i: Integer;
tmpSQL, tmpTbname: String;
tmpPropList: TMPropList;
begin
tmpPropList := pTO.FPropList;
tmpTbname := pTO.tbName;
tmpSQL := 'insert '+tmpTbname+' (';
for i:=0 to tmpPropList.PropCount-1do
begin
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL + GetFldName(tmpPropList.PropNames)+','
end;
tmpSQL := Copy(tmpSQL,0, length(tmpSQL)-1)+') values(';
for i:=0 to tmpPropList.PropCount-1do
begin
case tmpPropList.Props^.PropType^.Kind of
tkInteger:
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL +IntToStr( GetOrdProp(pTO, tmpPropList.Props) )+',';
tkInt64:
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL +IntToStr( GetInt64Prop(pTO, tmpPropList.Props) )+',';
tkChar, tkLString, tkString:
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL +QuotedStr( GetStrProp(pTO, tmpPropList.Props) )+',';
tkSet:
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL +QuotedStr( GetSetProp(pTO, tmpPropList.Props) )+',';
tkEnumeration:
if GetFldName(tmpPropList.PropNames)<>'' then
begin
if GetEnumProp(pTO, tmpPropList.Props) = 'True' then
tmpSQL := tmpSQL + QuotedStr('1')+',';
if GetEnumProp(pTO, tmpPropList.Props) = 'False' then
tmpSQL := tmpSQL + QuotedStr('0')+',';
if (GetEnumProp(pTO, tmpPropList.Props) = '') then
tmpSQL := tmpSQL + QuotedStr('0')+',';
end;
tkFloat:
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL +FloatToStr( GetFloatProp(pTO, tmpPropList.Props) )+',';
tkWChar, tkWString:
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL +QuotedStr( GetWideStrProp(pTO, tmpPropList.Props) )+',';
end;
end;
Result := Copy(tmpSQL,0, length(tmpSQL)-1)+')';
end;
function TDAOAdapter.getUpdateSQLString(Const pTO: TDataTransferObject): String;
var
i: Integer;
tmpSQL, tmpTbname: String;
tmpPropList: TMPropList;
filter: String;
begin
tmpPropList := pTO.FPropList;
tmpTbname := pTO.tbName;
tmpSQL := 'Update '+tmpTbname+' set ';
for i:=0 to tmpPropList.PropCount-1do
begin
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL + GetFldName(tmpPropList.PropNames)+'=';
case tmpPropList.Props^.PropType^.Kind of
tkInteger:
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL +IntToStr( GetOrdProp(pTO, tmpPropList.Props) )+',';
tkInt64:
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL +IntToStr( GetInt64Prop(pTO, tmpPropList.Props) )+',';
tkChar, tkLString, tkString:
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL +QuotedStr( GetStrProp(pTO, tmpPropList.Props) )+',';
tkSet:
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL +QuotedStr( GetSetProp(pTO, tmpPropList.Props) )+',';
tkEnumeration:
if GetFldName(tmpPropList.PropNames)<>'' then
begin
if GetEnumProp(pTO, tmpPropList.Props) = 'True' then
tmpSQL := tmpSQL + QuotedStr('1')+',';
if GetEnumProp(pTO, tmpPropList.Props) = 'False' then
tmpSQL := tmpSQL + QuotedStr('0')+',';
if (GetEnumProp(pTO, tmpPropList.Props) = '') then
tmpSQL := tmpSQL + QuotedStr('0')+',';
end;
tkFloat:
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL +FloatToStr( GetFloatProp(pTO, tmpPropList.Props) )+',';
tkWChar, tkWString:
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL +QuotedStr( GetWideStrProp(pTO, tmpPropList.Props) )+',';
end;
end;
tmpSQL := Copy(tmpSQL,0, length(tmpSQL)-1);
//主键产生Update语句
filter := ' Where 1=1 and ';
for i:=0 to tmpPropList.PropCount-1do
begin
if (GetFldName(tmpPropList.PropNames)<>'') and (IsKeyFld(tmpPropList.PropNames) = true) then
filter := filter + GetFldName(tmpPropList.PropNames)+'='
else
Continue;
case tmpPropList.Props^.PropType^.Kind of
tkInteger:
if GetFldName(tmpPropList.PropNames)<>'' then
filter := filter +IntToStr( GetOrdProp(pTO, tmpPropList.Props) )+' and ';
tkInt64:
if GetFldName(tmpPropList.PropNames)<>'' then
filter := filter +IntToStr( GetInt64Prop(pTO, tmpPropList.Props) )+' and ';
tkChar, tkLString, tkString:
if GetFldName(tmpPropList.PropNames)<>'' then
filter := filter +QuotedStr( GetStrProp(pTO, tmpPropList.Props) )+' and ';
tkSet:
if GetFldName(tmpPropList.PropNames)<>'' then
filter := filter +QuotedStr( GetSetProp(pTO, tmpPropList.Props) )+' and ';
tkEnumeration:
if GetFldName(tmpPropList.PropNames)<>'' then
begin
if GetEnumProp(pTO, tmpPropList.Props) = 'True' then
filter := filter + QuotedStr('1')+' and ';
if GetEnumProp(pTO, tmpPropList.Props) = 'False' then
filter := filter + QuotedStr('0')+' and ';
if (GetEnumProp(pTO, tmpPropList.Props) = '') then
filter := filter + QuotedStr('0')+' and ';
end;
tkFloat:
if GetFldName(tmpPropList.PropNames)<>'' then
filter := filter +FloatToStr( GetFloatProp(pTO, tmpPropList.Props) )+' and ';
tkWChar, tkWString:
if GetFldName(tmpPropList.PropNames)<>'' then
filter := filter +QuotedStr( GetWideStrProp(pTO, tmpPropList.Props) )+' and ';
end;
end;
Result := Copy(tmpSQL+filter,0, length(tmpSQL+filter)-4);
end;
function TDAOAdapter.getDeleteSQLString(Const pTO: TDataTransferObject): String;
var
i: Integer;
tmpSQL, tmpTbname: String;
tmpPropList: TMPropList;
begin
tmpPropList := pTO.FPropList;
tmpTbname := pTO.tbName;
tmpSQL := 'Delete '+tmpTbname+' where 1=1 ';
for i:=0 to tmpPropList.PropCount-1do
begin
if not( (GetFldName(tmpPropList.PropNames)<>'') and (IsKeyFld(tmpPropList.PropNames) = true)) then
Continue;
case tmpPropList.Props^.PropType^.Kind of
tkInteger:
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL +' and '+ GetFldName(tmpPropList.PropNames)+'='+IntToStr( GetOrdProp(pTO, tmpPropList.Props) );
tkInt64:
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL +' and '+ GetFldName(tmpPropList.PropNames)+'='+IntToStr( GetInt64Prop(pTO, tmpPropList.Props) );
tkChar, tkLString, tkString:
if not (Trim( GetStrProp(pTO, tmpPropList.Props) ) = '') and (GetFldName(tmpPropList.PropNames)<>'') then
tmpSQL := tmpSQL +' and '+ GetFldName(tmpPropList.PropNames)+'='+QuotedStr( GetStrProp(pTO, tmpPropList.Props) );
tkSet:
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL +' and '+ GetFldName(tmpPropList.PropNames)+'='+QuotedStr( GetSetProp(pTO, tmpPropList.Props) );
tkEnumeration:
if GetFldName(tmpPropList.PropNames)<>'' then
begin
if GetEnumProp(pTO, tmpPropList.Props) = 'True' then
tmpSQL := tmpSQL +' and '+ GetFldName(tmpPropList.PropNames)+'='+QuotedStr('1');
if GetEnumProp(pTO, tmpPropList.Props) = 'False' then
tmpSQL := tmpSQL +' and '+ GetFldName(tmpPropList.PropNames)+'='+QuotedStr('0');
if (GetEnumProp(pTO, tmpPropList.Props) = '') then
tmpSQL := tmpSQL +' and '+ GetFldName(tmpPropList.PropNames)+'='+QuotedStr('0');
end;
tkFloat:
if GetFldName(tmpPropList.PropNames)<>'' then
tmpSQL := tmpSQL +' and '+ GetFldName(tmpPropList.PropNames)+'='+FloatToStr( GetFloatProp(pTO, tmpPropList.Props) );
tkWChar, tkWString:
if not (Trim( GetStrProp(pTO, tmpPropList.Props) ) = '') and (GetFldName(tmpPropList.PropNames)<>'') then
tmpSQL := tmpSQL +' and '+ GetFldName(tmpPropList.PropNames)+'='+QuotedStr( GetWideStrProp(pTO, tmpPropList.Props) );
end;
end;
Result := tmpSQL;
end;
destructor TDAOAdapter.Destroy;
begin
inherited Destroy;
end;
end.