Created
July 6, 2018 03:29
-
-
Save esivres/b0afbd47480a45c780ae3988bcffd9e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String[] getExpressions(String query,StringBuilder resultQuery) throws QueryHelperException { | |
int blockStart = 0; | |
int blockEnd = 0; | |
boolean atArg = false; | |
char[] array = query.toCharArray(); | |
ArrayList<String> arguments = new ArrayList<>(); | |
for (int i = 0; i < array.length; i++) { | |
if (!atArg) { | |
if (array[i] == ':' && ((i - 1 >= 0 && array[i - 1] != '\\') || i == 0)) { | |
if (blockStart != blockEnd) | |
resultQuery.append(array, blockStart, blockEnd - blockStart); | |
atArg = true; | |
blockStart = i + 1; | |
} | |
} else { | |
if (!Character.isDigit(array[i]) && !Character.isLetter(array[i]) && array[i] != '.') { | |
if (blockStart >= blockEnd) { | |
throw new QueryHelperException( | |
"expression:'" + query + "' not valid check at position:" + i + " near:[" + new String( | |
array, i - 5 > 0 ? i - 5 : 0, i + 5 < array.length ? 5 : array.length - i) + "]"); | |
} | |
atArg = false; | |
resultQuery.append('?').append(array[i]); | |
arguments.add(new String(array, blockStart, blockEnd - blockStart)); | |
blockStart = i + 1; | |
} | |
} | |
blockEnd = i + 1; | |
} | |
if (blockStart != blockEnd) { | |
if (atArg) { | |
resultQuery.append('?'); | |
arguments.add(new String(array, blockStart, blockEnd - blockStart)); | |
} else if (blockEnd - blockStart + 1 > 0) { | |
resultQuery.append(array, blockStart, blockEnd - blockStart); | |
} | |
return arguments.toArray(new String[] {}); | |
}else{ | |
throw new QueryHelperException("expression: [ "+query+" ], not valid"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment