001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004:
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008:
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on 24/05/2004 17:40:25
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.dao.generic;
044:
045: import java.sql.Connection;
046: import java.sql.PreparedStatement;
047: import java.sql.ResultSet;
048: import java.sql.SQLException;
049: import java.sql.Statement;
050:
051: import net.jforum.JForumExecutionContext;
052: import net.jforum.util.DbUtils;
053: import net.jforum.util.preferences.ConfigKeys;
054: import net.jforum.util.preferences.SystemGlobals;
055:
056: /**
057: * @author Rafael Steil
058: * @version $Id: AutoKeys.java,v 1.11 2007/10/01 14:26:43 rafaelsteil Exp $
059: */
060: public class AutoKeys {
061: private String autoGeneratedKeysQuery;
062:
063: protected boolean supportAutoGeneratedKeys() {
064: return SystemGlobals
065: .getBoolValue(ConfigKeys.DATABASE_AUTO_KEYS);
066: }
067:
068: /**
069: * {@link #setSupportAutoGeneratedKey(boolean)} is set to <code>false</code>
070: *
071: * @param query The query to execute to retreive the last generated key
072: */
073: protected void setAutoGeneratedKeysQuery(String query) {
074: this .autoGeneratedKeysQuery = query;
075: }
076:
077: protected String getAutoGeneratedKeysQuery() {
078: return this .autoGeneratedKeysQuery;
079: }
080:
081: protected String getErrorMessage() {
082: return "Could not obtain the latest generated key. This error may be associated"
083: + " to some invalid database driver operation or server failure."
084: + " Please check the database configurations and code logic.";
085: }
086:
087: protected PreparedStatement getStatementForAutoKeys(
088: String queryName, Connection conn) throws SQLException {
089: PreparedStatement p = null;
090:
091: if (this .supportAutoGeneratedKeys()) {
092: p = conn.prepareStatement(SystemGlobals.getSql(queryName),
093: Statement.RETURN_GENERATED_KEYS);
094: } else {
095: p = conn.prepareStatement(SystemGlobals.getSql(queryName));
096: }
097:
098: return p;
099: }
100:
101: protected PreparedStatement getStatementForAutoKeys(String queryName)
102: throws SQLException {
103: return this .getStatementForAutoKeys(queryName,
104: JForumExecutionContext.getConnection());
105: }
106:
107: protected int executeAutoKeysQuery(PreparedStatement p)
108: throws SQLException {
109: return this .executeAutoKeysQuery(p, JForumExecutionContext
110: .getConnection());
111: }
112:
113: protected int executeAutoKeysQuery(PreparedStatement p,
114: Connection conn) throws SQLException {
115: int id = -1;
116: p.executeUpdate();
117:
118: ResultSet rs = null;
119:
120: try {
121: if (this .supportAutoGeneratedKeys()) {
122: rs = p.getGeneratedKeys();
123:
124: if (rs.next()) {
125: id = rs.getInt(1);
126: }
127: } else {
128: p = conn.prepareStatement(this
129: .getAutoGeneratedKeysQuery());
130: rs = p.executeQuery();
131:
132: if (rs.next()) {
133: id = rs.getInt(1);
134: }
135: }
136: } finally {
137: DbUtils.close(rs);
138: }
139:
140: if (id == -1) {
141: throw new SQLException(this.getErrorMessage());
142: }
143:
144: return id;
145: }
146: }
|