1
2
3
4
5
6
7 package org.asyrinx.joey.gen.command.rdb;
8
9 import org.asyrinx.brownie.core.lang.StringUtils;
10 import org.asyrinx.joey.gen.model.rdb.Column;
11 import org.asyrinx.joey.gen.model.rdb.Database;
12 import org.asyrinx.joey.gen.model.rdb.ForeignKey;
13 import org.asyrinx.joey.gen.model.rdb.ForeignKeyEntry;
14 import org.asyrinx.joey.gen.model.rdb.ForeignKeyType;
15 import org.asyrinx.joey.gen.model.rdb.Table;
16
17 /***
18 * @author takeshi
19 */
20 public class PrepareColumnFk extends RdbCommand {
21
22 public void visit(Column column) {
23 final ForeignKeyType foreignKeyType = ForeignKeyType.get(column.getFkType());
24 buildFk(column, column.getFk(), foreignKeyType);
25 }
26
27 /***
28 * @param column
29 */
30 private ForeignKey buildFk(Column column, String foreignColumnName, ForeignKeyType fkType) {
31 if (fkType == ForeignKeyType.EXTENDS)
32 throw new UnsupportedOperationException("fkType must be " + ForeignKeyType.NORMAL.getName() + " or "
33 + ForeignKeyType.BIDIRECTION.getName());
34 if (StringUtils.isEmpty(foreignColumnName))
35 return null;
36 final Table localTable = column.getParent();
37 final Database db = localTable.getParent();
38 final Column fkCol = db.getColumn(foreignColumnName);
39 if (fkCol == null) {
40 addError(column, "cannot find:" + foreignColumnName);
41 return null;
42 }
43 final ForeignKey foreignKey = new ForeignKey(localTable, fkCol.getParent().getName());
44 foreignKey.setLabel(column.getLabel());
45 foreignKey.setType(fkType);
46 foreignKey.setCascade(column.getFkCascade());
47 new ForeignKeyEntry(foreignKey, column.getName(), fkCol.getName());
48 this.log(foreignKey, "create FK from " + column.getFullName());
49 return foreignKey;
50 }
51
52 }