1 21 22 package org.apache.derby.impl.load; 23 24 import java.sql.ResultSet ; 25 import java.sql.SQLException ; 26 import java.sql.SQLWarning ; 27 import java.sql.Statement ; 28 import java.sql.PreparedStatement ; 29 import java.sql.Connection ; 30 import java.sql.ResultSetMetaData ; 31 import java.sql.DatabaseMetaData ; 32 import java.util.*; 33 34 40 41 public class Import extends ImportAbstract{ 42 43 private String inputFileName; 44 45 50 public Import(String inputFileName, String columnDelimiter, 51 String characterDelimiter, String codeset, 52 int noOfColumnsExpected) throws SQLException 53 { 54 55 try{ 56 this.inputFileName = inputFileName; 57 this.noOfColumnsExpected = noOfColumnsExpected; 58 controlFileReader = new ControlInfo(); 59 controlFileReader.setControlProperties(characterDelimiter, 60 columnDelimiter, codeset); 61 doImport(); 62 63 }catch(Exception e) 64 { 65 throw LoadError.unexpectedError(e); 66 } 67 } 68 69 70 private void doImport() throws Exception 71 { 72 if (inputFileName == null) 73 throw LoadError.dataFileNull(); 74 doAllTheWork(); 75 76 } 77 78 79 93 94 public static void importTable(Connection connection, String schemaName, 95 String tableName, String inputFileName, 96 String columnDelimiter, String characterDelimiter, 97 String codeset, short replace) 98 throws SQLException { 99 100 101 performImport(connection, schemaName, null, null , tableName, inputFileName, columnDelimiter, 104 characterDelimiter, codeset, replace); 105 } 106 107 108 109 110 128 public static void importData(Connection connection, String schemaName, 129 String tableName, String insertColumnList, 130 String columnIndexes, 131 String inputFileName, String columnDelimiter, 132 String characterDelimiter, 133 String codeset, short replace) 134 throws SQLException 135 { 136 137 138 performImport(connection, schemaName, insertColumnList,columnIndexes, 139 tableName, inputFileName, columnDelimiter, 140 characterDelimiter, codeset, replace); 141 } 142 143 144 152 private static void performImport(Connection connection, String schemaName, 153 String insertColumnList, String columnIndexes, 154 String tableName, 155 String inputFileName, String columnDelimiter, 156 String characterDelimiter, String codeset, 157 short replace) 158 throws SQLException 159 { 160 161 if (connection == null) 162 throw LoadError.connectionNull(); 163 164 165 166 if (tableName == null) 167 throw LoadError.entityNameMissing(); 168 169 170 ColumnInfo columnInfo = new ColumnInfo(connection , schemaName , 171 tableName, insertColumnList, 172 columnIndexes, COLUMNNAMEPREFIX); 173 174 179 if(characterDelimiter!=null && characterDelimiter.equals("'")) 180 characterDelimiter = "''"; 181 if(columnDelimiter !=null && columnDelimiter.equals("'")) 182 columnDelimiter = "''"; 183 184 185 StringBuffer sb = new StringBuffer ("new "); 186 sb.append("org.apache.derby.impl.load.Import"); 187 sb.append("(") ; 188 sb.append( (inputFileName !=null ? "'" + inputFileName + "'" : null)); 189 sb.append(",") ; 190 sb.append( (columnDelimiter !=null ? "'" + columnDelimiter + "'" : null)); 191 sb.append(",") ; 192 sb.append( (characterDelimiter !=null ? "'" + characterDelimiter + "'" : null)); 193 sb.append(",") ; 194 sb.append( (codeset !=null ? "'" + codeset + "'" : null)); 195 sb.append(", "); 196 sb.append( columnInfo.getExpectedNumberOfColumnsInFile()); 197 sb.append(" )") ; 198 199 String importvti = sb.toString(); 200 201 205 211 String entityName = (schemaName == null ? "\""+ tableName + "\"" : 212 "\"" + schemaName + "\"" + "." + "\"" + tableName + "\""); 213 214 String insertModeValue; 215 if(replace > 0) 216 insertModeValue = "replace"; 217 else 218 insertModeValue = "bulkInsert"; 219 220 String cNamesWithCasts = columnInfo.getColumnNamesWithCasts(); 221 String insertColumnNames = columnInfo.getInsertColumnNames(); 222 if(insertColumnNames !=null) 223 insertColumnNames = "(" + insertColumnNames + ") " ; 224 else 225 insertColumnNames = ""; 226 String insertSql = "INSERT INTO " + entityName + insertColumnNames + 227 " --DERBY-PROPERTIES insertMode=" + insertModeValue + "\n" + 228 " SELECT " + cNamesWithCasts + " from " + 229 importvti + " AS importvti" ; 230 231 PreparedStatement ips = connection.prepareStatement(insertSql); 233 234 Statement statement = connection.createStatement(); 238 String lockSql = "LOCK TABLE " + entityName + " IN EXCLUSIVE MODE"; 239 statement.executeUpdate(lockSql); 240 241 ips.executeUpdate(); 243 statement.close(); 244 ips.close(); 245 246 } 247 248 249 252 ImportReadData getImportReadData() throws Exception { 253 return new ImportReadData(inputFileName, controlFileReader); 254 } 255 } 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | Popular Tags |