Batch Insert for SecurityTable

This commit is contained in:
Rsl1122 2017-09-02 11:34:17 +03:00
parent f31c597300
commit 4061c9c4cd
5 changed files with 45 additions and 7 deletions

View File

@ -4,6 +4,7 @@
*/
package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException;
import main.java.com.djrapitops.plan.data.Action;
@ -165,7 +166,7 @@ public class ActionsTable extends UserIDTable {
}
public void insertActions(Map<UUID, Map<UUID, List<Action>>> allActions) throws SQLException {
if (allActions.isEmpty()) {
if (Verify.isEmpty(allActions)) {
return;
}
PreparedStatement statement = null;

View File

@ -1,5 +1,6 @@
package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Select;
@ -196,7 +197,7 @@ public class IPsTable extends UserIDTable {
}
public void insertIPsAndGeolocations(Map<UUID, Map<String, String>> allIPsAndGeolocations) throws SQLException {
if (allIPsAndGeolocations.isEmpty()) {
if (Verify.isEmpty(allIPsAndGeolocations)) {
return;
}
PreparedStatement statement = null;

View File

@ -1,5 +1,6 @@
package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
@ -200,7 +201,7 @@ public class NicknamesTable extends UserIDTable {
}
public void insertNicknames(Map<UUID, Map<UUID, List<String>>> allNicknames) throws SQLException {
if (allNicknames.isEmpty()) {
if (Verify.isEmpty(allNicknames)) {
return;
}
PreparedStatement statement = null;

View File

@ -5,6 +5,7 @@
*/
package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException;
import main.java.com.djrapitops.plan.data.WebUser;
@ -28,9 +29,14 @@ public class SecurityTable extends Table {
private final String columnUser = "username";
private final String columnSaltedHash = "salted_pass_hash";
private final String columnPermLevel = "permission_level";
private String insertStatement;
public SecurityTable(SQLDB db, boolean usingMySQL) {
super("plan_security", db, usingMySQL);
insertStatement = Insert.values(tableName,
columnUser,
columnSaltedHash,
columnPermLevel);
}
@Override
@ -67,10 +73,7 @@ public class SecurityTable extends Table {
public void addNewUser(String user, String saltPassHash, int permLevel) throws SQLException {
PreparedStatement statement = null;
try {
statement = prepareStatement(Insert.values(tableName,
columnUser,
columnSaltedHash,
columnPermLevel));
statement = prepareStatement(insertStatement);
statement.setString(1, user);
statement.setString(2, saltPassHash);
statement.setInt(3, permLevel);
@ -110,6 +113,7 @@ public class SecurityTable extends Table {
ResultSet set = null;
try {
statement = prepareStatement(Select.all(tableName).toString());
statement.setFetchSize(5000);
set = statement.executeQuery();
List<WebUser> list = new ArrayList<>();
while (set.next()) {
@ -125,4 +129,29 @@ public class SecurityTable extends Table {
close(set, statement);
}
}
public void addUsers(List<WebUser> users) throws SQLException {
if (Verify.isEmpty(users)) {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
for (WebUser user : users) {
String userName = user.getName();
String pass = user.getSaltedPassHash();
int permLvl = user.getPermLevel();
statement.setString(1, userName);
statement.setString(2, pass);
statement.setInt(3, permLvl);
statement.addBatch();
}
statement.executeBatch();
commit(statement.getConnection());
} finally {
close(statement);
}
}
}

View File

@ -25,6 +25,8 @@ import java.util.UUID;
* The copy methods assume that the table has been cleared, or that no duplicate data will be entered for a user.
* <p>
* clearTable methods can be used to clear the table beforehand.
* <p>
* Server & User tables should be copied first.
*
* @author Rsl1122
* @since 4.0.0
@ -62,4 +64,8 @@ public class BatchOperationTable extends Table {
public void copyNicknames(BatchOperationTable toDB) throws SQLException {
toDB.db.getNicknamesTable().insertNicknames(db.getNicknamesTable().getAllNicknames());
}
public void copyWebUsers(BatchOperationTable toDB) throws SQLException {
toDB.db.getSecurityTable().addUsers(db.getSecurityTable().getUsers());
}
}