Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Designandcodesmells #3303

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 52 additions & 40 deletions src/main/java/org/apache/ibatis/binding/MapperMethod.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,53 +54,65 @@ public MapperMethod(Class<?> mapperInterface, Method method, Configuration confi
this.method = new MethodSignature(config, mapperInterface, method);
}

private boolean isInsertOrUpdateOrDelete(SqlCommandType type) {
return type == SqlCommandType.INSERT || type == SqlCommandType.UPDATE || type == SqlCommandType.DELETE;
}

public Object execute(SqlSession sqlSession, Object[] args) {
Object result;
switch (command.getType()) {
case INSERT: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.insert(command.getName(), param));
break;
}
case UPDATE: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.update(command.getName(), param));
break;
}
case DELETE: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.delete(command.getName(), param));
break;
}
case SELECT:
if (method.returnsVoid() && method.hasResultHandler()) {
executeWithResultHandler(sqlSession, args);
result = null;
} else if (method.returnsMany()) {
result = executeForMany(sqlSession, args);
} else if (method.returnsMap()) {
result = executeForMap(sqlSession, args);
} else if (method.returnsCursor()) {
result = executeForCursor(sqlSession, args);
} else {
Object param = method.convertArgsToSqlCommandParam(args);
result = sqlSession.selectOne(command.getName(), param);
if (method.returnsOptional() && (result == null || !method.getReturnType().equals(result.getClass()))) {
result = Optional.ofNullable(result);
}
}
break;
case FLUSH:
result = sqlSession.flushStatements();
break;
SqlCommandType type = command.getType();
if (isInsertOrUpdateOrDelete(type)) {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(executeSqlCommand(sqlSession, type, param));
} else if (type == SqlCommandType.SELECT) {
result = executeSelect(sqlSession, args);
} else if (type == SqlCommandType.FLUSH) {
result = sqlSession.flushStatements();
} else {
throw new BindingException("Unknown execution method for: " + command.getName());
}
validateResult(result);
return result;
}

private int executeSqlCommand(SqlSession sqlSession, SqlCommandType type, Object param) {
switch (type) {
case INSERT:
return sqlSession.insert(command.getName(), param);
case UPDATE:
return sqlSession.update(command.getName(), param);
case DELETE:
return sqlSession.delete(command.getName(), param);
default:
throw new BindingException("Unknown execution method for: " + command.getName());
throw new BindingException("Unsupported SqlCommandType: " + type);
}
}

private Object executeSelect(SqlSession sqlSession, Object[] args) {
if (method.returnsVoid() && method.hasResultHandler()) {
executeWithResultHandler(sqlSession, args);
return null;
} else if (method.returnsMany()) {
return executeForMany(sqlSession, args);
} else if (method.returnsMap()) {
return executeForMap(sqlSession, args);
} else if (method.returnsCursor()) {
return executeForCursor(sqlSession, args);
} else {
Object param = method.convertArgsToSqlCommandParam(args);
Object result = sqlSession.selectOne(command.getName(), param);
if (method.returnsOptional() && (result == null || !method.getReturnType().equals(result.getClass()))) {
result = Optional.ofNullable(result);
}
return result;
}
}

private void validateResult(Object result) {
if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
throw new BindingException("Mapper method '" + command.getName()
+ "' attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
}
return result;
}

