Skip to content

Conversation

sivdead
Copy link

@sivdead sivdead commented Aug 23, 2018

make AbstratSQL.INSERT_VALUES could be invoke multiple times to do batch insert.

@harawata
Copy link
Member

Thank you for the PR, @sivdead !

It could break backward compatibility, though.
Please see the following test case. It will fail if this PR is merged.

  @Test
  public void variableLengthArgumentOnIntoColumnsAndValues() {
    final String sql = new SQL() {{
      INSERT_INTO("TABLE_A").INTO_COLUMNS("a", "b").INTO_VALUES("#{a}").INTO_VALUES("#{b}");
    }}.toString();
    assertEquals("INSERT INTO TABLE_A\n (a, b)\nVALUES (#{a}, #{b})", sql);
  }

I'm not sure what's the best approach here, but it might be better to add a new method instead of modifying INTO_VALUES.

Cc-ing @h3adache @kazuki43zoo

@kazuki43zoo
Copy link
Member

it might be better to add a new method instead of modifying INTO_VALUES.

I agree with it.

For example, one approach is as follow:

  @Test
  public void multipleRowsInsert() {
    final String sql = new SQL() {{
      INSERT_INTO("TABLE_A")
          .INTO_COLUMNS("a", "b")
          .INTO_VALUES("#{a1}", "#{b1}")
          .ADD_ROW()
          .INTO_VALUES("#{a2}", "#{b2}")
      ;
    }}.toString();

    assertEquals("INSERT INTO TABLE_A\n (a, b)\nVALUES \n (#{a1}, #{b1}),\n (#{a2}, #{b2})", sql);
  }

NOTE: Multiple rows insert style is different by database vender and versions.

@sivdead
Copy link
Author

sivdead commented Oct 17, 2018

it might be better to add a new method instead of modifying INTO_VALUES.

I agree with it.

For example, one approach is as follow:

  @Test
  public void multipleRowsInsert() {
    final String sql = new SQL() {{
      INSERT_INTO("TABLE_A")
          .INTO_COLUMNS("a", "b")
          .INTO_VALUES("#{a1}", "#{b1}")
          .ADD_ROW()
          .INTO_VALUES("#{a2}", "#{b2}")
      ;
    }}.toString();

    assertEquals("INSERT INTO TABLE_A\n (a, b)\nVALUES \n (#{a1}, #{b1}),\n (#{a2}, #{b2})", sql);
  }

NOTE: Multiple rows insert style is different by database vender and versions.

thanks for the advice,so I add a method ADD_ROW to avoid that multi INTO_VALUES in single insert problem.
For example:

  @Test
  public void batchInsertWithMultipleInsertValues(){
    final String sql = new SQL() {{
      INSERT_INTO("TABLE_A");
      INTO_COLUMNS("a", "b");
      INTO_VALUES("#{a1}");
      INTO_VALUES("#{b1}");
      ADD_ROW();
      INTO_VALUES("#{a2}");
      INTO_VALUES("#{b2}");
    }}.toString();
    System.out.println(sql);
    assertEquals("INSERT INTO TABLE_A\n (a, b)\nVALUES \n (#{a1}, #{b1}),\n (#{a2}, #{b2})", sql);
  }

I see that batch insert in oracle is different than others,but I don't know how to get database type or version in sqlBuilder-,-

@harawata
Copy link
Member

Thank you for the update, @sivdead !

@kazuki43zoo ,
Does ADD_ROW() look OK to you?
According to Wikipedia, this syntax is the standard one, so I personally think it's OK even if it does not work with all DBs.

I may request a few minor code changes later.

@harawata harawata changed the title support batch insert in AbstractSQL Add multi-row insert support to SQL builder Oct 18, 2018
@kazuki43zoo
Copy link
Member

Hi @harawata , sorry for late reply.

Does ADD_ROW() look OK to you?

I think it is OK.

According to Wikipedia, this syntax is the standard one, so I personally think it's OK even if it does not work with all DBs.

I thinks so, too!

Copy link
Member

@harawata harawata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @sivdead ,
I'm sorry about the delay! There were some high priority issues.
I have added a few comments.
Please take a look when you have time.

@kazuki43zoo ,
Thanks! Feel free to add comments if you noticed anything.

@kazuki43zoo kazuki43zoo added the enhancement Improve a feature or add a new feature label Apr 14, 2019
@sivdead
Copy link
Author

sivdead commented Jun 21, 2019

Hi @sivdead ,
I'm sorry about the delay! There were some high priority issues.
I have added a few comments.
Please take a look when you have time.

@kazuki43zoo ,
Thanks! Feel free to add comments if you noticed anything.

Hi @sivdead ,
I'm sorry about the delay! There were some high priority issues.
I have added a few comments.
Please take a look when you have time.

@kazuki43zoo ,
Thanks! Feel free to add comments if you noticed anything.

thanks for correcting my mistake! and also sorry about the delay reply :(

jingwenlqh and others added 2 commits June 21, 2019 13:25
# Conflicts:
#	src/main/java/org/apache/ibatis/jdbc/AbstractSQL.java
#	src/test/java/org/apache/ibatis/jdbc/SQLTest.java
@harawata
Copy link
Member

Thank you for the update, @sivdead !
I have resolved the conflict and made a few minor adjustment.

@kazuki43zoo ,
Could you do the final check and merge if everything is OK?
Thanks in advance!

Copy link
Member

@kazuki43zoo kazuki43zoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sivdead

I've added some small comments. If you have a time, please check it.

@kazuki43zoo kazuki43zoo added this to the 3.5.2 milestone Jun 22, 2019
@sivdead
Copy link
Author

sivdead commented Jun 23, 2019

@sivdead

I've added some small comments. If you have a time, please check it.

i have do as you suggested,again thank you for helping me a lot on this pr :)

Copy link
Member

@kazuki43zoo kazuki43zoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sivdead Thanks for your rework! But I have some change requests. Please check new comment and replying comments.

@sivdead
Copy link
Author

sivdead commented Jun 26, 2019

@sivdead Thanks for your rework! But I have some change requests. Please check new comment and replying comments.

get it fixed

kazuki43zoo added a commit to kazuki43zoo/mybatis-3 that referenced this pull request Jun 29, 2019
@kazuki43zoo kazuki43zoo merged commit 7476718 into mybatis:master Jun 29, 2019
@kazuki43zoo kazuki43zoo self-assigned this Jun 29, 2019
kazuki43zoo added a commit that referenced this pull request Jun 29, 2019
@kazuki43zoo kazuki43zoo changed the title Add multi-row insert support to SQL builder Support multi-row insert on SQL builder class Jul 3, 2019
pulllock pushed a commit to pulllock/mybatis-3 that referenced this pull request Oct 19, 2023
pulllock pushed a commit to pulllock/mybatis-3 that referenced this pull request Oct 19, 2023
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

Successfully merging this pull request may close these issues.

4 participants