01: package net.sourceforge.squirrel_sql.plugins.codecompletion.completionfunctions;
02:
03: import java.util.Hashtable;
04: import java.util.Vector;
05:
06: import net.sourceforge.squirrel_sql.client.session.ExtendedColumnInfo;
07: import net.sourceforge.squirrel_sql.client.session.ISession;
08: import net.sourceforge.squirrel_sql.plugins.codecompletion.CodeCompletionInfo;
09:
10: public class Join extends AbstractJoin {
11: private ISession _session;
12: private boolean _returnedLeftJoinBefore;
13:
14: public Join(ISession session) {
15: super (session);
16: _session = session;
17: }
18:
19: public String getCompareString() {
20: return "#j";
21: }
22:
23: public String getCompletionString() {
24: return "#j,<table1>,<table2>,...<tableN>,";
25: }
26:
27: public String toString() {
28: return getCompletionString() + " inner/left join";
29: }
30:
31: public CodeCompletionInfo[] getFunctionResults(String functionSting) {
32: _returnedLeftJoinBefore = false;
33: return super .getFunctionResults(functionSting);
34: }
35:
36: @Override
37: protected String getJoinClause(String fkName, String table1,
38: String table2,
39: Hashtable<String, Vector<ColBuffer>> colBuffersByFkName) {
40: if (_returnedLeftJoinBefore) {
41: return "LEFT JOIN ";
42: }
43:
44: ExtendedColumnInfo[] extCols1 = _session.getSchemaInfo()
45: .getExtendedColumnInfos(table1);
46: ExtendedColumnInfo[] extCols2 = _session.getSchemaInfo()
47: .getExtendedColumnInfos(table2);
48:
49: if (null == fkName) {
50: return "INNER JOIN ";
51: }
52:
53: Vector<ColBuffer> colBufs = colBuffersByFkName.get(fkName);
54:
55: for (int i = 0; i < colBufs.size(); i++) {
56: ColBuffer colBuf = colBufs.get(i);
57:
58: if (colBuf.tableName.equalsIgnoreCase(table1)) {
59: if (isNullable(colBuf.colName, extCols1)) {
60: _returnedLeftJoinBefore = true;
61: return "LEFT JOIN ";
62: }
63: }
64:
65: if (colBuf.tableName.equalsIgnoreCase(table2)) {
66: if (isNullable(colBuf.colName, extCols2)) {
67: _returnedLeftJoinBefore = true;
68: return "LEFT JOIN ";
69: }
70: }
71: }
72: return "INNER JOIN ";
73:
74: }
75:
76: private boolean isNullable(String colName,
77: ExtendedColumnInfo[] extCols) {
78: for (int i = 0; i < extCols.length; i++) {
79: if (extCols[i].getColumnName().equalsIgnoreCase(colName)) {
80: return extCols[i].isNullable();
81: }
82: }
83:
84: throw new IllegalArgumentException("Column " + colName
85: + " not found");
86: }
87:
88: }
|