3

first of all this is my first post here, so i want to sorry for my bad english first, second i want to sorry for any stupid question that may sound to be

well, im trying to write my own ORM specific for MySQL, Delphi and for the team i work here, but im getting stuck in some things that i dont know how to do, well i will post above the final efinition of my class.

    uses
      hsORM.Mapping,
      hsORM.Types;

    type
      [ThsORMTableMap('hscad_cadmunicipal')]
      TMunicipe = class(ThsORMTable)
      private
        { Private declarations }
        [ThsORMColumnPrimaryKeyMap('inscricaomunicipal')]
        Fid : ThsORMColumnPrimaryKey;

        [ThsORMColumnNullableMap('idlogradouro', varInteger)]
        Fidlogradouro : ThsORMColumnNullable;

        [ThsORMColumnNullableMap('idbairro', varInteger)]
        Fidbairro : ThsORMColumnNullable;

        [ThsORMColumnNullableMap('idestadocivil', varInteger)]
        Fidestadocivil : ThsORMColumnNullable;

        [ThsORMColumnNullableMap('idnaturezaestab', varInteger)]
        Fidnaturezaestabelecimento : ThsORMColumnNullable;

        [ThsORMColumnNullableMap('idnaturezajuridica', varInteger)]
        Fidnaturezajuridica : ThsORMColumnNullable;

        [ThsORMColumnNullableMap('idagencia', varInteger)]
        Fidagencia : ThsORMColumnNullable;

        [ThsORMColumnMap('datacadastro')]
        Fdatacadastro : ThsORMColumnDate;

        [ThsORMColumnMap('nome')]
        Fnome : ThsORMColumnString;

        [ThsORMColumnNullableMap('nomefantasia', varString)]
        Fnomefantasia : ThsORMColumnNullable;

        [ThsORMColumnMap('tipopessoa')]
        Ftipopessoa : ThsORMColumnString;

        [ThsORMColumnNullableMap('numero', varInteger)]
        Fnumero : ThsORMColumnNullable;

        [ThsORMColumnNullableMap('complemento', varString)]
        Fcomplemento : ThsORMColumnNullable;

        [ThsORMColumnNullableMap('observacao', varString)]
        Fobservacao : ThsORMColumnNullable;

        [ThsORMColumnNullableMap('telefone', varString)]
        Ftelefone : ThsORMColumnNullable;

        [ThsORMColumnNullableMap('celular', varString)]
        Fcelular : ThsORMColumnNullable;

        [ThsORMColumnNullableMap('fax', varString)]
        Ffax : ThsORMColumnNullable;

        [ThsORMColumnNullableMap('sexo', varString)]
        Fsexo : ThsORMColumnNullable;

        [ThsORMColumnNullableMap('email', varString)]
        Femail : ThsORMColumnNullable;

        [ThsORMColumnNullableMap('responsavel', varString)]
        Fresponsavel : ThsORMColumnNullable;

        [ThsORMColumnNullableMap('contacorrente', varString)]
        Fcontacorrente : ThsORMColumnNullable;

        [ThsORMColumnMap('foto')]
        Ffoto : ThsORMColumnBlob;

        [ThsORMColumnMap('fornecedor')]
        Ffornecedor : ThsORMColumnBoolean;

        [ThsORMColumnMap('tipocredor')]
        Ftipocredor : ThsORMColumnString;

        [ThsORMColumnMap('ativo')]
        Fativo : ThsORMColumnBoolean;
      public
        { Public declarations }
        property id : ThsORMColumnPrimaryKey read Fid write Fid;
        property idlogradouro : ThsORMColumnNullable read Fidlogradouro write Fidlogradouro;
        property idbairro : ThsORMColumnNullable read Fidbairro write Fidbairro;
        property idestadocivil : ThsORMColumnNullable read Fidestadocivil write Fidestadocivil;
        property idnaturezaestabelecimento : ThsORMColumnNullable read Fidnaturezaestabelecimento write Fidnaturezaestabelecimento;
        property idnaturezajuridica : ThsORMColumnNullable read Fidnaturezajuridica write Fidnaturezajuridica;
        property idagencia : ThsORMColumnNullable read Fidagencia write Fidagencia;
        property datacadastro : ThsORMColumnDate read Fdatacadastro write Fdatacadastro;
        property nome : ThsORMColumnString read Fnome write Fnome;
        property nomefantasia : ThsORMColumnNullable read Fnomefantasia write Fnomefantasia;
        property tipopessoa : ThsORMColumnString read Ftipopessoa write Ftipopessoa;
        property numero : ThsORMColumnNullable read Fnumero write Fnumero;
        property complemento : ThsORMColumnNullable read Fcomplemento write Fcomplemento;
        property observacao : ThsORMColumnNullable read Fobservacao write Fobservacao;
        property telefone : ThsORMColumnNullable read Ftelefone write Ftelefone;
        property celular : ThsORMColumnNullable read Fcelular write Fcelular;
        property fax : ThsORMColumnNullable read Ffax write Ffax;
        property sexo : ThsORMColumnNullable read Fsexo write Fsexo;
        property email : ThsORMColumnNullable read Femail write Femail;
        property responsavel : ThsORMColumnNullable read Fresponsavel write Fresponsavel;
        property contacorrente : ThsORMColumnNullable read Fcontacorrente write Fcontacorrente;
        property foto : ThsORMColumnBlob read Ffoto write Ffoto;
        property fornecedor : ThsORMColumnBoolean read Ffornecedor write Ffornecedor;
        property tipocredor : ThsORMColumnString read Ftipocredor write Ftipocredor;
        property ativo : ThsORMColumnBoolean read Fativo write Fativo;
      end;

