from wb import *
import grt
import mforms
import re

ModuleInfo = DefineModule(name= "Exporter", author= "Alexander Ustimenko", version="1.0")

@ModuleInfo.plugin(
    "wb.sqlide.exportToRedmine",
    caption= "Export to Redmine comments",
    input= [wbinputs.currentSQLEditor()],
    pluginMenu= "SQL/Utilities")
@ModuleInfo.export(grt.INT, grt.classes.db_query_Editor)
def exportToRedmine(editor):
    """ Exports current query and it's results to clipboard in Redmine comments format
    """
    
    if editor.activeQueryEditor.activeResultset == None:
        status = "No SQL result for export to redmine!"
        print status
        mforms.App.get().set_status_text(status)
        return 1

    rs = editor.activeQueryEditor.activeResultset

    table = [[]]
    for column in rs.columns:
        table[0].append(_textileHeader(column.name))
    table[0] = _textileRow(table[0])

    hasRows = rs.goToFirstRow()
    lastRow = rs.rowCount
    if isinstance(rs, grt.classes.db_query_EditableResultset):
        lastRow -= 1

    while hasRows:
        if rs.currentRow == lastRow:
            break
        row = []
        for column in rs.columns:
            row.append(rs.stringFieldValueByName(column.name))
        table.append(_textileRow(row))
        hasRows = rs.nextRow()

    output = '\n' . join(table + [
        '',
        '<pre><code class="sql">',
        rs.sql.strip(),
        '</code></pre>'
    ])
    
    print output
    mforms.Utilities.set_clipboard_text(output)
    sql = re.sub("\s+", " ", rs.sql.strip())
    mforms.App.get().set_status_text("SQL result exported to redmine: " + sql)
    
    return 0

def _textileHeader(cell):
    return '_. ' + cell

def _textileRow(row):
    sRow = ['']
    for cell in row:
        sRow.append(str(cell))
    sRow.append('')
    return '|' . join(sRow)

# debug
# exportToRedmine(grt.root.wb.sqlEditors[0])


from wb import *
import grt
import mforms
import re

ModuleInfo = DefineModule(name= "Exporter", author= "Alexander Ustimenko", version= "1.1")

@ModuleInfo.plugin(
    "wb.sqlide.exportToRedmine",
    caption= "Export to Redmine comments",
    input= [wbinputs.currentSQLEditor()],
    pluginMenu= "SQL/Utilities")
@ModuleInfo.export(grt.INT, grt.classes.db_query_Editor)
def exportToRedmine(editor):
    """ Exports current query and it's results to clipboard in Redmine comments format
    """

    if editor.activeQueryEditor.activeResultPanel == None:
        status = "No SQL result for export to redmine!"
        print status
        mforms.App.get().set_status_text(status)
        return 1

    rs = editor.activeQueryEditor.activeResultPanel.resultset

    table = [[]]
    for column in rs.columns:
        table[0].append(_textileHeader(column.name))
    table[0] = _textileRow(table[0])

    hasRows = rs.goToFirstRow()
    lastRow = rs.rowCount
    if isinstance(rs, grt.classes.db_query_EditableResultset):
        lastRow -= 1

    while hasRows:
        if rs.currentRow == lastRow:
            break
        row = []
        for column in rs.columns:
            row.append(rs.stringFieldValueByName(column.name))
        table.append(_textileRow(row))
        hasRows = rs.nextRow()

    output = '\n' . join(table + [
        '',
        '<pre><code class="sql">',
        rs.sql.strip(),
        '</code></pre>'
    ])

    print output
    mforms.Utilities.set_clipboard_text(output)
    sql = re.sub("\s+", " ", rs.sql.strip())
    mforms.App.get().set_status_text("SQL result exported to redmine: " + sql)

    return 0

def _textileHeader(cell):
    return '_. ' + cell

def _textileRow(row):
    sRow = ['']
    for cell in row:
        sRow.append(str(cell))
    sRow.append('')
    return '|' . join(sRow)

# to debug uncomment next string and press Ctrl + R
# exportToRedmine(grt.root.wb.sqlEditors[0])

Make compatible with latest db.QueryEditor class structure