Skip to content
This repository has been archived by the owner on Feb 5, 2023. It is now read-only.

Commit

Permalink
Adding some support for jsr310 - (spring-)jdbc interaction.
Browse files Browse the repository at this point in the history
Dependency updates.
Some refactorings.
  • Loading branch information
akiraly committed Feb 8, 2015
1 parent 2c38b31 commit e784647
Show file tree
Hide file tree
Showing 32 changed files with 366 additions and 161 deletions.
Expand Up @@ -44,6 +44,8 @@ public PlatformTransactionManager transactionManager(DataSource dataSource) {

@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource, false);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource, false);
jdbcTemplate.setSkipUndeclaredResults(true);
return jdbcTemplate;
}
}
54 changes: 54 additions & 0 deletions db4j/src/main/java/com/github/akiraly/db4j/Jsr310JdbcUtils.java
@@ -0,0 +1,54 @@
/**
* Copyright 2014 Attila Kiraly
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.akiraly.db4j;

import static com.github.akiraly.ver4j.Verify.argNotNull;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

import javax.annotation.Nonnull;

@Nonnull
public abstract class Jsr310JdbcUtils {
private static final TimeZone UTC = TimeZone.getTimeZone(ZoneOffset.UTC);

static final Calendar UTC_CAL = newCalendarBuilder().build();

private Jsr310JdbcUtils() {
}

private static Calendar.Builder newCalendarBuilder() {
return new Calendar.Builder().setTimeZone(UTC).setLocale(Locale.US);
}

public static Calendar toUtcCalendar(LocalDateTime localDateTime) {
argNotNull(localDateTime, "localDateTime");
return newCalendarBuilder().setInstant(
localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli()).build();
}

public static Calendar toUtcCalendar(LocalDate localDate) {
argNotNull(localDate, "localDate");
return newCalendarBuilder().setDate(localDate.getYear(),
localDate.getMonthValue() - 1, localDate.getDayOfMonth())
.build();
}
}
21 changes: 21 additions & 0 deletions db4j/src/main/java/com/github/akiraly/db4j/ResultSets.java
Expand Up @@ -17,8 +17,14 @@

import static com.github.akiraly.ver4j.Verify.argNotNull;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.UUID;

import javax.annotation.Nonnull;
Expand All @@ -34,4 +40,19 @@ public static UUID readBase64Uuid(ResultSet rs, String colName)
argNotNull(colName, "colName"));
return UuidBase64.decode(encodedUuid);
}

public static LocalDateTime readLocalDateTime(ResultSet rs, String colName)
throws SQLException {
Timestamp timestamp = argNotNull(rs, "rs").getTimestamp(
argNotNull(colName, "colName"), Jsr310JdbcUtils.UTC_CAL);
return LocalDateTime.ofInstant(timestamp.toInstant(), ZoneOffset.UTC);
}

public static LocalDate readLocalDate(ResultSet rs, String colName)
throws SQLException {
Date date = argNotNull(rs, "rs").getDate(
argNotNull(colName, "colName"), Jsr310JdbcUtils.UTC_CAL);
return LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()),
ZoneOffset.UTC).toLocalDate();
}
}
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.akiraly.db4j;
package com.github.akiraly.db4j.entity;

import javax.annotation.Nonnull;

Expand Down
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.akiraly.db4j;
package com.github.akiraly.db4j.entity;

import javax.annotation.Nonnull;

Expand Down
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.akiraly.db4j;
package com.github.akiraly.db4j.entity;

import static com.google.common.base.Preconditions.checkNotNull;

Expand All @@ -36,7 +36,7 @@ protected final E find(I id) {
return checkNotNull(doFind(id), "entity with id %s doesn't exists", id);
}

protected final Optional<E> tryFind(I id) {
protected Optional<E> tryFind(I id) {
return Optional.ofNullable(doFind(id));
}

Expand Down
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.akiraly.db4j;
package com.github.akiraly.db4j.entity;

import static com.github.akiraly.db4j.MoreSuppliers.memoizej8;
import static com.github.akiraly.ver4j.Verify.argNotNull;
Expand Down
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.akiraly.db4j;
package com.github.akiraly.db4j.entity;

import static com.github.akiraly.db4j.MoreSuppliers.memoizej8;
import static com.github.akiraly.ver4j.Verify.argNotNull;
Expand Down
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.akiraly.db4j;
package com.github.akiraly.db4j.entity;

import static com.google.common.base.Preconditions.checkNotNull;

Expand All @@ -36,7 +36,7 @@ protected final E find(long id) {
return checkNotNull(doFind(id), "entity with id %s doesn't exists", id);
}

protected final Optional<E> tryFind(long id) {
protected Optional<E> tryFind(long id) {
return Optional.ofNullable(doFind(id));
}

Expand Down
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.akiraly.db4j;
package com.github.akiraly.db4j.entity;

import java.util.UUID;

Expand Down
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.akiraly.db4j;
package com.github.akiraly.db4j.entity;

import java.util.UUID;

Expand Down
Expand Up @@ -13,16 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.akiraly.db4j;
package com.github.akiraly.db4j.entity;

import java.util.UUID;
import java.util.function.Supplier;

import javax.annotation.Nonnull;

import com.github.akiraly.db4j.EntityWithIds.EntityWithSuppliedId;
import com.github.akiraly.db4j.EntityWithIds.SimpleEntityWithId;
import com.github.akiraly.db4j.EntityWithIds.SuppliedEntityWithId;
import com.github.akiraly.db4j.entity.EntityWithIds.EntityWithSuppliedId;
import com.github.akiraly.db4j.entity.EntityWithIds.SimpleEntityWithId;
import com.github.akiraly.db4j.entity.EntityWithIds.SuppliedEntityWithId;

@Nonnull
public abstract class EntityWithUuids {
Expand Down
Expand Up @@ -19,7 +19,7 @@

import javax.annotation.Nonnull;

import com.github.akiraly.db4j.EntityWithLongId;
import com.github.akiraly.db4j.entity.EntityWithLongId;

@Nonnull
public abstract class ACreateUowAware {
Expand Down
Expand Up @@ -19,7 +19,7 @@

import javax.annotation.Nonnull;

import com.github.akiraly.db4j.EntityWithLongId;
import com.github.akiraly.db4j.entity.EntityWithLongId;

@Nonnull
public abstract class AbstractCreateUpdateUowAwarePersistable extends
Expand Down
Expand Up @@ -19,7 +19,7 @@

import javax.annotation.Nonnull;

import com.github.akiraly.db4j.EntityWithLongId;
import com.github.akiraly.db4j.entity.EntityWithLongId;

@Nonnull
public class EntityWithCreateUow<T> {
Expand Down
Expand Up @@ -19,7 +19,7 @@

import javax.annotation.Nonnull;

import com.github.akiraly.db4j.EntityWithLongId;
import com.github.akiraly.db4j.entity.EntityWithLongId;

@Nonnull
public class EntityWithCreateUpdateUow<T> extends EntityWithCreateUow<T> {
Expand Down
Expand Up @@ -25,11 +25,11 @@
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.object.SqlQuery;

import com.github.akiraly.db4j.EntityWithLongId;
import com.github.akiraly.db4j.EntityWithLongIdDao;
import com.github.akiraly.db4j.JdbcTemplateAware;
import com.github.akiraly.db4j.SimpleJdbcInsertBuilder;
import com.github.akiraly.db4j.SqlQueryBuilder;
import com.github.akiraly.db4j.entity.EntityWithLongId;
import com.github.akiraly.db4j.entity.EntityWithLongIdDao;
import com.google.common.collect.ImmutableMap;

@Nonnull
Expand Down
Expand Up @@ -17,7 +17,7 @@

import javax.annotation.Nonnull;

import com.github.akiraly.db4j.EntityWithLongId;
import com.github.akiraly.db4j.entity.EntityWithLongId;

@Nonnull
public class AuditedFoo extends AbstractCreateUpdateUowAwarePersistable {
Expand Down
Expand Up @@ -28,13 +28,13 @@
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.object.SqlUpdate;

import com.github.akiraly.db4j.EntityWithLongId;
import com.github.akiraly.db4j.EntityWithLongIdDao;
import com.github.akiraly.db4j.JdbcTemplateAware;
import com.github.akiraly.db4j.QueryFactory;
import com.github.akiraly.db4j.SimpleJdbcInsertBuilder;
import com.github.akiraly.db4j.SqlQueryBuilder;
import com.github.akiraly.db4j.SqlUpdateBuilder;
import com.github.akiraly.db4j.entity.EntityWithLongId;
import com.github.akiraly.db4j.entity.EntityWithLongIdDao;
import com.github.akiraly.db4j.uow.UowDaoFactory.UowDao;
import com.google.common.collect.ImmutableMap;

Expand Down
Expand Up @@ -30,15 +30,15 @@
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.object.SqlUpdate;

import com.github.akiraly.db4j.EntityWithUuid;
import com.github.akiraly.db4j.EntityWithUuidDao;
import com.github.akiraly.db4j.EntityWithUuids;
import com.github.akiraly.db4j.JdbcTemplateAware;
import com.github.akiraly.db4j.QueryFactory;
import com.github.akiraly.db4j.SimpleJdbcInsertBuilder;
import com.github.akiraly.db4j.SqlQueryBuilder;
import com.github.akiraly.db4j.SqlUpdateBuilder;
import com.github.akiraly.db4j.UuidBase64;
import com.github.akiraly.db4j.entity.EntityWithUuid;
import com.github.akiraly.db4j.entity.EntityWithUuidDao;
import com.github.akiraly.db4j.entity.EntityWithUuids;
import com.github.akiraly.db4j.uow.UowDaoFactory.UowDao;
import com.google.common.collect.ImmutableMap;

Expand Down
25 changes: 21 additions & 4 deletions db4j/src/test/java/com/github/akiraly/db4j/uow/Foo.java
Expand Up @@ -17,21 +17,38 @@

import static com.github.akiraly.ver4j.Verify.argNotNull;

import java.time.LocalDate;
import java.time.LocalDateTime;

import javax.annotation.Nonnull;

@Nonnull
public class Foo {
private String bar;
private final String bar;

private final LocalDateTime dt;

public Foo(String bar) {
private final LocalDate localDate;

public Foo(String bar, LocalDateTime dt) {
this(bar, dt, argNotNull(dt, "dt").toLocalDate());
}

public Foo(String bar, LocalDateTime dt, LocalDate localDate) {
this.bar = argNotNull(bar, "bar");
this.dt = argNotNull(dt, "dt");
this.localDate = argNotNull(localDate, "localDate");
}

public String getBar() {
return bar;
}

public void setBar(String bar) {
this.bar = bar;
public LocalDateTime getDt() {
return dt;
}

public LocalDate getLocalDate() {
return localDate;
}
}
23 changes: 18 additions & 5 deletions db4j/src/test/java/com/github/akiraly/db4j/uow/FooDaoFactory.java
Expand Up @@ -26,12 +26,14 @@
import org.springframework.jdbc.object.SqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import com.github.akiraly.db4j.EntityWithLongId;
import com.github.akiraly.db4j.EntityWithLongIdDao;
import com.github.akiraly.db4j.JdbcTemplateAware;
import com.github.akiraly.db4j.Jsr310JdbcUtils;
import com.github.akiraly.db4j.ResultSets;
import com.github.akiraly.db4j.SimpleJdbcInsertBuilder;
import com.github.akiraly.db4j.SqlQueryBuilder;
import com.github.akiraly.db4j.SqlUpdateBuilder;
import com.github.akiraly.db4j.entity.EntityWithLongId;
import com.github.akiraly.db4j.entity.EntityWithLongIdDao;
import com.google.common.collect.ImmutableMap;

@Nonnull
Expand All @@ -50,8 +52,13 @@ public class FooDaoFactory extends JdbcTemplateAware {
private final SqlQuery<Foo> queryById = //
new SqlQueryBuilder<Foo>(jdbcTemplate()) //
.sql("select * from foo where foo_id = ?") //
.parameters(new SqlParameter("foo_id", Types.BIGINT)) //
.rowMapper((rs, rn) -> new Foo(rs.getString("bar"))) //
.parameters(new SqlParameter("foo_id", Types.BIGINT))
//
.rowMapper((rs, rn) -> new Foo( //
rs.getString("bar"), //
ResultSets.readLocalDateTime(rs, "dt"), //
ResultSets.readLocalDate(rs, "local_date") //
)) //
.get();

public FooDaoFactory(JdbcTemplate jdbcTemplate) {
Expand All @@ -77,7 +84,13 @@ public EntityWithLongId<Foo> lazyFind(long id) {
@Override
protected long persist(Foo entity) {
return insert.executeAndReturnKey(
ImmutableMap.of("bar", entity.getBar())).longValue();
ImmutableMap.of("bar",
entity.getBar(), //
"dt",
Jsr310JdbcUtils.toUtcCalendar(entity.getDt()), //
"local_date", Jsr310JdbcUtils.toUtcCalendar(entity
.getLocalDate()) //
)).longValue();
}

@Override
Expand Down

0 comments on commit e784647

Please sign in to comment.