Allow to manually specify columnNames and isSelect for unparseable SQL queries in jdbcmapper

This commit is contained in:
Travis Burtrum 2017-08-04 13:55:05 -04:00
parent c1b8b2e705
commit 6f23696952
5 changed files with 65 additions and 1 deletions

View File

@ -58,6 +58,16 @@ public interface JdbcMapper extends Closeable {
*/ */
String value(); String value();
/**
* This avoids parsing SQL for select statements by specifying column names directly, implies SELECT statement, ie isSelect = true
*/
String[] columnNames() default {};
/**
* This avoids parsing SQL for non-select statements, set to false if executeUpdate should be ran instead of selection
*/
boolean isSelect() default true;
/** /**
* This defaults to the value of the class-level @JdbcMapper.Mapper.cachePreparedStatements annotation, but can be configured on a per-method level here * This defaults to the value of the class-level @JdbcMapper.Mapper.cachePreparedStatements annotation, but can be configured on a per-method level here
*/ */

View File

@ -322,7 +322,7 @@ public class JdbcMapperProcessor extends AbstractProcessor {
} }
} }
final SQLParser parsedSQl = parser.parse(sqlStatement); final SQLParser parsedSQl = ManualSQLParser.getSQLParser(sql, parser, sqlStatement);
// now implementation // now implementation
w.write("\t\tPreparedStatement ps = null;\n"); w.write("\t\tPreparedStatement ps = null;\n");
if (parsedSQl.isSelect()) if (parsedSQl.isSelect())

View File

@ -0,0 +1,32 @@
package com.moparisthebest.jdbc.codegen;
/**
* Created by mopar on 5/25/17.
*/
class ManualSQLParser extends AbstractSQLParser {
private static final SQLParser notSelect = new ManualSQLParser(null, false);
private ManualSQLParser(final String[] columnNames, final boolean isSelect) {
super(columnNames, isSelect);
}
public static SQLParser getSQLParser(final JdbcMapper.SQL sql, final SQLParser parser, final String sqlStatement) {
if(sql.columnNames().length != 0) {
final String[] newColumnNames = new String[sql.columnNames().length + 1];
int count = 0;
for (final String columnName : sql.columnNames())
newColumnNames[++count] = columnName.toUpperCase();
return new ManualSQLParser(newColumnNames, true);
} else if(!sql.isSelect()) {
return notSelect;
} else {
return parser.parse(sqlStatement);
}
}
@Override
public SQLParser parse(final String sql) {
throw new UnsupportedOperationException("use getDefinedColumns()");
}
}

View File

@ -57,9 +57,20 @@ public interface PersonDAO extends JdbcMapper {
@JdbcMapper.SQL(value = "SELECT first_name, last_name FROM person WHERE last_name = {lastName}", cachePreparedStatement = JdbcMapper.OptionalBool.TRUE) @JdbcMapper.SQL(value = "SELECT first_name, last_name FROM person WHERE last_name = {lastName}", cachePreparedStatement = JdbcMapper.OptionalBool.TRUE)
ResultSet getPeopleResultSetCached(String lastName) throws SQLException; ResultSet getPeopleResultSetCached(String lastName) throws SQLException;
@JdbcMapper.SQL(value = "SELECT first_name FROM person WHERE person_no = {personNo}", columnNames = {"firstName"})
String getFirstNameColumnNames(long personNo) throws SQLException;
@JdbcMapper.SQL("SELECT first_name FROM person WHERE person_no = {personNo}") @JdbcMapper.SQL("SELECT first_name FROM person WHERE person_no = {personNo}")
String getFirstName(long personNo) throws SQLException; String getFirstName(long personNo) throws SQLException;
@JdbcMapper.SQL(value = "SELECT first_name FROM person WHERE person_no = {personNo}", isSelect = false)
int getFirstNameUpdate(long personNo) throws SQLException;
@JdbcMapper.SQL(value = "SELECT person_no, first_name, last_name, birth_date FROM person WHERE person_no = {personNo}",
columnNames = {"personNo", "firstName", "lastName", "birthDate"})
FieldPerson getPersonColumnNames(long personNo) throws SQLException;
@JdbcMapper.SQL(value = "SELECT person_no, first_name, last_name, birth_date FROM person WHERE person_no = {personNo}") @JdbcMapper.SQL(value = "SELECT person_no, first_name, last_name, birth_date FROM person WHERE person_no = {personNo}")
FieldPerson getPerson(long personNo) throws SQLException; FieldPerson getPerson(long personNo) throws SQLException;

View File

@ -57,9 +57,20 @@ public interface PrestoPersonDAO extends PersonDAO {
@JdbcMapper.SQL(value = "SELECT first_name, last_name FROM person WHERE last_name = {lastName}", cachePreparedStatement = JdbcMapper.OptionalBool.TRUE) @JdbcMapper.SQL(value = "SELECT first_name, last_name FROM person WHERE last_name = {lastName}", cachePreparedStatement = JdbcMapper.OptionalBool.TRUE)
ResultSet getPeopleResultSetCached(String lastName) throws SQLException; ResultSet getPeopleResultSetCached(String lastName) throws SQLException;
@JdbcMapper.SQL(value = "SELECT first_name FROM person WHERE person_no = {personNo}", columnNames = {"firstName"})
String getFirstNameColumnNames(long personNo) throws SQLException;
@JdbcMapper.SQL("SELECT first_name FROM person WHERE person_no = {personNo}") @JdbcMapper.SQL("SELECT first_name FROM person WHERE person_no = {personNo}")
String getFirstName(long personNo) throws SQLException; String getFirstName(long personNo) throws SQLException;
@JdbcMapper.SQL(value = "SELECT first_name FROM person WHERE person_no = {personNo}", isSelect = false)
int getFirstNameUpdate(long personNo) throws SQLException;
@JdbcMapper.SQL(value = "SELECT person_no, first_name, last_name, birth_date FROM person WHERE person_no = {personNo}",
columnNames = {"personNo", "firstName", "lastName", "birthDate"})
FieldPerson getPersonColumnNames(long personNo) throws SQLException;
@JdbcMapper.SQL(value = "SELECT person_no, first_name, last_name, birth_date FROM person WHERE person_no = {personNo}") @JdbcMapper.SQL(value = "SELECT person_no, first_name, last_name, birth_date FROM person WHERE person_no = {personNo}")
FieldPerson getPerson(long personNo) throws SQLException; FieldPerson getPerson(long personNo) throws SQLException;