| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.phoenix.dbview; |
| 34 | | | |
| 35 | | | |
| 36 | | | import java.io.File; |
| 37 | | | import java.io.FileOutputStream; |
| 38 | | | import java.io.IOException; |
| 39 | | | import java.io.OutputStreamWriter; |
| 40 | | | import java.io.PrintWriter; |
| 41 | | | import java.io.Reader; |
| 42 | | | import java.lang.reflect.InvocationTargetException; |
| 43 | | | import java.lang.reflect.Method; |
| 44 | | | import java.lang.reflect.Modifier; |
| 45 | | | import java.nio.charset.Charset; |
| 46 | | | import java.sql.Blob; |
| 47 | | | import java.sql.Connection; |
| 48 | | | import java.sql.DriverManager; |
| 49 | | | import java.sql.PreparedStatement; |
| 50 | | | import java.sql.ResultSet; |
| 51 | | | import java.sql.ResultSetMetaData; |
| 52 | | | import java.sql.SQLException; |
| 53 | | | import java.sql.Timestamp; |
| 54 | | | import java.sql.Types; |
| 55 | | | import java.text.DateFormat; |
| 56 | | | import java.text.SimpleDateFormat; |
| 57 | | | import java.util.Date; |
| 58 | | | import java.util.HashMap; |
| 59 | | | import java.util.Map; |
| 60 | | | import java.util.TimeZone; |
| 61 | | | import java.util.logging.Level; |
| 62 | | | import java.util.logging.Logger; |
| 63 | | | import javax.naming.Context; |
| 64 | | | import javax.naming.InitialContext; |
| 65 | | | import javax.naming.NamingException; |
| 66 | | | import javax.sql.DataSource; |
| 67 | | | |
| 68 | | | import org.jcoderz.commons.util.Constants; |
| 69 | | | import org.jcoderz.commons.util.DbUtil; |
| 70 | | | import org.jcoderz.commons.util.IoUtil; |
| 71 | | | import org.jcoderz.commons.util.LoggingUtils; |
| 72 | | | import org.jcoderz.commons.util.XmlUtil; |
| 73 | | | |
| 74 | | | |
| 75 | | | |
| 76 | | | |
| 77 | | | |
| 78 | | | @author |
| 79 | | | |
| 80 | 0 | | public class DbView |
| 81 | | | { |
| 82 | | | |
| 83 | | | public static final String DATASOURCE = "java:comp/env/jdbc/svs/db"; |
| 84 | | | |
| 85 | | | |
| 86 | 0 | | public static final String LINE_SEPARATOR = Constants.LINE_SEPARATOR; |
| 87 | | | |
| 88 | | | private static final int MILLIS_PER_SECOND |
| 89 | | | = org.jcoderz.commons.types.Date.MILLIS_PER_SECOND; |
| 90 | | | private static final String SELECT_ALL_TABLES = "select * from tab"; |
| 91 | 0 | | private static final String CLASSNAME = DbView.class.getName(); |
| 92 | 0 | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 93 | | | |
| 94 | 0 | | private final Map mTypeMapper = new HashMap(); |
| 95 | 0 | | private final DateFormat mDateFormater |
| 96 | | | = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss.SSS", |
| 97 | | | Constants.SYSTEM_LOCALE); |
| 98 | | | |
| 99 | | (1) | { |
| 100 | 0 | | mDateFormater.setTimeZone(TimeZone.getTimeZone("UTC")); |
| 101 | | | } |
| 102 | | | |
| 103 | 0 | | private String mDbDriver = Constants.ORACLE_DRIVER_CLASS_NAME; |
| 104 | | | private int mNumberOfColumns; |
| 105 | | | private String mSqlStatement; |
| 106 | | | private String mDbUrl; |
| 107 | | | private String mDbUser; |
| 108 | | | private String mDbPasswd; |
| 109 | | | private File mOutputDir; |
| 110 | | | private File mOutputFile; |
| 111 | | | private Level mLogLevel; |
| 112 | | | |
| 113 | | | |
| 114 | | (2) | public static void main (String[] args) |
| 115 | | | throws SecurityException |
| 116 | | | { |
| 117 | 0 | | final DbView main = new DbView(); |
| 118 | | | try |
| 119 | | | { |
| 120 | 0 | | main.parseArguments(args); |
| 121 | 0 | | if (main.mSqlStatement == null) |
| 122 | | | { |
| 123 | 0 | | main.dumpAllTables(); |
| 124 | | | } |
| 125 | | | else |
| 126 | | | { |
| 127 | 0 | | main.performQuery(); |
| 128 | | | } |
| 129 | | | } |
| 130 | 0 | | catch (IllegalArgumentException ex) |
| 131 | | | { |
| 132 | 0 | | System.err.println(ex.getMessage()); |
| 133 | 0 | | System.err.println("Usage:"); |
| 134 | 0 | | System.err.println(DbView.class.getName()); |
| 135 | 0 | | System.err.println(" -dbUrl [-dbUser scott -dbPasswd tiger] " |
| 136 | | | + "-sql \"select * from tab\" " |
| 137 | | | + "-outFile foo.xml"); |
| 138 | 0 | | System.err.println("DbView -dbUrl [-dbUser scott -dbPasswd tiger] " |
| 139 | | | + "-outDir sql/"); |
| 140 | 0 | | System.err.println(); |
| 141 | 0 | | System.err.println("To add type converters use:"); |
| 142 | 0 | | System.err.println("-type ROW_NAME JAVA_CLASS FROM_DB TO_DISPLAY"); |
| 143 | 0 | | System.err.println("ex.: -type STATUS com.encous.foo.Status fromInt " |
| 144 | | | + "toDisplayString"); |
| 145 | 0 | | System.err.println(); |
| 146 | 0 | | System.err.println("To use a db driver other than oracle add:"); |
| 147 | 0 | | System.err.println("-dbDriver your.db.driver"); |
| 148 | | | } |
| 149 | 0 | | catch (Exception ex) |
| 150 | | | { |
| 151 | 0 | | ex.printStackTrace(); |
| 152 | 0 | | } |
| 153 | 0 | | } |
| 154 | | | |
| 155 | | (3) | public void dumpAllTables () |
| 156 | | | throws IOException, ClassNotFoundException, SQLException |
| 157 | | | { |
| 158 | 0 | | dumpAllTables(mOutputDir); |
| 159 | 0 | | } |
| 160 | | | |
| 161 | | (4) | public void performQuery () |
| 162 | | | throws IOException, ClassNotFoundException, SQLException |
| 163 | | | { |
| 164 | 0 | | performConvertion (mOutputFile, mSqlStatement); |
| 165 | 0 | | } |
| 166 | | | |
| 167 | | (5) | public void dumpAllTables (File dir) |
| 168 | | | throws IOException, ClassNotFoundException, SQLException |
| 169 | | | { |
| 170 | 0 | | Connection dbConnection = null; |
| 171 | 0 | | PreparedStatement statement = null; |
| 172 | | | try |
| 173 | | | { |
| 174 | 0 | | dbConnection = getConnectionFromDbUrl(); |
| 175 | | | |
| 176 | 0 | | performConvertion(new File(dir, "tables.xml").getCanonicalPath(), |
| 177 | | | dbConnection, SELECT_ALL_TABLES); |
| 178 | | | |
| 179 | 0 | | statement = dbConnection.prepareStatement(SELECT_ALL_TABLES); |
| 180 | 0 | (6) | final ResultSet rs = statement.executeQuery(); |
| 181 | 0 | | while (rs.next()) |
| 182 | | | { |
| 183 | 0 | | final String tableName = rs.getString(1); |
| 184 | 0 | | performConvertion( |
| 185 | | | new File(dir, |
| 186 | | | escapeTableName(tableName) + ".xml").getCanonicalPath(), |
| 187 | | | dbConnection, "select * from \"" + tableName + '"'); |
| 188 | 0 | | } |
| 189 | | | } |
| 190 | | | finally |
| 191 | | | { |
| 192 | 0 | | DbUtil.close(statement); |
| 193 | 0 | | DbUtil.close(dbConnection); |
| 194 | 0 | | } |
| 195 | | | |
| 196 | 0 | | } |
| 197 | | | |
| 198 | | | private void parseArguments (String[] args) |
| 199 | | | throws IOException, NoSuchMethodException, ClassNotFoundException |
| 200 | | | { |
| 201 | | | try |
| 202 | | | { |
| 203 | 0 | | for (int i = 0; i < args.length; ) |
| 204 | | | { |
| 205 | 0 | | if ("-sql".equals(args[i])) |
| 206 | | | { |
| 207 | 0 | | mSqlStatement = args[++i]; |
| 208 | | | } |
| 209 | 0 | | else if ("-dbUrl".equals(args[i])) |
| 210 | | | { |
| 211 | 0 | | mDbUrl = args[++i]; |
| 212 | | | } |
| 213 | 0 | | else if ("-dbUser".equals(args[i])) |
| 214 | | | { |
| 215 | 0 | | mDbUser = args[++i]; |
| 216 | | | } |
| 217 | 0 | | else if ("-dbPasswd".equals(args[i])) |
| 218 | | | { |
| 219 | 0 | | mDbPasswd = args[++i]; |
| 220 | | | } |
| 221 | 0 | (7) | else if (args[i].equals("-type")) |
| 222 | | | { |
| 223 | 0 | | final String rowname = args[++i]; |
| 224 | 0 | | final String classname = args[++i]; |
| 225 | 0 | | final String fromDbType = args[++i]; |
| 226 | 0 | | final String toDisplay = args[++i]; |
| 227 | 0 | | addTypeMapping(rowname, classname, fromDbType, toDisplay); |
| 228 | 0 | | } |
| 229 | 0 | | else if ("-outDir".equals(args[i])) |
| 230 | | | { |
| 231 | 0 | | mOutputDir = new File(args[++i]); |
| 232 | | | } |
| 233 | 0 | | else if ("-outFile".equals(args[i])) |
| 234 | | | { |
| 235 | 0 | | mOutputFile = new File(args[++i]); |
| 236 | | | } |
| 237 | 0 | | else if ("-dbDriver".equals(args[i])) |
| 238 | | | { |
| 239 | 0 | | mDbDriver = args[++i]; |
| 240 | | | } |
| 241 | 0 | | else if ("-loglevel".equals(args[i])) |
| 242 | | | { |
| 243 | 0 | | mLogLevel = Level.parse(args[++i]); |
| 244 | 0 | | Logger.getLogger("").setLevel(mLogLevel); |
| 245 | 0 | | LoggingUtils.setGlobalHandlerLogLevel(mLogLevel); |
| 246 | | | } |
| 247 | | | else |
| 248 | | | { |
| 249 | 0 | | throw new IllegalArgumentException( |
| 250 | | | "Invalid argument '" + args[i] + "'"); |
| 251 | | | } |
| 252 | 0 | | ++i; |
| 253 | | | } |
| 254 | | | |
| 255 | 0 | | checkParameters(); |
| 256 | | | } |
| 257 | 0 | | catch (IndexOutOfBoundsException e) |
| 258 | | | { |
| 259 | 0 | | final IllegalArgumentException ex = new IllegalArgumentException( |
| 260 | | | "Missing value for " + args[args.length - 1]); |
| 261 | 0 | | ex.initCause(e); |
| 262 | 0 | | throw ex; |
| 263 | 0 | | } |
| 264 | 0 | | } |
| 265 | | | |
| 266 | | | private void checkParameters () |
| 267 | | | throws IllegalArgumentException |
| 268 | | | { |
| 269 | 0 | | if (mOutputDir == null && mOutputFile == null) |
| 270 | | | { |
| 271 | 0 | | throw new IllegalArgumentException( |
| 272 | | | "Need either output dir or output file."); |
| 273 | | | } |
| 274 | 0 | | if (mSqlStatement != null && mOutputFile != null) |
| 275 | | | { |
| 276 | 0 | | throw new IllegalArgumentException( |
| 277 | | | "Need output file for sql statement."); |
| 278 | | | } |
| 279 | 0 | | if (mSqlStatement == null && mOutputDir == null) |
| 280 | | | { |
| 281 | 0 | | throw new IllegalArgumentException( |
| 282 | | | "Need output dir for global dump."); |
| 283 | | | } |
| 284 | 0 | | if (!mOutputDir.isDirectory()) |
| 285 | | | { |
| 286 | 0 | | throw new RuntimeException("out dir must be a directory."); |
| 287 | | | } |
| 288 | 0 | | if (mOutputFile.isDirectory()) |
| 289 | | | { |
| 290 | 0 | | throw new RuntimeException( |
| 291 | | | "out file must not be a directory."); |
| 292 | | | } |
| 293 | 0 | | } |
| 294 | | | |
| 295 | | (8) | public void performConvertion (File file, String query) |
| 296 | | | throws IOException, ClassNotFoundException, SQLException |
| 297 | | | { |
| 298 | 0 | | PrintWriter out = null; |
| 299 | 0 | | Connection dbConnection = null; |
| 300 | 0 | | PreparedStatement statement = null; |
| 301 | | | try |
| 302 | | | { |
| 303 | 0 | | final FileOutputStream stream = new FileOutputStream(file); |
| 304 | 0 | | final OutputStreamWriter writer = new OutputStreamWriter(stream, |
| 305 | | | Charset.forName("us-ascii")); |
| 306 | 0 | | out = new PrintWriter(writer); |
| 307 | 0 | | dbConnection = getConnectionFromDbUrl(); |
| 308 | 0 | (9) | statement = dbConnection.prepareStatement(query); |
| 309 | 0 | (10) | final ResultSet rs = statement.executeQuery(); |
| 310 | 0 | | xmlOpen(null, dbConnection.getMetaData().getURL(), query, out); |
| 311 | 0 | | metaData2Xml(rs.getMetaData(), out); |
| 312 | 0 | | resultSet2Xml(rs, out); |
| 313 | 0 | | xmlClose(out); |
| 314 | | | |
| 315 | | | } |
| 316 | | | finally |
| 317 | | | { |
| 318 | 0 | | IoUtil.close(out); |
| 319 | 0 | | DbUtil.close(statement); |
| 320 | 0 | | DbUtil.close(dbConnection); |
| 321 | 0 | | } |
| 322 | 0 | | } |
| 323 | | | |
| 324 | | (11) | public void addTypeMapping (String typeName, String typeClass, |
| 325 | | | String fromDb, String toString) |
| 326 | | | throws SecurityException, NoSuchMethodException, ClassNotFoundException |
| 327 | | | { |
| 328 | 0 | | mTypeMapper.put(typeName, |
| 329 | | | new TypeMapper(typeName, typeClass, fromDb, toString)); |
| 330 | 0 | | } |
| 331 | | | |
| 332 | | | private void performConvertion (String fileName, |
| 333 | | | final Connection dbConnection, String query) |
| 334 | | | throws IOException, SQLException |
| 335 | | | { |
| 336 | 0 | (12) | logger.fine("about to dump '" + query + "' into '" + fileName +"'."); |
| 337 | 0 | | PrintWriter out = null; |
| 338 | 0 | | PreparedStatement statement = null; |
| 339 | | | try |
| 340 | | | { |
| 341 | 0 | | final FileOutputStream stream = new FileOutputStream(fileName); |
| 342 | 0 | | final OutputStreamWriter writer = new OutputStreamWriter(stream, |
| 343 | | | Charset.forName("us-ascii")); |
| 344 | 0 | | out = new PrintWriter(writer); |
| 345 | 0 | (13) | statement = dbConnection.prepareStatement(query); |
| 346 | 0 | (14) | final ResultSet rs = statement.executeQuery(); |
| 347 | 0 | | xmlOpen(null, dbConnection.getMetaData().getURL(), query, out); |
| 348 | 0 | | metaData2Xml(rs.getMetaData(), out); |
| 349 | 0 | | resultSet2Xml(rs, out); |
| 350 | 0 | | xmlClose(out); |
| 351 | | | |
| 352 | | | } |
| 353 | | | finally |
| 354 | | | { |
| 355 | 0 | | IoUtil.close(out); |
| 356 | 0 | | DbUtil.close(statement); |
| 357 | 0 | | } |
| 358 | 0 | | } |
| 359 | | | |
| 360 | | (15) | private Connection getConnectionFromDataSource () |
| 361 | | | throws NamingException, SQLException |
| 362 | | | { |
| 363 | 0 | | final Context ctx = new InitialContext(); |
| 364 | 0 | | final DataSource ds = (DataSource) ctx.lookup(DATASOURCE); |
| 365 | 0 | | return ds.getConnection(); |
| 366 | | | } |
| 367 | | | |
| 368 | | | private Connection getConnectionFromDbUrl () |
| 369 | | | throws ClassNotFoundException, SQLException |
| 370 | | | { |
| 371 | 0 | | Class.forName(mDbDriver); |
| 372 | | | final Connection con; |
| 373 | | | |
| 374 | 0 | | if (mDbUser == null) |
| 375 | | | { |
| 376 | 0 | | con = DriverManager.getConnection(mDbUrl); |
| 377 | | | } |
| 378 | | | else |
| 379 | | | { |
| 380 | 0 | | con = DriverManager.getConnection(mDbUrl, mDbUser, mDbPasswd); |
| 381 | | | } |
| 382 | | | |
| 383 | 0 | | return con; |
| 384 | | | } |
| 385 | | | |
| 386 | | | private void xmlOpen (String dataSource, String dataBaseUri, |
| 387 | | | String statement, PrintWriter out) |
| 388 | | | throws IOException, SQLException |
| 389 | | | { |
| 390 | 0 | | out.print("<result statement='"); |
| 391 | 0 | | out.print(XmlUtil.attributeEscape(statement)); |
| 392 | 0 | | out.println("'"); |
| 393 | 0 | | if (dataSource != null) |
| 394 | | | { |
| 395 | 0 | | out.print(" data-source='"); |
| 396 | 0 | | out.print(XmlUtil.attributeEscape(dataSource)); |
| 397 | 0 | | out.println("'"); |
| 398 | | | } |
| 399 | 0 | | if (dataBaseUri != null) |
| 400 | | | { |
| 401 | 0 | | out.print(" db-uri='"); |
| 402 | 0 | | out.print(XmlUtil.attributeEscape(dataBaseUri)); |
| 403 | 0 | | out.println("'"); |
| 404 | | | } |
| 405 | 0 | | out.print(" creation-date='"); |
| 406 | 0 | | out.print(display(new Date())); |
| 407 | 0 | | out.println("'>"); |
| 408 | 0 | | } |
| 409 | | | |
| 410 | | | private void xmlClose (PrintWriter out) |
| 411 | | | { |
| 412 | 0 | | out.println("</result>"); |
| 413 | 0 | | } |
| 414 | | | |
| 415 | | | private void metaData2Xml (ResultSetMetaData md, PrintWriter out) |
| 416 | | | throws IOException, SQLException |
| 417 | | | { |
| 418 | 0 | | mNumberOfColumns = md.getColumnCount(); |
| 419 | 0 | | out.print(" <meta-data number-of-columns='"); |
| 420 | 0 | | out.print(mNumberOfColumns); |
| 421 | 0 | | out.println("'>"); |
| 422 | 0 | | for (int column = 1; column <= mNumberOfColumns; column++) |
| 423 | | | { |
| 424 | 0 | | out.println(" <column"); |
| 425 | 0 | | metaDataAsString("name", md.getColumnName(column), out); |
| 426 | 0 | | metaDataAsString("type-name", md.getColumnTypeName(column), out); |
| 427 | 0 | | metaDataAsString("display-name", md.getColumnLabel(column), out); |
| 428 | 0 | | metaDataAsString("class-name", md.getColumnClassName(column), out); |
| 429 | | | try |
| 430 | | | { |
| 431 | 0 | | metaDataAsString("precision", md.getPrecision(column), out); |
| 432 | | | } |
| 433 | 0 | | catch (NumberFormatException ex) |
| 434 | | | { |
| 435 | | | |
| 436 | 0 | | } |
| 437 | 0 | | metaDataAsString("scale", md.getScale(column), out); |
| 438 | 0 | | metaDataAsString("catalog-name", md.getCatalogName(column), out); |
| 439 | 0 | | metaDataAsString("schema-name", md.getSchemaName(column), out); |
| 440 | 0 | | metaDataAsString("table-name", md.getTableName(column), out); |
| 441 | 0 | | metaDataAsStringNullable("is-nullable", md.isNullable(column), out); |
| 442 | 0 | | metaDataAsString("type", md.getColumnType(column), out); |
| 443 | 0 | | metaDataAsString("display-size", md.getColumnDisplaySize(column), out); |
| 444 | 0 | | out.println(" />"); |
| 445 | | | } |
| 446 | 0 | | out.println(" </meta-data>"); |
| 447 | 0 | | } |
| 448 | | | |
| 449 | | | |
| 450 | | | private void metaDataAsStringNullable ( |
| 451 | | | String attributeName, int nullable, PrintWriter out) |
| 452 | | | { |
| 453 | 0 | | switch (nullable) |
| 454 | | | { |
| 455 | | | case ResultSetMetaData.columnNoNulls: |
| 456 | 0 | | metaDataAsString(attributeName, "no-nulls", out); |
| 457 | 0 | | break; |
| 458 | | | case ResultSetMetaData.columnNullable: |
| 459 | 0 | | metaDataAsString(attributeName, "nullable", out); |
| 460 | 0 | | break; |
| 461 | | | case ResultSetMetaData.columnNullableUnknown: |
| 462 | 0 | | metaDataAsString(attributeName, "unknown", out); |
| 463 | 0 | | break; |
| 464 | | | default: |
| 465 | 0 | | metaDataAsString(attributeName, "illeagal", out); |
| 466 | | | break; |
| 467 | | | } |
| 468 | 0 | | } |
| 469 | | | |
| 470 | | | |
| 471 | | | @param |
| 472 | | | @param |
| 473 | | | @param |
| 474 | | | |
| 475 | | | private void metaDataAsString ( |
| 476 | | | String attributeName, int i, PrintWriter out) |
| 477 | | | { |
| 478 | 0 | | metaDataAsString(attributeName, String.valueOf(i), out); |
| 479 | 0 | | } |
| 480 | | | |
| 481 | | | |
| 482 | | | |
| 483 | | | @param |
| 484 | | | @param |
| 485 | | | @param |
| 486 | | | |
| 487 | | | private void metaDataAsString (String attributeName, |
| 488 | | | String attributeValue, PrintWriter out) |
| 489 | | | { |
| 490 | 0 | | out.print(" "); |
| 491 | 0 | | out.print(attributeName); |
| 492 | 0 | | out.print("='"); |
| 493 | 0 | | out.print(XmlUtil.attributeEscape(attributeValue)); |
| 494 | 0 | | out.println("'"); |
| 495 | 0 | | } |
| 496 | | | |
| 497 | | | |
| 498 | | | |
| 499 | | | private void resultSet2Xml (ResultSet rs, PrintWriter out) |
| 500 | | | throws IOException, SQLException |
| 501 | | | { |
| 502 | 0 | | out.println(" <result-set>"); |
| 503 | 0 | | while (rs.next()) |
| 504 | | | { |
| 505 | 0 | | out.print(" <row row-number='"); |
| 506 | 0 | | out.print(rs.getRow()); |
| 507 | 0 | | out.println("'>"); |
| 508 | 0 | | result2Xml(rs, out); |
| 509 | 0 | | out.println(" </row>"); |
| 510 | | | } |
| 511 | 0 | | out.println(" </result-set>"); |
| 512 | 0 | | } |
| 513 | | | |
| 514 | | | private void result2Xml (ResultSet rs, PrintWriter out) |
| 515 | | | throws SQLException, IOException |
| 516 | | | { |
| 517 | 0 | | for (int column = 1; column <= mNumberOfColumns; column++) |
| 518 | | | { |
| 519 | 0 | | final Object o = typedGetter(rs, column); |
| 520 | 0 | | out.print(" <column"); |
| 521 | 0 | | if (o == null) |
| 522 | | | { |
| 523 | 0 | | out.println(" isNull='true'>"); |
| 524 | | | } |
| 525 | | | else |
| 526 | | | { |
| 527 | 0 | | out.println(">"); |
| 528 | | | } |
| 529 | 0 | | final String name = rs.getMetaData().getColumnName(column); |
| 530 | 0 | | final String typeName = rs.getMetaData().getColumnTypeName(column); |
| 531 | 0 | | object2Xml(o, name, typeName, out); |
| 532 | 0 | | out.println(" </column>"); |
| 533 | | | } |
| 534 | | | |
| 535 | 0 | | } |
| 536 | | | |
| 537 | | | private Object typedGetter (ResultSet rs, int column) |
| 538 | | | throws SQLException, IOException |
| 539 | | | { |
| 540 | | | final Object result; |
| 541 | 0 | | final int type = rs.getMetaData().getColumnType(column); |
| 542 | 0 | | switch (type) |
| 543 | | | { |
| 544 | | | case Types.TIMESTAMP: |
| 545 | 0 | | result = convertTimestamp(rs.getTimestamp(column)); |
| 546 | 0 | | break; |
| 547 | | | case Types.DATE: |
| 548 | | | case Types.TIME: |
| 549 | | (16) | |
| 550 | 0 | | result = rs.getDate(column); |
| 551 | 0 | | break; |
| 552 | | | case Types.CLOB: |
| 553 | 0 | | result = readNclob(rs, column); |
| 554 | 0 | | break; |
| 555 | | | case Types.BLOB: |
| 556 | 0 | | result = readBlob(rs, column); |
| 557 | 0 | | break; |
| 558 | | | default: |
| 559 | 0 | | result = rs.getObject(column); |
| 560 | | | } |
| 561 | 0 | | return result; |
| 562 | | | } |
| 563 | | | |
| 564 | | | private Object readBlob (ResultSet rs, int column) |
| 565 | | | throws SQLException |
| 566 | | | { |
| 567 | 0 | | final Blob blob = rs.getBlob(column); |
| 568 | | | final byte [] data; |
| 569 | 0 | | if (blob != null) |
| 570 | | | { |
| 571 | 0 | | final long length = blob.length(); |
| 572 | 0 | | if (length > Integer.MAX_VALUE) |
| 573 | | | { |
| 574 | 0 | | data = "Length of Blob exceeds maximum of MAX_INT".getBytes(); |
| 575 | | | } |
| 576 | | | else |
| 577 | | | { |
| 578 | 0 | | data = blob.getBytes(1, (int) length); |
| 579 | | | } |
| 580 | 0 | | } |
| 581 | | | else |
| 582 | | | { |
| 583 | 0 | | data = null; |
| 584 | | | } |
| 585 | 0 | | return data; |
| 586 | | | } |
| 587 | | | |
| 588 | | | private String readNclob (ResultSet rs, int column) |
| 589 | | | throws SQLException, IOException |
| 590 | | | { |
| 591 | | | final String result; |
| 592 | 0 | | final Reader reader = rs.getCharacterStream(column); |
| 593 | 0 | | if (reader != null) |
| 594 | | | { |
| 595 | | | try |
| 596 | | | { |
| 597 | 0 | | result = IoUtil.readFully(reader); |
| 598 | | | } |
| 599 | | | finally |
| 600 | | | { |
| 601 | 0 | | IoUtil.close(reader); |
| 602 | 0 | | } |
| 603 | | | } |
| 604 | | | else |
| 605 | | | { |
| 606 | 0 | | result = null; |
| 607 | | | } |
| 608 | 0 | | return result; |
| 609 | | | } |
| 610 | | | |
| 611 | | | |
| 612 | | | private void object2Xml (Object object, String name, String typeName, |
| 613 | | | PrintWriter out) |
| 614 | | | throws SQLException |
| 615 | | | { |
| 616 | 0 | | if (object != null) |
| 617 | | | { |
| 618 | 0 | | out.print(" <raw>"); |
| 619 | 0 | | out.print(XmlUtil.escape(String.valueOf(object))); |
| 620 | 0 | | out.println("</raw>"); |
| 621 | 0 | | final String display = objectFormater(object, name, typeName); |
| 622 | 0 | | if (display != null) |
| 623 | | | { |
| 624 | 0 | | out.print(" <display>"); |
| 625 | 0 | | out.print(XmlUtil.escape(display)); |
| 626 | 0 | | out.println("</display>"); |
| 627 | | | } |
| 628 | 0 | | } |
| 629 | | | else |
| 630 | | | { |
| 631 | 0 | | out.println(" <raw/>"); |
| 632 | | | } |
| 633 | 0 | | } |
| 634 | | | |
| 635 | | | private String objectFormater (Object object, String name, String typeName) |
| 636 | | | throws SQLException |
| 637 | | | { |
| 638 | 0 | | String result = null; |
| 639 | | | |
| 640 | 0 | | if (mTypeMapper.containsKey(name)) |
| 641 | | | { |
| 642 | | | try |
| 643 | | | { |
| 644 | 0 | | final TypeMapper mapper = (TypeMapper) mTypeMapper.get(name); |
| 645 | 0 | | result = mapper.toDisplay(object); |
| 646 | | | } |
| 647 | 0 | | catch (IllegalArgumentException e) |
| 648 | | | { |
| 649 | 0 | (17) | result = "Failed to convert type '" + e.toString() + "'."; |
| 650 | 0 | | logger.log(Level.WARNING, result, e); |
| 651 | | | } |
| 652 | 0 | | catch (IllegalAccessException e) |
| 653 | | | { |
| 654 | 0 | | result = "Failed to convert type '" + e.toString() + "'."; |
| 655 | 0 | | logger.log(Level.WARNING, result, e); |
| 656 | | | |
| 657 | | | } |
| 658 | 0 | | catch (InvocationTargetException e) |
| 659 | | | { |
| 660 | 0 | | result = "Failed to convert type '" + e.toString() + "'."; |
| 661 | 0 | | logger.log(Level.WARNING, result, e); |
| 662 | | | } |
| 663 | 0 | | catch (RuntimeException e) |
| 664 | | | { |
| 665 | 0 | | result = "Failed to convert type '" + e.toString() + "'."; |
| 666 | 0 | | logger.log(Level.WARNING, result, e); |
| 667 | 0 | | } |
| 668 | | | } |
| 669 | 0 | (18) | else if (object instanceof Date) |
| 670 | | | { |
| 671 | 0 | | result = display((Date) object); |
| 672 | | | } |
| 673 | 0 | | else if (object instanceof String) |
| 674 | | | { |
| 675 | 0 | | result = display((String) object); |
| 676 | | | } |
| 677 | | | else |
| 678 | | | { |
| 679 | 0 | | result = null; |
| 680 | | | } |
| 681 | 0 | | return result; |
| 682 | | | } |
| 683 | | | |
| 684 | | | |
| 685 | | | private String display (Date d) |
| 686 | | | { |
| 687 | | | final String result; |
| 688 | 0 | | if (d == null) |
| 689 | | | { |
| 690 | 0 | | result = null; |
| 691 | | | } |
| 692 | | | else |
| 693 | | | { |
| 694 | 0 | | result = mDateFormater.format(d); |
| 695 | | | } |
| 696 | 0 | | return result; |
| 697 | | | } |
| 698 | | | |
| 699 | | | private String display (String str) |
| 700 | | | { |
| 701 | | | final String result; |
| 702 | 0 | | if (str == null) |
| 703 | | | { |
| 704 | 0 | | result = null; |
| 705 | | | } |
| 706 | 0 | | else if (str.indexOf('<') != -1) |
| 707 | | | { |
| 708 | 0 | | final String detect = str.trim(); |
| 709 | 0 | | if (detect.indexOf('<') == 0 |
| 710 | | | && detect.lastIndexOf('>') == (detect.length() - 1)) |
| 711 | | | { |
| 712 | 0 | | result = XmlUtil.formatXml(str); |
| 713 | | | } |
| 714 | | | else |
| 715 | | | { |
| 716 | 0 | | result = null; |
| 717 | | | } |
| 718 | 0 | | } |
| 719 | | | else |
| 720 | | | { |
| 721 | 0 | | result = null; |
| 722 | | | } |
| 723 | | | |
| 724 | 0 | | return result; |
| 725 | | | } |
| 726 | | | |
| 727 | | | private static Date convertTimestamp (Timestamp ts) |
| 728 | | | { |
| 729 | | | final Date d; |
| 730 | 0 | | if (ts != null) |
| 731 | | | { |
| 732 | | | |
| 733 | 0 | | d = new Date(((ts.getTime() / MILLIS_PER_SECOND) * MILLIS_PER_SECOND) |
| 734 | | | + (ts.getNanos() |
| 735 | | | / org.jcoderz.commons.types.Date.NANOS_PER_MILLI)); |
| 736 | | | } |
| 737 | | | else |
| 738 | | | { |
| 739 | 0 | | d = null; |
| 740 | | | } |
| 741 | 0 | | return d; |
| 742 | | | } |
| 743 | | | |
| 744 | 0 | | private static class TypeMapper |
| 745 | | | { |
| 746 | | | private final Class mTypeClass; |
| 747 | | | private final Method mFromDb; |
| 748 | | | private final Method mToString; |
| 749 | | | private final String mTypeName; |
| 750 | | | private final Class mInputType; |
| 751 | | | |
| 752 | | | public TypeMapper (String typeName, String typeClass, String fromDb, |
| 753 | | | String toString) |
| 754 | | | throws SecurityException, NoSuchMethodException, |
| 755 | | | ClassNotFoundException |
| 756 | | | { |
| 757 | 0 | | this(typeName, Class.forName(typeClass), fromDb, toString); |
| 758 | 0 | | } |
| 759 | | | |
| 760 | | | public TypeMapper (String typeName, Class typeClass, String fromDb, |
| 761 | | | String toString) |
| 762 | | | throws SecurityException, NoSuchMethodException |
| 763 | 0 | | { |
| 764 | 0 | (19) | mTypeClass = typeClass; |
| 765 | 0 | | mToString = typeClass.getMethod(toString, null); |
| 766 | | | |
| 767 | 0 | | final Method [] methods = typeClass.getMethods(); |
| 768 | 0 | | Method method = null; |
| 769 | 0 | | for (int i = 0; i < methods.length; i++) |
| 770 | | | { |
| 771 | 0 | | if (methods[i].getReturnType() == mTypeClass |
| 772 | | | && methods[i].getName().startsWith(fromDb) |
| 773 | | | && ((methods[i].getModifiers() & Modifier.STATIC) != 0)) |
| 774 | | | { |
| 775 | 0 | | method = methods[i]; |
| 776 | 0 | | break; |
| 777 | | | } |
| 778 | | | } |
| 779 | | | |
| 780 | 0 | | mFromDb = method; |
| 781 | 0 | (20) | mInputType = method.getParameterTypes()[0]; |
| 782 | 0 | (21)(22) | mTypeName = typeName; |
| 783 | 0 | | } |
| 784 | | | |
| 785 | | | public String toDisplay (Object in) |
| 786 | | | throws IllegalArgumentException, IllegalAccessException, |
| 787 | | | InvocationTargetException |
| 788 | | | { |
| 789 | 0 | | Object type = null; |
| 790 | | | try |
| 791 | | | { |
| 792 | 0 | | type = mFromDb.invoke(null, new Object[] {in}); |
| 793 | | | } |
| 794 | 0 | | catch (Exception ex) |
| 795 | | | { |
| 796 | 0 | | if (in instanceof Number) |
| 797 | | | { |
| 798 | 0 | | if (mInputType == Integer.TYPE) |
| 799 | | | { |
| 800 | 0 | | type = mFromDb.invoke(null, |
| 801 | | | new Object[] |
| 802 | | | { |
| 803 | | | new Integer(((Number) in).intValue()) |
| 804 | | | }); |
| 805 | | | } |
| 806 | 0 | | else if (mInputType == Long.TYPE) |
| 807 | | | { |
| 808 | 0 | | type = mFromDb.invoke(null, |
| 809 | | | new Object[] |
| 810 | | | { |
| 811 | | | new Long(((Number) in).longValue()) |
| 812 | | | }); |
| 813 | | | } |
| 814 | | | } |
| 815 | | | else |
| 816 | | | { |
| 817 | 0 | | final IllegalArgumentException axe |
| 818 | | | = new IllegalArgumentException( |
| 819 | | | "Could not map type for object '" + String.valueOf(in) |
| 820 | | | + "' of class " + in.getClass().getName() |
| 821 | | | + " into expected converter " |
| 822 | | | + "type " + mInputType.getName() + "."); |
| 823 | 0 | | axe.initCause(ex); |
| 824 | 0 | | throw axe; |
| 825 | | | } |
| 826 | 0 | | } |
| 827 | 0 | | return (String) mToString.invoke(type, null); |
| 828 | | | } |
| 829 | | | } |
| 830 | | | |
| 831 | | (23) | public static String escapeTableName (String in) |
| 832 | | | { |
| 833 | 0 | | return in.replaceAll("[/\\\\$]", "#"); |
| 834 | | | } |
| 835 | | | } |