Skip to content

Commit

Permalink
TEIID-1557 adding support for generated keys
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Dec 10, 2012
1 parent 7f96a3f commit b8432bf
Show file tree
Hide file tree
Showing 22 changed files with 457 additions and 72 deletions.
21 changes: 21 additions & 0 deletions api/src/main/java/org/teiid/CommandContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,26 @@ public interface CommandContext {
* @return true if this is a continuous query
*/
boolean isContinuous();

/**
* Whether to return the keys generated by an insert.
* @return
*/
boolean isReturnAutoGeneratedKeys();

/**
* Creates a holder for generated keys.
* @param columnNames
* @param columnDataTypes
* @return
*/
GeneratedKeys returnGeneratedKeys(String[] columnNames,
Class<?>[] columnDataTypes);

/**
* Returns the last set of generated keys or null if no keys have been generated.
*/
GeneratedKeys getGeneratedKeys();


}
55 changes: 55 additions & 0 deletions api/src/main/java/org/teiid/GeneratedKeys.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* JBoss, Home of Professional Open Source.
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/

package org.teiid;

import java.util.Iterator;
import java.util.List;

public interface GeneratedKeys {

/**
* Add a generated key to this result. The list values must match the class types of this result.
* @param vals
*/
void addKey(List<?> vals);

/**
* Get the column names of this result.
* @return
*/
String[] getColumnNames();

/**
* Get the column types of this result.
* @return
*/
Class<?>[] getColumnTypes();

/**
* Get an iterator to the keys added to this result. The iterator is not guaranteed to be thread-safe
* with respect to the {@link #addKey(List)} method.
* @return
*/
Iterator<List<?>> getKeyIterator();

}
2 changes: 1 addition & 1 deletion api/src/main/java/org/teiid/translator/TypeFacility.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static String getDataTypeName(Class<?> type) {
}

