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

Update MapperProxy.java #10487

Merged
merged 5 commits into from
May 17, 2023
Merged
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.plugin.datasource.mapper.Mapper;
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -35,20 +36,20 @@
* @author hyx
**/
public class MapperProxy implements InvocationHandler {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indent changed, please use nacos code style to reformat code.

private static final Logger LOGGER = LoggerFactory.getLogger(MapperProxy.class);

private Mapper mapper;

private static final Map<String, MapperProxy> SINGLE_MAPPER_PROXY_MAP = new HashMap<>(16);

private static final ReadWriteLock LOCK = new ReentrantReadWriteLock(true);

public <R> R createProxy(Mapper mapper) {
this.mapper = mapper;
return (R) Proxy.newProxyInstance(MapperProxy.class.getClassLoader(), mapper.getClass().getInterfaces(), this);
}

/**
* create proxy-mapper single instead of using method createProxy.
*/
Expand All @@ -72,15 +73,21 @@ public static <R> R createSingleProxy(Mapper mapper) {
LOCK.readLock().unlock();
}
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object invoke = method.invoke(mapper, args);

String className = mapper.getClass().getSimpleName();
String methodName = method.getName();
String sql = invoke.toString();


String sql;
if (invoke instanceof MapperResult) {
sql = ((MapperResult) invoke).getSql();
} else {
sql = invoke.toString();
}

LOGGER.info("[{}] METHOD : {}, SQL : {}, ARGS : {}", className, methodName, sql, JacksonUtils.toJson(args));
return invoke;
}
Expand Down