1 17 package org.apache.ws.jaxme.sqls.impl; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.apache.ws.jaxme.sqls.Column; 24 import org.apache.ws.jaxme.sqls.ColumnReference; 25 import org.apache.ws.jaxme.sqls.DeleteStatement; 26 import org.apache.ws.jaxme.sqls.ForeignKey; 27 import org.apache.ws.jaxme.sqls.Index; 28 import org.apache.ws.jaxme.sqls.InsertStatement; 29 import org.apache.ws.jaxme.sqls.SelectStatement; 30 import org.apache.ws.jaxme.sqls.Table; 31 import org.apache.ws.jaxme.sqls.UpdateStatement; 32 import org.apache.ws.jaxme.sqls.Column.Type; 33 34 35 public class ViewImpl extends TableImpl { 36 private class ViewColumnImpl implements Column { 37 private final Column.Name colName; 38 private final Column col; 39 private Object data; 40 private ViewColumnImpl(ColumnReference pColumn) { 41 col = pColumn.getColumn(); 42 Column.Name alias = pColumn.getAlias(); 43 colName = alias == null ? col.getName() : alias; 44 } 45 public Table getTable() { return ViewImpl.this; } 46 public Name getName() { return colName; } 47 public String getQName() { return getTable() + "." + getName(); } 48 public Type getType() { return col.getType(); } 49 public boolean isPrimaryKeyPart() { return false; } 50 public void setNullable(boolean pNullable) { 51 throw new IllegalStateException ("Unable to set a view columns 'Nullable' property."); 52 } 53 public boolean isNullable() { return col.isNullable(); } 54 public boolean isStringColumn() { return col.isStringColumn(); } 55 public boolean isBinaryColumn() { return col.isBinaryColumn(); } 56 public void setCustomData(Object pData) { data = pData; } 57 public Object getCustomData() { return data; } 58 public boolean isVirtual() { return false; } 59 } 60 61 private final SelectStatement stmt; 62 63 protected ViewImpl(SelectStatement pSelectStatement, Table.Name pName) { 64 super(pSelectStatement.getSelectTableReference().getTable().getSchema(), 65 pName == null ? pSelectStatement.getTableReference().getTable().getName() : pName); 66 stmt = pSelectStatement; 67 } 68 69 public Iterator getColumns() { 70 List result = new ArrayList (); 71 for (Iterator iter = stmt.getResultColumns(); iter.hasNext(); ) { 72 ColumnReference col = (ColumnReference) iter.next(); 73 result.add(new ViewColumnImpl(col)); 74 } 75 return result.iterator(); 76 } 77 78 public Column newColumn(Column.Name pName, Type pType) { 79 throw new IllegalStateException ("A views columns cannot be changed."); 80 } 81 82 public Column newColumn(String pName, Type pType) { 83 throw new IllegalStateException ("A views columns cannot be changed."); 84 } 85 86 public Column getColumn(Column.Name pName) { 87 if (pName == null) { 88 throw new NullPointerException ("Column names must not be null."); 89 } 90 for (Iterator iter = stmt.getResultColumns(); iter.hasNext(); ) { 91 ColumnReference col = (ColumnReference) iter.next(); 92 Column.Name alias = col.getAlias(); 93 if (alias == null) { 94 alias = col.getColumn().getName(); 95 } 96 if (alias.equals(pName)) { 97 return new ViewColumnImpl(col); 98 } 99 } 100 return null; 101 } 102 103 public Index newKey() { 104 throw new IllegalStateException ("A view cannot have keys."); 105 } 106 public Index newIndex() { 107 throw new IllegalStateException ("A view cannot have indexes."); 108 } 109 public Index newPrimaryKey() { 110 throw new IllegalStateException ("A view cannot have a primary key."); 111 } 112 public ForeignKey newForeignKey(Table pReferencedTable) { 113 throw new IllegalStateException ("A view cannot have foreign keys."); 114 } 115 public InsertStatement getInsertStatement() { 116 throw new IllegalStateException ("A view is not updateable."); 117 } 118 public UpdateStatement getUpdateStatement() { 119 throw new IllegalStateException ("A view is not updateable."); 120 } 121 public DeleteStatement getDeleteStatement() { 122 throw new IllegalStateException ("A view is not updateable."); 123 } 124 public Index getPrimaryKey() { 125 throw new IllegalStateException ("A view cannot have a primary key."); 126 } 127 public Iterator getIndexes() { 128 throw new IllegalStateException ("A view cannot have indexes."); 129 } 130 public Iterator getForeignKeys() { 131 throw new IllegalStateException ("A view cannot have foreign keys."); 132 } 133 134 public SelectStatement getViewStatement() { 135 return stmt; 136 } 137 } 138 | Popular Tags |