/**
* Get the String constant for the given class
* Get the closest runtime type for the given class
*/
public static Class<?> getRuntimeType(Class<?> type) {
if (type.isPrimitive()) {
Expand Down
18 changes: 18 additions & 0 deletions client/src/main/java/org/teiid/client/RequestMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@

package org.teiid.client;

import java.io.EOFException;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.OptionalDataException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -92,6 +94,7 @@ public enum ShowPlan {
private long executionId;
private int transactionIsolation;
private boolean noExec;
private boolean autoGeneratedKeys;

/*
* Used by embedded connections, could change if we add support
Expand Down Expand Up @@ -396,6 +399,12 @@ public void readExternal(ObjectInput in) throws IOException,
this.executionId = in.readLong();
this.transactionIsolation = in.readInt();
this.noExec = in.readBoolean();
try {
//8.3 property
this.autoGeneratedKeys = in.readBoolean();
} catch (OptionalDataException e) {
} catch (EOFException e) {
}
}

@Override
Expand All @@ -420,6 +429,7 @@ public void writeExternal(ObjectOutput out) throws IOException {
out.writeLong(executionId);
out.writeInt(transactionIsolation);
out.writeBoolean(noExec);
out.writeBoolean(autoGeneratedKeys);
}

public RequestOptions getRequestOptions() {
Expand All @@ -432,5 +442,13 @@ public RequestOptions getRequestOptions() {
public void setRequestOptions(RequestOptions requestOptions) {
this.requestOptions = requestOptions;
}

public void setReturnAutoGeneratedKeys(boolean autoGenerateKeys) {
this.autoGeneratedKeys = autoGenerateKeys;
}

public boolean isReturnAutoGeneratedKeys() {
return this.autoGeneratedKeys;
}

}
25 changes: 25 additions & 0 deletions client/src/main/java/org/teiid/client/ResultsMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@

package org.teiid.client;

import java.io.EOFException;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.OptionalDataException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -83,6 +85,7 @@ public class ResultsMessage implements Externalizable {
private Collection<Annotation> annotations;

private boolean isUpdateResult;
private int updateCount = -1;

public ResultsMessage(){
}
Expand Down Expand Up @@ -111,6 +114,10 @@ public List<?>[] getResults() {
public void setResults(List<?>[] results) {
this.results = Arrays.asList(results);
}

public void setResults(List<? extends List<?>> results) {
this.results = results;
}

public String[] getColumnNames() {
return this.columnNames;
Expand Down Expand Up @@ -266,6 +273,13 @@ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundExcept
debugLog = (String)in.readObject();
annotations = ExternalizeUtil.readList(in, Annotation.class);
isUpdateResult = in.readBoolean();
if (isUpdateResult) {
try {
updateCount = in.readInt();
} catch (OptionalDataException e) {
} catch (EOFException e) {
}
}
}

public void writeExternal(ObjectOutput out) throws IOException {
Expand Down Expand Up @@ -300,6 +314,9 @@ public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(debugLog);
ExternalizeUtil.writeCollection(out, annotations);
out.writeBoolean(isUpdateResult);
if (isUpdateResult) {
out.writeInt(updateCount);
}
}

/**
Expand Down Expand Up @@ -357,5 +374,13 @@ public byte getClientSerializationVersion() {
public void setClientSerializationVersion(byte clientSerializationVersion) {
this.clientSerializationVersion = clientSerializationVersion;
}

public void setUpdateCount(int updateCount) {
this.updateCount = updateCount;
}

public int getUpdateCount() {
return updateCount;
}
}

28 changes: 10 additions & 18 deletions client/src/main/java/org/teiid/jdbc/ConnectionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -573,21 +573,12 @@ public PreparedStatementImpl prepareStatement(String sql) throws SQLException {
return prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
}

/**
* <p>Creates a PreparedStatement object that contains a sql and that will produce
* ResultSet objects of the type resultSetType and with a concurrency level of
* resultSetConcurrency.</p>
* @param sql string representing a prepared statement
* @param intvalue indicating the ResultSet's type
* @param intValue indicating the ResultSet's concurrency
* @return a PreparedStatement object
*/
public PreparedStatementImpl prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return prepareStatement(sql, resultSetType, resultSetConcurrency, ResultSet.HOLD_CURSORS_OVER_COMMIT);
}

public PreparedStatementImpl prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability ) throws SQLException {
int resultSetHoldability, int autoGeneratedKeys) throws SQLException {
//Check to see the connection is open
checkConnection();

Expand All @@ -597,8 +588,14 @@ public PreparedStatementImpl prepareStatement(String sql, int resultSetType, int

// add the statement object to the map
PreparedStatementImpl newStatement = new PreparedStatementImpl(this, sql, resultSetType, resultSetConcurrency);
newStatement.setAutoGeneratedKeys(autoGeneratedKeys == Statement.RETURN_GENERATED_KEYS);
statements.add(newStatement);
return newStatement;
}

public PreparedStatementImpl prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability ) throws SQLException {
return prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability, Statement.NO_GENERATED_KEYS);
}

public void rollback() throws SQLException {
Expand Down Expand Up @@ -905,30 +902,25 @@ public CallableStatementImpl prepareCall(String sql, int resultSetType,

public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
throws SQLException {
throw SqlUtil.createFeatureNotSupportedException();

return prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT, Statement.RETURN_GENERATED_KEYS);
}

public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
throws SQLException {
throw SqlUtil.createFeatureNotSupportedException();

return prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT, Statement.RETURN_GENERATED_KEYS);
}

public PreparedStatement prepareStatement(String sql, String[] columnNames)
throws SQLException {
throw SqlUtil.createFeatureNotSupportedException();

return prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT, Statement.RETURN_GENERATED_KEYS);
}

public void releaseSavepoint(Savepoint savepoint) throws SQLException {
throw SqlUtil.createFeatureNotSupportedException();

}

public void rollback(Savepoint savepoint) throws SQLException {
throw SqlUtil.createFeatureNotSupportedException();

}

