source: mauRepo/xmi2sqlalchemy/trunk/src/main/resources/velocity/python/sqlAlchemyTables.vm @ 8084

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/xmi2sqlalchemy/trunk/src/main/resources/velocity/python/sqlAlchemyTables.vm@8084
Revision 8084, 2.6 KB checked in by mnagni, 8 years ago (diff)

Correctly generates DB tables for CEDAEditor

in sqlTables.sql

1) No more "metadata = MetaData?()"
2) no more "metadata.create_all(engine)"

Just a couple of stupid thigs has still to be fixed by hand:
3) Column('refDirection', ARRAY, nullable=True) ----> Column('refDirection', ARRAY(TEXT), nullable=True)
4) Column('role', CI_RoleCode.db_type(), nullable=False) ----> Column('role', CI_RoleCode.db_type())

  • Property svn:executable set to *
RevLine 
[7998]1##########################################################################
[7974]2## sqlAlchemyTables.vm
[7998]3## The form expects the following context attributes
4## tableModel - an instance of ClassModel class
5##########################################################################
[7974]6
[8084]7##macro( attributeType $attribute)
8##if(!$attribute.associatedType.associatedClass.name.equalsIgnoreCase('boolean')
9##&& ($attribute.associatedType.isCodeList() || $attribute.associatedType.isEnumeration()))
10##${attribute.associatedType.associatedClass.name}.db_type()
11##else${vh.substituteDBType($attribute)}#end
12##end
[7974]13
[8041]14#macro(checkUseAlter $fk, $fkName, $relName)
15, use_alter=True, name='fk_${fkName}_${relName}'#end
[7996]16
[7974]17#macro( fk $foreignKey, $tableModel)
18#set ($fkName = ${foreignKey.tableModel.associatedClass.name.toLowerCase()})
[8041]19#set ($relName = ${foreignKey.parentAttributeName.toLowerCase()})
[8040]20#if($foreignKey.associatedRelationship.isOneToOne() || !$foreignKey.associatedRelationship.useList)
[8041]21Column('${fkName}_${relName}_id', Integer, ForeignKey('${fkName}.id'#checkUseAlter($foreignKey, $fkName, $relName))),
[8040]22#else
[8041]23Column('${fkName}_${relName}', Integer, ForeignKey('${fkName}.id'#checkUseAlter($foreignKey, $fkName, $relName))),
[7974]24#end
[8040]25#end
[7974]26
[7996]27#macro( primaryKey $tableModel)
28#set ($tn = ${tableModel.associatedClass.name.toLowerCase()})
[8040]29${tab}Column('id', Integer, Sequence('${tn}_id_seq'), primary_key=True),
30##if($tableModel.inherited && $tableModel.inherited.size() > 0)
31        ##foreach( $parent in $tableModel.inherited)
32                ##set ($pn = ${parent.associatedClass.name.toLowerCase()})
33                ##${tab}Column('_${pn}', Integer, ForeignKey('${pn}.${pn}_id')),#end
34##end
[7996]35#end
36
37#macro(checkNullable $simpleAttribute)
38#if($simpleAttribute.lowerBound != '0' && !$simpleAttribute.voidable)
39, nullable=False#else
40, nullable=True#end
41#end
42
[8064]43#macro( column $attribute)
[8084]44Column('${attribute.name}', ${vh.substituteDBType($attribute)} #checkNullable($attribute)),
[8064]45#end
[7996]46
[8064]47#macro( superColumn $tableModel)
48#foreach( $attribute in $tableModel.associatedClassModel.attributeModel)
49#if($attribute.subTypes)
50#foreach($sType in $attribute.subTypes)
51#set ($associatedCN = ${sType.associatedClass.name.toLowerCase()})
[8084]52Column('_${attribute.name}_${associatedCN}_id', Integer, ForeignKey('${associatedCN}.id')),
[8064]53#end
54#end   
55#end
56#end
[7996]57
[7974]58#macro( table $tableModel)
59#set ($associatedCN = ${tableModel.associatedClass.name.toLowerCase()})
60${associatedCN}_table = Table('${associatedCN}', metadata,
[7996]61#primaryKey($tableModel)
[7974]62#foreach( $foreignKey in $tableModel.fk)
[8084]63#fk($foreignKey, $tableModel)
[7974]64#end
[7998]65#foreach( $attribute in $tableModel.attributeModel)
[8084]66#column($attribute)
[7974]67#end
[8084]68##${tab}#superColumn($tableModel)
[7974]69${tab}extend_existing=False,)
70#end
71
72#table($tableModel)
73 
74 
Note: See TracBrowser for help on using the repository browser.