Best EvoMaster code snippet using org.evomaster.client.java.controller.internal.db.constraint.H2ConstraintExtractor.extractColumnConstraintsVersion1OrLower
Source:H2ConstraintExtractor.java
...53 private List<DbTableConstraint> extractColumnConstraints(Connection connectionToH2, DbSchemaDto schemaDto, String h2DatabaseVersion) throws SQLException {54 if (H2VersionUtils.isVersionGreaterOrEqual(h2DatabaseVersion, H2VersionUtils.H2_VERSION_2_0_0)) {55 return new ArrayList<>();56 } else {57 return extractColumnConstraintsVersion1OrLower(connectionToH2, schemaDto, h2DatabaseVersion);58 }59 }60 private List<DbTableConstraint> extractTableConstraints(Connection connectionToH2, DbSchemaDto schemaDto, String h2DatabaseVersion) throws SQLException {61 if (H2VersionUtils.isVersionGreaterOrEqual(h2DatabaseVersion, H2VersionUtils.H2_VERSION_2_0_0)) {62 return extractTableConstraintsVersionTwoOrHigher(connectionToH2, schemaDto);63 } else {64 return extractTableConstraintsVersionOneOrLower(connectionToH2, schemaDto);65 }66 }67 /**68 * For each table in the schema DTO, this method appends69 * the constraints that are originated in the ALTER TABLE commands70 * for those particular tables.71 * <p>72 * Foreign keys are handled separately in the JDBC metadata73 *74 * @param connectionToH2 a connection to a H2 database75 * @param schemaDto DTO with database schema information76 * @throws SQLException if the connection to the H2 database fails77 */78 private List<DbTableConstraint> extractTableConstraintsVersionTwoOrHigher(Connection connectionToH2,79 DbSchemaDto schemaDto) throws SQLException {80 List<DbTableConstraint> tableCheckExpressions = new ArrayList<>();81 String tableSchema = schemaDto.name;82 for (TableDto tableDto : schemaDto.tables) {83 String tableName = tableDto.name;84 try (Statement statement = connectionToH2.createStatement()) {85 final String query = String.format("Select CONSTRAINT_CATALOG,CONSTRAINT_SCHEMA,CONSTRAINT_NAME,CONSTRAINT_TYPE From INFORMATION_SCHEMA.TABLE_CONSTRAINTS\n" +86 " where TABLE_CONSTRAINTS.TABLE_SCHEMA='%s' \n"87 + " and TABLE_CONSTRAINTS.TABLE_NAME='%s' ", tableSchema, tableName);88 try (ResultSet constraints = statement.executeQuery(query)) {89 while (constraints.next()) {90 String constraintCatalog = constraints.getString(CONSTRAINT_CATALOG);91 String constraintSchema = constraints.getString(CONSTRAINT_SCHEMA);92 String constraintName = constraints.getString(CONSTRAINT_NAME);93 String constraintType = constraints.getString(CONSTRAINT_TYPE);94 switch (constraintType) {95 case UNIQUE: {96 DbTableUniqueConstraint constraint = getTableUniqueConstraint(connectionToH2, tableName,97 constraintCatalog, constraintSchema, constraintName);98 tableCheckExpressions.add(constraint);99 break;100 }101 case CHECK: {102 DbTableCheckExpression constraint = getTableCheckExpression(connectionToH2, tableName, constraintCatalog, constraintSchema, constraintName);103 tableCheckExpressions.add(constraint);104 break;105 }106 case PRIMARY_KEY:107 case PRIMARY_KEY_BLANK:108 case REFERENTIAL:109 /*110 * This type of constraint is already handled by111 * JDBC Metadata112 **/113 break;114 default:115 cannotHandle(constraintType);116 }117 }118 }119 }120 }121 return tableCheckExpressions;122 }123 private DbTableUniqueConstraint getTableUniqueConstraint(Connection connectionToH2,124 String tableName,125 String constraintCatalog,126 String constraintSchema,127 String constraintName) throws SQLException {128 try (Statement columnsUsageStatement = connectionToH2.createStatement()) {129 String columnsUsageQuery = String.format("SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE "130 + "WHERE CONSTRAINT_CATALOG='%s' AND CONSTRAINT_SCHEMA='%s' AND CONSTRAINT_NAME='%s' ",131 constraintCatalog, constraintSchema, constraintName);132 try (ResultSet columnsUsageResultSet = columnsUsageStatement.executeQuery(columnsUsageQuery)) {133 List<String> uniqueColumnNames = new ArrayList<>();134 while (columnsUsageResultSet.next()) {135 String columnName = columnsUsageResultSet.getString(COLUMN_NAME);136 uniqueColumnNames.add(columnName);137 }138 return new DbTableUniqueConstraint(tableName, uniqueColumnNames);139 }140 }141 }142 private DbTableCheckExpression getTableCheckExpression(Connection connectionToH2,143 String tableName,144 String constraintCatalog,145 String constraintSchema,146 String constraintName) throws SQLException {147 try (Statement checkClauseStatement = connectionToH2.createStatement()) {148 String checkClauseQuery = String.format("SELECT CHECK_CLAUSE FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS "149 + "WHERE CONSTRAINT_CATALOG='%s' AND CONSTRAINT_SCHEMA='%s' AND CONSTRAINT_NAME='%s' ",150 constraintCatalog, constraintSchema, constraintName);151 try (ResultSet checkClauseResultSet = checkClauseStatement.executeQuery(checkClauseQuery)) {152 if (checkClauseResultSet.next()) {153 String sqlCheckExpression = checkClauseResultSet.getString("CHECK_CLAUSE");154 return new DbTableCheckExpression(tableName, "(" + sqlCheckExpression + ")");155 } else {156 throw new IllegalArgumentException(String.format("Cannot find constraint such that CONSTRAINT_CATALOG='%s' AND CONSTRAINT_SCHEMA='%s' AND CONSTRAINT_NAME='%s' ", constraintCatalog, constraintSchema, constraintName));157 }158 }159 }160 }161 /**162 * Extracts DbTableConstraints for version 0.xxx and 1.xxx163 *164 * @param connectionToH2 the connection to the H2 database165 * @param schemaDto the schema166 * @return the list of constraints in the table167 * @throws SQLException an unexpected exception while executing the queries168 */169 private List<DbTableConstraint> extractTableConstraintsVersionOneOrLower(Connection connectionToH2, DbSchemaDto schemaDto) throws SQLException {170 List<DbTableConstraint> tableCheckExpressions = new ArrayList<>();171 String tableSchema = schemaDto.name;172 for (TableDto tableDto : schemaDto.tables) {173 String tableName = tableDto.name;174 try (Statement statement = connectionToH2.createStatement()) {175 final String query = String.format("Select CONSTRAINT_TYPE, CHECK_EXPRESSION, COLUMN_LIST From INFORMATION_SCHEMA.CONSTRAINTS\n" +176 " where CONSTRAINTS.TABLE_SCHEMA='%s' \n"177 + " and CONSTRAINTS.TABLE_NAME='%s' ", tableSchema, tableName);178 try (ResultSet constraints = statement.executeQuery(query)) {179 while (constraints.next()) {180 String constraintType = constraints.getString(CONSTRAINT_TYPE);181 DbTableConstraint constraint;182 switch (constraintType) {183 case UNIQUE: {184 String columnList = constraints.getString(COLUMN_LIST);185 List<String> uniqueColumnNames = Arrays.stream(columnList.split(",")).map(String::trim).collect(Collectors.toList());186 constraint = new DbTableUniqueConstraint(tableName, uniqueColumnNames);187 tableCheckExpressions.add(constraint);188 break;189 }190 case PRIMARY_KEY:191 case PRIMARY_KEY_BLANK:192 case REFERENTIAL:193 /*194 * This type of constraint is already handled by195 * JDBC Metadata196 **/197 break;198 case CHECK:199 String sqlCheckExpression = constraints.getString(CHECK_EXPRESSION);200 constraint = new DbTableCheckExpression(tableName, sqlCheckExpression);201 tableCheckExpressions.add(constraint);202 break;203 default:204 cannotHandle(constraintType);205 }206 }207 }208 }209 }210 return tableCheckExpressions;211 }212 /**213 * For each table in the schema DTO, this method appends214 * the constraints that are originated in the CREATE TABLE commands215 * for those particular tables.216 * <p>217 * Unique constraints and Foreign keys are handled separately in the JDBC metadata218 *219 * @param connectionToH2 a connection to a H2 database220 * @param schemaDto DTO with database schema information221 * @throws SQLException if the connection to the database fails222 */223 private List<DbTableConstraint> extractColumnConstraintsVersion1OrLower(Connection connectionToH2, DbSchemaDto schemaDto, String h2DatabaseVersion) throws SQLException {224 if (H2VersionUtils.isVersionGreaterOrEqual(h2DatabaseVersion, H2VersionUtils.H2_VERSION_2_0_0)) {225 throw new IllegalArgumentException("Cannot extract column constraints for H2 version 2 or higher with H2 database version " + h2DatabaseVersion);226 }227 String tableSchema = schemaDto.name;228 List<DbTableConstraint> columnConstraints = new ArrayList<>();229 for (TableDto tableDto : schemaDto.tables) {230 String tableName = tableDto.name;231 try (Statement statement = connectionToH2.createStatement()) {232 final String query = String.format("Select * From INFORMATION_SCHEMA.COLUMNS where COLUMNS.TABLE_SCHEMA='%s' and COLUMNS.TABLE_NAME='%s' ", tableSchema, tableName);233 try (ResultSet columns = statement.executeQuery(query)) {234 while (columns.next()) {235 String sqlCheckExpression = columns.getString(CHECK_CONSTRAINT);236 if (sqlCheckExpression != null && !sqlCheckExpression.equals("")) {237 DbTableCheckExpression constraint = new DbTableCheckExpression(tableName, sqlCheckExpression);...
extractColumnConstraintsVersion1OrLower
Using AI Code Generation
1 public List<ColumnConstraintDto> extractColumnConstraintsVersion1OrLower(String tableName) throws SQLException {2 final String sql = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ?";3 try (PreparedStatement ps = connection.prepareStatement(sql)) {4 ps.setString(1, tableName);5 try (ResultSet rs = ps.executeQuery()) {6 List<ColumnConstraintDto> constraints = new ArrayList<>();7 while (rs.next()) {8 String columnName = rs.getString("COLUMN_NAME");9 String dataType = rs.getString("DATA_TYPE");10 Integer columnSize = rs.getInt("COLUMN_SIZE");11 boolean isNullable = rs.getBoolean("IS_NULLABLE");12 boolean isAutoIncrement = rs.getBoolean("IS_AUTOINCREMENT");13 String defaultValue = rs.getString("COLUMN_DEFAULT");14 String checkConstraint = rs.getString("CHECK_CONSTRAINT");15 ColumnConstraintDto dto = new ColumnConstraintDto(columnName, dataType, columnSize, isNullable, isAutoIncrement, defaultValue, checkConstraint);16 constraints.add(dto);17 }18 return constraints;19 }20 }21 }22 public List<ColumnConstraintDto> extractColumnConstraintsVersion2OrHigher(String tableName) throws SQLException {23 final String sql = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ?";24 try (PreparedStatement ps = connection.prepareStatement(sql)) {25 ps.setString(1, tableName);26 try (ResultSet rs = ps.executeQuery()) {27 List<ColumnConstraintDto> constraints = new ArrayList<>();28 while (rs.next()) {29 String columnName = rs.getString("COLUMN_NAME");30 String dataType = rs.getString("DATA_TYPE");31 Integer columnSize = rs.getInt("CHARACTER_MAXIMUM_LENGTH");32 boolean isNullable = rs.getBoolean("IS_NULLABLE");33 boolean isAutoIncrement = rs.getBoolean("IS_AUTOINCREMENT");34 String defaultValue = rs.getString("COLUMN_DEFAULT");35 String checkConstraint = rs.getString("CHECK_CONSTRAINT");36 ColumnConstraintDto dto = new ColumnConstraintDto(columnName, dataType, columnSize, isNullable, isAutoIncrement, defaultValue, checkConstraint);37 constraints.add(dto);38 }39 return constraints;40 }41 }42 }43 public List<ColumnConstraintDto> extractColumnConstraints(String tableName) throws SQLException {
extractColumnConstraintsVersion1OrLower
Using AI Code Generation
1public class ConstraintExtractorTest {2 public void testExtractColumnConstraintsVersion1OrLower() {3 String tableName = "TEST_TABLE";4 String columnName = "TEST_COLUMN";5 String columnType = "INTEGER";6 String columnDefault = "0";7 boolean isNullable = false;8 boolean isAutoIncrement = true;9 boolean isPrimaryKey = false;10 boolean isUnique = false;11 boolean isIndex = true;12 boolean isUniqueIndex = false;13 boolean isForeignKey = false;14 boolean isForeignKeyUnique = false;15 boolean isForeignKeyIndex = false;16 boolean isForeignKeyUniqueIndex = false;17 String foreignKeyReferenceTable = null;18 String foreignKeyReferenceColumn = null;19 boolean foreignKeyReferenceColumnIsPrimaryKey = false;20 boolean foreignKeyReferenceColumnIsUnique = false;21 boolean foreignKeyReferenceColumnIsIndex = false;22 boolean foreignKeyReferenceColumnIsUniqueIndex = false;23 boolean foreignKeyReferenceColumnIsAutoIncrement = false;24 boolean foreignKeyReferenceColumnIsNullable = false;25 boolean foreignKeyReferenceColumnIsForeignKey = false;26 boolean foreignKeyReferenceColumnIsForeignKeyUnique = false;
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!!