How to use addForeignKeyToAutoIncrement method of org.evomaster.client.java.controller.internal.db.SchemaExtractor class

Best EvoMaster code snippet using org.evomaster.client.java.controller.internal.db.SchemaExtractor.addForeignKeyToAutoIncrement

Source:SchemaExtractor.java Github

copy

Full Screen

...147 tables.close();148 /*149 Mark those columns that are using auto generated values150 */151 addForeignKeyToAutoIncrement(schemaDto);152 /*153 JDBC MetaData is quite limited.154 To check constraints, we need to do SQL queries on the system tables.155 Unfortunately, this is database-dependent156 */157 addConstraints(connection, dt, schemaDto);158 assert validate(schemaDto);159 return schemaDto;160 }161 /**162 * Adds a unique constraint to the corresponding ColumnDTO for the selected table.column pair.163 * Requires the ColumnDTO to be contained in the TableDTO.164 * If the column DTO is not contained, a IllegalArgumentException is thrown.165 *166 * @param tableDto the DTO of the table167 * @param columnName the name of the column to add the unique constraint on168 **/169 public static void addUniqueConstraintToColumn(TableDto tableDto, String columnName) {170 ColumnDto columnDto = tableDto.columns.stream()171 .filter(c -> c.name.equals(columnName)).findAny().orElse(null);172 if (columnDto == null) {173 throw new IllegalArgumentException("Missing column DTO for column:" + tableDto.name + "." + columnName);174 }175 columnDto.unique = true;176 }177 /**178 * Appends constraints that are database specific.179 */180 private static void addConstraints(Connection connection, DatabaseType dt, DbSchemaDto schemaDto) throws SQLException {181 TableConstraintExtractor constraintExtractor = TableConstraintExtractorFactory.buildConstraintExtractor(dt);182 if (constraintExtractor != null) {183 final List<DbTableConstraint> dbTableConstraints = constraintExtractor.extract(connection, schemaDto);184 addConstraints(schemaDto, dbTableConstraints);185 } else {186 SimpleLogger.uniqueWarn("WARNING: EvoMaster cannot extract constraints from database " + dt);187 }188 }189 private static void addConstraints(DbSchemaDto schemaDto, List<DbTableConstraint> constraintList) {190 for (DbTableConstraint constraint : constraintList) {191 String tableName = constraint.getTableName();192 TableDto tableDto = schemaDto.tables.stream().filter(t -> t.name.equalsIgnoreCase(tableName)).findFirst().orElse(null);193 if (constraint instanceof DbTableCheckExpression) {194 TableCheckExpressionDto constraintDto = new TableCheckExpressionDto();195 final DbTableCheckExpression tableCheckExpression = (DbTableCheckExpression) constraint;196 constraintDto.sqlCheckExpression = tableCheckExpression.getSqlCheckExpression();197 tableDto.tableCheckExpressions.add(constraintDto);198 } else if (constraint instanceof DbTableUniqueConstraint) {199 DbTableUniqueConstraint tableUniqueConstraint = (DbTableUniqueConstraint) constraint;200 for (String columnName : tableUniqueConstraint.getUniqueColumnNames()) {201 addUniqueConstraintToColumn(tableDto, columnName);202 }203 } else {204 throw new RuntimeException("Unknown constraint type " + constraint.getClass().getName());205 }206 }207 }208 private static void handleTableEntry(DbSchemaDto schemaDto, DatabaseMetaData md, ResultSet tables, Set<String> tableNames) throws SQLException {209 TableDto tableDto = new TableDto();210 schemaDto.tables.add(tableDto);211 tableDto.name = tables.getString("TABLE_NAME");212 if (tableNames.contains(tableDto.name)) {213 /*214 * Perhaps we should throw a more specific exception than IllegalArgumentException215 */216 throw new IllegalArgumentException("Cannot handle repeated table " + tableDto.name + " in schema");217 } else {218 tableNames.add(tableDto.name);219 }220 Set<String> pks = new HashSet<>();221 SortedMap<Integer, String> primaryKeySequence = new TreeMap<>();222 ResultSet rsPK = md.getPrimaryKeys(null, null, tableDto.name);223 while (rsPK.next()) {224 String pkColumnName = rsPK.getString("COLUMN_NAME");225 int positionInPrimaryKey = (int) rsPK.getShort("KEY_SEQ");226 pks.add(pkColumnName);227 int pkIndex = positionInPrimaryKey - 1;228 primaryKeySequence.put(pkIndex, pkColumnName);229 }230 rsPK.close();231 tableDto.primaryKeySequence.addAll(primaryKeySequence.values());232 ResultSet columns = md.getColumns(null, schemaDto.name, tableDto.name, null);233 Set<String> columnNames = new HashSet<>();234 while (columns.next()) {235 ColumnDto columnDto = new ColumnDto();236 tableDto.columns.add(columnDto);237 columnDto.table = tableDto.name;238 columnDto.name = columns.getString("COLUMN_NAME");239 if (columnNames.contains(columnDto.name)) {240 /**241 * Perhaps we should throw a more specific exception than IllegalArgumentException242 */243 throw new IllegalArgumentException("Cannot handle repeated column " + columnDto.name + " in table " + tableDto.name);244 } else {245 columnNames.add(columnDto.name);246 }247 columnDto.type = columns.getString("TYPE_NAME");248 columnDto.size = columns.getInt("COLUMN_SIZE");249 columnDto.nullable = columns.getBoolean("IS_NULLABLE");250 columnDto.autoIncrement = columns.getBoolean("IS_AUTOINCREMENT");251 //columns.getString("DECIMAL_DIGITS");252 columnDto.primaryKey = pks.contains(columnDto.name);253 }254 columns.close();255 ResultSet fks = md.getImportedKeys(null, null, tableDto.name);256 while (fks.next()) {257 //TODO need to see how to handle case of multi-columns258 ForeignKeyDto fkDto = new ForeignKeyDto();259 fkDto.sourceColumns.add(fks.getString("FKCOLUMN_NAME"));260 fkDto.targetTable = fks.getString("PKTABLE_NAME");261 tableDto.foreignKeys.add(fkDto);262 }263 fks.close();264 }265 /**266 * Sets the foreignKeyToAutoIncrement field in the Column DTO267 * when a column is a foreign key to an auto increment value.268 * This information will be needed to properly handle the269 * automatically generated values in primary keys that are270 * referenced by columns in other tables271 *272 * @param schema a DTO with the database information273 */274 private static void addForeignKeyToAutoIncrement(DbSchemaDto schema) {275 for (TableDto tableDto : schema.tables) {276 for (ColumnDto columnDto : tableDto.columns) {277 if (isFKToAutoIncrementColumn(schema, tableDto, columnDto.name)) {278 columnDto.foreignKeyToAutoIncrement = true;279 }280 }281 }282 }283 /**284 * @return a table DTO for a particular table name285 */286 private static TableDto getTable(DbSchemaDto schema, String tableName) {287 TableDto tableDto = schema.tables.stream()288 .filter(t -> t.name.equalsIgnoreCase(tableName))...

Full Screen

Full Screen

addForeignKeyToAutoIncrement

Using AI Code Generation

copy

Full Screen

1import org.evomaster.client.java.controller.api.dto.database.schema.ForeignKeyConstraintDto;2import org.evomaster.client.java.controller.api.dto.database.schema.TableSchemaDto;3import org.evomaster.client.java.controller.internal.db.SchemaExtractor;4import java.sql.Connection;5import java.sql.DriverManager;6import java.sql.SQLException;7import java.util.List;8public class ExtractForeignKeyConstraints {9 public static void main(String[] args) throws SQLException {10 String username = "USERNAME";11 String password = "PASSWORD";12 try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password)) {13 SchemaExtractor extractor = new SchemaExtractor(connection);14 List<TableSchemaDto> schemas = extractor.extractTables();15 System.out.println("Total tables: " + schemas.size());16 for (TableSchemaDto table : schemas) {17 System.out.println("Table: " + table.getName());18 for (ForeignKeyConstraintDto fk : table.getForeignKeys()) {19 System.out.println(" FK: " + fk.getFromColumn() + " -> " + fk.getToTable() + "." + fk.getToColumn());20 }21 }22 }23 }24}

