public PageReturn<Object> wrapPageReturn(final AtomicInteger pageNum , final AtomicInteger pageSize , List<Object> records) { PageReturn<Object> pageReturn = new PageReturn<>(); pageReturn.setData(records); pageReturn.setPageNum(pageNum.get()); pageReturn.setPageSize(pageSize.get()); return pageReturn; }
public static <T> String pageable(Pageable<T> page) { StringBuilder query = new StringBuilder(); int position = (page.getPageNum() - 1) * page.getPageSize(); query.append(", ").append(page.getPageNum()).append(" AS pageNum"); query.append(", ").append(page.getPageSize()).append(" AS pageSize"); if(position == 0) query.append(" LIMIT ").append(page.getPageSize()); else { int skip = position - 1; query.append(" SKIP ").append(skip); query.append(" LIMIT ").append(page.getPageSize()); } return query.toString(); }
public static boolean includePageable(SqlMethod sqlMethod) { for(Class<?> type : sqlMethod.getDefinition().getParameterTypes()) { if(type.isAssignableFrom(Pageable.class)) return true; } return false; } }
.filter(clazz -> clazz.getAnnotation(Neo4jDao.class) != null) .map(clazz -> { Neo4jDaoInfo result = new Neo4jDaoInfo(); result.setDaoClass(clazz); Neo4jDao neo4jDao = clazz.getAnnotation(Neo4jDao.class); if(neo4jDao.value().isEmpty()) result.setName(clazz.getSimpleName()); else result.setName(neo4jDao.value()); HashMap<Class<?> , Class<?>> daoInterfaceMapping = Maps.newHashMap(); result.setDaoInterfaceMapping(daoInterfaceMapping); parse(clazz , daoInterfaceMapping); .filter(method -> method.getAnnotation(SqlClip.class) != null) .<SqlClipMethod>map(method -> { SqlClipMethod sqlClipMethod = new SqlClipMethod(); sqlClipMethod.setDefinition(method); SqlClip sqlClip = method.getAnnotation(SqlClip.class); sqlClipMethod.setSqlClipName(sqlClip.sqlClipName().isEmpty() ? method.getName() : sqlClip.sqlClipName()); SqlClipMethodParam[] sqlClipMethodParams = new SqlClipMethodParam[paramNames.length]; for(int i=0; i<paramNames.length; i++) { SqlClipMethodParam param = new SqlClipMethodParam(); sqlClipMethodParams[i] = param; param.setDefinition(parameters[i]); param.setNativeName(paramNames[i]); param.setParam(parameters[i].getAnnotation(Param.class)); sqlClipMethod.setSqlClipMethodParams(sqlClipMethodParams); return sqlClipMethod; }).collect(Collectors.toList());
@Override public Optional<Sql> compute(Method method , Neo4jDaoInfo neo4jDaoInfo , Object... values) { final MethodKey key = new MethodKey(); key.setMethodName(method.getName()); key.setParameTypes(method.getParameterTypes()); SqlMethod sqlMethod = neo4jDaoInfo.getSqlMethods().get(key); if(sqlMethod.getSqlClipMethod() == null) return next.compute(method , neo4jDaoInfo , values); SqlClipMethod sqlClipMethod = sqlMethod.getSqlClipMethod(); Method clipMethod = sqlClipMethod.getDefinition(); Class<?>[] clipArgTypes = clipMethod.getParameterTypes(); Object clips = clipsCache.computeIfAbsent(clipMethod.getDeclaringClass(), ClassOperation::newInstance); SqlClipMethodParam[] sqlClipMethodParams = sqlClipMethod.getSqlClipMethodParams(); final Map<String , Object> params = new HashMap<>(); Pageable<?> pageable = null; for(int i=0; i<sqlClipMethodParams.length; i++) { String paramKey; Param param = sqlClipMethodParams[i].getParam(); if(param != null) paramKey = param.value(); else paramKey = sqlClipMethodParams[i].getNativeName(); Object value = values[i]; params.put(paramKey , value); clipMethod.getDeclaringClass().getName(), clipMethod.getName())); } else { SqlMethodParam[] sqlMethodParams = sqlMethod.getSqlMethodParams(); final Object[] clipValues = new Object[sqlClipMethodParams.length]; MATCH_PARAM:for(int i=0; i<clipValues.length; i++) { Param param = sqlClipMethodParams[i].getParam(); if(param == null) throw new IllegalConfigException(
@Override public Optional<Sql> compute(Method method , Neo4jDaoInfo neo4jDaoInfo , Object... values) { final MethodKey key = new MethodKey(); key.setMethodName(method.getName()); key.setParameTypes(method.getParameterTypes()); SqlMethod sqlMethod = neo4jDaoInfo.getSqlMethods().get(key); String sql = sqlMethod.getSql(); if(StringOperation.isTrimEmptyOrNull(sql)) return next.compute(method , neo4jDaoInfo , values); SqlMethodParam[] sqlMethodParams = sqlMethod.getSqlMethodParams(); Map<String , Object> params = new HashMap<>(); Pageable<?> pageable = null; for(int i=0; i<sqlMethodParams.length; i++) { String paramKey; Param param = sqlMethodParams[i].getParam(); if(param != null) paramKey = param.value(); else paramKey = sqlMethodParams[i].getNativeName(); Object value = values[i]; params.put(paramKey , value); if(value instanceof Pageable) pageable = (Pageable<?>) value; } if(SqlComputes.isPageReturn(sqlMethod) && pageable != null) sql = sql + SqlComputes.pageable(pageable); return sqlFormatter.format(sql , params); }
@SuppressWarnings("unchecked") public <T> T process(DaoProcessorParam param, Neo4jDaoInfo neo4jDaoInfo) { MethodKey methodKey = new MethodKey(); methodKey.setMethodName(param.getMethodName()); methodKey.setParameTypes(param.getArgTypes()); SqlMethod sqlMethod = neo4jDaoInfo.getSqlMethods().get(methodKey); Optional<Sql> query = sqlCompute.compute(sqlMethod.getDefinition() , neo4jDaoInfo , param.getArgs()); if(!query.isPresent()) throw new Neo4jException("No Query find out"); Sql sql = query.get(); Transaction tx = null; for(Object arg : param.getArgs()) { if(arg instanceof Transaction) tx = (Transaction) arg; } Object obj = tx.runAsync(sql.getSql(), Values.value(sql.getParameters())) .thenComposeAsync(src -> deSerializer.deSerialize(src , sqlMethod)); return (T) obj; } }
public static final void checkReturnType(SqlMethod sqlMethod) { GenericNode returnTypeInfo = sqlMethod.getSqlMethodRet().getReturnTypeInfo(); if(!returnTypeInfo.getDeclareType().isAssignableFrom(CompletionStage.class)) { throw new IllegalConfigException(String.format("the return type of %s.%s should be %s in first level" , sqlMethod.getDefinition().getDeclaringClass().getName(), sqlMethod.getDefinition().getName(), CompletionStage.class)); } }
public static boolean isPageReturn(SqlMethod sqlMethod) { GenericNode returnTypeInfo = sqlMethod.getSqlMethodRet().getReturnTypeInfo(); if(returnTypeInfo.isGeneiric()) { GenericNode pageReturn = returnTypeInfo.getComponentTypes().get(0); if(pageReturn.getDeclareType().isAssignableFrom(PageReturn.class)) { return true; } } return false; }
public static final Object formatJavaBean(Class<?> returnType, SqlMethod sqlMethod, Value val) { List<Method> setMethods = sqlMethod.getSqlMethodRet().getSetMethods(returnType); if(val.isNull()) return null; Entity entity = val.asEntity(); if(entity.size() == 0) return null; Object tmp = ConstructorOperation.newInstance(returnType); for(Method setMethod : setMethods) { String key = setMethod.getName(); key = StringOperation.replaceFirst(key , "set" , ""); key = key.substring(0 , 1).toLowerCase() + key.substring(1); key = sqlMethod.getSqlMethodRet().getRealName(key , returnType); Object value = formatValue(setMethod.getParameterTypes()[0] , entity.get(key)); if(value != null) MethodOperation.invoked(tmp , setMethod , value); } return tmp; }
public PackageScanDaoContext(List<Package> pkg) { pkg.forEach(it -> { PackageMapperScanner scanner = new PackageMapperScanner(); scanner.setPackagePath(it); scanner.scan().forEach((k,v) -> { cacheByName.put(k , v); cacheByClass.put(v.getDaoClass() , v); }); }); }
@Override @SuppressWarnings("unchecked") public <T> T process(DaoProcessorParam param, Neo4jDaoInfo neo4jDaoInfo) { MethodKey methodKey = new MethodKey(); methodKey.setMethodName(param.getMethodName()); methodKey.setParameTypes(param.getArgTypes()); SqlMethod sqlMethod = neo4jDaoInfo.getSqlMethods().get(methodKey); Optional<Sql> query = sqlCompute.compute(sqlMethod.getDefinition() , neo4jDaoInfo , param.getArgs()); if(!query.isPresent()) throw new Neo4jException("No Query find out"); Sql sql = query.get();
static final boolean shouldProcess(SqlMethod sqlMethod) { GenericNode returnTypeInfo = sqlMethod.getSqlMethodRet().getReturnTypeInfo(); if(returnTypeInfo.getDeclareType().isAssignableFrom(CompletionStage.class)) { GenericNode node = returnTypeInfo.getComponentTypes().get(0); Class<?> declareType = node.getDeclareType(); if(declareType.isAssignableFrom(PageReturn.class)) { for(Class<?> paramType : sqlMethod.getDefinition().getParameterTypes()) { if(paramType.isAssignableFrom(Pageable.class)) return true; } } } return false; } }
static final boolean shouldProcess(SqlMethod sqlMethod) { GenericNode returnTypeInfo = sqlMethod.getSqlMethodRet().getReturnTypeInfo(); if(returnTypeInfo.getDeclareType().isAssignableFrom(CompletionStage.class)) { GenericNode node = returnTypeInfo.getComponentTypes().get(0); Class<?> declareType = node.getDeclareType(); if(declareType.isAssignableFrom(Void.class)) { return true; } } return false; } }
@Override public CompletionStage<?> deSerialize(StatementResultCursor src , SqlMethod sqlMethod) throws NotSupportConvertException { throw new NotSupportConvertException(String.format("Not support return type is %s" , sqlMethod.getDefinition().getReturnType().getName())); }
static final boolean shouldProcess(SqlMethod sqlMethod) { GenericNode returnTypeInfo = sqlMethod.getSqlMethodRet().getReturnTypeInfo(); if(returnTypeInfo.getDeclareType().isAssignableFrom(CompletionStage.class)) { GenericNode node = returnTypeInfo.getComponentTypes().get(0); Class<?> declareType = node.getDeclareType(); if(declareType.isAssignableFrom(List.class)) { return true; } } return false; } }
@Override public CompletionStage<?> deSerialize(StatementResultCursor src , SqlMethod sqlMethod) throws NotSupportConvertException { com.github.andyshao.neo4j.annotation.DeSerializer annotation = sqlMethod.getDefinition().getAnnotation(com.github.andyshao.neo4j.annotation.DeSerializer.class); if(annotation == null) return next.deSerialize(src , sqlMethod); DeSerializer deS = cache.computeIfAbsent(annotation , key -> ConstructorOperation.newInstance(key.value())); return deS.deSerialize(src , sqlMethod); }
public static final Class<?> getReturnType(SqlMethod sqlMethod){ GenericNode node = sqlMethod.getSqlMethodRet().getReturnTypeInfo(); List<GenericNode> nodes = node.getComponentTypes(); GenericNode tmp = nodes.get(0); Class<?> declareType = tmp.getDeclareType(); tmp = tmp.getComponentTypes().get(0); declareType = tmp.getDeclareType(); return declareType; }
static final boolean shouldProcess(SqlMethod sqlMethod) { GenericNode returnTypeInfo = sqlMethod.getSqlMethodRet().getReturnTypeInfo(); if(returnTypeInfo.getDeclareType().isAssignableFrom(CompletionStage.class)) { GenericNode node = returnTypeInfo.getComponentTypes().get(0); Class<?> declareType = node.getDeclareType(); if(declareType.isAssignableFrom(Optional.class)) { node = node.getComponentTypes().get(0); declareType = node.getDeclareType(); return !DeSerializers.isBaseType(declareType); } } return false; } }
@Override public CompletionStage<?> deSerialize(StatementResultCursor src , SqlMethod sqlMethod) throws NotSupportConvertException { GenericNode returnTypeInfo = sqlMethod.getSqlMethodRet().getReturnTypeInfo(); if(!shouldProcess(returnTypeInfo)) return next.deSerialize(src , sqlMethod); Class<?> returnType = getReturnType(returnTypeInfo); return src.nextAsync().thenApplyAsync(record -> Optional.ofNullable(DeSerializers.formatValue(returnType , record.get(0)))); }