Author Topic: Transformation. Copy field into new table.  (Read 2589 times)

novikovigor

  • EA User
  • **
  • Posts: 46
  • Karma: +0/-0
    • View Profile
Transformation. Copy field into new table.
« on: July 16, 2013, 12:37:27 am »
Hello!

I'm writing transformation template. I need to create additional table for attributes, that have type "A". For example I have table Country:
- Id - number type
- Capital - "A" type
- Area - "B" type
- Population - "C" type

After transformation next tables must exist:
Country
- Id - number type
- CapitalId - number type
- Area - "B" type
- Population - "C" type
Capital
- different fields (id, description, etc.)

I'd already written similar template but without "CapitalId" field, it's not so hard. But how can I resolve the problem that I've described above? Just show me highlights in "Class" & "Attribute" templates.

Thanks.

novikovigor

  • EA User
  • **
  • Posts: 46
  • Karma: +0/-0
    • View Profile
Re: Transformation. Copy field into new table.
« Reply #1 on: July 16, 2013, 07:18:49 pm »
Hello again!

I've solved my problem. "Add new custom template" function helped me.
So "Attribute" template creates columns and "Attributes__ext" template creates table for type "A".

Stephane B

  • EA User
  • **
  • Posts: 31
  • Karma: +0/-0
    • View Profile
Re: Transformation. Copy field into new table.
« Reply #2 on: July 16, 2013, 07:34:18 pm »
Priviet!

If you just want an Id attribute and no real connection between these classes, and if you know by advance that the newly created class will be Capital, then it's rather easy.

Code: [Select]
+-------------+
|  Namespace  |
+-------------+

$COMMENT="Create your Capital Class in a suitable package.""
$COMMENT="Let's say you want it in the same package as your other classes."

%list="Class"%

Class {

      $COMMENT = "Give this class a good XRef in case you have to point to it"
      %TRANSFORM_REFERENCE("Capital")%
      name = "Capital"
      notes = "Manually created class"

      Attribute {
      ...
      }
}

+----------------+
|  Class         |
+----------------+

Class {
      
      %list="Attribute"%
}

+----------------+
|  Attribute     |
+----------------+

%if attType == "A"%
$newAttName = %attName% + "Id"

Attribute {
      

      type = "long"
      name = %qt%$newAttName%qt%

}
%endTemplate%

$COMMENT="continue with whatever you had in Attribute"

If, on the other hand, you have type A = City, and you want to dynamically create a new class (table) for each attribute that is a city, then it's more complicated.
Like if Country has a Capital attribute of type City(=A), and a MainCity of type City(=A) you create a new table Capital and a new table MainCity. And every time you meet an attribute of type City you create a new table by the attribute's name... (btw you just reinvented inheritance). I can see solutions, but first tell me if that is what you want to do.