Skip to content
Permalink
Browse files

[misc] prepare statement boolean / byte better logging when exception

  • Loading branch information...
rusher committed Jul 26, 2019
1 parent ca1076c commit d8603ee42179ffcc6282e003d62ed86176e0856e
@@ -86,6 +86,7 @@
import org.mariadb.jdbc.internal.ColumnType;
import org.mariadb.jdbc.internal.com.send.parameters.LocalTimeParameter;
import org.mariadb.jdbc.internal.com.send.parameters.BigDecimalParameter;
import org.mariadb.jdbc.internal.com.send.parameters.BooleanParameter;
import org.mariadb.jdbc.internal.com.send.parameters.ByteArrayParameter;
import org.mariadb.jdbc.internal.com.send.parameters.ByteParameter;
import org.mariadb.jdbc.internal.com.send.parameters.DateParameter;
@@ -1478,7 +1479,7 @@ public void setBinaryStream(final int parameterIndex, final InputStream stream,
* <code>PreparedStatement</code>
*/
public void setBoolean(final int parameterIndex, final boolean value) throws SQLException {
setParameter(parameterIndex, new ByteParameter(value ? (byte) 1 : (byte) 0));
setParameter(parameterIndex, new BooleanParameter(value));
}

/**
@@ -0,0 +1,73 @@
/*
*
* MariaDB Client for Java
*
* Copyright (c) 2012-2014 Monty Program Ab.
* Copyright (c) 2015-2019 MariaDB Ab.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with this library; if not, write to Monty Program Ab info@montyprogram.com.
*
*/

package org.mariadb.jdbc.internal.com.send.parameters;

import org.mariadb.jdbc.internal.ColumnType;
import org.mariadb.jdbc.internal.io.output.PacketOutputStream;

import java.io.IOException;

public class BooleanParameter implements Cloneable, ParameterHolder {

private final boolean value;

public BooleanParameter(boolean value) {
this.value = value;
}

public void writeTo(final PacketOutputStream os) throws IOException {
os.write(value ? '1' : '0');
}

public long getApproximateTextProtocolLength() {
return 1;
}

/**
* Write data to socket in binary format.
*
* @param pos socket output stream
* @throws IOException if socket error occur
*/
public void writeBinary(final PacketOutputStream pos) throws IOException {
pos.write(value ? 1 : 0);
}

public ColumnType getColumnType() {
return ColumnType.TINYINT;
}

@Override
public String toString() {
return Boolean.toString(value);
}

public boolean isNullData() {
return false;
}

public boolean isLongData() {
return false;
}

}
@@ -58,18 +58,21 @@

public class ByteParameter implements Cloneable, ParameterHolder {

private final byte value;
private final int value;
private static final char[] hexArray = "0123456789ABCDEF".toCharArray();

public ByteParameter(byte value) {
this.value = value;
this.value = value & 0xFF;
}

public void writeTo(final PacketOutputStream os) throws IOException {
os.write(String.valueOf(value).getBytes());
os.write("0x");
os.write(hexArray[value >>> 4]);
os.write(hexArray[value & 0x0F]);
}

public long getApproximateTextProtocolLength() {
return String.valueOf(value).getBytes().length * 2;
return 4;
}

/**
@@ -88,7 +91,7 @@ public ColumnType getColumnType() {

@Override
public String toString() {
return Byte.toString(value);
return "0x" + hexArray[value >>> 4] + hexArray[value & 0x0F];
}

public boolean isNullData() {
@@ -74,6 +74,7 @@
import java.text.NumberFormat;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
@@ -107,7 +108,7 @@
private static final Set<String> tempViewList = new HashSet<>();
private static final Set<String> tempProcedureList = new HashSet<>();
private static final Set<String> tempFunctionList = new HashSet<>();
private static final NumberFormat numberFormat = DecimalFormat.getInstance();
private static final NumberFormat numberFormat = DecimalFormat.getInstance(Locale.ROOT);
protected static String connU;
protected static String connUri;
protected static String connDnsUri;
@@ -99,6 +99,23 @@ public void testBoolean() throws SQLException {
}
}

@Test
public void testBooleanSet() throws SQLException {
Statement stmt = sharedConnection.createStatement();
stmt.execute("CREATE TEMPORARY TABLE testBooleanSet (test BOOLEAN)");
try (PreparedStatement prep = sharedConnection.prepareStatement("INSERT INTO testBooleanSet VALUE (?)")) {
prep.setBoolean(1, true);
prep.execute();
prep.setBoolean(1, false);
prep.execute();
}
ResultSet rs = stmt.executeQuery("select * from testBooleanSet");
assertTrue(rs.next());
assertTrue(rs.getBoolean(1));
assertTrue(rs.next());
assertFalse(rs.getBoolean(1));
}

@Test
public void testBooleanString() throws SQLException {
Statement stmt = sharedConnection.createStatement();
@@ -1491,15 +1491,16 @@ public void sharedMemory() throws Exception {

@Test
public void preparedStatementToString() throws Exception {
try (PreparedStatement ps = sharedConnection.prepareStatement("SELECT ?,?,?,?,?,?")) {
try (PreparedStatement ps = sharedConnection.prepareStatement("SELECT ?,?,?,?,?,?,?")) {
ps.setInt(1, 1);
ps.setBigDecimal(2, new BigDecimal("1"));
ps.setString(3, "one");
ps.setBoolean(4, true);
Calendar calendar = new GregorianCalendar(1972, 3, 22);
ps.setDate(5, new Date(calendar.getTime().getTime()));
ps.setDouble(6, 1.5);
assertEquals("sql : 'SELECT ?,?,?,?,?,?', parameters : [1,1,'one',1,'1972-04-22',1.5]",
ps.setByte(7, (byte) 0xfe);
assertEquals("sql : 'SELECT ?,?,?,?,?,?,?', parameters : [1,1,'one',true,'1972-04-22',1.5,0xFE]",
ps.toString());
}
}

0 comments on commit d8603ee

Please sign in to comment.
You can’t perform that action at this time.