1 19 package org.netbeans.modules.db.sql.visualeditor.querymodel; 20 21 import java.util.ArrayList ; 22 import java.util.List ; 23 import java.util.Collection ; 24 25 28 public class FromNode implements From { 29 30 32 34 ArrayList _tableList; 35 36 37 39 public FromNode() { 40 } 41 42 public FromNode(ArrayList tableList) { 43 _tableList = tableList; 44 } 45 46 47 49 public String genText() { 51 String res = ""; 53 if (_tableList.size() > 0) { 54 55 res = "\nFROM " + ((JoinTableNode)_tableList.get(0)).genText(true); 57 for (int i=1; i<_tableList.size(); i++) 58 res += ((JoinTableNode)_tableList.get(i)).genText(); 59 } 60 61 return res; 62 } 63 64 65 public String toString() { 66 67 String res = "FROM:\n"; 69 for (int i=0; i<_tableList.size(); i++) 70 res += "\t" + ((JoinTableNode)_tableList.get(i)).toString() + "\n"; 72 return res; 73 } 74 75 76 78 80 public List getTableList() { 81 82 return _tableList; 83 } 84 85 87 ArrayList getTables() { 88 ArrayList tableRefs = new ArrayList (); 89 for (int i=0; i<_tableList.size(); i++) 90 tableRefs.add(((JoinTableNode)_tableList.get(i)).getTable()); 91 return tableRefs; 92 } 93 94 public String getPreviousTableFullName() { 97 JoinTableNode jt = (JoinTableNode)_tableList.get(_tableList.size()-2); 98 return jt.getTable().getFullTableName(); 99 } 100 101 102 104 public Table findTable(String tableSpec) { 105 for (int i=0; i<_tableList.size(); i++) { 106 JoinTableNode jt = (JoinTableNode) _tableList.get(i); 107 if (jt.getTableSpec().equals(tableSpec)) 108 return jt.getTable(); 109 } 110 return null; 111 } 112 113 public JoinTable findJoinTable(String table1, String column1, String table2, String column2) { 114 ArrayList tableList = _tableList; 115 for (int i=0; i<tableList.size(); i++) { 116 JoinTableNode jt = (JoinTableNode) tableList.get(i); 117 Expression cond = jt.getExpression(); 118 if (cond instanceof Predicate) { 119 Predicate pred = (Predicate) cond; 120 Value val1 = pred.getVal1(); 121 Value val2 = pred.getVal2(); 122 if ((val1 instanceof ColumnNode) && (val2 instanceof ColumnNode)) { 123 ColumnNode col1 = (ColumnNode) val1; 124 ColumnNode col2 = (ColumnNode) val2; 125 if (((col1.getTableSpec().equals(table1)) && 126 (col1.getColumnName().equals(column1)) && 127 (col2.getTableSpec().equals(table2)) && 128 (col2.getColumnName().equals(column2))) || 129 ((col2.getTableSpec().equals(table1)) && 130 (col2.getColumnName().equals(column1)) && 131 (col1.getTableSpec().equals(table2)) && 132 (col1.getColumnName().equals(column2)))) { 133 return jt; 134 } 135 } 136 } 137 } 138 return null; 139 } 140 141 public String getFullTableName(String corrName) { 142 for (int i=0; i<_tableList.size(); i++) { 143 JoinTable jt = (JoinTable) _tableList.get(i); 144 String cn=jt.getTableSpec(); 145 if ((cn!=null) && (cn.equals(corrName))) 146 return jt.getFullTableName(); 147 } 148 return null; 149 } 150 151 public String getTableSpec(String fullTableName) { 152 for (int i=0; i<_tableList.size(); i++) { 153 JoinTable jt = (JoinTable) _tableList.get(i); 154 String cn=jt.getFullTableName(); 155 if ((cn!=null) && (cn.equals(fullTableName))) 156 return jt.getTableSpec(); 157 } 158 return null; 159 } 160 161 162 164 public void addTable(JoinTable jt) { 165 _tableList.add(jt); 166 } 167 168 void removeTable(String tableSpec) { 170 for (int i=_tableList.size()-1; i>=0; i--) 171 if (((JoinTableNode)_tableList.get(i)).getTableSpec().equals(tableSpec)) 172 _tableList.remove(i); 173 } 174 175 178 void renameTableSpec(String oldTableSpec, String corrName) { 179 for (int i=0; i<_tableList.size(); i++) { 180 JoinTableNode jt = (JoinTableNode) _tableList.get(i); 181 jt.renameTableSpec(oldTableSpec, corrName); 182 } 183 } 184 185 186 189 public void setTableSpec(String oldTableSpec, String newTableSpec) { 190 for (int i=0; i<_tableList.size(); i++) { 191 JoinTableNode jt = (JoinTableNode) _tableList.get(i); 192 jt.setTableSpec(oldTableSpec, newTableSpec); 193 } 194 } 195 196 public void getReferencedColumns(Collection columns) {} 197 public void getQueryItems(Collection items) {} 198 } 199 | Popular Tags |