Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ-352] metadata correction on getPrecision() for numeric fields
  • Loading branch information
rusher committed Sep 21, 2016
1 parent 9b6c660 commit 26ed0fd
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
Expand Up @@ -226,7 +226,7 @@ public String getCatalogName(int column) throws SQLException {
* @throws SQLException if a database access error occurs
*/
public int getPrecision(final int column) throws SQLException {
return (int) getColumnInformation(column).getLength();
return (int) getColumnInformation(column).getPrecision();
}

/**
Expand Down
Expand Up @@ -101,6 +101,7 @@ public class ColumnInformation {
Buffer buffer;
private short charsetNumber;
private long length;
private long fixlength;
private MariaDbType type;
private byte decimals;
private short flags;
Expand Down Expand Up @@ -138,7 +139,7 @@ public ColumnInformation(Buffer buffer) {
buffer.skipLengthEncodedBytes(); /* original table */
buffer.skipLengthEncodedBytes(); /* name */
buffer.skipLengthEncodedBytes(); /* org_name */
buffer.skipBytes(1);
fixlength = buffer.readByte();
charsetNumber = buffer.readShort();
length = buffer.readInt();
type = MariaDbType.fromServer(buffer.readByte() & 0xff);
Expand Down Expand Up @@ -246,6 +247,24 @@ public long getLength() {
return length;
}

public long getPrecision() {
switch (type) {
case OLDDECIMAL:
case DECIMAL:
//DECIMAL and OLDDECIMAL are "exact" fixed-point number.
//so :
// - if can be signed, 1 byte is saved for sign
// - if decimal > 0, one byte more for dot
if (isSigned()) {
return length - ((decimals > 0) ? 2 : 1);
} else {
return length - ((decimals > 0) ? 1 : 0);
}
default:
return length;
}
}

/**
* Get column size.
* @return size
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/org/mariadb/jdbc/DatabaseMetadataTest.java
Expand Up @@ -817,4 +817,36 @@ public void conj72() throws Exception {
connection.close();
}
}

@Test
public void getPrecision() throws SQLException {
createTable("getPrecision", "num1 NUMERIC(9,4), "
+ "num2 NUMERIC (9,0),"
+ "num3 NUMERIC (9,4) UNSIGNED,"
+ "num4 NUMERIC (9,0) UNSIGNED,"
+ "num5 FLOAT(9,4),"
+ "num6 FLOAT(9,4) UNSIGNED,"
+ "num7 DOUBLE(9,4),"
+ "num8 DOUBLE(9,4) UNSIGNED"
);
Statement stmt = sharedConnection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM getPrecision");
ResultSetMetaData rsmd = rs.getMetaData();
assertEquals(9, rsmd.getPrecision(1));
assertEquals(4, rsmd.getScale(1));
assertEquals(9, rsmd.getPrecision(2));
assertEquals(0, rsmd.getScale(2));
assertEquals(9, rsmd.getPrecision(3));
assertEquals(4, rsmd.getScale(3));
assertEquals(9, rsmd.getPrecision(4));
assertEquals(0, rsmd.getScale(4));
assertEquals(9, rsmd.getPrecision(5));
assertEquals(4, rsmd.getScale(5));
assertEquals(9, rsmd.getPrecision(6));
assertEquals(4, rsmd.getScale(6));
assertEquals(9, rsmd.getPrecision(7));
assertEquals(4, rsmd.getScale(7));
assertEquals(9, rsmd.getPrecision(8));
assertEquals(4, rsmd.getScale(8));
}
}

0 comments on commit 26ed0fd

Please sign in to comment.