Skip to content

Instantly share code, notes, and snippets.

@berend
Created April 2, 2014 20:55
Show Gist options
  • Save berend/9942988 to your computer and use it in GitHub Desktop.
Save berend/9942988 to your computer and use it in GitHub Desktop.
Workbench Plugin: Execute query to jira formatted table
from wb import *
import grt
import mforms
# define this Python module as a GRT module
ModuleInfo = DefineModule(name="mysql_to_jira",
author="berend.kapelle",
version="1.2")
@ModuleInfo.plugin(
"wb.sqlide.mysql_to_jira_table",
caption="Execute Query To Jira Formatted Table",
input=[wbinputs.currentQueryBuffer()],
pluginMenu="SQL/Utilities"
)
@ModuleInfo.export(grt.INT, grt.classes.db_query_QueryBuffer)
def mysql_to_jira_table(qbuffer):
editor = qbuffer.owner
sql = qbuffer.selectedText or qbuffer.script
resultsets = editor.executeScript(sql)
for result in resultsets:
editor.addToOutput("Query:\n{code}\n%s\n{code}\n" % result.sql, 0)
headerline = []
ncolumns = len(result.columns)
for column in result.columns:
headerline.append(column.name)
headerline = " || ".join(headerline)
editor.addToOutput("|| " + headerline + " ||\n", 0)
rows = []
ok = result.goToFirstRow()
while ok:
line = []
for i in range(ncolumns):
value = result.stringFieldValue(i)
if value is None:
value = "NULL"
line.append(value)
if set(line) != set(["NULL"]):
line = " | ".join(line)
#dont add line, if all values are "NULL"
#WB adds an edit line to the result when index is included
rows.append("| " + line + " |\n")
ok = result.nextRow()
# much faster to do it at once than add lines one by one
editor.addToOutput("".join(rows), 0)
editor.addToOutput("%i rows\n" % len(rows), 0)
return 0
@berend
Copy link
Author

berend commented Apr 2, 2014

To install:
Open Workbench, Scripting>Install Plugin/Module, select script from above.

To run:
Type your SQL script, Tools>Utilities>Execute Query To Jira Formatted Table

Result should be in the textout pane (you can switch the Action Output pane to textoutput)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment