mirror of
https://gitee.com/ssssssss-team/magic-api.git
synced 2026-06-09 18:32:16 +08:00
格式化代码
This commit is contained in:
@@ -16,11 +16,10 @@ import java.util.stream.Collectors;
|
||||
|
||||
public abstract class ResourceAdapter {
|
||||
|
||||
public static final String SPRING_BOOT_CLASS_PATH = "BOOT-INF/classes/";
|
||||
private static PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
||||
|
||||
public static final String SPRING_BOOT_CLASS_PATH = "BOOT-INF/classes/";
|
||||
|
||||
public static Resource getResource(String location,boolean readonly) throws IOException {
|
||||
public static Resource getResource(String location, boolean readonly) throws IOException {
|
||||
if (location == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -21,13 +21,10 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class DatabaseResource extends KeyValueResource {
|
||||
|
||||
private final JdbcTemplate template;
|
||||
|
||||
private final String tableName;
|
||||
|
||||
private Map<String, String> cachedContent = new ConcurrentHashMap<>();
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DatabaseResource.class);
|
||||
private final JdbcTemplate template;
|
||||
private final String tableName;
|
||||
private Map<String, String> cachedContent = new ConcurrentHashMap<>();
|
||||
|
||||
public DatabaseResource(JdbcTemplate template, String tableName) {
|
||||
this(template, tableName, false);
|
||||
|
||||
@@ -14,9 +14,8 @@ import java.util.zip.ZipOutputStream;
|
||||
|
||||
public class FileResource implements Resource {
|
||||
|
||||
private File file;
|
||||
|
||||
private final boolean readonly;
|
||||
private File file;
|
||||
|
||||
public FileResource(File file, boolean readonly) {
|
||||
this.file = file;
|
||||
|
||||
@@ -22,10 +22,8 @@ public class JarResource implements Resource {
|
||||
private final List<JarEntry> entries;
|
||||
|
||||
private final String entryName;
|
||||
|
||||
private JarResource parent = this;
|
||||
|
||||
private final boolean inSpringBoot;
|
||||
private JarResource parent = this;
|
||||
|
||||
public JarResource(JarFile jarFile, String entryName, List<JarEntry> entries, boolean inSpringBoot) {
|
||||
this.jarFile = jarFile;
|
||||
|
||||
@@ -2,13 +2,10 @@ package org.ssssssss.magicapi.adapter.resource;
|
||||
|
||||
import org.ssssssss.magicapi.adapter.Resource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
public abstract class KeyValueResource implements Resource {
|
||||
|
||||
|
||||
@@ -16,10 +16,8 @@ import java.util.function.Function;
|
||||
|
||||
public class RedisResource extends KeyValueResource {
|
||||
|
||||
private final StringRedisTemplate redisTemplate;
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(RedisResource.class);
|
||||
|
||||
private final StringRedisTemplate redisTemplate;
|
||||
private final Map<String, String> cachedContent = new ConcurrentHashMap<>();
|
||||
|
||||
public RedisResource(StringRedisTemplate redisTemplate, String path, boolean readonly, RedisResource parent) {
|
||||
|
||||
@@ -42,7 +42,7 @@ public class MagicDynamicDataSource {
|
||||
* 注册数据源(可以运行时注册)
|
||||
*
|
||||
* @param dataSourceKey 数据源Key
|
||||
* @param maxRows 最大返回行数
|
||||
* @param maxRows 最大返回行数
|
||||
*/
|
||||
public void put(String dataSourceKey, DataSource dataSource, int maxRows) {
|
||||
put(null, dataSourceKey, dataSourceKey, dataSource, maxRows);
|
||||
@@ -61,7 +61,7 @@ public class MagicDynamicDataSource {
|
||||
}
|
||||
logger.info("注册数据源:{}", StringUtils.isNotBlank(dataSourceKey) ? dataSourceKey : "default");
|
||||
DataSourceNode node = this.dataSourceMap.put(dataSourceKey, new DataSourceNode(dataSource, dataSourceKey, datasourceName, id, maxRows));
|
||||
if(node != null){
|
||||
if (node != null) {
|
||||
node.close();
|
||||
}
|
||||
if (id != null) {
|
||||
@@ -100,8 +100,8 @@ public class MagicDynamicDataSource {
|
||||
// 检查参数是否合法
|
||||
if (datasourceKey != null && !datasourceKey.isEmpty()) {
|
||||
DataSourceNode node = this.dataSourceMap.remove(datasourceKey);
|
||||
result = node!= null;
|
||||
if(result){
|
||||
result = node != null;
|
||||
if (result) {
|
||||
node.close();
|
||||
}
|
||||
}
|
||||
@@ -139,7 +139,8 @@ public class MagicDynamicDataSource {
|
||||
|
||||
/**
|
||||
* 设置默认数据源
|
||||
* @param maxRows 最大返回行数
|
||||
*
|
||||
* @param maxRows 最大返回行数
|
||||
*/
|
||||
public void setDefault(DataSource dataSource, int maxRows) {
|
||||
put(null, null, null, dataSource, maxRows);
|
||||
@@ -225,7 +226,7 @@ public class MagicDynamicDataSource {
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
public void close(){
|
||||
public void close() {
|
||||
IoUtils.closeDataSource(this.dataSource);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,19 +8,19 @@ public class RequestContext {
|
||||
|
||||
private static final ThreadLocal<RequestEntity> REQUEST_ENTITY_THREAD_LOCAL = new InheritableThreadLocal<>();
|
||||
|
||||
public static void setRequestEntity(RequestEntity requestEntity) {
|
||||
REQUEST_ENTITY_THREAD_LOCAL.set(requestEntity);
|
||||
}
|
||||
|
||||
public static HttpServletRequest getHttpServletRequest() {
|
||||
RequestEntity requestEntity = REQUEST_ENTITY_THREAD_LOCAL.get();
|
||||
return requestEntity == null ? null : requestEntity.getRequest();
|
||||
}
|
||||
|
||||
public static RequestEntity getRequestEntity(){
|
||||
public static RequestEntity getRequestEntity() {
|
||||
return REQUEST_ENTITY_THREAD_LOCAL.get();
|
||||
}
|
||||
|
||||
public static void setRequestEntity(RequestEntity requestEntity) {
|
||||
REQUEST_ENTITY_THREAD_LOCAL.set(requestEntity);
|
||||
}
|
||||
|
||||
public static void remove() {
|
||||
REQUEST_ENTITY_THREAD_LOCAL.remove();
|
||||
}
|
||||
|
||||
@@ -7,11 +7,9 @@ import java.util.Objects;
|
||||
|
||||
public class DefaultAuthorizationInterceptor implements AuthorizationInterceptor {
|
||||
|
||||
private String validToken;
|
||||
|
||||
private MagicUser configMagicUser;
|
||||
|
||||
private final boolean requireLogin;
|
||||
private String validToken;
|
||||
private MagicUser configMagicUser;
|
||||
|
||||
public DefaultAuthorizationInterceptor(String username, String password) {
|
||||
if (this.requireLogin = username != null && password != null) {
|
||||
|
||||
@@ -20,7 +20,7 @@ public interface SQLInterceptor {
|
||||
* @see SQLInterceptor#preHandle(BoundSql, RequestEntity)
|
||||
*/
|
||||
@Deprecated
|
||||
default void preHandle(BoundSql boundSql){
|
||||
default void preHandle(BoundSql boundSql) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -6,22 +6,24 @@ import org.ssssssss.script.MagicScriptContext;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Map;
|
||||
|
||||
public class RequestEntity {
|
||||
|
||||
private ApiInfo apiInfo;
|
||||
private ApiInfo apiInfo;
|
||||
|
||||
private HttpServletRequest request;
|
||||
private HttpServletRequest request;
|
||||
|
||||
private HttpServletResponse response;
|
||||
private HttpServletResponse response;
|
||||
|
||||
private boolean requestedFromTest;
|
||||
private boolean requestedFromTest;
|
||||
|
||||
private Map<String, Object> parameters;
|
||||
private Map<String, Object> parameters;
|
||||
|
||||
private Map<String, Object> pathVariables;
|
||||
private Long requestTime = System.currentTimeMillis();
|
||||
private Map<String, Object> pathVariables;
|
||||
private Long requestTime = System.currentTimeMillis();
|
||||
private MagicScriptContext magicScriptContext;
|
||||
private Map<String, Object> headers;
|
||||
|
||||
private RequestEntity() {
|
||||
|
||||
}
|
||||
@@ -65,26 +67,50 @@ public class RequestEntity {
|
||||
return apiInfo;
|
||||
}
|
||||
|
||||
public void setApiInfo(ApiInfo apiInfo) {
|
||||
this.apiInfo = apiInfo;
|
||||
}
|
||||
|
||||
public HttpServletRequest getRequest() {
|
||||
return request;
|
||||
}
|
||||
|
||||
public void setRequest(HttpServletRequest request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public HttpServletResponse getResponse() {
|
||||
return response;
|
||||
}
|
||||
|
||||
public void setResponse(HttpServletResponse response) {
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
public boolean isRequestedFromTest() {
|
||||
return requestedFromTest;
|
||||
}
|
||||
|
||||
public void setRequestedFromTest(boolean requestedFromTest) {
|
||||
this.requestedFromTest = requestedFromTest;
|
||||
}
|
||||
|
||||
public Map<String, Object> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public void setParameters(Map<String, Object> parameters) {
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public Map<String, Object> getPathVariables() {
|
||||
return pathVariables;
|
||||
}
|
||||
|
||||
public void setPathVariables(Map<String, Object> pathVariables) {
|
||||
this.pathVariables = pathVariables;
|
||||
}
|
||||
|
||||
public Long getRequestTime() {
|
||||
return requestTime;
|
||||
}
|
||||
@@ -105,29 +131,5 @@ public class RequestEntity {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
public void setApiInfo(ApiInfo apiInfo) {
|
||||
this.apiInfo = apiInfo;
|
||||
}
|
||||
|
||||
public void setRequest(HttpServletRequest request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public void setResponse(HttpServletResponse response) {
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
public void setRequestedFromTest(boolean requestedFromTest) {
|
||||
this.requestedFromTest = requestedFromTest;
|
||||
}
|
||||
|
||||
public void setParameters(Map<String, Object> parameters) {
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public void setPathVariables(Map<String, Object> pathVariables) {
|
||||
this.pathVariables = pathVariables;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -355,8 +355,7 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
|
||||
int count = countBoundSql.getCacheValue(this.sqlInterceptors, () -> dataSourceNode.getJdbcTemplate().queryForObject(countBoundSql.getSql(), Integer.class, countBoundSql.getParameters()));
|
||||
List<Map<String, Object>> list = null;
|
||||
if (count > 0) {
|
||||
String pageSql = dialect.getPageSql(boundSql.getSql(), boundSql, page.getOffset(), page.getLimit());
|
||||
BoundSql pageBoundSql = boundSql.copy(pageSql);
|
||||
BoundSql pageBoundSql = buildPageBoundSql(dialect, boundSql, page.getOffset(), page.getLimit());
|
||||
list = pageBoundSql.getCacheValue(this.sqlInterceptors, () -> dataSourceNode.getJdbcTemplate().query(pageBoundSql.getSql(), this.columnMapRowMapper, pageBoundSql.getParameters()));
|
||||
}
|
||||
RequestEntity requestEntity = RequestContext.getRequestEntity();
|
||||
@@ -383,7 +382,9 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
|
||||
@UnableCall
|
||||
public Map<String, Object> selectOne(BoundSql boundSql) {
|
||||
return boundSql.getCacheValue(this.sqlInterceptors, () -> {
|
||||
List<Map<String, Object>> list = dataSourceNode.getJdbcTemplate().query(boundSql.getSql(), this.columnMapRowMapper, boundSql.getParameters());
|
||||
Dialect dialect = dataSourceNode.getDialect(dialectAdapter);
|
||||
BoundSql pageBoundSql = buildPageBoundSql(dialect, boundSql, 0, 1);
|
||||
List<Map<String, Object>> list = dataSourceNode.getJdbcTemplate().query(pageBoundSql.getSql(), this.columnMapRowMapper, pageBoundSql.getParameters());
|
||||
return list.size() > 0 ? list.get(0) : null;
|
||||
});
|
||||
}
|
||||
@@ -394,7 +395,11 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
|
||||
@Comment("查询单行单列的值")
|
||||
public Object selectValue(@Comment("`SQL`语句") String sql) {
|
||||
BoundSql boundSql = new BoundSql(sql, this);
|
||||
return boundSql.getCacheValue(this.sqlInterceptors, () -> dataSourceNode.getJdbcTemplate().queryForObject(boundSql.getSql(), boundSql.getParameters(), Object.class));
|
||||
return boundSql.getCacheValue(this.sqlInterceptors, () -> {
|
||||
Dialect dialect = dataSourceNode.getDialect(dialectAdapter);
|
||||
BoundSql pageBoundSql = buildPageBoundSql(dialect, boundSql, 0, 1);
|
||||
return dataSourceNode.getJdbcTemplate().queryForObject(pageBoundSql.getSql(), Object.class, pageBoundSql.getParameters());
|
||||
});
|
||||
}
|
||||
|
||||
@Comment("指定table,进行单表操作")
|
||||
@@ -402,6 +407,11 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
|
||||
return new NamedTable(tableName, this, rowMapColumnMapper);
|
||||
}
|
||||
|
||||
private BoundSql buildPageBoundSql(Dialect dialect, BoundSql boundSql, long offset, long limit) {
|
||||
String pageSql = dialect.getPageSql(boundSql.getSql(), boundSql, offset, limit);
|
||||
return boundSql.copy(pageSql);
|
||||
}
|
||||
|
||||
@UnableCall
|
||||
@Override
|
||||
public String getModuleName() {
|
||||
|
||||
@@ -87,9 +87,9 @@ public interface ResultProvider {
|
||||
|
||||
/**
|
||||
* @param requestEntity 请求相关信息
|
||||
* @param page 分页对象
|
||||
* @param total 总数
|
||||
* @param data 数据内容
|
||||
* @param page 分页对象
|
||||
* @param total 总数
|
||||
* @param data 数据内容
|
||||
*/
|
||||
default Object buildPageResult(RequestEntity requestEntity, Page page, long total, List<Map<String, Object>> data) {
|
||||
return buildPageResult(total, data);
|
||||
|
||||
@@ -7,8 +7,6 @@ import javax.sql.DataSource;
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -25,11 +25,11 @@ public class Mapping {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Map<RequestMappingInfo, HandlerMethod> getHandlerMethods(){
|
||||
public Map<RequestMappingInfo, HandlerMethod> getHandlerMethods() {
|
||||
return this.methodMapping.getHandlerMethods();
|
||||
}
|
||||
|
||||
public Mapping unregister(RequestMappingInfo requestMappingInfo){
|
||||
public Mapping unregister(RequestMappingInfo requestMappingInfo) {
|
||||
this.methodMapping.unregisterMapping(requestMappingInfo);
|
||||
return this;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user