delphi tag标签
delphi tag标签
tag标签,是go的叫法:
type Person struct { Name string `json:"name" xml:"name"` Email string `json:"email" xml:"email"` Age int `json:"age,omitempty" xml:"age,omitempty"` }
delphi是可以山寨GO TAG标签的:
// 商品资料 数据模型 "":"" [Attr('{"TableName":"goods","Caption":"商品资料"}')] TGoods = record [Attr('{"FieldName":"goodsid","Caption":"货号","DataType":"string","Size":9,"Required":True}')] goodsid: string; [Attr('{"FieldName":"goodsname","Caption":"品名","DataType":"string","Size":100,"Required":True}')] goodsname: string; end;
tag标签的常见用途:
1)序列化和反序列化:
标签常被用于控制结构体的序列化和反序列化。例如,在 Go 的 encoding/json 和 encoding/xml 包中,你可以使用标签来指定字段在 JSON 或 XML 中的名称,或者在编码时是否忽略某个字段。这可以让你有更大的灵活性来定义和控制序列化和反序列化的过程。
2)数据验证:
一些库允许你使用标签来为结构体的字段添加验证规则。例如,你可以使用 valid 标签来指定一个字段必须是邮件地址格式,或者使用 range 标签来指定一个整数字段的值必须在某个范围内。这些库通常提供了一套简洁的 DSL(领域特定语言)让你可以在标签中定义复杂的验证规则。
3)数据库 ORM 映射:
有些数据库 ORM(对象关系映射)库允许你使用标签来定义数据库表和结构体之间的映射关系。例如,你可以使用 sql 标签来指定字段对应的数据库列的名称,或者一个字段是否可以为 null。
4)HTTP 路由和处理:
在某些 Web 框架中,标签可以被用来定义 HTTP 路由规则或者请求处理逻辑。例如,你可以使用 route 标签来指定一个方法处理哪个 URL 路径的请求,或者使用 method 标签来指定一个方法处理哪种 HTTP 方法的请求。
delphi ORM:
unit Unit1; interface uses Rtti, core.json, Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type Attr = class(TCustomAttribute) public json: string; public constructor Create(AJson: string); end; // 商品资料 数据模型 [Attr('{"TableName":"goods","Caption":"商品资料","KeyFields":"goodsid"}')] TGoods = record [Attr('{"FieldName":"goodsid","Caption":"货号","DataType":"string","Size":9,"Required":true}')] goodsid: string; [Attr('{"FieldName":"goodsname","Caption":"品名","DataType":"string","Size":100,"Required":true}')] goodsname: string; function Insert: string; function Delete: string; function Update: string; end; TForm1 = class(TForm) Memo1: TMemo; Button2: TButton; Button1: TButton; Button3: TButton; procedure Button2Click(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} constructor Attr.Create(AJson: string); begin json := AJson; end; { TGoods } function TGoods.Delete: string; var LContext: TRttiContext; LType: TRttiType; LRttiField: TRttiField; LTable, LField: TCustomAttribute; LTableName, LWhere, LDataType, LFieldName, LValue, LKeyFields: string; LJsonO: TJsonO; begin LContext := TRttiContext.Create; try LType := LContext.GetType(TypeInfo(TGoods)); LTable := LType.GetAttribute<Attr>; LJsonO := SO(Attr(LTable).json); LTableName := LJsonO.S['TableName']; // 表名 LKeyFields := LJsonO.S['KeyFields']; // 主键 FreeAndNil(LJsonO); LValue := ''; LDataType := ''; LWhere := ''; for LRttiField in LType.AsRecord.GetFields do begin LField := LRttiField.GetAttribute<Attr>; LJsonO := SO(Attr(LField).json); LFieldName := LJsonO.S['FieldName']; // 字段名 if Pos(LFieldName, LKeyFields) = 0 then // 非主键不做where条件 begin FreeAndNil(LJsonO); Continue; end; LDataType := LJsonO.S['DataType']; // 数据类型 FreeAndNil(LJsonO); LValue := LRttiField.GetValue(@Self).ToString; // 值 if LDataType = 'string' then // 处理字符串字段值 LValue := QuotedStr(LValue); LWhere := LWhere + ' and ' + LFieldName + '=' + LValue; // 拼where条件 if LWhere = '' then begin Result := ''; ShowMessage('没有where条件'); Exit; end; end; System.Delete(LWhere, 1, 4); Result := 'delete from ' + LTableName + ' where ' + LWhere; // delete from goods where goodsid='666' finally LContext.Free; end; end; function TGoods.Insert: string; var LContext: TRttiContext; LType: TRttiType; LRttiField: TRttiField; LTable, LField: TCustomAttribute; LTableName: string; LJsonO: TJsonO; LFields, LValues, LValue, LDataType: string; begin LContext := TRttiContext.Create; try LType := LContext.GetType(TypeInfo(TGoods)); LTable := LType.GetAttribute<Attr>; LJsonO := SO(Attr(LTable).json); LTableName := LJsonO.S['TableName']; // 表名 FreeAndNil(LJsonO); LFields := ''; LValues := ''; LValue := ''; LDataType := ''; for LRttiField in LType.AsRecord.GetFields do begin LField := LRttiField.GetAttribute<Attr>; LJsonO := SO(Attr(LField).json); LFields := LFields + ',' + LJsonO.S['FieldName']; // 拼字段 LDataType := LJsonO.S['DataType']; // 数据类型 FreeAndNil(LJsonO); LValue := LRttiField.GetValue(@Self).ToString; // 值 if LDataType = 'string' then // 处理字符串字段值 LValue := QuotedStr(LValue); LValues := LValues + ',' + LValue; // 拼值 end; System.Delete(LFields, 1, 1); System.Delete(LValues, 1, 1); Result := 'insert into ' + LTableName + '(' + LFields + ') values (' + LValues + ')'; finally LContext.Free; end; end; procedure TForm1.Button1Click(Sender: TObject); begin var g: TGoods; g.goodsid := '666'; Memo1.Text := g.Delete; end; procedure TForm1.Button2Click(Sender: TObject); begin var g: TGoods; g.goodsid := '666'; g.goodsname := '商品一'; Memo1.Text := g.Insert; // insert into goods(goodsid,goodsname) values ('666','商品一') end; procedure TForm1.Button3Click(Sender: TObject); begin var g: TGoods; g.goodsid := '666'; g.goodsname := '修改'; Memo1.Text := g.Update; end; function TGoods.Update: string; var LContext: TRttiContext; LType: TRttiType; LRttiField: TRttiField; LTable, LField: TCustomAttribute; LTableName, LWhere, LDataType, LFieldName, LValue, LKeyFields, LSet: string; LJsonO: TJsonO; begin LContext := TRttiContext.Create; try LType := LContext.GetType(TypeInfo(TGoods)); LTable := LType.GetAttribute<Attr>; LJsonO := SO(Attr(LTable).json); LTableName := LJsonO.S['TableName']; // 表名 LKeyFields := LJsonO.S['KeyFields']; // 主键 FreeAndNil(LJsonO); LValue := ''; LDataType := ''; LWhere := ''; LSet := ''; for LRttiField in LType.AsRecord.GetFields do begin LField := LRttiField.GetAttribute<Attr>; LJsonO := SO(Attr(LField).json); LFieldName := LJsonO.S['FieldName']; // 字段名 LDataType := LJsonO.S['DataType']; // 数据类型 FreeAndNil(LJsonO); LValue := LRttiField.GetValue(@Self).ToString; // 值 if LDataType = 'string' then // 处理字符串字段值 LValue := QuotedStr(LValue); LSet := LSet + ',' + LFieldName + '=' + LValue; if Pos(LFieldName, LKeyFields) = 0 then Continue; // 非主键不做where条件 LWhere := LWhere + ' and ' + LFieldName + '=' + LValue; // 拼where条件 if LWhere = '' then begin Result := ''; ShowMessage('没有where条件'); Exit; end; end; System.Delete(LWhere, 1, 4); System.Delete(LSet, 1, 1); Result := 'update ' + LTableName + ' set ' + LSet + ' where ' + LWhere; // update goods set goodsid='666',goodsname='修改' where goodsid='666' finally LContext.Free; end; end; end.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/18886555

浙公网安备 33010602011771号