-
Notifications
You must be signed in to change notification settings - Fork 229
Implementing and using custom converters
Stefano Negri edited this page Dec 18, 2018
·
8 revisions
If you need to customize a datatype mapping for some reason, you can create your own Mapper implementation
Hint: if you have an updated JDBC driver you don't need to do this by hand as mapping to java.time.LocalDate
should work out of the box.
package org.example;
...
public class LocalDateConverter implements Converter<LocalDate> {
@Override
public LocalDate convert(final Object val) throws ConverterException {
if (val instanceof java.sql.Date) {
return ((java.sql.Date) val).toLocalDate();
} else {
return null;
}
}
@Override
public Object toDatabaseParam(final LocalDate val) {
if (val == null) {
return null;
} else {
return new java.sql.Date(val.atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli());
}
}
}
package org.example;
import org.sql2o.converters.Converter;
import org.sql2o.converters.ConvertersProvider;
import java.util.Map;
public class Sql2oConvertersProvider implements ConvertersProvider {
@Override
public void fill(Map<Class<?>, Converter<?>> mapToFill) {
mapToFill.put(LocalDate.class, new LocalDateConverter());
}
}
As Sql2o use the standard Java service loader mechanism, enabling a new Converter is easy:
- In the META-INF/services folder in your classpath (src/main/resources/META-INF/services for Maven-based projects) add an empty text file called
org.sql2o.converters.ConvertersProvider
- Add the name of your custom converter provider class to a single line followed by a line-feed. For the example above, we would add:
org.example.Sql2oConvertersProvider
- Inititialize Sql2o in a standard manner:
new Sql2o(datasource)
- Your custom Converter implementation will used for converting
java.sql.Date
tojava.time.LocalDate
fields Note: you may add multiple Converters to theorg.sql2o.converters.ConvertersProvider
-file
Add a line like provides org.sql2o.converters.ConvertersProvider with org.example.Sql2oConvertersProvider;
to your module-info.java
Example:
final Map<Class, Converter> mappers = new HashMap<>();
mappers.put(LocalDate.class, new LocalDateConverter());
final Sql2o database = new Sql2o(embeddedDatabaseRule.getDataSource(),
new NoQuirks(mappers));
Contributing
Documentation
- Configuration
- Fetching data from database
- Fetching data lazily
- Column mappings
- Updates and inserts
- Transactions
- Running queries in a batch
- Integration with Spring Framework
- Register custom converters
- Sql2o on Android
Other resources