private Object rowCountResult(int rowCount) {
Expand Down
42 changes: 3 additions & 39 deletions src/main/java/org/apache/ibatis/binding/MapperRegistry.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,11 +18,9 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.ibatis.builder.annotation.MapperAnnotationBuilder;
import org.apache.ibatis.io.ResolverUtil;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;

Expand Down Expand Up @@ -65,9 +63,6 @@ public <T> void addMapper(Class<T> type) {
boolean loadCompleted = false;
try {
knownMappers.put(type, new MapperProxyFactory<>(type));
// It's important that the type is added before the parser is run
// otherwise the binding may automatically be attempted by the
// mapper parser. If the type is already known, it won't try.
MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
parser.parse();
loadCompleted = true;
Expand All @@ -79,46 +74,15 @@ public <T> void addMapper(Class<T> type) {
}
}

/**
* Gets the mappers.
*
* @return the mappers
*
* @since 3.2.2
*/
public Collection<Class<?>> getMappers() {
return Collections.unmodifiableCollection(knownMappers.keySet());
}

/**
* Adds the mappers.
*
* @param packageName
* the package name
* @param superType
* the super type
*
* @since 3.2.2
*/
public void addMappers(String packageName, Class<?> superType) {
ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();
resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
for (Class<?> mapperClass : mapperSet) {
addMapper(mapperClass);
}
MapperRegistryHelper.addMappers(this, packageName, superType);
}

/**
* Adds the mappers.
*
* @param packageName
* the package name
*
* @since 3.2.2
*/
public void addMappers(String packageName) {
addMappers(packageName, Object.class);
MapperRegistryHelper.addMappers(this, packageName);
}

}
35 changes: 35 additions & 0 deletions src/main/java/org/apache/ibatis/binding/MapperRegistryHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2009-2024 the original author or authors.
*
* 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
*
* https://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 org.apache.ibatis.binding;

import java.util.Set;

import org.apache.ibatis.io.ResolverUtil;

public class MapperRegistryHelper {
public static void addMappers(MapperRegistry registry, String packageName, Class<?> superType) {
ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();
resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
for (Class<?> mapperClass : mapperSet) {
registry.addMapper(mapperClass);
}
}

public static void addMappers(MapperRegistry registry, String packageName) {
addMappers(registry, packageName, Object.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2022 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,9 +31,9 @@
*/
public class JndiDataSourceFactory implements DataSourceFactory {

public static final String INITIAL_CONTEXT = "initial_context";
public static final String DATA_SOURCE = "data_source";
public static final String ENV_PREFIX = "env.";
private static final String INITIAL_CONTEXT = "initial_context";
private static final String DATA_SOURCE = "data_source";
private static final String ENV_PREFIX = "env.";

private DataSource dataSource;

Expand Down Expand Up @@ -81,4 +81,16 @@ private static Properties getEnvProperties(Properties allProps) {
return contextProperties;
}

public static String getInitialContext() {
return INITIAL_CONTEXT;
}

public static String getDataSourceString() {
return DATA_SOURCE;
}

public static String getEnvPrefix() {
return ENV_PREFIX;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -53,7 +53,7 @@ public class PooledDataSource implements DataSource {
protected int poolMaximumIdleConnections = 5;
protected int poolMaximumCheckoutTime = 20000;
protected int poolTimeToWait = 20000;
protected int poolMaximumLocalBadConnectionTolerance = 3;
protected int maxBadConnTolerance = 3;
protected String poolPingQuery = "NO PING QUERY SET";
protected boolean poolPingEnabled;
protected int poolPingConnectionsNotUsedFor;
Expand Down Expand Up @@ -206,7 +206,7 @@ public void setPoolMaximumIdleConnections(int poolMaximumIdleConnections) {
* @since 3.4.5
*/
public void setPoolMaximumLocalBadConnectionTolerance(int poolMaximumLocalBadConnectionTolerance) {
this.poolMaximumLocalBadConnectionTolerance = poolMaximumLocalBadConnectionTolerance;
this.maxBadConnTolerance = poolMaximumLocalBadConnectionTolerance;
}

/**
Expand Down Expand Up @@ -313,7 +313,7 @@ public int getPoolMaximumIdleConnections() {
}

public int getPoolMaximumLocalBadConnectionTolerance() {
return poolMaximumLocalBadConnectionTolerance;
return maxBadConnTolerance;
}

public int getPoolMaximumCheckoutTime() {
Expand Down Expand Up @@ -525,7 +525,7 @@ private PooledConnection popConnection(String username, String password) throws
state.badConnectionCount++;
localBadConnectionCount++;
conn = null;
if (localBadConnectionCount > poolMaximumIdleConnections + poolMaximumLocalBadConnectionTolerance) {
if (localBadConnectionCount > poolMaximumIdleConnections + maxBadConnTolerance) {
if (log.isDebugEnabled()) {
log.debug("PooledDataSource: Could not get a good connection to the database.");
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/apache/ibatis/mapping/BooleanPropertySetter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2009-2024 the original author or authors.
*
* 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
*
* https://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 org.apache.ibatis.mapping;

import org.apache.ibatis.reflection.MetaObject;

public class BooleanPropertySetter implements PropertySetter {
@Override
public void setProperty(MetaObject metaCache, String name, String value) {
metaCache.setValue(name, Boolean.valueOf(value));
}
}
25 changes: 25 additions & 0 deletions src/main/java/org/apache/ibatis/mapping/BytePropertySetter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2009-2024 the original author or authors.
*
* 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
*
* https://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 org.apache.ibatis.mapping;

import org.apache.ibatis.reflection.MetaObject;

public class BytePropertySetter implements PropertySetter {
@Override
public void setProperty(MetaObject metaCache, String name, String value) {
metaCache.setValue(name, Byte.valueOf(value));
}
}
Loading