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

Best EvoMaster code snippet using org.evomaster.client.java.controller.internal.db.constraint.DbTableConstraint.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.schema.DatabaseType;2import org.evomaster.client.java.controller.api.dto.database.schema.DbColumnDto;3import org.evomaster.client.java.controller.api.dto.database.schema.DbTableDto;4import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseCommandDto;5import org.evomaster.client.java.controller.api.dto.database.operations.InsertionDto;6import org.evomaster.client.java.controller.api.dto.database.operations.TableConstraintDto;7import org.evomaster.client.java.controller.api.dto.database.operations.TableRowDto;8import org.evomaster.client.java.controller.internal.db.constraint.DbTableConstraint;9import org.evomaster.client.java.controller.internal.db.constraint.TableConstraint;10import java.util.ArrayList;11import java.util.List;12public class DbTableConstraintExample {13 public static void main(String[] args) {14 DbTableDto table = new DbTableDto("table", "schema");15 table.addColumn(new DbColumnDto("id", DatabaseType.INTEGER, true, true));16 table.addColumn(new DbColumnDto("name", DatabaseType.VARCHAR, true, false));17 table.addColumn(new DbColumnDto("surname", DatabaseType.VARCHAR, true, false));18 table.addColumn(new DbColumnDto("age", DatabaseType.INTEGER, true, false));19 table.addColumn(new DbColumnDto("address", DatabaseType.VARCHAR, true, false));20 List<DatabaseCommandDto> commands = new ArrayList<>();21 List<TableRowDto> rows = new ArrayList<>();22 TableRowDto row = new TableRowDto();23 row.addColumnValue("id", 1);24 row.addColumnValue("name", "John");25 row.addColumnValue("surname", "Doe");26 row.addColumnValue("age", 30);27 row.addColumnValue("address", "London");28 rows.add(row);29 row = new TableRowDto();30 row.addColumnValue("id", 2);31 row.addColumnValue("name", "Jane");32 row.addColumnValue("surname", "Doe");33 row.addColumnValue("age", 25);34 row.addColumnValue("address", "Paris");35 rows.add(row);

Full Screen

Full Screen

DbTableConstraint

Using AI Code Generation

copy

Full Screen

1public class DbTableConstraint {2 private String name;3 private String table;4 private String column;5 private String foreignTable;6 private String foreignColumn;7 private String type;8 private String onDelete;9 private String onUpdate;10 private boolean isNullable;11 private String defaultValue;12 private String checkExpression;13 private List<String> columns;14 private List<String> foreignColumns;15 private List<String> foreignTables;16 private List<String> foreignColumnsInTable;17 private List<String> foreignTablesInTable;18 private String foreignTableInTable;19 private String foreignColumnInTable;20 private boolean isPrimary;21 private String primaryColumn;22 private String primaryTable;23 private String primaryTableInTable;24 private String primaryColumnInTable;25 private List<String> primaryColumns;26 private List<String> primaryTables;27 private List<String> primaryColumnsInTable;28 private List<String> primaryTablesInTable;29 private boolean isUnique;30 private String uniqueColumn;31 private String uniqueTable;32 private String uniqueTableInTable;33 private String uniqueColumnInTable;34 private List<String> uniqueColumns;35 private List<String> uniqueTables;36 private List<String> uniqueColumnsInTable;37 private List<String> uniqueTablesInTable;38 private boolean isForeignKey;39 private String foreignKeyTable;40 private String foreignKeyColumn;41 private String foreignKeyTableInTable;42 private String foreignKeyColumnInTable;43 private List<String> foreignKeyTables;44 private List<String> foreignKeyColumns;45 private List<String> foreignKeyTablesInTable;46 private List<String> foreignKeyColumnsInTable;47 private boolean isForeignKeyInTable;48 private List<String> foreignKeyTablesInTable;49 private List<String> foreignKeyColumnsInTable;50 private String foreignKeyTableInTable;51 private String foreignKeyColumnInTable;52 private boolean isPrimaryKeyInTable;53 private List<String> primaryKeyTablesInTable;54 private List<String> primaryKeyColumnsInTable;55 private String primaryKeyTableInTable;56 private String primaryKeyColumnInTable;57 private boolean isUniqueKeyInTable;58 private List<String> uniqueKeyTablesInTable;59 private List<String> uniqueKeyColumnsInTable;60 private String uniqueKeyTableInTable;61 private String uniqueKeyColumnInTable;62 private boolean isForeignKeyInTable;

Full Screen

Full Screen

DbTableConstraint

Using AI Code Generation

copy

Full Screen

1DbTableConstraint dbTableConstraint = new DbTableConstraint();2Map<String, List<Constraint>> tableConstraints = new HashMap<>();3List<Constraint> constraints = new ArrayList<>();4Constraint constraint = new Constraint();5constraint.setName("PRIMARY");6constraint.setType(Constraint.ConstraintType.PRIMARY_KEY);7constraint.setColumns(Arrays.asList("id"));8constraints.add(constraint);9tableConstraints.put("users", constraints);10dbTableConstraint.insertData(tableConstraints);11DbTableConstraint dbTableConstraint = new DbTableConstraint();12Map<String, List<Constraint>> tableConstraints = new HashMap<>();13List<Constraint> constraints = new ArrayList<>();14Constraint constraint = new Constraint();15constraint.setName("PRIMARY");16constraint.setType(Constraint.ConstraintType.PRIMARY_KEY);17constraint.setColumns(Arrays.asList("id"));18constraints.add(constraint);19tableConstraints.put("users", constraints);20dbTableConstraint.insertData(tableConstraints);21DbTableConstraint dbTableConstraint = new DbTableConstraint();22Map<String, List<Constraint>> tableConstraints = new HashMap<>();23List<Constraint> constraints = new ArrayList<>();24Constraint constraint = new Constraint();25constraint.setName("PRIMARY");26constraint.setType(Constraint.ConstraintType.PRIMARY_KEY);27constraint.setColumns(Arrays.asList("id"));28constraints.add(constraint);29tableConstraints.put("users", constraints);

Full Screen

Full Screen

DbTableConstraint

Using AI Code Generation

copy

Full Screen

1List<String> tables = getTableNamesFromSchema(schema);2DbTableConstraint dbTableConstraint = new DbTableConstraint();3for(String tableName : tables){4 List<DbConstraint> dbConstraints = dbTableConstraint.getConstraintsForTable(tableName, schema, em);5 System.out.println("Table: " + tableName);6 for(DbConstraint dbConstraint : dbConstraints){7 System.out.println(dbConstraint);8 }9}10public static List<String> getTableNamesFromSchema(Schema schema){11 List<String> tables = new ArrayList<>();12 for(Table table : schema.getTables()){13 tables.add(table.getName());14 }15 return tables;16}17public static List<DbConstraint> getConstraintsForTable(String tableName, Schema schema, EMConfig em){18 List<DbConstraint> constraints = new ArrayList<>();19 Table table = schema.getTable(tableName);20 for(Constraint constraint : table.getConstraints()){21 if(constraint instanceof PrimaryKey){22 constraints.add(new PrimaryKeyConstraint((PrimaryKey) constraint, tableName, em));23 }24 else if(constraint instanceof ForeignKey){25 constraints.add(new ForeignKeyConstraint((ForeignKey) constraint, tableName, em));26 }27 else if(constraint instanceof Unique){28 constraints.add(new UniqueConstraint((Unique) constraint, tableName, em));29 }30 else if(constraint instanceof Check){31 constraints.add(new CheckConstraint((Check) constraint, tableName, em));32 }33 }34 return constraints;35}36public static class PrimaryKeyConstraint implements DbConstraint{37 private final PrimaryKey pk;38 private final String tableName;39 private final EMConfig em;40 public PrimaryKeyConstraint(PrimaryKey pk, String tableName, EMConfig em){41 this.pk = pk;42 this.tableName = tableName;43 this.em = em;44 }45 public String getConstraintName() {46 return pk.getName();47 }48 public String getTableName() {49 return tableName;50 }

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 method in DbTableConstraint

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful