From c19f608838fdcb18fa81cab8a607afdaa5131a58 Mon Sep 17 00:00:00 2001 From: rusher Date: Thu, 25 Apr 2024 14:33:32 +0200 Subject: [PATCH] [misc] code simplification --- src/benchmark/java/org/mariadb/jdbc/Common.java | 1 + src/benchmark/java/org/mariadb/jdbc/Insert_batch.java | 2 +- src/main/java/org/mariadb/jdbc/Configuration.java | 7 +++---- .../java/org/mariadb/jdbc/client/column/BlobColumn.java | 1 + .../java/org/mariadb/jdbc/client/column/JsonColumn.java | 1 + .../mariadb/jdbc/client/column/SignedSmallIntColumn.java | 1 + .../mariadb/jdbc/client/column/SignedTinyIntColumn.java | 1 + .../org/mariadb/jdbc/client/column/TimestampColumn.java | 8 ++------ .../jdbc/client/column/UnsignedMediumIntColumn.java | 1 - .../java/org/mariadb/jdbc/client/impl/StandardClient.java | 3 ++- src/main/java/org/mariadb/jdbc/client/result/Result.java | 4 ++-- .../jdbc/message/server/ColumnDefinitionPacket.java | 1 - 12 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/benchmark/java/org/mariadb/jdbc/Common.java b/src/benchmark/java/org/mariadb/jdbc/Common.java index eb3e87071..2a7a7c9fe 100644 --- a/src/benchmark/java/org/mariadb/jdbc/Common.java +++ b/src/benchmark/java/org/mariadb/jdbc/Common.java @@ -180,6 +180,7 @@ public static class SetupData { try { stmt.execute("INSTALL SONAME 'ha_blackhole'"); } catch (SQLException e) { + // eat } String createTable = diff --git a/src/benchmark/java/org/mariadb/jdbc/Insert_batch.java b/src/benchmark/java/org/mariadb/jdbc/Insert_batch.java index e355bd158..2e56365f1 100644 --- a/src/benchmark/java/org/mariadb/jdbc/Insert_batch.java +++ b/src/benchmark/java/org/mariadb/jdbc/Insert_batch.java @@ -25,7 +25,7 @@ public class Insert_batch extends Common { public static String randomString(int length) { StringBuilder result = new StringBuilder(); for (int i = length; i > 0; --i) - result.append(chars.get(Math.round((int) Math.random() * (chars.size() - 1)))); + result.append(chars.get((int) ((Math.random() * (chars.size() - 1))))); return result.toString(); } diff --git a/src/main/java/org/mariadb/jdbc/Configuration.java b/src/main/java/org/mariadb/jdbc/Configuration.java index e212310ba..030f278a5 100644 --- a/src/main/java/org/mariadb/jdbc/Configuration.java +++ b/src/main/java/org/mariadb/jdbc/Configuration.java @@ -783,7 +783,7 @@ private static Configuration parseInternal(String url, Properties properties) if (database.isEmpty()) database = null; } String urlParameters = additionalParameters.substring(optIndex + 1); - if (urlParameters != null && !urlParameters.isEmpty()) { + if (!urlParameters.isEmpty()) { String[] parameters = urlParameters.split("&"); for (String parameter : parameters) { int pos = parameter.indexOf('='); @@ -975,11 +975,10 @@ public static String toConf(String url) throws SQLException { if (!propertyToSkip.contains(field.getName())) { Object fieldValue = field.get(conf); if (fieldValue == null) { - (Objects.equals(fieldValue, field.get(defaultConf)) ? sbDefaultOpts : sbDifferentOpts) + (field.get(defaultConf) == null ? sbDefaultOpts : sbDifferentOpts) .append("\n * ") .append(field.getName()) - .append(" : ") - .append(fieldValue); + .append(" : null"); } else { if (field.getName().equals("haMode")) { (Objects.equals(fieldValue, field.get(defaultConf)) ? sbDefaultOpts : sbDifferentOpts) diff --git a/src/main/java/org/mariadb/jdbc/client/column/BlobColumn.java b/src/main/java/org/mariadb/jdbc/client/column/BlobColumn.java index 68c6f99a6..1f39e2135 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/BlobColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/BlobColumn.java @@ -48,6 +48,7 @@ public BlobColumn( protected BlobColumn(BlobColumn prev) { super(prev); } + public int getDisplaySize() { if (charset != 63) { Integer maxWidth = CharsetEncodingLength.maxCharlen.get(charset); diff --git a/src/main/java/org/mariadb/jdbc/client/column/JsonColumn.java b/src/main/java/org/mariadb/jdbc/client/column/JsonColumn.java index bb590bf72..8220f33ee 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/JsonColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/JsonColumn.java @@ -42,6 +42,7 @@ public JsonColumn( protected JsonColumn(JsonColumn prev) { super(prev); } + public int getDisplaySize() { if (charset != 63) { Integer maxWidth = CharsetEncodingLength.maxCharlen.get(charset); diff --git a/src/main/java/org/mariadb/jdbc/client/column/SignedSmallIntColumn.java b/src/main/java/org/mariadb/jdbc/client/column/SignedSmallIntColumn.java index ecfc62aa0..9579c1d7a 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/SignedSmallIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/SignedSmallIntColumn.java @@ -55,6 +55,7 @@ public SignedSmallIntColumn( protected SignedSmallIntColumn(SignedSmallIntColumn prev) { super(prev, true); } + public int getPrecision() { // UNSIGNED SMALLINT : 0..65535 digits=5 nchars=5 // SIGNED SMALLINT : -32768..32767 digits=5 nchars=6 diff --git a/src/main/java/org/mariadb/jdbc/client/column/SignedTinyIntColumn.java b/src/main/java/org/mariadb/jdbc/client/column/SignedTinyIntColumn.java index e7371e846..3bd86d422 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/SignedTinyIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/SignedTinyIntColumn.java @@ -55,6 +55,7 @@ public SignedTinyIntColumn( protected SignedTinyIntColumn(SignedTinyIntColumn prev) { super(prev, true); } + public int getPrecision() { // UNSIGNED TINYINT : 0..255 digits=3 nchars=3 // SIGNED TINYINT : -128..127 digits=3 nchars=4 diff --git a/src/main/java/org/mariadb/jdbc/client/column/TimestampColumn.java b/src/main/java/org/mariadb/jdbc/client/column/TimestampColumn.java index d563e4b1c..f4d0c35f8 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/TimestampColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/TimestampColumn.java @@ -403,12 +403,8 @@ public static ZonedDateTime localDateTimeToZoneDateTime( final LocalDateTime ldt, final Calendar calParam, final Context context) { if (calParam == null) { if (context.getConf().preserveInstants()) { - ZonedDateTime zdt = ldt.atZone(context.getConnectionTimeZone().toZoneId()); - ZonedDateTime zdt2 = - zdt.withZoneSameInstant( - (calParam == null ? TimeZone.getDefault() : calParam.getTimeZone()).toZoneId()); - LocalDateTime sss = zdt2.toLocalDateTime(); - return zdt2; + return ldt.atZone(context.getConnectionTimeZone().toZoneId()) + .withZoneSameInstant(TimeZone.getDefault().toZoneId()); } return ldt.atZone(TimeZone.getDefault().toZoneId()); } diff --git a/src/main/java/org/mariadb/jdbc/client/column/UnsignedMediumIntColumn.java b/src/main/java/org/mariadb/jdbc/client/column/UnsignedMediumIntColumn.java index b368ea749..dfd540656 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/UnsignedMediumIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/UnsignedMediumIntColumn.java @@ -12,7 +12,6 @@ import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; -import org.mariadb.jdbc.util.CharsetEncodingLength; /** Column metadata definition */ public class UnsignedMediumIntColumn extends ColumnDefinitionPacket implements ColumnDecoder { diff --git a/src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java b/src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java index a2f29253b..bf4f03b36 100644 --- a/src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java +++ b/src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java @@ -768,7 +768,8 @@ public String createSessionVariableQuery(Context context) { } // force client timezone to connection to ensure result of now(), ... - if (conf.forceConnectionTimeZoneToSession() == null || conf.forceConnectionTimeZoneToSession()) { + if (conf.forceConnectionTimeZoneToSession() == null + || conf.forceConnectionTimeZoneToSession()) { TimeZone connectionTz = context.getConnectionTimeZone(); ZoneId connectionZoneId = connectionTz.toZoneId(); diff --git a/src/main/java/org/mariadb/jdbc/client/result/Result.java b/src/main/java/org/mariadb/jdbc/client/result/Result.java index c386c6ca1..eaadcdfdf 100644 --- a/src/main/java/org/mariadb/jdbc/client/result/Result.java +++ b/src/main/java/org/mariadb/jdbc/client/result/Result.java @@ -1654,7 +1654,7 @@ public T getObject(int columnIndex, Class type) throws SQLException { fieldLength.set( rowDecoder.setPosition( columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); - Calendar calendar = null; + if (wasNull()) { if (type.isPrimitive()) { throw new SQLException( @@ -1672,7 +1672,7 @@ public T getObject(int columnIndex, Class type) throws SQLException { for (Codec codec : conf.codecs()) { if (codec.canDecode(column, type)) { return rowDecoder.decode( - (Codec) codec, calendar, rowBuf, fieldLength, metadataList, fieldIndex, context); + (Codec) codec, null, rowBuf, fieldLength, metadataList, fieldIndex, context); } } rowBuf.skip(fieldLength.get()); diff --git a/src/main/java/org/mariadb/jdbc/message/server/ColumnDefinitionPacket.java b/src/main/java/org/mariadb/jdbc/message/server/ColumnDefinitionPacket.java index 31c5bbd33..79e7e8fa9 100644 --- a/src/main/java/org/mariadb/jdbc/message/server/ColumnDefinitionPacket.java +++ b/src/main/java/org/mariadb/jdbc/message/server/ColumnDefinitionPacket.java @@ -8,7 +8,6 @@ import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.message.ServerMessage; -import org.mariadb.jdbc.util.CharsetEncodingLength; import org.mariadb.jdbc.util.constants.ColumnFlags; /** Column metadata definition */