1 22 package org.jboss.ejb.plugins.cmp.jdbc.keygen; 23 24 import java.sql.PreparedStatement ; 25 import java.sql.SQLException ; 26 import java.sql.ResultSet ; 27 28 import javax.ejb.EJBException ; 29 30 import org.jboss.ejb.plugins.cmp.jdbc.JDBCIdentityColumnCreateCommand; 31 import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil; 32 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityCommandMetaData; 33 import org.jboss.ejb.EntityEnterpriseContext; 34 import org.jboss.deployment.DeploymentException; 35 36 44 public class JDBCSQLServerCreateCommand extends JDBCIdentityColumnCreateCommand 45 { 46 protected void initEntityCommand(JDBCEntityCommandMetaData entityCommand) throws DeploymentException 47 { 48 super.initEntityCommand(entityCommand); 49 pkSQL = entityCommand.getAttribute("pk-sql"); 50 if(pkSQL == null) 51 { 52 pkSQL = "SELECT SCOPE_IDENTITY()"; 53 } 54 } 55 56 protected void initInsertSQL() 57 { 58 super.initInsertSQL(); 59 insertSQL = insertSQL + "; " + pkSQL; 60 } 61 62 protected int executeInsert(int index, PreparedStatement ps, EntityEnterpriseContext ctx) throws SQLException 63 { 64 ps.execute(); 65 ResultSet rs = null; 66 try 67 { 68 int rows = ps.getUpdateCount(); 69 if(rows != 1) 70 { 71 throw new EJBException ("Expected updateCount of 1, got " + rows); 72 } 73 if(ps.getMoreResults() == false) 74 { 75 throw new EJBException ("Expected ResultSet but got an updateCount. Is NOCOUNT set for all triggers?"); 76 } 77 78 rs = ps.getResultSet(); 79 if(!rs.next()) 80 { 81 throw new EJBException ("ResultSet was empty"); 82 } 83 pkField.loadInstanceResults(rs, 1, ctx); 84 return rows; 85 } 86 catch(RuntimeException e) 87 { 88 throw e; 89 } 90 catch(Exception e) 91 { 92 throw new EJBException ("Error extracting generated keys", e); 94 } 95 finally 96 { 97 JDBCUtil.safeClose(rs); 98 } 99 } 100 } 101 | Popular Tags |