1 19 20 package org.apache.cayenne.dba.ingres; 21 22 import java.sql.Connection ; 23 import java.sql.ResultSet ; 24 import java.sql.SQLException ; 25 import java.sql.Statement ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.List ; 29 30 import org.apache.cayenne.CayenneRuntimeException; 31 import org.apache.cayenne.access.DataNode; 32 import org.apache.cayenne.access.QueryLogger; 33 import org.apache.cayenne.dba.oracle.OraclePkGenerator; 34 import org.apache.cayenne.map.DbEntity; 35 import org.apache.cayenne.map.DbKeyGenerator; 36 37 43 public class IngresPkGenerator extends OraclePkGenerator { 44 52 protected int pkFromDatabase(DataNode node, DbEntity ent) throws Exception { 53 54 DbKeyGenerator pkGenerator = ent.getPrimaryKeyGenerator(); 55 String pkGeneratingSequenceName; 56 if (pkGenerator != null 57 && DbKeyGenerator.ORACLE_TYPE.equals(pkGenerator.getGeneratorType()) 58 && pkGenerator.getGeneratorName() != null) 59 pkGeneratingSequenceName = pkGenerator.getGeneratorName(); 60 else 61 pkGeneratingSequenceName = sequenceName(ent); 62 63 Connection con = node.getDataSource().getConnection(); 64 try { 65 Statement st = con.createStatement(); 66 try { 67 String sql = "SELECT " + pkGeneratingSequenceName + ".nextval"; 68 QueryLogger.logQuery(sql, Collections.EMPTY_LIST); 69 ResultSet rs = st.executeQuery(sql); 70 try { 71 if (!rs.next()) { 73 throw new CayenneRuntimeException( 74 "Error generating pk for DbEntity " + ent.getName()); 75 } 76 return rs.getInt(1); 77 } 78 finally { 79 rs.close(); 80 } 81 } 82 finally { 83 st.close(); 84 } 85 } 86 finally { 87 con.close(); 88 } 89 } 90 91 protected List getExistingSequences(DataNode node) throws SQLException { 92 93 Connection connection = node.getDataSource().getConnection(); 95 96 try { 97 Statement select = connection.createStatement(); 98 try { 99 String sql = "select seq_name from iisequences where seq_owner != 'DBA'"; 100 QueryLogger.logQuery(sql, Collections.EMPTY_LIST); 101 ResultSet rs = select.executeQuery(sql); 102 try { 103 List sequenceList = new ArrayList (); 104 while (rs.next()) { 105 String name = rs.getString(1); 106 if(name != null) { 107 sequenceList.add(name.trim()); 108 } 109 } 110 return sequenceList; 111 } 112 finally { 113 rs.close(); 114 } 115 } 116 finally { 117 select.close(); 118 } 119 } 120 finally { 121 connection.close(); 122 } 123 } 124 } 125 | Popular Tags |