-
Notifications
You must be signed in to change notification settings - Fork 0
/
result-to-redmine_grt.py
70 lines (57 loc) · 1.89 KB
/
result-to-redmine_grt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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])