From 507c2299da61a4e7a1dc0e758329c8ad4774ef35 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Mon, 14 Oct 2024 20:15:14 +0200 Subject: [PATCH] [FEAT] add clean on Mongo --- .../dataAccess/DataAccessMorphia.java | 65 ++----------------- test/src/test/kar/archidata/ConfigureDb.java | 3 + 2 files changed, 7 insertions(+), 61 deletions(-) diff --git a/src/org/kar/archidata/dataAccess/DataAccessMorphia.java b/src/org/kar/archidata/dataAccess/DataAccessMorphia.java index 488097b..60ce255 100644 --- a/src/org/kar/archidata/dataAccess/DataAccessMorphia.java +++ b/src/org/kar/archidata/dataAccess/DataAccessMorphia.java @@ -1259,74 +1259,17 @@ public class DataAccessMorphia extends DataAccess { @Override public void drop(final Class clazz, final QueryOption... option) throws Exception { - /* final QueryOptions options = new QueryOptions(option); final String collectionName = AnnotationTools.getCollectionName(clazz, options); - final DBEntry entry = DBInterfaceOption.getAutoEntry(options); - final StringBuilder query = new StringBuilder(); - query.append("DROP TABLE IF EXISTS `"); - query.append(tableName); - query.append("`"); - try { - LOGGER.trace("Execute Query: {}", query.toString()); - // Remove main table - final PreparedStatement ps = entry.connection.prepareStatement(query.toString()); - ps.executeUpdate(); - // search subTable: - for (final Field field : clazz.getFields()) { - // static field is only for internal global declaration ==> remove it .. - if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) { - continue; - } - if (AnnotationTools.isGenericField(field)) { - continue; - } - final DataAccessAddOn addOn = findAddOnforField(field); - if (addOn != null && !addOn.canInsert(field)) { - LOGGER.error("TODO: Add on not managed ... "); - //addOn.drop(tableName, field); - } - } - } finally { - entry.close(); - } - */ + final MongoCollection collection = this.db.getDatastore().getDatabase().getCollection(collectionName); + collection.drop(); } @Override public void cleanAll(final Class clazz, final QueryOption... option) throws Exception { - /* final QueryOptions options = new QueryOptions(option); final String collectionName = AnnotationTools.getCollectionName(clazz, options); - DBEntry entry = DBInterfaceOption.getAutoEntry(options); - final StringBuilder query = new StringBuilder(); - query.append("DELETE FROM `"); - query.append(tableName); - query.append("`"); - try { - LOGGER.trace("Execute Query: {}", query.toString()); - // Remove main table - final PreparedStatement ps = entry.connection.prepareStatement(query.toString()); - ps.executeUpdate(); - // search subTable: - for (final Field field : clazz.getFields()) { - // static field is only for internal global declaration ==> remove it .. - if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) { - continue; - } - if (AnnotationTools.isGenericField(field)) { - continue; - } - final DataAccessAddOn addOn = findAddOnforField(field); - if (addOn != null && !addOn.canInsert(field)) { - LOGGER.error("TODO: Add on not managed ... "); - //addOn.cleanAll(tableName, field); - } - } - } finally { - entry.close(); - entry = null; - } - */ + final MongoCollection collection = this.db.getDatastore().getDatabase().getCollection(collectionName); + collection.deleteMany(new Document()); } } diff --git a/test/src/test/kar/archidata/ConfigureDb.java b/test/src/test/kar/archidata/ConfigureDb.java index ac1ca0a..83fb75a 100644 --- a/test/src/test/kar/archidata/ConfigureDb.java +++ b/test/src/test/kar/archidata/ConfigureDb.java @@ -3,6 +3,7 @@ package test.kar.archidata; import java.io.IOException; import org.kar.archidata.GlobalConfiguration; +import org.kar.archidata.dataAccess.DataAccess; import org.kar.archidata.db.DBEntry; import org.kar.archidata.tools.ConfigBaseVariable; import org.slf4j.Logger; @@ -52,6 +53,8 @@ public class ConfigureDb { public static void clear() throws IOException { LOGGER.info("Remove the test db"); + final DataAccess da = DataAccess.createInterface(); + //da.cleanAll(null, null); DBEntry.closeAllForceMode(); ConfigBaseVariable.clearAllValue();