KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.load.ExportAbstract
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.ResultSet JavaDoc;
26 import java.sql.ResultSetMetaData JavaDoc;
27 import java.sql.Types JavaDoc;
28 import java.util.Date JavaDoc;
29
30 /**
31  *
32  * <P>
33  */

34 abstract class ExportAbstract {
35
36   protected ControlInfo controlFileReader;
37   protected ExportResultSetForObject exportResultSetForObject;
38   protected ExportWriteDataAbstract exportWriteData;
39   protected Connection JavaDoc con;
40   protected String JavaDoc entityName; //this can be a plain table name or qualified with schema also
41
protected String JavaDoc schemaName;
42   protected String JavaDoc selectStatement ;
43   
44
45   //following makes the resultset using select * from entityName
46
protected ResultSet JavaDoc resultSetForEntity() throws Exception JavaDoc {
47     exportResultSetForObject = new ExportResultSetForObject(con, schemaName,
48                                                             entityName,
49                                                             selectStatement);
50
51     ResultSet JavaDoc rs = exportResultSetForObject.getResultSet();
52     return rs;
53   }
54
55   //convert resultset data to string array
56
public String JavaDoc[] getOneRowAtATime(ResultSet JavaDoc rs) throws Exception JavaDoc {
57     int columnCount = exportResultSetForObject.getColumnCount();
58
59     ResultSetMetaData JavaDoc rsm=rs.getMetaData();
60     if (rs.next()){
61        String JavaDoc[] rowObjects = new String JavaDoc[columnCount];
62        for (int colNum = 0; colNum < columnCount; colNum++) {
63             rowObjects[colNum]=rs.getString(colNum + 1);
64        }
65        return rowObjects;
66     }
67     rs.close();
68     exportResultSetForObject.close();
69     return null;
70   }
71
72   //returns the control file reader corresponding to the control file passed
73
protected ControlInfo getControlFileReader(){
74       return controlFileReader;
75   }
76
77   protected abstract ExportWriteDataAbstract getExportWriteData() throws Exception JavaDoc;
78
79   protected void doAllTheWork() throws Exception JavaDoc {
80
81     ResultSet JavaDoc rs = null;
82     try {
83         rs = resultSetForEntity();
84         if (rs != null) {
85             ResultSetMetaData JavaDoc rsmeta = rs.getMetaData();
86             int ncols = rsmeta.getColumnCount();
87             boolean[] isNumeric = new boolean[ncols];
88             for (int i = 0; i < ncols; i++) {
89                 int ctype = rsmeta.getColumnType(i+1);
90                 if (ctype == Types.BIGINT || ctype == Types.DECIMAL || ctype == Types.DOUBLE ||
91                         ctype == Types.FLOAT ||ctype == Types.INTEGER || ctype == Types.NUMERIC ||
92                         ctype == Types.REAL ||ctype == Types.SMALLINT || ctype == Types.TINYINT)
93                     isNumeric[i] = true;
94                 else
95                     isNumeric[i] = false;
96             }
97             exportWriteData = getExportWriteData();
98             exportWriteData.writeColumnDefinitionOptionally(
99                         exportResultSetForObject.getColumnDefinition(),
100                         exportResultSetForObject.getColumnTypes());
101             exportWriteData.setColumnLengths(controlFileReader.getColumnWidths());
102
103             //get one row at a time and write it to the output file
104
String JavaDoc[] oneRow = getOneRowAtATime(rs);
105             while (oneRow != null) {
106                 exportWriteData.writeData(oneRow, isNumeric);
107                 oneRow = getOneRowAtATime(rs);
108             }
109         }
110     } finally {
111         //cleanup work after no more rows
112
if (exportWriteData != null)
113             exportWriteData.noMoreRows();
114         if (rs != null)
115             rs.close();
116     }
117   }
118
119
120 }
121
Popular Tags