Full Screen

Full Screen

addForeignKeyToAutoIncrement

Using AI Code Generation

copy

Full Screen

1package org.evomaster.client.java.controller.internal.db;2import java.util.List;3public class SchemaExtractor {4 public static void main(String[] args) {5 List<Table> tables = extractor.extractSchema();6 for (Table table : tables) {7 System.out.println(table.getName());8 for (ForeignKey fk : table.getForeignKeys()) {9 System.out.println(" " + fk.getColumnName() + " -> " + fk.getReferencedTableName() + "(" + fk.getReferencedColumnName() + ")");10 }11 }12 }13 private final String url;14 private final String username;15 private final String password;16 public SchemaExtractor(String url, String username, String password) {17 this.url = url;18 this.username = username;19 this.password = password;20 }21 public List<Table> extractSchema() {22 SchemaExtractorJDBC extractor = new SchemaExtractorJDBC(url, username, password);23 List<Table> tables = extractor.extractSchema();24 for (Table table : tables) {25 addForeignKeyToAutoIncrement(table);26 }27 return tables;28 }29 private void addForeignKeyToAutoIncrement(Table table) {30 for (Column column : table.getColumns()) {31 if (column.isAutoIncrement()) {32 String name = column.getName();33 if (name.endsWith("_id")) {34 String fkName = name.substring(0, name.length() - 3);35 table.addForeignKey(new ForeignKey(column.getName(), fkName, "id"));36 }37 }38 }39 }40}41package org.evomaster.client.java.controller.internal.db;42import java.sql.*;43import java.util.*;44public class SchemaExtractorJDBC {45 private final String url;46 private final String username;47 private final String password;48 public SchemaExtractorJDBC(String url, String username, String password) {49 this.url = url;50 this.username = username;51 this.password = password;52 }

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful