Hi all,
I'm trying to run sql query to get all users who have never been logged in to JIRA via Scriptrunner console.
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.db.DatabaseUtil
import groovy.sql.Sql
def issueManager = ComponentAccessor.issueManager
def output = []
DatabaseUtil.withSql('local') { sql ->
output = sql.rows("SELECT cwd_user.id, cwd_user.user_name, cwd_user.created_date FROM jiradb.cwd_user WHERE not exists (select cwd_user_attributes.user_id from jiradb.cwd_user_attributes where cwd_user_attributes.attribute_name = 'login.count' and cwd_user_attributes.user_id = cwd_user.id) and cwd_user.active = true order by cwd_user.created_date")
}
log.warn "${output.size()}"
log.warn "${output}"
output
Hi @Michael Krüger Welcome to Atlassian Community!
Try to modify your result part like below script, to get the Result in table format :-
import com.atlassian.jira.component.ComponentAccessor
import groovy.sql.Sql
import org.ofbiz.core.entity.ConnectionFactory
import org.ofbiz.core.entity.DelegatorInterface
import java.sql.Connection
def delegator = (DelegatorInterface) ComponentAccessor.getComponent(DelegatorInterface)
String helperName = delegator.getGroupHelperName("default")
//Update SQL query as per the your requirement
def sqlStmt = """
SELECT cwd_user.id, cwd_user.user_name, cwd_user.created_date FROM cwd_user
"""
Connection conn = ConnectionFactory.getConnection(helperName)
Sql sql = new Sql(conn)
//Create table
String result = """
<table>
<tr>
<th>UserId</th>
<th>User Name</th>
<th>Create Date</th>
</tr>
"""
sql.eachRow(sqlStmt){ it ->
result += """
<tr>
<td>${it.getAt("id")}</td>
<td>${it.getAt("user_name")}</td>
<td>${it.getAt("created_date")}</td>
</tr>
"""
}
return result+"</table>"
sql.close()
@Michael Krüger If SQL query doesn't works for you then try to run script mentioned in below link in script console :-
https://gist.github.com/vikrantyadav11/61a04c6f05a8fb719e773ab42b1b5b67
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
thanks a lot for your quick response.
Sounds interesting with the table creation (I'll try that also), but I simply want to have the result line by line without any further formatting.
This is how the result looks like now:
After each closing square bracket a line break should be done.
Thx,
Michael
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Try below script for this , use :- sb.append('\n')
But it give only result upto 300 lines only.
Update SQL query as well and column name cfname.
import com.atlassian.jira.component.ComponentAccessor
import groovy.sql.Sql
import org.ofbiz.core.entity.ConnectionFactory
import org.ofbiz.core.entity.DelegatorInterface
import java.sql.Connection
def delegator = (DelegatorInterface) ComponentAccessor.getComponent(DelegatorInterface)
String helperName = delegator.getGroupHelperName("default")
def sqlStmt = """
SELECT cfname
FROM customfield
WHERE customfieldtypekey = 'com.atlassian.jira.plugin.system.customfieldtypes:select';
"""
Connection conn = ConnectionFactory.getConnection(helperName)
Sql sql = new Sql(conn)
try {
StringBuffer sb = new StringBuffer()
sql.eachRow(sqlStmt) { it ->
sb.append( it.getAt("cfname") )
sb.append('\n')
}
log.warn sb.toString()
}
finally {
sql.close()
}
Thanks
V.Y
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.