well, i defined a respective class for every mysql data type. well what i want to do next is use RTTI to create dynamic every one os this fields. since classes in delphi need to be explicit created and im trying to avoid that, what im trying to do is using my class ThsORMTable to create dynamic this columns. for example:

    ThsORMTable = class
      private
        { Private declarations }
        FTableName: string;
        procedure InitializeTable();
        procedure InitializeColumns();
      public
        { Public declarations }
        constructor Create();
        property TableName : string read FTableName write FTableName;
      end;

    { ThsORMTable }

    {$REGION 'Private'}

    procedure ThsORMTable.InitializeTable();
    var
      AContext : TRttiContext;
      AType : TRttiType;
      AAttribute : TCustomAttribute;
      AFound : Boolean;
    begin
      AContext := TRttiContext.Create();
      try
        AFound := False;
        AType := AContext.GetType(ClassType);
        for AAttribute in AType.GetAttributes do
          if(AAttribute is ThsORMTableMap) then
            begin
              FTableName := (AAttribute as ThsORMTableMap).TableName;
              AFound := True;
              Break;
            end;
        if not(AFound) then raise Exception.Create(ETableNotMapped);
      finally
        AContext.Free();
      end;
    end;

    procedure ThsORMTable.InitializeColumns();
    var
      AContext : TRttiContext;
      AType : TRttiType;
      AField : TRttiField;
      AFound : Boolean;
    begin
      AContext := TRttiContext.Create();
      try
        AType := AContext.GetType(ClassType);
        for AField in AType.GetFields do 
                    /**********************************************
                    here i want something like for example
                    if(AField is ThsORMColumnInteger) then
                      begin
                        (AField as ThsORMColumnInteger) := ThsORMColumnInteger.Create();
                    is this possible? im going to the wrong way?
                      end;
                    **********************************************/
      finally
        AContext.Free();
      end;
    end;

    {$ENDREGION}

    {$REGION 'Public'}

    constructor ThsORMTable.Create();
    begin
      try
        InitializeTable();
        InitializeColumns();
      except on Error : Exception do
        raise ThsORMTableInitialization.Create(Format(ETableInitializationError, [Error.Message]));
      end;
    end;

    {$ENDREGION}

but i get a compile error, no matter the way. hope u guys can help me. thx in advantage

