Best EvoMaster code snippet using org.evomaster.dbconstraint.ast.SqlColumn
Source:SqlConditionTranslator.java
...5import org.evomaster.dbconstraint.ast.SqlBigDecimalLiteralValue;6import org.evomaster.dbconstraint.ast.SqlBigIntegerLiteralValue;7import org.evomaster.dbconstraint.ast.SqlBinaryDataLiteralValue;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) {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}...
SqlColumn
Using AI Code Generation
1SqlColumn column = new SqlColumn(table, "id");2SqlColumn column = new SqlColumn(table, "id");3SqlColumn column = new SqlColumn(table, "id");4SqlColumn column = new SqlColumn(table, "id");5SqlColumn column = new SqlColumn(table, "id");6SqlColumn column = new SqlColumn(table, "id");7SqlColumn column = new SqlColumn(table, "id");8SqlColumn column = new SqlColumn(table, "id");9SqlColumn column = new SqlColumn(table, "id");10SqlColumn column = new SqlColumn(table, "id");11SqlColumn column = new SqlColumn(table, "id");12SqlColumn column = new SqlColumn(table, "id");13SqlColumn column = new SqlColumn(table, "id");
SqlColumn
Using AI Code Generation
1public class SqlColumn {2 private String columnName;3 private SqlConstraint constraint;4 private String tableName;5 private String schemaName;6 private String catalogName;7 private String tableAlias;8 private String columnAlias;9 private boolean isPrimaryKey;10 private boolean isForeignKey;11 private boolean isUnique;12 private boolean isNotNull;13 private boolean isAutoIncrement;14 private boolean isGenerated;15 private boolean isCaseSensitive;16 private int dataType;17 private String typeName;18 private int precision;19 private int scale;20 private String columnClassName;21 private boolean isReadOnly;22 private boolean isWritable;23 private boolean isSearchable;24 private boolean isCurrency;25 private boolean isSigned;26 private int nullable;27 private boolean isDefinitelyWritable;28 private boolean isNullable;29 private boolean isAliased;30 private boolean isWritableOnInsert;31 private boolean isWritableOnUpdate;32 private boolean isHidden;33 private boolean isKey;34 public SqlColumn(String columnName, SqlConstraint constraint) {35 this.columnName = columnName;36 this.constraint = constraint;37 }38 public String getColumnName() {39 return columnName;40 }41 public void setColumnName(String columnName) {42 this.columnName = columnName;43 }44 public SqlConstraint getConstraint() {45 return constraint;46 }47 public void setConstraint(SqlConstraint constraint) {48 this.constraint = constraint;49 }50 public String getTableName() {51 return tableName;52 }53 public void setTableName(String tableName) {54 this.tableName = tableName;55 }56 public String getSchemaName() {57 return schemaName;58 }59 public void setSchemaName(String schemaName) {60 this.schemaName = schemaName;61 }62 public String getCatalogName() {63 return catalogName;64 }65 public void setCatalogName(String catalogName) {66 this.catalogName = catalogName;67 }68 public String getTableAlias() {69 return tableAlias;70 }71 public void setTableAlias(String tableAlias) {72 this.tableAlias = tableAlias;73 }74 public String getColumnAlias() {75 return columnAlias;76 }77 public void setColumnAlias(String columnAlias) {78 this.columnAlias = columnAlias;79 }80 public boolean isPrimaryKey() {81 return isPrimaryKey;82 }83 public void setPrimaryKey(boolean primaryKey) {
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!