Created
April 2, 2014 20:55
-
-
Save berend/9942988 to your computer and use it in GitHub Desktop.
Workbench Plugin: Execute query to jira formatted table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)