How to use TableConstraint class of org.evomaster.dbconstraint package

Best EvoMaster code snippet using org.evomaster.dbconstraint.TableConstraint

Source:SqlConditionTranslator.java Github

copy

Full Screen

...25import java.util.List;26import java.util.Set;27import java.util.stream.Collectors;28import static org.evomaster.dbconstraint.ast.SqlComparisonOperator.EQUALS_TO;29public class SqlConditionTranslator implements SqlConditionVisitor<TableConstraint, Void> {30 private static final String THIS_METHOD_SHOULD_NOT_BE_INVOKED = "This method should not be directly called";31 public static final String UNEXPECTED_COMPARISON_OPERATOR_MESSAGE = "Unexpected comparison operator ";32 private final TranslationContext translationContext;33 public SqlConditionTranslator(TranslationContext translationContext) {34 this.translationContext = translationContext;35 }36 /**37 * FIXME38 * temporary workaround before major refactoring.39 * Recall that Column.getTable() is not reliable40 */41 private String getTableName(SqlColumn column) {42 String tableName = column.getTableName();43 if (tableName != null) {44 return tableName;45 } else {46 return this.translationContext.getCurrentTableName();47 }48 }49 @Override50 public TableConstraint visit(SqlAndCondition andExpression, Void argument) {51 TableConstraint left = andExpression.getLeftExpr().accept(this, null);52 TableConstraint right = andExpression.getRightExpr().accept(this, null);53 return new AndConstraint(translationContext.getCurrentTableName(), left, right);54 }55 @Override56 public TableConstraint visit(SqlComparisonCondition e, Void argument) {57 SqlCondition left = e.getLeftOperand();58 SqlCondition right = e.getRightOperand();59 if (left instanceof SqlLiteralValue && right instanceof SqlColumn) {60 SqlLiteralValue leftLiteral = (SqlLiteralValue) left;61 SqlColumn rightColumn = (SqlColumn) right;62 return visit(leftLiteral, e, rightColumn);63 } else if (left instanceof SqlColumn && right instanceof SqlLiteralValue) {64 SqlColumn leftColumn = (SqlColumn) left;65 SqlLiteralValue rightLiteral = (SqlLiteralValue) right;66 return visit(leftColumn, e, rightLiteral);67 } else if (left instanceof SqlCondition && right instanceof SqlCondition) {68 TableConstraint leftTableConstraint = e.getLeftOperand().accept(this, null);69 TableConstraint rightTableConstraint = e.getRightOperand().accept(this, null);70 if (e.getSqlComparisonOperator().equals(EQUALS_TO)) {71 return new IffConstraint(translationContext.getCurrentTableName(), leftTableConstraint, rightTableConstraint);72 }73 }74 // TODO This translation should be implemented75 throw new SqlCannotBeTranslatedException(e.toSql() + " cannot be translated yet");76 }77 private TableConstraint visit(SqlColumn leftColumn, SqlComparisonCondition e, SqlLiteralValue rightLiteral) {78 final String tableName = getTableName(leftColumn);79 final String columnName = leftColumn.getColumnName();80 if (rightLiteral instanceof SqlBigIntegerLiteralValue) {81 long value = ((SqlBigIntegerLiteralValue) rightLiteral).getBigInteger().longValue();82 switch (e.getSqlComparisonOperator()) {83 case EQUALS_TO:84 return new RangeConstraint(tableName, columnName, value, value);85 case GREATER_THAN:86 return new LowerBoundConstraint(tableName, columnName, value + 1);87 case GREATER_THAN_OR_EQUAL:88 return new LowerBoundConstraint(tableName, columnName, value);89 case LESS_THAN:90 return new UpperBoundConstraint(tableName, columnName, value - 1);91 case LESS_THAN_OR_EQUAL:92 return new UpperBoundConstraint(tableName, columnName, value);93 default:94 throw new UnsupportedOperationException(UNEXPECTED_COMPARISON_OPERATOR_MESSAGE + e.getSqlComparisonOperator());95 }96 } else if (rightLiteral instanceof SqlStringLiteralValue) {97 SqlStringLiteralValue stringLiteralValue = (SqlStringLiteralValue) rightLiteral;98 if (e.getSqlComparisonOperator().equals(EQUALS_TO)) {99 return new EnumConstraint(tableName, columnName, Collections.singletonList(stringLiteralValue.getStringValue()));100 } else {101 throw new UnsupportedOperationException(UNEXPECTED_COMPARISON_OPERATOR_MESSAGE + e.getSqlComparisonOperator());102 }103 } else {104 throw new UnsupportedOperationException("Unsupported literal " + rightLiteral);105 }106 }107 private TableConstraint visit(SqlLiteralValue leftLiteral, SqlComparisonCondition e, SqlColumn rightColumn) {108 if (leftLiteral instanceof SqlBigIntegerLiteralValue) {109 long value = ((SqlBigIntegerLiteralValue) leftLiteral).getBigInteger().longValue();110 final String tableName = getTableName(rightColumn);111 final String columnName = rightColumn.getColumnName();112 switch (e.getSqlComparisonOperator()) {113 case EQUALS_TO:114 return new RangeConstraint(tableName, columnName, value, value);115 case GREATER_THAN:116 return new UpperBoundConstraint(tableName, columnName, value - 1);117 case GREATER_THAN_OR_EQUAL:118 return new UpperBoundConstraint(tableName, columnName, value);119 case LESS_THAN:120 return new LowerBoundConstraint(tableName, columnName, value + 1);121 case LESS_THAN_OR_EQUAL:122 return new LowerBoundConstraint(tableName, columnName, value);123 default:124 throw new UnsupportedOperationException(UNEXPECTED_COMPARISON_OPERATOR_MESSAGE + e.getSqlComparisonOperator());125 }126 } else {127 throw new UnsupportedOperationException("Unsupported literal " + e.getSqlComparisonOperator());128 }129 }130 @Override131 public TableConstraint visit(SqlInCondition inExpression, Void argument) {132 SqlColumn column = inExpression.getSqlColumn();133 String tableName = getTableName(column);134 String columnName = column.getColumnName();135 SqlConditionList rightItemsList = inExpression.getLiteralList();136 List<String> stringValues = new ArrayList<>();137 for (SqlCondition expressionValue : rightItemsList.getSqlConditionExpressions()) {138 final String stringValue;139 if (expressionValue instanceof SqlStringLiteralValue) {140 stringValue = new StringValue(expressionValue.toSql()).getNotExcapedValue();141 } else {142 stringValue = expressionValue.toSql();143 }144 stringValues.add(stringValue);145 }146 return new EnumConstraint(tableName, columnName, stringValues);147 }148 @Override149 public TableConstraint visit(SqlNullLiteralValue e, Void argument) {150 throw new UnsupportedOperationException(THIS_METHOD_SHOULD_NOT_BE_INVOKED);151 }152 @Override153 public TableConstraint visit(SqlStringLiteralValue e, Void argument) {154 throw new UnsupportedOperationException(THIS_METHOD_SHOULD_NOT_BE_INVOKED);155 }156 @Override157 public TableConstraint visit(SqlConditionList e, Void argument) {158 throw new UnsupportedOperationException(THIS_METHOD_SHOULD_NOT_BE_INVOKED);159 }160 @Override161 public TableConstraint visit(SqlIsNotNullCondition e, Void argument) {162 String tableName = getTableName(e.getColumn());163 String columnName = e.getColumn().getColumnName();164 return new IsNotNullConstraint(tableName, columnName);165 }166 @Override167 public TableConstraint visit(SqlBinaryDataLiteralValue e, Void argument) {168 throw new UnsupportedOperationException(THIS_METHOD_SHOULD_NOT_BE_INVOKED);169 }170 @Override171 public TableConstraint visit(SqlSimilarToCondition e, Void argument) {172 String tableName = getTableName(e.getColumn());173 final String pattern = e.getPattern().getStringValue();174 return new SimilarToConstraint(tableName, e.getColumn().getColumnName(), pattern, translationContext.getDatabaseType());175 }176 @Override177 public TableConstraint visit(SqlIsNullCondition e, Void argument) {178 throw new UnsupportedOperationException(THIS_METHOD_SHOULD_NOT_BE_INVOKED);179 }180 @Override181 public TableConstraint visit(SqlLikeCondition e, Void argument) {182 String tableName = getTableName(e.getColumnName());183 String columnName = e.getColumnName().getColumnName();184 String pattern = e.getPattern().getStringValue();185 return new LikeConstraint(tableName, columnName, pattern, translationContext.getDatabaseType());186 }187 @Override188 public TableConstraint visit(SqlOrCondition e, Void argument) {189 List<TableConstraint> orConstraints = e.getOrConditions().stream().map(c -> c.accept(this, null)).collect(Collectors.toList());190 if (orConstraints.stream().allMatch(c -> c instanceof LikeConstraint)) {191 // all like constraints should have the same tablename, column and database type192 Set<String> columnNames = orConstraints193 .stream()194 .map(c -> (LikeConstraint) c)195 .map(c -> c.getColumnName())196 .collect(Collectors.toSet());197 Set<String> tableNames = orConstraints198 .stream()199 .map(c -> (LikeConstraint) c)200 .map(c -> c.getTableName())201 .collect(Collectors.toSet());202 Set<ConstraintDatabaseType> databaseTypes = orConstraints203 .stream().map(c -> (LikeConstraint) c)204 .map(c -> c.getDatabaseType())205 .collect(Collectors.toSet());206 if (columnNames.size() == 1 && tableNames.size() == 1 && databaseTypes.size() == 1) {207 String tableName = tableNames.iterator().next();208 String columnName = columnNames.iterator().next();209 ConstraintDatabaseType databaseType = databaseTypes.iterator().next();210 List<String> patterns = orConstraints211 .stream()212 .map(c -> (LikeConstraint) c)213 .map(c -> c.getPatterns())214 .flatMap(List::stream)215 .collect(Collectors.toList());216 return new LikeConstraint(tableName,columnName,patterns,databaseType);217 }218 }219 return new OrConstraint(translationContext.getCurrentTableName(), orConstraints.toArray(new TableConstraint[]{}));220 }221 @Override222 public TableConstraint visit(SqlBigDecimalLiteralValue e, Void argument) {223 throw new UnsupportedOperationException(THIS_METHOD_SHOULD_NOT_BE_INVOKED);224 }225 @Override226 public TableConstraint visit(SqlBigIntegerLiteralValue e, Void argument) {227 throw new UnsupportedOperationException(THIS_METHOD_SHOULD_NOT_BE_INVOKED);228 }229 @Override230 public TableConstraint visit(SqlBooleanLiteralValue e, Void argument) {231 throw new UnsupportedOperationException(THIS_METHOD_SHOULD_NOT_BE_INVOKED);232 }233 @Override234 public TableConstraint visit(SqlColumn e, Void argument) {235 throw new UnsupportedOperationException(THIS_METHOD_SHOULD_NOT_BE_INVOKED);236 }237}...

Full Screen

Full Screen

TableConstraint

Using AI Code Generation

copy

Full Screen

1 def tableConstraint = new TableConstraint()2 def columnConstraint = new ColumnConstraint()3 def columnConstraint1 = new ColumnConstraint()4 def columnConstraint2 = new ColumnConstraint()5 def columnConstraint3 = new ColumnConstraint()6 def columnConstraint4 = new ColumnConstraint()7 def columnConstraint5 = new ColumnConstraint()8 def columnConstraint6 = new ColumnConstraint()9 def columnConstraint7 = new ColumnConstraint()10 def columnConstraint8 = new ColumnConstraint()11 def columnConstraint9 = new ColumnConstraint()12 def columnConstraint10 = new ColumnConstraint()13 def columnConstraint11 = new ColumnConstraint()14 def columnConstraint12 = new ColumnConstraint()15 def columnConstraint13 = new ColumnConstraint()16 def columnConstraint14 = new ColumnConstraint()17 def columnConstraint15 = new ColumnConstraint()18 def columnConstraint16 = new ColumnConstraint()19 def columnConstraint17 = new ColumnConstraint()20 def columnConstraint18 = new ColumnConstraint()21 def columnConstraint19 = new ColumnConstraint()22 def columnConstraint20 = new ColumnConstraint()23 def columnConstraint21 = new ColumnConstraint()24 def columnConstraint22 = new ColumnConstraint()25 def columnConstraint23 = new ColumnConstraint()26 def columnConstraint24 = new ColumnConstraint()27 def columnConstraint25 = new ColumnConstraint()28 def columnConstraint26 = new ColumnConstraint()29 def columnConstraint27 = new ColumnConstraint()30 def columnConstraint28 = new ColumnConstraint()31 def columnConstraint29 = new ColumnConstraint()32 def columnConstraint30 = new ColumnConstraint()33 def columnConstraint31 = new ColumnConstraint()34 def columnConstraint32 = new ColumnConstraint()35 def columnConstraint33 = new ColumnConstraint()36 def columnConstraint34 = new ColumnConstraint()37 def columnConstraint35 = new ColumnConstraint()38 def columnConstraint36 = new ColumnConstraint()39 def columnConstraint37 = new ColumnConstraint()40 def columnConstraint38 = new ColumnConstraint()41 def columnConstraint39 = new ColumnConstraint()42 def columnConstraint40 = new ColumnConstraint()43 def columnConstraint41 = new ColumnConstraint()44 def columnConstraint42 = new ColumnConstraint()45 def columnConstraint43 = new ColumnConstraint()46 def columnConstraint44 = new ColumnConstraint()47 def columnConstraint45 = new ColumnConstraint()48 def columnConstraint46 = new ColumnConstraint()49 def columnConstraint47 = new ColumnConstraint()50 def columnConstraint48 = new ColumnConstraint()

Full Screen

Full Screen

TableConstraint

Using AI Code Generation

copy

Full Screen

1TableConstraint tableConstraint = new TableConstraint("EMPLOYEE");2tableConstraint.addPrimaryKeyConstraint("ID");3tableConstraint.addUniqueConstraint("NAME");4tableConstraint.addNotNullConstraint("NAME");5tableConstraint.addForeignKeyConstraint("DEPARTMENT_ID", "DEPARTMENT", "ID");6tableConstraint.addCheckConstraint("SALARY", "> 0");7tableConstraint.addCheckConstraint("NAME", "LIKE 'A%'");8TableConstraintParser parser = new TableConstraintParser();9String sql = parser.parse(tableConstraint);10System.out.println(sql);11CREATE TABLE EMPLOYEE (12 NAME VARCHAR(255) NOT NULL UNIQUE,13 SALARY INT CHECK (SALARY > 0),14 DEPARTMENT_ID INT REFERENCES DEPARTMENT(ID),15 CHECK (NAME LIKE 'A%')16);17TableConstraintParser parser = new TableConstraintParser();18TableConstraint tableConstraint = parser.parse("CREATE TABLE EMPLOYEE (ID INT PRIMARY KEY, NAME VARCHAR(255) NOT NULL UNIQUE, SALARY INT CHECK (SALARY > 0), DEPARTMENT_ID INT REFERENCES DEPARTMENT(ID), CHECK (NAME LIKE 'A%'))");19System.out.println(tableConstraint);20TableConstraint(table=EMPLOYEE, columns=[Column(name=ID, type=INT, nullable=false, primaryKey=true, unique=false, check=null), Column(name=NAME, type=VARCHAR(255), nullable=false, primaryKey=false, unique=true, check=null), Column(name=SALARY, type=INT, nullable=false, primaryKey=false, unique=false, check=Check(name=null, expression=SALARY > 0)), Column(name=DEPARTMENT_ID, type=INT, nullable=false, primaryKey=false, unique=false, check=null)], foreignKeys=[ForeignKey(column=DEPARTMENT_ID, table=DEPARTMENT, reference=ID)], checks=[Check(name=null, expression=NAME LIKE 'A%')])

Full Screen

Full Screen

TableConstraint

Using AI Code Generation

copy

Full Screen

1TableConstraint tableConstraint = new TableConstraint("tableName");2tableConstraint.addConstraint(new ColumnConstraint("col1", "col1Constraint"));3tableConstraint.addConstraint(new ColumnConstraint("col2", "col2Constraint"));4tableConstraint.addConstraint(new ColumnConstraint("col3", "col3Constraint"));5tableConstraint.addConstraint(new ColumnConstraint("col4", "col4Constraint"));6tableConstraint.addConstraint(new ColumnConstraint("col5", "col5Constraint"));7TableConstraint tableConstraint = new TableConstraint("tableName");8tableConstraint.addConstraint(new ColumnConstraint("col1", "col1Constraint"));9tableConstraint.addConstraint(new ColumnConstraint("col2", "col2Constraint"));10tableConstraint.addConstraint(new ColumnConstraint("col3", "col3Constraint"));11tableConstraint.addConstraint(new ColumnConstraint("col4", "col4Constraint"));12tableConstraint.addConstraint(new ColumnConstraint("col5", "col5Constraint"));13TableConstraint tableConstraint = new TableConstraint("tableName");14tableConstraint.addConstraint(new ColumnConstraint("col1", "col1Constraint"));15tableConstraint.addConstraint(new ColumnConstraint("col2", "col2Constraint"));16tableConstraint.addConstraint(new ColumnConstraint("col3", "col3Constraint"));17tableConstraint.addConstraint(new ColumnConstraint("col4", "col4Constraint"));18tableConstraint.addConstraint(new ColumnConstraint("col5", "col5Constraint"));19TableConstraint tableConstraint = new TableConstraint("tableName");20tableConstraint.addConstraint(new ColumnConstraint("col1", "col1Constraint"));21tableConstraint.addConstraint(new ColumnConstraint("col2", "col2Constraint"));22tableConstraint.addConstraint(new ColumnConstraint("col3", "col3Constraint"));23tableConstraint.addConstraint(new ColumnConstraint("col4", "col4Constraint"));24tableConstraint.addConstraint(new ColumnConstraint("col5", "col5Constraint"));25TableConstraint tableConstraint = new TableConstraint("tableName");26tableConstraint.addConstraint(new ColumnConstraint("col1", "col1Constraint"));27tableConstraint.addConstraint(new ColumnConstraint("col2", "col2Constraint"));28tableConstraint.addConstraint(new ColumnConstraint("col3", "col3Constraint"));

Full Screen

Full Screen

TableConstraint

Using AI Code Generation

copy

Full Screen

1TableConstraint tc = new TableConstraint();2tc.setTableName("table1");3tc.setColumnName("column1");4tc.setConstraintType(ConstraintType.UNIQUE);5tc.setConstraintName("unique_constraint");6tc.setConstraintStatus(ConstraintStatus.ENABLED);7tc.setConstraintExpression("column1");8tcList.add(tc);9TableConstraint tc2 = new TableConstraint();10tc2.setTableName("table1");11tc2.setColumnName("column2");12tc2.setConstraintType(ConstraintType.PRIMARY_KEY);13tc2.setConstraintName("primary_key_constraint");14tc2.setConstraintStatus(ConstraintStatus.ENABLED);15tc2.setConstraintExpression("column2");16tcList.add(tc2);17TableConstraint tc3 = new TableConstraint();18tc3.setTableName("table1");19tc3.setColumnName("column3");20tc3.setConstraintType(ConstraintType.FOREIGN_KEY);21tc3.setConstraintName("foreign_key_constraint");22tc3.setConstraintStatus(ConstraintStatus.ENABLED);23tc3.setConstraintExpression("column3");24tcList.add(tc3);25TableConstraint tc4 = new TableConstraint();26tc4.setTableName("table1");27tc4.setColumnName("column4");28tc4.setConstraintType(ConstraintType.CHECK);29tc4.setConstraintName("check_constraint");30tc4.setConstraintStatus(ConstraintStatus.ENABLED);

Full Screen

Full Screen

TableConstraint

Using AI Code Generation

copy

Full Screen

1import org.evomaster.dbconstraint.TableConstraint2import org.evomaster.dbconstraint.parser.Parser3import org.evomaster.dbconstraint.parser.TableConstraintParser4Map<String, String> columnTypes = new HashMap<>()5columnTypes.put("col1", "int")6columnTypes.put("col2", "varchar(10)")7columnTypes.put("col3", "varchar(10)")8columnTypes.put("col4", "varchar(10)")9columnTypes.put("col5", "varchar(10)")10columnTypes.put("col6", "varchar(10)")11columnTypes.put("col7", "varchar(10)")12columnTypes.put("col8", "varchar(10)")13columnTypes.put("col9", "varchar(10)")14columnTypes.put("col10", "varchar(10)")15columnTypes.put("col11", "varchar(10)")16columnTypes.put("col12", "varchar(10)")17columnTypes.put("col13", "varchar(10)")18columnTypes.put("col14", "varchar(10)")19columnTypes.put("col15", "varchar(10)")20columnTypes.put("col16", "varchar(10)")21columnTypes.put("col17", "varchar(10)")22columnTypes.put("col18", "varchar(10)")23columnTypes.put("col19", "varchar(10)")24columnTypes.put("col20", "varchar(10)")25columnTypes.put("col21", "varchar(10)")26columnTypes.put("col22", "varchar(10)")27columnTypes.put("col23", "varchar(10)")28columnTypes.put("col24", "varchar(10)")29columnTypes.put("col25", "varchar(10)")30columnTypes.put("col26", "varchar(10)")31columnTypes.put("col27", "varchar(10)")32columnTypes.put("col28", "varchar(10)")33columnTypes.put("col29", "varchar(10)")34columnTypes.put("col30", "varchar(10)")35columnTypes.put("col31", "varchar(10)")36columnTypes.put("col32", "varchar(10)")37columnTypes.put("col33", "varchar(10)")38columnTypes.put("col34", "varchar(10)")

Full Screen

Full Screen

TableConstraint

Using AI Code Generation

copy

Full Screen

1package org.evomaster.dbconstraint;2import java.util.ArrayList;3import java.util.List;4public class CustomerConstraints {5 public static String getConstraints(){6 TableConstraint table = new TableConstraint("Customer", "testdb");7 table.addPrimaryKey(new PrimaryKeyConstraint("CustomerID", "testdb", "Customer", "CustomerID"));8 table.addColumn(new ColumnConstraint("CustomerID", "testdb", "Customer", "CustomerID", "int", 10, 0, true, false, false, false));9 table.addColumn(new ColumnConstraint("CustomerName", "testdb", "Customer", "CustomerName", "varchar", 255, 0, false, false, false, false));10 table.addColumn(new ColumnConstraint("ContactName", "testdb", "Customer", "ContactName", "varchar", 255, 0, false, false, false, false));11 table.addColumn(new ColumnConstraint("Address", "testdb", "Customer", "Address", "varchar", 255, 0, false, false, false, false));12 table.addColumn(new ColumnConstraint("City", "testdb", "Customer", "City", "varchar", 255, 0, false, false, false, false));13 table.addColumn(new ColumnConstraint("PostalCode", "test

Full Screen

Full Screen

TableConstraint

Using AI Code Generation

copy

Full Screen

1create table t(a int, b int, c int, d int, e int, f int, g int, h int, i int, j int);2insert into t values (1, 1, 1, 1, 1, 1, 1, 1, 1, 1);3insert into t values (2, 2, 2, 2, 2, 2, 2, 2, 2, 2);4insert into t values (3, 3, 3, 3, 3, 3, 3, 3, 3, 3);5insert into t values (4, 4, 4, 4, 4, 4, 4, 4, 4, 4);6insert into t values (5, 5, 5, 5, 5, 5, 5, 5, 5, 5);7insert into t values (6, 6, 6, 6, 6, 6, 6, 6, 6, 6);8insert into t values (7, 7, 7, 7, 7, 7, 7, 7, 7, 7);9insert into t values (8, 8, 8, 8, 8, 8, 8, 8, 8, 8);10insert into t values (9, 9, 9, 9, 9, 9, 9, 9, 9, 9);11insert into t values (10, 10, 10, 10, 10, 10, 10, 10, 10, 10);12insert into t values (11, 11, 11, 11, 11, 11, 11, 11, 11,

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 TableConstraint

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