from wb import *
import grt
ModuleInfo = DefineModule(name= "MyModule", author= "My Name", version="1.0")
def generate_script(editor):
from string import strip
rs = editor.activeResultset
buffer = "\n"
if rs:
rows = min(5, rs.rowCount)
cols = len(rs.columns)
rs.goToFirstRow()
buffer += "-- Ans. "
for j in xrange(cols):
if j < cols - 1:
buffer += rs.columns[j].name + ", "
else:
buffer += rs.columns[j].name + "\n"
for i in xrange(rows):
buffer += '-- '
for j in xrange(cols):
if j < cols - 1:
buffer += strip(rs.stringFieldValue(j)) + ", "
else:
buffer += strip(rs.stringFieldValue(j)) + "\n"
rs.nextRow()
buffer += "\n"
return buffer
# because of a bug in the wbinputs.currentSQLEditor() input specifier from the wb module
# in Workbench 5.2.26, we include our own version of it here
def currentSQLEditor():
arg= grt.classes.app_PluginObjectInput()
arg.name= "activeSQLEditor"
arg.objectStructName= "db.query.Editor"
return arg
@ModuleInfo.plugin("my.plugin.fill_random_query", caption= "Copy Results to Editor", input=[currentSQLEditor()], pluginMenu="SQL/Utilities")
@ModuleInfo.export(grt.INT, grt.classes.db_query_Editor)
def fill_random_query(editor):
active_buffer = editor.activeQueryBuffer
script = active_buffer.script
try:
buffer = generate_script(editor)
except Exception, exc:
buffer = "\nError: %s\n" % exc
pos = active_buffer.selectionEnd
new_script = script[:pos] + buffer + script[pos:]
active_buffer.replaceContents(new_script)
return 0
Tuesday, April 26, 2011
Writing plugins/modules for MySQL Workbench
The example below copies first five rows of the current resultset to the editor below the selected text (goes to the top if no text is selected).
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment