Skip to content

Commit

Permalink
#1: Using 'facilejdbc' library.
Browse files Browse the repository at this point in the history
  • Loading branch information
jenetics committed Nov 8, 2019
1 parent 468a0dd commit 217c426
Show file tree
Hide file tree
Showing 24 changed files with 283 additions and 1,971 deletions.
11 changes: 2 additions & 9 deletions jpx.jdbc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import io.franzbecker.gradle.lombok.task.DelombokTask

plugins {
id 'io.franzbecker.gradle-lombok' version '1.10'
id "io.franzbecker.gradle-lombok" version "3.2.0"
}

apply plugin: 'java-library'
Expand All @@ -44,10 +44,8 @@ repositories {
}

dependencies {
compileOnly 'org.projectlombok:lombok:1.18.8'
annotationProcessor 'org.projectlombok:lombok:1.18.8'

api project(':jpx')
implementation 'io.jenetics:facilejdbc:0.1.0'

testImplementation project(':jpx').sourceSets.test.output
testImplementation 'org.testng:testng:6.9.10'
Expand All @@ -57,11 +55,6 @@ dependencies {
testImplementation 'org.postgresql:postgresql:42.2.7'
}

lombok {
version = '1.18.2'
sha256 = ""
}

task delombok(type: DelombokTask, dependsOn: compileJava) {
ext.outputDir = file("$buildDir/delombok")
outputs.dir(outputDir)
Expand Down
24 changes: 14 additions & 10 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/BoundsAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
*/
package io.jenetics.jpx.jdbc;

import io.jenetics.facilejdbc.Dctor;
import io.jenetics.facilejdbc.Query;
import io.jenetics.jpx.Bounds;

import java.sql.Connection;
import java.sql.SQLException;

import io.jenetics.jpx.Bounds;
import io.jenetics.jpx.jdbc.internal.querily.Dctor;
import io.jenetics.jpx.jdbc.internal.querily.Dctor.Field;
import io.jenetics.jpx.jdbc.internal.querily.Query;
import static io.jenetics.facilejdbc.Dctor.field;

/**
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
Expand All @@ -37,21 +38,24 @@ private BoundsAccess() {}

private static final Query INSERT_QUERY = Query.of(
"INSERT INTO bounds(minlat, minlon, maxlat, maxlon) " +
"VALUES({minlat}, {minlon}, {maxlat}, {maxlon})"
"VALUES(:minlat:, :minlon, :maxlat, :maxlon)"
);

private static final Dctor<Bounds> DCTOR = Dctor.of(
Field.of("minlat", b -> b.getMinLatitude().doubleValue()),
Field.of("minlon", b -> b.getMinLongitude().doubleValue()),
Field.of("maxlat", b -> b.getMaxLatitude().doubleValue()),
Field.of("maxlon", b -> b.getMaxLongitude().doubleValue())
field("minlat", b -> b.getMinLatitude().doubleValue()),
field("minlon", b -> b.getMinLongitude().doubleValue()),
field("maxlat", b -> b.getMaxLatitude().doubleValue()),
field("maxlon", b -> b.getMaxLongitude().doubleValue())
);

public static Long insert(final Bounds bounds, final Connection conn)
throws SQLException
{
return bounds != null
? INSERT_QUERY.insert(bounds, DCTOR, conn)
? INSERT_QUERY
.on(bounds, DCTOR)
.executeInsert(conn)
.orElseThrow()
: null;
}

Expand Down
22 changes: 13 additions & 9 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/CopyrightAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
*/
package io.jenetics.jpx.jdbc;

import io.jenetics.facilejdbc.Dctor;
import io.jenetics.facilejdbc.Query;
import io.jenetics.jpx.Copyright;

import java.sql.Connection;
import java.sql.SQLException;
import java.time.Year;

import io.jenetics.jpx.Copyright;
import io.jenetics.jpx.jdbc.internal.querily.Dctor;
import io.jenetics.jpx.jdbc.internal.querily.Dctor.Field;
import io.jenetics.jpx.jdbc.internal.querily.Query;
import static io.jenetics.facilejdbc.Dctor.field;

/**
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
Expand All @@ -38,20 +39,23 @@ private CopyrightAccess() {}

private static final Query INSERT_QUERY = Query.of(
"INSERT INTO copyright(author, year, license) " +
"VALUES({author}, {year}, {license})"
"VALUES(:author:, :year, :license)"
);

private static final Dctor<Copyright> DCTOR = Dctor.of(
Field.of("author", Copyright::getAuthor),
Field.of("year", c -> c.getYear().map(Year::getValue)),
Field.of("license", Copyright::getLicense)
field("author", Copyright::getAuthor),
field("year", c -> c.getYear().map(Year::getValue)),
field("license", Copyright::getLicense)
);

public static Long insert(final Copyright copyright, final Connection conn)
throws SQLException
{
return copyright != null
? INSERT_QUERY.insert(copyright, DCTOR, conn)
? INSERT_QUERY
.on(copyright, DCTOR)
.executeInsert(conn)
.orElseThrow()
: null;
}

Expand Down
69 changes: 39 additions & 30 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/GPXAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@
*/
package io.jenetics.jpx.jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import io.jenetics.facilejdbc.Batch;
import io.jenetics.facilejdbc.Dctor;
import io.jenetics.facilejdbc.Query;
import io.jenetics.jpx.GPX;
import io.jenetics.jpx.Route;
import io.jenetics.jpx.Track;
import io.jenetics.jpx.WayPoint;
import io.jenetics.jpx.jdbc.internal.querily.Dctor;
import io.jenetics.jpx.jdbc.internal.querily.Dctor.Field;
import io.jenetics.jpx.jdbc.internal.querily.Query;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import static io.jenetics.facilejdbc.Dctor.field;

/**
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
Expand All @@ -41,13 +43,13 @@ private GPXAccess() {}

private static final Query INSERT_QUERY = Query.of(
"INSERT INTO gpx(version, creator, metadata_id) " +
"VALUES({version}, {creator}, {metadata_id});"
"VALUES(:version, :creator, :metadata_id);"
);

private static final Dctor<GPX> DCTOR = Dctor.of(
Field.of("version", GPX::getVersion),
Field.of("creator", GPX::getCreator),
Field.of(
field("version", GPX::getVersion),
field("creator", GPX::getCreator),
field(
"metadata_id",
(g, c) -> MetadataAccess.insert(g.getMetadata().orElse(null), c)
)
Expand All @@ -59,7 +61,11 @@ public static Long insert(final GPX gpx, final Connection conn)
{
if (gpx == null) return null;

final Long id = INSERT_QUERY.insert(gpx, DCTOR, conn);
final Long id = INSERT_QUERY
.on(gpx, DCTOR)
.executeInsert(conn)
.orElseThrow();

insertWayPoints(id, gpx.getWayPoints(), conn);
insertRoutes(id, gpx.getRoutes(), conn);
insertTracks(id, gpx.getTracks(), conn);
Expand All @@ -68,7 +74,7 @@ public static Long insert(final GPX gpx, final Connection conn)

private static final Query WAY_POINT_INSERT_QUERY = Query.of(
"INSERT INTO gpx_way_point(gpx_id, way_point_id) " +
"VALUES({gpx_id}, {way_point_id});"
"VALUES(:gpx_id, :way_point_id);"
);

private static void insertWayPoints(
Expand All @@ -78,19 +84,20 @@ private static void insertWayPoints(
)
throws SQLException
{
WAY_POINT_INSERT_QUERY.inserts(
final Batch batch = Batch.of(
points,
Dctor.of(
Field.ofValue("gpx_id", id),
Field.of("way_point_id", WayPointAccess::insert)
),
conn
field("gpx_id", r -> id),
field("way_point_id", WayPointAccess::insert)
)
);

WAY_POINT_INSERT_QUERY.executeUpdate(batch, conn);
}

private static final Query ROUTE_INSERT_QUERY = Query.of(
"INSERT INTO gpx_route(gpx_id, route_id) " +
"VALUES({gpx_id}, {route_id});"
"VALUES(:gpx_id, :route_id);"
);

private static void insertRoutes(
Expand All @@ -100,19 +107,20 @@ private static void insertRoutes(
)
throws SQLException
{
ROUTE_INSERT_QUERY.inserts(
final Batch batch = Batch.of(
routes,
Dctor.of(
Field.ofValue("gpx_id", id),
Field.of("route_id", RouteAccess::insert)
),
conn
field("gpx_id", r -> id),
field("route_id", RouteAccess::insert)
)
);

ROUTE_INSERT_QUERY.executeUpdate(batch, conn);
}

private static final Query TRACK_INSERT_QUERY = Query.of(
"INSERT INTO gpx_track(gpx_id, track_id) " +
"VALUES({gpx_id}, {track_id});"
"VALUES(:gpx_id, :track_id);"
);

private static void insertTracks(
Expand All @@ -122,14 +130,15 @@ private static void insertTracks(
)
throws SQLException
{
TRACK_INSERT_QUERY.inserts(
final Batch batch = Batch.of(
tracks,
Dctor.of(
Field.ofValue("gpx_id", id),
Field.of("track_id", TrackAccess::insert)
),
conn
field("gpx_id", r ->id),
field("track_id", TrackAccess::insert)
)
);

TRACK_INSERT_QUERY.executeUpdate(batch, conn);
}

}
22 changes: 13 additions & 9 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/LinkAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
*/
package io.jenetics.jpx.jdbc;

import io.jenetics.facilejdbc.Dctor;
import io.jenetics.facilejdbc.Query;
import io.jenetics.jpx.Link;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Optional;

import io.jenetics.jpx.Link;
import io.jenetics.jpx.jdbc.internal.querily.Dctor;
import io.jenetics.jpx.jdbc.internal.querily.Dctor.Field;
import io.jenetics.jpx.jdbc.internal.querily.Query;
import static io.jenetics.facilejdbc.Dctor.field;

/**
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
Expand All @@ -38,20 +39,23 @@ private LinkAccess() {}

private static final Query INSERT_QUERY = Query.of(
"INSERT INTO link(href, text, type) " +
"VALUES({href}, {text}, {type});"
"VALUES(:href, :text, :type);"
);

private static final Dctor<Link> DCTOR = Dctor.of(
Field.of("href", Link::getHref),
Field.of("text", Link::getText),
Field.of("type", Link::getType)
field("href", Link::getHref),
field("text", Link::getText),
field("type", Link::getType)
);

public static Long insert(final Link link, final Connection conn)
throws SQLException
{
return link != null
? INSERT_QUERY.insert(link, DCTOR, conn)
? INSERT_QUERY
.on(link, DCTOR)
.executeInsert(conn)
.orElseThrow()
: null;
}

Expand Down
Loading

0 comments on commit 217c426

Please sign in to comment.