1 21 22 package org.apache.derby.impl.load; 23 24 import java.sql.Connection ; 25 import java.sql.Statement ; 26 import java.sql.ResultSet ; 27 import java.sql.ResultSetMetaData ; 28 import java.sql.DatabaseMetaData ; 29 import java.sql.SQLException ; 30 31 class ExportResultSetForObject { 35 36 private Connection con; 37 private String selectQuery; 38 private ResultSet rs; 39 private int columnCount; 40 private String columnNames[]; 41 private String columnTypes[]; 42 private int columnLengths[]; 43 44 private Statement expStmt = null; 45 private String schemaName; 46 private String tableName; 47 48 51 public ExportResultSetForObject(Connection con, String schemaName, 52 String tableName, String selectQuery 53 ) 54 { 55 this.con = con; 56 if( selectQuery == null) 57 { 58 this.schemaName = schemaName; 59 this.tableName = tableName; 60 61 67 this.selectQuery = "select * from " + 68 (schemaName == null ? "\"" + tableName + "\"" : 69 "\"" + schemaName + "\"" + "." + "\"" + tableName + "\""); 70 } 71 else 72 { 73 this.selectQuery = selectQuery; 74 } 75 } 76 77 78 public ResultSet getResultSet() throws SQLException { 79 rs = null; 80 expStmt = con.createStatement(); 82 rs = expStmt.executeQuery(selectQuery); 83 getMetaDataInfo(); 84 return rs; 85 } 86 87 88 public int getColumnCount() { 89 return columnCount; 90 } 91 92 public String [] getColumnDefinition() { 93 return columnNames; 94 } 95 96 public String [] getColumnTypes() { 97 return columnTypes; 98 } 99 100 public int[] getColumnLengths() { 101 return columnLengths; 102 } 103 104 private void getMetaDataInfo() throws SQLException { 106 ResultSetMetaData metaData = rs.getMetaData(); 107 columnCount = metaData.getColumnCount(); 108 int numColumns = columnCount; 109 columnNames = new String [numColumns]; 110 columnTypes = new String [numColumns]; 111 columnLengths = new int[numColumns]; 112 113 for (int i=0; i<numColumns; i++) { 114 int jdbcTypeId = metaData.getColumnType(i+1); 115 columnNames[i] = metaData.getColumnName(i+1); 116 columnTypes[i] = metaData.getColumnTypeName(i+1); 117 if(!ColumnInfo.importExportSupportedType(jdbcTypeId)) 118 { 119 throw LoadError.nonSupportedTypeColumn( 120 columnNames[i], columnTypes[i]); 121 } 122 123 columnLengths[i] = metaData.getColumnDisplaySize(i+1); 124 } 125 } 126 127 public void close() throws Exception 128 { 129 if(expStmt !=null) 130 expStmt.close(); 131 } 132 } 133 | Popular Tags |