Skip to content

Commit 8cc16d4

Browse files
committed
[CONJ-850] MariaDbResultSetMetaData#getPrecision(int) returns wrong length for character data
1 parent a4f85ce commit 8cc16d4

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/main/java/org/mariadb/jdbc/MariaDbParameterMetaData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public boolean isSigned(int param) throws SQLException {
108108

109109
@Override
110110
public int getPrecision(int param) throws SQLException {
111-
long length = getParameterInformation(param).getLength();
111+
long length = getParameterInformation(param).getPrecision();
112112
return (length > Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int) length;
113113
}
114114

src/main/java/org/mariadb/jdbc/internal/com/read/resultset/ColumnDefinition.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,18 @@ public long getPrecision() {
273273
} else {
274274
return length - ((decimals > 0) ? 1 : 0);
275275
}
276+
case VARCHAR:
277+
case JSON:
278+
case ENUM:
279+
case SET:
280+
case VARSTRING:
281+
case STRING:
282+
int maxWidth = maxCharlen[charsetNumber & 0xff];
283+
if (maxWidth == 0) {
284+
return length;
285+
}
286+
return length / maxWidth;
287+
276288
default:
277289
return length;
278290
}

src/test/java/org/mariadb/jdbc/DatabaseMetadataTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ public static void initClass() throws SQLException {
140140
+ "t4 timestamp(6) DEFAULT '2000-01-01 00:00:00',"
141141
+ "t5 time(0),"
142142
+ "t6 time(6))");
143+
stmt.execute(
144+
"CREATE TABLE getStringPrecision("
145+
+ "col1 CHAR(100) CHARSET utf8mb4, "
146+
+ "col2 CHAR(100) CHARSET utf8,"
147+
+ "col3 CHAR(100) CHARSET latin2)");
143148
stmt.execute(
144149
"CREATE TABLE getTimePrecision2("
145150
+ "d date, "
@@ -203,6 +208,7 @@ public static void afterClass() throws SQLException {
203208
stmt.execute("drop table if exists fore_key1");
204209
stmt.execute("drop table if exists prim_key");
205210
stmt.execute("drop table if exists table_type_test");
211+
stmt.execute("drop table if exists getStringPrecision");
206212
}
207213
}
208214

@@ -1448,6 +1454,24 @@ public void getTimePrecision() throws SQLException {
14481454
assertEquals(6, rsmd.getScale(7));
14491455
}
14501456

1457+
1458+
@Test
1459+
public void getStringPrecision() throws SQLException {
1460+
Assume.assumeTrue(doPrecisionTest);
1461+
Statement stmt = sharedConnection.createStatement();
1462+
ResultSet rs = stmt.executeQuery("SELECT * FROM getStringPrecision");
1463+
ResultSetMetaData rsmd = rs.getMetaData();
1464+
// CHAR(100) CHARSET=utf8mb4
1465+
assertEquals(100, rsmd.getPrecision(1));
1466+
assertEquals(0, rsmd.getScale(1));
1467+
// CHAR(100) CHARSET=utf8
1468+
assertEquals(100, rsmd.getPrecision(2));
1469+
assertEquals(0, rsmd.getScale(2));
1470+
// CHAR(100) CHARSET=latin2
1471+
assertEquals(100, rsmd.getPrecision(3));
1472+
assertEquals(0, rsmd.getScale(3));
1473+
}
1474+
14511475
@Test
14521476
public void metaTimeResultSet() throws SQLException {
14531477
Assume.assumeTrue(doPrecisionTest);

0 commit comments

Comments
 (0)