D2010 RTTI + Attribute Simple Implementation orm

Source: Internet
Author: User

Remember when David I came to Shanda this April and asked, "is the reflection mechanism going to do a little better?" We want to give up rtti.
David I replied, "This is really a place to think about, of course rtti we won't give up." (This white beard old brother is really very cute, that year Borland several ups and downs, the only one who can see the pleasing is David I). I thought Rtti in D2010 was only improved, rehash. Unexpectedly, Rtti not only can reflect public, protected, private information, but also dynamically execute the method in the class, more surprisingly, also support attribute. D2010 New RTTI, to some extent, is the same as Unicode, with infinite reverie space on the extended frame. Let's say D2010 RTTI + Attribute simple implementation orm.

1, support ORM, the most basic two information is the table information and field information. These two messages, if used with attribute, are more concise and readable. You can use the property name as the real field name, or the attributes in the attribute as the real name, plus the field title (can be commented), required fields , whether the primary key , display format and so on, if there is no attribute, the auxiliary information of class and attribute must be described by other information, which is very troublesome.

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);
Field name
Property fieldname:string read Ffieldname write ffieldname;
Title
Property title:string read Ftitle write ftitle;
End

2. With these two attribute, we have to create a class that parses properties and attribute, and resolves SQL statements such as INSERT, UPDATE, delete, select, and so on. Let's call tstorable. This class can expand what you want, as needed. Currently only implemented the Insert method, other methods, left to the hard-working people to daydream.

Tstorable = Class

Public
Inserting SQL statements
function insert:string;
Get field title
function Getfieldtitle (const afieldname:string): string;
Set up
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; Get Insert Table name
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;
Edit property values based on data type
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, with the above parsing class and SQL Foundation, we must create an entity class . If the property name is Chinese , you can have a different argument. I currently live in a medical industry company, the medical professional English terminology is smelly and long, Obama may not be able to spell a few terms. If the attribute name is described in Chinese, the real field name is placed in the attribute, which may improve the readability and maintainability of the program.

Unit ucontact;

Interface
Uses Sysutils,uattribute;

Type
[Table (' CONTACTS ', ' contact information ')]
Tcontact = Class (Tstorable)
Private
fname:string;
Fage:integer;
F Tel: string;
Published
Public
[FieldInfo (' name ', ' names ')]
Property name:string read FName write FName;
[FieldInfo (' Ages ', ' age ')]
Property Age:integer read Fage write Fage;
[FieldInfo (' phone ', ' contact phone ')]
Property Phone: String read F telephone write f telephone; Try the name of the Chinese word, and get used to it.
End
Implementation


End.

4, the invocation of the example is very simple:

Procedure Tform4.btn1click (Sender:tobject);
Var
Contact:tcontact;
Begin
Contact: = Tcontact.create;
Contact.age: = 32;
Contact.name: = ' Tintin ';
Contact. Tel: = ' 135*****918 ';//Do you remember the humiliation of 918?

ShowMessage (Contact.insert);

ShowMessage (Contact.getfieldtitle (' age '));
Contact.free;
End

5. Summary:

ORM is really easy to use on object mapping, but not omnipotent, if you rely too much on ORM, not only can not understand the relationship between database tables and business, but also easy to write inefficient SQL query statements. UPDATE statement, keep in mind that changes in field values are not changed, otherwise it increases the risk of data inconsistency in the database and increases the database log overhead. Delete statement, with the attribute about the key information, and, if necessary, verifying whether one or more records are affected.

This is a simple example, one step further from real productivity, in order to execute the SQL statement, you can read and write the data set in Tstorable, before invoking the Execute SQL statement.

http://blog.csdn.net/shuaihj/article/details/6125697

D2010 RTTI + Attribute Simple Implementation orm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.