cancel
Showing results for 
Search instead for 
Did you mean: 

How to simplify groovy code for several sql request in different table ?

SOLVED
Highlighted
Contributor

How to simplify groovy code for several sql request in different table ?

Hi,

how can we simplify a groovy code for several sql request which are not same.

i'm querying different table for different data so i have at least 50 sql queries and for each query i set propertis with data retrieved from table.

 

My problem is:

i have 50 sql queries.

Is there a way to simplify this code.

I cannot use loop because all tables are differents.

 

example of query:

 

StringBuilder builder = new StringBuilder()
def tableValues = sql.eachRow("select type from order where id = "abc123"") { row -> 
builder.append( "${row.type}" )
}
//Set properties
String myvalue = builder.toString()
testRunner.testCase.setPropertyValue( "type", type)



StringBuilder builder2 = new StringBuilder()
def tableValues2 = sql.eachRow("select name from product where desc = "yzx"") { row -> 
builder.append( "${row.name}" )
}
//Set properties
String myvalue2 = builder2.toString()
testRunner.testCase.setPropertyValue( "name", name)

 

1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: How to simplify groovy code for several sql request in different table ?

I would probably just put the results into the test run context.

context.type = sql.firstRow('select type from order where id = ?', ["abc123"]).type
context.name = sql.firstRow('select name from product where desc = ?', ["yzx"]).name

That form uses prepared statements, which you should be doing and is not much more difficult.

 

To get the values in a request's body:

${type}
${name}

To get the values in another groovy script:

context.type
context.name

 

2 REPLIES 2
Community Hero

Re: How to simplify groovy code for several sql request in different table ?

I would probably just put the results into the test run context.

context.type = sql.firstRow('select type from order where id = ?', ["abc123"]).type
context.name = sql.firstRow('select name from product where desc = ?', ["yzx"]).name

That form uses prepared statements, which you should be doing and is not much more difficult.

 

To get the values in a request's body:

${type}
${name}

To get the values in another groovy script:

context.type
context.name

 

Contributor

Re: How to simplify groovy code for several sql request in different table ?

Thank you so much JHunt.

in the case i have this sql request:

 

select type from order where number = "789" and id = "abc123";

 

i tried this:

context.type = sql.firstRow('select type from order where number ="789" and id = ?', ["abc123"]).type

it returns error


 

New Here?
Join us and watch the welcome video:
SeptemberNews
Join the exciting event
SeptemberHubBub
Top Kudoed Authors
Join the September Hub-bub to show off, learn and win