WB Plugin : Dump column comments

之前寫的用來 Dump 欄位說明欄內容的,主要是參照 Data Dictionary Creator 的功能,在 MySQL Workbench 中實現。(當然沒像 Data Dictionary Creator 功能那樣多樣就是了)

#
#  wb_utils_dark_grt.py
#  MySQLWorkbench
#
#  Created by Dragon Chuang on 2010/09/04.
#  Creative Common License.
#

# import the wb module
from wb import *
# import the grt module
import grt

# define this Python module as a GRT module
ModuleInfo = DefineModule(name= "PyWbUtilsDark", author= "Dragon Chuang", version= "1.0")

# this is just a function used by the plugin, it's not exported
def printTableLine(fields, filler= " "):
  print "|",
  for text, size in fields:
    # DBCS在位置對齊時字數問題的修正 (WB 一個中文 len() 3, unicode 一個中文 len() 1, big5 一個中文 len() 2)
    dbcssizefix= 0
    dbcssizefix= len(text) - len(text.decode("utf-8").encode("big5"))
    #print len(text), len(text.decode("utf-8").encode("big5"))
	# 將換行取代成空白
    print text.replace("\n"," ").ljust(size + dbcssizefix, filler), "|",
  print

# @wbexport makes this function be exported by the module and also describes the return and
# argument types of the function
# @wbplugin defines the name of the plugin to "wb.catalog.util.dumpColumns", sets the caption to be
# shown in places like the menu, where to take input arguments from and also that it should be included
# in the Catalog submenu in Plugins.
@ModuleInfo.plugin("wb.catalog.util.dumpColumnComments", caption= "Dump All Table Column Comments", input= [wbinputs.currentCatalog()], pluginMenu= "Catalog")
@ModuleInfo.export(grt.INT, grt.classes.db_Catalog)

def printAllColumns(catalog):
  lines= []
  schemalen= 10
  tablelen= 10
  columnlen= 10
  typelen= 10
  commentbytes= 0
  commentlen= 10

  for schema in catalog.schemata:
    schemalen= max(schemalen, len(schema.name))
    for table in schema.tables:
      tablelen= max(tablelen, len(table.name))
      for column in table.columns:
        columnlen= max(columnlen, len(column.name))
        typelen= max(typelen, len(column.formattedType))
        #print isinstance(column.comment, unicode)
        # GRT 字串為 UTF-8 編碼,要先轉換成 Python 的 unicode,然後再轉成 big5 算長度 (bytes)
        commentbytes= len(column.comment.decode("utf-8").encode("big5"))
        commentlen= max(commentlen, commentbytes)
        lines.append((schema.name, table.name, column.name, column.formattedType, column.comment))

  printTableLine([("-", schemalen), ("-", tablelen), ("-", columnlen), ("-", typelen), ("-", commentlen)], "-")
  printTableLine([("Schema", schemalen), ("Table", tablelen), ("Column", columnlen), ("Type", typelen), ("Comment", commentlen)])
  printTableLine([("-", schemalen), ("-", tablelen), ("-", columnlen), ("-", typelen), ("-", commentlen)], "-")

  for s,t,c,dt,dc in lines:
    printTableLine([(s, schemalen), (t, tablelen), (c, columnlen), (dt, typelen), (dc, commentlen)])

  printTableLine([("-", schemalen), ("-", tablelen), ("-", columnlen), ("-", typelen), ("-", commentlen)], "-")
  print len(lines), "columns printed"

  return 0
分類: 軟體開發,標籤: 。這篇內容的永久連結