views:

36

answers:

1

I am hoping to use Groovy more as a functional language than I can with Java, but one area that seems to be a problem is when I call to a stored procedure, as I am passing perhaps 40 parameters in a single call, but, I also need to do some prep work, at the moment before I even call.

So, for example, I need a time stamp, so I will have something similar to (so there may be errors, but it is the concept I am going for)

def timestamp = (int) (Calendar.instance.timeInMillis/1000)
def ismanager = input.isManager ? 1 : 0
sql.call("{call myfunction($timestamp, ..., $ismanager, ..."})

It would be helpful if I could do these type of calls inside the query, as it would make more sense what is going on, since there are so many parameters in the stored procedure, so having to look around for what went into initializing ise manager can be problematic.

Is there a way to have these functions executed within the call gstring?

+2  A: 

You can do it by embedding them inside the string in this way:

sql.call("{call myfunction(${(int) (Calendar.instance.timeInMillis/1000)}, 
                          ..., ${input.isManager ? 1 : 0}, ...}")

then since SQL should ignore line breaks if not in the middle of a word (I absolutely don't remember this well) you can use triple-double-escaped-strings (what a powerful name):

"""call myfunction${(int) (Calendar.instance.timeInMillis/1000)}
..., ${input.isManager ? 1 : 0}, ...}"""

without annoying yourself with a long string.

Jack
I am currently just concatenating them using plus operator, but I will try this tomorrow, thank you.
James Black