1 22 23 package org.xquark.mapping; 24 import java.sql.*; 25 26 38 public class KeyGenerator implements UserGenerator { 39 40 private long counter = -1; 41 private String table = null; 42 private String column = null; 43 boolean useQuotes = false; 44 45 public KeyGenerator(CompilationContext properties) { 46 table = properties.getProperty(CompilationContext.TABLE_NAME); 47 column = properties.getProperty(CompilationContext.COLUMN_NAME); 48 useQuotes = Boolean.valueOf( 49 properties.getProperty(CompilationContext.USE_QUOTES_4_DDL) 50 ).booleanValue(); 51 } 52 53 public String getXMLType() { 54 return "long"; 55 } 56 57 public synchronized Object getValue(StorageContext context) { 58 if (counter < 0) 59 initCounter(context.getConnection()); 60 return new Long (++counter); 61 } 62 63 private void initCounter(Connection connection) { 64 ResultSet rs = null; 65 Statement statement = null; 66 try { 67 StringBuffer buf = new StringBuffer ("select max("); 68 if (useQuotes) 69 buf.append('"'); 70 buf.append(column); 71 if (useQuotes) 72 buf.append('"'); 73 buf.append(") from "); 74 if (useQuotes) 75 buf.append('"'); 76 buf.append(table); 77 if (useQuotes) 78 buf.append('"'); 79 80 statement = connection.createStatement(); 81 rs = statement.executeQuery(buf.toString()); 82 while (rs.next()) { 83 counter = rs.getLong(1); 84 } 85 } 86 catch (Exception ex) { 87 ex.printStackTrace(); 88 } 89 finally { 90 try { 91 if (rs != null) 92 rs.close(); 93 if (statement != null) 94 statement.close(); 95 } 96 catch (Exception ex) { 97 } 99 } 100 } 101 } 102 | Popular Tags |