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

SQL include does not replace in attributes #1069

Closed
kmoco2am opened this issue Aug 8, 2017 · 3 comments
Closed

SQL include does not replace in attributes #1069

kmoco2am opened this issue Aug 8, 2017 · 3 comments
Assignees
Labels
enhancement Improve a feature or add a new feature
Milestone

Comments

@kmoco2am
Copy link
Contributor

kmoco2am commented Aug 8, 2017

Current implementation of SQL fragment include replaces placeholders only in text nodes of XML. If placeholder is placed for example in "test" attribute of "if" it is not replaced. It looks like a bug in the implementation which scans only "child" nodes but it had "attribute" branch too which has not been ever used.

Example usage:

<sql id="select-part">
    SELECT d.field1, d.field2, ..., d.fieldN
    <if test="${source} == 'active'">
        , null AS archivedDate
        FROM active_table d
    </if>
    <if test="${source} == 'archive'">
        , d.achived AS archivedDate
        FROM archive_table d
    </if>
    LEFT JOIN ...
    ORDER BY ...
</sql>

<select id="fetchData" resultMap="Data">
    <include refid="select-part">
        <property name="source" value="active"/>
    </include>
    UNION ALL
    <include refid="select-part">
        <property name="source" value="archive"/>
    </include>
</select>

(example taken from original question on StackOverflow)

@lushaorong
Copy link

lushaorong commented Aug 11, 2017

mayby you can use <bind name="" value="">

<select id="select-part" resultClass="HashMap"  parameterClass="HashMap">
  <bind name="email" value="'%' +email + '%'" />
  select * from tableone
  where
  <if test="email != null and email != ''">
    email like #{email}
  </if>
</select>

@w6et
Copy link

w6et commented Aug 11, 2017

@c340c340 diff case you are

@kmoco2am
Copy link
Contributor Author

@c340c340 bind cannot be used in this case. Current implementation replaces include parameters only in text nodes in XML, attributes are completely ignored (so value attribute of bind element would be ignored the same way as attributes in if, for, or similar). Or could you provide some working example?

@harawata harawata added the enhancement Improve a feature or add a new feature label Sep 7, 2017
@harawata harawata added this to the 3.4.6 milestone Sep 7, 2017
pulllock pushed a commit to pulllock/mybatis-3 that referenced this issue Oct 19, 2023
fixes mybatis#1069 Replace placeholders in attribute values when including SQL fragments.
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

4 participants