-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ActiveJDBC: Review the architecture for converters #1242
Comments
related issue: #1241 |
I just did some dirty hack to make ActiveJDBC can accept an encrypted object for query parameters and return encrypted object too from result set. public class NameEnc {
private String encrypted;
...
public void setEncrypted(String s) { ... }
public String getEncrypted() { ... }
public static class ActiveConverter implements org.javalite.conversion.Converter<Object, Object> {
public static final ActiveConverter INSTANCE = new ActiveConverter();
@Override
public boolean canConvert(Class src, Class dst) {
return true;
}
@Override
public Object convert(Object src) {
if (src == null) {
return null;
}
if (src instanceof NameEnc) {
return ((NameEnc) src).getEncrypted();
} else if (src instanceof String) {
// XXX: ActiveJDBC actually always requires String for VARCHAR column,
// XXX: set() and get() from user code have elements[3] not in ActiveJDBC code,
// XXX: so it returns Encrypted only for user code.
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
boolean internalCall = elements[3].getClassName().startsWith("org.javalite.activejdbc.");
if (internalCall) {
return src;
} else {
NameEnc e = new NameEnc();
e.setEncrypted((String) src);
return e;
}
} else {
throw new UnsupportedOperationException("can't convert from class " + src.getClass());
}
}
}
} My entity class:
|
Possible converter interface: public interface Converter<T> {
// cls is required so that we can share an converter among sub classes of T
T fromDatabase(ResultSet rs, int i, Class<T> cls);
void toDatabase(PreparedStatement ps, int i, T t);
} The objects in get(), set() should be just Java object, they are mapped to JDBC type only when call into JDBC. |
@Dieken, thanks for the idea, but I have a much simpler implementation for what you are doing here. I will publish a blog article on the topic sometime this week, and share it with you. |
Two month passed, just a ping … ;-) |
@Dieken, sorry for the late response. I will get this published in the next 3 days, |
It seems to have a few flaws:
model.set(attr, val)
method calls amodel.get(attr)
, which is not semantically correctget(attr)
as well as inside theset(attr)
model.set("created_at", "10/11/2022");
The text was updated successfully, but these errors were encountered: