KickJava   Java API By Example, From Geeks To Geeks.

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


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  * The sequence is called by the parameter attribute "sequence_name".
42  * As an example, the sequence_name could be %%t_sequence to use <table_name>_sequence
43  * for each distinct table.
44  *
45  * @author Guillaume Compagnon
46  * @version $Revision: 44181 $
47  */

48 public class JDBCOracleSequenceCreateCommand extends JDBCIdentityColumnCreateCommand
49 {
50    private String JavaDoc 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 JavaDoc sql = new StringBuffer JavaDoc();
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 JavaDoc 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 JavaDoc prepareStatement(Connection JavaDoc c, String JavaDoc sql, EntityEnterpriseContext ctx) throws SQLException JavaDoc
98    {
99       return c.prepareCall(sql);
100    }
101
102    protected int executeInsert(int paramInd, PreparedStatement JavaDoc ps, EntityEnterpriseContext ctx) throws SQLException JavaDoc
103    {
104       CallableStatement JavaDoc cs = (CallableStatement JavaDoc) ps;
105       cs.registerOutParameter(pkIndex, jdbcType);
106       cs.execute();
107       Object JavaDoc pk = JDBCUtil.getParameter(log, cs, pkIndex, jdbcType, pkField.getFieldType());
108       pkField.setInstanceValue(ctx, pk);
109       return 1;
110    }
111    
112    /**
113     * Replace %%t in the sql command with the current table name
114     *
115     * @param in sql statement with possible %%t to substitute with table name
116     * @param table the table name
117     * @return String with sql statement
118     */

119    private static String JavaDoc replaceTable(String JavaDoc in, String JavaDoc table)
120    {
121       int pos;
122
123       pos = in.indexOf("%%t");
124       // No %%t -> return input
125
if(pos == -1)
126          return in;
127
128       String JavaDoc first = in.substring(0, pos);
129       String JavaDoc last = in.substring(pos + 3);
130
131       return first + table + last;
132    }
133    
134 }
135
Popular Tags