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 48 public class JDBCOracleSequenceCreateCommand extends JDBCIdentityColumnCreateCommand 49 { 50 private String sequence_name; 51 private int pkIndex; 52 private int jdbcType; 53 54 public void init(JDBCStoreManager manager) throws DeploymentException 55 { 56 super.init(manager); 57 } 58 59 protected void initEntityCommand(JDBCEntityCommandMetaData entityCommand) throws DeploymentException 60 { 61 super.initEntityCommand(entityCommand); 62 sequence_name = entityCommand.getAttribute("sequence_name"); 63 if (sequence_name == null) { 64 throw new DeploymentException("sequence_name attribute must be specified inside <entity-command>"); 65 } 66 } 67 68 protected void initInsertSQL() 69 { 70 pkIndex = 1 + insertFields.length; 71 jdbcType = pkField.getJDBCType().getJDBCTypes()[0]; 72 73 StringBuffer sql = new StringBuffer (); 74 sql.append("{call INSERT INTO ").append(entity.getTableName()); 75 sql.append(" ("); 76 SQLUtil.getColumnNamesClause(pkField, sql) 77 .append(", "); 78 79 SQLUtil.getColumnNamesClause(insertFields, sql); 80 81 sql.append(")"); 82 sql.append(" VALUES ("); 83 String sequence_name_inst = replaceTable(sequence_name,entity.getTableName()); 84 85 sql.append(sequence_name_inst+".NEXTVAL, "); 86 SQLUtil.getValuesClause(insertFields, sql); 87 sql.append(")"); 88 sql.append(" RETURNING "); 89 SQLUtil.getColumnNamesClause(pkField, sql) 90 .append(" INTO ? }"); 91 insertSQL = sql.toString(); 92 if (debug) { 93 log.debug("Insert Entity SQL: " + insertSQL); 94 } 95 } 96 97 protected PreparedStatement prepareStatement(Connection c, String sql, EntityEnterpriseContext ctx) throws SQLException 98 { 99 return c.prepareCall(sql); 100 } 101 102 protected int executeInsert(int paramInd, PreparedStatement ps, EntityEnterpriseContext ctx) throws SQLException 103 { 104 CallableStatement cs = (CallableStatement ) ps; 105 cs.registerOutParameter(pkIndex, jdbcType); 106 cs.execute(); 107 Object pk = JDBCUtil.getParameter(log, cs, pkIndex, jdbcType, pkField.getFieldType()); 108 pkField.setInstanceValue(ctx, pk); 109 return 1; 110 } 111 112 119 private static String replaceTable(String in, String table) 120 { 121 int pos; 122 123 pos = in.indexOf("%%t"); 124 if(pos == -1) 126 return in; 127 128 String first = in.substring(0, pos); 129 String last = in.substring(pos + 3); 130 131 return first + table + last; 132 } 133 134 } 135 | Popular Tags |