You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ibatis.apache.org by cb...@apache.org on 2007/03/03 01:33:48 UTC
svn commit: r514030 - in /ibatis/trunk/java/mapper/mapper2: build/
src/com/ibatis/sqlmap/engine/builder/xml/
src/com/ibatis/sqlmap/engine/conifg/
Author: cbegin
Date: Fri Mar 2 16:33:48 2007
New Revision: 514030
URL: http://svn.apache.org/viewvc?view=rev&rev=514030
Log:
Refactored SqlMapConfiguration into multiple configuration classes for better workflow control.
Added:
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/CacheModelConfig.java
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/MappedStatementConfig.java
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ParameterMapConfig.java
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ResultMapConfig.java
Modified:
ibatis/trunk/java/mapper/mapper2/build/version.properties
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapParser.java
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlStatementParser.java
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/XmlParserState.java
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlMapConfiguration.java
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlSource.java
Modified: ibatis/trunk/java/mapper/mapper2/build/version.properties
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/build/version.properties?view=diff&rev=514030&r1=514029&r2=514030
==============================================================================
--- ibatis/trunk/java/mapper/mapper2/build/version.properties (original)
+++ ibatis/trunk/java/mapper/mapper2/build/version.properties Fri Mar 2 16:33:48 2007
@@ -1,5 +1,5 @@
#Build version info
-#Fri Mar 02 14:49:45 MST 2007
+#Fri Mar 02 17:32:54 MST 2007
version=2.3.1
-buildDate=2007/03/02 14\:49
-buildNum=686
+buildDate=2007/03/02 17\:32
+buildNum=687
Modified: ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapParser.java
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapParser.java?view=diff&rev=514030&r1=514029&r2=514030
==============================================================================
--- ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapParser.java (original)
+++ ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapParser.java Fri Mar 2 16:33:48 2007
@@ -7,6 +7,9 @@
import com.ibatis.sqlmap.client.SqlMapException;
import com.ibatis.sqlmap.engine.cache.CacheModel;
import com.ibatis.sqlmap.engine.mapping.statement.*;
+import com.ibatis.sqlmap.engine.conifg.ParameterMapConfig;
+import com.ibatis.sqlmap.engine.conifg.ResultMapConfig;
+import com.ibatis.sqlmap.engine.conifg.CacheModelConfig;
import org.w3c.dom.Node;
import java.io.InputStream;
@@ -87,12 +90,6 @@
private void addCacheModelNodelets() {
parser.addNodelet("/sqlMap/cacheModel", new Nodelet() {
public void process(Node node) throws Exception {
- state.getConfig().cacheModel = new CacheModel();
- state.getConfig().cacheProps = new Properties();
- }
- });
- parser.addNodelet("/sqlMap/cacheModel/end()", new Nodelet() {
- public void process(Node node) throws Exception {
Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
String id = state.applyNamespace(attributes.getProperty("id"));
String type = attributes.getProperty("type");
@@ -100,7 +97,13 @@
Boolean readOnly = readOnlyAttr == null || readOnlyAttr.length() <= 0 ? null : new Boolean("true".equals(readOnlyAttr));
String serializeAttr = attributes.getProperty("serialize");
Boolean serialize = serializeAttr == null || serializeAttr.length() <= 0 ? null : new Boolean("true".equals(serializeAttr));
- state.getConfig().addCacheModel(id, type, readOnly, serialize, state.getConfig().cacheProps);
+ CacheModelConfig cacheConfig = state.getConfig().newCacheModelConfig(id, type, readOnly, serialize);
+ state.setCacheConfig(cacheConfig);
+ }
+ });
+ parser.addNodelet("/sqlMap/cacheModel/end()", new Nodelet() {
+ public void process(Node node) throws Exception {
+ state.getCacheConfig().saveCacheModel();
}
});
parser.addNodelet("/sqlMap/cacheModel/property", new Nodelet() {
@@ -109,14 +112,14 @@
Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
String name = attributes.getProperty("name");
String value = NodeletUtils.parsePropertyTokens(attributes.getProperty("value"), state.getGlobalProps());
- state.getConfig().cacheProps.put(name, value);
+ state.getCacheConfig().setProperty(name, value);
}
});
parser.addNodelet("/sqlMap/cacheModel/flushOnExecute", new Nodelet() {
public void process(Node node) throws Exception {
Properties childAttributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
String statement = childAttributes.getProperty("statement");
- state.getConfig().addFlushTriggerStatement(statement);
+ state.getCacheConfig().addFlushTriggerStatement(statement);
}
});
parser.addNodelet("/sqlMap/cacheModel/flushInterval", new Nodelet() {
@@ -127,9 +130,9 @@
int seconds = childAttributes.getProperty("seconds") == null ? 0 : Integer.parseInt(childAttributes.getProperty("seconds"));
int minutes = childAttributes.getProperty("minutes") == null ? 0 : Integer.parseInt(childAttributes.getProperty("minutes"));
int hours = childAttributes.getProperty("hours") == null ? 0 : Integer.parseInt(childAttributes.getProperty("hours"));
- state.getConfig().setFlushInterval(hours, minutes, seconds, milliseconds);
+ state.getCacheConfig().setFlushInterval(hours, minutes, seconds, milliseconds);
} catch (NumberFormatException e) {
- throw new RuntimeException("Error building cache '" + state.getConfig().cacheModel.getId() + "' in '" + "resourceNAME" + "'. Flush interval milliseconds must be a valid long integer value. Cause: " + e, e);
+ throw new RuntimeException("Error building cache in '" + "resourceNAME" + "'. Flush interval milliseconds must be a valid long integer value. Cause: " + e, e);
}
}
});
@@ -138,8 +141,8 @@
private void addParameterMapNodelets() {
parser.addNodelet("/sqlMap/parameterMap/end()", new Nodelet() {
public void process(Node node) throws Exception {
-
- state.getConfig().finalizeParameterMap();
+ state.getParamConfig().saveParameterMap();
+ state.setParamConfig(null);
}
});
parser.addNodelet("/sqlMap/parameterMap", new Nodelet() {
@@ -147,8 +150,8 @@
Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
String id = state.applyNamespace(attributes.getProperty("id"));
String parameterClassName = attributes.getProperty("class");
-
- state.getConfig().addParameterMap(id, parameterClassName);
+ ParameterMapConfig paramConf = state.getConfig().newParameterMapConfig(id, parameterClassName);
+ state.setParamConfig(paramConf);
}
});
parser.addNodelet("/sqlMap/parameterMap/parameter", new Nodelet() {
@@ -164,7 +167,7 @@
String callback = childAttributes.getProperty("typeHandler");
String numericScale = childAttributes.getProperty("numericScale");
- state.getConfig().addParameterMapping(callback, javaType, resultMap, propertyName, jdbcType, type, nullValue, mode, numericScale);
+ state.getParamConfig().addParameterMapping(callback, javaType, resultMap, propertyName, jdbcType, type, nullValue, mode, numericScale);
}
});
@@ -174,7 +177,7 @@
private void addResultMapNodelets() {
parser.addNodelet("/sqlMap/resultMap/end()", new Nodelet() {
public void process(Node node) throws Exception {
- state.getConfig().finalizeResultMap();
+ state.getResultConfig().saveResultMap();
}
});
parser.addNodelet("/sqlMap/resultMap", new Nodelet() {
@@ -185,7 +188,8 @@
String extended = state.applyNamespace(attributes.getProperty("extends"));
String xmlName = attributes.getProperty("xmlName");
String groupBy = attributes.getProperty("groupBy");
- state.getConfig().addResultMap(id, resultClassName, xmlName, groupBy, extended);
+ ResultMapConfig resultConf = state.getConfig().newResultMapConfig(id, resultClassName, xmlName, groupBy, extended);
+ state.setResultConfig(resultConf);
}
});
parser.addNodelet("/sqlMap/resultMap/result", new Nodelet() {
@@ -201,7 +205,7 @@
String resultMapName = childAttributes.getProperty("resultMap");
String callback = childAttributes.getProperty("typeHandler");
- state.getConfig().addResultMapping(callback, javaType, propertyName, jdbcType, columnName, nullValue, statementName, resultMapName, columnIndex);
+ state.getResultConfig().addResultMapping(callback, javaType, propertyName, jdbcType, columnName, nullValue, statementName, resultMapName, columnIndex);
}
});
@@ -211,7 +215,7 @@
String value = childAttributes.getProperty("value");
String resultMap = childAttributes.getProperty("resultMap");
resultMap = state.applyNamespace(resultMap);
- state.getConfig().addSubMap(value, resultMap);
+ state.getResultConfig().addSubMap(value, resultMap);
}
});
@@ -225,7 +229,7 @@
String columnIndex = childAttributes.getProperty("columnIndex");
String callback = childAttributes.getProperty("typeHandler");
- state.getConfig().addDiscriminator(callback, javaType, jdbcType, columnName, nullValue, columnIndex);
+ state.getResultConfig().setDiscriminator(callback, javaType, jdbcType, columnName, nullValue, columnIndex);
}
});
}
Modified: ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlStatementParser.java
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlStatementParser.java?view=diff&rev=514030&r1=514029&r2=514030
==============================================================================
--- ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlStatementParser.java (original)
+++ ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlStatementParser.java Fri Mar 2 16:33:48 2007
@@ -2,6 +2,7 @@
import com.ibatis.common.xml.NodeletUtils;
import com.ibatis.sqlmap.engine.mapping.statement.*;
+import com.ibatis.sqlmap.engine.conifg.MappedStatementConfig;
import org.w3c.dom.*;
import java.util.Properties;
@@ -48,23 +49,16 @@
additionalResultClasses = state.getAllButFirstToken(resultClassName);
resultClassName = state.getFirstToken(resultClassName);
}
- MappedStatement mappedStatement = state.getConfig().prepareGeneralStatement(new XMLSqlSource(state, node), statement, id, resultMapName, additionalResultMapNames, parameterMapName, resultSetType, fetchSize, parameterClassName, resultClassName, additionalResultClasses, allowRemapping, xmlResultName, timeout, cacheModelName);
+ MappedStatementConfig statementConf = state.getConfig().newMappedStatementConfig(new XMLSqlSource(state, node), statement, id, resultMapName, additionalResultMapNames, parameterMapName, resultSetType, fetchSize, parameterClassName, resultClassName, additionalResultClasses, allowRemapping, xmlResultName, timeout, cacheModelName);
- findAndParseSelectKey(node, statement);
- return mappedStatement;
+ findAndParseSelectKey(node, statementConf);
+ return statementConf.getMappedStatement();
}
- private void findAndParseSelectKey(Node node, GeneralStatement statement) {
- if (statement instanceof InsertStatement) {
+ private void findAndParseSelectKey(Node node, MappedStatementConfig config) {
state.getConfig().getErrorContext().setActivity("parsing select key tags");
-
- InsertStatement insertStatement = ((InsertStatement) statement);
-
- SelectKeyStatement selectKeyStatement = null;
-
boolean foundSQLFirst = false;
-
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
@@ -80,13 +74,12 @@
String keyPropName = attributes.getProperty("keyProperty");
String resultClassName = attributes.getProperty("resultClass");
String type = attributes.getProperty("type");
- selectKeyStatement = state.getConfig().prepareSelectKeyStatement(new XMLSqlSource(state, child), resultClassName, statement.getId(), keyPropName, foundSQLFirst, type, statement.getParameterClass());
+ config.setSelectKeyStatement(new XMLSqlSource(state, child), resultClassName, keyPropName, foundSQLFirst, type);
break;
}
}
state.getConfig().getErrorContext().setMoreInfo(null);
- insertStatement.setSelectKeyStatement(selectKeyStatement);
- }
+
}
Modified: ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/XmlParserState.java
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/XmlParserState.java?view=diff&rev=514030&r1=514029&r2=514030
==============================================================================
--- ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/XmlParserState.java (original)
+++ ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/XmlParserState.java Fri Mar 2 16:33:48 2007
@@ -2,6 +2,9 @@
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.engine.conifg.SqlMapConfiguration;
+import com.ibatis.sqlmap.engine.conifg.ParameterMapConfig;
+import com.ibatis.sqlmap.engine.conifg.ResultMapConfig;
+import com.ibatis.sqlmap.engine.conifg.CacheModelConfig;
import java.util.*;
@@ -15,6 +18,10 @@
private boolean useStatementNamespaces = false;
private Map sqlIncludes = new HashMap();
+ private ParameterMapConfig paramConfig;
+ private ResultMapConfig resultConfig;
+ private CacheModelConfig cacheConfig;
+
private String namespace;
public SqlMapConfiguration getConfig() {
@@ -36,7 +43,7 @@
public Properties getDsProps() {
return dsProps;
}
-
+
public void setUseStatementNamespaces(boolean useStatementNamespaces) {
this.useStatementNamespaces = useStatementNamespaces;
}
@@ -59,6 +66,30 @@
newId = namespace + "." + id;
}
return newId;
+ }
+
+ public CacheModelConfig getCacheConfig() {
+ return cacheConfig;
+ }
+
+ public void setCacheConfig(CacheModelConfig cacheConfig) {
+ this.cacheConfig = cacheConfig;
+ }
+
+ public ParameterMapConfig getParamConfig() {
+ return paramConfig;
+ }
+
+ public void setParamConfig(ParameterMapConfig paramConfig) {
+ this.paramConfig = paramConfig;
+ }
+
+ public ResultMapConfig getResultConfig() {
+ return resultConfig;
+ }
+
+ public void setResultConfig(ResultMapConfig resultConfig) {
+ this.resultConfig = resultConfig;
}
public String getFirstToken(String s) {
Added: ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/CacheModelConfig.java
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/CacheModelConfig.java?view=auto&rev=514030
==============================================================================
--- ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/CacheModelConfig.java (added)
+++ ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/CacheModelConfig.java Fri Mar 2 16:33:48 2007
@@ -0,0 +1,75 @@
+package com.ibatis.sqlmap.engine.conifg;
+
+import com.ibatis.sqlmap.engine.cache.CacheModel;
+import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
+import com.ibatis.sqlmap.engine.scope.ErrorContext;
+import com.ibatis.sqlmap.engine.type.TypeHandlerFactory;
+
+import java.util.Properties;
+
+public class CacheModelConfig {
+ private ErrorContext errorContext;
+ private CacheModel cacheModel;
+ private Properties properties;
+
+ CacheModelConfig(SqlMapConfiguration config, String id, String type, Boolean readOnly, Boolean serialize) {
+ this.errorContext = config.getErrorContext();
+ this.cacheModel = new CacheModel();
+ this.properties = new Properties();
+ ExtendedSqlMapClient client = config.getClient();
+ TypeHandlerFactory typeHandlerFactory = config.getTypeHandlerFactory();
+ errorContext.setActivity("building a cache model");
+ type = typeHandlerFactory.resolveAlias(type);
+ if (readOnly != null) {
+ cacheModel.setReadOnly(readOnly.booleanValue());
+ } else {
+ cacheModel.setReadOnly(true);
+ }
+ if (serialize != null) {
+ cacheModel.setSerialize(serialize.booleanValue());
+ } else {
+ cacheModel.setSerialize(false);
+ }
+ errorContext.setObjectId(id + " cache model");
+ errorContext.setMoreInfo("Check the cache model type.");
+ cacheModel.setId(id);
+ cacheModel.setResource(errorContext.getResource());
+ try {
+ cacheModel.setControllerClassName(type);
+ } catch (Exception e) {
+ throw new RuntimeException("Error setting Cache Controller Class. Cause: " + e, e);
+ }
+ errorContext.setMoreInfo("Check the cache model configuration.");
+ if (client.getDelegate().isCacheModelsEnabled()) {
+ client.getDelegate().addCacheModel(cacheModel);
+ }
+ errorContext.setMoreInfo(null);
+ errorContext.setObjectId(null);
+ }
+
+ public void setProperty(String name, String value) {
+ properties.setProperty(name, value);
+ }
+
+ public void setFlushInterval(int hours, int minutes, int seconds, int milliseconds) {
+ errorContext.setMoreInfo("Check the cache model flush interval.");
+ long t = 0;
+ t += milliseconds;
+ t += seconds * 1000;
+ t += minutes * 60 * 1000;
+ t += hours * 60 * 60 * 1000;
+ if (t < 1)
+ throw new RuntimeException("A flush interval must specify one or more of milliseconds, seconds, minutes or hours.");
+ cacheModel.setFlushInterval(t);
+ }
+
+ public void addFlushTriggerStatement(String statement) {
+ errorContext.setMoreInfo("Check the cache model flush on statement elements.");
+ cacheModel.addFlushTriggerStatement(statement);
+ }
+
+ public void saveCacheModel() {
+ cacheModel.configure(properties);
+ }
+
+}
Added: ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/MappedStatementConfig.java
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/MappedStatementConfig.java?view=auto&rev=514030
==============================================================================
--- ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/MappedStatementConfig.java (added)
+++ ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/MappedStatementConfig.java Fri Mar 2 16:33:48 2007
@@ -0,0 +1,241 @@
+package com.ibatis.sqlmap.engine.conifg;
+
+import com.ibatis.common.beans.Probe;
+import com.ibatis.common.beans.ProbeFactory;
+import com.ibatis.common.resources.Resources;
+import com.ibatis.sqlmap.client.SqlMapException;
+import com.ibatis.sqlmap.engine.cache.CacheModel;
+import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
+import com.ibatis.sqlmap.engine.mapping.parameter.BasicParameterMap;
+import com.ibatis.sqlmap.engine.mapping.parameter.InlineParameterMapParser;
+import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
+import com.ibatis.sqlmap.engine.mapping.result.AutoResultMap;
+import com.ibatis.sqlmap.engine.mapping.result.BasicResultMap;
+import com.ibatis.sqlmap.engine.mapping.sql.Sql;
+import com.ibatis.sqlmap.engine.mapping.sql.SqlText;
+import com.ibatis.sqlmap.engine.mapping.sql.dynamic.DynamicSql;
+import com.ibatis.sqlmap.engine.mapping.sql.simple.SimpleDynamicSql;
+import com.ibatis.sqlmap.engine.mapping.sql.stat.StaticSql;
+import com.ibatis.sqlmap.engine.mapping.statement.*;
+import com.ibatis.sqlmap.engine.scope.ErrorContext;
+import com.ibatis.sqlmap.engine.type.TypeHandlerFactory;
+
+import java.sql.ResultSet;
+import java.util.Arrays;
+import java.util.List;
+
+public class MappedStatementConfig {
+ private static final Probe PROBE = ProbeFactory.getProbe();
+ private static final InlineParameterMapParser PARAM_PARSER = new InlineParameterMapParser();
+ private SqlMapConfiguration config;
+ private ErrorContext errorContext;
+ private ExtendedSqlMapClient client;
+ private TypeHandlerFactory typeHandlerFactory;
+ private MappedStatement mappedStatement;
+ private MappedStatement rootStatement;
+
+ MappedStatementConfig(SqlMapConfiguration config, SqlSource processor, GeneralStatement statement, String id, String resultMapName, String[] additionalResultMapNames, String parameterMapName, String resultSetType, String fetchSize, String parameterClassName, String resultClassName, String[] additionalResultClasses, String allowRemapping, String xmlResultName, String timeout, String cacheModelName) {
+ this.config = config;
+ this.errorContext = config.getErrorContext();
+ this.client = config.getClient();
+ this.typeHandlerFactory = config.getTypeHandlerFactory();
+ errorContext.setActivity("parsing a mapped statement");
+ errorContext.setObjectId(id + " statement");
+ errorContext.setMoreInfo("Check the result map name.");
+ if (resultMapName != null) {
+ statement.setResultMap((BasicResultMap) client.getDelegate().getResultMap(resultMapName));
+ if (additionalResultMapNames != null) {
+ for (int i = 0; i < additionalResultMapNames.length; i++) {
+ statement.addResultMap((BasicResultMap) client.getDelegate().getResultMap(additionalResultMapNames[i]));
+ }
+ }
+ }
+ errorContext.setMoreInfo("Check the parameter map name.");
+ if (parameterMapName != null) {
+ statement.setParameterMap((BasicParameterMap) client.getDelegate().getParameterMap(parameterMapName));
+ }
+ statement.setId(id);
+ statement.setResource(errorContext.getResource());
+ if (resultSetType != null) {
+ if ("FORWARD_ONLY".equals(resultSetType)) {
+ statement.setResultSetType(new Integer(ResultSet.TYPE_FORWARD_ONLY));
+ } else if ("SCROLL_INSENSITIVE".equals(resultSetType)) {
+ statement.setResultSetType(new Integer(ResultSet.TYPE_SCROLL_INSENSITIVE));
+ } else if ("SCROLL_SENSITIVE".equals(resultSetType)) {
+ statement.setResultSetType(new Integer(ResultSet.TYPE_SCROLL_SENSITIVE));
+ }
+ }
+ if (fetchSize != null) {
+ statement.setFetchSize(new Integer(fetchSize));
+ }
+
+ // set parameter class either from attribute or from map (make sure to match)
+ ParameterMap parameterMap = statement.getParameterMap();
+ if (parameterMap == null) {
+ try {
+ if (parameterClassName != null) {
+ errorContext.setMoreInfo("Check the parameter class.");
+ parameterClassName = typeHandlerFactory.resolveAlias(parameterClassName);
+ Class parameterClass = Resources.classForName(parameterClassName);
+ statement.setParameterClass(parameterClass);
+ }
+ } catch (ClassNotFoundException e) {
+ throw new SqlMapException("Error. Could not set parameter class. Cause: " + e, e);
+ }
+ } else {
+ statement.setParameterClass(parameterMap.getParameterClass());
+ }
+
+ // process SQL statement, including inline parameter maps
+ errorContext.setMoreInfo("Check the SQL statement.");
+ Sql sql = processor.getSql();
+ setSqlForStatement(statement, sql);
+
+ // set up either null result map or automatic result mapping
+ BasicResultMap resultMap = (BasicResultMap) statement.getResultMap();
+ if (resultMap == null && resultClassName == null) {
+ statement.setResultMap(null);
+ } else if (resultMap == null) {
+ resultMap = buildAutoResultMap(allowRemapping, statement, resultClassName, xmlResultName);
+ statement.setResultMap(resultMap);
+ if (additionalResultClasses != null) {
+ for (int i = 0; i < additionalResultClasses.length; i++) {
+ statement.addResultMap(buildAutoResultMap(allowRemapping, statement, additionalResultClasses[i], xmlResultName));
+ }
+ }
+
+ }
+ statement.setTimeout(config.getDefaultStatementTimeout());
+ if (timeout != null) {
+ try {
+ statement.setTimeout(Integer.valueOf(timeout));
+ } catch (NumberFormatException e) {
+ throw new SqlMapException("Specified timeout value for statement " + statement.getId() + " is not a valid integer");
+ }
+ }
+ errorContext.setMoreInfo(null);
+ errorContext.setObjectId(null);
+ statement.setSqlMapClient(client);
+ if (cacheModelName != null && cacheModelName.length() > 0 && client.getDelegate().isCacheModelsEnabled()) {
+ CacheModel cacheModel = client.getDelegate().getCacheModel(cacheModelName);
+ mappedStatement = new CachingStatement(statement, cacheModel);
+ } else {
+ mappedStatement = statement;
+ }
+ rootStatement = statement;
+ }
+
+ public void setSelectKeyStatement(SqlSource processor, String resultClassName, String keyPropName, boolean runAfterSQL, String type) {
+ if (rootStatement instanceof InsertStatement) {
+ InsertStatement insertStatement = ((InsertStatement) rootStatement);
+ Class parameterClass = insertStatement.getParameterClass();
+ errorContext.setActivity("parsing a select key");
+ SelectKeyStatement selectKeyStatement = new SelectKeyStatement();
+ resultClassName = typeHandlerFactory.resolveAlias(resultClassName);
+ Class resultClass = null;
+
+ // get parameter and result maps
+ selectKeyStatement.setSqlMapClient(client);
+ selectKeyStatement.setId(insertStatement.getId() + "-SelectKey");
+ selectKeyStatement.setResource(errorContext.getResource());
+ selectKeyStatement.setKeyProperty(keyPropName);
+ selectKeyStatement.setRunAfterSQL(runAfterSQL);
+ // process the type (pre or post) attribute
+ if (type != null) {
+ selectKeyStatement.setRunAfterSQL("post".equals(type));
+ }
+ try {
+ if (resultClassName != null) {
+ errorContext.setMoreInfo("Check the select key result class.");
+ resultClass = Resources.classForName(resultClassName);
+ } else {
+ if (keyPropName != null && parameterClass != null) {
+ resultClass = PROBE.getPropertyTypeForSetter(parameterClass, selectKeyStatement.getKeyProperty());
+ }
+ }
+ } catch (ClassNotFoundException e) {
+ throw new SqlMapException("Error. Could not set result class. Cause: " + e, e);
+ }
+ if (resultClass == null) {
+ resultClass = Object.class;
+ }
+
+ // process SQL statement, including inline parameter maps
+ errorContext.setMoreInfo("Check the select key SQL statement.");
+ Sql sql = processor.getSql();
+ setSqlForStatement(selectKeyStatement, sql);
+ BasicResultMap resultMap;
+ resultMap = new AutoResultMap(client.getDelegate(), false);
+ resultMap.setId(selectKeyStatement.getId() + "-AutoResultMap");
+ resultMap.setResultClass(resultClass);
+ resultMap.setResource(selectKeyStatement.getResource());
+ selectKeyStatement.setResultMap(resultMap);
+ errorContext.setMoreInfo(null);
+ insertStatement.setSelectKeyStatement(selectKeyStatement);
+ } else {
+ throw new SqlMapException("You cant set a select key statement on statement named " + rootStatement.getId() + " because it is not an InsertStatement.");
+ }
+ }
+
+ private void setSqlForStatement(GeneralStatement statement, Sql sql) {
+ if (sql instanceof DynamicSql) {
+ statement.setSql(sql);
+ } else {
+ applyInlineParameterMap(statement, sql.getSql(null, null));
+ }
+ }
+
+ private void applyInlineParameterMap(GeneralStatement statement, String sqlStatement) {
+ String newSql = sqlStatement;
+ errorContext.setActivity("building an inline parameter map");
+ ParameterMap parameterMap = statement.getParameterMap();
+ errorContext.setMoreInfo("Check the inline parameters.");
+ if (parameterMap == null) {
+ BasicParameterMap map;
+ map = new BasicParameterMap(client.getDelegate());
+ map.setId(statement.getId() + "-InlineParameterMap");
+ map.setParameterClass(statement.getParameterClass());
+ map.setResource(statement.getResource());
+ statement.setParameterMap(map);
+ SqlText sqlText = PARAM_PARSER.parseInlineParameterMap(client.getDelegate().getTypeHandlerFactory(), newSql, statement.getParameterClass());
+ newSql = sqlText.getText();
+ List mappingList = Arrays.asList(sqlText.getParameterMappings());
+ map.setParameterMappingList(mappingList);
+ }
+ Sql sql;
+ if (SimpleDynamicSql.isSimpleDynamicSql(newSql)) {
+ sql = new SimpleDynamicSql(client.getDelegate(), newSql);
+ } else {
+ sql = new StaticSql(newSql);
+ }
+ statement.setSql(sql);
+
+ }
+
+ private BasicResultMap buildAutoResultMap(String allowRemapping, GeneralStatement statement, String firstResultClass, String xmlResultName) {
+ BasicResultMap resultMap;
+ resultMap = new AutoResultMap(client.getDelegate(), "true".equals(allowRemapping));
+ resultMap.setId(statement.getId() + "-AutoResultMap");
+ resultMap.setResultClass(resolveClass(firstResultClass));
+ resultMap.setXmlName(xmlResultName);
+ resultMap.setResource(statement.getResource());
+ return resultMap;
+ }
+
+ private Class resolveClass(String resultClassName) {
+ try {
+ if (resultClassName != null) {
+ errorContext.setMoreInfo("Check the result class.");
+ return Resources.classForName(typeHandlerFactory.resolveAlias(resultClassName));
+ } else {
+ return null;
+ }
+ } catch (ClassNotFoundException e) {
+ throw new SqlMapException("Error. Could not set result class. Cause: " + e, e);
+ }
+ }
+
+ public MappedStatement getMappedStatement() {
+ return mappedStatement;
+ }
+}
Added: ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ParameterMapConfig.java
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ParameterMapConfig.java?view=auto&rev=514030
==============================================================================
--- ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ParameterMapConfig.java (added)
+++ ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ParameterMapConfig.java Fri Mar 2 16:33:48 2007
@@ -0,0 +1,110 @@
+package com.ibatis.sqlmap.engine.conifg;
+
+import com.ibatis.common.resources.Resources;
+import com.ibatis.sqlmap.client.SqlMapException;
+import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;
+import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
+import com.ibatis.sqlmap.engine.mapping.parameter.BasicParameterMap;
+import com.ibatis.sqlmap.engine.mapping.parameter.BasicParameterMapping;
+import com.ibatis.sqlmap.engine.scope.ErrorContext;
+import com.ibatis.sqlmap.engine.type.CustomTypeHandler;
+import com.ibatis.sqlmap.engine.type.TypeHandler;
+import com.ibatis.sqlmap.engine.type.TypeHandlerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ParameterMapConfig {
+ private SqlMapConfiguration config;
+ private ErrorContext errorContext;
+ private ExtendedSqlMapClient client;
+ private TypeHandlerFactory typeHandlerFactory;
+ private BasicParameterMap parameterMap;
+ private List parameterMappingList;
+
+ ParameterMapConfig(SqlMapConfiguration config, String id, String parameterClassName) {
+ this.config = config;
+ this.errorContext = config.getErrorContext();
+ this.client = config.getClient();
+ this.typeHandlerFactory = config.getTypeHandlerFactory();
+ errorContext.setActivity("building a parameter map");
+ parameterMap = new BasicParameterMap(client.getDelegate());
+ parameterClassName = typeHandlerFactory.resolveAlias(parameterClassName);
+ parameterMap.setId(id);
+ parameterMap.setResource(errorContext.getResource());
+ errorContext.setObjectId(id + " parameter map");
+ Class parameterClass;
+ try {
+ errorContext.setMoreInfo("Check the parameter class.");
+ parameterClass = Resources.classForName(parameterClassName);
+ } catch (Exception e) {
+ throw new SqlMapException("Error configuring ParameterMap. Could not set ParameterClass. Cause: " + e, e);
+ }
+ parameterMap.setParameterClass(parameterClass);
+ errorContext.setMoreInfo("Check the parameter mappings.");
+ this.parameterMappingList = new ArrayList();
+ }
+
+ public void addParameterMapping(String callback, String javaType, String resultMap, String propertyName, String jdbcType, String type, String nullValue, String mode, String numericScale) {
+ callback = typeHandlerFactory.resolveAlias(callback);
+ javaType = typeHandlerFactory.resolveAlias(javaType);
+ errorContext.setObjectId(propertyName + " mapping of the " + parameterMap.getId() + " parameter map");
+ TypeHandler handler;
+ if (callback != null) {
+ errorContext.setMoreInfo("Check the parameter mapping typeHandler attribute '" + callback + "' (must be a TypeHandler or TypeHandlerCallback implementation).");
+ try {
+ Object impl = Resources.instantiate(callback);
+ if (impl instanceof TypeHandlerCallback) {
+ handler = new CustomTypeHandler((TypeHandlerCallback) impl);
+ } else if (impl instanceof TypeHandler) {
+ handler = (TypeHandler) impl;
+ } else {
+ throw new RuntimeException("The class '" + callback + "' is not a valid implementation of TypeHandler or TypeHandlerCallback");
+ }
+ } catch (Exception e) {
+ throw new RuntimeException("Error occurred during custom type handler configuration. Cause: " + e, e);
+ }
+ } else {
+ errorContext.setMoreInfo("Check the parameter mapping property type or name.");
+ handler = config.resolveTypeHandler(client.getDelegate().getTypeHandlerFactory(), parameterMap.getParameterClass(), propertyName, javaType, jdbcType);
+ }
+ BasicParameterMapping mapping = new BasicParameterMapping();
+ mapping.setPropertyName(propertyName);
+ mapping.setJdbcTypeName(jdbcType);
+ mapping.setTypeName(type);
+ mapping.setResultMapName(resultMap);
+ mapping.setNullValue(nullValue);
+ if (mode != null && mode.length() > 0) {
+ mapping.setMode(mode);
+ }
+ mapping.setTypeHandler(handler);
+ try {
+ if (javaType != null && javaType.length() > 0) {
+ mapping.setJavaType(Resources.classForName(javaType));
+ }
+ } catch (ClassNotFoundException e) {
+ throw new RuntimeException("Error setting javaType on parameter mapping. Cause: " + e);
+ }
+ if (numericScale != null) {
+ try {
+ Integer scale = Integer.valueOf(numericScale);
+ if (scale.intValue() < 0) {
+ throw new RuntimeException("Error setting numericScale on parameter mapping. Cause: scale must be greater than or equal to zero");
+ }
+ mapping.setNumericScale(scale);
+ } catch (NumberFormatException e) {
+ throw new RuntimeException("Error setting numericScale on parameter mapping. Cause: " + numericScale + " is not a valid integer");
+ }
+ }
+ parameterMappingList.add(mapping);
+ }
+
+ public void saveParameterMap() {
+ parameterMap.setParameterMappingList(parameterMappingList);
+ client.getDelegate().addParameterMap(parameterMap);
+ errorContext.setMoreInfo(null);
+ errorContext.setObjectId(null);
+ }
+
+
+}
Added: ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ResultMapConfig.java
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ResultMapConfig.java?view=auto&rev=514030
==============================================================================
--- ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ResultMapConfig.java (added)
+++ ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ResultMapConfig.java Fri Mar 2 16:33:48 2007
@@ -0,0 +1,196 @@
+package com.ibatis.sqlmap.engine.conifg;
+
+import com.ibatis.common.resources.Resources;
+import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;
+import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
+import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
+import com.ibatis.sqlmap.engine.mapping.result.BasicResultMap;
+import com.ibatis.sqlmap.engine.mapping.result.BasicResultMapping;
+import com.ibatis.sqlmap.engine.mapping.result.Discriminator;
+import com.ibatis.sqlmap.engine.mapping.result.ResultMapping;
+import com.ibatis.sqlmap.engine.scope.ErrorContext;
+import com.ibatis.sqlmap.engine.type.CustomTypeHandler;
+import com.ibatis.sqlmap.engine.type.TypeHandler;
+import com.ibatis.sqlmap.engine.type.TypeHandlerFactory;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.StringTokenizer;
+
+public class ResultMapConfig {
+ private SqlMapConfiguration config;
+ private ErrorContext errorContext;
+ private ExtendedSqlMapClient client;
+ private SqlMapExecutorDelegate delegate;
+ private TypeHandlerFactory typeHandlerFactory;
+ private BasicResultMap resultMap;
+ private List resultMappingList;
+ private int resultMappingIndex;
+ private Discriminator discriminator;
+
+ ResultMapConfig(SqlMapConfiguration config, String id, String resultClassName, String xmlName, String groupBy, String extended) {
+ this.config = config;
+ this.errorContext = config.getErrorContext();
+ this.client = config.getClient();
+ this.delegate = config.getDelegate();
+ this.typeHandlerFactory = config.getTypeHandlerFactory();
+ this.resultMap = new BasicResultMap(client.getDelegate());
+ this.resultMappingList = new ArrayList();
+ errorContext.setActivity("building a result map");
+ resultClassName = typeHandlerFactory.resolveAlias(resultClassName);
+ errorContext.setObjectId(id + " result map");
+ resultMap.setId(id);
+ resultMap.setXmlName(xmlName);
+ resultMap.setResource(errorContext.getResource());
+ if (groupBy != null && groupBy.length() > 0) {
+ StringTokenizer parser = new StringTokenizer(groupBy, ", ", false);
+ while (parser.hasMoreTokens()) {
+ resultMap.addGroupByProperty(parser.nextToken());
+ }
+ }
+ Class resultClass;
+ try {
+ errorContext.setMoreInfo("Check the result class.");
+ resultClass = Resources.classForName(resultClassName);
+ } catch (Exception e) {
+ throw new RuntimeException("Error configuring Result. Could not set ResultClass. Cause: " + e, e);
+ }
+ resultMap.setResultClass(resultClass);
+ errorContext.setMoreInfo("Check the extended result map.");
+ if (extended != null) {
+ BasicResultMap extendedResultMap = (BasicResultMap) client.getDelegate().getResultMap(extended);
+ ResultMapping[] resultMappings = extendedResultMap.getResultMappings();
+ for (int i = 0; i < resultMappings.length; i++) {
+ resultMappingList.add(resultMappings[i]);
+ }
+ List nestedResultMappings = extendedResultMap.getNestedResultMappings();
+ if (nestedResultMappings != null) {
+ Iterator iter = nestedResultMappings.iterator();
+ while (iter.hasNext()) {
+ resultMap.addNestedResultMappings((ResultMapping) iter.next());
+ }
+ }
+ if (groupBy == null || groupBy.length() == 0) {
+ if (extendedResultMap.hasGroupBy()) {
+ Iterator i = extendedResultMap.groupByProps();
+ while (i.hasNext()) {
+ resultMap.addGroupByProperty((String) i.next());
+ }
+ }
+ }
+ }
+ errorContext.setMoreInfo("Check the result mappings.");
+ resultMappingIndex = resultMappingList.size();
+ }
+
+ public void setDiscriminator(String callback, String javaType, String jdbcType, String columnName, String nullValue, String columnIndex) {
+ callback = typeHandlerFactory.resolveAlias(callback);
+ javaType = typeHandlerFactory.resolveAlias(javaType);
+ TypeHandler handler;
+ if (callback != null) {
+ errorContext.setMoreInfo("Check the result mapping typeHandler attribute '" + callback + "' (must be a TypeHandlerCallback implementation).");
+ try {
+ Object impl = Resources.instantiate(callback);
+ if (impl instanceof TypeHandlerCallback) {
+ handler = new CustomTypeHandler((TypeHandlerCallback) impl);
+ } else if (impl instanceof TypeHandler) {
+ handler = (TypeHandler) impl;
+ } else {
+ throw new RuntimeException("The class '' is not a valid implementation of TypeHandler or TypeHandlerCallback");
+ }
+ } catch (Exception e) {
+ throw new RuntimeException("Error occurred during custom type handler configuration. Cause: " + e, e);
+ }
+ } else {
+ errorContext.setMoreInfo("Check the result mapping property type or name.");
+ handler = config.resolveTypeHandler(client.getDelegate().getTypeHandlerFactory(), resultMap.getResultClass(), "", javaType, jdbcType, true);
+ }
+ BasicResultMapping mapping = new BasicResultMapping();
+ mapping.setColumnName(columnName);
+ mapping.setJdbcTypeName(jdbcType);
+ mapping.setTypeHandler(handler);
+ mapping.setNullValue(nullValue);
+ try {
+ if (javaType != null && javaType.length() > 0) {
+ mapping.setJavaType(Resources.classForName(javaType));
+ }
+ } catch (ClassNotFoundException e) {
+ throw new RuntimeException("Error setting javaType on result mapping. Cause: " + e);
+ }
+ if (columnIndex != null && columnIndex.length() > 0) {
+ mapping.setColumnIndex(Integer.parseInt(columnIndex));
+ }
+ discriminator = new Discriminator(delegate, mapping);
+ }
+
+ public void addSubMap(String value, String resultMap) {
+ if (discriminator == null) {
+ throw new RuntimeException("The discriminator is null, but somehow a subMap was reached. This is a bug.");
+ }
+ discriminator.addSubMap(value, resultMap);
+ }
+
+ public void addResultMapping(String callback, String javaType, String propertyName, String jdbcType, String columnName, String nullValue, String statementName, String resultMapName, String columnIndex) {
+ callback = typeHandlerFactory.resolveAlias(callback);
+ javaType = typeHandlerFactory.resolveAlias(javaType);
+ errorContext.setObjectId(propertyName + " mapping of the " + resultMap.getId() + " result map");
+ TypeHandler handler;
+ if (callback != null) {
+ errorContext.setMoreInfo("Check the result mapping typeHandler attribute '" + callback + "' (must be a TypeHandler or TypeHandlerCallback implementation).");
+ try {
+ Object impl = Resources.instantiate(callback);
+ if (impl instanceof TypeHandlerCallback) {
+ handler = new CustomTypeHandler((TypeHandlerCallback) impl);
+ } else if (impl instanceof TypeHandler) {
+ handler = (TypeHandler) impl;
+ } else {
+ throw new RuntimeException("The class '" + callback + "' is not a valid implementation of TypeHandler or TypeHandlerCallback");
+ }
+ } catch (Exception e) {
+ throw new RuntimeException("Error occurred during custom type handler configuration. Cause: " + e, e);
+ }
+ } else {
+ errorContext.setMoreInfo("Check the result mapping property type or name.");
+ handler = config.resolveTypeHandler(client.getDelegate().getTypeHandlerFactory(), resultMap.getResultClass(), propertyName, javaType, jdbcType, true);
+ }
+ BasicResultMapping mapping = new BasicResultMapping();
+ mapping.setPropertyName(propertyName);
+ mapping.setColumnName(columnName);
+ mapping.setJdbcTypeName(jdbcType);
+ mapping.setTypeHandler(handler);
+ mapping.setNullValue(nullValue);
+ mapping.setStatementName(statementName);
+ mapping.setNestedResultMapName(resultMapName);
+ if (resultMapName != null && resultMapName.length() > 0) {
+ resultMap.addNestedResultMappings(mapping);
+ }
+ try {
+ if (javaType != null && javaType.length() > 0) {
+ mapping.setJavaType(Resources.classForName(javaType));
+ }
+ } catch (ClassNotFoundException e) {
+ throw new RuntimeException("Error setting javaType on result mapping. Cause: " + e);
+ }
+ if (columnIndex != null && columnIndex.length() > 0) {
+ mapping.setColumnIndex(Integer.parseInt(columnIndex));
+ } else {
+ resultMappingIndex++;
+ mapping.setColumnIndex(resultMappingIndex);
+ }
+ resultMappingList.add(mapping);
+ }
+
+ public void saveResultMap() {
+ if (resultMappingList.size() == 0) {
+ throw new RuntimeException("resultMap " + resultMap.getId() + " must have at least one result mapping");
+ }
+ resultMap.setResultMappingList(resultMappingList);
+ resultMap.setDiscriminator(discriminator);
+ discriminator = null;
+ client.getDelegate().addResultMap(resultMap);
+ errorContext.setMoreInfo(null);
+ errorContext.setObjectId(null);
+ }
+
+}
Modified: ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlMapConfiguration.java
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlMapConfiguration.java?view=diff&rev=514030&r1=514029&r2=514030
==============================================================================
--- ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlMapConfiguration.java (original)
+++ ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlMapConfiguration.java Fri Mar 2 16:33:48 2007
@@ -1,6 +1,8 @@
package com.ibatis.sqlmap.engine.conifg;
-import com.ibatis.common.beans.*;
+import com.ibatis.common.beans.ClassInfo;
+import com.ibatis.common.beans.Probe;
+import com.ibatis.common.beans.ProbeFactory;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapException;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;
@@ -9,68 +11,51 @@
import com.ibatis.sqlmap.engine.cache.fifo.FifoCacheController;
import com.ibatis.sqlmap.engine.cache.lru.LruCacheController;
import com.ibatis.sqlmap.engine.cache.memory.MemoryCacheController;
-import com.ibatis.sqlmap.engine.datasource.*;
-import com.ibatis.sqlmap.engine.impl.*;
-import com.ibatis.sqlmap.engine.mapping.parameter.*;
-import com.ibatis.sqlmap.engine.mapping.result.*;
-import com.ibatis.sqlmap.engine.mapping.statement.*;
-import com.ibatis.sqlmap.engine.mapping.sql.*;
-import com.ibatis.sqlmap.engine.mapping.sql.raw.RawSql;
-import com.ibatis.sqlmap.engine.mapping.sql.dynamic.DynamicSql;
-import com.ibatis.sqlmap.engine.mapping.sql.stat.StaticSql;
-import com.ibatis.sqlmap.engine.mapping.sql.simple.SimpleDynamicSql;
+import com.ibatis.sqlmap.engine.datasource.DataSourceFactory;
+import com.ibatis.sqlmap.engine.datasource.DbcpDataSourceFactory;
+import com.ibatis.sqlmap.engine.datasource.JndiDataSourceFactory;
+import com.ibatis.sqlmap.engine.datasource.SimpleDataSourceFactory;
+import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
+import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl;
+import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
+import com.ibatis.sqlmap.engine.mapping.result.Discriminator;
+import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
+import com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactory;
+import com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement;
+import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
import com.ibatis.sqlmap.engine.scope.ErrorContext;
-import com.ibatis.sqlmap.engine.transaction.*;
+import com.ibatis.sqlmap.engine.transaction.TransactionConfig;
+import com.ibatis.sqlmap.engine.transaction.TransactionManager;
import com.ibatis.sqlmap.engine.transaction.external.ExternalTransactionConfig;
import com.ibatis.sqlmap.engine.transaction.jdbc.JdbcTransactionConfig;
import com.ibatis.sqlmap.engine.transaction.jta.JtaTransactionConfig;
import com.ibatis.sqlmap.engine.type.*;
import javax.sql.DataSource;
-import java.util.*;
-import java.sql.ResultSet;
+import java.util.Iterator;
+import java.util.Properties;
-/**
- * A work in progress...it works, but there are many refactorings
- * still required to make it a usable API. Most notably, the public
- * fields and the insane method signatures...all holdovers from the
- * old XML exclusive configuration.
- */
public class SqlMapConfiguration {
-
private static final Probe PROBE = ProbeFactory.getProbe();
- private static final InlineParameterMapParser PARAM_PARSER = new InlineParameterMapParser();
-
- private ErrorContext errorContext = new ErrorContext();
-
- private ExtendedSqlMapClient client;
+ private ErrorContext errorContext;
private SqlMapExecutorDelegate delegate;
private TypeHandlerFactory typeHandlerFactory;
-
+ private ExtendedSqlMapClient client;
private Integer defaultStatementTimeout;
private DataSource dataSource;
- // TODO: Move to ResultMapConfig
- private BasicResultMap resultMap;
- private List resultMappingList;
- private int resultMappingIndex;
- private Discriminator discriminator;
-
- // TODO: Move to ParameterMapConfig
- private BasicParameterMap parameterMap;
- private List parameterMappingList;
-
- // TODO: Move to CacheModelConfig
- public CacheModel cacheModel;
- public Properties cacheProps;
-
public SqlMapConfiguration() {
+ errorContext = new ErrorContext();
delegate = new SqlMapExecutorDelegate();
typeHandlerFactory = delegate.getTypeHandlerFactory();
client = new SqlMapClientImpl(delegate);
registerDefaultTypeAliases();
}
+ public TypeHandlerFactory getTypeHandlerFactory() {
+ return typeHandlerFactory;
+ }
+
public ErrorContext getErrorContext() {
return errorContext;
}
@@ -83,6 +68,9 @@
return delegate;
}
+ public Integer getDefaultStatementTimeout() {
+ return defaultStatementTimeout;
+ }
//
// Utility Methods
@@ -137,37 +125,6 @@
return handler;
}
-
- private void registerDefaultTypeAliases() {
- // TRANSACTION ALIASES
- typeHandlerFactory.putTypeAlias("JDBC", JdbcTransactionConfig.class.getName());
- typeHandlerFactory.putTypeAlias("JTA", JtaTransactionConfig.class.getName());
- typeHandlerFactory.putTypeAlias("EXTERNAL", ExternalTransactionConfig.class.getName());
-
- // DATA SOURCE ALIASES
- typeHandlerFactory.putTypeAlias("SIMPLE", SimpleDataSourceFactory.class.getName());
- typeHandlerFactory.putTypeAlias("DBCP", DbcpDataSourceFactory.class.getName());
- typeHandlerFactory.putTypeAlias("JNDI", JndiDataSourceFactory.class.getName());
-
- // CACHE ALIASES
- typeHandlerFactory.putTypeAlias("FIFO", FifoCacheController.class.getName());
- typeHandlerFactory.putTypeAlias("LRU", LruCacheController.class.getName());
- typeHandlerFactory.putTypeAlias("MEMORY", MemoryCacheController.class.getName());
- // use a string for OSCache to avoid unnecessary loading of properties upon init
- typeHandlerFactory.putTypeAlias("OSCACHE", "com.ibatis.sqlmap.engine.cache.oscache.OSCacheController");
-
- // TYPE ALIASEs
- typeHandlerFactory.putTypeAlias("dom", DomTypeMarker.class.getName());
- typeHandlerFactory.putTypeAlias("domCollection", DomCollectionTypeMarker.class.getName());
- typeHandlerFactory.putTypeAlias("xml", XmlTypeMarker.class.getName());
- typeHandlerFactory.putTypeAlias("xmlCollection", XmlCollectionTypeMarker.class.getName());
- }
-
- //
- // SQL Map Config methods
- //
-
-
// TODO: Split into separate methods
public void setSettings(boolean classInfoCacheEnabled, boolean lazyLoadingEnabled, boolean statementCachingEnabled, boolean cacheModelsEnabled, boolean enhancementEnabled, Integer maxTransactions, Integer maxRequests, Integer maxSessions, Integer defaultTimeout) {
errorContext.setActivity("loading settings properties");
@@ -181,21 +138,16 @@
enhancementEnabled = false;
}
client.getDelegate().setEnhancementEnabled(enhancementEnabled);
-
if (maxTransactions != null && maxTransactions.intValue() > 0) {
client.getDelegate().setMaxTransactions(maxTransactions.intValue());
}
-
if (maxRequests != null && maxRequests.intValue() > 0) {
client.getDelegate().setMaxRequests(maxRequests.intValue());
}
-
if (maxSessions != null && maxSessions.intValue() > 0) {
client.getDelegate().setMaxSessions(maxSessions.intValue());
}
-
AccessPlanFactory.setBytecodeEnhancementEnabled(client.getDelegate().isEnhancementEnabled());
-
if (defaultTimeout != null) {
try {
defaultStatementTimeout = defaultTimeout;
@@ -213,12 +165,9 @@
try {
errorContext.setActivity("building a building custom type handler");
TypeHandlerFactory typeHandlerFactory = client.getDelegate().getTypeHandlerFactory();
-
callback = typeHandlerFactory.resolveAlias(callback);
javaType = typeHandlerFactory.resolveAlias(javaType);
-
errorContext.setMoreInfo("Check the callback attribute '" + callback + "' (must be a classname).");
-
TypeHandler typeHandler;
Object impl = Resources.instantiate(callback);
if (impl instanceof TypeHandlerCallback) {
@@ -228,7 +177,6 @@
} else {
throw new RuntimeException("The class '' is not a valid implementation of TypeHandler or TypeHandlerCallback");
}
-
errorContext.setMoreInfo("Check the javaType attribute '" + javaType + "' (must be a classname) or the jdbcType '" + jdbcType + "' (must be a JDBC type name).");
if (jdbcType != null && jdbcType.length() > 0) {
typeHandlerFactory.register(Resources.classForName(javaType), jdbcType, typeHandler);
@@ -299,7 +247,6 @@
// TODO - post processing sql map config
public void wireupCacheModels() {
Iterator cacheNames = client.getDelegate().getCacheModelNames();
-
while (cacheNames.hasNext()) {
String cacheName = (String) cacheNames.next();
CacheModel cacheModel = client.getDelegate().getCacheModel(cacheName);
@@ -316,12 +263,7 @@
}
}
- //
- // SQL Map methods
- //
-
// TODO: post processing sql map
-
public void bindDelegateSubMaps() {
Iterator names = delegate.getResultMapNames();
while (names.hasNext()) {
@@ -334,576 +276,45 @@
}
}
- // TODO: pass into addCacheModel as parameter
- public void setFlushInterval(int hours, int minutes, int seconds, int milliseconds) {
- errorContext.setMoreInfo("Check the cache model flush interval.");
- long t = 0;
- t += milliseconds;
- t += seconds * 1000;
- t += minutes * 60 * 1000;
- t += hours * 60 * 60 * 1000;
- if (t < 1)
- throw new RuntimeException("A flush interval must specify one or more of milliseconds, seconds, minutes or hours.");
- cacheModel.setFlushInterval(t);
- }
-
- // TODO: remove in favour of working directly with cacheModel if possible
- public void addFlushTriggerStatement(String statement) {
- errorContext.setMoreInfo("Check the cache model flush on statement elements.");
- cacheModel.addFlushTriggerStatement(statement);
- }
-
- public void addCacheModel(String id, String type, Boolean readOnly, Boolean serialize, Properties props) {
- errorContext.setActivity("building a cache model");
- type = typeHandlerFactory.resolveAlias(type);
- if (readOnly != null) {
- cacheModel.setReadOnly(readOnly.booleanValue());
- } else {
- cacheModel.setReadOnly(true);
- }
- if (serialize != null) {
- cacheModel.setSerialize(serialize.booleanValue());
- } else {
- cacheModel.setSerialize(false);
- }
-
- errorContext.setObjectId(id + " cache model");
-
- errorContext.setMoreInfo("Check the cache model type.");
- cacheModel.setId(id);
- cacheModel.setResource(errorContext.getResource());
-
- try {
- cacheModel.setControllerClassName(type);
- } catch (Exception e) {
- throw new RuntimeException("Error setting Cache Controller Class. Cause: " + e, e);
- }
-
- errorContext.setMoreInfo("Check the cache model configuration.");
- cacheModel.configure(props);
-
- if (client.getDelegate().isCacheModelsEnabled()) {
- client.getDelegate().addCacheModel(cacheModel);
- }
-
- errorContext.setMoreInfo(null);
- errorContext.setObjectId(null);
- cacheProps = null;
- cacheModel = null;
- }
-
- // TODO: merge with addPArameterMap
- public void finalizeParameterMap() {
- parameterMap.setParameterMappingList(parameterMappingList);
-
- client.getDelegate().addParameterMap(parameterMap);
-
- errorContext.setMoreInfo(null);
- errorContext.setObjectId(null);
+ public ParameterMapConfig newParameterMapConfig(String id, String parameterClassName) {
+ return new ParameterMapConfig(this, id, parameterClassName);
}
- // TODO: pass list into addParameterMap
- public void addParameterMapping(String callback, String javaType, String resultMap, String propertyName, String jdbcType, String type, String nullValue, String mode, String numericScale) {
- callback = typeHandlerFactory.resolveAlias(callback);
- javaType = typeHandlerFactory.resolveAlias(javaType);
-
- errorContext.setObjectId(propertyName + " mapping of the " + parameterMap.getId() + " parameter map");
-
- TypeHandler handler;
- if (callback != null) {
- errorContext.setMoreInfo("Check the parameter mapping typeHandler attribute '" + callback + "' (must be a TypeHandler or TypeHandlerCallback implementation).");
- try {
- Object impl = Resources.instantiate(callback);
- if (impl instanceof TypeHandlerCallback) {
- handler = new CustomTypeHandler((TypeHandlerCallback) impl);
- } else if (impl instanceof TypeHandler) {
- handler = (TypeHandler) impl;
- } else {
- throw new RuntimeException("The class '" + callback + "' is not a valid implementation of TypeHandler or TypeHandlerCallback");
- }
- } catch (Exception e) {
- throw new RuntimeException("Error occurred during custom type handler configuration. Cause: " + e, e);
- }
- } else {
- errorContext.setMoreInfo("Check the parameter mapping property type or name.");
- handler = resolveTypeHandler(client.getDelegate().getTypeHandlerFactory(), parameterMap.getParameterClass(), propertyName, javaType, jdbcType);
- }
-
- BasicParameterMapping mapping = new BasicParameterMapping();
- mapping.setPropertyName(propertyName);
- mapping.setJdbcTypeName(jdbcType);
- mapping.setTypeName(type);
- mapping.setResultMapName(resultMap);
- mapping.setNullValue(nullValue);
- if (mode != null && mode.length() > 0) {
- mapping.setMode(mode);
- }
- mapping.setTypeHandler(handler);
- try {
- if (javaType != null && javaType.length() > 0) {
- mapping.setJavaType(Resources.classForName(javaType));
- }
- } catch (ClassNotFoundException e) {
- throw new RuntimeException("Error setting javaType on parameter mapping. Cause: " + e);
- }
-
- if (numericScale != null) {
- try {
- Integer scale = Integer.valueOf(numericScale);
- if (scale.intValue() < 0) {
- throw new RuntimeException("Error setting numericScale on parameter mapping. Cause: scale must be greater than or equal to zero");
- }
-
- mapping.setNumericScale(scale);
- } catch (NumberFormatException e) {
- throw new RuntimeException("Error setting numericScale on parameter mapping. Cause: " + numericScale + " is not a valid integer");
- }
- }
-
- parameterMappingList.add(mapping);
- }
-
- public void addParameterMap(String id, String parameterClassName) {
- errorContext.setActivity("building a parameter map");
- parameterMap = new BasicParameterMap(client.getDelegate());
-
- parameterClassName = typeHandlerFactory.resolveAlias(parameterClassName);
-
- parameterMap.setId(id);
- parameterMap.setResource(errorContext.getResource());
-
- errorContext.setObjectId(id + " parameter map");
-
- Class parameterClass;
- try {
- errorContext.setMoreInfo("Check the parameter class.");
- parameterClass = Resources.classForName(parameterClassName);
- } catch (Exception e) {
- throw new SqlMapException("Error configuring ParameterMap. Could not set ParameterClass. Cause: " + e, e);
- }
-
- parameterMap.setParameterClass(parameterClass);
-
- parameterMappingList = new ArrayList();
-
- errorContext.setMoreInfo("Check the parameter mappings.");
+ public ResultMapConfig newResultMapConfig(String id, String resultClassName, String xmlName, String groupBy, String extended) {
+ return new ResultMapConfig(this, id, resultClassName, xmlName, groupBy, extended);
}
- // TODO: pass into addResultMap
- public void addDiscriminator(String callback, String javaType, String jdbcType, String columnName, String nullValue, String columnIndex) {
- callback = typeHandlerFactory.resolveAlias(callback);
- javaType = typeHandlerFactory.resolveAlias(javaType);
-
- TypeHandler handler;
- if (callback != null) {
- errorContext.setMoreInfo("Check the result mapping typeHandler attribute '" + callback + "' (must be a TypeHandlerCallback implementation).");
- try {
- Object impl = Resources.instantiate(callback);
- if (impl instanceof TypeHandlerCallback) {
- handler = new CustomTypeHandler((TypeHandlerCallback) impl);
- } else if (impl instanceof TypeHandler) {
- handler = (TypeHandler) impl;
- } else {
- throw new RuntimeException("The class '' is not a valid implementation of TypeHandler or TypeHandlerCallback");
- }
- } catch (Exception e) {
- throw new RuntimeException("Error occurred during custom type handler configuration. Cause: " + e, e);
- }
- } else {
- errorContext.setMoreInfo("Check the result mapping property type or name.");
- handler = resolveTypeHandler(client.getDelegate().getTypeHandlerFactory(), resultMap.getResultClass(), "", javaType, jdbcType, true);
- }
-
- BasicResultMapping mapping = new BasicResultMapping();
- mapping.setColumnName(columnName);
- mapping.setJdbcTypeName(jdbcType);
- mapping.setTypeHandler(handler);
- mapping.setNullValue(nullValue);
-
- try {
- if (javaType != null && javaType.length() > 0) {
- mapping.setJavaType(Resources.classForName(javaType));
- }
- } catch (ClassNotFoundException e) {
- throw new RuntimeException("Error setting javaType on result mapping. Cause: " + e);
- }
-
- if (columnIndex != null && columnIndex.length() > 0) {
- mapping.setColumnIndex(Integer.parseInt(columnIndex));
- }
-
- discriminator = new Discriminator(delegate, mapping);
+ public CacheModelConfig newCacheModelConfig(String id, String type, Boolean readOnly, Boolean serialize) {
+ return new CacheModelConfig(this, id, type, readOnly, serialize);
}
- // TODO: pass into addResultMap
- public void addSubMap(String value, String resultMap) {
- if (discriminator == null) {
- throw new RuntimeException("The discriminator is null, but somehow a subMap was reached. This is a bug.");
- }
- discriminator.addSubMap(value, resultMap);
+ public MappedStatementConfig newMappedStatementConfig(SqlSource processor, GeneralStatement statement, String id, String resultMapName, String[] additionalResultMapNames, String parameterMapName, String resultSetType, String fetchSize, String parameterClassName, String resultClassName, String[] additionalResultClasses, String allowRemapping, String xmlResultName, String timeout, String cacheModelName) {
+ return new MappedStatementConfig(this, processor, statement, id, resultMapName, additionalResultMapNames, parameterMapName, resultSetType, fetchSize, parameterClassName, resultClassName, additionalResultClasses, allowRemapping, xmlResultName, timeout, cacheModelName);
}
- // TODO: pass into addResultMap
- public void addResultMapping(String callback, String javaType, String propertyName, String jdbcType, String columnName, String nullValue, String statementName, String resultMapName, String columnIndex) {
- callback = typeHandlerFactory.resolveAlias(callback);
- javaType = typeHandlerFactory.resolveAlias(javaType);
-
- errorContext.setObjectId(propertyName + " mapping of the " + resultMap.getId() + " result map");
-
- TypeHandler handler;
- if (callback != null) {
- errorContext.setMoreInfo("Check the result mapping typeHandler attribute '" + callback + "' (must be a TypeHandler or TypeHandlerCallback implementation).");
- try {
- Object impl = Resources.instantiate(callback);
- if (impl instanceof TypeHandlerCallback) {
- handler = new CustomTypeHandler((TypeHandlerCallback) impl);
- } else if (impl instanceof TypeHandler) {
- handler = (TypeHandler) impl;
- } else {
- throw new RuntimeException("The class '" + callback + "' is not a valid implementation of TypeHandler or TypeHandlerCallback");
- }
- } catch (Exception e) {
- throw new RuntimeException("Error occurred during custom type handler configuration. Cause: " + e, e);
- }
- } else {
- errorContext.setMoreInfo("Check the result mapping property type or name.");
- handler = resolveTypeHandler(client.getDelegate().getTypeHandlerFactory(), resultMap.getResultClass(), propertyName, javaType, jdbcType, true);
- }
-
-
- BasicResultMapping mapping = new BasicResultMapping();
- mapping.setPropertyName(propertyName);
- mapping.setColumnName(columnName);
- mapping.setJdbcTypeName(jdbcType);
- mapping.setTypeHandler(handler);
- mapping.setNullValue(nullValue);
- mapping.setStatementName(statementName);
- mapping.setNestedResultMapName(resultMapName);
-
- if (resultMapName != null && resultMapName.length() > 0) {
- resultMap.addNestedResultMappings(mapping);
- }
-
- try {
- if (javaType != null && javaType.length() > 0) {
- mapping.setJavaType(Resources.classForName(javaType));
- }
- } catch (ClassNotFoundException e) {
- throw new RuntimeException("Error setting javaType on result mapping. Cause: " + e);
- }
-
- if (columnIndex != null && columnIndex.length() > 0) {
- mapping.setColumnIndex(Integer.parseInt(columnIndex));
- } else {
- resultMappingIndex++;
- mapping.setColumnIndex(resultMappingIndex);
- }
-
- resultMappingList.add(mapping);
- }
-
- public void addResultMap(String id, String resultClassName, String xmlName, String groupBy, String extended) {
- errorContext.setActivity("building a result map");
-
- resultMap = new BasicResultMap(client.getDelegate());
-
- resultClassName = typeHandlerFactory.resolveAlias(resultClassName);
-
- errorContext.setObjectId(id + " result map");
-
- resultMap.setId(id);
- resultMap.setXmlName(xmlName);
- resultMap.setResource(errorContext.getResource());
-
- if (groupBy != null && groupBy.length() > 0) {
- StringTokenizer parser = new StringTokenizer(groupBy, ", ", false);
- while (parser.hasMoreTokens()) {
- resultMap.addGroupByProperty(parser.nextToken());
- }
- }
-
- Class resultClass;
- try {
- errorContext.setMoreInfo("Check the result class.");
- resultClass = Resources.classForName(resultClassName);
- } catch (Exception e) {
- throw new RuntimeException("Error configuring Result. Could not set ResultClass. Cause: " + e, e);
-
- }
-
- resultMap.setResultClass(resultClass);
-
- resultMappingList = new ArrayList();
-
- errorContext.setMoreInfo("Check the extended result map.");
- if (extended != null) {
- BasicResultMap extendedResultMap = (BasicResultMap) client.getDelegate().getResultMap(extended);
- ResultMapping[] resultMappings = extendedResultMap.getResultMappings();
- for (int i = 0; i < resultMappings.length; i++) {
- resultMappingList.add(resultMappings[i]);
- }
-
- List nestedResultMappings = extendedResultMap.getNestedResultMappings();
- if (nestedResultMappings != null) {
- Iterator iter = nestedResultMappings.iterator();
- while (iter.hasNext()) {
- resultMap.addNestedResultMappings((ResultMapping) iter.next());
- }
- }
-
- if (groupBy == null || groupBy.length() == 0) {
- if (extendedResultMap.hasGroupBy()) {
- Iterator i = extendedResultMap.groupByProps();
- while (i.hasNext()) {
- resultMap.addGroupByProperty((String) i.next());
- }
- }
- }
- }
-
- errorContext.setMoreInfo("Check the result mappings.");
- resultMappingIndex = resultMappingList.size();
- }
-
- // TODO: pass into addResultMap
- public void finalizeResultMap() {
- if (resultMappingList.size() == 0) {
- throw new RuntimeException("resultMap " + resultMap.getId() + " must have at least one result mapping");
- }
-
- resultMap.setResultMappingList(resultMappingList);
-
- resultMap.setDiscriminator(discriminator);
- discriminator = null;
-
- client.getDelegate().addResultMap(resultMap);
-
- errorContext.setMoreInfo(null);
-
- errorContext.setObjectId(null);
- }
-
- //
- // SQL Statement methods
- //
-
- // TODO: Clean up method signature
- public MappedStatement prepareGeneralStatement(SqlSource processor, GeneralStatement statement, String id, String resultMapName, String[] additionalResultMapNames, String parameterMapName, String resultSetType, String fetchSize, String parameterClassName, String resultClassName, String[] additionalResultClasses, String allowRemapping, String xmlResultName, String timeout, String cacheModelName) {
- errorContext.setActivity("parsing a mapped statement");
- errorContext.setObjectId(id + " statement");
- errorContext.setMoreInfo("Check the result map name.");
- if (resultMapName != null) {
- statement.setResultMap((BasicResultMap) client.getDelegate().getResultMap(resultMapName));
- if (additionalResultMapNames != null) {
- for (int i = 0; i < additionalResultMapNames.length; i++) {
- statement.addResultMap((BasicResultMap) client.getDelegate().getResultMap(additionalResultMapNames[i]));
- }
- }
- }
-
- errorContext.setMoreInfo("Check the parameter map name.");
-
- if (parameterMapName != null) {
- statement.setParameterMap((BasicParameterMap) client.getDelegate().getParameterMap(parameterMapName));
- }
-
- statement.setId(id);
- statement.setResource(errorContext.getResource());
-
- if (resultSetType != null) {
- if ("FORWARD_ONLY".equals(resultSetType)) {
- statement.setResultSetType(new Integer(ResultSet.TYPE_FORWARD_ONLY));
- } else if ("SCROLL_INSENSITIVE".equals(resultSetType)) {
- statement.setResultSetType(new Integer(ResultSet.TYPE_SCROLL_INSENSITIVE));
- } else if ("SCROLL_SENSITIVE".equals(resultSetType)) {
- statement.setResultSetType(new Integer(ResultSet.TYPE_SCROLL_SENSITIVE));
- }
- }
-
- if (fetchSize != null) {
- statement.setFetchSize(new Integer(fetchSize));
- }
-
- // set parameter class either from attribute or from map (make sure to match)
- ParameterMap parameterMap = statement.getParameterMap();
- if (parameterMap == null) {
- try {
- if (parameterClassName != null) {
- errorContext.setMoreInfo("Check the parameter class.");
- parameterClassName = typeHandlerFactory.resolveAlias(parameterClassName);
- Class parameterClass = Resources.classForName(parameterClassName);
- statement.setParameterClass(parameterClass);
- }
- } catch (ClassNotFoundException e) {
- throw new SqlMapException("Error. Could not set parameter class. Cause: " + e, e);
- }
- } else {
- statement.setParameterClass(parameterMap.getParameterClass());
- }
-
- // process SQL statement, including inline parameter maps
- errorContext.setMoreInfo("Check the SQL statement.");
- Sql sql = processor.getSql();
- setSqlForStatement(statement, sql);
-
- // set up either null result map or automatic result mapping
- BasicResultMap resultMap = (BasicResultMap) statement.getResultMap();
- if (resultMap == null && resultClassName == null) {
- statement.setResultMap(null);
- } else if (resultMap == null) {
- resultMap = buildAutoResultMap(allowRemapping, statement, resultClassName, xmlResultName);
- statement.setResultMap(resultMap);
- if (additionalResultClasses != null) {
- for (int i = 0; i < additionalResultClasses.length; i++) {
- statement.addResultMap(buildAutoResultMap(allowRemapping, statement, additionalResultClasses[i], xmlResultName));
- }
- }
-
- }
-
- statement.setTimeout(defaultStatementTimeout);
- if (timeout != null) {
- try {
- statement.setTimeout(Integer.valueOf(timeout));
- } catch (NumberFormatException e) {
- throw new SqlMapException("Specified timeout value for statement "
- + statement.getId() + " is not a valid integer");
- }
- }
-
- errorContext.setMoreInfo(null);
- errorContext.setObjectId(null);
-
- statement.setSqlMapClient(client);
- if (cacheModelName != null && cacheModelName.length() > 0 && client.getDelegate().isCacheModelsEnabled()) {
- CacheModel cacheModel = client.getDelegate().getCacheModel(cacheModelName);
- return new CachingStatement(statement, cacheModel);
- } else {
- return statement;
- }
- }
-
- // TODO: Clean up method signature!
- public SelectKeyStatement prepareSelectKeyStatement(SqlSource processor, String resultClassName, String statementId, String keyPropName, boolean runAfterSQL, String type, Class parameterClass) {
- errorContext.setActivity("parsing a select key");
-
- SelectKeyStatement selectKeyStatement = new SelectKeyStatement();
-
- resultClassName = typeHandlerFactory.resolveAlias(resultClassName);
- Class resultClass = null;
-
- // get parameter and result maps
- selectKeyStatement.setSqlMapClient(client);
-
- selectKeyStatement.setId(statementId + "-SelectKey");
- selectKeyStatement.setResource(errorContext.getResource());
- selectKeyStatement.setKeyProperty(keyPropName);
-
- selectKeyStatement.setRunAfterSQL(runAfterSQL);
- // process the type (pre or post) attribute
- if (type != null) {
- selectKeyStatement.setRunAfterSQL("post".equals(type));
- }
-
- try {
- if (resultClassName != null) {
- errorContext.setMoreInfo("Check the select key result class.");
- resultClass = Resources.classForName(resultClassName);
- } else {
- if (keyPropName != null && parameterClass != null) {
- resultClass = PROBE.getPropertyTypeForSetter(parameterClass, selectKeyStatement.getKeyProperty());
- }
- }
- } catch (ClassNotFoundException e) {
- throw new SqlMapException("Error. Could not set result class. Cause: " + e, e);
- }
-
- if (resultClass == null) {
- resultClass = Object.class;
- }
-
- // process SQL statement, including inline parameter maps
- errorContext.setMoreInfo("Check the select key SQL statement.");
- Sql sql = processor.getSql();
- setSqlForStatement(selectKeyStatement, sql);
-
-
- BasicResultMap resultMap;
- resultMap = new AutoResultMap(client.getDelegate(), false);
- resultMap.setId(selectKeyStatement.getId() + "-AutoResultMap");
- resultMap.setResultClass(resultClass);
- resultMap.setResource(selectKeyStatement.getResource());
- selectKeyStatement.setResultMap(resultMap);
-
- errorContext.setMoreInfo(null);
- return selectKeyStatement;
- }
-
- private void setSqlForStatement(GeneralStatement statement, Sql sql) {
- if (sql instanceof DynamicSql) {
- statement.setSql(sql);
- } else {
- applyInlineParameterMap(statement, sql.getSql(null, null));
- }
- }
-
- private void applyInlineParameterMap(GeneralStatement statement, String sqlStatement) {
- String newSql = sqlStatement;
-
- errorContext.setActivity("building an inline parameter map");
-
- ParameterMap parameterMap = statement.getParameterMap();
-
- errorContext.setMoreInfo("Check the inline parameters.");
- if (parameterMap == null) {
-
- BasicParameterMap map;
- map = new BasicParameterMap(client.getDelegate());
-
- map.setId(statement.getId() + "-InlineParameterMap");
- map.setParameterClass(statement.getParameterClass());
- map.setResource(statement.getResource());
- statement.setParameterMap(map);
-
- SqlText sqlText = PARAM_PARSER.parseInlineParameterMap(client.getDelegate().getTypeHandlerFactory(), newSql, statement.getParameterClass());
- newSql = sqlText.getText();
- List mappingList = Arrays.asList(sqlText.getParameterMappings());
-
- map.setParameterMappingList(mappingList);
- }
-
- Sql sql;
- if (SimpleDynamicSql.isSimpleDynamicSql(newSql)) {
- sql = new SimpleDynamicSql(client.getDelegate(), newSql);
- } else {
- sql = new StaticSql(newSql);
- }
- statement.setSql(sql);
+ private void registerDefaultTypeAliases() {
+ // TRANSACTION ALIASES
+ typeHandlerFactory.putTypeAlias("JDBC", JdbcTransactionConfig.class.getName());
+ typeHandlerFactory.putTypeAlias("JTA", JtaTransactionConfig.class.getName());
+ typeHandlerFactory.putTypeAlias("EXTERNAL", ExternalTransactionConfig.class.getName());
- }
+ // DATA SOURCE ALIASES
+ typeHandlerFactory.putTypeAlias("SIMPLE", SimpleDataSourceFactory.class.getName());
+ typeHandlerFactory.putTypeAlias("DBCP", DbcpDataSourceFactory.class.getName());
+ typeHandlerFactory.putTypeAlias("JNDI", JndiDataSourceFactory.class.getName());
- private BasicResultMap buildAutoResultMap(String allowRemapping, GeneralStatement statement, String firstResultClass, String xmlResultName) {
- BasicResultMap resultMap;
- resultMap = new AutoResultMap(client.getDelegate(), "true".equals(allowRemapping));
- resultMap.setId(statement.getId() + "-AutoResultMap");
- resultMap.setResultClass(resolveClass(firstResultClass));
- resultMap.setXmlName(xmlResultName);
- resultMap.setResource(statement.getResource());
- return resultMap;
- }
+ // CACHE ALIASES
+ typeHandlerFactory.putTypeAlias("FIFO", FifoCacheController.class.getName());
+ typeHandlerFactory.putTypeAlias("LRU", LruCacheController.class.getName());
+ typeHandlerFactory.putTypeAlias("MEMORY", MemoryCacheController.class.getName());
+ // use a string for OSCache to avoid unnecessary loading of properties upon init
+ typeHandlerFactory.putTypeAlias("OSCACHE", "com.ibatis.sqlmap.engine.cache.oscache.OSCacheController");
- private Class resolveClass(String resultClassName) {
- try {
- if (resultClassName != null) {
- errorContext.setMoreInfo("Check the result class.");
- return Resources.classForName(typeHandlerFactory.resolveAlias(resultClassName));
- } else {
- return null;
- }
- } catch (ClassNotFoundException e) {
- throw new SqlMapException("Error. Could not set result class. Cause: " + e, e);
- }
+ // TYPE ALIASEs
+ typeHandlerFactory.putTypeAlias("dom", DomTypeMarker.class.getName());
+ typeHandlerFactory.putTypeAlias("domCollection", DomCollectionTypeMarker.class.getName());
+ typeHandlerFactory.putTypeAlias("xml", XmlTypeMarker.class.getName());
+ typeHandlerFactory.putTypeAlias("xmlCollection", XmlCollectionTypeMarker.class.getName());
}
-
}
Modified: ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlSource.java
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlSource.java?view=diff&rev=514030&r1=514029&r2=514030
==============================================================================
--- ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlSource.java (original)
+++ ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlSource.java Fri Mar 2 16:33:48 2007
@@ -3,7 +3,6 @@
import com.ibatis.sqlmap.engine.mapping.sql.Sql;
public interface SqlSource {
-
Sql getSql();
}