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

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

Source:SchemaExtractor.java Github

copy

Full Screen

...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 7204 */205 schemaName = "public";206 }207 //see https://www.progress.com/blogs/jdbc-tutorial-extracting-database-metadata-via-jdbc-driver208 schemaName = schemaName.toUpperCase();209 }210 return schemaName;211 }212 private static class ColumnAttributes {213 public String tableName;214 public String columnName;215 public int numberOfDimensions;216 }217 private static List<ColumnAttributes> getPostgresColumnAttributes(Connection connection) throws SQLException {218 String query = "SELECT pg_namespace.nspname as TABLE_NAMESPACE, pg_class.relname as TABLE_NAME, pg_attribute.attname as COLUMN_NAME, pg_attribute.attndims as NUMBER_OF_DIMENSIONS \n" +219 "FROM pg_attribute \n" +220 "INNER JOIN pg_class ON pg_class.oid = pg_attribute.attrelid " +221 "INNER JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace " +222 "WHERE pg_namespace.nspname != 'pg_catalog' ";223 List<ColumnAttributes> listOfColumnAttributes = new LinkedList<>();224 try (Statement stmt = connection.createStatement()) {225 ResultSet columnAttributesResultSet = stmt.executeQuery(query);226 while (columnAttributesResultSet.next()) {227 String tableNamesapce = columnAttributesResultSet.getString("TABLE_NAMESPACE");228 String tableName = columnAttributesResultSet.getString("TABLE_NAME");229 String columnName = columnAttributesResultSet.getString("COLUMN_NAME");230 int numberOfDimensions = columnAttributesResultSet.getInt("NUMBER_OF_DIMENSIONS");231 if (numberOfDimensions == 0) {232 // skip attribute rows when data types are not arrays, matrixes, etc.233 continue;234 }235 ColumnAttributes columnAttributes = new ColumnAttributes();236 columnAttributes.tableName = tableName;237 columnAttributes.columnName = columnName;238 columnAttributes.numberOfDimensions = numberOfDimensions;239 listOfColumnAttributes.add(columnAttributes);240 }241 }242 return listOfColumnAttributes;243 }244 private static List<String> getAllCompositeTypeNames(Connection connection) throws SQLException {245 // Source: https://stackoverflow.com/questions/3660787/how-to-list-custom-types-using-postgres-information-schema246 String listAllCompositeTypesQuery = "SELECT n.nspname as schema, t.typname as typename \n" +247 "FROM pg_type t \n" +248 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace \n" +249 "WHERE (t.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) \n" +250 "AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)\n" +251 "AND n.nspname NOT IN ('pg_catalog', 'information_schema')" +252 "AND t.typtype ='c';";253 List<String> compositeTypeNames = new ArrayList<>();254 try (Statement listAllCompositeTypesStmt = connection.createStatement()) {255 ResultSet listAllCompositeTypesResultSet = listAllCompositeTypesStmt.executeQuery(listAllCompositeTypesQuery);256 while (listAllCompositeTypesResultSet.next()) {257 compositeTypeNames.add(listAllCompositeTypesResultSet.getString("typename"));258 }259 }260 return compositeTypeNames;261 }262 private static List<CompositeTypeDto> getPostgresCompositeTypes(Connection connection) throws SQLException {263 List<CompositeTypeDto> compositeTypeDtos = new ArrayList<>();264 List<String> compositeTypeNames = getAllCompositeTypeNames(connection);265 for (String compositeTypeName : compositeTypeNames) {266 List<CompositeTypeColumnDto> columnDtos = getAllCompositeTypeColumns(connection, compositeTypeName, compositeTypeNames);267 CompositeTypeDto compositeTypeDto = new CompositeTypeDto();268 compositeTypeDto.name = compositeTypeName;269 compositeTypeDto.columns = columnDtos;270 compositeTypeDtos.add(compositeTypeDto);271 }272 return compositeTypeDtos;273 }274 private static final String TEXT_DATA_TYPE = "text";275 private static final String BIT_DATA_TYPE = "bit";276 private static final String VAR_BIT_DATA_TYPE = "varbit";...

Full Screen

Full Screen

getPostgresCompositeTypes

Using AI Code Generation

copy

Full Screen

1 List<CompositeType> compositeTypes = schemaExtractor.getPostgresCompositeTypes();2 List<CompositeType> compositeTypes = schemaExtractor.getPostgresCompositeTypes();3 List<CompositeType> compositeTypes = schemaExtractor.getPostgresCompositeTypes();4 List<CompositeType> compositeTypes = schemaExtractor.getPostgresCompositeTypes();5 List<CompositeType> compositeTypes = schemaExtractor.getPostgresCompositeTypes();6 List<CompositeType> compositeTypes = schemaExtractor.getPostgresCompositeTypes();7 List<CompositeType> compositeTypes = schemaExtractor.getPostgresCompositeTypes();8 List<CompositeType> compositeTypes = schemaExtractor.getPostgresCompositeTypes();9 List<CompositeType> compositeTypes = schemaExtractor.getPostgresCompositeTypes();

Full Screen

Full Screen

getPostgresCompositeTypes

Using AI Code Generation

copy

Full Screen

1import org.evomaster.client.java.controller.internal.db.PostgresCompositeType2import org.evomaster.client.java.controller.internal.db.PostgresCompositeTypeColumn3import org.evomaster.client.java.controller.internal.db.SchemaExtractor4import org.evomaster.client.java.controller.internal.db.SqlScriptRunner5import java.sql.Connection6import java.sql.DriverManager7import java.sql.SQLException8import java.util.logging.Level9import java.util.logging.Logger10def getPostgresCompositeTypes() {11 def postgresCompositeTypeNames = SchemaExtractor.getPostgresCompositeTypeNames()12 postgresCompositeTypeNames.each { postgresCompositeTypeName ->13 postgresCompositeTypes.add(SchemaExtractor.getPostgresCompositeTypeColumns(postgresCompositeTypeName))14 }15}16def getPostgresCompositeTypeColumns(postgresCompositeType) {17 postgresCompositeType.getColumns().each { postgresCompositeTypeColumn ->18 postgresCompositeTypeColumns.add(postgresCompositeTypeColumn.getName())19 }20}21def getPostgresCompositeTypeNames() {22 SchemaExtractor.getPostgresCompositeTypeNames().each { postgresCompositeTypeName ->23 postgresCompositeTypeNames.add(postgresCompositeTypeName)24 }25}26def getPostgresCompositeTypesMap() {27 def postgresCompositeTypes = getPostgresCompositeTypes()28 postgresCompositeTypes.each { postgresCompositeType ->29 postgresCompositeTypesMap.put(postgresCompositeType.getName(), getPostgresCompositeTypeColumns(postgresCompositeType))30 }31}32try {33 Class.forName("org.postgresql.Driver")34} catch (ClassNotFoundException ex) {35 Logger.getLogger(SchemaExtractor.class.getName()).log(Level.SEVERE, null, ex)36}

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