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); }
/** * Extracts a variable from the input. * * @param text the text to parse, may be null * @return the variable, null if none */ private String extractVariable(String text) { if (text == null) { return null; } text = text.trim(); if (text.startsWith("=")) { return extractVariable(text.substring(1)); } return text; }
/** * Parses the top-level named sections. */ private void parseNamedSections() { ContainerSqlFragment containerFragment = new ContainerSqlFragment(); parseContainerSection(containerFragment, _lines.listIterator(), -1); }
/** * Parse the input returning the named sections. * * @return the map of named sections, keyed by name, not null */ Map<String, NameSqlFragment> parse() { rejectTabs(); parseNamedSections(); return _namedFragments; }
parseIncludeTag(container, line); parseOperatorTag(container, line, "@LIKE"); parseOperatorTag(container, line, "@EQUALS"); parseOffsetFetchTag(container, line); parseFetchTag(container, line); parseValueTag(container, line);
parseContainerSection(nameFragment, lineIterator, line.indent()); if (nameFragment.getFragments().size() == 0) { throw new IllegalArgumentException("@NAME found with no subsequent indented lines: " + line); parseContainerSection(whereFragment, lineIterator, line.indent()); if (whereFragment.getFragments().size() == 0) { throw new IllegalArgumentException("@PAGING found with no subsequent indented lines: " + line); parseContainerSection(whereFragment, lineIterator, line.indent()); if (whereFragment.getFragments().size() == 0) { throw new IllegalArgumentException("@WHERE found with no subsequent indented lines: " + line); AndSqlFragment andFragment = new AndSqlFragment(andMatcher.group(1), extractVariable(andMatcher.group(2))); parseContainerSection(andFragment, lineIterator, line.indent()); if (andFragment.getFragments().size() == 0) { throw new IllegalArgumentException("@AND found with no subsequent indented lines: " + line); OrSqlFragment orFragment = new OrSqlFragment(orMatcher.group(1), extractVariable(orMatcher.group(2))); parseContainerSection(orFragment, lineIterator, line.indent()); if (orFragment.getFragments().size() == 0) { throw new IllegalArgumentException("@OR found with no subsequent indented lines: " + line); IfSqlFragment ifFragment = new IfSqlFragment(ifMatcher.group(1), extractVariable(ifMatcher.group(2))); parseContainerSection(ifFragment, lineIterator, line.indent()); if (ifFragment.getFragments().size() == 0) { throw new IllegalArgumentException("@IF found with no subsequent indented lines: " + line); parseLine(container, line); } else {
/** * Parse INCLUDE tag. * <p> * This tag can appear anywhere in a line. * It substitutes the entire content of the named section in at this point. * The text before is treated as simple text. * The text after is parsed. * * @param container the container to add to, not null * @param line the line to parse, not null */ private void parseIncludeTag(ContainerSqlFragment container, Line line) { Line[] split = line.split(line.lineTrimmed().indexOf("@INCLUDE")); parseLine(container, split[0]); String trimmed = split[1].lineTrimmed(); Matcher matcher = INCLUDE_PATTERN.matcher(trimmed); if (matcher.matches() == false) { throw new IllegalArgumentException("@INCLUDE found with invalid format: " + line); } IncludeSqlFragment includeFragment = new IncludeSqlFragment(matcher.group(1)); container.addFragment(includeFragment); Line subLine = split[1].splitRemainder(matcher.start(2)); parseLine(container, subLine); }
/** * Parse VALUE tag. * <p> * This tag can appear anywhere in a line. * The text before is treated as simple text. * The text after is parsed. * * @param container the container to add to, not null * @param line the line to parse, not null */ private void parseValueTag(ContainerSqlFragment container, Line line) { Line[] split = line.split(line.lineTrimmed().indexOf("@VALUE")); parseLine(container, split[0]); Line lineStartingAtTag = split[1]; Matcher matcher = VALUE_PATTERN.matcher(lineStartingAtTag.lineTrimmed()); if (matcher.matches() == false) { throw new IllegalArgumentException("@VALUE found with invalid format: " + line); } String followingSpaces = matcher.group(2); Line subLine = lineStartingAtTag.splitRemainder(matcher.start(3)); boolean followWithSpace = (followingSpaces != null && followingSpaces.length() > 0) || subLine.lineTrimmed().isEmpty(); ValueSqlFragment valueFragment = new ValueSqlFragment(matcher.group(1), followWithSpace); container.addFragment(valueFragment); parseLine(container, subLine); }