Skip to content

Commit

Permalink
[CONJ-950] correct ResultSetMetaData.getPrecision() and getColumnType…
Browse files Browse the repository at this point in the history
…() for TINYTEXT/TEXT/MEDIUMTEXT/LONGTEXT

precision correction :
* was returning the byte length, not character length.
* when unknown/too long will return 0, not -1

column type :
* the columnType value for LONGBLOB/LONGTEXT will return correct Type value. (example LONGTEXT data will have Types.LONGVARCHAR, not Types.VARCHAR).
  • Loading branch information
rusher committed May 16, 2022
1 parent 15d8321 commit 2ba083c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/mariadb/jdbc/client/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public interface Column {
*
* @return precision
*/
long getPrecision();
int getPrecision();

/**
* return column type from column server type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,19 @@ public boolean isSigned() {
}

public int getDisplaySize() {
if (dataType == DataType.VARCHAR
|| dataType == DataType.JSON
|| dataType == DataType.ENUM
|| dataType == DataType.SET
|| dataType == DataType.VARSTRING
|| dataType == DataType.STRING) {
if (!isBinary()
&& (dataType == DataType.VARCHAR
|| dataType == DataType.JSON
|| dataType == DataType.ENUM
|| dataType == DataType.SET
|| dataType == DataType.VARSTRING
|| dataType == DataType.STRING
|| dataType == DataType.BLOB
|| dataType == DataType.TINYBLOB
|| dataType == DataType.MEDIUMBLOB
|| dataType == DataType.LONGBLOB)) {
Integer maxWidth = CharsetEncodingLength.maxCharlen.get(charset);
if (maxWidth == null) {
return (int) length;
}
return (int) length / maxWidth;
if (maxWidth != null) return (int) (length / maxWidth);
}
return (int) length;
}
Expand Down Expand Up @@ -250,7 +252,7 @@ public String getExtTypeName() {
*
* @return precision
*/
public long getPrecision() {
public int getPrecision() {
switch (dataType) {
case OLDDECIMAL:
case DECIMAL:
Expand All @@ -259,9 +261,9 @@ public long getPrecision() {
// - if is signed, 1 byte is saved for sign
// - if decimal > 0, one byte more for dot
if (isSigned()) {
return length - ((decimals > 0) ? 2 : 1);
return (int) (length - ((decimals > 0) ? 2 : 1));
} else {
return length - ((decimals > 0) ? 1 : 0);
return (int) (length - ((decimals > 0) ? 1 : 0));
}
case VARCHAR:
case JSON:
Expand All @@ -271,12 +273,22 @@ public long getPrecision() {
case STRING:
Integer maxWidth = CharsetEncodingLength.maxCharlen.get(charset);
if (maxWidth == null) {
return length;
return (int) length;
}
return (int) (length / maxWidth);

case BLOB:
case TINYBLOB:
case MEDIUMBLOB:
case LONGBLOB:
if (!isBinary()) {
Integer maxWidth2 = CharsetEncodingLength.maxCharlen.get(charset);
if (maxWidth2 != null) return (int) (length / maxWidth2);
}
return length / maxWidth;
return (int) length;

default:
return length;
return (int) length;
}
}

Expand Down Expand Up @@ -394,14 +406,18 @@ public int getColumnType(Configuration conf) {
if (conf.yearIsDateType()) return Types.DATE;
return Types.SMALLINT;
case JSON:
return Types.VARCHAR;
return Types.LONGVARCHAR;
case VARCHAR:
case ENUM:
case SET:
case VARSTRING:
case TINYBLOB:
case BLOB:
return isBinary() ? Types.VARBINARY : Types.VARCHAR;
if (length <= 0 || getDisplaySize() > 16777215) {
return isBinary() ? Types.LONGVARBINARY : Types.LONGVARCHAR;
} else {
return isBinary() ? Types.VARBINARY : Types.VARCHAR;
}
case GEOMETRY:
return Types.VARBINARY;
case STRING:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static void drop() throws SQLException {
stmt.execute("drop table if exists cross2");
stmt.execute("drop table if exists cross1");
stmt.execute("drop table if exists get_index_info");
stmt.execute("drop table if exists text_types_text");
}

@BeforeAll
Expand Down Expand Up @@ -125,6 +126,13 @@ public static void initClass() throws SQLException {
+ " PRIMARY KEY(no),\n"
+ " INDEX ind_prod (product_category, product_id),\n"
+ " INDEX ind_cust (customer_id))");
stmt.execute(
"create table text_types_text (varchar100 varchar(100),\n"
+ " varchar255 varchar(255),\n"
+ " text text,\n"
+ " `tinytext` tinytext,\n"
+ " `mediumtext` mediumtext,\n"
+ " `longtext` longtext)");
}

private static void checkType(String name, int actualType, String colName, int expectedType) {
Expand Down Expand Up @@ -2002,10 +2010,45 @@ public void getMetaData() throws SQLException {
ResultSetMetaData meta = rs.getMetaData();
assertTrue(
"LONGTEXT".equals(meta.getColumnTypeName(1)) || "JSON".equals(meta.getColumnTypeName(1)));
assertEquals(Types.VARCHAR, meta.getColumnType(1));
assertEquals(Types.LONGVARCHAR, meta.getColumnType(1));
assertEquals("java.lang.String", meta.getColumnClassName(1));
}

@Test
public void getTypeMetaData() throws SQLException {
// "create table text_types_text (varchar100 varchar(100),\n" +
// " varchar255 varchar(255),\n" +
// " text text,\n" +
// " `tinytext` tinytext,\n" +
// " `mediumtext` mediumtext,\n" +
// " `longtext` longtext)"
try (ResultSet resultSet =
sharedConn.createStatement().executeQuery("select * from text_types_text")) {
ResultSetMetaData metaData = resultSet.getMetaData();

String[] expected =
new String[] {
"varchar100 12 VARCHAR 100",
"varchar255 12 VARCHAR 255",
"text 12 TEXT 65535",
"tinytext 12 VARCHAR 255",
"mediumtext 12 MEDIUMTEXT 16777215",
"longtext -1 LONGTEXT 0"
};
for (int i = 0; i < expected.length; i++) {
assertEquals(
expected[i],
metaData.getColumnName(i + 1)
+ " "
+ metaData.getColumnType(i + 1)
+ " "
+ metaData.getColumnTypeName(i + 1)
+ " "
+ metaData.getPrecision(i + 1));
}
}
}

@Test
public void foreignKeyTest() throws SQLException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -735,10 +735,12 @@ public void getMetaData() throws SQLException {
assertEquals("t1", meta.getColumnName(1));
assertEquals(Types.VARCHAR, meta.getColumnType(1));
assertEquals(4, meta.getColumnCount());
assertEquals(1020, meta.getPrecision(1));
assertEquals(0, meta.getScale(1));
assertEquals("", meta.getSchemaName(1));
assertEquals(1020, meta.getColumnDisplaySize(1));
if (!isXpand()) {
assertEquals(255, meta.getColumnDisplaySize(1));
assertEquals(255, meta.getPrecision(1));
}
}

@Test
Expand Down

0 comments on commit 2ba083c

Please sign in to comment.