Trim all strings from SQL

they might end with null terminator
This commit is contained in:
Jason Penilla committed 2023-03-26 11:00:29 -07:00
1 parent f24a64c59a
commit 4eff3579ed
5 files changed
+52 -13

No files matched your search

@@ -21,6 +21,7 @@ package net.draycia.carbon.common.users.db;
import java.sql.ResultSet;
import java.sql.SQLException;
import net.draycia.carbon.common.util.Strings;
import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.intellij.lang.annotations.Subst;
@@ -31,11 +32,10 @@ public class KeyColumnMapper implements ColumnMapper<Key> {
@Override
public Key map(final ResultSet rs, final int columnNumber, final StatementContext ctx) throws SQLException {
final @Nullable String keyValue = rs.getString(columnNumber);
final @Nullable @Subst("key:value") String trimmed = keyValue == null ? null : keyValue.trim();
final @Nullable @Subst("key:value") String keyValue = Strings.trim(rs.getString(columnNumber));
if (keyValue != null) {
return Key.key(trimmed);
return Key.key(keyValue);
}
return null;
@@ -23,6 +23,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.util.Strings;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -34,17 +35,17 @@ public class MySQLPlayerRowMapper implements RowMapper<CarbonPlayerCommon> {
@Override
public CarbonPlayerCommon map(final ResultSet rs, final StatementContext ctx) throws SQLException {
final String id = rs.getString("id");
final @Nullable String lastWhisperTarget = rs.getString("lastwhispertarget");
final @Nullable String whisperReplyTarget = rs.getString("whisperreplytarget");
final @Nullable @Subst("carbon:global") String selectedChannel = rs.getString("selectedchannel");
final @Nullable String displayName = rs.getString("displayname");
final String id = Strings.trim(rs.getString("id"));
final @Nullable String lastWhisperTarget = Strings.trim(rs.getString("lastwhispertarget"));
final @Nullable String whisperReplyTarget = Strings.trim(rs.getString("whisperreplytarget"));
final @Nullable @Subst("carbon:global") String selectedChannel = Strings.trim(rs.getString("selectedchannel"));
final @Nullable String displayName = Strings.trim(rs.getString("displayname"));
return new CarbonPlayerCommon(
rs.getBoolean("muted"),
rs.getBoolean("deafened"),
selectedChannel == null ? null : Key.key(selectedChannel),
rs.getString("username"),
Strings.trim(rs.getString("username")),
UUID.fromString(id),
displayName == null ? null : GsonComponentSerializer.gson().deserialize(displayName),
lastWhisperTarget == null ? null : UUID.fromString(lastWhisperTarget),
@@ -23,6 +23,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.util.Strings;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -34,14 +35,14 @@ public class PostgreSQLPlayerRowMapper implements RowMapper<CarbonPlayerCommon>
@Override
public CarbonPlayerCommon map(final ResultSet rs, final StatementContext ctx) throws SQLException {
final @Nullable @Subst("carbon:global") String selectedChannel = rs.getString("selectedchannel");
final @Nullable String displayName = rs.getString("displayname");
final @Nullable @Subst("carbon:global") String selectedChannel = Strings.trim(rs.getString("selectedchannel"));
final @Nullable String displayName = Strings.trim(rs.getString("displayname"));
return new CarbonPlayerCommon(
rs.getBoolean("muted"),
rs.getBoolean("deafened"),
selectedChannel == null ? null : Key.key(selectedChannel),
rs.getString("username"),
Strings.trim(rs.getString("username")),
rs.getObject("id", UUID.class),
displayName == null ? null : GsonComponentSerializer.gson().deserialize(displayName),
rs.getObject("lastwhispertarget", UUID.class),
@@ -0,0 +1,36 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.util;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public final class Strings {
private Strings() {
}
public static @Nullable String trim(final @Nullable String s) {
return s == null ? null : s.trim();
}
}