-
Notifications
You must be signed in to change notification settings - Fork 619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Long type value convert bug #1177
Comments
Can you explain why this is a bug here and not in what MyBatis does ? I am not sure to understand. |
that's most likely the problem. This would be fine with any other SQL database, but SQLite can store anything in a column. One row could be text, the other int, and another one blob, all in the same column. |
so does it have a solution? |
Hello, I understand that there are some cases that are difficult to determine the right class name for a column, but there seems to be a room for improvement when the column is defined as Can we, for example, look up the column type name first? diff --git a/src/main/java/org/sqlite/jdbc3/JDBC3ResultSet.java b/src/main/java/org/sqlite/jdbc3/JDBC3ResultSet.java
index 7647112..c51b3c8 100644
--- a/src/main/java/org/sqlite/jdbc3/JDBC3ResultSet.java
+++ b/src/main/java/org/sqlite/jdbc3/JDBC3ResultSet.java
@@ -580,8 +580,11 @@ public abstract class JDBC3ResultSet extends CoreResultSet {
public String getColumnClassName(int col) throws SQLException {
switch (safeGetColumnType(markCol(col))) {
case SQLITE_INTEGER:
- long val = getLong(col);
- if (val > Integer.MAX_VALUE || val < Integer.MIN_VALUE) {
+ String typeName = getColumnTypeName(col);
+ if ("BIGINT".equals(typeName)
+ || "INT8".equals(typeName)
+ || "UNSIGNED BIG INT".equals(typeName)
+ || isLong(getLong(col))) {
return "java.lang.Long";
} else {
return "java.lang.Integer";
@@ -985,4 +988,8 @@ public abstract class JDBC3ResultSet extends CoreResultSet {
private String safeGetColumnName(int col) throws SQLException {
return stmt.pointer.safeRun((db, ptr) -> db.column_name(ptr, checkCol(col)));
}
+
+ private boolean isLong(long val) {
+ return val > Integer.MAX_VALUE || val < Integer.MIN_VALUE;
+ }
} And here is a new test I added to @Test
void gh1177_getColumnClassName_BIGINT() throws SQLException {
stat.executeUpdate("create table gh1177 (c1 BIGINT)");
stat.executeUpdate("insert into gh1177 values (850361281191579648)");
{
ResultSet rs = stat.executeQuery("select c1 from gh1177 order by c1");
ResultSetMetaData meta = rs.getMetaData();
assertThat(meta.getColumnClassName(1)).isEqualTo(Long.class.getName());
}
stat.executeUpdate("insert into gh1177 values (22)");
{
ResultSet rs = stat.executeQuery("select c1 from gh1177 order by c1");
ResultSetMetaData meta = rs.getMetaData();
assertThat(meta.getColumnClassName(1)).isEqualTo(Long.class.getName());
}
} It still returns If it's worth a review, I would be happy to submit a PR. p.s. |
there's 2 different methods from what i have seen in JDBC, one on the table level, which looks at the optional type hints, and return a SQL type, and the one you are referring to which looks at the exact result set ROW metadata. For instance in your case, if you were to move to the next row of the result set, it would return Long for the column. To me this is an issue with the dynamic nature of SQLite and the fact that MyBatis caches the type of the first row and reuses that on all subsequent rows.
it actually does |
Hello @gotson , Thank you for the comment! @sailingsky , |
a good read about it is https://sqlite.org/datatype3.html
no, SQLite is probably the only DB that has this |
Closing this, since it is not a bug but expected behaviour. |
Describe the bug
Long type value convert bug
To Reproduce
use sqlite create a table(eg:test),it has two columns(id INTEGER, name TEXT), insert two records, like
then, use mybaits write a query interface and a mapper xml config:
when you run this query ,it will return wrong id value. the second row's id value converted to Integer type be overflowed.
I tracked mybatis source code and the sqlite-jdbc source code, found root cause at JDBC3ResultSet.java#getColumnClassName(int col).
Expected behavior
expect return correct Long value.
Environment (please complete the following information):
hope this can help you.
The text was updated successfully, but these errors were encountered: