Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/derby/impl/load/ExportAbstract.java


1   /*
2   
3      Derby - Class org.apache.derby.impl.load.ExportAbstract
4   
5      Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable.
6   
7      Licensed under the Apache License, Version 2.0 (the "License");
8      you may not use this file except in compliance with the License.
9      You may obtain a copy of the License at
10  
11        http://www.apache.org/licenses/LICENSE-2.0
12  
13     Unless required by applicable law or agreed to in writing, software
14     distributed under the License is distributed on an "AS IS" BASIS,
15     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16     See the License for the specific language governing permissions and
17     limitations under the License.
18  
19   */
20  
21  package org.apache.derby.impl.load;
22  
23  import java.sql.Connection;
24  import java.sql.ResultSet;
25  import java.sql.ResultSetMetaData;
26  import java.sql.Types;
27  import java.util.Date;
28  
29  /**
30   * 
31   * <P>
32   */
33  abstract class ExportAbstract {
34  
35    protected ControlInfo controlFileReader;
36    protected ExportResultSetForObject exportResultSetForObject;
37    protected ExportWriteDataAbstract exportWriteData;
38    protected Connection con;
39    protected String entityName;  //this can be a plain table name or qualified with schema also
40    protected String schemaName;
41    protected String selectStatement ;
42    
43  
44    //following makes the resultset using select * from entityName
45    protected ResultSet resultSetForEntity() throws Exception {
46      exportResultSetForObject = new ExportResultSetForObject(con, schemaName,
47                                entityName, 
48                                selectStatement);
49  
50      ResultSet rs = exportResultSetForObject.getResultSet();
51      return rs;
52    }
53  
54    //convert resultset data to string array
55    public String[] getOneRowAtATime(ResultSet rs) throws Exception {
56      int columnCount = exportResultSetForObject.getColumnCount();
57  
58    ResultSetMetaData rsm=rs.getMetaData();
59      if (rs.next()){
60         String[] rowObjects = new String[columnCount];
61         for (int colNum = 0; colNum < columnCount; colNum++) {
62             if (rs.getObject(colNum + 1) != null)
63        {
64          rowObjects[colNum]=rs.getString(colNum + 1);
65        }
66         }
67         return rowObjects;
68      }
69      rs.close();
70    exportResultSetForObject.close();
71      return null;
72    }
73  
74    //returns the control file reader corresponding to the control file passed
75    protected ControlInfo getControlFileReader(){
76      return controlFileReader; 
77    }
78  
79    protected abstract ExportWriteDataAbstract getExportWriteData() throws Exception;
80  
81    protected void doAllTheWork() throws Exception {
82  
83    ResultSet rs = null;
84    try {
85        rs = resultSetForEntity();
86        if (rs != null) {
87        ResultSetMetaData rsmeta = rs.getMetaData();
88        int ncols = rsmeta.getColumnCount();
89        boolean[] isNumeric = new boolean[ncols];
90        for (int i = 0; i < ncols; i++) {
91          int ctype = rsmeta.getColumnType(i+1);
92          if (ctype == Types.BIGINT || ctype == Types.DECIMAL || ctype == Types.DOUBLE ||
93              ctype == Types.FLOAT ||ctype == Types.INTEGER || ctype == Types.NUMERIC ||
94              ctype == Types.REAL ||ctype == Types.SMALLINT || ctype == Types.TINYINT)
95              isNumeric[i] = true;
96          else
97            isNumeric[i] = false;
98        }
99        exportWriteData = getExportWriteData();
100       exportWriteData.writeColumnDefinitionOptionally(
101             exportResultSetForObject.getColumnDefinition(),
102             exportResultSetForObject.getColumnTypes());
103       exportWriteData.setColumnLengths(controlFileReader.getColumnWidths());
104 
105            //get one row at a time and write it to the output file
106            String[] oneRow = getOneRowAtATime(rs);
107            while (oneRow != null) {
108              exportWriteData.writeData(oneRow, isNumeric);
109              oneRow = getOneRowAtATime(rs);
110            }
111     }
112   } finally {
113     //cleanup work after no more rows
114     if (exportWriteData != null)
115       exportWriteData.noMoreRows();
116     if (rs != null)
117       rs.close();
118     }
119   }
120 
121 
122 }