Skip to content

Commit

Permalink
finish generate simple ref table
Browse files Browse the repository at this point in the history
  • Loading branch information
lilliCao committed Jul 20, 2020
1 parent 4863191 commit 1b7a6d1
Showing 1 changed file with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CREATE TABLE ${tableName} (
<#else>
<#assign name = field.name>
</#if>
<#--Field: primary key-->
<#if field.annotations.javax_persistence_Id??>
<#assign pk = name>
<#assign type = get_type(field)>
Expand All @@ -23,6 +24,7 @@ CREATE TABLE ${tableName} (
</#if>
<#assign columns = columns + [{"name": name, "type":type}]>
<#elseif !JavaUtil.isCollection2(classObject, field.name)>
<#--Field: simple entity-->
<#if field.type?ends_with("Entity")>
<#if field.annotations.javax_persistence_JoinColumn?? && field.annotations.javax_persistence_JoinColumn.referencedColumnName?has_content>
<#assign id = field.annotations.javax_persistence_JoinColumn.referencedColumnName>
Expand All @@ -48,31 +50,56 @@ CREATE TABLE ${tableName} (
<#assign fkList = fkList + [{"key": name, "table": table, "id": id}]>
</#if>
<#else>
<#--Field: primitive-->
<#assign type = get_type(field)/>
</#if>
<#assign columns = columns + [{"name": name, "type":type}]>
<#else>
<#if field.annotations.javax_persistence_ManyToMany?? && field.annotations.javax_persistence_JoinTable??>
<#--Field: collection of entity-->
<#assign entity = field.canonicalType?substring(field.canonicalType?index_of("<") + 1,field.canonicalType?length - 1)>
<#assign entityTable = JavaUtil.getEntityTableName(entity)>
<#assign table1 = tableName>
<#assign table2 = entityTable>
<#if field.annotations.javax_persistence_JoinTable.name?has_content>
<#assign refTableName = field.annotations.javax_persistence_JoinTable.name>
<#else>
<#assign refTableName = tableName + "_" + JavaUtil.getEntityTableName(entity)>
<#assign refTableName = table1 + "_" + table2>
</#if>
<#assign table1 = tableName>
<#if field.annotations.javax_persistence_JoinTable.joinColumns?has_content>
<#assign name1 = field.annotations.javax_persistence_JoinTable.joinColumns[0].javax_persistence_JoinColumn.name>
<#if field.annotations.javax_persistence_JoinTable.joinColumns[0].javax_persistence_JoinColumn.referencedColumnName?has_content>
<#assign id1 = field.annotations.javax_persistence_JoinTable.joinColumns[0].javax_persistence_JoinColumn.referencedColumnName>
<#--not yet support multiple JoinColumns or no JoinColumn-->
<#if field.annotations.javax_persistence_JoinTable.joinColumns?has_content
&& field.annotations.javax_persistence_JoinTable.joinColumns?is_enumerable
&& field.annotations.javax_persistence_JoinTable.joinColumns[0]?has_content>
<#assign col = field.annotations.javax_persistence_JoinTable.joinColumns[0].javax_persistence_JoinColumn>
<#assign name1 = col.name>
<#if col.referencedColumnName?has_content>
<#assign id1 = col.referencedColumnName>
<#assign type1 = get_mapping_type(JavaUtil.getCanonicalNameOfField(pojo.canonicalName, id1))>
<#else>
<#assign result = JavaUtil.getPrimaryKey(pojo.canonicalName)?split(",")>
<#assign type1 = get_mapping_type(result[0])>
<#assign id1 = result[1]>
</#if>
<#else>
tbd
<#continue>
</#if>
<#if field.annotations.javax_persistence_JoinTable.inverseJoinColumns?has_content>
<#assign refId2 = field.annotations.javax_persistence_JoinTable.inverseJoinColumns[0].javax_persistence_JoinColumn.name>
<#if field.annotations.javax_persistence_JoinTable.inverseJoinColumns?has_content
&& field.annotations.javax_persistence_JoinTable.inverseJoinColumns?is_enumerable
&& field.annotations.javax_persistence_JoinTable.inverseJoinColumns[0]?has_content>
<#assign col = field.annotations.javax_persistence_JoinTable.inverseJoinColumns[0].javax_persistence_JoinColumn>
<#assign name2 = col.name>
<#if col.referencedColumnName?has_content>
<#assign id2 = col.referencedColumnName>
<#assign type2 = get_mapping_type(JavaUtil.getCanonicalNameOfField(entity, id2))>
<#else>
<#assign result = JavaUtil.getPrimaryKey(entity)?split(",")>
<#assign type2 = get_mapping_type(result[0])>
<#assign id2 = result[1]>
</#if>
<#else>
<#continue>
</#if>
<#assign refTables = refTables + [{"table": refTableName, "columns":[{"name": name1, "id": id1, "type": type1, "table": table1}, {"name": refId2, "id": "ID2", "type": "refType2", "table": "TB2"}] }]>
<#assign refTables = refTables + [{"table": refTableName, "columns":[{"name": name1, "id": id1, "type": type1, "table": table1}, {"name": name2, "id": id2, "type": type2, "table": table2}] }]>
</#if>
</#if>
</#if>
Expand All @@ -90,8 +117,8 @@ CREATE TABLE ${tableName} (
CREATE TABLE ${tb.table} (
<#assign col1 = tb.columns[0]>
<#assign col2 = tb.columns[1]>
${col1.name?right_pad(30)} ${col1.type} NOT NULL,
${col2.name?right_pad(30)} ${col2.type} NOT NULL,
${col1.name?right_pad(30)} ${col1.type} NOT NULL,
${col2.name?right_pad(30)} ${col2.type} NOT NULL,
CONSTRAINT PK_${tb.table} PRIMARY KEY(${col1.name}, ${col2.name}),
CONSTRAINT FK_${tb.table}_${col1.name} FOREIGN KEY(${col1.name}) REFERENCES ${col1.table}(${col1.id}),
CONSTRAINT FK_${tb.table}_${col2.name} FOREIGN KEY(${col2.name}) REFERENCES ${col2.table}(${col2.id}),
Expand Down

0 comments on commit 1b7a6d1

Please sign in to comment.