Skip to content

Commit

Permalink
[misc] client side prepare parameter setting avoiding null check for …
Browse files Browse the repository at this point in the history
…primitive parameter
  • Loading branch information
rusher committed Sep 7, 2022
1 parent 7b9458c commit 57a5f06
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/main/java/org/mariadb/jdbc/BasePreparedStatement.java
Expand Up @@ -294,7 +294,7 @@ public void setNull(int parameterIndex, int sqlType) throws SQLException {
@Override
public void setBoolean(int parameterIndex, boolean x) throws SQLException {
checkIndex(parameterIndex);
parameters.set(parameterIndex - 1, new Parameter<>(BooleanCodec.INSTANCE, x));
parameters.set(parameterIndex - 1, new NonNullParameter<>(BooleanCodec.INSTANCE, x));
}

/**
Expand All @@ -310,7 +310,7 @@ public void setBoolean(int parameterIndex, boolean x) throws SQLException {
@Override
public void setByte(int parameterIndex, byte x) throws SQLException {
checkIndex(parameterIndex);
parameters.set(parameterIndex - 1, new Parameter<>(ByteCodec.INSTANCE, x));
parameters.set(parameterIndex - 1, new NonNullParameter<>(ByteCodec.INSTANCE, x));
}

/**
Expand All @@ -326,7 +326,7 @@ public void setByte(int parameterIndex, byte x) throws SQLException {
@Override
public void setShort(int parameterIndex, short x) throws SQLException {
checkIndex(parameterIndex);
parameters.set(parameterIndex - 1, new Parameter<>(ShortCodec.INSTANCE, x));
parameters.set(parameterIndex - 1, new NonNullParameter<>(ShortCodec.INSTANCE, x));
}

/**
Expand All @@ -342,7 +342,7 @@ public void setShort(int parameterIndex, short x) throws SQLException {
@Override
public void setInt(int parameterIndex, int x) throws SQLException {
checkIndex(parameterIndex);
parameters.set(parameterIndex - 1, new Parameter<>(IntCodec.INSTANCE, x));
parameters.set(parameterIndex - 1, new NonNullParameter<>(IntCodec.INSTANCE, x));
}

/**
Expand All @@ -358,7 +358,7 @@ public void setInt(int parameterIndex, int x) throws SQLException {
@Override
public void setLong(int parameterIndex, long x) throws SQLException {
checkIndex(parameterIndex);
parameters.set(parameterIndex - 1, new Parameter<>(LongCodec.INSTANCE, x));
parameters.set(parameterIndex - 1, new NonNullParameter<>(LongCodec.INSTANCE, x));
}

/**
Expand All @@ -374,7 +374,7 @@ public void setLong(int parameterIndex, long x) throws SQLException {
@Override
public void setFloat(int parameterIndex, float x) throws SQLException {
checkIndex(parameterIndex);
parameters.set(parameterIndex - 1, new Parameter<>(FloatCodec.INSTANCE, x));
parameters.set(parameterIndex - 1, new NonNullParameter<>(FloatCodec.INSTANCE, x));
}

/**
Expand All @@ -390,7 +390,7 @@ public void setFloat(int parameterIndex, float x) throws SQLException {
@Override
public void setDouble(int parameterIndex, double x) throws SQLException {
checkIndex(parameterIndex);
parameters.set(parameterIndex - 1, new Parameter<>(DoubleCodec.INSTANCE, x));
parameters.set(parameterIndex - 1, new NonNullParameter<>(DoubleCodec.INSTANCE, x));
}

/**
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/org/mariadb/jdbc/codec/NonNullParameter.java
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (c) 2012-2014 Monty Program Ab
// Copyright (c) 2015-2021 MariaDB Corporation Ab

package org.mariadb.jdbc.codec;

import java.io.IOException;
import java.sql.SQLException;
import org.mariadb.jdbc.client.Context;
import org.mariadb.jdbc.client.socket.Writer;
import org.mariadb.jdbc.plugin.Codec;

public class NonNullParameter<T> extends Parameter {

public NonNullParameter(Codec<T> codec, T value) {
super(codec, value);
}

public NonNullParameter(Codec<T> codec, T value, Long length) {
super(codec, value, length);
}

@Override
public void encodeText(Writer encoder, Context context) throws IOException, SQLException {
codec.encodeText(encoder, context, this.value, null, length);
}

@Override
public boolean isNull() {
return false;
}
}

0 comments on commit 57a5f06

Please sign in to comment.