How to use DbTableConstraint class of org.evomaster.client.java.controller.internal.db.constraint package

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

Source:H2ConstraintExtractor.java Github

copy

Full Screen

...26 * @param connectionToH2 a connection to a H2 database27 * @param schemaDto a DTO schema with retrieved information from the JBDC metadata28 * @throws SQLException if the connection to the H2 database fails29 */30 public List<DbTableConstraint> extract(Connection connectionToH2, DbSchemaDto schemaDto) throws SQLException {31 List<DbTableConstraint> columnConstraints = extractColumnConstraints(connectionToH2, schemaDto);32 List<DbTableConstraint> tableCheckExpressions = extractTableConstraints(connectionToH2, schemaDto);33 List<DbTableConstraint> allConstraints = new ArrayList<>();34 allConstraints.addAll(columnConstraints);35 allConstraints.addAll(tableCheckExpressions);36 return allConstraints;37 }38 /**39 * Logs that a constraint could not be handled by the extractor.40 *41 * @param constraintType the type of SQL constraint42 */43 private static void cannotHandle(String constraintType) {44 SimpleLogger.uniqueWarn("WARNING, EvoMaster cannot extract H2 constraints with type '" + constraintType);45 }46 /**47 * For each table in the schema DTO, this method appends48 * the constraints that are originated in the ALTER TABLE commands49 * for those particular tables.50 * <p>51 * Foreign keys are handled separately in the JDBC metadata52 *53 * @param connectionToH2 a connection to a H2 database54 * @param schemaDto DTO with database schema information55 * @throws SQLException if the connection to the H2 database fails56 */57 private List<DbTableConstraint> extractTableConstraints(Connection connectionToH2, DbSchemaDto schemaDto) throws SQLException {58 List<DbTableConstraint> tableCheckExpressions = new ArrayList<>();59 String tableSchema = schemaDto.name;60 for (TableDto tableDto : schemaDto.tables) {61 String tableName = tableDto.name;62 try (Statement statement = connectionToH2.createStatement()) {63 final String query = String.format("Select * From INFORMATION_SCHEMA.CONSTRAINTS\n" +64 " where CONSTRAINTS.TABLE_SCHEMA='%s' \n"65 + " and CONSTRAINTS.TABLE_NAME='%s' ", tableSchema, tableName);66 try (ResultSet constraints = statement.executeQuery(query)) {67 while (constraints.next()) {68 String constraintType = constraints.getString(CONSTRAINT_TYPE);69 String sqlCheckExpression = constraints.getString(CHECK_EXPRESSION);70 String columnList = constraints.getString(COLUMN_LIST);71 DbTableConstraint constraint;72 switch (constraintType) {73 case UNIQUE:74 List<String> uniqueColumnNames = Arrays.stream(columnList.split(",")).map(String::trim).collect(Collectors.toList());75 constraint = new DbTableUniqueConstraint(tableName, uniqueColumnNames);76 tableCheckExpressions.add(constraint);77 break;78 case PRIMARY_KEY:79 case PRIMARY_KEY_BLANK:80 case REFERENTIAL:81 /**82 * This type of constraint is already handled by83 * JDBC Metadata84 **/85 break;86 case CHECK:87 constraint = new DbTableCheckExpression(tableName, sqlCheckExpression);88 tableCheckExpressions.add(constraint);89 break;90 default:91 cannotHandle(constraintType);92 }93 }94 }95 }96 }97 return tableCheckExpressions;98 }99 /**100 * For each table in the schema DTO, this method appends101 * the constraints that are originated in the CREATE TABLE commands102 * for those particular tables.103 * <p>104 * Unique constraints and Foreign keys are handled separately in the JDBC metadata105 *106 * @param connectionToH2 a connection to a H2 database107 * @param schemaDto DTO with database schema information108 * @throws SQLException if the connection to the database fails109 */110 private List<DbTableConstraint> extractColumnConstraints(Connection connectionToH2, DbSchemaDto schemaDto) throws SQLException {111 String tableSchema = schemaDto.name;112 List<DbTableConstraint> columnConstraints = new ArrayList<>();113 for (TableDto tableDto : schemaDto.tables) {114 String tableName = tableDto.name;115 try (Statement statement = connectionToH2.createStatement()) {116 final String query = String.format("Select * From INFORMATION_SCHEMA.COLUMNS where COLUMNS.TABLE_SCHEMA='%s' and COLUMNS.TABLE_NAME='%s' ", tableSchema, tableName);117 try (ResultSet columns = statement.executeQuery(query)) {118 while (columns.next()) {119 String sqlCheckExpression = columns.getString("CHECK_CONSTRAINT");120 if (sqlCheckExpression != null && !sqlCheckExpression.equals("")) {121 DbTableCheckExpression constraint = new DbTableCheckExpression(tableName, sqlCheckExpression);122 columnConstraints.add(constraint);123 }124 }125 }126 }...

Full Screen

Full Screen

DbTableConstraint

Using AI Code Generation

copy

Full Screen

1import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseCommandDto;2import org.evomaster.client.java.controller.api.dto.database.operations.InsertionDto;3import org.evomaster.client.java.controller.api.dto.database.schema.DbTableConstraint;4import org.evomaster.client.java.controller.api.dto.database.schema.TableDto;5import org.evomaster.client.java.controller.internal.db.DataRow;6import org.evomaster.client.java.controller.internal.db.constraint.DbConstraintHandler;7import org.evomaster.client.java.controller.internal.db.constraint.ForeignKeyConstraintHandler;8import org.evomaster.client.java.controller.internal.db.constraint.UniqueConstraintHandler;9import org.evomaster.client.java.controller.internal.db.schema.SchemaExtractor;10import org.evomaster.client.java.controller.internal.db.sql.SqlInsertBuilder;11import org.evomaster.client.java.controller.internal.db.sql.SqlScriptExecutor;12import java.sql.Connection;13import java.sql.SQLException;14import java.util.ArrayList;15import java.util.List;16public class InsertionCommand extends DatabaseCommand {17 private final TableDto table;18 private final DataRow dataRow;19 private final List<DbTableConstraint> constraints;20 private final List<DbConstraintHandler> constraintHandlers;21 public InsertionCommand(TableDto table, DataRow dataRow, List<DbTableConstraint> constraints) {22 this.table = table;23 this.dataRow = dataRow;24 this.constraints = constraints;25 this.constraintHandlers = new ArrayList<>();26 this.constraintHandlers.add(new ForeignKeyConstraintHandler());27 this.constraintHandlers.add(new UniqueConstraintHandler());28 }29 public DatabaseCommandDto execute(String databaseType, Connection connection) throws SQLException {30 SqlInsertBuilder builder = new SqlInsertBuilder(databaseType);31 String sql = builder.getInsertionSql(table, dataRow);32 SqlScriptExecutor executor = new SqlScriptExecutor(connection);33 executor.execute(sql);34 DatabaseCommandDto dto = new DatabaseCommandDto();35 InsertionDto insertionDto = new InsertionDto();36 insertionDto.setTableName(table.getName());37 insertionDto.setRow(dataRow);38 dto.setInsertion(insertionDto);39 return dto;40 }41 public boolean isExecutable(String databaseType, Connection connection) throws SQLException {

Full Screen

Full Screen

DbTableConstraint

Using AI Code Generation

copy

Full Screen

1import org.evomaster.client.java.controller.internal.db.constraint.DbTableConstraint;2import org.evomaster.client.java.controller.internal.db.constraint.DbTableConstraintType;3import java.sql.Connection;4import java.sql.DriverManager;5import java.sql.SQLException;6import java.util.ArrayList;7import java.util.List;8public class DbTableConstraintExample {9 private static Connection getConnection() throws SQLException {10 }11 public static void main(String[] args) throws SQLException {12 Connection connection = getConnection();13 List<DbTableConstraint> constraints = DbTableConstraint.getConstraints(connection, "public", "test_table");14 System.out.println(constraints);15 }16}17[DbTableConstraint{table='test_table', name='test_table_pkey', type=PRIMARY_KEY, columns=[id], checkClause='null', expression='null', deferrable=false, initiallyDeferred=false, isNoInherit=false, isDeferrable=false, isInitiallyDeferred=false, isNotValid=false, isNotNull=false, isNotNullable=false, isNotNullColumns=false, isNoInherit=false, isNoInheritColumns=false, isNotNullColumns=false, isNotNullableColumns=false, isNotNullable=false, isNotNull=false, isDeferrable=false, isInitiallyDeferred=false, isNotValid=false, isNoInherit=false, isDeferrable=false, isInitiallyDeferred=false, isNotValid=false, isNotNull=false, isNotNullable=false, isNotNullColumns=false, isNoInherit=false, isNoInheritColumns=false, isNotNullColumns=false, isNotNullableColumns=false, isNotNullable=false, isNotNull=false, isDeferrable=false, isInitiallyDeferred=false, isNotValid=false, isNoInherit=false, isDeferrable=false, isInitiallyDeferred=false, isNotValid=false, isNotNull=false, isNotNullable=false, isNotNullColumns=false, isNoInherit=false, isNoInheritColumns=false, isNotNullColumns=false, isNotNullableColumns=false, isNotNullable=false, isNotNull=false, isDeferrable=false, isInitiallyDeferred=false, isNotValid=false, isNoInherit=false, isDeferrable=false, isInitiallyDeferred=false, isNotValid=false, isNotNull=false, isNotNullable=false, isNotNullColumns=false, isNoInherit=false,

Full Screen

Full Screen

DbTableConstraint

Using AI Code Generation

copy

Full Screen

1import org.evomaster.client.java.controller.internal.db.constraint.DbTableConstraint;2public class DbTableConstraintExample {3 public static void main(String[] args) {4 DbTableConstraint tableConstraint = new DbTableConstraint();5 tableConstraint.setTableName("table1");6 tableConstraint.setColumnName("column1");7 tableConstraint.setConstraintName("constraint1");8 tableConstraint.setConstraintType("type1");9 tableConstraint.setCheckExpression("check1");10 tableConstraint.setForeignKey("fk1");11 tableConstraint.setForeignKeyTable("fkTable1");12 tableConstraint.setForeignKeyColumn("fkColumn1");13 tableConstraint.setUniqueConstraint("unique1");14 tableConstraint.setPrimaryKey("pk1");15 System.out.println(tableConstraint.getTableName());16 System.out.println(tableConstraint.getColumnName());17 System.out.println(tableConstraint.getConstraintName());18 System.out.println(tableConstraint.getConstraintType());19 System.out.println(tableConstraint.getCheckExpression());20 System.out.println(tableConstraint.getForeignKey());21 System.out.println(tableConstraint.getForeignKeyTable());22 System.out.println(tableConstraint.getForeignKeyColumn());23 System.out.println(tableConstraint.getUniqueConstraint());24 System.out.println(tableConstraint.getPrimaryKey());25 }26}27import org.evomaster.client.java.controller.internal.db.constraint.DbTableConstraint28fun main(args: Array<String>) {29 val tableConstraint = DbTableConstraint()30 println(tableConstraint.tableName)31 println(tableConstraint.columnName)32 println(tableConstraint.constraintName)33 println(tableConstraint.constraintType)34 println(tableConstraint.checkExpression)35 println(tableConstraint.foreignKey)36 println(tableConstraint.foreignKeyTable)37 println(tableConstraint.foreignKeyColumn)38 println(tableConstraint.uniqueConstraint)39 println(tableConstraint.primaryKey)40}

Full Screen

Full Screen

DbTableConstraint

Using AI Code Generation

copy

Full Screen

1package org.evomaster.client.java.controller.internal.db.constraint;2import java.sql.DatabaseMetaData;3import java.sql.ResultSet;4import java.sql.SQLException;5import java.util.ArrayList;6import java.util.List;7public class TableConstraintInfo {8 private final String tableName;9 private final List<DbTableConstraint> constraints;10 public TableConstraintInfo(String tableName, List<DbTableConstraint> constraints) {11 this.tableName = tableName;12 this.constraints = constraints;13 }14 public String getTableName() {15 return tableName;16 }17 public List<DbTableConstraint> getConstraints() {18 return constraints;19 }20 public static List<TableConstraintInfo> getTableConstraintInfo(DatabaseMetaData metaData, String schema, String tableName) throws SQLException {21 List<TableConstraintInfo> constraints = new ArrayList<>();22 try (ResultSet rs = metaData.getImportedKeys(null, schema, tableName)) {23 while (rs.next()) {24 String pkTableName = rs.getString("PKTABLE_NAME");25 String pkColumnName = rs.getString("PKCOLUMN_NAME");26 String fkColumnName = rs.getString("FKCOLUMN_NAME");27 String fkName = rs.getString("FK_NAME");28 constraints.add(new TableConstraintInfo(pkTableName, List.of(new DbTableConstraint(pkColumnName, fkColumnName, fkName))));29 }30 }31 return constraints;32 }33}34package org.evomaster.client.java.controller.internal.db.constraint;35import java.util.List;36public class TableConstraintInfo {37 private final String tableName;38 private final List<DbTableConstraint> constraints;39 public TableConstraintInfo(String tableName, List<DbTableConstraint> constraints) {40 this.tableName = tableName;41 this.constraints = constraints;42 }43 public String getTableName() {44 return tableName;45 }46 public List<DbTableConstraint> getConstraints() {47 return constraints;48 }49}50package org.evomaster.client.java.controller.internal.db.constraint;51public class DbTableConstraint {52 private final String pkColumnName;53 private final String fkColumnName;54 private final String fkName;55 public DbTableConstraint(String pkColumnName, String fkColumnName, String fkName) {56 this.pkColumnName = pkColumnName;57 this.fkColumnName = fkColumnName;58 this.fkName = fkName;59 }60 public String getPkColumnName() {

Full Screen

Full Screen

DbTableConstraint

Using AI Code Generation

copy

Full Screen

1package org.evomaster.client.java.controller.api.dto.database.operations;2import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType;3import org.evomaster.client.java.controller.api.dto.database.schema.Table;4import org.evomaster.client.java.controller.api.dto.database.schema.TableColumn;5import org.evomaster.client.java.controller.api.dto.database.schema.TableIndex;6import org.evomaster.client.java.controller.api.dto.database.schema.TableIndexColumn;7import org.evomaster.client.java.controller.api.dto.database.schema.TablePrimaryKey;8import org.evomaster.client.java.controller.api.dto.database.schema.TableUniqueKey;9import org.evomaster.client.java.controller.api.dto.database.schema.TableUniqueKeyColumn;10import org.evomaster.client.java.controller.api.dto.database.operations.QueryDto;11import org.evomaster.client.java.controller.api.dto.database.operations.QueryResultDto;12import org.evomaster.client.java.controller.api.dto.database.operations.SqlScriptDto;13import org.evomaster.client.java.controller.api.dto.database.operations.SqlScriptResultDto;14import org.evomaster.client.java.controller.internal.db.SqlScriptExecutor;15import org.evomaster.client.java.controller.internal.db.SqlScriptRunner;16import org.evomaster.client.java.controller.internal.db.TableSchemaExtractor;17import org.evomaster.client.java.controller.internal.db.constraint.DbTableConstraint;18import org.evomaster.client.java.controll

Full Screen

Full Screen

DbTableConstraint

Using AI Code Generation

copy

Full Screen

1DbTableConstraint tableConstraint = new DbTableConstraint();2tableConstraint.setTableName("table_name");3tableConstraint.setPrimaryKey("primary_key");4tableConstraint.setUniqueColumns(Arrays.asList("unique_column"));5tableConstraint.setNotNullColumns(Arrays.asList("not_null_column"));6tableConstraint.setForeignKeyColumns(Arrays.asList("foreign_key_column"));7tableConstraint.setForeignKeyRefTable("foreign_key_ref_table");8tableConstraint.setForeignKeyRefColumns(Arrays.asList("foreign_key_ref_column"));9tableConstraint.setCheckConstraints(Arrays.asList("check_constraint"));10tableConstraint.setDefaultValueColumns(Arrays.asList("default_value_column"));11tableConstraint.setDefaultValueValues(Arrays.asList("default_value_value"));12DbTableData tableData = new DbTableData();13tableData.setTableName("table_name");14tableData.addColumnData("column_name", Arrays.asList("column_value"));15DbInitialization dbInitialization = new DbInitialization();16dbInitialization.addTableConstraint(tableConstraint);17dbInitialization.addTableData(tableData);18DbActionBuilder dbActionBuilder = new DbActionBuilder();19dbActionBuilder.addInitialization(dbInitialization);20DbAction dbAction = dbActionBuilder.build();21DbActionBuilder dbActionBuilder = new DbActionBuilder();22dbActionBuilder.addInitialization(dbInitialization);23DbAction dbAction = dbActionBuilder.build();24em.addInitialAction(dbAction);25em.addInitialAction(dbAction);26RestCallActionBuilder restCallActionBuilder = new RestCallActionBuilder();27restCallActionBuilder.setHttpMethod(HttpMethod.POST);28restCallActionBuilder.setHost("localhost");29restCallActionBuilder.setPort(8080);30restCallActionBuilder.setScheme(Scheme.HTTP);31restCallActionBuilder.setPath("/api/path");

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.

Run EvoMaster automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in DbTableConstraint

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful