Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
重构一些代码
  • Loading branch information
xixifeng committed Jun 12, 2019
1 parent 280fe9f commit 4860c57
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 25 deletions.
16 changes: 6 additions & 10 deletions src/main/java/org/fastquery/asm/AsmRepository.java
Expand Up @@ -40,6 +40,7 @@
import org.fastquery.analysis.GenerateExtends;
import org.fastquery.core.AbstractQueryRepository;
import org.fastquery.core.Placeholder;
import org.fastquery.core.QueryRepository;
import org.fastquery.mapper.QueryValidator;

/**
Expand Down Expand Up @@ -156,21 +157,16 @@ public static synchronized byte[] generateBytes(Class<?> repositoryClazz) {
String className = repositoryClazz.getName() + Placeholder.DB_SUF;
CtClass ctClass = pool.makeClass(className);
try {

// 设置父类
ctClass.setSuperclass(pool.get(AbstractQueryRepository.class.getName()));
if(QueryRepository.class.isAssignableFrom(repositoryClazz)) {
// 设置父类
ctClass.setSuperclass(pool.get(AbstractQueryRepository.class.getName()));
}
// 增加接口
ctClass.setInterfaces(new CtClass[] { pool.get(repositoryClazz.getName()) });

addGetInterfaceClassMethod(repositoryClazz, ctClass);

makeSingleton(className, ctClass);

makeMethod(repositoryClazz, ctClass);

return ctClass.toBytecode();

} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
Expand All @@ -179,7 +175,7 @@ public static synchronized byte[] generateBytes(Class<?> repositoryClazz) {
static void addGetInterfaceClassMethod(Class<?> repositoryClazz, CtClass ctClass) throws CannotCompileException {
// 增加字段
CtField field = CtField.make("private Class c = "+repositoryClazz.getName()+".class;", ctClass);
ctClass.addField(field);
ctClass.addField(field);
// 覆盖部分default 方法
CtMethod cm = CtMethod.make("public Class getInterfaceClass() {return c;}", ctClass);
ctClass.addMethod(cm);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/fastquery/core/Prepared.java
Expand Up @@ -52,11 +52,11 @@ private Prepared() {
* @param target 目标 Repository
* @return 执行之后的值
*/
public static Object excute(MethodInfo methodInfo,Object[] args, QueryRepository target) {
public static Object excute(MethodInfo methodInfo,Object[] args, Repository target) {
long start = System.currentTimeMillis();
Method method = methodInfo.getMethod();
try {
Class<? extends Repository> iclazz = target.getInterfaceClass();
Class<?> iclazz = target.getInterfaceClass();

// 如果是调试模式
if (FastQueryJSONObject.getDebug()) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/fastquery/core/QueryContext.java
Expand Up @@ -35,7 +35,7 @@ public final class QueryContext {
private Class<?> returnType; // 返回类型
private Connection connection; // 当前连接
private String sourceName; // 当前数据源名称
private Class<? extends Repository> iclass;// 当前拦截到的接口
private Class<?> iclass;// 当前拦截到的接口
private Object[] args; // 当前方法的实参
private List<String> sqls = new ArrayList<>(); // 当前method所执行的SQL集合
private MetaData metaData; // 当前上下文元数据
Expand All @@ -52,7 +52,7 @@ private static QueryContext getQueryContext() {
return threadLocal.get();
}

static void start(Class<? extends Repository> iclass, MethodInfo methodInfo, Object[] args) throws SQLException {
static void start(Class<?> iclass, MethodInfo methodInfo, Object[] args) throws SQLException {
QueryContext context = threadLocal.get();
if (context != null && !debug) {
if(context.connection != null && !context.connection.isClosed()) { // 不等于null并且没有关闭
Expand Down Expand Up @@ -88,7 +88,7 @@ static void start(Class<? extends Repository> iclass, MethodInfo methodInfo, Obj
setPageable(methodInfo.getParameters(), args, context);
}

private static void setConnection(Class<? extends Repository> iclass, Id id, QueryContext context) throws SQLException {
private static void setConnection(Class<?> iclass, Id id, QueryContext context) throws SQLException {
if (context.connection == null || context.connection.isClosed()) { // 不加这行,测试StudentDBServiceTest会卡顿
DataSource ds = getDataSource(context.sourceName, iclass.getName());
if(TxContext.enabled()) {
Expand Down Expand Up @@ -168,7 +168,7 @@ static Pageable getPageable() {
return getQueryContext().pageable;
}

public static Class<? extends Repository> getIclass() {
public static Class<?> getIclass() {
return getQueryContext().iclass;
}

Expand Down
4 changes: 0 additions & 4 deletions src/main/java/org/fastquery/core/QueryRepository.java
Expand Up @@ -575,8 +575,4 @@ default <E> E saveOrUpdate(String dataSourceName, String dbName, E entity) {
*/
@Id(MethodId.QUERY9)
int tx(Supplier<Integer> fun);

default Class<? extends Repository> getInterfaceClass() {
return null;
}
}
4 changes: 4 additions & 0 deletions src/main/java/org/fastquery/core/Repository.java
Expand Up @@ -28,4 +28,8 @@
* @author xixifeng (fastquery@126.com)
*/
public interface Repository {
// 当前Repository接口的class
default Class<? extends Repository> getInterfaceClass() {
return null;
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/fastquery/jersey/FqClassLoader.java
Expand Up @@ -27,7 +27,7 @@
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.fastquery.core.QueryRepository;
import org.fastquery.core.Repository;
import org.fastquery.core.RepositoryException;

/**
Expand All @@ -50,7 +50,7 @@ final Class<?> defineClassByName(String name, byte[] b, boolean isWebQuery) {
resourceNames.add(name);
} else {
try {
QueryRepository repository = (QueryRepository) clazz.getMethod("g").invoke(null);
Repository repository = (Repository) clazz.getMethod("g").invoke(null);
binder.bind(repository).to(repository.getInterfaceClass());
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
throw new RepositoryException(e);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/fastquery/test/FastQueryTestRule.java
Expand Up @@ -165,7 +165,7 @@ public List<String> getExecutedSQLs() {
private void after(Description description) throws SQLException {
LOG.debug("{} --------------------------------------------已经结束,当前线程:{}", description.getMethodName(), Thread.currentThread());
QueryContext context = QueryContextHelper.getQueryContext();
if (context != null) {
if (context != null && QueryContext.getConn() != null) {
Rollback rollback = description.getAnnotation(Rollback.class);
if (rollback == null || rollback.value()) {
QueryContext.getConn().rollback();
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/fastquery/dao/ConditionDBService.java
Expand Up @@ -28,7 +28,7 @@
import org.fastquery.bean.UserInfo;
import org.fastquery.core.Param;
import org.fastquery.core.Query;
import org.fastquery.core.QueryRepository;
import org.fastquery.core.Repository;
import org.fastquery.page.Page;
import org.fastquery.page.Pageable;
import org.fastquery.where.Condition;
Expand All @@ -38,7 +38,7 @@
*
* @author mei.sir@aliyun.cn
*/
public interface ConditionDBService extends QueryRepository {
public interface ConditionDBService extends Repository {

@Query("select * ${tname} #{#where} limit 3")
@Condition(value=" $nameWhere",ignoreScript=":nameWhere==null")
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/fastquery/test/ConditionTest.java
Expand Up @@ -22,6 +22,7 @@

package org.fastquery.test;

import org.fastquery.core.QueryRepository;
import org.fastquery.dao.ConditionDBService;
import org.fastquery.page.PageableImpl;
import org.fastquery.service.FQuery;
Expand All @@ -30,6 +31,9 @@
import org.junit.Test;

import static org.junit.Assert.assertThat;

import org.apache.commons.lang3.exception.ExceptionUtils;

import static org.hamcrest.Matchers.*;

/**
Expand All @@ -43,6 +47,17 @@ public class ConditionTest extends FastQueryTest {
@Rule
public FastQueryTestRule rule = new FastQueryTestRule();

@Test
public void db() {
assertThat(QueryRepository.class.isAssignableFrom(db.getClass()), is(false));
// 断言是否继承了QueryRepository中的方法
try {
db.getClass().getMethod("executeBatch", String.class,String.class);
} catch (Exception e) {
assertThat(ExceptionUtils.getStackTrace(e), containsString("java.lang.NoSuchMethodException: com.sun.proxy.$Proxy42.executeBatch(java.lang.String, java.lang.String)"));
}
}

@Test
public void findUserInfo1() {
String tname = "from UserInfo";
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/fastquery/test/MethodQueryTest.java
Expand Up @@ -23,6 +23,7 @@
package org.fastquery.test;

import org.fastquery.bean.UserInfo;
import org.fastquery.core.QueryRepository;
import org.fastquery.core.RepositoryException;
import org.fastquery.dao.UserInfoDBService;
import org.fastquery.example.StudentDBService;
Expand Down Expand Up @@ -55,6 +56,15 @@ public class MethodQueryTest extends FastQueryTest {
private static StudentDBService studentDBService = FQuery.getRepository(StudentDBService.class);
private static UserInfoDBService userInfoDBService = FQuery.getRepository(UserInfoDBService.class);

@Test
public void db() throws NoSuchMethodException, SecurityException {
assertThat(QueryRepository.class.isAssignableFrom(studentDBService.getClass()), is(true));
// 断言是否继承了QueryRepository中的方法
assertThat(studentDBService.getClass().getMethod("executeBatch", String.class,String.class), notNullValue());
assertThat(QueryRepository.class.isAssignableFrom(userInfoDBService.getClass()), is(true));
assertThat(userInfoDBService.getClass().getMethod("executeBatch", String.class,String.class), notNullValue());
}

@Test
public void testSave() {

Expand Down

0 comments on commit 4860c57

Please sign in to comment.