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

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

Source:SchemaExtractor.java Github

copy

Full Screen

...33 //not a foreign key34 return;35 }36 //TODO proper handling of multi-column PKs/FKs37 Optional<TableDto> targetTable = schema.tables.stream()38 .filter(t -> t.name.equals(fk.get().targetTable))39 .findFirst();40 if (!targetTable.isPresent()) {41 throw new IllegalArgumentException("Foreign key in table " + table.name +42 " pointing to non-existent table " + fk.get().targetTable);43 }44 List<ColumnDto> pks = targetTable.get().columns.stream()45 .filter(c -> c.primaryKey)46 .collect(Collectors.toList());47 if (pks.isEmpty()) {48 throw new IllegalArgumentException("No PK in table " + targetTable.get().name + " that has FKs pointing to it");49 }50 for (ColumnDto pk : pks) {51 if (pk.autoIncrement || pk.foreignKeyToAutoIncrement) {52 throw new IllegalArgumentException("Column " + pk.name + " in table " +53 pk.table + " is auto-increment, although FK pointing to it does not mark it " +54 "as autoincrement in " + column.name + " in " + table.name55 );56 }57 }58 }59 private static void checkForeignKeyToAutoIncrementPresent(DbSchemaDto schema, TableDto table, ColumnDto column) {60 if (!column.foreignKeyToAutoIncrement) {61 return;62 }63 Optional<ForeignKeyDto> fk = table.foreignKeys.stream()64 .filter(it -> it.sourceColumns.contains(column.name))65 .findFirst();66 if (!fk.isPresent()) {67 throw new IllegalArgumentException("No foreign key constraint for marked column " +68 column.name + " in table " + table.name);69 }70 //TODO proper handling of multi-column PKs/FKs71 Optional<TableDto> targetTable = schema.tables.stream()72 .filter(t -> t.name.equals(fk.get().targetTable))73 .findFirst();74 if (!targetTable.isPresent()) {75 throw new IllegalArgumentException("Foreign key in table " + table.name +76 " pointing to non-existent table " + fk.get().targetTable);77 }78 //there should be only 1 PK, and that must be auto-increment79 List<ColumnDto> pks = targetTable.get().columns.stream()80 .filter(c -> c.primaryKey)81 .collect(Collectors.toList());82 if (pks.size() != 1) {83 throw new IllegalArgumentException("There must be only 1 PK in table " +84 targetTable.get().name + " pointed by the FK-to-autoincrement " +85 column.name + " in " + table.name + ". However, there were: " + pks.size());86 }87 ColumnDto pk = pks.get(0);88 if (!pk.autoIncrement && !pk.foreignKeyToAutoIncrement) {89 throw new IllegalArgumentException("Column " + pk.name + " in table " +90 pk.table + " is not auto-increment, although FK pointing to it does mark it" +91 "as autoincrement in " + column.name + " in " + table.name92 );93 }94 }95 public static DbSchemaDto extract(Connection connection) throws Exception {96 Objects.requireNonNull(connection);97 DbSchemaDto schemaDto = new DbSchemaDto();98 try {99 schemaDto.name = connection.getSchema();100 } catch (Exception | AbstractMethodError e) {101 /*102 In remote sessions, getSchema might fail.103 We do not do much with it anyway (at least for104 now), so not a big deal...105 Furthermore, some drivers might be compiled to Java 6,106 whereas getSchema was introduced in Java 7107 */108 schemaDto.name = "public";109 }110 DatabaseMetaData md = connection.getMetaData();111 String protocol = md.getURL(); //TODO better handling112 DatabaseType dt = DatabaseType.OTHER;113 if (protocol.contains(":h2")) {114 dt = DatabaseType.H2;115 } else if (protocol.contains(":derby")) {116 dt = DatabaseType.DERBY;117 } else if (protocol.contains(":postgresql")) {118 dt = DatabaseType.POSTGRES;119 }120 schemaDto.databaseType = dt;121 //see https://www.progress.com/blogs/jdbc-tutorial-extracting-database-metadata-via-jdbc-driver122 schemaDto.name = schemaDto.name.toUpperCase();123 ResultSet tables = md.getTables(null, schemaDto.name, null, new String[]{"TABLE"});124 Set<String> tableNames = new HashSet<>();125 /*126 Interfaces to deal with DBs are simply awful...127 Here, we first check with schema name in upper case, and, if that gives no results,128 we try with lower case... this is because different databases deal with upper/lower129 cases differently.130 But API does not give you any info on whether result set131 is empty or not, and only way is to call next()132 */133 if (!tables.next()) {134 tables.close();135 schemaDto.name = schemaDto.name.toLowerCase();136 tables = md.getTables(null, schemaDto.name, null, new String[]{"TABLE"});137 if (tables.next()) {138 do {139 handleTableEntry(schemaDto, md, tables, tableNames);140 } while (tables.next());141 }142 } else {143 do {144 handleTableEntry(schemaDto, md, tables, tableNames);145 } while (tables.next());146 }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))289 .findFirst().orElse(null);290 return tableDto;291 }292 private static ColumnDto getColumn(TableDto table, String columnName) {293 ColumnDto columnDto = table.columns.stream()294 .filter(c -> c.name.equalsIgnoreCase(columnName))295 .findFirst().orElse(null);296 return columnDto;297 }298 /**299 * Checks if the given table/column is a foreign key to an autoincrement column.300 * This is done to be able to compute foreignKeyToAutoIncrement boolean.301 * Otherwise, we could just read that boolean.302 *303 * @return true if the given table/column is a foreign key to an autoincrement column.304 */305 private static boolean isFKToAutoIncrementColumn(DbSchemaDto schema, TableDto tableDto, String columnName) {306 Objects.requireNonNull(schema);307 Objects.requireNonNull(tableDto);308 Objects.requireNonNull(columnName);309 // is this column among the declared FKs?310 if (!tableDto.foreignKeys.stream()311 .anyMatch(fk -> fk.sourceColumns.stream()312 .anyMatch(s -> s.equalsIgnoreCase(columnName)))) {313 return false;314 }315 ColumnDto columnDto = getColumn(tableDto, columnName);316 if (columnDto.autoIncrement == true) {317 // Assuming here that a FK cannot be auto-increment318 return false;319 }320 // check if the column belongs to a foreign key that is non printable321 for (ForeignKeyDto fk : tableDto.foreignKeys) {322 if (fk.sourceColumns.stream()323 .anyMatch(s -> s.equalsIgnoreCase(columnName))) {324 /*325 TODO: instead of using those positions, should have proper326 support for multi-column PKs/FKs327 */328 int positionInFKSequence = fk.sourceColumns.indexOf(columnName);329 TableDto targetTableDto = getTable(schema, fk.targetTable);330 String targetColumnName = targetTableDto.primaryKeySequence.get(positionInFKSequence);331 ColumnDto targetColumnDto = getColumn(targetTableDto, targetColumnName);332 /*333 Either that target PK is auto-increment, or itself is a FK to a non-printable PK334 */335 if (targetColumnDto.autoIncrement ||336 isFKToAutoIncrementColumn(schema, targetTableDto, targetColumnName)) {337 return true;338 }339 }340 }341 return false;342 }343}...