Update: sorry that i was not clear, ill try again. what im trying to do is, through the ThsORMTable, my ancestor class, on the constructor method specifically, initialize every field (create) so i do not need to explicit create of every one of this fields in every class that inherited from this ancestor

3
  • [DCC Error] hsORM.Types.pas(580): E2015 Operator not applicable to this operand type
    – kabstergo
    Commented Aug 6, 2012 at 23:48
  • My guess is that you mixing up concepts, AField is a TRTTIField not a table field, check Remy Lebeau answer with attention. Commented Aug 6, 2012 at 23:59
  • yep mate, i already asked his answer, tried to explain myself better this time. if you can check out again pls. thx
    – kabstergo
    Commented Aug 7, 2012 at 0:02

1 Answer 1

4

Calling AField is ThsORMColumnInteger and AField as ThsORMColumnInteger will always fail because TRttiField does not derive from ThsORMColumnInteger and vice versa.

It is unclear exactly what you are trying to accomplish with each field of the ThsORMTable class. If you just want to access the attributes of each field, you don't need to construct an actual object instance to do that, eg:

uses
  ..., TypInfo;

procedure ThsORMTable.InitializeColumns();  
var  
  AContext : TRttiContext;  
  AType : TRttiType;  
  AField : TRttiField;  
  AAttribute : TCustomAttribute;
  AFound : Boolean;
begin  
  AContext := TRttiContext.Create();  
  try  
    AType := AContext.GetType(ClassType);  
    for AField in AType.GetFields do   
    begin
      // TRttiType.TypeData is private so have to use the TypInfo unit directly...
      TypeData := TypInfo.GetTypeData(AField.FieldType.Handle);

      if TypeData^.ClassType is ThsORMColumnInteger then
      begin
        for AAttribute in AField.GetAttributes do      
        begin
          if (AAttribute is ThsORMColumnMap) then      
          begin      
            // use (AAttribute as ThsORMColumnMap) as needed ...
            AFound := True;      
            Break;      
          end;
        end;      
        if (not AFound) then raise Exception.Create(EColumnNotMapped);      
      end;
    end;
  finally  
    AContext.Free();  
  end;  
end;  

Update: based on your updated information, you might be able to do something like the following, depending on how your column class types are set up:

type
  ThsORMColumn = class(...)
    //...
  end;

  ThsORMColumnClass = class of ThsORMColumn;

  //...

  ThsORMColumnInteger = class(ThsORMColumn)
    // ...
  end;

  //...

procedure ThsORMTable.InitializeColumns();     
var     
  AContext : TRttiContext;     
  AType : TRttiType;     
  AField : TRttiField;     
  AAttribute : TCustomAttribute;   
  AObj : ThsORMColumn;   
begin     
  AContext := TRttiContext.Create();     
  try     
    AType := AContext.GetType(ClassType);     
    for AField in AType.GetFields do      
    begin   
      if AField.FieldType.TypeKind = tkClass then
      begin
        // TRttiType.TypeData is private so have to use the TypInfo unit directly...   
        TypeData := TypInfo.GetTypeData(AField.FieldType.Handle);   
        if (not TypeData^.ClassType.InheritsFrom(ThsORMColumn)) then
          raise Exception.Create(...);         
        AObj := ThsORMColumnClass(TypeData^.ClassType).Create();         
        AField.SetValue(Self, Obj);
      end;   
    end;
  finally     
    AContext.Free();     
  end;     
end;     

The nice thing about this approach is that you technically don't have to tag your fields with attributes at all for it to work, though I suspect you will still want to do that for other purposes.

2
  • sorry that i was not clear, ill try again. what im trying to do is, through the ThsORMTable, my ancestor class, on the constructor method specifically, initialize every field (create) so i do not need to explicit create of every one of this fields in every class that inherited from this ancestor
    – kabstergo
    Commented Aug 6, 2012 at 23:54
  • thats exactly what i was looking for, this piece work like a charm, thx for your help mate
    – kabstergo
    Commented Aug 7, 2012 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.