1 22 package org.jboss.ejb.plugins.cmp.jdbc.keygen; 23 24 import java.sql.PreparedStatement ; 25 import java.sql.Connection ; 26 import java.sql.SQLException ; 27 import java.sql.CallableStatement ; 28 29 import org.jboss.ejb.plugins.cmp.jdbc.JDBCIdentityColumnCreateCommand; 30 import org.jboss.ejb.plugins.cmp.jdbc.SQLUtil; 31 import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil; 32 import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager; 33 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityCommandMetaData; 34 import org.jboss.ejb.EntityEnterpriseContext; 35 import org.jboss.deployment.DeploymentException; 36 37 44 public class JDBCOracleCreateCommand extends JDBCIdentityColumnCreateCommand 45 { 46 private String sequence; 47 private int jdbcType; 48 49 public void init(JDBCStoreManager manager) throws DeploymentException 50 { 51 super.init(manager); 52 } 53 54 protected void initEntityCommand(JDBCEntityCommandMetaData entityCommand) throws DeploymentException 55 { 56 super.initEntityCommand(entityCommand); 57 sequence = entityCommand.getAttribute("sequence"); 58 if (sequence == null) { 59 throw new DeploymentException("Sequence must be specified"); 60 } 61 } 62 63 protected void initInsertSQL() 64 { 65 jdbcType = pkField.getJDBCType().getJDBCTypes()[0]; 66 67 StringBuffer sql = new StringBuffer (); 68 sql.append("{call INSERT INTO ").append(entity.getQualifiedTableName()); 69 sql.append(" ("); 70 SQLUtil.getColumnNamesClause(pkField, sql) 71 .append(", "); 72 73 SQLUtil.getColumnNamesClause(insertFields, sql); 74 75 sql.append(")"); 76 sql.append(" VALUES ("); 77 sql.append(sequence+".NEXTVAL, "); 78 SQLUtil.getValuesClause(insertFields, sql); 79 sql.append(")"); 80 sql.append(" RETURNING "); 81 SQLUtil.getColumnNamesClause(pkField, sql) 82 .append(" INTO ? }"); 83 insertSQL = sql.toString(); 84 if (debug) { 85 log.debug("Insert Entity SQL: " + insertSQL); 86 } 87 } 88 89 protected PreparedStatement prepareStatement(Connection c, String sql, EntityEnterpriseContext ctx) throws SQLException 90 { 91 return c.prepareCall(sql); 92 } 93 94 protected int executeInsert(int paramIndex, PreparedStatement ps, EntityEnterpriseContext ctx) throws SQLException 95 { 96 CallableStatement cs = (CallableStatement ) ps; 97 cs.registerOutParameter(paramIndex, jdbcType); 98 cs.execute(); 99 Object pk = JDBCUtil.getParameter(log, cs, paramIndex, jdbcType, pkField.getFieldType()); 100 pkField.setInstanceValue(ctx, pk); 101 return 1; 102 } 103 } 104 | Popular Tags |