How to use setSchema method of org.evomaster.client.java.controller.internal.db.SqlHandler class

Best EvoMaster code snippet using org.evomaster.client.java.controller.internal.db.SqlHandler.setSchema

Source:SutController.java Github

copy

Full Screen

...197 * the SUT is started.198 */199 public final void initSqlHandler() {200 sqlHandler.setConnection(getConnectionIfExist());201 sqlHandler.setSchema(getSqlDatabaseSchema());202 }203 /**204 * TODO further handle multiple connections205 * @return sql connection if there exists206 */207 public final Connection getConnectionIfExist(){208 return (getDbSpecifications() == null209 || getDbSpecifications().isEmpty())? null: getDbSpecifications().get(0).connection;210 }211 /**212 *213 * @return whether to employ smart db clean214 */215 public final boolean doEmploySmartDbClean(){...

Full Screen

Full Screen

Source:SqlHandler.java Github

copy

Full Screen

...77 }78 public void setConnection(Connection connection) {79 this.connection = connection;80 }81 public void setSchema(DbSchemaDto schema) {82 this.schema = schema;83 }84 /**85 * handle executed sql info86 * @param sql to be handled87 */88 public void handle(SqlInfo sql) {89 executedInfo.add(new SqlExecutionLogDto(sql.getCommand(), sql.getExecutionTime()));90 handle(sql.getCommand());91 }92 public void handle(String sql) {93 Objects.requireNonNull(sql);94 if(!calculateHeuristics && !extractSqlExecution){95 return;96 }97 numberOfSqlCommands++;98 if(! ParserUtils.canParseSqlStatement(sql)){99 SimpleLogger.warn("Cannot handle SQL statement: " + sql);100 return;101 }102 buffer.add(sql);103 if (isSelect(sql)) {104 mergeNewData(queriedData, ColumnTableAnalyzer.getSelectReadDataFields(sql));105 } else if(isDelete(sql)){106 deletedData.addAll(ColumnTableAnalyzer.getDeletedTables(sql));107 } else if(isInsert(sql)){108 mergeNewData(insertedData, ColumnTableAnalyzer.getInsertedDataFields(sql));109 } else if(isUpdate(sql)){110 mergeNewData(updatedData, ColumnTableAnalyzer.getUpdatedDataFields(sql));111 }112 }113 public ExecutionDto getExecutionDto() {114 if(!calculateHeuristics && !extractSqlExecution){115 return null;116 }117 ExecutionDto executionDto = new ExecutionDto();118 executionDto.queriedData.putAll(queriedData);119 executionDto.failedWhere.putAll(failedWhere);120 executionDto.insertedData.putAll(insertedData);121 executionDto.updatedData.putAll(updatedData);122 executionDto.deletedData.addAll(deletedData);123 executionDto.numberOfSqlCommands = this.numberOfSqlCommands;124 executionDto.sqlExecutionLogDtoList.addAll(executedInfo);125 return executionDto;126 }127 /**128 * compute (SELECT, DELETE and UPDATE) sql distance for sql commands which exists in [buffer]129 * Note that we skip `SELECT 1` (typically for testing sql connection) since its distance is 0130 * @return a list of heuristics for sql commands131 */132 public List<PairCommandDistance> getDistances() {133 if (connection == null || !calculateHeuristics) {134 return distances;135 }136 buffer.stream()137 .forEach(sql -> {138 if (!isSelectOne(sql) && (isSelect(sql) || isDelete(sql) || isUpdate(sql))) {139 double dist;140 try {141 dist = computeDistance(sql);142 }catch (Exception e){143 SimpleLogger.error("FAILED TO COMPUTE HEURISTICS FOR SQL: " + sql);144 //assert false; //TODO put back once we update JSqlParser145 return;146 }147 distances.add(new PairCommandDistance(sql, dist));148 }149 });150 //side effects on buffer is not important, as it is just a cache151 buffer.clear();152 return distances;153 }154 private Double computeDistance(String command) {155 if (connection == null) {156 throw new IllegalStateException("Trying to calculate SQL distance with no DB connection");157 }158 Statement statement;159 try {160 statement = CCJSqlParserUtil.parse(command);161 } catch (Exception e) {162 SimpleLogger.uniqueWarn("Cannot handle command: " + command + "\n" + e.toString());163 return Double.MAX_VALUE;164 }165 Map<String, Set<String>> columns = extractColumnsInvolvedInWhere(statement);166 /*167 even if columns.isEmpty(), we need to check if any data was present168 */169 double dist;170 if(columns.isEmpty()){171 //TODO check if table(s) not empty, and give >0 otherwise172 dist = 0;173 } else {174 dist = getDistanceForWhere(command, columns);175 }176 if (dist > 0) {177 mergeNewData(failedWhere, columns);178 }179 return dist;180 }181 private double getDistanceForWhere(String command, Map<String, Set<String>> columns) {182 String select;183 /*184 TODO:185 this might be likely unnecessary... we are only interested in the variables used186 in the WHERE. Furthermore, this would not support DELETE/INSERT/UPDATE.187 So, we just need to create a new SELECT based on that.188 But SELECT could be complex with many JOINs... whereas DIP would be simple(r)?189 TODO: we need a general solution190 */191 if(isSelect(command)) {192 select = SelectTransformer.addFieldsToSelect(command);193 select = SelectTransformer.removeConstraints(select);194 select = SelectTransformer.removeOperations(select);195 } else {196 if(columns.size() > 1){197 SimpleLogger.uniqueWarn("Cannot analyze: " + command);198 }199 Map.Entry<String, Set<String>> mapping = columns.entrySet().iterator().next();200 select = createSelectForSingleTable(mapping.getKey(), mapping.getValue());201 }202 QueryResult data;203 try {204 data = SqlScriptRunner.execCommand(connection, select);205 } catch (SQLException e) {206 throw new RuntimeException(e);207 }208 return HeuristicsCalculator.computeDistance(command, data);209 }210 private String createSelectForSingleTable(String tableName, Set<String> columns){211 StringBuilder buffer = new StringBuilder();212 buffer.append("SELECT ");213 String variables = columns.stream().collect(Collectors.joining(", "));214 buffer.append(variables);215 buffer.append(" FROM ");216 buffer.append(tableName);217 return buffer.toString();218 }219 /**220 * Check the fields involved in the WHERE clause (if any).221 * Return a map from table name to column names of the involved fields.222 */223 public Map<String, Set<String>> extractColumnsInvolvedInWhere(Statement statement) {224 /*225 TODO226 following does not handle the case of sub-selects involving other227 tables... but likely that is not something we need to support right now228 */229 Map<String, Set<String>> data = new HashMap<>();230 // move getWhere before SqlNameContext, otherwise null where would cause exception in new SqlNameContext231 Expression where = ParserUtils.getWhere(statement);232 if (where == null) {233 return data;234 }235 SqlNameContext context = new SqlNameContext(statement);236 if(schema != null) {237 context.setSchema(schema);238 }239 ExpressionVisitor visitor = new ExpressionVisitorAdapter() {240 @Override241 public void visit(Column column) {242 String tn = context.getTableName(column);243 if(tn.equalsIgnoreCase(SqlNameContext.UNNAMED_TABLE)){244 // TODO handle it properly when ll have support for sub-selects245 return;246 }247 String cn = column.getColumnName().toLowerCase();248 if(! context.hasColumn(tn, cn)) {249 /*250 This is an issue with the JsqlParser library. Until we upgrade it, or fix it if not fixed yet,251 we use this workaround....

Full Screen

Full Screen

setSchema

Using AI Code Generation

copy

Full Screen

1package org.evomaster.client.java.controller.internal.db;2import java.sql.*;3import java.util.*;4public class SqlHandler {5 private final String url;6 private final String username;7 private final String password;8 private final String schema;9 private final String driver;10 private final String dialect;11 private final String dbType;12 public SqlHandler(String url, String username, String password, String schema, String driver, String dialect, String dbType) {13 this.url = url;14 this.username = username;15 this.password = password;16 this.schema = schema;17 this.driver = driver;18 this.dialect = dialect;19 this.dbType = dbType;20 }

Full Screen

Full Screen

setSchema

Using AI Code Generation

copy

Full Screen

1public class 3 {2 public static void main(String[] args) {3 try {4 SqlHandler sqlHandler = new SqlHandler();5 sqlHandler.setSchema("public");6 sqlHandler.close();7 } catch (SQLException e) {8 e.printStackTrace();9 }10 }11}12public class 4 {13 public static void main(String[] args) {14 try {15 SqlHandler sqlHandler = new SqlHandler();16 sqlHandler.setSchema("public");17 sqlHandler.close();18 } catch (SQLException e) {19 e.printStackTrace();20 }21 }22}23public class 5 {24 public static void main(String[] args) {25 try {26 SqlHandler sqlHandler = new SqlHandler();27 sqlHandler.setSchema("public");28 sqlHandler.close();29 } catch (SQLException e) {30 e.printStackTrace();31 }32 }33}34public class 6 {35 public static void main(String[] args) {36 try {37 SqlHandler sqlHandler = new SqlHandler();38 sqlHandler.setSchema("public");39 sqlHandler.close();40 } catch (SQLException e) {41 e.printStackTrace();42 }43 }44}45public class 7 {46 public static void main(String[] args) {47 try {48 SqlHandler sqlHandler = new SqlHandler();49 sqlHandler.setSchema("public");50 sqlHandler.close();51 } catch (SQLException e) {52 e.printStackTrace();53 }54 }55}

Full Screen

Full Screen

setSchema

Using AI Code Generation

copy

Full Screen

1import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType;2import org.evomaster.client.java.controller.internal.db.SqlHandler;3import org.evomaster.client.java.controller.internal.db.schema.SqlSchema;4import java.sql.SQLException;5public class 3 {6 public static void main(String[] args) throws SQLException {7 SqlSchema schema = new SqlSchema(DatabaseType.MYSQL);8 schema.addTable("my_table")9 .addColumn("id").primaryKey().build()10 .addColumn("name").build()11 .addColumn("age").build();12 SqlHandler.getInstance().setSchema(schema);13 }14}

Full Screen

Full Screen

setSchema

Using AI Code Generation

copy

Full Screen

1import org.evomaster.client.java.controller.internal.db.SqlHandler;2import java.sql.SQLException;3public class Main {4 public static void main(String[] args) throws SQLException {5 SqlHandler sqlHandler = new SqlHandler();6 sqlHandler.setSchema("test");7 System.out.println("Database schema name is set to " + sqlHandler.getSchema());8 }9}10import org.evomaster.client.java.controller.internal.db.SqlHandler;11import java.sql.SQLException;12public class Main {13 public static void main(String[] args) throws SQLException {14 SqlHandler sqlHandler = new SqlHandler();15 sqlHandler.setAutoCommit(false);16 System.out.println("Auto commit is set to " + sqlHandler.isAutoCommit());17 }18}19import org.evomaster.client.java.controller.internal.db.SqlHandler;20import java.sql.SQLException;21public class Main {22 public static void main(String[] args) throws SQLException {23 SqlHandler sqlHandler = new SqlHandler();24 sqlHandler.setAutoCommit(false);25 sqlHandler.commit();26 System.out.println("Changes are committed to the database");27 }28}29import org.evomaster.client.java.controller.internal.db.SqlHandler;30import java.sql.SQLException;31public class Main {32 public static void main(String[] args) throws SQLException {33 SqlHandler sqlHandler = new SqlHandler();34 sqlHandler.setAutoCommit(false);35 sqlHandler.rollback();36 System.out.println("Changes are rolled back to the database");37 }38}

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