From da977ffb74528b986ee1f4a75ab0dc576be2c956 Mon Sep 17 00:00:00 2001 From: cn-src Date: Tue, 2 Nov 2021 09:43:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E6=B8=85=E7=90=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/javaer/snippets/jooq/CrudStepTest.java | 74 --------- .../snippets/jooq/CrudStepWithH2Test.java | 75 --------- .../java/cn/javaer/snippets/jooq/TCity.java | 157 ------------------ 3 files changed, 306 deletions(-) delete mode 100644 snippets-jooq/src/test/java/cn/javaer/snippets/jooq/CrudStepTest.java delete mode 100644 snippets-jooq/src/test/java/cn/javaer/snippets/jooq/CrudStepWithH2Test.java delete mode 100644 snippets-jooq/src/test/java/cn/javaer/snippets/jooq/TCity.java diff --git a/snippets-jooq/src/test/java/cn/javaer/snippets/jooq/CrudStepTest.java b/snippets-jooq/src/test/java/cn/javaer/snippets/jooq/CrudStepTest.java deleted file mode 100644 index 096e0e07..00000000 --- a/snippets-jooq/src/test/java/cn/javaer/snippets/jooq/CrudStepTest.java +++ /dev/null @@ -1,74 +0,0 @@ -package cn.javaer.snippets.jooq; - -import cn.hutool.core.util.ObjectUtil; -import lombok.Value; -import org.jooq.DSLContext; -import org.jooq.InsertValuesStepN; -import org.jooq.Record; -import org.jooq.SQLDialect; -import org.jooq.SelectLimitStep; -import org.jooq.UpdateConditionStep; -import org.jooq.impl.DSL; -import org.junit.jupiter.api.Test; -import org.springframework.data.annotation.CreatedBy; -import org.springframework.data.annotation.Id; - -import java.util.Arrays; -import java.util.Optional; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author cn-src - */ -class CrudStepTest { - DSLContext dsl = DSL.using(SQLDialect.POSTGRES); - CrudStep crudStep = new CrudStep(this.dsl, (AuditorAware) () -> Optional.of(999L)); - - @Test - void insertStep() { - final InsertValuesStepN step = this.crudStep.insertStep( - CrudReflection.getTableMeta(Demo.class), new Demo(1L, "name", 996L)); - assertThat(this.dsl.renderInlined(step)) - .isEqualTo("insert into demo (id, name, created_by_id) values (1, 'name', 999)"); - } - - @Test - void batchInsertStep() { - final InsertValuesStepN step = this.crudStep.batchInsertStep( - CrudReflection.getTableMeta(Demo.class), - Arrays.asList(new Demo(1L, "name1", 996L), new Demo(2L, "name2", null))); - assertThat(this.dsl.renderInlined(step)) - .isEqualTo("insert into demo (id, name, created_by_id) " + - "values (1, 'name1', 999), (2, 'name2', 999)"); - } - - @Test - void dynamicUpdateStep() { - final UpdateConditionStep step = this.crudStep.dynamicUpdateStep( - CrudReflection.getTableMeta(Demo.class), new Demo(3L, "name", null), - ObjectUtil::isNotEmpty); - assertThat(this.dsl.renderInlined(step)) - .isEqualTo("update demo set name = 'name' where id = 3"); - } - - @Test - void findByIdAndCreatorStep() { - final SelectLimitStep step = this.crudStep.findByIdAndCreatorStep( - CrudReflection.getTableMeta(Demo.class), 1L); - assertThat(this.dsl.renderInlined(step)) - .isEqualTo("select id, name, created_by_id from demo " + - "where (id = 1 and created_by_id = 999)"); - } - - @Value - public static class Demo { - @Id - Long id; - - String name; - - @CreatedBy - Long createdById; - } -} \ No newline at end of file diff --git a/snippets-jooq/src/test/java/cn/javaer/snippets/jooq/CrudStepWithH2Test.java b/snippets-jooq/src/test/java/cn/javaer/snippets/jooq/CrudStepWithH2Test.java deleted file mode 100644 index e1eae5a9..00000000 --- a/snippets-jooq/src/test/java/cn/javaer/snippets/jooq/CrudStepWithH2Test.java +++ /dev/null @@ -1,75 +0,0 @@ -package cn.javaer.snippets.jooq; - -import lombok.Value; -import org.h2.jdbcx.JdbcDataSource; -import org.jooq.DSLContext; -import org.jooq.SQLDialect; -import org.jooq.impl.DSL; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.data.annotation.Id; - -import java.util.Arrays; -import java.util.Optional; -import java.util.UUID; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author cn-src - */ -class CrudStepWithH2Test { - DSLContext dsl = null; - CrudStep crudStep = null; - - @BeforeEach - void setUp() { - final JdbcDataSource dataSource = new JdbcDataSource(); - dataSource.setUser("sa"); - dataSource.setUrl("jdbc:h2:mem:" + UUID.randomUUID() + - ";DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1"); - this.dsl = DSL.using(dataSource, SQLDialect.H2); - //language=H2 - this.dsl.execute("CREATE TABLE demo (id bigint, name varchar)"); - //language=H2 - this.dsl.execute("CREATE TABLE CITY (ID bigint, NAME varchar)"); - this.crudStep = new CrudStep(this.dsl, (AuditorAware) () -> Optional.of(999L)); - - this.dsl.meta().getTables().forEach(System.out::println); - } - - @Test - void oneStep() { - final TableMeta meta = CrudReflection.getTableMeta(Demo.class); - this.crudStep.insertStep(meta, new Demo(1L, "name")) - .execute(); - final Demo demo = this.crudStep.findByIdStep(meta, 1L).fetchOneInto(Demo.class); - assertThat(demo).extracting(Demo::getId).isEqualTo(1L); - assertThat(demo).extracting(Demo::getName).isEqualTo("name"); - } - - @Test - void batchInsertStep() { - final TableMeta meta = CrudReflection.getTableMeta(Demo.class); - this.crudStep.batchInsertStep(meta, Arrays.asList(new Demo(1L, "name1"), - new Demo(2L, "name2"))) - .execute(); - } - - @Test - void oneMetaStep() { - this.crudStep.insertStep(TCity.CITY, new City(1, "name")) - .execute(); - final City city = this.crudStep.findByIdStep(TCity.CITY, 1).fetchOneInto(City.class); - assertThat(city).extracting(City::getId).isEqualTo(1); - assertThat(city).extracting(City::getName).isEqualTo("name"); - } - - @Value - public static class Demo { - @Id - Long id; - - String name; - } -} \ No newline at end of file diff --git a/snippets-jooq/src/test/java/cn/javaer/snippets/jooq/TCity.java b/snippets-jooq/src/test/java/cn/javaer/snippets/jooq/TCity.java deleted file mode 100644 index f335a7c3..00000000 --- a/snippets-jooq/src/test/java/cn/javaer/snippets/jooq/TCity.java +++ /dev/null @@ -1,157 +0,0 @@ -/* - * This file is generated by jOOQ. - */ -package cn.javaer.snippets.jooq; - -import org.jetbrains.annotations.UnmodifiableView; -import org.jooq.Field; -import org.jooq.ForeignKey; -import org.jooq.Name; -import org.jooq.Record; -import org.jooq.Row2; -import org.jooq.Table; -import org.jooq.TableField; -import org.jooq.impl.DSL; -import org.jooq.impl.TableImpl; - -import java.util.Arrays; -import java.util.List; -import java.util.Optional; -import java.util.function.Function; - -/** - * This class is generated by jOOQ. - */ -public class TCity extends TableImpl implements TableMeta { - - private static final long serialVersionUID = -484834304; - - /** - * The reference instance of PUBLIC.CITY - */ - public static final TCity CITY = new TCity(); - - /** - * The class holding records for this type - */ - @Override - public Class getRecordType() { - return Record.class; - } - - /** - * The column PUBLIC.CITY.ID. - */ - public final TableField ID = createField(DSL.name("ID"), - org.jooq.impl.SQLDataType.INTEGER.nullable(false).identity(true), this, ""); - - /** - * The column PUBLIC.CITY.NAME. - */ - public final TableField NAME = createField(DSL.name("NAME"), - org.jooq.impl.SQLDataType.VARCHAR(30), this, ""); - - private final Table __table = DSL.table(this.getUnqualifiedName()); - private final List> __selectFields = Arrays.asList(this.ID, this.NAME); - private final List> __columnMeta = - Arrays.asList(new ColumnMeta((Function) City::getName, this.NAME)); - private final ColumnMeta __idMeta = new ColumnMeta<>(City::getId, this.ID); - - /** - * Create a PUBLIC.CITY table reference - */ - public TCity() { - this(DSL.name("CITY"), null); - } - - /** - * Create an aliased PUBLIC.CITY table reference - */ - public TCity(final String alias) { - this(DSL.name(alias), CITY); - } - - /** - * Create an aliased PUBLIC.CITY table reference - */ - public TCity(final Name alias) { - this(alias, CITY); - } - - private TCity(final Name alias, final Table aliased) { - this(alias, aliased, null); - } - - private TCity(final Name alias, final Table aliased, final Field[] parameters) { - super(alias, null, aliased, parameters, DSL.comment("")); - } - - public TCity(final Table child, final ForeignKey key) { - super(child, key, CITY); - } - - @Override - public TCity as(final String alias) { - return new TCity(DSL.name(alias), this); - } - - @Override - public TCity as(final Name alias) { - return new TCity(alias, this); - } - - /** - * Rename this table - */ - @Override - public TCity rename(final String name) { - return new TCity(DSL.name(name), null); - } - - /** - * Rename this table - */ - @Override - public TCity rename(final Name name) { - return new TCity(name, null); - } - - // ------------------------------------------------------------------------- - // Row2 type methods - // ------------------------------------------------------------------------- - - @Override - public Row2 fieldsRow() { - return (Row2) super.fieldsRow(); - } - - @Override - public Table getTable() { - return this.__table; - } - - @Override - public Class getEntityClass() { - return City.class; - } - - @Override - public Optional> idGenerator() { - return Optional.of(this.__idMeta); - } - - @Override - public Optional> getId() { - return Optional.of(this.__idMeta); - } - - @Override - public @UnmodifiableView List> selectFields() { - return this.__selectFields; - } - - @Override - public @UnmodifiableView List> saveColumnMetas() { - return this.__columnMeta; - } -}