KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > load > ExportResultSetForObject


1 /*
2
3    Derby - Class org.apache.derby.impl.load.ExportResultSetForObject
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.load;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.ResultSetMetaData JavaDoc;
28 import java.sql.DatabaseMetaData JavaDoc;
29 import java.sql.SQLException JavaDoc;
30
31 //uses the passed connection and table/view name to make the resultset on
32
//that entity. If the entity to be exported has non-sql types in it, an
33
//exception will be thrown
34
class ExportResultSetForObject {
35
36     private Connection JavaDoc con;
37     private String JavaDoc selectQuery;
38     private ResultSet JavaDoc rs;
39     private int columnCount;
40     private String JavaDoc columnNames[];
41     private String JavaDoc columnTypes[];
42     private int columnLengths[];
43
44     private Statement JavaDoc expStmt = null;
45     private String JavaDoc schemaName;
46     private String JavaDoc tableName;
47
48     /* set up the connection and table/view name or the select query
49      * to make the result set, whose data is exported.
50      **/

51     public ExportResultSetForObject(Connection JavaDoc con, String JavaDoc schemaName,
52                                     String JavaDoc tableName, String JavaDoc selectQuery
53                                     )
54     {
55         this.con = con;
56         if( selectQuery == null)
57         {
58             this.schemaName = schemaName;
59             this.tableName = tableName;
60             
61             // delimit schema Name and table Name using quotes because
62
// they can be case-sensitive names or SQL reserved words. Export
63
// procedures are expected to be called with case-senisitive names.
64
// undelimited names are passed in upper case, because that is
65
// the form database stores them.
66

67             this.selectQuery = "select * from " +
68                 (schemaName == null ? "\"" + tableName + "\"" :
69                  "\"" + schemaName + "\"" + "." + "\"" + tableName + "\"");
70         }
71         else
72         {
73             this.selectQuery = selectQuery;
74         }
75     }
76
77
78     public ResultSet JavaDoc getResultSet() throws SQLException JavaDoc {
79         rs = null;
80         //execute the select query and keep it's meta data info ready
81
expStmt = con.createStatement();
82         rs = expStmt.executeQuery(selectQuery);
83         getMetaDataInfo();
84         return rs;
85       }
86
87
88     public int getColumnCount() {
89         return columnCount;
90     }
91
92     public String JavaDoc[] getColumnDefinition() {
93         return columnNames;
94     }
95
96     public String JavaDoc[] getColumnTypes() {
97         return columnTypes;
98     }
99
100     public int[] getColumnLengths() {
101         return columnLengths;
102     }
103
104     //if the entity to be exported has non-sql types in it, an exception will be thrown
105
private void getMetaDataInfo() throws SQLException JavaDoc {
106         ResultSetMetaData JavaDoc metaData = rs.getMetaData();
107         columnCount = metaData.getColumnCount();
108         int numColumns = columnCount;
109         columnNames = new String JavaDoc[numColumns];
110         columnTypes = new String JavaDoc[numColumns];
111         columnLengths = new int[numColumns];
112
113         for (int i=0; i<numColumns; i++) {
114             int jdbcTypeId = metaData.getColumnType(i+1);
115             columnNames[i] = metaData.getColumnName(i+1);
116             columnTypes[i] = metaData.getColumnTypeName(i+1);
117             if(!ColumnInfo.importExportSupportedType(jdbcTypeId))
118             {
119                 throw LoadError.nonSupportedTypeColumn(
120                             columnNames[i], columnTypes[i]);
121             }
122          
123             columnLengths[i] = metaData.getColumnDisplaySize(i+1);
124         }
125     }
126
127     public void close() throws Exception JavaDoc
128     {
129         if(expStmt !=null)
130             expStmt.close();
131     }
132 }
133
Popular Tags