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

@@ -37,6 +37,7 @@ public interface UserManager<C extends CarbonPlayer> {
/** /**
* Loads and returns a {@link CarbonPlayer} with the given {@link UUID}. * Loads and returns a {@link CarbonPlayer} with the given {@link UUID}.
* *
* @deprecated use {@link #user(UUID)} instead
* @param uuid the player's uuid * @param uuid the player's uuid
* @return the result * @return the result
* @since 2.0.0 * @since 2.0.0
@@ -55,6 +56,6 @@ public interface UserManager<C extends CarbonPlayer> {
* @return the player * @return the player
* @since 2.1.0 * @since 2.1.0
*/ */
CompletableFuture<C> user(final UUID uuid); CompletableFuture<C> user(UUID uuid);
} }
@@ -21,6 +21,7 @@ package net.draycia.carbon.common.users.db;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import net.draycia.carbon.common.util.Strings;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.intellij.lang.annotations.Subst; import org.intellij.lang.annotations.Subst;
@@ -31,11 +32,10 @@ public class KeyColumnMapper implements ColumnMapper<Key> {
@Override @Override
public Key map(final ResultSet rs, final int columnNumber, final StatementContext ctx) throws SQLException { 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 keyValue = Strings.trim(rs.getString(columnNumber));
final @Nullable @Subst("key:value") String trimmed = keyValue == null ? null : keyValue.trim();
if (keyValue != null) { if (keyValue != null) {
return Key.key(trimmed); return Key.key(keyValue);
} }
return null; return null;
@@ -23,6 +23,7 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.UUID; import java.util.UUID;
import net.draycia.carbon.common.users.CarbonPlayerCommon; import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.util.Strings;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
@@ -34,17 +35,17 @@ public class MySQLPlayerRowMapper implements RowMapper<CarbonPlayerCommon> {
@Override @Override
public CarbonPlayerCommon map(final ResultSet rs, final StatementContext ctx) throws SQLException { public CarbonPlayerCommon map(final ResultSet rs, final StatementContext ctx) throws SQLException {
final String id = rs.getString("id"); final String id = Strings.trim(rs.getString("id"));
final @Nullable String lastWhisperTarget = rs.getString("lastwhispertarget"); final @Nullable String lastWhisperTarget = Strings.trim(rs.getString("lastwhispertarget"));
final @Nullable String whisperReplyTarget = rs.getString("whisperreplytarget"); final @Nullable String whisperReplyTarget = Strings.trim(rs.getString("whisperreplytarget"));
final @Nullable @Subst("carbon:global") String selectedChannel = rs.getString("selectedchannel"); final @Nullable @Subst("carbon:global") String selectedChannel = Strings.trim(rs.getString("selectedchannel"));
final @Nullable String displayName = rs.getString("displayname"); final @Nullable String displayName = Strings.trim(rs.getString("displayname"));
return new CarbonPlayerCommon( return new CarbonPlayerCommon(
rs.getBoolean("muted"), rs.getBoolean("muted"),
rs.getBoolean("deafened"), rs.getBoolean("deafened"),
selectedChannel == null ? null : Key.key(selectedChannel), selectedChannel == null ? null : Key.key(selectedChannel),
rs.getString("username"), Strings.trim(rs.getString("username")),
UUID.fromString(id), UUID.fromString(id),
displayName == null ? null : GsonComponentSerializer.gson().deserialize(displayName), displayName == null ? null : GsonComponentSerializer.gson().deserialize(displayName),
lastWhisperTarget == null ? null : UUID.fromString(lastWhisperTarget), lastWhisperTarget == null ? null : UUID.fromString(lastWhisperTarget),
@@ -23,6 +23,7 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.UUID; import java.util.UUID;
import net.draycia.carbon.common.users.CarbonPlayerCommon; import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.util.Strings;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
@@ -34,14 +35,14 @@ public class PostgreSQLPlayerRowMapper implements RowMapper<CarbonPlayerCommon>
@Override @Override
public CarbonPlayerCommon map(final ResultSet rs, final StatementContext ctx) throws SQLException { public CarbonPlayerCommon map(final ResultSet rs, final StatementContext ctx) throws SQLException {
final @Nullable @Subst("carbon:global") String selectedChannel = rs.getString("selectedchannel"); final @Nullable @Subst("carbon:global") String selectedChannel = Strings.trim(rs.getString("selectedchannel"));
final @Nullable String displayName = rs.getString("displayname"); final @Nullable String displayName = Strings.trim(rs.getString("displayname"));
return new CarbonPlayerCommon( return new CarbonPlayerCommon(
rs.getBoolean("muted"), rs.getBoolean("muted"),
rs.getBoolean("deafened"), rs.getBoolean("deafened"),
selectedChannel == null ? null : Key.key(selectedChannel), selectedChannel == null ? null : Key.key(selectedChannel),
rs.getString("username"), Strings.trim(rs.getString("username")),
rs.getObject("id", UUID.class), rs.getObject("id", UUID.class),
displayName == null ? null : GsonComponentSerializer.gson().deserialize(displayName), displayName == null ? null : GsonComponentSerializer.gson().deserialize(displayName),
rs.getObject("lastwhispertarget", UUID.class), 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();
}
}