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

Allow to specify a default value on placeholder #852

Closed
kazuki43zoo opened this issue Nov 24, 2016 · 8 comments
Closed

Allow to specify a default value on placeholder #852

kazuki43zoo opened this issue Nov 24, 2016 · 8 comments
Assignees
Labels
enhancement Improve a feature or add a new feature
Milestone

Comments

@kazuki43zoo
Copy link
Member

kazuki43zoo commented Nov 24, 2016

I want to support that allow to specify a default value on properties placeholder as follow:

<dataSource type="POOLED">
  <property name="username" value="${db.username:postgres}"/>
  <property name="password" value="${db.password:Postgres}"/>
</dataSource>

This solution has been supported by the Spring Framework , Logback, etc...
Note that : We should take to consider the : character has been used as placeholder key or used ternary operator(such as condition ? value_if_true : value_if_false) using OGNL. (for backward compatibility)

What do you think this enhancement ?

@kazuki43zoo kazuki43zoo added the enhancement Improve a feature or add a new feature label Nov 24, 2016
@kazuki43zoo
Copy link
Member Author

For prototype see 241ba45. In this changes, I does not take to consider backward compatibility yet.

kazuki43zoo added a commit to kazuki43zoo/mybatis-3 that referenced this issue Nov 25, 2016
@kazuki43zoo
Copy link
Member Author

I've updated the prototype via cbc6fe0. In this version, backward compatibility has been taken to consider.

By default this feature is disabled .
When using this feature, a special property will specify as follow:

<properties>
    <property name="org.apache.ibatis.parsing.PropertyParser.enable-default-value" value="true"/>
</properties>

@emacarron
Copy link
Member

I like the feature @kazuki43zoo

+1

@kazuki43zoo
Copy link
Member Author

kazuki43zoo commented Nov 26, 2016

@emacarron @eddumelendez

Thanks for your reaction !! I will submit a PR after adding some improvements.

@kazuki43zoo kazuki43zoo added this to the 3.4.2 milestone Nov 26, 2016
@kazuki43zoo kazuki43zoo self-assigned this Nov 26, 2016
@kazuki43zoo
Copy link
Member Author

kazuki43zoo commented Nov 26, 2016

I've improved the prototype via 0f057c2.

In this change, support to customize a separator characters.
When customizing separator characters(default is ":"), a special property will specify as follow:

<properties>
    <property name="org.apache.ibatis.parsing.PropertyParser.enable-default-value" value="true"/>
    <property name="org.apache.ibatis.parsing.PropertyParser.default-value-separator" value="?:"/> <!-- ### Adding ### -->
</properties>

If ":" character has been used as placeholder key already, this mechanism is effective as follow:

<dataSource type="POOLED">
  <property name="username" value="${db:username?:postgres}"/>
  <property name="password" value="${db:password?:Postgres}"/>
</dataSource>

and If ternary operator has been used using OGNL in your sql already, this mechanism is effective as follow:

@Select("SELECT * FROM ${db:schema?:prod}.${tableName == null ? 'global_constants' : tableName}" ORDER BY ${orderByColumns})
List<SysConstant> findAll(@Param("tableName") String tableName, @Param("orderByColumns") String orderByColumns);

Note that : A default value can not specify to the parameter replacement variable (above example: ${orderByColumns}) on your sql. If you want to specify a default value on the parameter replacement variable, should be use the OGNL expression instead of (e.g. ${orderByColumns != null ? orderByColumns : 'id'}).

kazuki43zoo added a commit to kazuki43zoo/mybatis-3 that referenced this issue Nov 26, 2016
@harawata
Copy link
Member

Looks like a handy feature. Thank you :)

@kazuki43zoo
Copy link
Member Author

Hi @harawata , thanks for your reaction !! I will gh-853 merge at later.

kazuki43zoo added a commit to kazuki43zoo/mybatis-3 that referenced this issue Nov 27, 2016
@kazuki43zoo
Copy link
Member Author

Hi guys, I've fixed via gh-853. If my fix has problems, please reopen this issue.
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improve a feature or add a new feature
Projects
None yet
Development

No branches or pull requests

3 participants