Skip to content

Commit

Permalink
Optimized code
Browse files Browse the repository at this point in the history
  • Loading branch information
360matt committed Mar 20, 2021
1 parent 159abf6 commit b74149b
Showing 1 changed file with 37 additions and 39 deletions.
76 changes: 37 additions & 39 deletions src/main/java/wtf/listenia/citronDB/api/TableManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.lang.reflect.Field;
import java.sql.*;
import java.util.*;
import java.util.stream.Collectors;

public class TableManager <D> {

Expand Down Expand Up @@ -81,7 +82,7 @@ public final void deleteTable () {

public void updateStructure () {
try {
final List<String> local = new ArrayList<>();
final Map<String, Field> local = new HashMap<>();
final List<String> distant = new ArrayList<>();

final Statement stmt = this.database.getStatementWithException();
Expand All @@ -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<String> toCreate = new ArrayList<>(local.keySet());
toCreate.removeAll(distant);
final List<String> toDelete = new ArrayList<>(distant);
toDelete.removeAll(local.keySet());
final List<String> 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();
}
}
Expand Down

0 comments on commit b74149b

Please sign in to comment.