How to use SqlConditionList class of org.evomaster.dbconstraint.ast package

Best EvoMaster code snippet using org.evomaster.dbconstraint.ast.SqlConditionList

Source:SqlConditionTranslator.java Github

copy

Full Screen

...8import org.evomaster.dbconstraint.ast.SqlBooleanLiteralValue;9import org.evomaster.dbconstraint.ast.SqlColumn;10import org.evomaster.dbconstraint.ast.SqlComparisonCondition;11import org.evomaster.dbconstraint.ast.SqlCondition;12import org.evomaster.dbconstraint.ast.SqlConditionList;13import org.evomaster.dbconstraint.ast.SqlConditionVisitor;14import org.evomaster.dbconstraint.ast.SqlInCondition;15import org.evomaster.dbconstraint.ast.SqlIsNotNullCondition;16import org.evomaster.dbconstraint.ast.SqlIsNullCondition;17import org.evomaster.dbconstraint.ast.SqlLikeCondition;18import org.evomaster.dbconstraint.ast.SqlLiteralValue;19import org.evomaster.dbconstraint.ast.SqlNullLiteralValue;20import org.evomaster.dbconstraint.ast.SqlOrCondition;21import org.evomaster.dbconstraint.ast.SqlSimilarToCondition;22import org.evomaster.dbconstraint.ast.SqlStringLiteralValue;23import java.util.ArrayList;24import java.util.Collections;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) {...

Full Screen

Full Screen

SqlConditionList

Using AI Code Generation

copy

Full Screen

1SqlConditionList sqlConditionList = new SqlConditionList();2sqlConditionList.add(new SqlCondition("id", SqlCondition.Operator.EQ, 1));3sqlConditionList.add(new SqlCondition("name", SqlCondition.Operator.EQ, "foo"));4sqlConditionList.add(new SqlCondition("age", SqlCondition.Operator.GT, 18));5sqlConditionList.add(new SqlCondition("age", SqlCondition.Operator.LT, 65));6String sql = sqlConditionList.toSql();7SqlConditionList sqlConditionList = new SqlConditionList();8sqlConditionList.add(new SqlCondition("id", SqlCondition.Operator.EQ, 1));9sqlConditionList.add(new SqlCondition("name", SqlCondition.Operator.EQ, "foo"));10sqlConditionList.add(new SqlCondition("age", SqlCondition.Operator.GT, 18));11sqlConditionList.add(new SqlCondition("age", SqlCondition.Operator.LT, 65));12String sql = sqlConditionList.toSql();13SqlConditionList sqlConditionList = new SqlConditionList();14sqlConditionList.add(new SqlCondition("id", SqlCondition.Operator.EQ, 1));15sqlConditionList.add(new SqlCondition("name", SqlCondition.Operator.EQ, "foo"));16sqlConditionList.add(new SqlCondition("age", SqlCondition.Operator.GT, 18));17sqlConditionList.add(new SqlCondition("age", SqlCondition.Operator.LT, 65));18String sql = sqlConditionList.toSql();19SqlConditionList sqlConditionList = new SqlConditionList();20sqlConditionList.add(new SqlCondition("id", SqlCondition.Operator.EQ, 1));21sqlConditionList.add(new SqlCondition("name", SqlCondition

Full Screen

Full Screen

SqlConditionList

Using AI Code Generation

copy

Full Screen

1SqlConditionList conditionList = new SqlConditionList();2conditionList.add(new SqlCondition("id", SqlCondition.Op.GT, 0));3conditionList.add(new SqlCondition("name", SqlCondition.Op.EQ, "foo"));4conditionList.add(new SqlCondition("salary", SqlCondition.Op.GT, 1000));5conditionList.add(new SqlCondition("salary", SqlCondition.Op.LT, 10000));6conditionList.add(new SqlCondition("age", SqlCondition.Op.GT, 20));7conditionList.add(new SqlCondition("age", SqlCondition.Op.LT, 50));8String sql = conditionList.toSql();9The toSql() method of the SqlConditionList class has two overloaded versions. One version takes a SqlConditionList object as input, and the other version takes a SqlConditionList object and a boolean value as input. The boolean value indicates whether the SQL string should be generated in a single line or multi-line. The default value of the boolean value is false, which means the SQL string will be generated in multi-line. The toSql() method of the SqlConditionList class is shown in the following code:10public String toSql(SqlConditionList conditionList, boolean singleLine) {11 StringBuilder sql = new StringBuilder();12 for (int i = 0; i < conditionList.size(); i++) {13 SqlCondition condition = conditionList.get(i);14 sql.append(condition.toSql());15 if (i < conditionList.size() - 1) {16 sql.append(singleLine ? " AND " : "17AND ");18 }19 }20 return sql.toString();21}22The toSql() method of the SqlConditionList class first creates a StringBuilder object to store the generated SQL string. Then the toSql() method loops over all the conditions in the condition list and calls the toSql() method of the SqlCondition

Full Screen

Full Screen

SqlConditionList

Using AI Code Generation

copy

Full Screen

1public class SqlConditionListTest {2 public void testSqlConditionList() {3 SqlConditionList sqlConditionList = new SqlConditionList();4 sqlConditionList.addCondition(new SqlCondition("column1", "=", "value1"));5 sqlConditionList.addCondition(new SqlCondition("column2", "<", "value2"));6 sqlConditionList.addCondition(new SqlCondition("column3", ">", "value3"));7 sqlConditionList.addCondition(new SqlCondition("column4", "<=", "value4"));8 sqlConditionList.addCondition(new SqlCondition("column5", ">=", "value5"));9 sqlConditionList.addCondition(new SqlCondition("column6", "<>", "value6"));10 sqlConditionList.addCondition(new SqlCondition("column7", "!=", "value7"));11 sqlConditionList.addCondition(new SqlCondition("column8", "like", "value8"));12 sqlConditionList.addCondition(new SqlCondition("column9", "in", "value9"));13 sqlConditionList.addCondition(new SqlCondition("column10", "is null", "value10"));14 sqlConditionList.addCondition(new SqlCondition("column11", "is not null", "value11"));15 sqlConditionList.addCondition(new SqlCondition("column12", "between", "value12"));16 sqlConditionList.addCondition(new SqlCondition("column13", "is true", "value13"));17 sqlConditionList.addCondition(new SqlCondition("column14", "is not true", "value14"));18 sqlConditionList.addCondition(new SqlCondition("column15", "is false", "value15"));19 sqlConditionList.addCondition(new SqlCondition("column16", "is not false", "value16"));20 sqlConditionList.addCondition(new SqlCondition("column17", "is unknown", "value17"));21 sqlConditionList.addCondition(new SqlCondition("column18", "is not unknown", "value18"));22 sqlConditionList.addCondition(new SqlCondition("column19", "is distinct from", "value19"));23 sqlConditionList.addCondition(new SqlCondition("column20", "is not distinct from", "value20"));24 sqlConditionList.addCondition(new SqlCondition("column21", "is null", null));

Full Screen

Full Screen

SqlConditionList

Using AI Code Generation

copy

Full Screen

1SqlConditionList conditions = new SqlConditionList();2SqlCondition idCondition = new SqlCondition("users", "id", ComparisonOperator.GREATER_THAN, "0");3conditions.add(idCondition);4SqlCondition nameCondition = new SqlCondition("users", "name", ComparisonOperator.LIKE, "'%john%'");5conditions.add(nameCondition);6SqlCondition ageCondition = new SqlCondition("users", "age", ComparisonOperator.GREATER_THAN, "18");7conditions.add(ageCondition);8SqlCondition ageCondition2 = new SqlCondition("users", "age", ComparisonOperator.LESS_THAN, "100");9conditions.add(ageCondition2);10System.out.println(conditions);11SqlCondition idCondition2 = new SqlCondition("users", "id", ComparisonOperator.EQUALS, "1");12conditions.add(idCondition2);13System.out.println(conditions);14SqlCondition idCondition3 = new SqlCondition("users", "id", ComparisonOperator.EQUALS, "2");15conditions.add(idCondition3);16System.out.println(conditions);17SqlCondition idCondition4 = new SqlCondition("users", "id", ComparisonOperator.EQUALS, "3");18conditions.add(idCondition4);19System.out.println(conditions);20SqlCondition idCondition5 = new SqlCondition("users", "id", ComparisonOperator.EQUALS, "4");21conditions.add(idCondition5

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.

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