Created
May 1, 2018 21:01
-
-
Save tporto/94a12f63bc0417ba7f20af35e59b7b2c to your computer and use it in GitHub Desktop.
list.pas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Criei um orm simples, mas que funciona bem. Nele tenho essa chamada: | |
class function list(pObjeto: TVO; pFiltro: String): TListaVO; | |
class function TORM.list(pObjeto: TVO; pFiltro: String): TListaVO; | |
var | |
Query: TZQuery; | |
I, J, K: Integer; | |
ObjetoLocal: TVO; | |
Campo, Propriedade, Classe, Caminho, PropriedadeXML, ColunaXML: String; | |
ClassRef: TPersistentClass; | |
Documento: TXMLDocument; | |
Node: TDOMNode; | |
begin | |
try | |
// Lê no arquivo xml no disco | |
Caminho := CAMINHO_XML + pObjeto.UnitName + '.xml'; | |
ReadXMLFile(Documento, Caminho); | |
Node := Documento.DocumentElement.FirstChild; | |
Result := Nil; | |
Query := get(pObjeto, pFiltro); | |
Query.Active := True; | |
Result := TListaVO.Create; | |
if not Query.IsEmpty then | |
begin | |
while not Query.EOF do | |
begin | |
Classe := pObjeto.ClassName; | |
ClassRef := GetClass(Classe); | |
ObjetoLocal := TVO(ClassRef.Create); | |
for I := 0 to Query.FieldCount - 1 do | |
begin | |
Campo := Query.Fields[I].DisplayName; | |
// Encontra o nome da propriedade no arquivo XML mapeado | |
for J := 0 to (Node.ChildNodes.Count - 1) do | |
begin | |
if Node.ChildNodes.Item[J].NodeName = 'property' then | |
begin | |
for K := 0 to 4 do | |
begin | |
if Node.ChildNodes.Item[J].Attributes.Item[K].NodeName = 'name' then | |
PropriedadeXML := Node.ChildNodes.Item[J].Attributes.Item[K].NodeValue; | |
if Node.ChildNodes.Item[J].Attributes.Item[K].NodeName = 'column' then | |
ColunaXML := Node.ChildNodes.Item[J].Attributes.Item[K].NodeValue; | |
end; | |
if ColunaXML = UpperCase(Campo) then | |
begin | |
Propriedade := PropriedadeXML; | |
end; | |
end | |
else if Node.ChildNodes.Item[J].NodeName = 'id' then | |
begin | |
Propriedade := 'Id'; | |
end; | |
end; | |
if Query.Fields[I].DataType in [ftFloat, ftDate, ftDateTime] then | |
begin | |
SetFloatProp(ObjetoLocal, Propriedade, Query.Fields[I].AsFloat); | |
end | |
else if Query.Fields[I].DataType in [ftInteger, ftSmallint, ftLargeint] then | |
begin | |
SetInt64Prop(ObjetoLocal, Propriedade, Query.Fields[I].AsInteger); | |
end | |
else if Query.Fields[I].DataType in [ftString, ftMemo, ftFixedChar] then | |
begin | |
SetStrProp(ObjetoLocal, Propriedade, Query.Fields[I].AsString); | |
end; | |
end; | |
Result.Add(ObjetoLocal); | |
Query.Next; | |
end; | |
end; | |
finally | |
Query.Close; | |
Query.Free; | |
end; | |
end; | |
No controller chamo assim: | |
class function TReceber_parcelasController.get_by_receber2(idreceber: integer | |
): TListReceber_parcelas; | |
begin | |
result := TListReceber_parcelas(TORM.list(TReceber_parcelas.Create, 'id_receber = ' + inttostr(idreceber))); | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment