-2

question from a groovy newbie:

sql is initiated as follows

 final Binding binding = new Binding();  
 binding.setProperty("sql", sql);
final groovy.sql.Sql sql = Sql.newInstance(dbConfig.getUrl(), dbConfig.getUserName(), dbConfig.getPasswd(),"oracle.jdbc.OracleDriver");

I am running a query in groovy like this

def listOfRows = sql.rows (select column1 from table1);

listOfRows when printed shows contents like [[column1_name:value1], [column1_name:value2], [column1_name:value3]]

I want to check if value2 (a String) exists in the returned list of values from the above query.

I have tried doing listOfRows.contains('value2') and listOfRows.find('value2'), it complains that the method does not exist for lists..

what's the best way of doing this ?

EDITED: I have corrected the list of printed values. What's being returned is List<GroovyResultSet> and I have also added the definition of sql.

Robin Bajaj
  • 2,002
  • 4
  • 29
  • 47
  • What's your definition of `sql`? – Charles Wood Dec 30 '13 at 16:23
  • 2
    What does listOfRows actually contain? It can't be `[column1_name:value1, column1_name:value2, column1_name:value3]` – tim_yates Dec 30 '13 at 16:24
  • I second @tim_yates, `[column1_name:value1, column1_name:value2, column1_name:value3]` is not even a map, since `keys` are same. Are you getting a list of such maps with keys as column1, column2, ....? – dmahapatro Dec 30 '13 at 16:34
  • I stand corrected, what's being returned is list Correction - listOfRows when printed shows contents like [[column1_name:value1], [column1_name:value2], [column1_name:value3]] – Robin Bajaj Dec 31 '13 at 15:36

4 Answers4

1

I would suggest you to take a look at groovy documentation, and particularly to collections documentation (both tutorial and JDK/GDK).

in that case, the most specifically adapted solution would be to use Collection#find() ... with something like

listOfRows.find { it.contains(':value2') }

Which can be translated into human-readable

find the first element in this collection which string contains ":value2".

Riduidel
  • 22,052
  • 14
  • 85
  • 185
  • In 2021, those links are broken. This is the most appropriate "modern" one: http://docs.groovy-lang.org/next/html/documentation/working-with-collections.html – Necreaux Nov 08 '21 at 16:45
1

You probably want

listOfRows.column1.contains( 'value2' )
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • just to add that: listOfRows is list of maps with your data read from the DB. with colums names as keys and the expression `listOfRows.column1` (equivalnet to `listOfRows['column1']` converts the map in a list with the content of all the rows for the 'column1'. – user1708042 Dec 31 '13 at 17:54
  • As I mentioned I am Groovy newbie, I cann't figure out the correct syntax for doing this .. (a) listOfRows.column1.contains( 'value2' ) (b) listOfRows['column1'].contains( 'value2' ) neither works.. – Robin Bajaj Dec 31 '13 at 22:26
0

You are probably invoking this method which takes a GString (note that GString != String) as an argument. According to this question, a string in single quotes is a standard java string, and a string in double quotes is a templatable string.

'hello'    //java.lang.String
"hello"    //groovy.lang.GString

Try this:

listOfRows.contains("value2")
Community
  • 1
  • 1
Hernán Erasmo
  • 371
  • 5
  • 15
0

what i ended up doing is following :

iterate the listOfRows, get all the values for column1 from each GroovyResultSet into a listOfValues ,then check for my values in that list.

def listOfValues=[];
listOfRows.collect(listOfValues){it.getAt('column1')};
if(listOfValues.size()==3){
  println('success');
}
Robin Bajaj
  • 2,002
  • 4
  • 29
  • 47