[英]What's the most elegant way to retrieve a MySQL table comment?
I'm trying to programmatically retrieve the comment for a MySQL table. 我正在尝试以编程方式检索MySQL表的注释。 The first method suggested to me was: 向我建议的第一种方法是:
$shown = $db->query('show create table ' . TABLE_NAME)->fetch_row();
preg_match("/COMMENT='(.*)'/", $shown[0], $m);
$comment = $m[1];
But that kind of workaround makes me cringe. 但这种解决方法让我感到畏缩。 I stumbled upon another way: 我偶然发现了另一种方式:
$result = $db->query("select table_comment from information_schema.tables where table_schema = '" .
DATABASE_NAME . "' and table_name = '" TABLE_NAME '\'')->fetch_row();
$comment = $result[0];
It's a little better (no string parsing), but it still makes me uncomfortable because I'm digging into internal structures where I don't feel like I belong. 它有点好(没有字符串解析),但它仍然让我感到不舒服,因为我正在挖掘内部结构,我觉得我不属于自己。
Is there a nice, simple way to get at the table comment in code? 有没有一个很好的,简单的方法来获取代码中的表注释?
Information schema isn't really an internal structure where you don't belong. 信息模式实际上不是您不属于的内部结构。 It's part of the ANSI SQL standard and its purpose is to give you a legitimate way to query metadata. 它是ANSI SQL标准的一部分,其目的是为您提供查询元数据的合法方式。
I would feel no hesitation to use it. 我会毫不犹豫地使用它。
The one disadvantage is that MySQL's implementation of information schema tends to have pretty poor performance . 一个缺点是MySQL的信息架构实现往往具有相当差的性能 。 So be careful about running queries against IS in routines that should be quick. 所以要小心在应该快速的例程中对IS运行查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.