mirror of
https://github.com/moparisthebest/JdbcMapper
synced 2024-11-21 08:35:00 -05:00
Handle binding of Enums correctly
This commit is contained in:
parent
cb36e783ed
commit
5c3a88232e
@ -749,7 +749,7 @@ public class JdbcMapperProcessor extends AbstractProcessor {
|
||||
// we are going to put most common ones up top so it should execute faster normally
|
||||
// todo: avoid string concat here
|
||||
if (method == null)
|
||||
if (o.getKind().isPrimitive() || types.isAssignable(o, stringType) || types.isAssignable(o, numberType) || types.isAssignable(o, enumType)) {
|
||||
if (o.getKind().isPrimitive() || types.isAssignable(o, stringType) || types.isAssignable(o, numberType)) {
|
||||
method = "Object";
|
||||
// java.util.Date support, put it in a Timestamp
|
||||
} else if (types.isAssignable(o, utilDateType)) {
|
||||
@ -797,6 +797,9 @@ public class JdbcMapperProcessor extends AbstractProcessor {
|
||||
variableName = variableName + " == null ? null : new java.io.ByteArrayInputStream(" + variableName + ")";
|
||||
} else if (types.isAssignable(o, sqlArrayType)) {
|
||||
method = "Array";
|
||||
} else if (types.isAssignable(o, enumType)) {
|
||||
method = "Object";
|
||||
variableName = variableName + " == null ? null : " + variableName + ".name()";
|
||||
} else {
|
||||
// shouldn't get here ever, if we do the types should be more specific
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@JdbcMapper.SQL could not properly infer PreparedStatement bind call for param", param);
|
||||
|
@ -5,11 +5,14 @@ import com.moparisthebest.jdbc.codegen.JdbcMapperFactory;
|
||||
import com.moparisthebest.jdbc.util.ResultSetIterable;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
//IFJAVA8_START
|
||||
import java.time.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
//IFJAVA8_END
|
||||
|
||||
import static com.moparisthebest.jdbc.TryClose.tryClose;
|
||||
@ -19,6 +22,10 @@ public class QueryMapper implements JdbcMapper {
|
||||
public static final Object noBind = new Object();
|
||||
public static final ResultSetMapper defaultRsm = new ResultSetMapper();
|
||||
|
||||
/*IFJAVA6_START
|
||||
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
IFJAVA6_END*/
|
||||
|
||||
protected final ResultSetMapper cm;
|
||||
protected final Connection conn;
|
||||
protected final boolean closeConn;
|
||||
@ -114,8 +121,10 @@ public class QueryMapper implements JdbcMapper {
|
||||
}
|
||||
|
||||
private static class BlobString extends StringWrapper {
|
||||
private BlobString(String s) {
|
||||
private final Charset charset;
|
||||
private BlobString(final String s, final Charset charset) {
|
||||
super(s);
|
||||
this.charset = charset;
|
||||
}
|
||||
}
|
||||
|
||||
@ -124,7 +133,11 @@ public class QueryMapper implements JdbcMapper {
|
||||
}
|
||||
|
||||
public static Object wrapBlob(String s) {
|
||||
return new BlobString(s);
|
||||
return new BlobString(s, UTF_8);
|
||||
}
|
||||
|
||||
public static Object wrapBlob(final String s, final Charset charset) {
|
||||
return new BlobString(s, charset == null ? UTF_8 : charset);
|
||||
}
|
||||
|
||||
public static void setObject(final PreparedStatement ps, final int index, final Object o) throws SQLException {
|
||||
@ -165,22 +178,20 @@ public class QueryMapper implements JdbcMapper {
|
||||
ps.setBlob(index, (InputStream) o);
|
||||
else if (o instanceof File)
|
||||
try {
|
||||
ps.setBlob(index, new FileInputStream((File) o));
|
||||
ps.setBlob(index, new FileInputStream((File) o)); // todo: does this close this or leak a file descriptor?
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new SQLException("File to Blob FileNotFoundException", e);
|
||||
}
|
||||
else if (o instanceof BlobString)
|
||||
try {
|
||||
ps.setBlob(index, ((BlobString) o).s == null ? null : new ByteArrayInputStream(((BlobString) o).s.getBytes("UTF-8")));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new SQLException("String to Blob UnsupportedEncodingException", e);
|
||||
}
|
||||
ps.setBlob(index, ((BlobString) o).s == null ? null : new ByteArrayInputStream(((BlobString) o).s.getBytes(((BlobString) o).charset)));
|
||||
else if (o instanceof java.sql.Blob)
|
||||
ps.setBlob(index, (java.sql.Blob) o);
|
||||
else if (o instanceof ArrayInList.ArrayListObject)
|
||||
ps.setArray(index, ((ArrayInList.ArrayListObject) o).getArray());
|
||||
else if (o instanceof java.sql.Array)
|
||||
ps.setArray(index, (java.sql.Array) o);
|
||||
else if (o instanceof Enum)
|
||||
ps.setObject(index, ((Enum)o).name());
|
||||
else
|
||||
ps.setObject(index, o); // probably won't get here ever, but just in case...
|
||||
/*
|
||||
|
Loading…
Reference in New Issue
Block a user