--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.load.ExportAbstract\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.load;\r
+\r
+import java.sql.Connection;\r
+import java.sql.ResultSet;\r
+import java.sql.ResultSetMetaData;\r
+import java.sql.Types;\r
+import java.util.Date;\r
+import java.io.InputStream;\r
+import java.io.Reader;\r
+\r
+/**\r
+ * \r
+ * <P>\r
+ */\r
+abstract class ExportAbstract {\r
+\r
+ protected ControlInfo controlFileReader;\r
+ protected ExportResultSetForObject exportResultSetForObject;\r
+ protected ExportWriteDataAbstract exportWriteData;\r
+ protected Connection con;\r
+ protected String entityName; //this can be a plain table name or qualified with schema also\r
+ protected String schemaName;\r
+ protected String selectStatement ;\r
+ protected boolean lobsInExtFile = false;\r
+\r
+ //following makes the resultset using select * from entityName\r
+ protected ResultSet resultSetForEntity() throws Exception {\r
+ exportResultSetForObject = new ExportResultSetForObject(con, schemaName,\r
+ entityName, \r
+ selectStatement);\r
+\r
+ ResultSet rs = exportResultSetForObject.getResultSet();\r
+ return rs;\r
+ }\r
+\r
+ /** convert resultset data for the current row to string array. \r
+ * If large objects are being exported to an external file, \r
+ * then write the lob data into the external file and store \r
+ * the lob data location in the string array for that column.\r
+ * @param rs resultset that contains the data to export.\r
+ * @param isLargeBinary boolean array, whose elements will\r
+ * be true, if the column type is blob/or \r
+ * other large binary type, otherwise false. \r
+ * @param isLargeChar boolean array, whose elements will\r
+ * be true, if the column type is clob/ \r
+ * other large char type, otherwise false. \r
+ * @return A string array of the row data to write to export file.\r
+ * @exception Exception if any errors during conversion. \r
+ */\r
+ private String[] getOneRowAtATime(ResultSet rs, \r
+ boolean[] isLargeBinary, \r
+ boolean[] isLargeChar) \r
+ throws Exception \r
+ {\r
+ int columnCount = exportResultSetForObject.getColumnCount();\r
+\r
+ ResultSetMetaData rsm=rs.getMetaData();\r
+ if (rs.next()){\r
+ String[] rowObjects = new String[columnCount];\r
+ for (int colNum = 0; colNum < columnCount; colNum++) {\r
+ if (lobsInExtFile && \r
+ (isLargeChar[colNum] || isLargeBinary[colNum])) \r
+ { \r
+ String LobExtLocation;\r
+ if (isLargeBinary[colNum]) {\r
+\r
+ // get input stream that has the column value as a \r
+ // stream of uninterpreted bytes; if the value is SQL NULL, \r
+ // the return value is null\r
+ InputStream is = rs.getBinaryStream(colNum + 1);\r
+ LobExtLocation = \r
+ exportWriteData.writeBinaryColumnToExternalFile(is);\r
+ } else {\r
+ // It is clob data, get character stream that has \r
+ // the column value. if the value is SQL NULL, the \r
+ // return value is null\r
+ Reader ir = rs.getCharacterStream(colNum + 1);\r
+ LobExtLocation = \r
+ exportWriteData.writeCharColumnToExternalFile(ir);\r
+ }\r
+ rowObjects[colNum]= LobExtLocation;\r
+\r
+ // when lob data is written to the main export file, binary \r
+ // data is written in hex format. getString() call on binary \r
+ // columns returns the data in hex format, no special handling \r
+ // required. In case of large char tpe like Clob, data \r
+ // is written to main export file similar to other char types. \r
+ \r
+ // TODO : handling of Nulls. \r
+ }\r
+ else {\r
+ rowObjects[colNum]=rs.getString(colNum + 1);\r
+ }\r
+ }\r
+ return rowObjects;\r
+ }\r
+ rs.close();\r
+ exportResultSetForObject.close();\r
+ return null;\r
+ }\r
+\r
+ //returns the control file reader corresponding to the control file passed\r
+ protected ControlInfo getControlFileReader(){\r
+ return controlFileReader; \r
+ }\r
+\r
+ protected abstract ExportWriteDataAbstract getExportWriteData() throws Exception;\r
+\r
+ protected void doAllTheWork() throws Exception {\r
+\r
+ ResultSet rs = null;\r
+ try {\r
+ rs = resultSetForEntity();\r
+ if (rs != null) {\r
+ ResultSetMetaData rsmeta = rs.getMetaData();\r
+ int ncols = rsmeta.getColumnCount();\r
+ boolean[] isNumeric = new boolean[ncols];\r
+ boolean[] isLargeChar = new boolean[ncols];\r
+ boolean[] isLargeBinary = new boolean[ncols];\r
+ for (int i = 0; i < ncols; i++) {\r
+ int ctype = rsmeta.getColumnType(i+1);\r
+ if (ctype == Types.BIGINT || ctype == Types.DECIMAL || ctype == Types.DOUBLE ||\r
+ ctype == Types.FLOAT ||ctype == Types.INTEGER || ctype == Types.NUMERIC ||\r
+ ctype == Types.REAL ||ctype == Types.SMALLINT || ctype == Types.TINYINT)\r
+ isNumeric[i] = true;\r
+ else \r
+ isNumeric[i] = false;\r
+ \r
+ if (ctype == Types.CLOB)\r
+ isLargeChar[i] = true;\r
+ else \r
+ isLargeChar[i]= false;\r
+ \r
+ if (ctype == Types.BLOB) \r
+ isLargeBinary[i] = true;\r
+ else \r
+ isLargeBinary[i] = false;\r
+ }\r
+\r
+\r
+ exportWriteData = getExportWriteData();\r
+ exportWriteData.writeColumnDefinitionOptionally(\r
+ exportResultSetForObject.getColumnDefinition(),\r
+ exportResultSetForObject.getColumnTypes());\r
+ exportWriteData.setColumnLengths(controlFileReader.getColumnWidths());\r
+\r
+ // get one row at a time and write it to the output file\r
+ String[] oneRow = getOneRowAtATime(rs, \r
+ isLargeBinary, \r
+ isLargeChar);\r
+ while (oneRow != null) {\r
+ exportWriteData.writeData(oneRow, isNumeric);\r
+ oneRow = getOneRowAtATime(rs, isLargeBinary, isLargeChar);\r
+ }\r
+ }\r
+ } finally {\r
+ //cleanup work after no more rows\r
+ if (exportWriteData != null)\r
+ exportWriteData.noMoreRows();\r
+ if (rs != null)\r
+ rs.close();\r
+ }\r
+ }\r
+}\r