001: /*
002:
003: Derby - Class org.apache.derby.impl.load.ImportAbstract
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.load;
023:
024: import java.sql.SQLException;
025: import java.sql.SQLWarning;
026: import java.sql.ResultSetMetaData;
027: import org.apache.derby.vti.VTITemplate;
028: import java.util.ArrayList;
029:
030: /**
031: *
032: * <P>
033: */
034: abstract class ImportAbstract extends VTITemplate {
035:
036: ControlInfo controlFileReader;
037: ImportReadData importReadData;
038:
039: String[] columnNames;
040: int numberOfColumns;
041: int[] columnWidths;
042:
043: String[] nextRow;
044:
045: ResultSetMetaData importResultSetMetaData;
046: int noOfColumnsExpected;
047:
048: private boolean wasNull;
049:
050: static final String COLUMNNAMEPREFIX = "COLUMN";
051:
052: abstract ImportReadData getImportReadData() throws Exception;
053:
054: /** Does all the work
055: * @exception Exception if there is an error
056: */
057: void doAllTheWork() throws Exception {
058:
059: //prepare the input file for import. Get the number of columns per row
060: //from the input file.
061: importReadData = getImportReadData();
062: numberOfColumns = importReadData.getNumberOfColumns();
063: if (numberOfColumns == 0) {
064: //file is empty. Assume same number of columns expected
065: //and return no data , But No rows gets insereted.
066: this .numberOfColumns = noOfColumnsExpected;
067: }
068:
069: columnWidths = controlFileReader.getColumnWidths();
070: columnNames = new String[numberOfColumns];
071: loadColumnNames();
072: nextRow = new String[numberOfColumns];
073:
074: // get the ResultSetMetaData now as we know it's needed
075: importResultSetMetaData = new ImportResultSetMetaData(
076: numberOfColumns, columnNames, columnWidths);
077:
078: //FIXME don't go through the resultset here. just for testing
079: // while (next()) ;
080: }
081:
082: //the column names will be Column#
083: void loadColumnNames() {
084: for (int i = 1; i <= numberOfColumns; i++)
085: columnNames[i - 1] = COLUMNNAMEPREFIX + i;
086: }
087:
088: /** Gets the resultset meta data
089: * @exception SQLException if there is an error
090: */
091: public ResultSetMetaData getMetaData() {
092: return importResultSetMetaData;
093: }
094:
095: //all the resultset interface methods
096: /** gets the next row
097: * @exception SQLException if there is an error
098: */
099: public int getRow() throws SQLException {
100: return (importReadData.getCurrentRowNumber());
101: }
102:
103: public boolean next() throws SQLException {
104: try {
105: return (importReadData.readNextRow(nextRow));
106: } catch (Exception ex) {
107: throw LoadError.unexpectedError(ex);
108: }
109: }
110:
111: /** closes the resultset
112: * @exception SQLException if there is an error
113: */
114: public void close() throws SQLException {
115: try {
116: if (importReadData != null)
117: importReadData.closeStream();
118: } catch (Exception ex) {
119: throw LoadError.unexpectedError(ex);
120: }
121: }
122:
123: public boolean wasNull() {
124: return wasNull;
125: }
126:
127: /**
128: * @exception SQLException if there is an error
129: */
130: public String getString(int columnIndex) throws SQLException {
131: if (columnIndex <= numberOfColumns) {
132:
133: String val = nextRow[columnIndex - 1];
134: wasNull = (val == null);
135: return val;
136: } else {
137: throw LoadError.invalidColumnNumber(numberOfColumns);
138: }
139: }
140: }
|