KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > keygen > JDBCOracleCreateCommand


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc.keygen;
23
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.CallableStatement JavaDoc;
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 /**
38  * Create command for use with Oracle that uses a sequence in conjuction with
39  * a RETURNING clause to generate keys in a single statement
40  *
41  * @author <a HREF="mailto:jeremy@boynes.com">Jeremy Boynes</a>
42  * @version $Revision: 37459 $
43  */

44 public class JDBCOracleCreateCommand extends JDBCIdentityColumnCreateCommand
45 {
46    private String JavaDoc 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 JavaDoc sql = new StringBuffer JavaDoc();
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 JavaDoc prepareStatement(Connection JavaDoc c, String JavaDoc sql, EntityEnterpriseContext ctx) throws SQLException JavaDoc
90    {
91       return c.prepareCall(sql);
92    }
93
94    protected int executeInsert(int paramIndex, PreparedStatement JavaDoc ps, EntityEnterpriseContext ctx) throws SQLException JavaDoc
95    {
96       CallableStatement JavaDoc cs = (CallableStatement JavaDoc) ps;
97       cs.registerOutParameter(paramIndex, jdbcType);
98       cs.execute();
99       Object JavaDoc pk = JDBCUtil.getParameter(log, cs, paramIndex, jdbcType, pkField.getFieldType());
100       pkField.setInstanceValue(ctx, pk);
101       return 1;
102    }
103 }
104
Popular Tags