Refactored SecurityTable#addUsers to an executable

This commit is contained in:
Rsl1122 2019-01-24 19:32:19 +02:00
parent 2516c55872
commit cf8cadeb28
3 changed files with 28 additions and 32 deletions

View File

@ -82,7 +82,7 @@ public class BackupCopyTransaction extends RemoveEverythingTransaction {
}
private void copyWebUsers() {
db.getSecurityTable().addUsers(sourceDB.query(LargeFetchQueries.fetchAllPlanWebUsers()));
copy(LargeStoreQueries::storeAllPlanWebUsers, LargeFetchQueries.fetchAllPlanWebUsers());
}
private void copyServers() {

View File

@ -16,6 +16,7 @@
*/
package com.djrapitops.plan.db.sql.queries;
import com.djrapitops.plan.data.WebUser;
import com.djrapitops.plan.data.container.GeoInfo;
import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.db.access.ExecBatchStatement;
@ -23,6 +24,7 @@ import com.djrapitops.plan.db.access.Executable;
import com.djrapitops.plan.db.sql.tables.CommandUseTable;
import com.djrapitops.plan.db.sql.tables.GeoInfoTable;
import com.djrapitops.plan.db.sql.tables.NicknamesTable;
import com.djrapitops.plan.db.sql.tables.SecurityTable;
import com.djrapitops.plugin.utilities.Verify;
import java.sql.PreparedStatement;
@ -136,4 +138,26 @@ public class LargeStoreQueries {
}
};
}
public static Executable storeAllPlanWebUsers(List<WebUser> users) {
if (Verify.isEmpty(users)) {
return Executable.empty();
}
return new ExecBatchStatement(SecurityTable.INSERT_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
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();
}
}
};
}
}

View File

@ -25,12 +25,10 @@ import com.djrapitops.plan.db.sql.parsing.CreateTableParser;
import com.djrapitops.plan.db.sql.parsing.Insert;
import com.djrapitops.plan.db.sql.parsing.Select;
import com.djrapitops.plan.db.sql.parsing.Sql;
import com.djrapitops.plugin.utilities.Verify;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
* Table that is in charge of storing WebUser data.
@ -48,16 +46,12 @@ public class SecurityTable extends Table {
public static final String SALT_PASSWORD_HASH = "salted_pass_hash";
public static final String PERMISSION_LEVEL = "permission_level";
public static final String INSERT_STATEMENT = Insert.values(TABLE_NAME, USERNAME, SALT_PASSWORD_HASH, PERMISSION_LEVEL);
public SecurityTable(SQLDB db) {
super(TABLE_NAME, db);
insertStatement = Insert.values(tableName,
USERNAME,
SALT_PASSWORD_HASH,
PERMISSION_LEVEL);
}
private String insertStatement;
public static String createTableSQL(DBType dbType) {
return CreateTableParser.create(TABLE_NAME, dbType)
.column(USERNAME, Sql.varchar(100)).notNull().unique()
@ -103,7 +97,7 @@ public class SecurityTable extends Table {
}
public void addNewUser(String user, String saltPassHash, int permLevel) {
execute(new ExecStatement(insertStatement) {
execute(new ExecStatement(INSERT_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, user);
@ -116,26 +110,4 @@ public class SecurityTable extends Table {
public boolean userExists(String user) {
return getWebUser(user) != null;
}
public void addUsers(List<WebUser> users) {
if (Verify.isEmpty(users)) {
return;
}
executeBatch(new ExecStatement(insertStatement) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
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();
}
}
});
}
}