diff --git a/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/dml/DmlBuilder.java b/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/dml/DmlBuilder.java index 885ef3f40532b5bf56324fc27e5e8de1e3138e87..9e74caddfe86b617ef2cb063e85881b86de1e904 100644 --- a/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/dml/DmlBuilder.java +++ b/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/dml/DmlBuilder.java @@ -15,6 +15,7 @@ package org.opengauss.datachecker.extract.dml; +import org.opengauss.datachecker.common.entry.enums.DataBaseType; import org.opengauss.datachecker.common.entry.extract.ColumnsMetaData; import javax.validation.constraints.NotNull; @@ -66,12 +67,13 @@ public class DmlBuilder { * mysql dataType */ protected static final List DIGITAL = - List.of("int", "tinyint", "smallint", "mediumint", "bit", "bigint", "double", "float", "decimal"); - + List.of("int", "integer", "tinyint", "smallint", "mediumint", "bit", "bigint", "double", "float", "decimal", + "year"); /** * columns */ protected String columns; + /** * columnsValue */ @@ -92,6 +94,10 @@ public class DmlBuilder { * conditionValue */ protected String conditionValue; + /** + * dataBaseType + */ + protected DataBaseType dataBaseType; /** * Build SQL column statement fragment @@ -102,6 +108,10 @@ public class DmlBuilder { columns = columnsMetas.stream().map(ColumnsMetaData::getColumnName).collect(Collectors.joining(DELIMITER)); } + protected void buildDataBaseType(@NotNull DataBaseType dataBaseType) { + this.dataBaseType = dataBaseType; + } + /** * DML Builder: setting schema parameters * @@ -117,7 +127,7 @@ public class DmlBuilder { * @param tableName tableName */ protected void buildTableName(@NotNull String tableName) { - this.tableName = tableName; + this.tableName = isConvertTable() ? convert(tableName) : tableName; } /** @@ -131,6 +141,14 @@ public class DmlBuilder { .collect(Collectors.joining(DELIMITER, LEFT_BRACKET, RIGHT_BRACKET)); } + private boolean isConvertTable() { + return Objects.equals(dataBaseType, DataBaseType.OG); + } + + private String convert(String tableName) { + return "\"" + tableName + "\""; + } + /** * columnsValueList * @@ -160,11 +178,12 @@ public class DmlBuilder { /** * DML SQL statement insert fragment */ - String DML_INSERT = "insert into #schema.#tablename (#columns) value (#value);"; + String DML_INSERT = "insert into #schema.#tablename (#columns) values (#value);"; /** * DML SQL statement replace fragment */ - String DML_REPLACE = "replace into #schema.#tablename (#columns) value (#value);"; + String DML_REPLACE = "replace into #schema.#tablename (#columns) values (#value);"; + String DML_UPDATE = "update #schema.#tablename set #columns where #condition;"; /** * DML SQL statement select fragment */ @@ -185,10 +204,14 @@ public class DmlBuilder { * DML SQL statement space fragment */ String SPACE = " "; + String SINGLE_QUOTES = "'"; + String AND = " and "; + String EQUAL = "="; /** * DML SQL statement END fragment */ String END = ";"; + String COMMA = " , "; /** * DML SQL statement linker fragment */ @@ -209,5 +232,10 @@ public class DmlBuilder { * DML SQL statement value fragment */ String VALUE = "#value"; + /** + * DML SQL statement condition fragment + */ + String CONDITION = "#condition"; + } } diff --git a/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/dml/SelectDmlBuilder.java b/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/dml/SelectDmlBuilder.java index b05b0b063033d0fe535841bbd5ac1e9e336aa938..c89ccc7f5ea7ec72502978aecd7fd5679dd5281e 100644 --- a/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/dml/SelectDmlBuilder.java +++ b/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/dml/SelectDmlBuilder.java @@ -16,6 +16,7 @@ package org.opengauss.datachecker.extract.dml; import org.apache.commons.lang3.StringUtils; +import org.opengauss.datachecker.common.entry.enums.DataBaseType; import org.opengauss.datachecker.common.entry.extract.ColumnsMetaData; import org.opengauss.datachecker.extract.constants.ExtConstants; import org.springframework.lang.NonNull; @@ -27,12 +28,13 @@ import java.util.List; import java.util.stream.IntStream; /** + * SelectDmlBuilder + * * @author :wangchao * @date :Created in 2022/6/13 * @since :11 */ public class SelectDmlBuilder extends DmlBuilder { - /** * build SQL column statement fragment * @@ -44,6 +46,17 @@ public class SelectDmlBuilder extends DmlBuilder { return this; } + /** + * build dataBaseType + * + * @param dataBaseType dataBaseType + * @return SelectDMLBuilder + */ + public SelectDmlBuilder dataBaseType(@NotNull DataBaseType dataBaseType) { + super.buildDataBaseType(dataBaseType); + return this; + } + /** * build Schema * @@ -117,10 +130,7 @@ public class SelectDmlBuilder extends DmlBuilder { } public String build() { - StringBuffer sb = new StringBuffer(); - sb.append(Fragment.SELECT).append(columns).append(Fragment.FROM).append(schema).append(Fragment.LINKER) - .append(tableName).append(Fragment.WHERE).append(condition).append(Fragment.END); - return sb.toString(); + return Fragment.SELECT + columns + Fragment.FROM + schema + Fragment.LINKER + tableName + Fragment.WHERE + + condition + Fragment.END; } - } diff --git a/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/dml/UpdateDmlBuilder.java b/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/dml/UpdateDmlBuilder.java index 1e889a50c0ea130859e8fdec0e1e719f1305a997..1d5003e76efb4392e7535bfd676acca2e44eba09 100644 --- a/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/dml/UpdateDmlBuilder.java +++ b/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/dml/UpdateDmlBuilder.java @@ -15,13 +15,15 @@ package org.opengauss.datachecker.extract.dml; +import org.opengauss.datachecker.common.entry.enums.ColumnKey; +import org.opengauss.datachecker.common.entry.enums.DataBaseType; import org.opengauss.datachecker.common.entry.extract.ColumnsMetaData; +import org.opengauss.datachecker.common.entry.extract.TableMetadata; import javax.validation.constraints.NotNull; -import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; +import java.util.Objects; /** * UpdateDmlBuilder @@ -31,12 +33,14 @@ import java.util.stream.Collectors; * @since :11 */ public class UpdateDmlBuilder extends DmlBuilder { + private TableMetadata metadata; + private Map columnsValues; /** * build Schema * * @param schema Schema - * @return InsertDMLBuilder + * @return UpdateDmlBuilder */ public UpdateDmlBuilder schema(@NotNull String schema) { super.buildSchema(schema); @@ -44,42 +48,83 @@ public class UpdateDmlBuilder extends DmlBuilder { } /** - * build tableName + * build dataBaseType * - * @param tableName tableName - * @return InsertDMLBuilder + * @param dataBaseType dataBaseType + * @return UpdateDmlBuilder */ - public UpdateDmlBuilder tableName(@NotNull String tableName) { - super.buildTableName(tableName); + public UpdateDmlBuilder dataBaseType(@NotNull DataBaseType dataBaseType) { + super.buildDataBaseType(dataBaseType); return this; } /** - * build SQL column statement fragment + * build tableName * - * @param columnsMetas Field Metadata - * @return InsertDMLBuilder + * @param tableName tableName + * @return UpdateDmlBuilder */ - public UpdateDmlBuilder columns(@NotNull List columnsMetas) { - columns = columnsMetas.stream().map(ColumnsMetaData::getColumnName).collect(Collectors.joining(DELIMITER)); + public UpdateDmlBuilder tableName(@NotNull String tableName) { + super.buildTableName(tableName); return this; } /** * build SQL column value statement fragment * - * @param columnsMetaList Field Metadata - * @return InsertDMLBuilder + * @param columnsValues Field values + * @return UpdateDmlBuilder */ - public UpdateDmlBuilder columnsValue(@NotNull Map columnsValue, - @NotNull List columnsMetaList) { - List valueList = new ArrayList<>(columnsValueList(columnsValue, columnsMetaList)); - this.columnsValue = String.join(DELIMITER, valueList); + public UpdateDmlBuilder columnsValues(@NotNull Map columnsValues) { + this.columnsValues = columnsValues; + return this; + } + + public UpdateDmlBuilder metadata(@NotNull TableMetadata metadata) { + this.metadata = metadata; return this; } public String build() { - return Fragment.DML_REPLACE.replace(Fragment.SCHEMA, schema).replace(Fragment.TABLE_NAME, tableName) - .replace(Fragment.COLUMNS, columns).replace(Fragment.VALUE, columnsValue); + return Fragment.DML_UPDATE.replace(Fragment.SCHEMA, schema).replace(Fragment.TABLE_NAME, tableName) + .replace(Fragment.COLUMNS, buildColumnsValue()) + .replace(Fragment.CONDITION, buildConditionCompositePrimary()); + } + + private String buildConditionCompositePrimary() { + StringBuilder builder = new StringBuilder(); + final List primaryMetaDatas = metadata.getPrimaryMetas(); + for (ColumnsMetaData primaryMeta : primaryMetaDatas) { + builder.append(primaryMeta.getColumnName()).append(Fragment.EQUAL).append( + isDigital(primaryMeta.getDataType()) ? columnsValues.get(primaryMeta.getColumnName()) : + convertValue(columnsValues.get(primaryMeta.getColumnName()))).append(Fragment.AND); + } + final int length = builder.length(); + builder.delete(length - 4, length); + return builder.toString(); + } + + private String convertValue(String fieldValue) { + return Fragment.SINGLE_QUOTES + fieldValue + Fragment.SINGLE_QUOTES; + } + + private boolean isDigital(String dataType) { + return DIGITAL.contains(dataType); + } + + private String buildColumnsValue() { + StringBuilder builder = new StringBuilder(); + final List columnMetaDatas = metadata.getColumnsMetas(); + for (ColumnsMetaData columnMeta : columnMetaDatas) { + if (Objects.equals(columnMeta.getColumnKey(), ColumnKey.PRI)) { + continue; + } + builder.append(columnMeta.getColumnName()).append(Fragment.EQUAL).append( + isDigital(columnMeta.getDataType()) ? columnsValues.get(columnMeta.getColumnName()) : + convertValue(columnsValues.get(columnMeta.getColumnName()))).append(Fragment.COMMA); + } + final int length = builder.length(); + builder.delete(length - 3, length); + return builder.toString(); } } diff --git a/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/service/MetaDataService.java b/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/service/MetaDataService.java index ef6bf09fe2d4794d3814919de128494ff0ed3dd9..449a0d6b86b778da589ad65fe52252e21c8e3691 100644 --- a/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/service/MetaDataService.java +++ b/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/service/MetaDataService.java @@ -74,7 +74,6 @@ public class MetaDataService { tableMeta.setColumnsMetas(tableColumnMap.get(tableMeta.getTableName())) .setPrimaryMetas(getTablePrimaryColumn(tableColumnMap.get(tableMeta.getTableName()))); }); - log.info("Query database metadata information completed total=" + columnsMetadata.size()); } return tableMetadata.stream().collect(Collectors.toMap(TableMetadata::getTableName, Function.identity())); } @@ -101,7 +100,7 @@ public class MetaDataService { TableMetadata tableMetadata = queryTableMetadataByTableName(tableName); List columnsMetadata = dataBaseMetadataDAOImpl.queryColumnMetadata(List.of(tableName)); tableMetadata.setColumnsMetas(columnsMetadata).setPrimaryMetas(getTablePrimaryColumn(columnsMetadata)); - log.info("Query database metadata information completed total={}", columnsMetadata); + log.debug("Query database metadata information completed total={}", columnsMetadata); return tableMetadata; } diff --git a/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/task/DataManipulationService.java b/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/task/DataManipulationService.java index 01fba3476d9a2ccbb0a8ff450465738355382935..182ee941bc94557cdd0daeb8e39b63d557673448 100644 --- a/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/task/DataManipulationService.java +++ b/datachecker-extract/src/main/java/org/opengauss/datachecker/extract/task/DataManipulationService.java @@ -29,8 +29,8 @@ import org.opengauss.datachecker.extract.dml.BatchDeleteDmlBuilder; import org.opengauss.datachecker.extract.dml.DeleteDmlBuilder; import org.opengauss.datachecker.extract.dml.DmlBuilder; import org.opengauss.datachecker.extract.dml.InsertDmlBuilder; -import org.opengauss.datachecker.extract.dml.ReplaceDmlBuilder; import org.opengauss.datachecker.extract.dml.SelectDmlBuilder; +import org.opengauss.datachecker.extract.dml.UpdateDmlBuilder; import org.opengauss.datachecker.extract.service.MetaDataService; import org.opengauss.datachecker.extract.util.MetaDataUtil; import org.springframework.beans.factory.annotation.Autowired; @@ -221,17 +221,16 @@ public class DataManipulationService { TableMetadata metadata) { List resultList = new ArrayList<>(); final String localSchema = getLocalSchema(schema); - ReplaceDmlBuilder builder = - new ReplaceDmlBuilder().schema(localSchema).tableName(tableName).columns(metadata.getColumnsMetas()); - - List> columnValues = - queryColumnValues(tableName, new ArrayList<>(compositeKeySet), metadata); + List> columnValues = queryColumnValues(tableName, List.copyOf(compositeKeySet), metadata); Map> compositeKeyValues = transtlateColumnValues(columnValues, metadata.getPrimaryMetas()); + UpdateDmlBuilder builder = new UpdateDmlBuilder(); + builder.metadata(metadata).tableName(tableName).dataBaseType(DataBaseType.OG).schema(localSchema); compositeKeySet.forEach(compositeKey -> { Map columnValue = compositeKeyValues.get(compositeKey); if (Objects.nonNull(columnValue) && !columnValue.isEmpty()) { - resultList.add(builder.columnsValue(columnValue, metadata.getColumnsMetas()).build()); + builder.columnsValues(columnValue); + resultList.add(builder.build()); } }); return resultList; diff --git a/datachecker-extract/src/test/java/org/opengauss/datachecker/extract/task/sql/MockTableMeta.java b/datachecker-extract/src/test/java/org/opengauss/datachecker/extract/task/sql/MockTableMeta.java new file mode 100644 index 0000000000000000000000000000000000000000..16dca3cb3eaff8d6fa08a99eb615a9d85b808793 --- /dev/null +++ b/datachecker-extract/src/test/java/org/opengauss/datachecker/extract/task/sql/MockTableMeta.java @@ -0,0 +1,47 @@ +package org.opengauss.datachecker.extract.task.sql; + +import com.alibaba.fastjson.JSONObject; +import org.apache.commons.io.IOUtils; +import org.opengauss.datachecker.common.entry.extract.TableMetadata; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.Objects; + +/** + * MockTableMeta + * + * @author :wangchao + * @date :Created in 2022/9/21 + * @since :11 + */ +public class MockTableMeta { + private String metaJsonResource = "data/update_dml_builder_test/metadata.json"; + private String valueJsonResource = "data/update_dml_builder_test/values.json"; + + public String getSchema() { + return "test"; + } + + public Map getValues() { + return JSONObject.parseObject(mockMetadataJson(valueJsonResource), Map.class); + } + + public TableMetadata mockSingleTablePrimaryMetadata() { + return JSONObject.parseObject(mockMetadataJson(metaJsonResource), TableMetadata.class); + } + + public String mockMetadataJson(String resource) { + try (InputStream inputStream = MockTableMeta.class.getClassLoader().getResourceAsStream(resource)) { + if (Objects.nonNull(inputStream)) { + return IOUtils.toString(inputStream, String.valueOf(StandardCharsets.UTF_8)); + } else { + return null; + } + } catch (IOException ex) { + return null; + } + } +} diff --git a/datachecker-extract/src/test/java/org/opengauss/datachecker/extract/task/sql/SelectDmlBuilderTest.java b/datachecker-extract/src/test/java/org/opengauss/datachecker/extract/task/sql/SelectDmlBuilderTest.java new file mode 100644 index 0000000000000000000000000000000000000000..8b0b1e6d8b7864d2c0845428b481461e0079d8b3 --- /dev/null +++ b/datachecker-extract/src/test/java/org/opengauss/datachecker/extract/task/sql/SelectDmlBuilderTest.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022-2022 Huawei Technologies Co.,Ltd. + * + * openGauss is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * + * http://license.coscl.org.cn/MulanPSL2 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package org.opengauss.datachecker.extract.task.sql; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opengauss.datachecker.common.entry.enums.DataBaseType; +import org.opengauss.datachecker.common.entry.extract.TableMetadata; +import org.opengauss.datachecker.extract.dml.SelectDmlBuilder; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +/** + * SelectDmlBuilderTest + * + * @author :wangchao + * @date :Created in 2022/9/21 + * @since :11 + */ +@ExtendWith(MockitoExtension.class) +public class SelectDmlBuilderTest extends MockTableMeta { + private TableMetadata mockTableMetadata; + private SelectDmlBuilder selectDmlBuilder; + + @BeforeEach + void setUp() { + mockTableMetadata = mockSingleTablePrimaryMetadata(); + selectDmlBuilder = new SelectDmlBuilder(); + } + + @DisplayName("openGauss no divisions single primary select SQL build") + @Test + void testSelectNoDivisionsSqlBuilder() { + String result = selectDmlBuilder.columns(mockTableMetadata.getColumnsMetas()) + .conditionPrimary(mockTableMetadata.getPrimaryMetas().get(0)) + .schema(getSchema()).dataBaseType(DataBaseType.OG) + .tableName(mockTableMetadata.getTableName()).build(); + // Verify the results + assertThat(result).isEqualTo( + "select id,c_date_time,c_date_time_3,c_timestamp,c_date,c_time,c_year from test.\"t_data_checker_time_0018_01\" where id in ( :primaryKeys );"); + } + +} diff --git a/datachecker-extract/src/test/java/org/opengauss/datachecker/extract/task/sql/SelectSqlBuilderTest.java b/datachecker-extract/src/test/java/org/opengauss/datachecker/extract/task/sql/SelectSqlBuilderTest.java index dcfcd0454dbb7d70ab286f01e9eefa0523fa50c4..4149e9a5a25160c393c7d043b027685c8c11c05b 100644 --- a/datachecker-extract/src/test/java/org/opengauss/datachecker/extract/task/sql/SelectSqlBuilderTest.java +++ b/datachecker-extract/src/test/java/org/opengauss/datachecker/extract/task/sql/SelectSqlBuilderTest.java @@ -16,21 +16,14 @@ package org.opengauss.datachecker.extract.task.sql; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import org.opengauss.datachecker.common.entry.enums.ColumnKey; import org.opengauss.datachecker.common.entry.enums.DataBaseType; -import org.opengauss.datachecker.common.entry.extract.ColumnsMetaData; import org.opengauss.datachecker.common.entry.extract.TableMetadata; -import org.opengauss.datachecker.extract.task.sql.SelectSqlBuilder.Message; -import java.util.List; - -import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; -import static org.mockito.Mockito.when; /** * SelectSqlBuilderTest @@ -40,83 +33,36 @@ import static org.mockito.Mockito.when; * @since :11 */ @ExtendWith(MockitoExtension.class) -class SelectSqlBuilderTest { - @Mock +class SelectSqlBuilderTest extends MockTableMeta { private TableMetadata mockTableMetadata; private SelectSqlBuilder selectSqlBuilder; @BeforeEach void setUp() { - selectSqlBuilder = new SelectSqlBuilder(mockTableMetadata, "schema"); + mockTableMetadata = mockSingleTablePrimaryMetadata(); + selectSqlBuilder = new SelectSqlBuilder(mockTableMetadata, getSchema()); } /** * testBuilder */ + @DisplayName("openGauss no divisions single primary select SQL build") @Test - void testBuilder() { - // Setup - // Configure TableMetadata.getColumnsMetas(...). - final ColumnsMetaData columnsMetaData1 = new ColumnsMetaData(); - columnsMetaData1.setTableName("tableName"); - columnsMetaData1.setColumnName("columnName"); - columnsMetaData1.setColumnType("columnType"); - columnsMetaData1.setDataType("dataType"); - columnsMetaData1.setOrdinalPosition(0); - columnsMetaData1.setColumnKey(ColumnKey.PRI); - final List columnsMetaData = List.of(columnsMetaData1); - when(mockTableMetadata.getColumnsMetas()).thenReturn(columnsMetaData); - - when(mockTableMetadata.getTableName()).thenReturn("tableName"); - // Run the test - final String result = selectSqlBuilder.dataBaseType(DataBaseType.MS).offset(0, 120).builder(); - + void testSelectNoDivisionsSqlBuilder() { + String result = selectSqlBuilder.isDivisions(false).dataBaseType(DataBaseType.OG).builder(); // Verify the results - assertThat(result).isEqualTo("SELECT columnName FROM schema.tableName LIMIT 0,120"); + assertThat(result).isEqualTo( + "SELECT id,c_date_time,c_date_time_3,c_timestamp,c_date,c_time,c_year FROM test.\"t_data_checker_time_0018_01\""); } - /** - * testBuilder_TableMetadataGetColumnsMetasReturnsNoItems - */ + @DisplayName("openGauss divisions single primary select SQL build") @Test - void testBuilder_TableMetadataGetColumnsMetasReturnsNoItems() { - // Run the test - assertThatThrownBy(() -> selectSqlBuilder.builder()).isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining( - Message.COLUMN_METADATA_EMPTY_NOT_TO_BUILD_SQL); - } - - /** - * testBuildSelectSqlOffset - */ - @Test - void testBuildSelectSqlOffset() { - // Setup - final TableMetadata tableMetadata = new TableMetadata(); - tableMetadata.setTableName("tableName"); - tableMetadata.setTableRows(0L); - final ColumnsMetaData columnsMetaData = new ColumnsMetaData(); - columnsMetaData.setTableName("tableName"); - columnsMetaData.setColumnName("pk_columnName1"); - columnsMetaData.setColumnType("pk_columnType1"); - columnsMetaData.setDataType("dataType"); - columnsMetaData.setOrdinalPosition(0); - columnsMetaData.setColumnKey(ColumnKey.PRI); - tableMetadata.setPrimaryMetas(List.of(columnsMetaData)); - final ColumnsMetaData columnsMetaData1 = new ColumnsMetaData(); - columnsMetaData1.setTableName("tableName"); - columnsMetaData1.setColumnName("columnName2"); - columnsMetaData1.setColumnType("columnType2"); - columnsMetaData1.setDataType("dataType"); - columnsMetaData1.setOrdinalPosition(0); - columnsMetaData1.setColumnKey(ColumnKey.PRI); - tableMetadata.setColumnsMetas(List.of(columnsMetaData, columnsMetaData1)); - - // Run the test - final String result = - selectSqlBuilder.dataBaseType(DataBaseType.MS).offset(0, 120).buildSelectSqlOffset(tableMetadata, 0L, 100L); - + void testSelectDivisionsSqlBuilder() { + String result = selectSqlBuilder.isDivisions(false).dataBaseType(DataBaseType.OG) + .buildSelectSqlOffset(mockTableMetadata, 0, 12); // Verify the results - assertThat(result).isEqualTo("SELECT pk_columnName1,columnName2 FROM schema.tableName LIMIT 0,100"); + assertThat(result).isEqualTo( + "SELECT id,c_date_time,c_date_time_3,c_timestamp,c_date,c_time,c_year FROM test.\"t_data_checker_time_0018_01\" LIMIT 0,12"); } + } diff --git a/datachecker-extract/src/test/java/org/opengauss/datachecker/extract/task/sql/UpdateDmlBuilderTest.java b/datachecker-extract/src/test/java/org/opengauss/datachecker/extract/task/sql/UpdateDmlBuilderTest.java new file mode 100644 index 0000000000000000000000000000000000000000..13afbb91fed7806116b724007bbbeb9310d2873f --- /dev/null +++ b/datachecker-extract/src/test/java/org/opengauss/datachecker/extract/task/sql/UpdateDmlBuilderTest.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2022-2022 Huawei Technologies Co.,Ltd. + * + * openGauss is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * + * http://license.coscl.org.cn/MulanPSL2 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package org.opengauss.datachecker.extract.task.sql; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opengauss.datachecker.common.entry.enums.DataBaseType; +import org.opengauss.datachecker.common.entry.extract.TableMetadata; +import org.opengauss.datachecker.extract.dml.UpdateDmlBuilder; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +/** + * SelectDmlBuilderTest + * + * @author :wangchao + * @date :Created in 2022/9/21 + * @since :11 + */ +@ExtendWith(MockitoExtension.class) +public class UpdateDmlBuilderTest extends MockTableMeta { + private TableMetadata mockTableMetadata; + private UpdateDmlBuilder updateDmlBuilder; + + @BeforeEach + void setUp() { + mockTableMetadata = mockSingleTablePrimaryMetadata(); + updateDmlBuilder = new UpdateDmlBuilder(); + } + + @DisplayName("openGauss update SQL build") + @Test + void testSelectNoDivisionsSqlBuilder() { + String result = + updateDmlBuilder.metadata(mockTableMetadata).columnsValues(getValues()).dataBaseType(DataBaseType.OG) + .schema(getSchema()).tableName(mockTableMetadata.getTableName()).build(); + // Verify the results + assertThat(result).isEqualTo( + "update test.\"t_data_checker_time_0018_01\" set c_date_time='2022-09-18 17:39:51' , c_date_time_3='2022-09-19 10:13:37.741' , c_timestamp='2022-09-18 17:39:51' , c_date='2022-09-18' , c_time='17:39:49' , c_year=2022 where id=15 ;"); + } + +} diff --git a/datachecker-extract/src/test/resources/data/update_dml_builder_test/metadata.json b/datachecker-extract/src/test/resources/data/update_dml_builder_test/metadata.json new file mode 100644 index 0000000000000000000000000000000000000000..269b6ca4691d4e8fe493c536ed0368b8af2acd2f --- /dev/null +++ b/datachecker-extract/src/test/resources/data/update_dml_builder_test/metadata.json @@ -0,0 +1,61 @@ +{ + "columnsMetas": [ + { + "columnKey": "PRI", + "columnName": "id", + "columnType": "integer", + "dataType": "integer", + "ordinalPosition": 1, + "tableName": "t_data_checker_time_0018_01" + }, + { + "columnName": "c_date_time", + "columnType": "timestamp without time zone", + "dataType": "timestamp without time zone", + "ordinalPosition": 5, + "tableName": "t_data_checker_time_0018_01" + }, + { + "columnName": "c_date_time_3", + "columnType": "timestamp without time zone", + "dataType": "timestamp without time zone", + "ordinalPosition": 6, + "tableName": "t_data_checker_time_0018_01" + }, + { + "columnName": "c_timestamp", + "columnType": "timestamp without time zone", + "dataType": "timestamp without time zone", + "ordinalPosition": 7, + "tableName": "t_data_checker_time_0018_01" + }, + { + "columnName": "c_date", + "columnType": "date", + "dataType": "date", + "ordinalPosition": 2, + "tableName": "t_data_checker_time_0018_01" + }, + { + "columnName": "c_time", + "columnType": "time without time zone", + "dataType": "time without time zone", + "ordinalPosition": 3, + "tableName": "t_data_checker_time_0018_01" + }, + { + "columnName": "c_year", + "columnType": "year", + "dataType": "year", + "ordinalPosition": 4, + "tableName": "t_data_checker_time_0018_01" + } + ], + "primaryMetas": [ + { + "$ref": "$.columnsMetas[0]" + } + ], + "tableName": "t_data_checker_time_0018_01", + "tableRows": 4 +} \ No newline at end of file diff --git a/datachecker-extract/src/test/resources/data/update_dml_builder_test/values.json b/datachecker-extract/src/test/resources/data/update_dml_builder_test/values.json new file mode 100644 index 0000000000000000000000000000000000000000..259cc22088642c7feec9a4b074fbae0254d55a40 --- /dev/null +++ b/datachecker-extract/src/test/resources/data/update_dml_builder_test/values.json @@ -0,0 +1,9 @@ +{ + "c_date_time_3": "2022-09-19 10:13:37.741", + "c_timestamp": "2022-09-18 17:39:51", + "c_date": "2022-09-18", + "c_time": "17:39:49", + "c_year": "2022", + "id": "15", + "c_date_time": "2022-09-18 17:39:51" +} \ No newline at end of file