1、支持ORM,最基础的两个信息是表的信息和字段信息。这两个信息,如果用Attribute 来辅助,代码更简洁和可读性更好。可以把属性名当做真实字段名,也可以将特性里的属性当成真实姓名,再加上字段标题(可以当成注释)、必填字段、是否为主键、显示格式等等,如果没有Attribute ,类、属性的辅助信息必须用其他信息来描述,非常麻烦。

uses
SysUtils, RTTI, TypInfo,Types;

type
Table = class(TCustomAttribute)
private
FName: string;
FTitle: string;
published

public
constructor Create(ATableName, ATitle: string);
property Name: string read FName write FName;
property Title: string read FTitle write FTitle;
end;

FieldInfo = class(TCustomAttribute)
private
FFieldName: string;
FTitle: string;
published
public
constructor Create(AFieldName, ATitle: string);
//字段名
property FieldName: string read FFieldName write FFieldName;
//标题
property Title: string read FTitle write FTitle;
end;

2、有了这两个Attribute,我们必须创建一个解析属性和Attribute的类,并且能解析Insert、update、delete、select等SQL语句。我们姑且叫 TStorable。这个类可以根据需要扩展你所想要的东西。目前只实现了Insert方法,其他的方法,留给勤奋的人去遐想。

TStorable = class

public
//插入SQL语句
function Insert: string;
//获取字段标题
function GetFieldTitle(const AFieldName: string): string;
//设置
//function SetAttributeValue(const PropName, AttributeValue: string): Boolean;
end;

function TStorable.GetFieldTitle(const AFieldName: string): string;
var
Context: TRttiContext;
typ: TRttiType;
A1, A2: TCustomAttribute;
Prop: TRttiProperty;
begin
Context := TRttiContext.Create;
try
typ := Context.GetType(ClassType);
for Prop in typ.GetProperties do
begin
for A2 in Prop.GetAttributes do
begin
if (A2 is FieldInfo) and SameText(FieldInfo(A2).FieldName, AFieldName) then
begin
Result := FieldInfo(A2).Title;
Break;
end;
end;
end;
finally
Context.Free;
end;
end;

function TStorable.Insert: string;
var
Context:TRttiContext;
Prop:TRttiProperty;
typ:TRttiType;
A1,A2:TCustomAttribute;
Sqls,Fields,Values,Value:string;

begin
Context := TRttiContext.Create;
try
Sqls := '';
Fields := '';
Values := '';

typ := Context.GetType(ClassType);
for A1 in typ.GetAttributes do
begin
if A1 is Table then
begin
Sqls := 'Insert Into '+Table(A1).Name; //获取Insert表名
for Prop in typ.GetProperties do
begin
for A2 in Prop.GetAttributes do
begin
if A2 is FieldInfo then //AHa
begin
Fields := Fields + ','+ FieldInfo(A2).FieldName ;
// the value of the attribute
Value := Prop.GetValue(Self).ToString;
//根据数据类型对属性值加以编辑
case Prop.GetValue(Self).Kind of
tkString, tkChar, tkWChar, tkWString, tkUString:
Value := QuotedStr(Value);
tkInteger, tkInt64, tkFloat:
Value := Value;
else
Value := QuotedStr(Value);
end;
Values := Values + ',' + Value ;
end; //for A2 in Prop.GetAttributes
end;
end; //enf of for Prop
Delete(Fields,1,1);
Delete(Values,1,1);

Sqls := Sqls + ' (' + Fields + ') VALUES (' + Values + ');';

Result := Sqls;

end; //if A1 is Table then
end; //for A1 in typ.GetAttributes do

finally
Context.Free;
end;
end;

constructor FieldInfo.Create(AFieldName, ATitle: string);
begin
FFieldName := AFieldName;
FTitle := ATitle;
end;

3、有了上面的解析类和SQL基础,我们必须创建一个实体类。属性名是否为中文,可以有不同的说法。偶目前栖身在一个医疗行业公司,医疗专业英语术语又臭又长,奥巴马未必能拼写出几个术语。如果用属性名用中文描述,将其真实的字段名放在Attribute 里,或许更能提高程序的可读性和维护性。

unit uContact;

interface
uses SysUtils,uAttribute;

type
[Table('CONTACTS','联系人信息')]
TContact = class(TStorable)
private
FName: string;
FAge: integer;
F电话: string;
published
public
[FieldInfo('NAME','名称')]
property Name: string read FName write FName;
[FieldInfo('AGE','年龄')]
property Age: integer read FAge write FAge;
[FieldInfo('电话','联系电话')]
property 电话:string read F电话 write F电话; //尝试一下中文字段名,习惯就好
end;
implementation

end.

4、调用示例就很简单了:

procedure TForm4.btn1Click(Sender: TObject);
var
Contact:TContact;
begin
Contact := TContact.Create;
Contact.Age := 32;
Contact.Name := 'TinTin';
Contact.电话 := '135*****918';//你还会记得918的屈辱吗?

ShowMessage(Contact.Insert);

ShowMessage(Contact.GetFieldTitle('Age'));
Contact.Free;
end;

5、综述:

