Author: cbegin
Date: Fri May 15 04:48:38 2009
New Revision: 775003
URL: http://svn.apache.org/viewvc?rev=775003&view=rev
Log:
Extracted KeyGenerator interface from multiple classes, created placeholder for
the selectkey generator
Added:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/keygen/
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/keygen/KeyGenerator.java
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/keygen/SelectKeyGenerator.java
Modified:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/builder/xml/XMLStatementParser.java
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/BatchExecutor.java
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/statement/BaseStatementHandler.java
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/statement/PreparedStatementHandler.java
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/statement/SimpleStatementHandler.java
Modified:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/builder/xml/XMLStatementParser.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/builder/xml/XMLStatementParser.java?rev=775003&r1=775002&r2=775003&view=diff
==============================================================================
---
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/builder/xml/XMLStatementParser.java
(original)
+++
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/builder/xml/XMLStatementParser.java
Fri May 15 04:48:38 2009
@@ -92,6 +92,7 @@
put("choose", new ChooseHandler());
put("when", new IfHandler());
put("otherwise", new OtherwiseHandler());
+ put("selectKey", new SelectKeyHandler());
}
};
@@ -99,6 +100,41 @@
void handleNode(NodeletContext nodeToHandle, List<SqlNode> targetContents);
}
+ private class SelectKeyHandler implements NodeHandler {
+ public void handleNode(NodeletContext nodeToHandle, List<SqlNode>
targetContents) {
+ String id = nodeToHandle.getStringAttribute("id");
+ Integer fetchSize = nodeToHandle.getIntAttribute("fetchSize", null);
+ Integer timeout = nodeToHandle.getIntAttribute("timeout", null);
+ boolean isSelect = "select".equals(nodeToHandle.getNode().getNodeName());
+ boolean flushCache = nodeToHandle.getBooleanAttribute("flushCache",
!isSelect);
+ boolean useCache = nodeToHandle.getBooleanAttribute("useCache",
isSelect);
+ String parameterMap = nodeToHandle.getStringAttribute("parameterMap");
+ String parameterType = nodeToHandle.getStringAttribute("parameterType");
+ Class parameterTypeClass = resolveClass(parameterType);
+ String resultMap = nodeToHandle.getStringAttribute("resultMap");
+ String resultType = nodeToHandle.getStringAttribute("resultType");
+
+ Class resultTypeClass = resolveClass(resultType);
+ String resultSetType = nodeToHandle.getStringAttribute("resultSetType");
+ StatementType statementType =
StatementType.valueOf(nodeToHandle.getStringAttribute("statementType",
StatementType.PREPARED.toString()));
+ ResultSetType resultSetTypeEnum = resolveResultSetType(resultSetType);
+
+ List<SqlNode> contents = parseDynamicTags(nodeToHandle);
+ MixedSqlNode rootSqlNode = new MixedSqlNode(contents);
+ SqlSource sqlSource = new DynamicSqlSource(configuration, rootSqlNode);
+ String nodeName = nodeToHandle.getNode().getNodeName();
+ SqlCommandType sqlCommandType =
SqlCommandType.valueOf(nodeName.toUpperCase());
+
+ String keyProperty = nodeToHandle.getStringAttribute("keyProperty");
+ boolean useGeneratedKeys =
nodeToHandle.getBooleanAttribute("useGeneratedKeys",
+ configuration.isUseGeneratedKeys() &&
SqlCommandType.INSERT.equals(sqlCommandType));
+
+ sequentialBuilder.statement(id, sqlSource, fetchSize, timeout,
parameterMap, parameterTypeClass,
+ resultMap, resultTypeClass, resultSetTypeEnum, isSelect, flushCache,
useCache, statementType, sqlCommandType,useGeneratedKeys,keyProperty);
+
+ }
+ }
+
private class IncludeNodeHandler implements NodeHandler {
public void handleNode(NodeletContext nodeToHandle, List<SqlNode>
targetContents) {
String refid = nodeToHandle.getStringAttribute("refid");
Modified:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/BatchExecutor.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/BatchExecutor.java?rev=775003&r1=775002&r2=775003&view=diff
==============================================================================
---
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/BatchExecutor.java
(original)
+++
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/BatchExecutor.java
Fri May 15 04:48:38 2009
@@ -2,6 +2,7 @@
import org.apache.ibatis.executor.result.ResultHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
+import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
import org.apache.ibatis.mapping.*;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.reflection.MetaObject;
@@ -63,7 +64,9 @@
BatchResult batchResult = batchResultList.get(i);
try {
batchResult.setUpdateCounts(stmt.executeBatch());
- processBatchGeneratedKeys(batchResult, stmt);
+ MappedStatement ms = batchResult.getMappedStatement();
+ Object parameter = batchResult.getParameterObject();
+ new Jdbc3KeyGenerator().processGeneratedKeys(ms, stmt, parameter);
} catch (BatchUpdateException e) {
StringBuffer message = new StringBuffer();
message.append(batchResult.getMappedStatement().getId())
@@ -91,42 +94,6 @@
}
}
- protected void processBatchGeneratedKeys(BatchResult batchResult, Statement
stmt) throws SQLException {
- MappedStatement ms = batchResult.getMappedStatement();
- Configuration configuration = ms.getConfiguration();
- TypeHandlerRegistry typeHandlerRegistry =
configuration.getTypeHandlerRegistry();
- Object parameter = batchResult.getParameterObject();
- if (parameter != null && ms.isUseGeneratedKeys()) {
- String keyProperty = ms.getKeyProperty();
- final MetaObject metaParam = MetaObject.forObject(parameter);
- if (keyProperty != null && metaParam.hasSetter(keyProperty)) {
- Class keyPropertyType = metaParam.getSetterType(keyProperty);
- TypeHandler th = typeHandlerRegistry.getTypeHandler(keyPropertyType);
- if (th != null) {
- ResultSet rs = stmt.getGeneratedKeys();
- try {
- ResultSetMetaData rsmd = rs.getMetaData();
- int colCount = rsmd.getColumnCount();
- if (colCount > 0) {
- String colName = rsmd.getColumnName(1);
- while (rs.next()) {
- Object value = th.getResult(rs,colName);
- metaParam.setValue(keyProperty,value);
- }
- }
- } finally {
- try {
- if (rs != null) rs.close();
- } catch (Exception e) {
- //ignore
- }
- }
- }
- }
- }
- }
-
-
}
Added:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java?rev=775003&view=auto
==============================================================================
---
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java
(added)
+++
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java
Fri May 15 04:48:38 2009
@@ -0,0 +1,50 @@
+package org.apache.ibatis.executor.keygen;
+
+import org.apache.ibatis.mapping.*;
+import org.apache.ibatis.type.*;
+import org.apache.ibatis.reflection.MetaObject;
+import org.apache.ibatis.executor.ExecutorException;
+
+import java.sql.*;
+
+public class Jdbc3KeyGenerator implements KeyGenerator {
+
+ public void processGeneratedKeys(MappedStatement ms, Statement stmt, Object
parameter) {
+ try {
+ final Configuration configuration = ms.getConfiguration();
+ final TypeHandlerRegistry typeHandlerRegistry =
configuration.getTypeHandlerRegistry();
+ if (parameter != null && ms.isUseGeneratedKeys()) {
+ String keyProperty = ms.getKeyProperty();
+ final MetaObject metaParam = MetaObject.forObject(parameter);
+ if (keyProperty != null && metaParam.hasSetter(keyProperty)) {
+ Class keyPropertyType = metaParam.getSetterType(keyProperty);
+ TypeHandler th =
typeHandlerRegistry.getTypeHandler(keyPropertyType);
+ if (th != null) {
+ ResultSet rs = stmt.getGeneratedKeys();
+ try {
+ ResultSetMetaData rsmd = rs.getMetaData();
+ int colCount = rsmd.getColumnCount();
+ if (colCount > 0) {
+ String colName = rsmd.getColumnName(1);
+ while (rs.next()) {
+ Object value = th.getResult(rs,colName);
+ metaParam.setValue(keyProperty,value);
+ }
+ }
+ } finally {
+ try {
+ if (rs != null) rs.close();
+ } catch (Exception e) {
+ //ignore
+ }
+ }
+ }
+ }
+ }
+ } catch (Exception e) {
+ throw new ExecutorException("Error getting generated key or setting
result to parameter object. Cause: " + e, e);
+ }
+ }
+
+
+}
Added:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/keygen/KeyGenerator.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/keygen/KeyGenerator.java?rev=775003&view=auto
==============================================================================
---
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/keygen/KeyGenerator.java
(added)
+++
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/keygen/KeyGenerator.java
Fri May 15 04:48:38 2009
@@ -0,0 +1,12 @@
+package org.apache.ibatis.executor.keygen;
+
+import org.apache.ibatis.mapping.MappedStatement;
+
+import java.sql.*;
+
+public interface KeyGenerator {
+
+ void processGeneratedKeys(MappedStatement ms, Statement stmt, Object
parameter);
+
+
+}
Added:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/keygen/SelectKeyGenerator.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/keygen/SelectKeyGenerator.java?rev=775003&view=auto
==============================================================================
---
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/keygen/SelectKeyGenerator.java
(added)
+++
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/keygen/SelectKeyGenerator.java
Fri May 15 04:48:38 2009
@@ -0,0 +1,50 @@
+package org.apache.ibatis.executor.keygen;
+
+import org.apache.ibatis.mapping.*;
+import org.apache.ibatis.type.*;
+import org.apache.ibatis.reflection.MetaObject;
+import org.apache.ibatis.executor.ExecutorException;
+
+import java.sql.*;
+
+public class SelectKeyGenerator implements KeyGenerator {
+
+ public void processGeneratedKeys(MappedStatement ms, Statement stmt, Object
parameter) {
+ try {
+ final Configuration configuration = ms.getConfiguration();
+ final TypeHandlerRegistry typeHandlerRegistry =
configuration.getTypeHandlerRegistry();
+
+ if (parameter != null && ms.isUseGeneratedKeys()) {
+ String keyProperty = ms.getKeyProperty();
+ final MetaObject metaParam = MetaObject.forObject(parameter);
+ if (keyProperty != null && metaParam.hasSetter(keyProperty)) {
+ Class keyPropertyType = metaParam.getSetterType(keyProperty);
+ TypeHandler th =
typeHandlerRegistry.getTypeHandler(keyPropertyType);
+ if (th != null) {
+ ResultSet rs = stmt.getGeneratedKeys();
+ try {
+ ResultSetMetaData rsmd = rs.getMetaData();
+ int colCount = rsmd.getColumnCount();
+ if (colCount > 0) {
+ String colName = rsmd.getColumnName(1);
+ while (rs.next()) {
+ Object value = th.getResult(rs,colName);
+ metaParam.setValue(keyProperty,value);
+ }
+ }
+ } finally {
+ try {
+ if (rs != null) rs.close();
+ } catch (Exception e) {
+ //ignore
+ }
+ }
+ }
+ }
+ }
+ } catch (Exception e) {
+ throw new ExecutorException("Error getting generated key or setting
result to parameter object. Cause: " + e, e);
+ }
+ }
+
+}
Modified:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/statement/BaseStatementHandler.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/statement/BaseStatementHandler.java?rev=775003&r1=775002&r2=775003&view=diff
==============================================================================
---
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/statement/BaseStatementHandler.java
(original)
+++
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/statement/BaseStatementHandler.java
Fri May 15 04:48:38 2009
@@ -69,38 +69,6 @@
protected abstract Statement instantiateStatement(Connection connection)
throws SQLException;
-
- protected void processGeneratedKeys(MappedStatement ms, Statement stmt,
Object parameter) throws SQLException {
- if (parameter != null && ms.isUseGeneratedKeys()) {
- String keyProperty = ms.getKeyProperty();
- final MetaObject metaParam = MetaObject.forObject(parameter);
- if (keyProperty != null && metaParam.hasSetter(keyProperty)) {
- Class keyPropertyType = metaParam.getSetterType(keyProperty);
- TypeHandler th = typeHandlerRegistry.getTypeHandler(keyPropertyType);
- if (th != null) {
- ResultSet rs = stmt.getGeneratedKeys();
- try {
- ResultSetMetaData rsmd = rs.getMetaData();
- int colCount = rsmd.getColumnCount();
- if (colCount > 0) {
- String colName = rsmd.getColumnName(1);
- while (rs.next()) {
- Object value = th.getResult(rs,colName);
- metaParam.setValue(keyProperty,value);
- }
- }
- } finally {
- try {
- if (rs != null) rs.close();
- } catch (Exception e) {
- //ignore
- }
- }
- }
- }
- }
- }
-
protected void setStatementTimeout(Statement stmt)
throws SQLException {
Integer timeout = mappedStatement.getTimeout();
Modified:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/statement/PreparedStatementHandler.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/statement/PreparedStatementHandler.java?rev=775003&r1=775002&r2=775003&view=diff
==============================================================================
---
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/statement/PreparedStatementHandler.java
(original)
+++
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/statement/PreparedStatementHandler.java
Fri May 15 04:48:38 2009
@@ -1,6 +1,7 @@
package org.apache.ibatis.executor.statement;
import org.apache.ibatis.executor.Executor;
+import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
import org.apache.ibatis.executor.result.ResultHandler;
import org.apache.ibatis.mapping.MappedStatement;
@@ -19,7 +20,7 @@
ps.execute();
int result = ps.getUpdateCount();
Object parameterObject = boundSql.getParameterObject();
- processGeneratedKeys(mappedStatement, ps, parameterObject);
+ new Jdbc3KeyGenerator().processGeneratedKeys(mappedStatement, ps,
parameterObject);
return result;
}
Modified:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/statement/SimpleStatementHandler.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/statement/SimpleStatementHandler.java?rev=775003&r1=775002&r2=775003&view=diff
==============================================================================
---
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/statement/SimpleStatementHandler.java
(original)
+++
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/statement/SimpleStatementHandler.java
Fri May 15 04:48:38 2009
@@ -1,6 +1,7 @@
package org.apache.ibatis.executor.statement;
import org.apache.ibatis.executor.Executor;
+import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
import org.apache.ibatis.executor.result.ResultHandler;
import org.apache.ibatis.mapping.*;
@@ -23,7 +24,7 @@
statement.execute(sql);
}
int result = statement.getUpdateCount();
- processGeneratedKeys(mappedStatement, statement, parameterObject);
+ new Jdbc3KeyGenerator().processGeneratedKeys(mappedStatement, statement,
parameterObject);
return result;
}