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 public class QueryNode implements Query { 26 27 29 SelectNode _select; 30 FromNode _from; 31 WhereNode _where; 32 GroupByNode _groupBy; 33 HavingNode _having; 34 OrderByNode _orderBy; 35 36 37 39 public QueryNode() { 40 } 41 42 public QueryNode(SelectNode select, FromNode from, WhereNode where, 43 GroupByNode groupBy, HavingNode having, OrderByNode orderBy) { 44 _select = select; 45 _from = from; 46 _where = where; 47 _groupBy = groupBy; 48 _having = having; 49 _orderBy = orderBy; 50 } 51 52 public QueryNode(SelectNode select, FromNode from) { 53 this(select, from, null, null, null, null); 54 } 55 56 57 59 61 public String genText() { 62 String res = _select.genText() + " " + _from.genText(); 64 if (_where!=null) 65 res += _where.genText(); 66 67 if (_groupBy!=null) 68 res += _groupBy.genText(); 69 70 if (_having!=null) 71 res += _having.genText(); 72 73 if (_orderBy!=null) 74 res += _orderBy.genText(); 75 76 return res; 77 } 78 79 80 82 public String toString() { 83 return (_select.toString() + 84 _from.toString() + 85 _where.toString() ); 86 } 87 88 89 91 public Select getSelect() { 92 return _select; 93 } 94 95 public void setSelect(Select select) { 96 _select = (SelectNode)select; 97 } 98 99 public From getFrom() { 100 return _from; 101 } 102 103 public void setFrom(From from) { 104 _from = (FromNode)from; 105 } 106 107 public Where getWhere() { 108 return _where; 109 } 110 111 public void setWhere(Where where) { 112 _where = (WhereNode)where; 113 } 114 115 public GroupBy getGroupBy() { 116 return _groupBy; 117 } 118 119 public void setGroupBy(GroupBy groupBy) { 120 _groupBy = (GroupByNode)groupBy; 121 } 122 123 public OrderBy getOrderBy() { 124 return _orderBy; 125 } 126 127 public void setOrderBy(OrderBy orderBy) { 128 _orderBy = (OrderByNode)orderBy; 129 } 130 131 public Having getHaving() { 132 return _having; 133 } 134 135 public void setHaving(Having having) { 136 _having = (HavingNode)having; 137 } 138 139 public void removeTable (String tableSpec) { 140 _from.removeTable(tableSpec); 142 143 145 _select.removeTable(tableSpec); 147 148 if (_where!=null) { 150 _where.removeTable(tableSpec); 151 if (_where.getExpression() == null) 152 _where = null; 153 } 154 155 if (_groupBy!=null) 157 { 158 _groupBy.removeTable(tableSpec); 159 if (_from._tableList.size() == 0) 160 _groupBy = null; 161 } 162 removeSortSpecification(tableSpec); 163 } 164 165 public void replaceStar(ColumnProvider tableReader) { 166 if (_select.hasAsteriskQualifier()) { 168 ArrayList columns = new ArrayList (); 170 171 ArrayList tables = _from.getTables(); 173 174 for (int i=0; i<tables.size(); i++) { 176 TableNode tbl = (TableNode) tables.get(i); 177 String fullTableName = tbl.getFullTableName(); 178 List columnNames = new ArrayList (); 179 tableReader.getColumnNamesFull(fullTableName, columnNames); 180 String corrName=tbl.getCorrName(); 181 String tableName=tbl.getTableName(); 182 String schemaName=tbl.getSchemaName(); 183 for (int j=0; j<columnNames.size(); j++) { 184 String columnName = (String ) columnNames.get(j); 185 columns.add(new ColumnNode(tableName, columnName, corrName, schemaName)); 186 } 187 } 188 _select.setColumnList(columns); 189 } 190 } 191 192 public void addColumn(String tableSpec, String columnName) { 193 Table table = _from.findTable(tableSpec); 196 ColumnNode col = new ColumnNode(table, columnName); 197 198 _select.addColumn(col); 200 if (_groupBy != null) 201 _groupBy.addColumn(col); 202 } 203 204 public void removeColumn(String tableSpec, String columnName) { 205 _select.removeColumn(tableSpec, columnName); 206 if (_groupBy != null) 207 _groupBy.removeColumn(tableSpec, columnName); 208 removeSortSpecification(tableSpec, columnName); 210 } 211 212 public void renameTableSpec(String oldTableSpec, String corrName) { 213 _from.renameTableSpec(oldTableSpec, corrName); 214 _select.renameTableSpec(oldTableSpec, corrName); 215 if (_where!=null) 216 _where.renameTableSpec(oldTableSpec, corrName); 217 if (_groupBy!=null) 218 _groupBy.renameTableSpec(oldTableSpec, corrName); 219 if (_having!=null) 220 _having.renameTableSpec(oldTableSpec, corrName); 221 if (_orderBy!=null) 222 _orderBy.renameTableSpec(oldTableSpec, corrName); 223 } 224 225 public void getReferencedColumns(Collection columns) { 226 _from.getReferencedColumns(columns); 227 _select.getReferencedColumns(columns); 228 if (_where!=null) 229 _where.getReferencedColumns(columns); 230 if (_groupBy!=null) 231 _groupBy.getReferencedColumns(columns); 232 if (_having!=null) 233 _having.getReferencedColumns(columns); 234 if (_orderBy!=null) 235 _orderBy.getReferencedColumns(columns); 236 } 237 238 public void getQueryItems(Collection items) { 239 items.add(_from); 240 items.add(_select); 241 if (_where!=null) 242 items.add(_where); 243 if (_groupBy!=null) 244 items.add(_groupBy); 245 if (_having!=null) 246 items.add(_having); 247 if (_orderBy!=null) 248 items.add(_orderBy); 249 } 250 254 private void removeSortSpecification(String tableSpec) { 255 if (_orderBy!=null) 256 _orderBy.removeSortSpecification(tableSpec); 257 } 258 259 private void removeSortSpecification(String tableSpec, String columnName) { 260 if (_orderBy!=null) 261 _orderBy.removeSortSpecification(tableSpec, columnName); 262 } 263 264 } 265 266 267 | Popular Tags |