Full Screen

Full Screen

getTable

Using AI Code Generation

copy

Full Screen

1import org.evomaster.client.java.controller.internal.db.Table;2import org.evomaster.client.java.controller.internal.db.TableColumn;3import org.evomaster.client.java.controller.internal.db.TableIndex;4import org.evomaster.client.java.controller.internal.db.TableForeignKey;5import org.evomaster.client.java.controller.internal.db.TableRow;6import org.evomaster.client.java.controller.internal.db.SchemaExtractor;7import org.evomaster.client.java.controller.db.SqlScriptRunner;8import org.evomaster.client.java.controller.db.DbCleaner;9import org.evomaster.client.java.controller.db.SqlScriptRunner;10import org.evomaster.client.java.controller.db.DbCleaner;11import org.evomaster.client.java.controller.problem.ProblemInfo;12import org.evomaster.client.java.controller.problem.RestProblem;13import org.evomaster.client.java.controller.api.dto.SutInfoDto;14import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseCommandDto;15import org.evomaster.client.java.controller.api.dto.database.operations.InsertionDto;16import org.evomaster.client.java.controller.api.dto.database.operations.SqlScriptDto;17import org.evomaster.client.java.controller.api.dto.database.operations.DeleteTableDto;18import org.evomaster.client.java.controller.api.dto.database.operations.InsertionDto;19import org.evomaster.client.java.controller.api.dto.database.operations.SqlScriptDto;20import org.evomaster.client.java.controller.api.dto.database.operations.DeleteTableDto;21import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType;22import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseCommandDto;23import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType;24import org.evomaster.client.java.controller.api.dto.database.schema.SchemaDto;25import org.evomaster.client.java.controller.api.dto.database.schema.TableDto;26import org.evomaster.client.java.controller.api.dto.database.schema.TableColumnDto;27import org.evomaster.client.java.controller.api.dto.database.schema.TableIndexDto;28import org.evomaster.client.java.controller.api.dto.database.schema.TableDto;29import org.evomaster.client.java.controller.api.dto.database.schema.TableColumnDto;30import org.evomaster.client.java.controller.api.dto.database.schema.TableIndexDto;31import org.evomaster.client.java.controller.api.dto.database.schema.TableForeignKeyDto;32import org.evomaster.client.java.controller.api.dto.database.schema.TableRowDto;33import org.evomaster.client.java.controller.api.dto.database.schema.TableForeignKeyDto;34import org.evom

