001: /*
002:
003: Derby - Class org.apache.derby.impl.load.Export
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.Connection;
025: import java.sql.ResultSet;
026: import java.io.IOException;
027: import java.sql.SQLException;
028: import java.util.*;
029:
030: /**
031: * This class provides ways to export data from
032: * a table or a view into a file. Export functions provided
033: * in this class are called through Systement Procedures.
034: */
035: public class Export extends ExportAbstract {
036:
037: private String outputFileName;
038:
039: private void doExport() throws SQLException {
040: try {
041: if (entityName == null && selectStatement == null)
042: throw LoadError.entityNameMissing();
043:
044: if (outputFileName == null)
045: throw LoadError.dataFileNull();
046: try {
047: doAllTheWork();
048: } catch (IOException iex) {
049: //in case of ioexception, catch it and throw it as our own exception
050: throw LoadError.errorWritingData();
051: }
052: } catch (Exception ex) {
053: throw LoadError.unexpectedError(ex);
054: }
055:
056: }
057:
058: private Export(Connection con, String schemaName, String tableName,
059: String selectStatement, String outputFileName,
060: String characterDelimeter, String columnDelimeter,
061: String codeset) throws SQLException {
062: this .con = con;
063: this .schemaName = schemaName;
064: this .entityName = tableName;
065: this .selectStatement = selectStatement;
066: this .outputFileName = outputFileName;
067: try {
068: controlFileReader = new ControlInfo();
069: controlFileReader.setControlProperties(characterDelimeter,
070: columnDelimeter, codeset);
071: } catch (Exception ex) {
072: throw LoadError.unexpectedError(ex);
073: }
074: }
075:
076: /**
077: * SYSCS_EXPORT_TABLE system Procedure from ij or from a Java application
078: * invokes this method to perform export of a table data to a file.
079: * @param con The Cloudscape database connection URL for the database containing the table
080: * @param schemaName schema name of the table data is being exported from
081: * @param tableName Name of the Table from which data has to be exported.
082: * @param outputFileName Name of the file to which data has to be exported.
083: * @param columnDelimeter Delimiter that seperates columns in the output file
084: * @param characterDelimeter Delimiter that is used to quoate non-numeric types
085: * @param codeset Codeset that should be used to write the data to the file
086: * @exception SQL Exception on errors
087: */
088:
089: public static void exportTable(Connection con, String schemaName,
090: String tableName, String outputFileName,
091: String columnDelimeter, String characterDelimeter,
092: String codeset) throws SQLException {
093:
094: Export fex = new Export(con, schemaName, tableName, null,
095: outputFileName, characterDelimeter, columnDelimeter,
096: codeset);
097:
098: fex.doExport();
099: }
100:
101: /**
102: * SYSCS_EXPORT_QUERY system Procedure from ij or from a Java application
103: * invokes this method to perform export of the data retrieved by select statement to a file.
104: * @param con The Cloudscape database connection URL for the database containing the table
105: * @param selectStatement select query that is used to export the data
106: * @param outputFileName Name of the file to which data has to be exported.
107: * @param columnDelimeter Delimiter that seperates columns in the output file
108: * @param characterDelimeter Delimiter that is used to quiote non-numeric types
109: * @param codeset Codeset that should be used to write the data to the file
110: * @exception SQL Exception on errors
111: */
112: public static void exportQuery(Connection con,
113: String selectStatement, String outputFileName,
114: String columnDelimeter, String characterDelimeter,
115: String codeset) throws SQLException {
116:
117: Export fex = new Export(con, null, null, selectStatement,
118: outputFileName, characterDelimeter, columnDelimeter,
119: codeset);
120: fex.doExport();
121: }
122:
123: /**
124: * For internal use only
125: * @exception Exception if there is an error
126: */
127: //returns the control file reader corresponding to the control file passed
128: protected ExportWriteDataAbstract getExportWriteData()
129: throws Exception {
130: return new ExportWriteData(outputFileName, controlFileReader);
131: }
132: }
|