/** * Returns a copy of this bundle with a different configuration. * <p> * This does not reload the underlying resources. * * @param config the new config, not null * @return a bundle with the config updated, not null */ SqlFragments withConfig(ElSqlConfig config) { return new SqlFragments(_map, config); }
/** * Gets the configuration object. * * @return the config, not null */ public ElSqlConfig getConfig() { return _fragments.getConfig(); }
/** * Finds SQL for a named fragment key, without specifying parameters. * <p> * This finds, processes and returns a named block from the bundle. * Note that if the SQL contains tags that depend on variables, like AND or LIKE, * then an error will be thrown. * * @param name the name, not null * @return the SQL, not null * @throws IllegalArgumentException if there is no fragment with the specified name * @throws RuntimeException if a problem occurs */ public String getSql(String name) { return _fragments.getSql(name, EmptySqlParams.INSTANCE); }
static SqlFragments parseResource(URL[] resources, ElSqlConfig config) { List<List<String>> files = new ArrayList<List<String>>(); boolean resourceFound = false; for (URL resource : resources) { if (resource != null) { resourceFound = true; List<String> lines = loadResource(resource); files.add(lines); } } if (!resourceFound) { throw new IllegalArgumentException("No matching resource was found"); } return parse(files, config); }
/** * Returns a copy of this bundle with a different configuration. * <p> * This does not reload the underlying resources. * * @param config the new config, not null * @return a bundle with the config updated, not null */ public ElSqlBundle withConfig(ElSqlConfig config) { return new ElSqlBundle(_fragments.withConfig(config)); }
/** * Finds SQL for a named fragment key. * <p> * This finds, processes and returns a named block from the bundle. * * @param name the name, not null * @param params the Spring SQL parameters, not null * @return the SQL, not null * @throws IllegalArgumentException if there is no fragment with the specified name * @throws RuntimeException if a problem occurs */ String getSql(String name, SqlParams params) { if (name == null) { throw new IllegalArgumentException("Fragment name must not be null"); } if (params == null) { throw new IllegalArgumentException("SqlParams must not be null"); } NameSqlFragment fragment = getFragment(name); StringBuilder buf = new StringBuilder(1024); fragment.toSQL(buf, this, params, NO_LOOP); return buf.toString(); }
static SqlFragments parse(List<String> lines) { ArrayList<List<String>> files = new ArrayList<List<String>>(); files.add(lines); return parse(files, ElSqlConfig.DEFAULT); }
/** * Parses a bundle from a resource locating a file, specify the config. * <p> * This parses a list of resources, expressed as {@code URL}s. * Named blocks in later resources override blocks with the same name in earlier resources. * <p> * The config is designed to handle some, but not all, database differences. * Other differences are handled via the override resources passed in. * <p> * Each resource is a {@link URL}. A null URL is permitted and ignored. * This allows classpath resources, obtained from {@link Class#getResource(String)} * or {@link ClassLoader#getResource(String)} to be called and passed in directly * as those methods return null when the target does not exist. * * @param config the config to use, not null * @param resources the resources to load, not null, may contain nulls which are ignored * @return the external identifier, not null * @throws IllegalArgumentException if the input cannot be parsed or if none of the resources exists */ public static ElSql parse(ElSqlConfig config, URL... resources) { if (config == null) { throw new IllegalArgumentException("Config must not be null"); } if (resources == null) { throw new IllegalArgumentException("Resources must not be null"); } return new ElSql(SqlFragments.parseResource(resources, config)); }
private static ElSqlBundle parseResource(Resource[] resources, ElSqlConfig config) { List<List<String>> files = new ArrayList<List<String>>(); boolean resourceFound = false; for (Resource resource : resources) { if (resource.exists()) { resourceFound = true; URL url; try { url = resource.getURL(); } catch (IOException ex) { throw new RuntimeException(ex); } List<String> lines = SqlFragments.loadResource(url); files.add(lines); } } if (!resourceFound) { throw new IllegalArgumentException("No matching resource was found"); } return new ElSqlBundle(SqlFragments.parse(files, config)); }
/** * Returns a copy of this bundle with a different configuration. * <p> * This does not reload the underlying resources. * * @param config the new config, not null * @return a bundle with the config updated, not null */ public ElSql withConfig(ElSqlConfig config) { return new ElSql(_fragments.withConfig(config)); }
@Override void toSQL(StringBuilder buf, SqlFragments fragments, SqlParams params, int[] loopIndex) { String key = _includeKey; if (key.startsWith(":")) { String var = extractVariableName(key); if (params.contains(var)) { key = params.get(var).toString(); } } NameSqlFragment unit = fragments.getFragment(key); unit.toSQL(buf, fragments, params, loopIndex); }
/** * Gets the configuration object. * * @return the config, not null */ public ElSqlConfig getConfig() { return _fragments.getConfig(); }
/** * Finds SQL for a named fragment key, without specifying parameters. * <p> * This finds, processes and returns a named block from the bundle. * Note that if the SQL contains tags that depend on variables, like AND or LIKE, * then an error will be thrown. * * @param name the name, not null * @return the SQL, not null * @throws IllegalArgumentException if there is no fragment with the specified name * @throws RuntimeException if a problem occurs */ public String getSql(String name) { return _fragments.getSql(name, EmptySqlParams.INSTANCE); }
static SqlFragments parse(List<List<String>> files, ElSqlConfig config) { Map<String, NameSqlFragment> parsed = new LinkedHashMap<String, NameSqlFragment>(); for (List<String> lines : files) { ElSqlParser parser = new ElSqlParser(lines); parsed.putAll(parser.parse()); } return new SqlFragments(parsed, config); }
@Override void toSQL(StringBuilder buf, SqlFragments fragments, SqlParams params, int[] loopIndex) { String var = applyLoopIndex(_variable, loopIndex); Object val = params.get(var); if (val == null) { buf.append("IS NULL "); } else { if (fragments.getConfig().isLikeWildcard(val.toString())) { buf.append("LIKE "); super.toSQL(buf, fragments, params, loopIndex); buf.append(fragments.getConfig().getLikeSuffix()); } else { buf.append("= "); super.toSQL(buf, fragments, params, loopIndex); } } }
/** * Finds SQL for a named fragment key, providing the SQL parameters. * <p> * This finds, processes and returns a named block from the bundle. * The parameters are used to provide intelligent processing of SQL based on * the actual data in the request. * <p> * See {@link MapSqlParams} and {@link SpringSqlParams}. * * @param name the name, not null * @param params the SQL parameters, not null * @return the SQL, not null * @throws IllegalArgumentException if there is no fragment with the specified name * @throws RuntimeException if a problem occurs */ public String getSql(String name, SqlParams params) { return _fragments.getSql(name, params); }
@Override void toSQL(StringBuilder buf, SqlFragments fragments, SqlParams params, int[] loopIndex) { int offset = extractVariableOrLiteral(params, _offsetVariable); int fetchLimit = extractVariableOrLiteral(params, _fetchVariable); buf.append(fragments.getConfig().getPaging(offset, fetchLimit == Integer.MAX_VALUE ? 0 : fetchLimit)); }
/** * Finds SQL for a named fragment key. * <p> * This finds, processes and returns a named block from the bundle. * The parameters are used to provide intelligent processing of SQL based on * the actual data in the request. * * @param name the name, not null * @param paramSource the Spring SQL parameters, not null * @return the SQL, not null * @throws IllegalArgumentException if there is no fragment with the specified name * @throws RuntimeException if a problem occurs */ public String getSql(String name, SqlParameterSource paramSource) { return _fragments.getSql(name, new SpringSqlParams(paramSource)); }
/** * Applies the paging. * * @param selectToPage the contents of the enclosed block, not null * @param fragments the SQL fragments for context, not null * @param params the SQL arguments, not null */ String applyPaging(String selectToPage, SqlFragments fragments, SqlParams params) { int offset = extractVariableOrLiteral(params, _offsetVariable); int fetchLimit = extractVariableOrLiteral(params, _fetchVariable); return fragments.getConfig().addPaging(selectToPage, offset, fetchLimit == Integer.MAX_VALUE ? 0 : fetchLimit); }
/** * Finds SQL for a named fragment key, providing a map of SQL parameters. * <p> * This finds, processes and returns a named block from the bundle. * The parameters are used to provide intelligent processing of SQL based on * the actual data in the request. * * @param name the name, not null * @param params the SQL parameters, not null * @return the SQL, not null * @throws IllegalArgumentException if there is no fragment with the specified name * @throws RuntimeException if a problem occurs */ public String getSql(String name, Map<String, Object> params) { return _fragments.getSql(name, new MapSqlParams(params)); }