How to ad a TGUID/TSMARTGUID Generator?

How to define table fileds  and generator to have ID fields defined as GUIDs?
For example say I have a table with field ID defined as Primary Key VARCHAR(32).

I would like that the generated entity has the following attribute:

[Id('FId', TIdGenerator.SmattGuid)]

How can I obtain this?

I have found the solution on this forum:
How to set custom TidGenerator from DataModeler:
You can use Data Modeler customization script feature: http://www.tmssoftware.biz/business/dmodeler/doc/web/customization-script.html



How do you do it? In OnClassGenerated Event
Can you show an example?

I want to set the generator to none for TGuid columns

Yes, using OnClassGenerated. Here is an example:




procedure OnClassGenerated(Args: TClassGeneratedArgs);
var
  Att: TCodeAttributeDeclaration;
  I: Integer;
begin                                
  for I := 0 to Args.CodeType.CustomAttributes.Count - 1 do
  begin
    Att := Args.CodeType.CustomAttributes.Items;
    if (Att.Name = 'Id') and (Att.Arguments.Count > 1) then
    begin
      TCodeSnippetExpression(Att.Arguments.Items[1].Value)
        .Value := 'TIdGenerator.SmartGuid';
    end;                                      
  end;                   
end;     

I took the freedom to extend Wagner's example to only change ID fields that are called "GUID" - maybe useful for users, who do not exclusively use GUIDs for PKs. (Note that there was a subtle small bug in Wagner's original script)

//SmartGuid for all GUID-type ID fields   
  for i := 0 to Args.CodeType.CustomAttributes.Count - 1 do
  begin                                                      
    LAttribute := Args.CodeType.CustomAttributes.Items[i]; //missing [i] in original script
    if (LAttribute.Name = 'Id') and (LAttribute.Arguments.Count > 1) then
    begin                                                         
      LIdName := TCodeSnippetExpression(LAttribute.Arguments.Items[0].Value).Value;                                       
      if UpperCase(LIdName) = #39'FGUID'#39 then         //important: there are quotes around the value!   
      begin
        TCodeSnippetExpression(LAttribute.Arguments.Items[1].Value).Value := 'TIdGenerator.SmartGuid';
      end;   
    end;                                      
  end;  

Regards
Olaf Monien

1 Like

Thank you for the contribution, @Olaf_Monien

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.