Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

How do I split SQL output into separate lines via Scriptrunner?

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
I got a result, but it's one large string separated by comma.
I want to have the output line by line (1 line per user id), but everything I try fails.
Thx,
Michael

1 answer

1 accepted

0 votes
Answer accepted
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 25, 2022 • edited

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()
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 25, 2022

@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

Hi @Vikrant Yadav

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:

result.jpg

After each closing square bracket a line break should be done.

Thx,

Michael

Like Vikrant Yadav likes this
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 28, 2022

@Michael Krüger  

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

Hi @Vikrant Yadav

that works for me. Thanks a lot.

Best,

Michael

Like Vikrant Yadav likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events