/** * 执行execute * * @param context * @param model * @param execute */ private static void execute(Context context, Model model, Execute execute) { String modelName = model.getModelName(); String currentPath = execute.getCurrentPath(); LogUtil.info( "Execute info (modelName:" + modelName + ", executeName:" + execute.getName() + ")"); // 把model、currentPath对象放入到 当前线程里 context.setCurrentPath(currentPath); context.setCurrentModel(modelName); // 设想sql查询参数 Collection<Param> params = execute.getParams().values(); for (Param nfParam : params) { setParam(nfParam, context, currentPath); } execute.execute(context, model.getConnectionProvide(context)); // 执行 }
/** * 查询 可以指定当前路径、是否自动分页、返回对象的查询 * * @param context * @param modelName * @param queryName * @param currentPath * @param autoPaging * @param obect * @return */ public static <T> QueryResponseObject<T> query( Context context, String modelName, String queryName, String currentPath, boolean autoPaging, Class<T> obect) { LogUtil.info("-----------------------query begin--------------------------------------"); try { if (queryName == null || "".equals(queryName)) queryName = "_default"; LogUtil.info("Query info (modelName:" + modelName + ", queryName:" + queryName + ")"); Model model = context.getModel(modelName); Query query = model.getQuery(queryName); if (query == null) { throw new QueryNotFoundException( "can not found Query:" + queryName + ", on Model:" + modelName); } if (context.isExport) { context.queryLimitSize = context.reportQueryLimitSize; } query.setCurrentPath(currentPath); // 初始化查询参数param Collection<Param> params = query.getParams().values(); for (Param nfParam : params) { setParam(nfParam, context, query.getCurrentPath()); } // 初始化查询过滤参数filter Collection<Filter> filters = query.getFilters().values(); for (Filter filter : filters) { setParam(filter, context, query.getCurrentPath()); Object value = filter.getValue(); if (value != null) { if (value instanceof String && !"".equals(value)) { filter.setActive(true); query.getParams().put(filter.getName(), filter); } else if (value instanceof Integer && (Integer) value != 0) { filter.setActive(true); query.getParams().put(filter.getName(), filter); } else if (value instanceof Long && (Long) value != 0) { filter.setActive(true); query.getParams().put(filter.getName(), filter); } else { filter.setActive(true); query.getParams().put(filter.getName(), filter); } } else if (filter.isActive()) { query.getParams().put(filter.getName(), filter); } } // 设想分页参数 long start = 0; if (autoPaging) { String startMessage = context.getString("param.start"); if (startMessage != null && startMessage != "") { start = Long.parseLong(startMessage); query.setStartWith(start); } String sizeMessage = context.getString("param.limit"); if (sizeMessage != null && sizeMessage != "") { int size = Integer.parseInt(sizeMessage); query.setPagerSize(size); } } // 查询数据,返回结果 String provideName = model.getConnectionProvide(context); List<T> datas = query.query(context, provideName, obect); QueryResponseObject<T> ro = new QueryResponseObject<T>(); ro.setDatas(datas); int dataSize = datas.size(); int pSize = query.getPagerSize(); if (autoPaging == false || pSize == 0 || (pSize > dataSize && start == 0)) { ro.setTotalCounts(datas.size()); } else { Connection conn = context.getConn(provideName); long totalCounts = query.countItems(conn); ro.setTotalCounts(totalCounts); } ro.setSuccess(true); ro.setMessage("success"); ro.setOutParam(getOutParams(context, query.getParams())); return ro; } finally { context.closeConns(); LogUtil.info("-----------------------query end----------------------------------------"); } }