这不是您要寻找的答案,但我会为那些好奇的人添加它。将来我会研究如何制作它自己的插件。也许吧。
对于这个练习,让我们修改 Workbench 的“全选语句”行为。右键单击表并选择“复制到剪贴板”或“发送到 SQL 编辑器”后,您会在上下文菜单中看到“选择所有语句”。
在我的系统 (OS X) 上,这是在这里定义的:
/Applications/MySQLWorkbench.app/Contents/Resources/plugins/sqlide_schematree_ext.py
具体来说,这段代码:
(_("Select All Statement"), 'select_all_statement', ['db.Table', 'db.View'], len(selection) > 0, False),
(_("Select All Statement"), 'select_all_statement', ['columns'], len(selection) == 1 and selection[0].type == 'columns', False),
引用此代码:
def select_all_statement(self):
# assumes only table nodes (or the Columns node of a table)
parts = []
for obj in self.selection:
if obj.type == 'columns':
obj = obj.owner
parts.append("SELECT %s\nFROM %s.%s;\n" % (",\n ".join("%s.%s" % (esc_ident(obj.name), esc_ident(c[0])) for c in self.get_table_columns(obj.schemaName, obj.name)), esc_ident(obj.schemaName), esc_ident(obj.name)))
self.send("\n".join(parts))
好吧,我还不确定如何让它变得智能(例如,找出 PK,或在上下文菜单中提供列建议),但现在让我们添加愚蠢的代码(即 ORDER BY id)。您可以将 parts.append 行修改为:
parts.append("SELECT %s\nORDER BY id\nFROM %s.%s;\n" % (",\n ".join("%s.%s" % (esc_ident(obj.name), esc_ident(c[0])) for c in self.get_table_columns(obj.schemaName, obj.name)), esc_ident(obj.schemaName), esc_ident(obj.name)))
就像我说的,它不是智能的。希望有一天我(或某人)将其更新为更智能。也就是说,重新启动 Workbench 将使上述更改显示在 Workbench 中,因为 Workbench 会在启动时重新编译插件(并且 Workbench 中的大多数内容都是插件)。随意将“Select All Statement”更改为“Select All Statement and Order by id”或其他任何内容。或者,添加新条目和新函数,例如 select_all_and_order_statement。快乐黑客! :)