From 0da9db00d0688d8371f45eb47a6b21ab07322ed6 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Wed, 29 Apr 2015 18:09:29 +1000 Subject: [PATCH] Workin on UUID conversion (still) --- .../plot/database/SQLManager.java | 100 +++++++++++++++++- .../plot/database/StmtMod.java | 34 ++++++ 2 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 PlotSquared/src/main/java/com/intellectualcrafters/plot/database/StmtMod.java diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java index d4c598ccf..d158b08b7 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java @@ -238,7 +238,105 @@ public class SQLManager implements AbstractDB { } } + public void setBulk(ArrayList objList, StmtMod mod) { + final int size = objList.size(); + int packet; + if (PlotSquared.getMySQL() != null) { + packet = Math.min(size, 50000); + } else { + packet = Math.min(size, 50); + } + final int amount = size / packet; + for (int j = 0; j <= amount; j++) { + final List subList = objList.subList(j * packet, Math.min(size, (j + 1) * packet)); + if (subList.size() == 0) { + return; + } + String statement = mod.getCreateMySQL(packet); + PreparedStatement stmt = null; + try { + stmt = this.connection.prepareStatement(statement.toString()); + for (int i = 0; i < subList.size(); i++) { + final T obj = subList.get(i); + mod.setMySQL(stmt,i , obj); + } + stmt.executeUpdate(); + stmt.close(); + } catch (final Exception e) { + try { + String unionstmt = mod.getCreateSQLite(packet); + stmt = this.connection.prepareStatement(unionstmt.toString()); + for (int i = 0; i < subList.size(); i++) { + mod.setSQLite(stmt, i, subList.get(i)); + } + stmt.executeUpdate(); + stmt.close(); + } + catch (Exception e2) { + e2.printStackTrace(); + PlotSquared.log("&6[WARN] " + "Could not bulk save!"); + try { + for (final T obj : subList) { + try { + stmt = connection.prepareStatement(mod.getCreateSQL()); + mod.setSQL(stmt, obj); + stmt.executeUpdate(); + stmt.close(); + } catch (final Exception e3) { + PlotSquared.log("&c[ERROR] " + "Failed to save " + obj + "!"); + } + } + } catch (final Exception e4) { + e4.printStackTrace(); + PlotSquared.log("&c[ERROR] " + "Failed to save all!"); + } + } + } + } + } + public void createSettings(final ArrayList mylist) { + StmtMod mod = new StmtMod() { + + @Override + public String getCreateMySQL(int size) { + return getCreateMySQL(size, CREATE_SETTINGS, 1); + } + + @Override + public String getCreateSQLite(int size) { + String query = "INSERT INTO `" + prefix + "plot_settings` SELECT ? AS `plot_plot_id`, ? AS `biome`, ? AS `rain`, ? AS `custom_time`, ? AS `time`, ? AS `deny_entry`, ? AS `alias`, ? AS `flags`, ? AS `merged`, ? AS `position` "; + int params = 10; + for (int i = 0; i < (ids.size() - 2); i++) { + unionstmt.append("UNION SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ? "); + } + unionstmt.append("UNION SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ? "); + } + + @Override + public String getCreateSQL() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void setMySQL(PreparedStatement stmt, int i, Integer obj) { + // TODO Auto-generated method stub + } + + @Override + public void setSQLite(PreparedStatement stmt, int i, Integer obj) { + // TODO Auto-generated method stub + } + + @Override + public void setSQL(PreparedStatement stmt, Integer obj) { + // TODO Auto-generated method stub + } + }; + + String create1 = CREATE_SETTINGS; + final int size = mylist.size(); int packet; if (PlotSquared.getMySQL() != null) { @@ -252,7 +350,7 @@ public class SQLManager implements AbstractDB { if (ids.size() == 0) { return; } - final StringBuilder statement = new StringBuilder(this.CREATE_SETTINGS); + final StringBuilder statement = new StringBuilder(create1); for (int i = 0; i < (ids.size() - 1); i++) { statement.append("(?),"); } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/StmtMod.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/StmtMod.java new file mode 100644 index 000000000..429750375 --- /dev/null +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/StmtMod.java @@ -0,0 +1,34 @@ +package com.intellectualcrafters.plot.database; + +import java.sql.PreparedStatement; + +import org.apache.commons.lang.StringUtils; + +public abstract class StmtMod { + public abstract String getCreateMySQL(int size); + + public String getCreateMySQL(int size, String query, int params) { + final StringBuilder statement = new StringBuilder(query); + for (int i = 0; i < size - 1; i++) { + statement.append(StringUtils.repeat("(?),", params)); + } + statement.append(StringUtils.repeat(",(?)", params).substring(1)); + return statement.toString(); + } + + public String getCreateSQLite(int size, String query, int params) { + StringBuilder statement = new StringBuilder(query); + String modParams = StringUtils.repeat(",?", params).substring(1); + for (int i = 0; i < (size - 1); i++) { + statement.append("UNION SELECT " + modParams + " "); + } + return statement.toString(); + } + + public abstract String getCreateSQLite(int size); + public abstract String getCreateSQL(); + + public abstract void setMySQL(PreparedStatement stmt, int i, T obj); + public abstract void setSQLite(PreparedStatement stmt, int i, T obj); + public abstract void setSQL(PreparedStatement stmt, T obj); +}