Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add another option to process null bind list #1377

Closed
shelocks opened this issue Dec 26, 2018 · 3 comments · Fixed by #1378
Closed

Add another option to process null bind list #1377

shelocks opened this issue Dec 26, 2018 · 3 comments · Fixed by #1378

Comments

@shelocks
Copy link
Contributor

Current for this sql use BindList and StringTemplate4

@SqlQuery("select * from resource where app_id = :appId <if(resourceIds)> and resource_id in 
                     +"(<resourceIds>) <endif> "
@UseStringTemplateEngine 
List<Object> query(@Bind("appId") String appId,@Define("resourceIds") @BindList(value = "resourceIds", onEmpty = BindList.EmptyHandling.NULL) List<String> resourceIds)

when resourceIds input is null,the sql will render to

select * from resource where app_id=xxx and resource_id in (null)

but i want it render to

select * from resource where app_id=xxx

currently a workaround way is convert input null to a empty list,but not everyone invoke this method know this trick

can BindList.EmptyHandling add another option?we can process null list like this:

stmt.define(name, null); 
@leaumar
Copy link

leaumar commented Dec 26, 2018

I don't see how EmptyHandling would offer a solution here, that option only controls what the in (?) part should render to if the list is null or empty: in (null), in (), or an exception.

I'm not familiar with using StringTemplate, but it seems odd to me that it considers resourceIds truthy when it's null. Is that the root problem we should look at?

Does your code work when you pass in an emptyList() instead of null?

@shelocks
Copy link
Contributor Author

but it seems odd to me that it considers resourceIds truthy when it's null. Is that the root problem we should look at?

StringTemplate evaluate like this:

expr x undefined x null x="" x=list len=0
<if(x)>y< endif> "" "" y ""

Currently EmptyHandling value processed like:
EmptyHandling.VOID --> stmt.define("name","")
EmptyHandling.NULL --> stmt.define("name","null"), is null string,not null value
EmptyHandling.THROW --> throw a exception

Does your code work when you pass in an emptyList() instead of null?

yes.but everyone invoke this method need to know this trick(empty list will evaluated to false),and convert a null value to empty list

@leaumar
Copy link

leaumar commented Dec 27, 2018

Hmm, I think the reason we insert "null" instead of null is because it's a Map, where getting a non-existent key returns null, so putting any key with value null is pointless. We also have to put it as a string because this is in the context of templating, where we need the literal word "null" to show up in the query.

leaumar pushed a commit that referenced this issue Dec 27, 2018
leaumar pushed a commit that referenced this issue Dec 27, 2018
stevenschlansker added a commit that referenced this issue Jan 3, 2019
fix #1377: BindList conditionals with null and StringTemplate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants