Groovy GString issues
Asked Answered
C

4

0

I'm want use $ macro in groovy GString. When i'm wrote this code

['cdata','tdata'].each { def sql = "select * from $it_1" }

i'm get error unknown property $it_

ok, i'm rewrite it

['cdata','tdata'].each { def sql = "select * from ${it}_1" }

then i'm get unwanted quotes in result string - "select * from 'cdata'_1"

Question is how i'm can use $-macro in GString to achive "select * from cdata_1" result string?

Chantilly answered 25/10, 2009 at 15:57 Comment(2)
dont understand question? when you execute the code above in the groovy shell, there are not quotes in the result?Unequivocal
Hmm... shortly - $it_1 = error, ${it}_1 = 'cdata'_1, need $[somewhat] = cdata_1.Chantilly
C
4

You can use Groovy's Sql expand feature to help here. The following code will do the trick:

['cdata','tdata'].each {table -> def sql = "select * from ${Sql.expand table}_1" }

Using this method is particularly important if you have other parameters in your GString:

def name = 'Charlie Sheen'
def tables = ['normalPeople','crazyPeople']
tables.each { table -> 
    def sqlString = "select * from ${Sql.expand table} where name = ${name}"
    /* Execute SQL here */
}

In the example above a prepared statement will still be used, and the contents of the 'name' variable will still be handled as a parameter (thus helping to protect you against SQL injection attacks) but the table variable parameter will be expanded correctly.

Cresset answered 23/3, 2011 at 0:28 Comment(1)
great. I'm solve it by assign GString variable to String. In any way this groovy sql "feature" (automatically replace GStrings with query params) very unclear at my point of view.Chantilly
S
1

If the quotes are not from your IDE, or whatever you're evaluating your code in, you can do this:

['cdata','tdata'].each { def sql = "select * from ${it.replaceAll("'","")}_1" } 
Squelch answered 25/10, 2009 at 18:23 Comment(1)
I'm using Eclipse with Groovy Plugin and run as Groovy Script. May be problem there... But i'm try your solution, not so elegant but do work.Chantilly
U
0
groovy:000> ['cdata','tdata'].each { def sql = "select * from ${it}_1"; println sql }
select * from cdata_1
select * from tdata_1
===> [cdata, tdata]

I dont see any quotes... that is why I was asking for clarification

Unequivocal answered 25/10, 2009 at 18:7 Comment(1)
Hm... really... very strange... I'm using Eclipse with Groovy Plugin and run as Groovy Script. May be problem there... I'm try in groovy console and you right, i'm don't get quotes.Chantilly
C
0

Real answer was behind the question, so I'm sorry.
Groovy SQL makes parameterized query from GString, so after I had defined GString

def sql = "select * from ${it}_1";

I passed it to Groovy SQL and when I tried to execute the query, the actual query was

"select * from :?_1";

Of cource this drives MSSQL crazy.
Thanks to you all again, maybe someone will find this useful.

Chantilly answered 25/1, 2011 at 8:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.