public void setHoldability(int holdability) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1962,15 +1962,8 @@ public boolean supportsFullOuterJoins() throws SQLException {
return true;
}

/**
* Retrieves whether auto-generated keys can be retrieved after a
* statement has been executed.
* @return boolean true if auto-generated keys can be retrieved
* after a statement has executed; false otherwise
* @throws SQLException, should never occur
*/
public boolean supportsGetGeneratedKeys() throws SQLException {
return false;
return true;
}

public boolean supportsGroupBy() throws SQLException {
Expand Down
51 changes: 49 additions & 2 deletions client/src/main/java/org/teiid/jdbc/PreparedStatementImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public class PreparedStatementImpl extends StatementImpl implements TeiidPrepare

private Calendar serverCalendar;
private Object command;

private boolean autoGeneratedKeys;

/**
* <p>MMPreparedStatement constructor.
Expand All @@ -105,6 +107,10 @@ public class PreparedStatementImpl extends StatementImpl implements TeiidPrepare
this.serverCalendar = Calendar.getInstance(timezone);
}
}

public void setAutoGeneratedKeys(boolean getAutoGeneratedKeys) {
this.autoGeneratedKeys = getAutoGeneratedKeys;
}

/**
* <p>Adds a set of parameters to this PreparedStatement object's list of commands
Expand Down Expand Up @@ -170,6 +176,47 @@ public int executeUpdate(String sql) throws SQLException {
throw new TeiidSQLException(msg);
}

@Override
public boolean execute(String sql, int autoGeneratedKeys)
throws SQLException {
String msg = JDBCPlugin.Util.getString("JDBC.Method_not_supported"); //$NON-NLS-1$
throw new TeiidSQLException(msg);
}

@Override
public int executeUpdate(String sql, int autoGeneratedKeys)
throws SQLException {
String msg = JDBCPlugin.Util.getString("JDBC.Method_not_supported"); //$NON-NLS-1$
throw new TeiidSQLException(msg);
}

@Override
public boolean execute(String sql, int[] columnIndexes) throws SQLException {
String msg = JDBCPlugin.Util.getString("JDBC.Method_not_supported"); //$NON-NLS-1$
throw new TeiidSQLException(msg);
}

@Override
public boolean execute(String sql, String[] columnNames)
throws SQLException {
String msg = JDBCPlugin.Util.getString("JDBC.Method_not_supported"); //$NON-NLS-1$
throw new TeiidSQLException(msg);
}

@Override
public int executeUpdate(String sql, int[] columnIndexes)
throws SQLException {
String msg = JDBCPlugin.Util.getString("JDBC.Method_not_supported"); //$NON-NLS-1$
throw new TeiidSQLException(msg);
}

@Override
public int executeUpdate(String sql, String[] columnNames)
throws SQLException {
String msg = JDBCPlugin.Util.getString("JDBC.Method_not_supported"); //$NON-NLS-1$
throw new TeiidSQLException(msg);
}

@Override
public void addBatch(String sql) throws SQLException {
String msg = JDBCPlugin.Util.getString("JDBC.Method_not_supported"); //$NON-NLS-1$
Expand Down Expand Up @@ -208,13 +255,13 @@ public int[] executeBatch() throws SQLException {

@Override
public ResultSet executeQuery() throws SQLException {
executeSql(new String[] {this.prepareSql}, false, ResultsMode.RESULTSET, true, null);
executeSql(new String[] {this.prepareSql}, false, ResultsMode.RESULTSET, true, null, autoGeneratedKeys);
return resultSet;
}

@Override
public int executeUpdate() throws SQLException {
executeSql(new String[] {this.prepareSql}, false, ResultsMode.UPDATECOUNT, true, null);
executeSql(new String[] {this.prepareSql}, false, ResultsMode.UPDATECOUNT, true, null, autoGeneratedKeys);
return this.updateCounts[0];
}

Expand Down
Loading

0 comments on commit b8432bf

Please sign in to comment.