001: package net.sourceforge.squirrel_sql.plugins.graph;
002:
003: import java.util.Arrays;
004: import java.util.Vector;
005:
006: import net.sourceforge.squirrel_sql.plugins.graph.xmlbeans.ColumnInfoXmlBean;
007: import net.sourceforge.squirrel_sql.plugins.graph.xmlbeans.ConstraintDataXmlBean;
008:
009: public class ConstraintData {
010: private String _pkTableName;
011: private String _fkTableName;
012: private String _constraintName;
013:
014: private ColumnInfo[] _columnInfos = new ColumnInfo[0];
015:
016: public ConstraintData(String pkTableName, String fkTableName,
017: String constraintName) {
018: _pkTableName = pkTableName;
019: _fkTableName = fkTableName;
020: _constraintName = constraintName;
021: }
022:
023: public ConstraintData(ConstraintDataXmlBean constraintDataXmlBean) {
024: _pkTableName = constraintDataXmlBean.getPkTableName();
025: _fkTableName = constraintDataXmlBean.getFkTableName();
026: _constraintName = constraintDataXmlBean.getConstraintName();
027:
028: _columnInfos = new ColumnInfo[constraintDataXmlBean
029: .getColumnInfoXmlBeans().length];
030: for (int i = 0; i < _columnInfos.length; i++) {
031: _columnInfos[i] = new ColumnInfo(constraintDataXmlBean
032: .getColumnInfoXmlBeans()[i]);
033: }
034: }
035:
036: public ConstraintDataXmlBean getXmlBean() {
037: ConstraintDataXmlBean ret = new ConstraintDataXmlBean();
038: ret.setPkTableName(_pkTableName);
039: ret.setFkTableName(_fkTableName);
040: ret.setConstraintName(_constraintName);
041:
042: ColumnInfoXmlBean[] colInfoXmlBeans = new ColumnInfoXmlBean[_columnInfos.length];
043: for (int i = 0; i < _columnInfos.length; i++) {
044: colInfoXmlBeans[i] = _columnInfos[i].getXmlBean();
045: }
046: ret.setColumnInfoXmlBeans(colInfoXmlBeans);
047:
048: return ret;
049: }
050:
051: public void addColumnInfo(ColumnInfo colInfo) {
052: Vector<ColumnInfo> buf = new Vector<ColumnInfo>();
053: buf.addAll(Arrays.asList(_columnInfos));
054: buf.add(colInfo);
055:
056: _columnInfos = buf.toArray(new ColumnInfo[buf.size()]);
057: }
058:
059: public String getPkTableName() {
060: return _pkTableName;
061: }
062:
063: public ColumnInfo[] getColumnInfos() {
064: return _columnInfos;
065: }
066:
067: public String getTitle() {
068: return _fkTableName + "." + _constraintName;
069: }
070:
071: public String[] getDDL() {
072: Vector<String> ret = new Vector<String>();
073:
074: ret.add("ALTER TABLE " + _fkTableName);
075: ret.add("ADD CONSTRAINT " + _constraintName);
076:
077: if (_columnInfos.length == 1) {
078: StringBuffer sb = new StringBuffer();
079: sb.append("FOREIGN KEY (")
080: .append(_columnInfos[0].getName());
081:
082: for (int i = 1; i < _columnInfos.length; i++) {
083: sb.append(",").append(_columnInfos[i].getName());
084: }
085: sb.append(")");
086: ret.add(sb.toString());
087:
088: sb.setLength(0);
089:
090: sb.append("REFERENCES ").append(_pkTableName).append("(");
091: sb.append(_columnInfos[0].getImportedColumnName());
092: for (int i = 1; i < _columnInfos.length; i++) {
093: sb.append(",").append(
094: _columnInfos[i].getImportedColumnName());
095: }
096: sb.append(")");
097: ret.add(sb.toString());
098:
099: } else {
100: ret.add("FOREIGN KEY");
101: ret.add("(");
102: for (int i = 0; i < _columnInfos.length; i++) {
103: if (i < _columnInfos.length - 1) {
104: ret.add(" " + _columnInfos[i].getName() + ",");
105: } else {
106: ret.add(" " + _columnInfos[i].getName());
107: }
108: }
109: ret.add(")");
110:
111: ret.add("REFERENCES " + _pkTableName);
112: ret.add("(");
113: for (int i = 0; i < _columnInfos.length; i++) {
114: if (i < _columnInfos.length - 1) {
115: ret.add(" "
116: + _columnInfos[i].getImportedColumnName()
117: + ",");
118: } else {
119: ret.add(" "
120: + _columnInfos[i].getImportedColumnName());
121: }
122: }
123: ret.add(")");
124:
125: }
126:
127: return ret.toArray(new String[ret.size()]);
128: }
129:
130: public void replaceCopiedColsByReferences(ColumnInfo[] colInfoRefs) {
131: for (int i = 0; i < colInfoRefs.length; i++) {
132: for (int j = 0; j < _columnInfos.length; j++) {
133: if (colInfoRefs[i].getName().equals(
134: _columnInfos[j].getName())) {
135: _columnInfos[j] = colInfoRefs[i];
136: break;
137: }
138: }
139: }
140: }
141:
142: public String getConstraintName() {
143: return _constraintName;
144: }
145: }
|