KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > sqls > impl > ViewImpl


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.sqls.impl;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
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 JavaDoc 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 JavaDoc 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 JavaDoc("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 JavaDoc pData) { data = pData; }
57         public Object JavaDoc 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 JavaDoc getColumns() {
70         List JavaDoc result = new ArrayList JavaDoc();
71         for (Iterator JavaDoc 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 JavaDoc("A views columns cannot be changed.");
80     }
81
82     public Column newColumn(String JavaDoc pName, Type pType) {
83         throw new IllegalStateException JavaDoc("A views columns cannot be changed.");
84     }
85
86     public Column getColumn(Column.Name pName) {
87         if (pName == null) {
88             throw new NullPointerException JavaDoc("Column names must not be null.");
89          }
90         for (Iterator JavaDoc 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 JavaDoc("A view cannot have keys.");
105     }
106     public Index newIndex() {
107         throw new IllegalStateException JavaDoc("A view cannot have indexes.");
108     }
109     public Index newPrimaryKey() {
110         throw new IllegalStateException JavaDoc("A view cannot have a primary key.");
111     }
112     public ForeignKey newForeignKey(Table pReferencedTable) {
113         throw new IllegalStateException JavaDoc("A view cannot have foreign keys.");
114     }
115     public InsertStatement getInsertStatement() {
116         throw new IllegalStateException JavaDoc("A view is not updateable.");
117     }
118     public UpdateStatement getUpdateStatement() {
119         throw new IllegalStateException JavaDoc("A view is not updateable.");
120     }
121     public DeleteStatement getDeleteStatement() {
122         throw new IllegalStateException JavaDoc("A view is not updateable.");
123     }
124     public Index getPrimaryKey() {
125         throw new IllegalStateException JavaDoc("A view cannot have a primary key.");
126     }
127     public Iterator JavaDoc getIndexes() {
128         throw new IllegalStateException JavaDoc("A view cannot have indexes.");
129     }
130     public Iterator JavaDoc getForeignKeys() {
131         throw new IllegalStateException JavaDoc("A view cannot have foreign keys.");
132     }
133
134     public SelectStatement getViewStatement() {
135         return stmt;
136     }
137 }
138
Popular Tags