KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.load.ImportAbstract
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.SQLException JavaDoc;
25 import java.sql.SQLWarning JavaDoc;
26 import java.sql.ResultSetMetaData JavaDoc;
27 import org.apache.derby.vti.VTITemplate;
28 import java.util.ArrayList JavaDoc;
29
30 /**
31  *
32  * <P>
33  */

34 abstract class ImportAbstract extends VTITemplate {
35
36   ControlInfo controlFileReader;
37   ImportReadData importReadData;
38
39   String JavaDoc[] columnNames;
40   int numberOfColumns;
41   int[] columnWidths;
42
43   String JavaDoc[] nextRow;
44
45   ResultSetMetaData JavaDoc importResultSetMetaData;
46   int noOfColumnsExpected;
47
48   private boolean wasNull;
49
50     static final String JavaDoc COLUMNNAMEPREFIX = "COLUMN";
51
52   abstract ImportReadData getImportReadData() throws Exception JavaDoc;
53
54   /** Does all the work
55     * @exception Exception if there is an error
56     */

57   void doAllTheWork() throws Exception JavaDoc {
58
59     //prepare the input file for import. Get the number of columns per row
60
//from the input file.
61
importReadData = getImportReadData();
62     numberOfColumns = importReadData.getNumberOfColumns();
63     if(numberOfColumns == 0)
64     {
65         //file is empty. Assume same number of columns expected
66
//and return no data , But No rows gets insereted.
67
this.numberOfColumns = noOfColumnsExpected;
68     }
69
70     columnWidths = controlFileReader.getColumnWidths();
71     columnNames = new String JavaDoc[numberOfColumns];
72     loadColumnNames();
73     nextRow = new String JavaDoc[numberOfColumns];
74
75     // get the ResultSetMetaData now as we know it's needed
76
importResultSetMetaData =
77         new ImportResultSetMetaData(numberOfColumns, columnNames, columnWidths);
78
79
80     //FIXME don't go through the resultset here. just for testing
81
// while (next()) ;
82
}
83   //the column names will be Column#
84
void loadColumnNames() {
85     for (int i=1; i<=numberOfColumns; i++)
86       columnNames[i-1] = COLUMNNAMEPREFIX + i;
87   }
88
89
90   /** Gets the resultset meta data
91     * @exception SQLException if there is an error
92     */

93   public ResultSetMetaData JavaDoc getMetaData() {
94     return importResultSetMetaData;
95   }
96
97   //all the resultset interface methods
98
/** gets the next row
99     * @exception SQLException if there is an error
100     */

101   public int getRow() throws SQLException JavaDoc {
102     return (importReadData.getCurrentRowNumber());
103   }
104   
105   public boolean next() throws SQLException JavaDoc {
106     try {
107       return (importReadData.readNextRow(nextRow));
108     } catch (Exception JavaDoc ex) {
109         throw LoadError.unexpectedError(ex);
110     }
111   }
112
113   /** closes the resultset
114     * @exception SQLException if there is an error
115     */

116   public void close() throws SQLException JavaDoc {
117     try {
118         if(importReadData!=null)
119             importReadData.closeStream();
120     } catch (Exception JavaDoc ex) {
121         throw LoadError.unexpectedError(ex);
122     }
123   }
124
125   public boolean wasNull() {
126         return wasNull;
127   }
128
129   /**
130     * @exception SQLException if there is an error
131     */

132   public String JavaDoc getString(int columnIndex) throws SQLException JavaDoc {
133     if (columnIndex <= numberOfColumns) {
134
135         String JavaDoc val = nextRow[columnIndex-1];
136         wasNull = (val == null);
137        return val;
138     }
139     else {
140        throw LoadError.invalidColumnNumber(numberOfColumns);
141     }
142   }
143 }
144
Popular Tags