How to use getAllTableHasIdentify method of org.evomaster.client.java.controller.db.DbCleaner class

Best EvoMaster code snippet using org.evomaster.client.java.controller.db.DbCleaner.getAllTableHasIdentify

Source:DbCleaner.java Github

copy

Full Screen

...109 }110 }111 Set<String> tablesHaveIdentifies = new HashSet<>();112 if (type == DatabaseType.MS_SQL_SERVER){113 ResultSet rst = statement.executeQuery(getAllTableHasIdentify(type, schema));114 while (rst.next()) {115 tablesHaveIdentifies.add(rst.getString(1));116 }117 rst.close();118 }119 List<String> tablesToClear = tables.stream()120 .filter(n -> tablesToSkip == null || tablesToSkip.isEmpty() ||121 !tablesToSkip.stream().anyMatch(skip -> skip.equalsIgnoreCase(n)))122 .collect(Collectors.toList());123 if (singleCommand) {124 String ts = tablesToClear.stream()125 .sorted()126 .collect(Collectors.joining(","));127 if (type != DatabaseType.POSTGRES)128 throw new IllegalArgumentException("do not support for cleaning all data by one single command for " +type);129 if (doDropTable)130 dropTables(statement, ts);131 else{132 statement.executeUpdate("TRUNCATE TABLE " + ts);133 }134 } else {135 //note: if one at a time, need to make sure to first disable FK checks136 for(String t : tablesToClear){137 if (doDropTable)138 dropTables(statement, t);139 else{140 /*141 for MS_SQL_SERVER, we cannot use truncate tables if there exist fk142 see143 https://docs.microsoft.com/en-us/sql/t-sql/statements/truncate-table-transact-sql?view=sql-server-ver15#restrictions144 https://stackoverflow.com/questions/155246/how-do-you-truncate-all-tables-in-a-database-using-tsql#156813145 then it will cause a problem to reset identify146 */147 if (type == DatabaseType.MS_SQL_SERVER)148 deleteTables(statement, t, tablesHaveIdentifies);149 else150 truncateTables(statement, t);151 }152 }153 }154 }155 private static void dropTables(Statement statement, String table) throws SQLException {156 statement.executeUpdate("DROP TABLE IF EXISTS " +table);157 }158 private static void deleteTables(Statement statement, String table, Set<String> tableHasIdentify) throws SQLException {159 statement.executeUpdate("DELETE FROM "+table);160// NOTE TAHT ideally we should reseed identify here, but there would case an issue, i.e., does not contain an identity column161 if (tableHasIdentify.contains(table))162 statement.executeUpdate("DBCC CHECKIDENT ('"+table+"', RESEED, 0)");163 }164 private static void truncateTables(Statement statement, String table) throws SQLException {165 statement.executeUpdate("TRUNCATE TABLE " + table);166 }167 private static void resetSequences(Statement s, DatabaseType type, String schemaName) throws SQLException {168 ResultSet rs;// Idem for sequences169 Set<String> sequences = new HashSet<>();170 rs = s.executeQuery(getAllSequenceCommand(type, schemaName));171 while (rs.next()) {172 sequences.add(rs.getString(1));173 }174 rs.close();175 for (String seq : sequences) {176 s.executeUpdate(resetSequenceCommand(seq, type));177 }178 /*179 Note: we reset all sequences from 1. But the original database might180 have used a different value.181 In most cases (99.99%), this should not be a problem.182 We could allow using different values in this API... but, maybe just easier183 for the user to reset it manually if really needed?184 */185 }186 private static void disableReferentialIntegrity(Statement s, DatabaseType type) throws SQLException {187 switch (type)188 {189 case POSTGRES: break;190 case MS_SQL_SERVER:191 //https://stackoverflow.com/questions/159038/how-can-foreign-key-constraints-be-temporarily-disabled-using-t-sql192 //https://stackoverflow.com/questions/155246/how-do-you-truncate-all-tables-in-a-database-using-tsql#156813193 //https://docs.microsoft.com/en-us/sql/relational-databases/tables/disable-foreign-key-constraints-with-insert-and-update-statements?view=sql-server-ver15194 s.execute("EXEC sp_MSForEachTable \"ALTER TABLE ? NOCHECK CONSTRAINT all\"");195 break;196 case H2:197 s.execute("SET REFERENTIAL_INTEGRITY FALSE");198 break;199 case MARIADB:200 case MYSQL:201 s.execute("SET @@foreign_key_checks = 0;");202 break;203 case OTHER:204 throw new DbUnsupportedException(type);205 }206 }207 private static void enableReferentialIntegrity(Statement s, DatabaseType type) throws SQLException {208 switch (type)209 {210 case POSTGRES: break;211 case MS_SQL_SERVER:212 s.execute("exec sp_MSForEachTable \"ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all\"");213 break;214 case H2:215 /*216 For H2, we have to delete tables one at a time... but, to avoid issues217 with FKs, we must temporarily disable the integrity checks218 */219 s.execute( "SET REFERENTIAL_INTEGRITY TRUE");220 break;221 case MARIADB:222 case MYSQL:223 s.execute("SET @@foreign_key_checks = 1;");224 break;225 case OTHER:226 throw new DbUnsupportedException(type);227 }228 }229 private static int getDefaultReties(DatabaseType type){230 switch (type){231 case MS_SQL_SERVER:232 case POSTGRES: return 0;233 case H2:234 case MARIADB:235 case MYSQL: return 3;236 }237 throw new DbUnsupportedException(type);238 }239 private static String getDefaultSchema(DatabaseType type){240 switch (type){241 case H2: return "PUBLIC";242 //https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/ownership-and-user-schema-separation-in-sql-server243 case MS_SQL_SERVER: return "dbo";244 case MARIADB:245 case MYSQL: throw new IllegalArgumentException("there is no default schema for "+type+", and you must specify a db name here");246 case POSTGRES: return "public";247 }248 throw new DbUnsupportedException(type);249 }250 private static boolean isSingleCleanCommand(DatabaseType type){251 return type == DatabaseType.POSTGRES;252 }253 private static String getAllTableHasIdentify(DatabaseType type, String schema){254 if(type != DatabaseType.MS_SQL_SERVER)255 throw new IllegalArgumentException("getAllTableHasIdentify only supports for MS_SQL_SERVER, not for "+type);256 return getAllTableCommand(type, schema) + " AND OBJECTPROPERTY(OBJECT_ID(TABLE_NAME), 'TableHasIdentity') = 1";257 }258 private static String getAllTableCommand(DatabaseType type, String schema) {259 String command = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES where (TABLE_TYPE='TABLE' OR TABLE_TYPE='BASE TABLE')";260 switch (type){261 // https://stackoverflow.com/questions/175415/how-do-i-get-list-of-all-tables-in-a-database-using-tsql, TABLE_CATALOG='"+dbname+"'"262 case MS_SQL_SERVER:263 // for MySQL, schema is dbname264 case MYSQL:265 case MARIADB:266 case H2:267 case POSTGRES:268 if (schema.isEmpty())269 return command;...

Full Screen

Full Screen

getAllTableHasIdentify

Using AI Code Generation

copy

Full Screen

1import org.evomaster.client.java.controller.db.DbCleaner;2import java.sql.SQLException;3import java.util.List;4public class CleanDatabase{5 public static void cleanDatabase() throws SQLException {6 List<String> tables = DbCleaner.getAllTableHasIdentify();7 for (String table : tables) {8 DbCleaner.dropTable(table);9 }10 }11}

Full Screen

Full Screen

getAllTableHasIdentify

Using AI Code Generation

copy

Full Screen

1function cleanDatabase(){2 var xhr = new XMLHttpRequest();3 xhr.open("POST", url, false);4 xhr.send();5}6function cleanTable(tableName){7 var xhr = new XMLHttpRequest();8 xhr.open("POST", url, false);9 xhr.send();10}11function cleanTable(tableName){12 var xhr = new XMLHttpRequest();13 xhr.open("POST", url, false);14 xhr.send();15}16function cleanTable(tableName){17 var xhr = new XMLHttpRequest();18 xhr.open("POST", url, false);19 xhr.send();20}21function cleanTable(tableName){22 var xhr = new XMLHttpRequest();23 xhr.open("POST", url, false);24 xhr.send();25}26function cleanTable(tableName){

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