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 *
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
14#macro(checkUseAlter $fk, $fkName, $relName)
15, use_alter=True, name='fk_${fkName}_${relName}'#end
16
17#macro( fk $foreignKey, $tableModel)
18#set ($fkName = ${foreignKey.tableModel.associatedClass.name.toLowerCase()})
19#set ($relName = ${foreignKey.parentAttributeName.toLowerCase()})
20#if($foreignKey.associatedRelationship.isOneToOne() || !$foreignKey.associatedRelationship.useList)
21Column('${fkName}_${relName}_id', Integer, ForeignKey('${fkName}.id'#checkUseAlter($foreignKey, $fkName, $relName))),
22#else
23Column('${fkName}_${relName}', Integer, ForeignKey('${fkName}.id'#checkUseAlter($foreignKey, $fkName, $relName))),
24#end
25#end
26
27#macro( primaryKey $tableModel)
28#set ($tn = ${tableModel.associatedClass.name.toLowerCase()})
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
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
43#macro( column $attribute)
44Column('${attribute.name}', ${vh.substituteDBType($attribute)} #checkNullable($attribute)),
45#end
46
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()})
52Column('_${attribute.name}_${associatedCN}_id', Integer, ForeignKey('${associatedCN}.id')),
53#end
54#end   
55#end
56#end
57
58#macro( table $tableModel)
59#set ($associatedCN = ${tableModel.associatedClass.name.toLowerCase()})
60${associatedCN}_table = Table('${associatedCN}', metadata,
61#primaryKey($tableModel)
62#foreach( $foreignKey in $tableModel.fk)
63#fk($foreignKey, $tableModel)
64#end
65#foreach( $attribute in $tableModel.attributeModel)
66#column($attribute)
67#end
68##${tab}#superColumn($tableModel)
69${tab}extend_existing=False,)
70#end
71
72#table($tableModel)
73 
74 
Note: See TracBrowser for help on using the repository browser.