Skip to content

Commit

Permalink
refactoring and cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
arcuri82 committed Dec 16, 2024
1 parent fe202e6 commit 82e2f87
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,11 @@ private void computeSQLHeuristics(ExtraHeuristicsDto dto, List<AdditionalInfo> a
if (!additionalInfoList.isEmpty()) {
AdditionalInfo last = additionalInfoList.get(additionalInfoList.size() - 1);
last.getSqlInfoData().stream().forEach(it -> {
// String sql = it.getCommand();
try {
final SqlExecutionLogDto sqlExecutionLogDto = new SqlExecutionLogDto(it.getSqlCommand(), it.hasThrownSqlException(), it.getExecutionTime());
SqlExecutionLogDto sqlExecutionLogDto = new SqlExecutionLogDto(
it.getSqlCommand(),
it.hasThrownSqlException(),
it.getExecutionTime());
sqlHandler.handle(sqlExecutionLogDto);
} catch (Exception e) {
SimpleLogger.error("FAILED TO HANDLE SQL COMMAND: " + it.getSqlCommand());
Expand Down Expand Up @@ -406,7 +408,6 @@ private void computeSQLHeuristics(ExtraHeuristicsDto dto, List<AdditionalInfo> a
accessedTables.addAll(sqlExecutionsDto.deletedData);
accessedTables.addAll(sqlExecutionsDto.insertedData.keySet());
//accessedTables.addAll(executionDto.queriedData.keySet());
// accessedTables.addAll(sqlExecutionsDto.insertedData.keySet());
accessedTables.addAll(sqlExecutionsDto.updatedData.keySet());
}
}
Expand Down Expand Up @@ -495,13 +496,13 @@ public final void cleanAccessedTables(){
// clean accessed tables
Set<String> tableDataToInit = null;
if (!accessedTables.isEmpty()){
List<String> tablesToClean = new ArrayList<>();
getTableToClean(accessedTables, tablesToClean);
List<String> tablesToClean = getTablesToClean(accessedTables);
if (!tablesToClean.isEmpty()){
if (emDbClean.schemaNames != null && !emDbClean.schemaNames.isEmpty()){
emDbClean.schemaNames.forEach(sch-> DbCleaner.clearDatabase(getConnectionIfExist(), sch, null, tablesToClean, emDbClean.dbType));
}else
DbCleaner.clearDatabase(getConnectionIfExist(), null, null, tablesToClean, emDbClean.dbType);
} else {
DbCleaner.clearDatabase(getConnectionIfExist(), null, null, tablesToClean, emDbClean.dbType);
}
tableDataToInit = tablesToClean.stream().filter(a-> tableInitSqlMap.keySet().stream().anyMatch(t-> t.equalsIgnoreCase(a))).collect(Collectors.toSet());
}
}
Expand Down Expand Up @@ -582,7 +583,13 @@ public void addSuccessfulInitSqlInsertion(InsertionDto insertionDto){
successfulInitSqlInsertions.add(insertionDto);
}

private void getTableToClean(List<String> accessedTables, List<String> tablesToClean){
private List<String> getTablesToClean(List<String> accessedTables) {
List<String> tablesToClean = new ArrayList<>();
fillTablesToClean(accessedTables,tablesToClean);
return tablesToClean;
}

private void fillTablesToClean(List<String> accessedTables, List<String> tablesToClean){
for (String t: accessedTables){
if (!findInCollectionIgnoreCase(t, tablesToClean).isPresent()){
if (findInMapIgnoreCase(t, fkMap).isPresent()){
Expand All @@ -591,7 +598,7 @@ private void getTableToClean(List<String> accessedTables, List<String> tablesToC
findInCollectionIgnoreCase(t, e.getValue()).isPresent()
&& !findInCollectionIgnoreCase(e.getKey(), tablesToClean).isPresent()).map(Map.Entry::getKey).collect(Collectors.toList());
if (!fk.isEmpty())
getTableToClean(fk, tablesToClean);
fillTablesToClean(fk, tablesToClean);
}else {
SimpleLogger.uniqueWarn("Cannot find the table "+t+" in ["+String.join(",", fkMap.keySet())+"]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,12 +544,7 @@ private static void addConstraints(DbInfoDto schemaDto, List<DbTableConstraint>
}
}

private static String getId(TableDto dto){
if(dto.schema == null){
return dto.name;
}
return dto.schema + "." + dto.name;
}


private static void handleTableEntry(Connection connection, DbInfoDto schemaDto, DatabaseMetaData md, ResultSet tables, Set<String> tableIds) throws SQLException {

Expand Down Expand Up @@ -582,13 +577,13 @@ private static void handleTableEntry(Connection connection, DbInfoDto schemaDto,
tableDto.schema = tableSchema;
tableDto.catalog = tableCatalog;

if (tableIds.contains(getId(tableDto))) {
if (tableIds.contains(SqlDtoUtils.getId(tableDto))) {
/*
* Perhaps we should throw a more specific exception than IllegalArgumentException
*/
throw new IllegalArgumentException("Cannot handle repeated table " + getId(tableDto) + " in database");
throw new IllegalArgumentException("Cannot handle repeated table " + SqlDtoUtils.getId(tableDto) + " in database");
} else {
tableIds.add(getId(tableDto));
tableIds.add(SqlDtoUtils.getId(tableDto));
}

Set<String> pks = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.evomaster.client.java.sql.internal;
package org.evomaster.client.java.sql;

import org.evomaster.client.java.controller.api.dto.database.operations.InsertionDto;
import org.evomaster.client.java.controller.api.dto.database.schema.ColumnDto;
Expand All @@ -9,9 +9,16 @@
import java.util.Set;
import java.util.stream.Collectors;

public class SqlDatabaseDtoUtils {
public class SqlDtoUtils {


public static String getId(TableDto dto){
if(dto.schema == null){
return dto.name;
}
return dto.schema + "." + dto.name;
}

/**
*
* @param filter specifies which column should be returned, null means all columns should be returned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ private Object getValue(Expression exp, DataRow data) {
Column column = (Column) exp;

String name = column.getColumnName();
String table = context.getTableName(column);
String table = context.getFullyQualifiedTableName(column);

return data.getValueByName(name, table);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.evomaster.client.java.controller.api.dto.database.schema.TableDto;
import org.evomaster.client.java.sql.DataRow;
import org.evomaster.client.java.sql.QueryResult;
import org.evomaster.client.java.sql.SqlDtoUtils;
import org.evomaster.client.java.sql.VariableDescriptor;

import java.time.Instant;
Expand Down Expand Up @@ -129,7 +130,7 @@ public static <T> List<List<T>> cartesianProduct(List<List<T>> values){


private static QueryResult convertInsertionDtoToQueryResult(InsertionDto insertionDto, String tableName, Set<String> relatedColumns, DbInfoDto dto, List<QueryResult> existingQueryResults){
List<String> relatedColumnNames = SqlDatabaseDtoUtils.extractColumnNames(insertionDto, relatedColumns);
List<String> relatedColumnNames = SqlDtoUtils.extractColumnNames(insertionDto, relatedColumns);
if (!relatedColumnNames.isEmpty()){
QueryResult found = null;
if (!existingQueryResults.isEmpty())
Expand All @@ -143,13 +144,13 @@ private static QueryResult convertInsertionDtoToQueryResult(InsertionDto inserti
if (foundTableSchema.isPresent()){
TableDto tableDto = foundTableSchema.get();

List<String> printableValue = SqlDatabaseDtoUtils.extractColumnPrintableValues(insertionDto, relatedColumns);
List<String> printableValue = SqlDtoUtils.extractColumnPrintableValues(insertionDto, relatedColumns);
assert printableValue.size() == relatedColumnNames.size();

List<Object> values = new ArrayList<>();

for (int i = 0; i < printableValue.size(); i++){
ColumnDto columnDto = SqlDatabaseDtoUtils.extractColumnInfo(tableDto, relatedColumnNames.get(i));
ColumnDto columnDto = SqlDtoUtils.extractColumnInfo(tableDto, relatedColumnNames.get(i));
if (columnDto == null)
throw new IllegalArgumentException("Cannot find column schema of "+ relatedColumnNames.get(i) + " in Table "+ tableName);
values.add(getColumnValueBasedOnPrintableValue(printableValue.get(i), columnDto));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public Map<String, Set<String>> extractColumnsInvolvedInWhere(Statement statemen
@Override
public void visit(Column column) {

String tn = context.getTableName(column);
String tn = context.getFullyQualifiedTableName(column);

if (tn.equalsIgnoreCase(SqlNameContext.UNNAMED_TABLE)) {
// TODO handle it properly when ll have support for sub-selects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public boolean hasColumn(String tableName, String columnName){
* @param column a column object
* @return the name of the table that this column belongs to
*/
public String getTableName(Column column) {
public String getFullyQualifiedTableName(Column column) {

Table table = column.getTable();

if (table != null) {
return tableAliases.getOrDefault(table.getName().toLowerCase(), table.getName().toLowerCase());
return tableAliases.getOrDefault(table.getFullyQualifiedName().toLowerCase(), table.getFullyQualifiedName().toLowerCase());
}

if(statement instanceof Select) {
Expand All @@ -112,10 +112,10 @@ public String getTableName(Column column) {
}
} else if(statement instanceof Delete){
Delete delete = (Delete) statement;
return delete.getTable().getName().toLowerCase();
return delete.getTable().getFullyQualifiedName().toLowerCase();
} else if(statement instanceof Update){
Update update = (Update) statement;
return update.getTable().getName().toLowerCase();
return update.getTable().getFullyQualifiedName().toLowerCase();
}else {
throw new IllegalArgumentException("Cannot handle table name for: " + statement);
}
Expand All @@ -131,7 +131,7 @@ private List<String> getTableNamesInFrom() {
FromItemVisitorAdapter visitor = new FromItemVisitorAdapter(){
@Override
public void visit(Table table) {
names.add(table.getName().toLowerCase());
names.add(table.getFullyQualifiedName().toLowerCase());
}
};

Expand Down
7 changes: 7 additions & 0 deletions core/src/main/kotlin/org/evomaster/core/sql/SqlAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ class SqlAction(
}
}

fun getFullQualifyingTableName() : String{
if(openGroupName.isNullOrBlank()){
return table.name
}
return "$openGroupName.${table.name}"
}

private val genes: List<Gene> = (computedGenes
?: selectedColumns.map { SqlActionGeneBuilder().buildGene(id, table, it) }
).also {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ object SqlActionTransformer {
}


val insertion = InsertionDto().apply { targetTable = action.table.name }
val insertion = InsertionDto().apply { targetTable = action.getFullQualifyingTableName() }

for (g in action.seeTopGenes()) {
if (g is SqlPrimaryKeyGene) {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.foo.rest.examples.multidb.base

import com.foo.rest.examples.multidb.separatedschemas.EntityX
import org.springframework.http.ResponseEntity
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.web.bind.annotation.GetMapping
Expand All @@ -11,18 +10,15 @@ import org.springframework.web.bind.annotation.RestController
@RequestMapping(path = ["/api/base"])
@RestController
open class BaseRest(
//private val repository: BaseRepository
private val jdbc: JdbcTemplate
) {

@GetMapping(path = ["/{id}"])
open fun get(@PathVariable id: String) : ResponseEntity<String> {


val x = jdbc.queryForObject("SELECT * FROM foo.EntityX WHERE id = '$id'", BaseEntity::class.java)
val x = jdbc.queryForObject("SELECT * FROM foo.BaseTable WHERE id = '$id'", BaseTable::class.java)

// val found = repository.findById(id)
// if(found.isPresent) {
if(x != null) {
return ResponseEntity.ok("OK")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.foo.rest.examples.multidb.base


open class BaseTable(

open var id: String? = null,

open var name: String? = null,
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
CREATE TABLE IF NOT EXISTS BaseEntity(
CREATE SCHEMA IF NOT EXISTS foo;

CREATE TABLE IF NOT EXISTS foo.BaseTable(
id VARCHAR(50) NOT NULL,
name VARCHAR(50) NOT NULL,
constraint pk_x primary key (id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.evomaster.e2etests.spring.multidb

import org.evomaster.client.java.controller.EmbeddedSutController
import org.evomaster.client.java.controller.InstrumentedSutStarter
import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType
import org.evomaster.client.java.instrumentation.InputProperties
Expand All @@ -14,6 +13,7 @@ import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.EnumSource


abstract class MultiDbParameterizedE2ETemplate : RestTestBase(){

companion object {
Expand Down

0 comments on commit 82e2f87

Please sign in to comment.