Full Screen

Full Screen

getTable

Using AI Code Generation

copy

Full Screen

1 schemaExtractor = new SchemaExtractor(connection);2 List<Table> tables = schemaExtractor.getTables();3 schemaExtractor = new SchemaExtractor(connection);4 List<Table> tables = schemaExtractor.getTables();5 schemaExtractor = new SchemaExtractor(connection);6 List<Table> tables = schemaExtractor.getTables();7 schemaExtractor = new SchemaExtractor(connection);8 List<Table> tables = schemaExtractor.getTables();9 schemaExtractor = new SchemaExtractor(connection);10 List<Table> tables = schemaExtractor.getTables();11 schemaExtractor = new SchemaExtractor(connection);12 List<Table> tables = schemaExtractor.getTables();13 schemaExtractor = new SchemaExtractor(connection);14 List<Table> tables = schemaExtractor.getTables();15 schemaExtractor = new SchemaExtractor(connection);16 List<Table> tables = schemaExtractor.getTables();17 schemaExtractor = new SchemaExtractor(connection);18 List<Table> tables = schemaExtractor.getTables();19 schemaExtractor = new SchemaExtractor(connection);20 List<Table> tables = schemaExtractor.getTables();21 schemaExtractor = new SchemaExtractor(connection);22 List<Table> tables = schemaExtractor.getTables();23 schemaExtractor = new SchemaExtractor(connection);24 List<Table> tables = schemaExtractor.getTables();

Full Screen

Full Screen

getTable

Using AI Code Generation

copy

Full Screen

1import org.evomaster.client.java.controller.api.Controller2import org.evomaster.client.java.controller.internal.db.SqlScriptRunner3import org.evomaster.client.java.controller.internal.db.Table4import org.evomaster.client.java.controller.internal.db.TableColumn5import org.evomaster.client.java.controller.internal.db.TableRow6import org.evomaster.client.java.controller.internal.db.schema.SchemaExtractor7import org.evomaster.client.java.controller.problem.ProblemInfo8import org.evomaster.client.java.controller.problem.RestProblem9import org.evomaster.client.java.controller.problem.RestCallResult10import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseCommandDto11import org.evomaster.client.java.controller.api.dto.database.operations.InsertionDto12import org.evomaster.client.java.controller.api.dto.database.operations.DeletionDto13import org.evomaster.client.java.controller.api.dto.database.operations.UpdateDto14import org.evomaster.client.java.controller.api.dto.database.operations.SqlScriptDto15import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseActionResultDto16import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseExecutionDto17import org.evomaster.client.java.controller.api.dto.database.operations.SelectionDto18import org.evomaster.client.java.controller.api.dto.database.operations.TableDto19import org.evomaster.client.java.controller.api.dto.database.operations.TableRowDto20import org.evomaster.client.java.controller.api.dto.database.operations.TableColumnDto21import org.evomaster.client.java.controller.api.dto.database.operations.SqlRowDto22import org.evomaster.client.java.controller.api.dto.database.operations.SqlColumnDto23import org.evomaster.client.java.controller.api.dto.database.operations.SqlTableDto24import org.evomaster.client.java.controller.api.dto.database.operations.SqlDataDto25import org.evomaster.client.java.controller.api.dto.database.operations.TableSchemaDto26import org.evomaster.client.java.controller.api.dto.database.operations.SchemaDto27import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseExecutionDto28import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseActionResultDto29import org.evomaster.client.java.controller.api.dto.database.operations.InsertionDto30import org.evomaster.client.java.controller.api.dto.database.operations.DeletionDto31import org.evomaster.client.java.controller.api.dto.database.operations.UpdateDto32import org.evomaster.client.java.controller.api.dto.database.operations.Selection

Full Screen

Full Screen

getTable

Using AI Code Generation

copy

Full Screen

1public class Test_0_0 extends EMTestBase {2 public static void initClass() throws Exception {3 EMConfig config = new EMConfig();4 config.setTestSuiteSplitType(EMConfig.SplitType.WITH_SEED);5 config.setSeed(0);6 config.setTestSuiteSplitNumber(1);7 config.setTestSplitNumber(0);8 initClass(config);9 }10 public void init() throws Exception {11 initTest();12 }13 public void test_0_0() throws Throwable {14 ObjectMapper mapper = new ObjectMapper();15 mapper.writeValue(new File("schema.json"), schema);16 }17}18{19 "tables" : [ {20 "columns" : [ {21 }, {22 }, {23 } ],24 } ]25}26CREATE TABLE person (27 PRIMARY KEY (id)28);29INSERT INTO person VALUES (1, 'John', 30);30INSERT INTO person VALUES (2, 'Mary', 28);31INSERT INTO person VALUES (3, 'Peter', 35);

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