#784 Make DataSource#purgeRecords case-insensitive

This commit is contained in:
ljacqu 2016-07-18 21:29:05 +02:00
parent cf1032d936
commit 57f90fe410
4 changed files with 16 additions and 30 deletions

View File

@ -258,34 +258,7 @@ public class FlatFile implements DataSource {
@Override
public void purgeRecords(Collection<String> toPurge) {
BufferedReader br = null;
BufferedWriter bw = null;
ArrayList<String> lines = new ArrayList<>();
try {
br = new BufferedReader(new FileReader(source));
bw = new BufferedWriter(new FileWriter(source));
String line;
while ((line = br.readLine()) != null) {
String[] args = line.split(":");
if (args.length >= 4) {
if (toPurge.contains(args[0])) {
lines.add(line);
continue;
}
}
}
for (String l : lines) {
bw.write(l + "\n");
}
} catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage());
return;
} finally {
silentClose(br);
silentClose(bw);
}
throw new UnsupportedOperationException("Flat file no longer supported");
}
@Override

View File

@ -698,7 +698,7 @@ public class MySQL implements DataSource {
String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
for (String name : toPurge) {
pst.setString(1, name);
pst.setString(1, name.toLowerCase());
pst.executeUpdate();
}
} catch (SQLException ex) {

View File

@ -322,7 +322,7 @@ public class SQLite implements DataSource {
String delete = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
try (PreparedStatement deletePst = con.prepareStatement(delete)) {
for (String name : toPurge) {
deletePst.setString(1, name);
deletePst.setString(1, name.toLowerCase());
deletePst.executeUpdate();
}
} catch (SQLException ex) {

View File

@ -369,4 +369,17 @@ public abstract class AbstractDataSourceIntegrationTest {
assertThat(dataSource.getLoggedPlayers(), empty());
}
@Test
public void shouldPerformPurgeOperation() {
// given
List<String> names = Arrays.asList("Bobby", "USER", "DoesnotExist");
DataSource dataSource = getDataSource();
// when
dataSource.purgeRecords(names);
// then
assertThat(dataSource.getAllAuths(), empty());
}
}