Fórum Carregar tabela com RTTI #415960

27/04/2012

0

Estou aprendendo como trabalhar com rtti e estou com algumas dificuldades, até consegui fazer um insert e update e não estou conseguindo fazer uma forma de eu passar o código de certa tabela ele mapear a tabela e preencher a classe com o resultado.

Código insert para ter uma idéia de como estou fazendo:

class function TPersist.Inserir(pObjeto: TObject): Boolean;
var
Query : TSQLQuery;
Contexto : TRttiContext;
Tipo : TRttiType;
Propriedade : TRttiProperty;
Atributo : TCustomAttribute;
ConsultaSQL, CamposSQL, ValoresSQL: String;
begin
Contexto := TRttiContext.Create;
try
Tipo := Contexto.GetType(pObjeto.ClassType);

//localiza o nome da tabela
for Atributo in Tipo.GetAttributes do
begin
if Atributo is TTabela then
ConsultaSQL := INSERT INTO + (Atributo as TTabela).Nome;
end;

//preenche os nomes dos campos e valores
for Propriedade in Tipo.GetProperties do
begin
for Atributo in Propriedade.GetAttributes do
begin
if Atributo is TCampo then
begin
CamposSQL := CamposSQL + (Atributo as TCampo).Nome + ,;
ValoresSQL := ValoresSQL + QuotedStr(Propriedade.GetValue(pObjeto).ToString) + ,;
end;
end;
end;

//retirando as vírgulas que sobraram no final
Delete(CamposSQL, Length(CamposSQL), 1);
Delete(ValoresSQL, Length(ValoresSQL), 1);

ConsultaSQL := ConsultaSQL + ( + CamposSQL + ) VALUES ( + ValoresSQL + );

Query := TSQLQuery.Create(nil);
try
Query.SQLConnection := Conn;
Query.sql.Text := ConsultaSQL;
Query.ExecSQL();
finally
Query.Free;
end;
Result := True;
finally
Contexto.Free;
end;
end;

Classe:

unit uBancos;

interface

uses uAttributes;

type
[TTabela(BANCO)]
TBanco = class
private
fBanco : String;
fId : Integer;
public
[TCampo(CODIGO)]
property Id : Integer read fId write fId;

[TCampo(BANCO)]
property Banco : String read fBanco write fBanco;
end;

implementation

end.


Thiago Porto

Thiago Porto

Responder

Posts

07/10/2018

Valter Rui

Eu também estou aprendendo agora a usar o RTTi, mias para preencher um objeto de uma class eu uso assim:

function TNFCeMovimentoDao.CarregarMovimento(vNFCeMovimentoModel: TNFCeMovimentoModel): TFDQuery;
var
VQry: TFDQuery;

Ctx: TRttiContext;
RTtype: TRttiType;
RTpro: TRttiProperty;
Att: TCustomAttribute;
Value: TValue;
iCampo: integer;
//nomecampo: string;
begin
try
VQry := FConexao.CriarQuery();
VQry.Open('select * from NFCE_MOVIMENTO where UPPER(STATUS_MOVIMENTO)=:STATUS',[vNFCeMovimentoModel.StatusMovimento]);
if VQry.IsEmpty then
begin
vNFCeMovimentoModel.FStatMov := tmFechado;
Exit;
end;
vNFCeMovimentoModel.FStatMov := tmAberto;
// set os dados encontrados no banco, para todas property da class TNFCeMovimentoModel
Ctx := TRttiContext.Create;
try
RTtype := ctx.GetType(TNFCeMovimentoModel.ClassInfo);
with vQry do
begin
iCampo:=0;
for RTpro in RTtype.GetProperties do
begin
//nomecampo := Fields[iCampo].FieldName;
case RTpro.PropertyType.TypeKind of
tkUnknown:;
tkString,tkWString,tkUString :
Value := TValue.From(Fields[iCampo].AsString);
tkInteger,tkInt64,tkEnumeration:
Value := TValue.From(Fields[iCampo].AsInteger); // Value := TValue.From(FieldByName(FieldName(RTpro.Name)).AsInteger );
tkFloat: begin

if CompareText(RTpro.PropertyType.Name, 'TDateTime') = 0 then
value := Tvalue.FromVariant(Fields[iCampo].AsFloat).AsType<TDateTime>
else
Value := TValue.From(Fields[iCampo].AsExtended );
end;

else
Value := TValue.FromVariant(Fields[iCampo].AsVariant);
end;
if not Value.IsEmpty then
RTpro.SetValue(vNFCeMovimentoModel,Value);

inc(iCampo);
end;
end;
finally
Ctx.Free;
end;

finally
Result := VQry
end;

end;

Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar