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

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

Stable release 0.1.0

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