{"id":15,"date":"2010-11-11T15:23:22","date_gmt":"2010-11-11T07:23:22","guid":{"rendered":"http:\/\/blog.nami.idv.tw\/?p=15"},"modified":"2016-04-19T15:40:54","modified_gmt":"2016-04-19T07:40:54","slug":"wb-plugin-dump-column-comments","status":"publish","type":"post","link":"https:\/\/blog.nami.idv.tw\/?p=15","title":{"rendered":"WB Plugin : Dump column comments"},"content":{"rendered":"<p>\u4e4b\u524d\u5beb\u7684\u7528\u4f86 Dump \u6b04\u4f4d\u8aaa\u660e\u6b04\u5167\u5bb9\u7684\uff0c\u4e3b\u8981\u662f\u53c3\u7167 <a href=\"http:\/\/datadictionary.codeplex.com\/\" target=\"_blank\">Data Dictionary Creator<\/a> \u7684\u529f\u80fd\uff0c\u5728 MySQL Workbench \u4e2d\u5be6\u73fe\u3002(\u7576\u7136\u6c92\u50cf Data Dictionary Creator \u529f\u80fd\u90a3\u6a23\u591a\u6a23\u5c31\u662f\u4e86)<\/p>\n<pre lang=\"Python\">#\r\n#  wb_utils_dark_grt.py\r\n#  MySQLWorkbench\r\n#\r\n#\u00a0 Created by Dragon Chuang on 2010\/09\/04.\r\n#\u00a0 Creative Common License.\r\n#\r\n\r\n# import the wb module\r\nfrom wb import *\r\n# import the grt module\r\nimport grt\r\n\r\n# define this Python module as a GRT module\r\nModuleInfo = DefineModule(name= \"PyWbUtilsDark\", author= \"Dragon Chuang\", version= \"1.0\")\r\n\r\n# this is just a function used by the plugin, it's not exported\r\ndef printTableLine(fields, filler= \" \"):\r\n  print \"|\",\r\n  for text, size in fields:\r\n    # DBCS\u5728\u4f4d\u7f6e\u5c0d\u9f4a\u6642\u5b57\u6578\u554f\u984c\u7684\u4fee\u6b63 (WB \u4e00\u500b\u4e2d\u6587 len() 3, unicode \u4e00\u500b\u4e2d\u6587 len() 1, big5 \u4e00\u500b\u4e2d\u6587 len() 2)\r\n    dbcssizefix= 0\r\n    dbcssizefix= len(text) - len(text.decode(\"utf-8\").encode(\"big5\"))\r\n    #print len(text), len(text.decode(\"utf-8\").encode(\"big5\"))\r\n\t# \u5c07\u63db\u884c\u53d6\u4ee3\u6210\u7a7a\u767d\r\n    print text.replace(\"\\n\",\" \").ljust(size + dbcssizefix, filler), \"|\",\r\n  print\r\n\r\n# @wbexport makes this function be exported by the module and also describes the return and\r\n# argument types of the function\r\n# @wbplugin defines the name of the plugin to \"wb.catalog.util.dumpColumns\", sets the caption to be\r\n# shown in places like the menu, where to take input arguments from and also that it should be included\r\n# in the Catalog submenu in Plugins.\r\n@ModuleInfo.plugin(\"wb.catalog.util.dumpColumnComments\", caption= \"Dump All Table Column Comments\", input= [wbinputs.currentCatalog()], pluginMenu= \"Catalog\")\r\n@ModuleInfo.export(grt.INT, grt.classes.db_Catalog)\r\n\r\ndef printAllColumns(catalog):\r\n  lines= []\r\n  schemalen= 10\r\n  tablelen= 10\r\n  columnlen= 10\r\n  typelen= 10\r\n  commentbytes= 0\r\n  commentlen= 10\r\n\r\n  for schema in catalog.schemata:\r\n    schemalen= max(schemalen, len(schema.name))\r\n    for table in schema.tables:\r\n      tablelen= max(tablelen, len(table.name))\r\n      for column in table.columns:\r\n        columnlen= max(columnlen, len(column.name))\r\n        typelen= max(typelen, len(column.formattedType))\r\n        #print isinstance(column.comment, unicode)\r\n        # GRT \u5b57\u4e32\u70ba UTF-8 \u7de8\u78bc\uff0c\u8981\u5148\u8f49\u63db\u6210 Python \u7684 unicode\uff0c\u7136\u5f8c\u518d\u8f49\u6210 big5 \u7b97\u9577\u5ea6 (bytes)\r\n        commentbytes= len(column.comment.decode(\"utf-8\").encode(\"big5\"))\r\n        commentlen= max(commentlen, commentbytes)\r\n        lines.append((schema.name, table.name, column.name, column.formattedType, column.comment))\r\n\r\n  printTableLine([(\"-\", schemalen), (\"-\", tablelen), (\"-\", columnlen), (\"-\", typelen), (\"-\", commentlen)], \"-\")\r\n  printTableLine([(\"Schema\", schemalen), (\"Table\", tablelen), (\"Column\", columnlen), (\"Type\", typelen), (\"Comment\", commentlen)])\r\n  printTableLine([(\"-\", schemalen), (\"-\", tablelen), (\"-\", columnlen), (\"-\", typelen), (\"-\", commentlen)], \"-\")\r\n\r\n  for s,t,c,dt,dc in lines:\r\n    printTableLine([(s, schemalen), (t, tablelen), (c, columnlen), (dt, typelen), (dc, commentlen)])\r\n\r\n  printTableLine([(\"-\", schemalen), (\"-\", tablelen), (\"-\", columnlen), (\"-\", typelen), (\"-\", commentlen)], \"-\")\r\n  print len(lines), \"columns printed\"\r\n\r\n  return 0<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e4b\u524d\u5beb\u7684\u7528\u4f86 Dump \u6b04\u4f4d\u8aaa\u660e\u6b04\u5167\u5bb9\u7684\uff0c\u4e3b\u8981\u662f\u53c3\u7167 Data Dictiona &hellip; <a href=\"https:\/\/blog.nami.idv.tw\/?p=15\">\u95b1\u8b80\u5168\u6587 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[33],"tags":[3],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-develop","tag-mysql"],"_links":{"self":[{"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/15","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15"}],"version-history":[{"count":11,"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":252,"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/15\/revisions\/252"}],"wp:attachment":[{"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}