diff --git a/src/main/java/wtf/listenia/citronDB/api/TableManager.java b/src/main/java/wtf/listenia/citronDB/api/TableManager.java index d6c88c1..44ce1ad 100644 --- a/src/main/java/wtf/listenia/citronDB/api/TableManager.java +++ b/src/main/java/wtf/listenia/citronDB/api/TableManager.java @@ -9,6 +9,7 @@ import java.lang.reflect.Field; import java.sql.*; import java.util.*; +import java.util.stream.Collectors; public class TableManager { @@ -81,7 +82,7 @@ public final void deleteTable () { public void updateStructure () { try { - final List local = new ArrayList<>(); + final Map local = new HashMap<>(); final List distant = new ArrayList<>(); final Statement stmt = this.database.getStatementWithException(); @@ -91,55 +92,52 @@ public void updateStructure () { distant.add(meta.getColumnName(i + 1)); rs.close(); - final Class instance = this.defaultInstance; for (final Field field : this.defaultInstance.getFields()) - local.add(field.getName()); // + " TEXT" + local.put(field.getName(), field); // + " TEXT" + if (local.size() == 0 || distant.size() == 0) { + stmt.close(); + return; + } - if (local.size() > 0 || distant.size() > 0) { - final StringJoiner toDelete = new StringJoiner(","); - final StringJoiner toModify = new StringJoiner(","); - final StringJoiner toCreate = new StringJoiner(","); - for (final String candid : distant) { - if (!local.contains(candid)) { - toDelete.add("DROP " + candid); - } else { - final Field field = instance.getField(candid); - toModify.add("MODIFY " + ColumnType.getFormat(field, false)); - } - } - local.removeAll(distant); // no-GC - distant.clear(); // GC-only - for (final String candid : local) { - final Field field = instance.getField(candid); - toCreate.add(ColumnType.getFormat(field)); - } + final List toCreate = new ArrayList<>(local.keySet()); + toCreate.removeAll(distant); + final List toDelete = new ArrayList<>(distant); + toDelete.removeAll(local.keySet()); + final List toModify = new ArrayList<>(local.keySet()); + toModify.removeAll(toCreate); - final StringJoiner actions = new StringJoiner(", "); - boolean mustSend = false; + final StringJoiner action = new StringJoiner(","); + boolean mustSaad = false; - if (toDelete.length() > 0) { - actions.add(toDelete.toString()); - mustSend = true; - } - if (toModify.length() > 0) { - actions.add(toModify.toString()); - mustSend = true; - } - if (toCreate.length() > 0) { - actions.add("ADD(" + toCreate.toString() + ")"); - mustSend = true; - } - - if (mustSend) // If request is complete - stmt.execute("ALTER TABLE `" + this.name + "` " + actions.toString() + ";"); + if (toCreate.size() > 0) { + action.add("ADD(" + toCreate.stream() + .map(x -> ColumnType.getFormat(local.get(x), false)) + .collect(Collectors.joining(",")) + + ")"); + mustSaad = true; + } + if (toDelete.size() > 0) { + action.add("DROP " + String.join(",DROP ", toDelete)); + mustSaad = true; + } + if (toModify.size() > 0) { + action.add("MODIFY " + toModify.stream() + .map(x -> ColumnType.getFormat(local.get(x), false)) + .collect(Collectors.joining(",MODIFY ")) + ); + mustSaad = true; } + if (mustSaad) // If request is complete + stmt.execute("ALTER TABLE `" + this.name + "` " + action.toString() + ";"); + + stmt.close(); - } catch (final SQLException | NoSuchFieldException e) { + } catch (final SQLException e) { e.printStackTrace(); } }