Is there a way to make a query parameter mandatory?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Is there a way to make a query parameter mandatory?

rwfischer
Hello,

is there a way to make a query parameter mandatory, so that MyBatis throws an appropriate exception when the parameter does not have a value?

select * from some_table
where category = #{category}
 
<if test = "region != null">
   
and region = #{region}
 
</fi>

When the caller does not specify a value for the category parameter, the SQL will be invalid (category = null).

I would prefer if the caller receives a more specific error. 
  • Would it be possible to throw an exception within an <if>?
  • Is there an annotation in the query or the parameter model to indicate that a parameter is mandatory?
Thanks...

Roger

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a way to make a query parameter mandatory?

Guy Rouillier-2
Wouldn't your code be simpler if you validated the parameter in your source code before invoking the SQL statement?  Then you could make the message as informative as you like.  If you are concerned about having to do that in multiple clients, then write a central method that all clients can call, and put the parameter validation in that central method.  In general, that's a preferred approach anyway.

--
Guy Rouillier



------ Original Message ------
From: "rwfischer" <[hidden email]>
To: "mybatis-user" <[hidden email]>
Sent: 5/31/2017 12:19:41 PM
Subject: Is there a way to make a query parameter mandatory?

Hello,

is there a way to make a query parameter mandatory, so that MyBatis throws an appropriate exception when the parameter does not have a value?

select * from some_table
where category = #{category}
 
<if test = "region != null">
   
and region = #{region}
 
</fi>

When the caller does not specify a value for the category parameter, the SQL will be invalid (category = null).

I would prefer if the caller receives a more specific error. 
  • Would it be possible to throw an exception within an <if>?
  • Is there an annotation in the query or the parameter model to indicate that a parameter is mandatory?
Thanks...

Roger

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

Virus-free. www.avast.com

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.