📄 basicresultmap.java
字号:
/*
* Copyright 2004 Clinton Begin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibatis.sqlmap.engine.mapping.result;
import com.ibatis.common.beans.Probe;
import com.ibatis.common.beans.ProbeFactory;
import com.ibatis.common.jdbc.exception.NestedSQLException;
import com.ibatis.sqlmap.client.SqlMapException;
import com.ibatis.sqlmap.engine.exchange.DataExchange;
import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
import com.ibatis.sqlmap.engine.mapping.result.loader.ResultLoader;
import com.ibatis.sqlmap.engine.mapping.sql.Sql;
import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
import com.ibatis.sqlmap.engine.scope.ErrorContext;
import com.ibatis.sqlmap.engine.scope.RequestScope;
import com.ibatis.sqlmap.engine.type.DomCollectionTypeMarker;
import com.ibatis.sqlmap.engine.type.DomTypeMarker;
import com.ibatis.sqlmap.engine.type.TypeHandler;
import com.ibatis.sqlmap.engine.type.TypeHandlerFactory;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
/**
* Basic implementation of ResultMap interface
*/
public class BasicResultMap implements ResultMap {
private static final Probe PROBE = ProbeFactory.getProbe();
private static final String KEY_SEPARATOR = "\002";
private String id;
private Class resultClass;
// DO NOT ACCESS EITHER OF THESE OUTSIDE OF THEIR BEAN GETTER/SETTER
private ResultMapping[] resultMappings;
private ThreadLocal remappableResultMappings = new ThreadLocal();
private DataExchange dataExchange;
private List nestedResultMappings;
private Discriminator discriminator;
private Set groupByProps;
private String xmlName;
private String resource;
private SqlMapExecutorDelegate delegate;
protected boolean allowRemapping = false;
/**
* Constructor to pass a SqlMapExecutorDelegate in
*
* @param delegate - the SqlMapExecutorDelegate
*/
public BasicResultMap(SqlMapExecutorDelegate delegate) {
this.delegate = delegate;
}
/**
* Getter for the SqlMapExecutorDelegate
*
* @return - the delegate
*/
public SqlMapExecutorDelegate getDelegate() {
return delegate;
}
public String getId() {
return id;
}
/**
* Setter for the ID
*
* @param id - the new ID
*/
public void setId(String id) {
this.id = id;
}
public Class getResultClass() {
return resultClass;
}
public Object getUniqueKey(String keyPrefix, Object[] values) {
if (groupByProps != null) {
StringBuffer keyBuffer;
if ( keyPrefix != null ) keyBuffer = new StringBuffer(keyPrefix);
else keyBuffer = new StringBuffer();
for (int i = 0; i < getResultMappings().length; i++) {
String propertyName = getResultMappings()[i].getPropertyName();
if (groupByProps.contains(propertyName)) {
keyBuffer.append(values[i]);
keyBuffer.append('-');
}
}
if (keyBuffer.length() < 1) {
return null;
} else {
// seperator value not likely to appear in a database
keyBuffer.append(KEY_SEPARATOR);
return keyBuffer.toString();
}
} else {
return null;
}
}
public Object getUniqueKey(Object[] values) {
return getUniqueKey(null, values);
}
/**
* Setter for the result class (what the results will be mapped into)
*
* @param resultClass - the result class
*/
public void setResultClass(Class resultClass) {
this.resultClass = resultClass;
}
/**
* Getter for the DataExchange object to be used
*
* @return - the DataExchange object
*/
public DataExchange getDataExchange() {
return dataExchange;
}
/**
* Setter for the DataExchange object to be used
*
* @param dataExchange - the new DataExchange object
*/
public void setDataExchange(DataExchange dataExchange) {
this.dataExchange = dataExchange;
}
/**
* Getter (used by DomDataExchange) for the xml name of the results
*
* @return - the name
*/
public String getXmlName() {
return xmlName;
}
/**
* Setter (used by the SqlMapBuilder) for the xml name of the results
*
* @param xmlName - the name
*/
public void setXmlName(String xmlName) {
this.xmlName = xmlName;
}
/**
* Getter for the resource (used to report errors)
*
* @return - the resource
*/
public String getResource() {
return resource;
}
/**
* Setter for the resource (used by the SqlMapBuilder)
*
* @param resource - the resource name
*/
public void setResource(String resource) {
this.resource = resource;
}
public void addGroupByProperty(String name) {
if (groupByProps == null) {
groupByProps = new HashSet();
}
groupByProps.add(name);
}
public boolean hasGroupBy() {
return groupByProps != null && groupByProps.size() > 0;
}
public Iterator groupByProps() {
return groupByProps.iterator();
}
public void addNestedResultMappings(ResultMapping mapping) {
if (nestedResultMappings == null) {
nestedResultMappings = new ArrayList();
}
nestedResultMappings.add(mapping);
}
public List getNestedResultMappings() {
return nestedResultMappings;
}
public ResultMapping[] getResultMappings() {
if (allowRemapping) {
return (ResultMapping[]) remappableResultMappings.get();
} else {
return resultMappings;
}
}
public void setDiscriminator (Discriminator discriminator) {
if (this.discriminator != null) {
throw new SqlMapException ("A discriminator may only be set once per result map.");
}
this.discriminator = discriminator;
}
public Discriminator getDiscriminator() {
return discriminator;
}
public ResultMap resolveSubMap (RequestScope request, ResultSet rs) throws SQLException {
ResultMap subMap = this;
if (discriminator != null) {
BasicResultMapping mapping = (BasicResultMapping)discriminator.getResultMapping();
Object value = getPrimitiveResultMappingValue(rs, mapping);
if (value == null) {
value = doNullMapping(value, mapping);
}
subMap = discriminator.getSubMap(String.valueOf(value));
if (subMap == null) {
subMap = this;
} else if (subMap != this) {
subMap = subMap.resolveSubMap(request, rs);
}
}
return subMap;
}
/**
* Setter for a list of the individual ResultMapping objects
*
* @param resultMappingList - the list
*/
public void setResultMappingList(List resultMappingList) {
if (allowRemapping) {
this.remappableResultMappings.set((BasicResultMapping[]) resultMappingList.toArray(new BasicResultMapping[resultMappingList.size()]));
} else {
this.resultMappings = (BasicResultMapping[]) resultMappingList.toArray(new BasicResultMapping[resultMappingList.size()]);
}
Map props = new HashMap();
props.put("map", this);
dataExchange = getDelegate().getDataExchangeFactory().getDataExchangeForClass(resultClass);
dataExchange.initialize(props);
}
/**
* Getter for the number of ResultMapping objects
*
* @return - the count
*/
public int getResultCount() {
return this.getResultMappings().length;
}
/**
* Read a row from a resultset and map results to an array.
*
* @param request scope of the request
* @param rs ResultSet to read from
*
* @return row read as an array of column values.
*
* @throws java.sql.SQLException
*/
public Object[] getResults(RequestScope request, ResultSet rs)
throws SQLException {
ErrorContext errorContext = request.getErrorContext();
errorContext.setActivity("applying a result map");
errorContext.setObjectId(this.getId());
errorContext.setResource(this.getResource());
errorContext.setMoreInfo("Check the result map.");
boolean foundData = false;
Object[] columnValues = new Object[getResultMappings().length];
for (int i = 0; i < getResultMappings().length; i++) {
BasicResultMapping mapping = (BasicResultMapping) getResultMappings()[i];
errorContext.setMoreInfo(mapping.getErrorString());
if (mapping.getStatementName() != null) {
if (resultClass == null) {
throw new SqlMapException("The result class was null when trying to get results for ResultMap named " + getId() + ".");
} else if (Map.class.isAssignableFrom(resultClass)) {
Class javaType = mapping.getJavaType();
if (javaType == null) {
javaType = Object.class;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -