Reputation: 1196
I am currently working on a grails project where I need to execute a hibernate query. Everytime I run my code with existing data on my database, everything works fine. But if I run it with my database empty, I got an error thrown.
This is my code:
def actActivation = AuditLog.executeQuery("select ll.username from AuditLog ll where ll.event = ? ",[AuditLogEvent.ACCOUNT_ACTIVATION])
def actDeleted = AuditLog.executeQuery("select ll.username from AuditLog ll where ll.event = ? ",[AuditLogEvent.ACCOUNT_DELETION])
def actPurged = AuditLog.executeQuery("select ll.username from AuditLog ll where ll.event = ? ",[AuditLogEvent.ACCOUNT_PURGING])
def list = AuditLog.executeQuery("""select sum(case when l.details like '%Gender: Male.%' and l.event = :actActivationEvent then 1 else 0 end),
sum(case when l.details like '%Gender: Female.%' and l.event = :actActivationEvent then 1 else 0 end),
sum(case when l.event = :actActivationEvent then 1 else 0 end),
sum(case when l.details like '%Gender: Male.%' and l.event = :actFinishRegEvent and ((l.username not in(:actActivation)) or (l.username in(:actDeleted) or l.username in(:actPurged))) then 1 else 0 end),
sum(case when l.details like '%Gender: Female.%' and l.event = :actFinishRegEvent and ((l.username not in(:actActivation)) or (l.username in(:actDeleted) or l.username in(:actPurged))) then 1 else 0 end),
sum(case when l.event = :actFinishRegEvent and ((l.username not in(:actActivation)) or (l.username in(:actDeleted) or l.username in(:actPurged))) then 1 else 0 end),
sum(case when l.details like '%Gender: Male.%' and l.event = :actDeletionEvent then 1 else 0 end),
sum(case when l.details like '%Gender: Female.%' and l.event = :actDeletionEvent then 1 else 0 end),
sum(case when l.event = :actDeletionEvent then 1 else 0 end),
sum(case when l.details like '%Gender: Male.%' and l.event = :actPurgedEvent then 1 else 0 end),
sum(case when l.details like '%Gender: Female.%' and l.event = :actPurgedEvent then 1 else 0 end),
sum(case when l.event = :actPurgedEvent then 1 else 0 end)
from AuditLog l
where l.dateCreated >= :startDate and l.dateCreated < :endDate""",
[
actActivationEvent:AuditLogEvent.ACCOUNT_ACTIVATION,
actDeletionEvent:AuditLogEvent.ACCOUNT_DELETION,
actPurgedEvent:AuditLogEvent.ACCOUNT_PURGING,
actFinishRegEvent:AuditLogEvent.FINISH_REGISTRATION,
actActivation:actActivation,
actDeleted:actDeleted,
actPurged:actPurged,
startDate: startDate,
endDate:endDate
])
This is the error message when I execute the code with the database empty.
Class: org.hibernate.hql.ast.QuerySyntaxException
Message:
unexpected end of subtree [select sum(case when l.details like '%Gender: Male.%' and l.event = :actActivationEvent then 1 else 0 end), sum(case when l.details like '%Gender: Female.%' and l.event = :actActivationEvent then 1 else 0 end), sum(case when l.event = :actActivationEvent then 1 else 0 end), sum(case when l.details like '%Gender: Male.%' and l.event = :actFinishRegEvent and ((l.username not in()) or (l.username in() or l.username in())) then 1 else 0 end), sum(case when l.details like '%Gender: Female.%' and l.event = :actFinishRegEvent and ((l.username not in()) or (l.username in() or l.username in())) then 1 else 0 end), sum(case when l.event = :actFinishRegEvent and ((l.username not in()) or (l.username in() or l.username in())) then 1 else 0 end), sum(case when l.details like '%Gender: Male.%' and l.event = :actDeletionEvent then 1 else 0 end), sum(case when l.details like '%Gender: Female.%' and l.event = :actDeletionEvent then 1 else 0 end), sum(case when l.event = :actDeletionEvent then 1 else 0 end), sum(case when l.details like '%Gender: Male.%' and l.event = :actPurgedEvent then 1 else 0 end), sum(case when l.details like '%Gender: Female.%' and l.event = :actPurgedEvent then 1 else 0 end), sum(case when l.event = :actPurgedEvent then 1 else 0 end) from ph.gov.csc.comex.log.AuditLog l where l.dateCreated >= :startDate and l.dateCreated < :endDate]
How will I resolve this? Help please! Thanks!
Upvotes: 0
Views: 675
Reputation: 691865
The problem comes from the fact that you blindly pass the result of the three first queries as arguments to the last query. This results in a where clause containing:
where ... l.username not in () ...
which is invalid. An in ()
clause must at least have one element inside the parentheses. You should use another query if one of the list you pass as argument to the in
clauses is empty.
Upvotes: 2