Skip to content

Instantly share code, notes, and snippets.

@FrantisekGazo
Created March 16, 2016 15:15
Show Gist options
  • Save FrantisekGazo/95b454437db089f87277 to your computer and use it in GitHub Desktop.
Save FrantisekGazo/95b454437db089f87277 to your computer and use it in GitHub Desktop.
package eu.f3rog.db;
import com.j256.ormlite.field.FieldType;
import com.j256.ormlite.field.SqlType;
import com.j256.ormlite.field.types.BaseDataType;
import com.j256.ormlite.support.DatabaseResults;
import java.sql.SQLException;
/**
* Class {@link CompositeIdFieldPersister} is used for persisting / retrieving {@link TimeStamp} objects to / from database.
*
* @author Frantisek Gazo
* @version 2015-09-09
*/
public class CompositeIdFieldPersister extends BaseDataType {
private static final CompositeIdFieldPersister instance = new CompositeIdFieldPersister();
private CompositeIdFieldPersister() {
super(SqlType.STRING, new Class<?>[]{TimeStamp.class});
}
public static CompositeIdFieldPersister getSingleton() {
return instance;
}
@Override
public Object javaToSqlArg(FieldType fieldType, Object javaObject) {
if (javaObject == null) {
return null;
} else {
return javaObject.toString();
}
}
@Override
public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos) throws SQLException {
return CompositeId.Builder.buildFrom((String) sqlArg);
}
@Override
public Object resultToSqlArg(FieldType fieldType, DatabaseResults results, int columnPos) throws SQLException {
return results.getString(columnPos);
}
@Override
public Object parseDefaultString(FieldType fieldType, String defaultStr) throws SQLException {
return defaultStr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment