Best EvoMaster code snippet using org.evomaster.client.java.controller.internal.db.SchemaExtractor.getSchemaName
Source:SchemaExtractor.java
...119 schemaDto.databaseType = dt;120 /*121 schema name122 */123 schemaDto.name = getSchemaName(connection, dt);124 if (dt.equals(DatabaseType.POSTGRES)) {125 Map<String, Set<String>> enumLabels = getPostgresEnumTypes(connection);126 addPostgresEnumTypesToSchema(schemaDto, enumLabels);127 schemaDto.compositeTypes = getPostgresCompositeTypes(connection);128 }129 ResultSet tables = md.getTables(null, schemaDto.name, null, new String[]{"TABLE"});130 Set<String> tableNames = new HashSet<>();131 /*132 Interfaces to deal with DBs are simply awful...133 Here, we first check with schema name in upper case, and, if that gives no results,134 we try with lower case... this is because different databases deal with upper/lower135 cases differently.136 But API does not give you any info on whether result set137 is empty or not, and only way is to call next()138 */139 if (!tables.next()) {140 tables.close();141 schemaDto.name = schemaDto.name.toLowerCase();142 tables = md.getTables(null, schemaDto.name, null, new String[]{"TABLE"});143 if (tables.next()) {144 do {145 handleTableEntry(schemaDto, md, tables, tableNames);146 } while (tables.next());147 }148 } else {149 do {150 handleTableEntry(schemaDto, md, tables, tableNames);151 } while (tables.next());152 }153 tables.close();154 /*155 Mark those columns that are using auto generated values156 */157 addForeignKeyToAutoIncrement(schemaDto);158 /*159 JDBC MetaData is quite limited.160 To check constraints, we need to do SQL queries on the system tables.161 Unfortunately, this is database-dependent162 */163 addConstraints(connection, dt, schemaDto);164 if (dt.equals(DatabaseType.POSTGRES)) {165 List<ColumnAttributes> columnAttributes = getPostgresColumnAttributes(connection);166 addColumnAttributes(schemaDto, columnAttributes);167 }168 assert validate(schemaDto);169 return schemaDto;170 }171 private static void addColumnAttributes(DbSchemaDto schemaDto, List<ColumnAttributes> listOfColumnAttributes) {172 for (ColumnAttributes columnAttributes : listOfColumnAttributes) {173 String tableName = columnAttributes.tableName;174 String columnName = columnAttributes.columnName;175 ColumnDto columnDto = getColumnDto(schemaDto, tableName, columnName);176 columnDto.numberOfDimensions = columnAttributes.numberOfDimensions;177 }178 }179 private static ColumnDto getColumnDto(DbSchemaDto schemaDto, String tableName, String columnName) {180 TableDto tableDto = schemaDto.tables.stream()181 .filter(t -> t.name.equals(tableName.toLowerCase()))182 .findFirst()183 .orElse(null);184 return tableDto.columns.stream()185 .filter(c -> c.name.equals(columnName.toLowerCase()))186 .findFirst()187 .orElse(null);188 }189 private static String getSchemaName(Connection connection, DatabaseType dt) throws SQLException {190 String schemaName;191 if (dt.equals(DatabaseType.MYSQL)) {192 // schema is database name in mysql193 schemaName = connection.getCatalog();194 } else {195 try {196 schemaName = connection.getSchema();197 } catch (Exception | AbstractMethodError e) {198 /*199 In remote sessions, getSchema might fail.200 We do not do much with it anyway (at least for201 now), so not a big deal...202 Furthermore, some drivers might be compiled to Java 6,203 whereas getSchema was introduced in Java 7...
getSchemaName
Using AI Code Generation
1import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType2import org.evomaster.client.java.controller.api.dto.database.schema.DbActionDto3import org.evomaster.client.java.controller.api.dto.database.schema.SchemaDto4import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseCommandDto5import org.evomaster.client.java.controller.api.dto.database.operations.QueryDto6import org.evomaster.client.java.controller.api.dto.database.operations.SqlScriptDto7import org.evomaster.client.java.controller.api.dto.database.schema.TableDto8import org.evomaster.client.java.controller.api.dto.database.schema.ColumnDto9import org.evomaster.client.java.controller.api.dto.database.schema.ForeignKeyDto10import org.evomaster.client.java.controller.api.dto.database.schema.IndexDto11import org.evomaster.client.java.controller.api.dto.database.schema.ColumnDataType12import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseActionResultDto13import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseExecutionDto14import org.evomaster.client.java.controller.api.dto.database.operations.ExecutionStatusDto15import org.evomaster.client.java.controller.api.dto.database.operations.ExecutionTimingDto16import org.evomaster.client.java.controller.api.dto.database.operations.ExecutionTimingTypeDto17import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseExecutionResultDto18import org.evomaster.client.java.controller.internal.db.SqlScriptRunner19import org.evomaster.client.java.controller.internal.db.SqlScriptRunner.Companion.runSql20import org.evomaster.client.java.controller.internal.db.SqlScriptRunner.Companion.runStatement21import org.evomaster.client.java.controller.internal.db.SqlScriptRunner.Companion.runQuery22import org.evomaster.client.java.controller.internal.db.SqlScriptRunner.Companion.runQueryWithParams23import org.evomaster.client.java.controller.internal.db.SqlScriptRunner.Companion.runScript24import org.evomaster.client.java.controller.internal.db.SqlScriptRunner.Companion.runScriptWithParams25import org.evomaster.client.java.controller.internal.db.SqlScriptRunner.Companion.runBatch26import org.evomaster.client.java.controller.internal.db.SqlScriptRunner.Companion.runBatchWithParams27import org.evomaster.client.java.controller.internal.db.SqlScriptRunner.Companion.runBatchWithParamsAndTypes28import org.evomaster.client.java.controller.internal.db.SqlScriptRunner.Companion.runBatchWithParamsAndTypesAndNames29import org.evomaster
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!!