quinta-feira, 30 de janeiro de 2014

Database Reverse engineering with Delphi mORMot

POST #029 =======================================================================
I would like to map DB to class dynamically depending on the database. Now, I just have to select some tables and my little mORMot can generate the class on the fly to be used in models. Something like this:

TSQLSampleDB = Class(TSQLRecord  
public    
  fAuthUser       : TSQLAuthUser; 
  fLogonName      : RawUTF8; 
  fPasswordPlain  : RawUTF8; 
  fPrice          : currency; 
  fDisplayName    : RawUTF8; 
  fGroupRights    : RawUTF8; 
  fData           : TSQLRawBlob; 

published 
  property AuthUser: TSQLAuthUser read fAuthUser write fAuthUser; 
  property LogonName: RawUTF8 index 20 read fLogonName write fLogonName; 
  property PasswordPlain: RawUTF8 index 10 read fPasswordPlain write fPasswordPlain; 
  property Price: currency read fPrice write fPrice; 
  property DisplayName: RawUTF8 index 50 read fDisplayName write fDisplayName; 
  property GroupRights: RawUTF8 index 15 read fGroupRights write fGroupRights; 
  property Data: TSQLRawBlob read fData write fData; 
end; 

It's a good practice to put all data definition into a stand-alone unit called model. This unit will be shared between client and server.

This is not a real class but something quite similar, it just has to inherits from TSQLRecord, and the published properties will be used for the Delphi mORMot framework, in ORM side, ready to use in client/server environment. Very handy, isn't it?

Tags: Convert table to class; DB reverse engineering; mORMot framework.