ORM确实在对象映射上使用起来非常方便,但并非万能,如果过分依赖于ORM,不仅不能了解数据库表与业务的关系,而且还容易写出低效的SQL查询语句。Update语句,须谨记,字段值变化才去更改,否则,会增加数据库的数据不一致风险及其增加数据库日志开销。Delete语句,配合有关键字信息的Attribute,必要时候,还要校验是否影响单条或多条记录。

这只是一个简单的例子,离真正的生产力还差一步,为了执行SQL语句,你可以在TStorable 实现数据集的读写,然后才调用执行SQL语句。

转载于:https://www.cnblogs.com/MaxWoods/p/3814805.html

Delphi2010 RTTI + Attribute 简单实现ORM实例相关推荐

  1. 《简明电路分析》——1.6节简单电路分析实例

    本节书摘来自华章社区<简明电路分析>一书中的第1章,第1.6节简单电路分析实例,作者钟洪声 吴 涛 孙利佳,更多章节内容可以访问云栖社区"华章社区"公众号查看 1.6 ...

  2. 【转】JS回调函数--简单易懂有实例

    JS回调函数--简单易懂有实例 初学js的时候,被回调函数搞得很晕,现在回过头来总结一下什么是回调函数. 我们先来看看回调的英文定义:A callback is a function that is ...

  3. php xml对象解析_php解析xml 的四种简单方法(附实例)

    XML处理是开发过程中经常遇到的,PHP对其也有很丰富的支持,本文只是对其中某几种解析技术做简要说明,包括:Xml parser, SimpleXML, XMLReader, DOMDocument. ...

  4. python爬虫简单实例-Python 利用Python编写简单网络爬虫实例3

    利用Python编写简单网络爬虫实例3 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站"http://bbs.51tes ...

  5. python3爬虫实例-python3.7简单的爬虫实例详解

    python3.7简单的爬虫,具体代码如下所示: #https://www.runoob.com/w3cnote/python-spider-intro.html #Python 爬虫介绍 impor ...

  6. python爬虫实例-python3.7简单的爬虫实例详解

    python3.7简单的爬虫,具体代码如下所示: #https://www.runoob.com/w3cnote/python-spider-intro.html #Python 爬虫介绍 impor ...

  7. 有史以来最简单的三层实例(C#)

    三层已经学了很久了,一直没有写博客是因为自己感觉对三层的理解还太肤浅,怕写的不对误导别人.当然就现在我的水平而言对于三层的理解还是不够深刻,但是我感觉不至于误导别人了,所以将我对于三层的一些理解写出来 ...

  8. android 代码浏览,Webview实现android简单的浏览器实例代码

    WebView是Android中一个非常实用的组件,它和Safai.Chrome一样都是基于Webkit网页渲染引擎,可以通过加载HTML数据的方式便捷地展现软件的界面,下面通过本文给大家介绍Webv ...

  9. php django mysql配置文件_Mysql学习Django+mysql配置与简单操作数据库实例代码

    <Mysql学习Django+mysql配置与简单操作数据库实例代码>要点: 本文介绍了Mysql学习Django+mysql配置与简单操作数据库实例代码,希望对您有用.如果有疑问,可以联 ...

  10. php mysql简单留言本_php+mysql写的简单留言本实例代码

    php+mysql写的简单留言本实例代码 更新时间:2008年07月25日 09:41:32   作者: 方便新手学习php guestbook.php: COLOR: #002878; TEXT-D ...

最新文章

  1. mysql 主从复制
  2. IOS开发笔记15-自定义类
  3. OpenCV区域提取之利用Rect提取在源代码中预先定义好的区域
  4. 借鉴开源框架自研日志收集系统
  5. 网易云信深度优化解决移动聊天室“痼疾”
  6. heroku_WhateverOrigin –与Heroku和Play对抗原产地政策! 构架
  7. WINCE REG文件相关资料
  8. smallint占用几个字节_面试官问我:Object o = new Object() 占用了多少个字节?
  9. Python为你打开一扇门
  10. mysql将字符串转成数字
  11. Photoshop插件-奥顿效果(梦幻柔焦)-脚本开发-PS插件
  12. 【MTK sensor】alsps分析(以色温为例)
  13. java rtf_Java中存取Rtf文件 | 学步园
  14. Vm虚拟机Deepin安装教程---kalrry
  15. 兴业研发晨会纪要2008年 10月 30日
  16. 阴阳师服务器维护稍后,《阴阳师》11月7日更新维护到几点 暂时无法进入服务器进行游戏...
  17. TSV文件、CSV文件
  18. 2022年8月6日(星期六):骑行宁湖公园
  19. R语言基于mediation包行中介效应分析(2)
  20. VS中Qt 开发——无法解析的外部符号 QMetaObject 解决办法

热门文章

  1. SQL基础---SQL DELETE 语句
  2. 再谈“颠覆”冯.诺依曼计算机体系结构 —— 计算机的未来发展方向:去内存化...
  3. 设置Image控件加载图片完毕后的效果.
  4. 二叉树:层序遍历登场!
  5. 苹果mac专业的图像后期处理软件:Lightroom Classic
  6. 苹果Mac专业的 Go 开发集成环境:JetBrains GoLand
  7. 专业的raw图像处理编辑工具Capture One Pro 22 for Mac
  8. 如何禁用 MacBook 在打开盖子时自动启动功能
  9. Screaming Frog SEO Spide如何解决IIS 10中缺少CS协议支持的问题
  10. 深度剖析Spring Cloud底层原理