Skip to content

Commit ff368ea

Browse files
committed
Add some jooq helpers and bump version
1 parent fc94c20 commit ff368ea

File tree

5 files changed

+108
-1
lines changed

5 files changed

+108
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ allprojects {
2121

2222
// Using Jitpack so I need the repo name in the group to match.
2323
group = 'com.stubbornjava.StubbornJava'
24-
version = '0.1.23-SNAPSHOT'
24+
version = '0.1.24-SNAPSHOT'
2525

2626
repositories {
2727
mavenLocal()

stubbornjava-common/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ dependencies {
3434
compile libs.jsoup
3535
compile libs.sitemapgen4j
3636
compile libs.jbcrypt
37+
compile libs.jooq
38+
compile libs.jooqCodegen
3739

3840
testCompile libs.junit
3941
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.stubbornjava.common.db.jooq;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import javax.sql.DataSource;
7+
8+
import org.jooq.SQLDialect;
9+
import org.jooq.impl.DataSourceConnectionProvider;
10+
import org.jooq.impl.DefaultConfiguration;
11+
import org.jooq.util.jaxb.ForcedType;
12+
13+
import com.google.common.collect.Lists;
14+
15+
public class JooqConfig {
16+
17+
public static DefaultConfiguration defaultConfigFromDataSource(DataSource ds) {
18+
DataSourceConnectionProvider dcp = new DataSourceConnectionProvider(ds);
19+
DefaultConfiguration jooqConfig = new DefaultConfiguration();
20+
jooqConfig.set(SQLDialect.MYSQL);
21+
jooqConfig.set(dcp);
22+
return jooqConfig;
23+
}
24+
25+
public static List<ForcedType> defaultForcedTypes() {
26+
return Lists.newLinkedList(Arrays.asList(new ForcedType[] {
27+
forcedType("BOOLEAN", "tinyint", Boolean.class),
28+
forcedType("LocalDate", "date", LocalDateConverter.class),
29+
forcedType("LocalDateTime", "datetime", LocalDateTimeConverter.class)
30+
}));
31+
}
32+
33+
private static ForcedType forcedType(String name, String types, Class<?> clazz) {
34+
ForcedType type = new ForcedType();
35+
type.setName(name);
36+
type.setTypes(types);
37+
type.setConverter(clazz.getName());
38+
return type;
39+
}
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.stubbornjava.common.db.jooq;
2+
3+
import org.jooq.Converter;
4+
5+
import java.sql.Date;
6+
import java.time.Instant;
7+
import java.time.LocalDate;
8+
import java.time.LocalDateTime;
9+
import java.time.ZoneId;
10+
11+
public class LocalDateConverter implements Converter<Date, LocalDate> {
12+
13+
@Override
14+
public LocalDate from(Date date) {
15+
return null == date ? null : LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault()).toLocalDate();
16+
}
17+
18+
@Override
19+
public Date to(LocalDate localDate) {
20+
return null == localDate ? null : java.sql.Date.valueOf(localDate);
21+
}
22+
23+
@Override
24+
public Class<Date> fromType() {
25+
return Date.class;
26+
}
27+
28+
@Override
29+
public Class<LocalDate> toType() {
30+
return LocalDate.class;
31+
}
32+
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.stubbornjava.common.db.jooq;
2+
3+
import org.jooq.Converter;
4+
5+
import java.sql.Timestamp;
6+
import java.time.Instant;
7+
import java.time.LocalDateTime;
8+
import java.time.ZoneId;
9+
10+
public class LocalDateTimeConverter implements Converter<Timestamp, LocalDateTime> {
11+
12+
@Override
13+
public LocalDateTime from(Timestamp date) {
14+
return null == date ? null : LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
15+
}
16+
17+
@Override
18+
public Timestamp to(LocalDateTime localDate) {
19+
return null == localDate ? null : Timestamp.from(localDate.atZone(ZoneId.systemDefault()).toInstant());
20+
}
21+
22+
@Override
23+
public Class<Timestamp> fromType() {
24+
return Timestamp.class;
25+
}
26+
27+
@Override
28+
public Class<LocalDateTime> toType() {
29+
return LocalDateTime.class;
30+
}
31+
32+
}

0 commit comments

Comments
 (0)