int qmarkIndex = 1;
for (int j = 0; j < totalTokens; j++) {
String token = tokens[j];
if (token.startsWith("?")) {
JdbcStatementParameter param = new JdbcStatementParameter();
param.setIndex(qmarkIndex);
param.setName(getNameFromToken(qmarkIndex, token));
st.addParameter(param);
qmarkIndex = qmarkIndex + 1;
//get inline sql data type
int sqlDataType = getInlineSqlDataTypeFromToken(token);
if (sqlDataType != JdbcStatementParameter.UNKNOWN_SQL_DATA_TYPE) {
param.setSqlDataType(sqlDataType);
continue;//no need to parse
}
String columnName = "";
String tableName = "";
int dotPosition = token.indexOf('.');
if (dotPosition != -1) {
String[] results = getTableAndColumnFromDottedToken(token);
if (results != null) {
tableName = results[0];
columnName = results[1];
param.setColumnName(columnName);
param.setTableName(tableName);
continue;
}
}
String columnNameToken = "";
if (j>=2 && "BETWEEN".equalsIgnoreCase(tokens[j-1])) {
columnNameToken = tokens[j-2];
}
else if (j>=4 && "BETWEEN".equalsIgnoreCase(tokens[j-3]) && "AND".equalsIgnoreCase(tokens[j-1])) {
columnNameToken = tokens[j-4];
}
else if (j>=2 && "COUNT".equalsIgnoreCase(tokens[j-1])) {
columnNameToken = "COUNT(*)";
param.setUsedByCount(true);
}
else if (j>=2 && "COUNT".equalsIgnoreCase(tokens[j-2])) {
columnNameToken = tokens[j-1];
param.setUsedByCount(true);
}
else if (j>=2 && "IN".equalsIgnoreCase(tokens[j-1])) {
columnNameToken = tokens[j-2];
}
else if (j>=2 && "LIKE".equalsIgnoreCase(tokens[j-1])) {
columnNameToken = tokens[j-2];
}
else if (!"?".equals(tokens[j-1])) {
columnNameToken = tokens[j-1];
}
else {
log.warn("Failed to detect column name for ? with index " + qmarkIndex +
", suggest to use inline sql type declaration if " +
"the underlying database doesn't support ParameterMetaData.");
continue;
}
dotPosition = columnNameToken.indexOf('.');
if (dotPosition == -1) {
columnName = columnNameToken;
tableName = getTableName(j, tokens, columnName);
}
else {
columnName = columnNameToken.substring(dotPosition + 1, columnNameToken.length());
tableName = columnNameToken.substring(0, dotPosition);
}
//set the found column and table names
if (tableName != null) {
param.setColumnName(columnName);
param.setTableName(tableName);
}
}
}
}