mirror of
https://github.com/moparisthebest/JdbcMapper
synced 2024-11-21 16:45:02 -05:00
Allow to manually specify columnNames and isSelect for unparseable SQL queries in jdbcmapper
This commit is contained in:
parent
c1b8b2e705
commit
6f23696952
@ -58,6 +58,16 @@ public interface JdbcMapper extends Closeable {
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
@ -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
|
||||
w.write("\t\tPreparedStatement ps = null;\n");
|
||||
if (parsedSQl.isSelect())
|
||||
|
@ -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()");
|
||||
}
|
||||
}
|
@ -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)
|
||||
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}")
|
||||
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}")
|
||||
FieldPerson getPerson(long personNo) throws SQLException;
|
||||
|
||||
|
@ -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)
|
||||
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}")
|
||||
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}")
|
||||
FieldPerson getPerson(long personNo) throws SQLException;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user