BAEL-4520 Getting Started with jOOQ (#10101)

Co-authored-by: Krzysztof Majewski <[email protected]>
This commit is contained in:
Krzysztof Majewski
2020-09-28 20:30:32 -07:00
committed by GitHub
co-authored by Krzysztof Majewski
parent 664d170cf4
commit 42141b8889
2 changed files with 25 additions and 27 deletions
@@ -19,14 +19,14 @@ public class Crud {
public static Result<Record> getAll(DSLContext context, Table<? extends Record> table) {
return context.select()
.from(table)
.fetch();
.from(table)
.fetch();
}
public static Result<Record> getFields(DSLContext context, Table<? extends Record> table, SelectFieldOrAsterisk... fields) {
return context.select(fields)
.from(table)
.fetch();
.from(table)
.fetch();
}
public static <R extends Record> R getOne(DSLContext context, Table<R> table, Condition condition) {
@@ -35,9 +35,9 @@ public class Crud {
public static <T> void update(DSLContext context, Table<? extends Record> table, Map<Field<T>, T> values, Condition condition) {
context.update(table)
.set(values)
.where(condition)
.execute();
.set(values)
.where(condition)
.execute();
}
public static <R extends UpdatableRecord<R>> void update(UpdatableRecord<R> record) {
@@ -46,8 +46,8 @@ public class Crud {
public static void delete(DSLContext context, Table<? extends Record> table, Condition condition) {
context.delete(table)
.where(condition)
.execute();
.where(condition)
.execute();
}
public static <R extends UpdatableRecord<R>> void delete(UpdatableRecord<R> record) {
@@ -60,8 +60,8 @@ public class CrudExamples {
private void readValues(DSLContext context) {
Result<Record> authors = getAll(
context,
Author.AUTHOR
context,
Author.AUTHOR
);
authors.forEach(author -> {
@@ -73,15 +73,15 @@ public class CrudExamples {
});
Result<Record> articles = getFields(
context,
Author.AUTHOR,
Article.ARTICLE.ID, Article.ARTICLE.TITLE
context,
Author.AUTHOR,
Article.ARTICLE.ID, Article.ARTICLE.TITLE
);
AuthorRecord author = getOne(
context,
Author.AUTHOR,
Author.AUTHOR.ID.eq(1)
context,
Author.AUTHOR,
Author.AUTHOR.ID.eq(1)
);
}
@@ -90,24 +90,22 @@ public class CrudExamples {
fieldsToUpdate.put(Author.AUTHOR.FIRST_NAME, "David");
fieldsToUpdate.put(Author.AUTHOR.LAST_NAME, "Brown");
update(
context,
Author.AUTHOR,
fieldsToUpdate,
Author.AUTHOR.ID.eq(1)
context,
Author.AUTHOR,
fieldsToUpdate,
Author.AUTHOR.ID.eq(1)
);
ArticleRecord article = context.fetchOne(Article.ARTICLE, Article.ARTICLE.ID.eq(1));
article.setTitle("A New Article Title");
update(
article
);
update(article);
}
private void deleteValues(DSLContext context) {
delete(
context,
Article.ARTICLE,
Article.ARTICLE.ID.eq(1)
context,
Article.ARTICLE,
Article.ARTICLE.ID.eq(1)
);
AuthorRecord author = context.fetchOne(Author.AUTHOR, Author.AUTHOR.ID.eq(1));