“ResultSet.getMetaData.getTableName(col)”是否正确? postgresql的jdbc驱动程序总是返回空字符串?

发布于 2025-01-04 15:24:08 字数 1787 浏览 1 评论 0 原文

当我使用 postgresql 时,我发现以下代码:

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from t");

String tableName = rs.getMetaData().getTableName(1);
System.out.println(tableName);

它打印一个空字符串。

于是我检查了源代码,发现方法org.postgresql.jdbc2.AbstractJdbc2ResultSetMetaData#g​​etTableName总是返回一个空字符串。

源代码是:

public abstract class AbstractJdbc2ResultSetMetaData implements PGResultSetMetaData {

    /*
     * @param column the first column is 1, the second is 2...
     * @return column name, or "" if not applicable
     * @exception SQLException if a database access error occurs
     */
    public String getTableName(int column) throws SQLException
    {
        return "";
    }
}

可以看到它只是返回一个""

我找到了有关此问题的讨论,请参阅:http://archives. postgresql.org/pgsql-jdbc/2009-12/msg00100.php

他们认为 “rs.getMetaData.getTableName(col)”应返回别名 查询不是基础表名。但这很难实现,所以最好留空。

他们还给出了获取表名的方法,使用:

PGResultSetMetaData.getBaseTableName() 

Sample:

ResultSet rs = stmt.executeQuery("select * from x");
// convert it to PGResultSetMetaData 
PGResultSetMetaData meta = (PGResultSetMetaData)rs.getMetaData(); 
String tableName = meta.getBaseTableName(1);

现在可以打印正确的表名了。

我不知道postgresql的实现是否正确,但是返回底层表名比空字符串有用得多,并且大多数其他数据库提供底层表名而不是空字符串。

我在 postgesql 中使用 play2 的 anorm 框架时遇到问题: Play2 的 anorm 无法在 postgresql 上工作< /a>,但这在其他数据库上运行良好。

你认为postgresql的jdbc驱动程序的正确实现是什么?返回空字符串、基础表名或其他内容?

When I use postgresql, I found following code:

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from t");

String tableName = rs.getMetaData().getTableName(1);
System.out.println(tableName);

It prints an empty string.

So I checked the source code, and found the method org.postgresql.jdbc2.AbstractJdbc2ResultSetMetaData#getTableName always returns an empty string.

The source code is:

public abstract class AbstractJdbc2ResultSetMetaData implements PGResultSetMetaData {

    /*
     * @param column the first column is 1, the second is 2...
     * @return column name, or "" if not applicable
     * @exception SQLException if a database access error occurs
     */
    public String getTableName(int column) throws SQLException
    {
        return "";
    }
}

You can see it just return a "".

I found a discussion about this, please see: http://archives.postgresql.org/pgsql-jdbc/2009-12/msg00100.php

They think
"rs.getMetaData.getTableName(col)" should return the alias name in
query not the underlying table name. But which is hard to implement, so it's better to leave it empty.

Also they gave a method to get the table name, use:

PGResultSetMetaData.getBaseTableName() 

Sample:

ResultSet rs = stmt.executeQuery("select * from x");
// convert it to PGResultSetMetaData 
PGResultSetMetaData meta = (PGResultSetMetaData)rs.getMetaData(); 
String tableName = meta.getBaseTableName(1);

Now it can print the correct table name.

I don't know the implementation of postgresql is correct, but returning the underlying table name is much more useful than an empty string, and, most of other databases provides underlying table name instead of an empty string.

I have a problem using play2's anorm framework with postgesql: Play2's anorm can't work on postgresql, but that works well on other databases.

What do you think the correct implementation of postgresql's jdbc driver? Return an empty string, underlying table name, or something else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荆棘i 2025-01-11 15:24:08

我想说,返回空字符串显然是接口的错误实现,因为表名永远不能被视为空字符串。

我认为他们正在努力解决的问题是,虽然他们当前的实现是错误的,但一旦他们选择了一个实现,他们就会坚持下去,直到他们决定打破对行为的依赖是可以接受的。因此,他们选择添加一个名称明确的方法,并提供大多数用户期望来自 getTableName 的数据,并留下明显损坏的 getTableName 方法的实现直到就它应该返回的内容达成某种共识,或者直到提交实现共识的补丁为止。

我的直觉反应是方法 getTableName 应该返回用于该表的别名。表可以与其自身连接,并且使用别名可以让您识别正在引用哪个表。查询中可能已生成表(例如取消数组嵌套),因此数据库中甚至没有表名。如果您做出决定“绝对总是,getTableName 返回别名”,那么至少用户知道会发生什么;否则,您最终会发现该方法应该返回什么并不明显。

然而,即使我假设我的直觉反应是“正确的实现”,它也会引发兼容性问题。如果 PostgreSQL 的目标之一是普及,那么最好能够以尽可能少的投资从另一个 DBMS 切换到 PostgreSQL。因此,诸如“其他 JDBC 如何实现 java.sql 接口?”之类的问题变得相关。正如您所说,存在一个框架,它对如何实现 ResultSetMetaData 有期望,而且它可能不是唯一一个对 java.sql 接口如何实现有一定期望的框架。予以实施。

无论他们最终选择哪种实现方式都将是一种权衡,所以我可以理解为什么“把罐子扔到一边”是他们的选择。一旦他们选择了想要做出的权衡,他们就被锁定了。

编辑:我建议抛出有关未实现的异常比只是默默地失败要好。我希望依赖 getTableName 特定实现的框架对空字符串没有太多用处,并且要么出现错误,要么它们本身会默默失败。

I would say that returning an empty string is obviously an incorrect implementation of the interface, since the table name could never be considered to be an empty string.

The problem that I believe they are struggling with is that while their current implementation is wrong, once they choose an implementation they will be stuck with it until they decide that breaking dependencies on the behaviour is acceptable. Therefore, they choose to add a method whose name is unambiguous and provide the data that most users were expecting to come from getTableName, and leave an obviously broken implementation of the getTableName method until some consensus is reached on what it should return or until a patch is submitted that implements the consensus.

My gut reaction is that the method getTableName should return the alias being used for that table. A table could be joined with itself, and using the alias would allow you to identify which was being referenced. A table might have been generated in the query (such as unnesting an array), and therefore not even have a table name in the database. If you make the decision “absolutely always, getTableName returns the alias”, then at least users know what to expect; otherwise, you end up with it not being obvious what the method should return.

However, even if I assume that my gut reaction is “the correct implementation”, it raises the issue of compatibility. It is desirable that it be possible to switch from another DBMS to PostgreSQL with as little investment as possible, if one of PostgreSQL’s goals is to grow in popularity. Therefore, things like “how do other JDBCs implement the java.sql interfaces?” become relevant. As you say, a framework exists that has expectations of how the ResultSetMetaData should be implemented, and it is likely not the only one with certain expectations of how java.sql interfaces will be implemented.

Whichever implementation they end up choosing is going to be a tradeoff, so I can see why “kick the can down the road” is their choice. Once they choose the tradeoff they want to make, they are locked in.

EDIT: I would suggest that throwing an exception regarding not implemented would be better than just silently failing. I expect that frameworks that rely on a specific implementation of getTableName will not have much use for empty string anyway, and either error or themselves fail silently.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文