Use a SQL date object for LocalDates (#842)

* Use a SQL date object for LocalDates

* Clean up comment
This commit is contained in:
gbrodman 2020-10-20 15:44:23 -04:00 committed by GitHub
parent 7f87938942
commit 12ec54f56c
12 changed files with 3325 additions and 3278 deletions

View file

@ -75,7 +75,7 @@ public class Spec11ThreatMatch extends ImmutableObject implements Buildable, Sql
String registrarId;
/** Date on which the check was run, on which the domain was flagged as abusive. */
@Column(nullable = false)
@Column(nullable = false, columnDefinition = "date")
LocalDate checkDate;
/** The domain's top-level domain. */

View file

@ -16,23 +16,25 @@ package google.registry.model.reporting;
import com.google.common.collect.ImmutableList;
import google.registry.persistence.transaction.JpaTransactionManager;
import google.registry.util.DateTimeUtils;
import javax.persistence.TemporalType;
import org.joda.time.LocalDate;
/**
* Data access object for {@link google.registry.model.reporting.Spec11ThreatMatch}.
*
* <p>A JpaTransactionManager is passed into each static method because they are called from a BEAM
* pipeline and we don't know where it's coming from.</p>
* pipeline and we don't know where it's coming from.
*/
public class Spec11ThreatMatchDao {
/** Delete all entries with the specified date from the database. */
public static void deleteEntriesByDate(JpaTransactionManager jpaTm, LocalDate date) {
jpaTm.assertInTransaction();
jpaTm
.getEntityManager()
.createQuery("DELETE FROM Spec11ThreatMatch WHERE check_date = :date")
.setParameter("date", date.toString())
.setParameter("date", DateTimeUtils.toSqlDate(date), TemporalType.DATE)
.executeUpdate();
}

View file

@ -14,17 +14,23 @@
package google.registry.persistence.converter;
import google.registry.util.DateTimeUtils;
import java.sql.Date;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import org.joda.time.LocalDate;
import org.joda.time.format.ISODateTimeFormat;
/** JPA converter for {@link LocalDate}. */
/** JPA converter for {@link LocalDate}, to/from {@link Date}. */
@Converter(autoApply = true)
public class LocalDateConverter extends ToStringConverterBase<LocalDate> {
public class LocalDateConverter implements AttributeConverter<LocalDate, Date> {
/** Converts the string (a date in ISO-8601 format) into a LocalDate. */
@Override
public LocalDate convertToEntityAttribute(String columnValue) {
return (columnValue == null) ? null : LocalDate.parse(columnValue, ISODateTimeFormat.date());
public Date convertToDatabaseColumn(LocalDate attribute) {
return attribute == null ? null : DateTimeUtils.toSqlDate(attribute);
}
@Override
public LocalDate convertToEntityAttribute(Date dbData) {
return dbData == null ? null : DateTimeUtils.toLocalDate(dbData);
}
}

View file

@ -92,15 +92,13 @@ public class Spec11ThreatMatchDaoTest extends EntityTestCase {
}
private Spec11ThreatMatch createThreatMatch(String domainName, LocalDate date) {
Spec11ThreatMatch threatMatch =
new Spec11ThreatMatch()
.asBuilder()
.setThreatTypes(ImmutableSet.of(ThreatType.MALWARE))
.setCheckDate(date)
.setDomainName(domainName)
.setRegistrarId("Example Registrar")
.setDomainRepoId("1-COM")
.build();
return threatMatch;
return new Spec11ThreatMatch()
.asBuilder()
.setThreatTypes(ImmutableSet.of(ThreatType.MALWARE))
.setCheckDate(date)
.setDomainName(domainName)
.setRegistrarId("Example Registrar")
.setDomainRepoId("1-COM")
.build();
}
}