Repository navigation
Expand file tree
/
Copy pathdbSelectConsoleScript.groovy
More file actions
62 lines (50 loc) · 1.89 KB
/
Copy pathdbSelectConsoleScript.groovy
File metadata and controls
62 lines (50 loc) · 1.89 KB
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
import groovy.sql.Sql;
import java.sql.Connection;
import java.sql.SQLException;
import groovy.sql.GroovyRowResult;
import com.atlassian.jira.ofbiz.OfBizDelegator;
import org.ofbiz.core.entity.ConnectionFactory;
import org.ofbiz.core.entity.DelegatorInterface;
import org.ofbiz.core.entity.GenericEntityException;
import com.atlassian.jira.component.ComponentAccessor;
String query = """SELECT * FROM jiraissue"""
List<GroovyRowResult> result = dbSelect(query)
listToHtmlTable(result)
List<GroovyRowResult> dbSelect(String query) throws SQLException, GenericEntityException {
OfBizDelegator delegator = ComponentAccessor.getOfBizDelegator();
DelegatorInterface delegatorInterface = delegator.getDelegatorInterface();
String helperName = delegatorInterface.getGroupHelperName("default");
try (Connection connection = ConnectionFactory.getConnection(helperName);
Sql sql = new Sql(connection)) {
return sql.rows(query);
}
}
String listToHtmlTable(List<GroovyRowResult> dataList) {
StringBuilder stringBuilder = new StringBuilder()
stringBuilder.append("<table class='aui' border='1'>")
// <-- Headers -->
if (!dataList.isEmpty()) {
stringBuilder.append("<thead>")
stringBuilder.append("<tr>")
GroovyRowResult firstRow = dataList.get(0)
firstRow.keySet().each {key ->
stringBuilder.append("<th>$key</th>")
}
stringBuilder.append("</tr>")
stringBuilder.append("</thead>")
}
// <-- Headers -->
// <-- Body -->
stringBuilder.append("<tbody>")
dataList.each { row ->
stringBuilder.append("<tr>")
row.each { key, value ->
stringBuilder.append("<td>${value}</td>")
}
stringBuilder.append("</tr>")
}
stringBuilder.append("</tbody>")
// <-- Body -->
stringBuilder.append("</table>")
return stringBuilder.toString()
}