How to use extractTableConstraintsVersionOneOrLower method of org.evomaster.client.java.controller.internal.db.constraint.H2ConstraintExtractor class

Best EvoMaster code snippet using org.evomaster.client.java.controller.internal.db.constraint.H2ConstraintExtractor.extractTableConstraintsVersionOneOrLower

Source:H2ConstraintExtractor.java Github

copy

Full Screen

...60 private List<DbTableConstraint> extractTableConstraints(Connection connectionToH2, DbSchemaDto schemaDto, String h2DatabaseVersion) throws SQLException {61 if (H2VersionUtils.isVersionGreaterOrEqual(h2DatabaseVersion, H2VersionUtils.H2_VERSION_2_0_0)) {62 return extractTableConstraintsVersionTwoOrHigher(connectionToH2, schemaDto);63 } else {64 return extractTableConstraintsVersionOneOrLower(connectionToH2, schemaDto);65 }66 }67 /**68 * For each table in the schema DTO, this method appends69 * the constraints that are originated in the ALTER TABLE commands70 * for those particular tables.71 * <p>72 * Foreign keys are handled separately in the JDBC metadata73 *74 * @param connectionToH2 a connection to a H2 database75 * @param schemaDto DTO with database schema information76 * @throws SQLException if the connection to the H2 database fails77 */78 private List<DbTableConstraint> extractTableConstraintsVersionTwoOrHigher(Connection connectionToH2,79 DbSchemaDto schemaDto) throws SQLException {80 List<DbTableConstraint> tableCheckExpressions = new ArrayList<>();81 String tableSchema = schemaDto.name;82 for (TableDto tableDto : schemaDto.tables) {83 String tableName = tableDto.name;84 try (Statement statement = connectionToH2.createStatement()) {85 final String query = String.format("Select CONSTRAINT_CATALOG,CONSTRAINT_SCHEMA,CONSTRAINT_NAME,CONSTRAINT_TYPE From INFORMATION_SCHEMA.TABLE_CONSTRAINTS\n" +86 " where TABLE_CONSTRAINTS.TABLE_SCHEMA='%s' \n"87 + " and TABLE_CONSTRAINTS.TABLE_NAME='%s' ", tableSchema, tableName);88 try (ResultSet constraints = statement.executeQuery(query)) {89 while (constraints.next()) {90 String constraintCatalog = constraints.getString(CONSTRAINT_CATALOG);91 String constraintSchema = constraints.getString(CONSTRAINT_SCHEMA);92 String constraintName = constraints.getString(CONSTRAINT_NAME);93 String constraintType = constraints.getString(CONSTRAINT_TYPE);94 switch (constraintType) {95 case UNIQUE: {96 DbTableUniqueConstraint constraint = getTableUniqueConstraint(connectionToH2, tableName,97 constraintCatalog, constraintSchema, constraintName);98 tableCheckExpressions.add(constraint);99 break;100 }101 case CHECK: {102 DbTableCheckExpression constraint = getTableCheckExpression(connectionToH2, tableName, constraintCatalog, constraintSchema, constraintName);103 tableCheckExpressions.add(constraint);104 break;105 }106 case PRIMARY_KEY:107 case PRIMARY_KEY_BLANK:108 case REFERENTIAL:109 /*110 * This type of constraint is already handled by111 * JDBC Metadata112 **/113 break;114 default:115 cannotHandle(constraintType);116 }117 }118 }119 }120 }121 return tableCheckExpressions;122 }123 private DbTableUniqueConstraint getTableUniqueConstraint(Connection connectionToH2,124 String tableName,125 String constraintCatalog,126 String constraintSchema,127 String constraintName) throws SQLException {128 try (Statement columnsUsageStatement = connectionToH2.createStatement()) {129 String columnsUsageQuery = String.format("SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE "130 + "WHERE CONSTRAINT_CATALOG='%s' AND CONSTRAINT_SCHEMA='%s' AND CONSTRAINT_NAME='%s' ",131 constraintCatalog, constraintSchema, constraintName);132 try (ResultSet columnsUsageResultSet = columnsUsageStatement.executeQuery(columnsUsageQuery)) {133 List<String> uniqueColumnNames = new ArrayList<>();134 while (columnsUsageResultSet.next()) {135 String columnName = columnsUsageResultSet.getString(COLUMN_NAME);136 uniqueColumnNames.add(columnName);137 }138 return new DbTableUniqueConstraint(tableName, uniqueColumnNames);139 }140 }141 }142 private DbTableCheckExpression getTableCheckExpression(Connection connectionToH2,143 String tableName,144 String constraintCatalog,145 String constraintSchema,146 String constraintName) throws SQLException {147 try (Statement checkClauseStatement = connectionToH2.createStatement()) {148 String checkClauseQuery = String.format("SELECT CHECK_CLAUSE FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS "149 + "WHERE CONSTRAINT_CATALOG='%s' AND CONSTRAINT_SCHEMA='%s' AND CONSTRAINT_NAME='%s' ",150 constraintCatalog, constraintSchema, constraintName);151 try (ResultSet checkClauseResultSet = checkClauseStatement.executeQuery(checkClauseQuery)) {152 if (checkClauseResultSet.next()) {153 String sqlCheckExpression = checkClauseResultSet.getString("CHECK_CLAUSE");154 return new DbTableCheckExpression(tableName, "(" + sqlCheckExpression + ")");155 } else {156 throw new IllegalArgumentException(String.format("Cannot find constraint such that CONSTRAINT_CATALOG='%s' AND CONSTRAINT_SCHEMA='%s' AND CONSTRAINT_NAME='%s' ", constraintCatalog, constraintSchema, constraintName));157 }158 }159 }160 }161 /**162 * Extracts DbTableConstraints for version 0.xxx and 1.xxx163 *164 * @param connectionToH2 the connection to the H2 database165 * @param schemaDto the schema166 * @return the list of constraints in the table167 * @throws SQLException an unexpected exception while executing the queries168 */169 private List<DbTableConstraint> extractTableConstraintsVersionOneOrLower(Connection connectionToH2, DbSchemaDto schemaDto) throws SQLException {170 List<DbTableConstraint> tableCheckExpressions = new ArrayList<>();171 String tableSchema = schemaDto.name;172 for (TableDto tableDto : schemaDto.tables) {173 String tableName = tableDto.name;174 try (Statement statement = connectionToH2.createStatement()) {175 final String query = String.format("Select CONSTRAINT_TYPE, CHECK_EXPRESSION, COLUMN_LIST From INFORMATION_SCHEMA.CONSTRAINTS\n" +176 " where CONSTRAINTS.TABLE_SCHEMA='%s' \n"177 + " and CONSTRAINTS.TABLE_NAME='%s' ", tableSchema, tableName);178 try (ResultSet constraints = statement.executeQuery(query)) {179 while (constraints.next()) {180 String constraintType = constraints.getString(CONSTRAINT_TYPE);181 DbTableConstraint constraint;182 switch (constraintType) {183 case UNIQUE: {...

Full Screen

Full Screen

extractTableConstraintsVersionOneOrLower

Using AI Code Generation

copy

Full Screen

1package org.evomaster.client.java.controller.internal.db.constraint;2import org.evomaster.client.java.controller.api.dto.database.schema.TableDto;3import org.evomaster.client.java.controller.api.dto.database.schema.TableIndexDto;4import org.evomaster.client.java.controller.api.dto.database.schema.TableUniqueDto;5import org.evomaster.client.java.controller.api.dto.database.schema.TableForeignKeyDto;6import org.evomaster.client.java.controller.api.dto.database.schema.TableCheckDto;7import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType;8import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseTypeDto;9import org.evomaster.client.java.controller.api.dto.database.operations.SchemaDto;10import org.evomaster.client.java.controller.api.dto.database.operations.SqlScriptDto;11import org.evomaster.client.java.controller.api.dto.database.operations.SqlScriptEntryDto;12import org.evomaster.client.java.controller.api.dto.database.operations.SqlScriptType;13import org.evomaster.client.java.controller.api.dto.database.operations.SqlStatementDto;14import org.evomaster.client.java.controller.api.dto.database.operations.SqlStatementType;15import org.evomaster.client.java.controller.api.dto.database.operations.TableDataDto;16import org.evomaster.client.java.controller.internal.db.schema.Column;17import org.evomaster.client.java.controller.internal.db.schema.ColumnDataType;18import org.evomaster.client.java.controller.internal.db.schema.ColumnType;19import org.evomaster.client.java.controller.internal.db.schema.ForeignKey;20import org.evomaster.client.java.controller.internal.db.schema.Table;21import org.evomaster.client.java.controller.internal.db.schema.UniqueConstraint;22import org.evomaster.client.java.controller.internal.db.schema.View;23import org.evomaster.client.java.controller.internal.db.schema.H2SchemaExtractor;24import org.evomaster.client.java.controller.internal.db.H2ConnectionFactory;25import org.evomaster.client.java.controller.internal.db.SqlScriptRunner;26import org.evomaster.client.java.controller.internal.db.SqlScriptWriter;27import org.evomaster.client.java.controller.internal.db.TableDataInitializer;28import org.evomaster.client.java.controller.internal.db.TableRowData;29import org.evomaster.client.java.controller.internal.db.TableRowDataBuilder;30import org.evomaster.client.java.controller.internal.db.TableRowWriter;31import org.evomaster.client.java.controller.internal.db.h2.H2TableConstraintExtractor;32import org.evomaster.client.java.controller.internal.db.h2.H2TableDataExtractor;33import org.evomaster.client.java.controller.internal.db.h2.H2TableIndexExtractor;34import

Full Screen

Full Screen

extractTableConstraintsVersionOneOrLower

Using AI Code Generation

copy

Full Screen

1package org.evomaster.client.java.controller.internal.db.constraint;2import org.evomaster.client.java.controller.internal.db.SqlScriptRunner;3import org.evomaster.client.java.controller.internal.db.TableConstraint;4import org.evomaster.client.java.controller.internal.db.TableSchema;5import org.evomaster.client.java.controller.internal.db.schema.H2SchemaExtractor;6import org.junit.jupiter.api.AfterEach;7import org.junit.jupiter.api.BeforeEach;8import org.junit.jupiter.api.Test;9import java.sql.Connection;10import java.sql.DriverManager;11import java.sql.SQLException;12import java.util.List;13import static org.junit.jupiter.api.Assertions.assertEquals;14import static org.junit.jupiter.api.Assertions.assertTrue;15public class H2ConstraintExtractorTest {16 private Connection connection;17 public void init() throws SQLException {18 connection = DriverManager.getConnection("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "sa", "");19 SqlScriptRunner runner = new SqlScriptRunner(connection);20 runner.run("sql/tables.sql");21 }22 public void tearDown() throws SQLException {23 connection.close();24 }25 public void testExtractTableConstraintsVersionOneOrLower() throws SQLException {26 H2SchemaExtractor extractor = new H2SchemaExtractor(connection);27 TableSchema schema = extractor.extractSchema();28 H2ConstraintExtractor constraintExtractor = new H2ConstraintExtractor(connection);29 List<TableConstraint> constraints = constraintExtractor.extractTableConstraintsVersionOneOrLower(schema);30 assertEquals(3, constraints.size());31 TableConstraint c1 = constraints.get(0);32 assertEquals("FK_1", c1.getName());33 assertEquals("FK_1", c1.getForeignKeyName());34 assertEquals("FK_1", c1.getUniqueKeyName());35 assertEquals("A", c1.getTableName());36 assertEquals("A", c1.getReferencedTableName());37 assertEquals(2, c1.getColumns().size());38 assertTrue(c1.getColumns().contains("a"));39 assertTrue(c1.getColumns().contains("b"));40 assertEquals(2, c1.getReferencedColumns().size());41 assertTrue(c1.getReferencedColumns().contains("a"));42 assertTrue(c1.getReferencedColumns().contains("b"));43 TableConstraint c2 = constraints.get(1);44 assertEquals("FK_2", c2.getName());45 assertEquals("FK_2", c2.getForeignKeyName());46 assertEquals("FK_2", c2.getUniqueKeyName());47 assertEquals("

Full Screen

Full Screen

extractTableConstraintsVersionOneOrLower

Using AI Code Generation

copy

Full Screen

1package org.evomaster.client.java.controller.internal.db.constraint;2import java.sql.Connection;3import java.sql.DatabaseMetaData;4import java.sql.ResultSet;5import java.sql.SQLException;6import java.util.ArrayList;7import java.util.List;8public class H2ConstraintExtractor implements ConstraintExtractor {9 public List<TableConstraint> extractTableConstraints(Connection connection) throws SQLException {10 List<TableConstraint> constraints = new ArrayList<>();11 DatabaseMetaData metaData = connection.getMetaData();12 ResultSet rs = metaData.getTables(null, null, null, new String[]{"TABLE"});13 while (rs.next()) {14 String tableName = rs.getString("TABLE_NAME");15 String schema = rs.getString("TABLE_SCHEM");16 constraints.addAll(extractTableConstraintsVersionOneOrLower(connection, tableName, schema));17 }18 return constraints;19 }20 private List<TableConstraint> extractTableConstraintsVersionOneOrLower(Connection connection, String tableName, String schema) throws SQLException {21 List<TableConstraint> constraints = new ArrayList<>();22 DatabaseMetaData metaData = connection.getMetaData();23 ResultSet rs = metaData.getColumns(null, null, tableName, null);24 while (rs.next()) {25 String columnName = rs.getString("COLUMN_NAME");26 constraints.add(new TableConstraint(tableName, columnName, schema));27 }28 return constraints;29 }30}31package org.evomaster.client.java.controller.internal.db;32import java.sql.Connection;33import java.sql.SQLException;34import java.util.List;35public interface ConstraintExtractor {36 List<TableConstraint> extractTableConstraints(Connection connection) throws SQLException;37}38package org.evomaster.client.java.controller.internal.db;39public class TableConstraint {40 public final String tableName;41 public final String columnName;42 public final String schema;43 public TableConstraint(String tableName, String columnName, String schema) {44 this.tableName = tableName;45 this.columnName = columnName;46 this.schema = schema;47 }48 public String toString() {49 return "TableConstraint{" +50 '}';51 }52}53package org.evomaster.client.java.controller.internal.db;54import java

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful