use of org.jooq.impl.ThreadLocalTransactionProvider in project jOOQ by jOOQ.
the class SparkCRUD method main.
public static void main(String[] args) throws Exception {
final BasicDataSource ds = new BasicDataSource();
final Properties properties = new Properties();
properties.load(SparkCRUD.class.getResourceAsStream("/config.properties"));
ds.setDriverClassName(properties.getProperty("db.driver"));
ds.setUrl(properties.getProperty("db.url"));
ds.setUsername(properties.getProperty("db.username"));
ds.setPassword(properties.getProperty("db.password"));
final ConnectionProvider cp = new DataSourceConnectionProvider(ds);
final Configuration configuration = new DefaultConfiguration().set(cp).set(SQLDialect.H2).set(new ThreadLocalTransactionProvider(cp, true));
final DSLContext ctx = DSL.using(configuration);
final JSONFormat format = new JSONFormat().format(true).header(false).recordFormat(RecordFormat.OBJECT);
// Creates a new book resource, will return the ID to the created resource
// author and title are sent as query parameters e.g. /books?author=Foo&title=Bar
post("/books", (request, response) -> {
return ctx.transactionResult(() -> {
AuthorRecord author = upsertAuthor(ctx, request);
BookRecord book = ctx.newRecord(BOOK);
book.setAuthorId(author.getId());
book.setTitle(request.queryParams("title"));
book.store();
// 201 Created
response.status(201);
return book.formatJSON(format);
});
});
// Gets the book resource for the provided id
get("/books/:id", (request, response) -> {
Record2<String, String> book = ctx.select(BOOK.TITLE, AUTHOR.NAME).from(BOOK).join(AUTHOR).on(BOOK.AUTHOR_ID.eq(AUTHOR.ID)).where(BOOK.ID.eq(BOOK.ID.getDataType().convert(request.params(":id")))).fetchOne();
if (book != null) {
return book.formatJSON(format);
} else {
// 404 Not found
response.status(404);
return "{\"error\":\"Book not found\"}";
}
});
// Updates the book resource for the provided id with new information
// author and title are sent as query parameters e.g. /books/<id>?author=Foo&title=Bar
put("/books/:id", (request, response) -> {
return ctx.transactionResult(() -> {
BookRecord book = ctx.selectFrom(BOOK).where(BOOK.ID.eq(BOOK.ID.getDataType().convert(request.params(":id")))).fetchOne();
if (book != null) {
AuthorRecord author = upsertAuthor(ctx, request);
String newAuthor = request.queryParams("author");
String newTitle = request.queryParams("title");
if (newAuthor != null)
book.setAuthorId(author.getId());
if (newTitle != null)
book.setTitle(newTitle);
book.update();
return book.formatJSON(format);
} else {
// 404 Not found
response.status(404);
return "{\"error\":\"Book not found\"}";
}
});
});
// Deletes the book resource for the provided id
delete("/books/:id", (request, response) -> {
return ctx.transactionResult(() -> {
BookRecord book = ctx.deleteFrom(BOOK).where(BOOK.ID.eq(BOOK.ID.getDataType().convert(request.params(":id")))).returning().fetchOne();
if (book != null) {
return book.formatJSON(format);
} else {
// 404 Not found
response.status(404);
return "{\"error\":\"Book not found\"}";
}
});
});
// Gets all available book resources
get("/books", (request, response) -> {
return ctx.select(BOOK.ID, BOOK.TITLE).from(BOOK).fetch().formatJSON(format);
});
}
Aggregations