Skip to content

Commit

Permalink
fix #1 support unnamed parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhoss committed Apr 2, 2021
1 parent 731144d commit 012cd50
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
@FunctionalInterface
public interface SqlFileParser {

/** The regex to extract parameters out of SQL statements. */
String PARAMETER_REGEX = "(?<!')(:[\\w]*)(?!')";
/** The regex to extract named parameters out of SQL statements. */
String NAMED_PARAMETER_REGEX = "(?<!')(:[\\w]*)(?!')";
String PARAMETER_REGEX = "\\?";

/** The pattern to extract parameters out of SQL statements */
/** The pattern to extract named parameters out of SQL statements */
Pattern NAMED_PARAMETER_PATTERN = Pattern.compile(NAMED_PARAMETER_REGEX);
Pattern PARAMETER_PATTERN = Pattern.compile(PARAMETER_REGEX);

/**
Expand All @@ -39,13 +41,21 @@ public interface SqlFileParser {
Stream<SqlStatement> parse(Path pathToFile);

/**
* Extracts parameter indices from a given SQL statement.
*
* @param sqlStatement
* The SQL statement to use.
* The raw SQL statement to parse.
* @return Extracted parameters and their indices.
*/
default Map<String, List<Integer>> extractParameterIndices(final String sqlStatement) {
final var namedIndices = parseNamedParameters(sqlStatement);
namedIndices.putAll(parseParameters(sqlStatement));
return Map.copyOf(namedIndices);
}

private Map<String, List<Integer>> parseNamedParameters(final String sqlStatement) {
final Map<String, List<Integer>> indices = new LinkedHashMap<>();
final Matcher matcher = PARAMETER_PATTERN.matcher(sqlStatement);
final Matcher matcher = NAMED_PARAMETER_PATTERN.matcher(sqlStatement);
int counter = 1;
while (matcher.find()) {
final String parameterName = matcher.group().substring(1);
Expand All @@ -54,4 +64,14 @@ default Map<String, List<Integer>> extractParameterIndices(final String sqlState
return indices;
}

private Map<String, List<Integer>> parseParameters(final String sqlStatement) {
final Map<String, List<Integer>> indices = new LinkedHashMap<>();
final Matcher matcher = PARAMETER_PATTERN.matcher(sqlStatement);
int counter = 1;
while (matcher.find()) {
indices.computeIfAbsent("unnamedParameters", string -> new ArrayList<>(3)).add(counter++);
}
return indices;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private FieldSpec asConstantSqlField(final SqlStatement sqlStatement) {
}

private static String replaceNamedParameters(final String rawSqlStatement) {
return rawSqlStatement.replaceAll(SqlFileParser.PARAMETER_PATTERN.pattern(), "?");
return rawSqlStatement.replaceAll(SqlFileParser.NAMED_PARAMETER_PATTERN.pattern(), "?");
}

private FieldSpec asConstantSqlParameterIndexField(final SqlStatement sqlStatement) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* This file is part of yosql. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of yosql,
* including this file, may be copied, modified, propagated, or distributed except according to the terms contained
* in the LICENSE file.
*/

-- returningMode: ONE
select *
from users
where name = ?
and id = :userId
;

0 comments on commit 012cd50

Please sign in to comment.