SpellBound: Free Spellchecker for Firefox The web email innovation wars continue: Oddblog bought by Yahoo!
Jul 14

Simple gotchas with debugging Groovy SQL

Groovy, Tech Add comments

Here is another little gotcha that I have run into (again, due to not thinking and being dumb).

I would use Groovy SQL and start testing in the script leaving me with:

sql.eachRow(”SELECT userpk FROM users where email=’[email protected]’”) { user |
… do stuff
}

then at some point I would want to use a variable for the email and would end up with:

sql.eachRow(”SELECT userpk FROM users where email=’${email}’”) { user |
… do stuff
}

What is wrong with this? The quotes around the variable are wrong. You don’t think of it as “put in this variable here” as Groovy SQL can be smart and will make a prepared statement with question marks ? ? ?

So, taking out the quotes does the trick:

sql.eachRow(”SELECT userpk FROM users where email=${email}”) { user |
… do stuff
}

There is talk on allowing $email as well as ${email}. Bring it on :)

3 Responses to “Simple gotchas with debugging Groovy SQL”

  1. James Strachan Says:

    Maybe we could turn on warnings in Groovy SQL so that if you use a $expression inside some SQL it warns you if there are single or double quotes around the expression.

    e..g

    sql.eachRow “select * from blah where x = ‘$foo’”

    would generate a warning but this wouldn’t

    sql.eachRow “select * from blah where x = $foo”

  2. Dion Says:

    That would be like butter :)

    D

  3. James Strachan Says:

    Dion, your wish is my command (again :).

    CVS HEAD now warns if you accidentally quote a dynamic expression.

    Strictly speaking, GroovySQL could be clever enough to trim off the unnecessary quotes for you automatically and still reuse the prepared statement – but for now the query still works (maybe there’s a need for this as it enforces string coercion?) – but gives a nice warning to let you know there’s a possible security leak.

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: What are the first four letters in the word British?