KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > table > FunctionTable


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.table;
6
7 import java.sql.ResultSet JavaDoc;
8 import java.sql.ResultSetMetaData JavaDoc;
9 import java.sql.SQLException JavaDoc;
10
11 import org.h2.engine.Session;
12 import org.h2.expression.Expression;
13 import org.h2.expression.FunctionCall;
14 import org.h2.index.FunctionIndex;
15 import org.h2.index.Index;
16 import org.h2.index.IndexType;
17 import org.h2.message.Message;
18 import org.h2.result.LocalResult;
19 import org.h2.result.Row;
20 import org.h2.schema.Schema;
21 import org.h2.util.ObjectArray;
22 import org.h2.value.DataType;
23 import org.h2.value.Value;
24 import org.h2.value.ValueResultSet;
25
26 public class FunctionTable extends Table {
27     
28     private FunctionCall function;
29     
30     public FunctionTable(Schema schema, Session session, FunctionCall function) throws SQLException JavaDoc {
31         super(schema, 0, function.getName(), false);
32         this.function = function;
33         function.optimize(session);
34         int type = function.getType();
35         if(type != Value.RESULT_SET) {
36             throw Message.getSQLException(Message.FUNCTION_MUST_RETURN_RESULT_SET_1, function.getName());
37         }
38         int params = function.getParameterCount();
39         Expression[] columnListArgs = new Expression[params];
40         Expression[] args = function.getArgs();
41         for(int i=0; i<params; i++) {
42             args[i] = args[i].optimize(session);
43             columnListArgs[i] = args[i];
44         }
45         ValueResultSet template = function.getValueForColumnList(session, columnListArgs);
46         if(template == null) {
47             throw Message.getSQLException(Message.FUNCTION_MUST_RETURN_RESULT_SET_1, function.getName());
48         }
49         ResultSet JavaDoc rs = template.getResultSet();
50         ResultSetMetaData JavaDoc meta = rs.getMetaData();
51         int columnCount = meta.getColumnCount();
52         Column[] cols = new Column[columnCount];
53         for(int i=0; i<columnCount; i++) {
54             cols[i] = new Column(meta.getColumnName(i+1),
55                     DataType.convertSQLTypeToValueType(meta.getColumnType(i + 1)),
56                     meta.getPrecision(i + 1),
57                     meta.getScale(i + 1));
58         }
59         setColumns(cols);
60     }
61
62     public void lock(Session session, boolean exclusive) throws SQLException JavaDoc {
63     }
64
65     public void close(Session session) throws SQLException JavaDoc {
66     }
67
68     public void unlock(Session s) {
69     }
70     
71     public boolean isLockedExclusively() {
72         return false;
73     }
74
75     public Index addIndex(Session session, String JavaDoc indexName, int indexId, Column[] cols, IndexType indexType, int headPos, String JavaDoc comment) throws SQLException JavaDoc {
76         throw Message.getUnsupportedException();
77     }
78
79     public void removeRow(Session session, Row row) throws SQLException JavaDoc {
80         throw Message.getUnsupportedException();
81     }
82
83     public void truncate(Session session) throws SQLException JavaDoc {
84         throw Message.getUnsupportedException();
85     }
86     
87     public boolean canDrop() {
88         throw Message.getInternalError();
89     }
90
91     public void addRow(Session session, Row row) throws SQLException JavaDoc {
92         throw Message.getUnsupportedException();
93     }
94
95     public void checkSupportAlter() throws SQLException JavaDoc {
96         throw Message.getUnsupportedException();
97     }
98
99     public String JavaDoc getTableType() {
100         throw Message.getInternalError();
101     }
102
103     public Index getScanIndex(Session session) throws SQLException JavaDoc {
104         return new FunctionIndex(this, columns, function);
105     }
106
107     public ObjectArray getIndexes() {
108         return null;
109     }
110
111     public boolean canGetRowCount() {
112         return false;
113     }
114
115     public int getRowCount() throws SQLException JavaDoc {
116         throw Message.getInternalError();
117     }
118
119     public String JavaDoc getCreateSQL() {
120         return null;
121     }
122
123     public void checkRename() throws SQLException JavaDoc {
124         throw Message.getUnsupportedException();
125     }
126     
127     public LocalResult getResult(Session session) throws SQLException JavaDoc {
128         function.optimize(session);
129         ValueResultSet value = (ValueResultSet) function.getValue(session);
130         return LocalResult.read(session, value.getResultSet());
131     }
132
133     public long getMaxDataModificationId() {
134         // TODO optimization: table-as-a-function currently doesn't know the last modified date
135
return Long.MAX_VALUE;
136     }
137
138     public Index getUniqueIndex() {
139         return null;
140     }
141
142 }
143
Popular Tags