BAEL-4520 Getting Started with jOOQ (#10101)

Co-authored-by: Krzysztof Majewski <krzysztof.majewski.km1@contractors.roche.com>
This commit is contained in:
Krzysztof Majewski 2020-09-29 05:30:32 +02:00 committed by GitHub
parent 664d170cf4
commit 42141b8889
2 changed files with 25 additions and 27 deletions

View File

@ -19,14 +19,14 @@ public class Crud {
public static Result<Record> getAll(DSLContext context, Table<? extends Record> table) { public static Result<Record> getAll(DSLContext context, Table<? extends Record> table) {
return context.select() return context.select()
.from(table) .from(table)
.fetch(); .fetch();
} }
public static Result<Record> getFields(DSLContext context, Table<? extends Record> table, SelectFieldOrAsterisk... fields) { public static Result<Record> getFields(DSLContext context, Table<? extends Record> table, SelectFieldOrAsterisk... fields) {
return context.select(fields) return context.select(fields)
.from(table) .from(table)
.fetch(); .fetch();
} }
public static <R extends Record> R getOne(DSLContext context, Table<R> table, Condition condition) { 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) { public static <T> void update(DSLContext context, Table<? extends Record> table, Map<Field<T>, T> values, Condition condition) {
context.update(table) context.update(table)
.set(values) .set(values)
.where(condition) .where(condition)
.execute(); .execute();
} }
public static <R extends UpdatableRecord<R>> void update(UpdatableRecord<R> record) { 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) { public static void delete(DSLContext context, Table<? extends Record> table, Condition condition) {
context.delete(table) context.delete(table)
.where(condition) .where(condition)
.execute(); .execute();
} }
public static <R extends UpdatableRecord<R>> void delete(UpdatableRecord<R> record) { public static <R extends UpdatableRecord<R>> void delete(UpdatableRecord<R> record) {

View File

@ -60,8 +60,8 @@ public class CrudExamples {
private void readValues(DSLContext context) { private void readValues(DSLContext context) {
Result<Record> authors = getAll( Result<Record> authors = getAll(
context, context,
Author.AUTHOR Author.AUTHOR
); );
authors.forEach(author -> { authors.forEach(author -> {
@ -73,15 +73,15 @@ public class CrudExamples {
}); });
Result<Record> articles = getFields( Result<Record> articles = getFields(
context, context,
Author.AUTHOR, Author.AUTHOR,
Article.ARTICLE.ID, Article.ARTICLE.TITLE Article.ARTICLE.ID, Article.ARTICLE.TITLE
); );
AuthorRecord author = getOne( AuthorRecord author = getOne(
context, context,
Author.AUTHOR, Author.AUTHOR,
Author.AUTHOR.ID.eq(1) Author.AUTHOR.ID.eq(1)
); );
} }
@ -90,24 +90,22 @@ public class CrudExamples {
fieldsToUpdate.put(Author.AUTHOR.FIRST_NAME, "David"); fieldsToUpdate.put(Author.AUTHOR.FIRST_NAME, "David");
fieldsToUpdate.put(Author.AUTHOR.LAST_NAME, "Brown"); fieldsToUpdate.put(Author.AUTHOR.LAST_NAME, "Brown");
update( update(
context, context,
Author.AUTHOR, Author.AUTHOR,
fieldsToUpdate, fieldsToUpdate,
Author.AUTHOR.ID.eq(1) Author.AUTHOR.ID.eq(1)
); );
ArticleRecord article = context.fetchOne(Article.ARTICLE, Article.ARTICLE.ID.eq(1)); ArticleRecord article = context.fetchOne(Article.ARTICLE, Article.ARTICLE.ID.eq(1));
article.setTitle("A New Article Title"); article.setTitle("A New Article Title");
update( update(article);
article
);
} }
private void deleteValues(DSLContext context) { private void deleteValues(DSLContext context) {
delete( delete(
context, context,
Article.ARTICLE, Article.ARTICLE,
Article.ARTICLE.ID.eq(1) Article.ARTICLE.ID.eq(1)
); );
AuthorRecord author = context.fetchOne(Author.AUTHOR, Author.AUTHOR.ID.eq(1)); AuthorRecord author = context.fetchOne(Author.AUTHOR, Author.AUTHOR.ID.eq(1));