Skip to content

Commit

Permalink
Merge pull request mybatis#926 from h3adache/master
Browse files Browse the repository at this point in the history
Fix for mybatis#903
  • Loading branch information
h3adache authored Feb 25, 2017
2 parents 586b0d2 + 06bee56 commit ed2cc17
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 47 deletions.
16 changes: 10 additions & 6 deletions src/main/java/org/apache/ibatis/jdbc/AbstractSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,18 +363,22 @@ private String selectSQL(SafeAppendable builder) {
}

sqlClause(builder, "FROM", tables, "", "", ", ");
sqlClause(builder, "JOIN", join, "", "", "\nJOIN ");
sqlClause(builder, "INNER JOIN", innerJoin, "", "", "\nINNER JOIN ");
sqlClause(builder, "OUTER JOIN", outerJoin, "", "", "\nOUTER JOIN ");
sqlClause(builder, "LEFT OUTER JOIN", leftOuterJoin, "", "", "\nLEFT OUTER JOIN ");
sqlClause(builder, "RIGHT OUTER JOIN", rightOuterJoin, "", "", "\nRIGHT OUTER JOIN ");
joins(builder);
sqlClause(builder, "WHERE", where, "(", ")", " AND ");
sqlClause(builder, "GROUP BY", groupBy, "", "", ", ");
sqlClause(builder, "HAVING", having, "(", ")", " AND ");
sqlClause(builder, "ORDER BY", orderBy, "", "", ", ");
return builder.toString();
}

private void joins(SafeAppendable builder) {
sqlClause(builder, "JOIN", join, "", "", "\nJOIN ");
sqlClause(builder, "INNER JOIN", innerJoin, "", "", "\nINNER JOIN ");
sqlClause(builder, "OUTER JOIN", outerJoin, "", "", "\nOUTER JOIN ");
sqlClause(builder, "LEFT OUTER JOIN", leftOuterJoin, "", "", "\nLEFT OUTER JOIN ");
sqlClause(builder, "RIGHT OUTER JOIN", rightOuterJoin, "", "", "\nRIGHT OUTER JOIN ");
}

private String insertSQL(SafeAppendable builder) {
sqlClause(builder, "INSERT INTO", tables, "", "", "");
sqlClause(builder, "", columns, "(", ")", ", ");
Expand All @@ -389,8 +393,8 @@ private String deleteSQL(SafeAppendable builder) {
}

private String updateSQL(SafeAppendable builder) {

sqlClause(builder, "UPDATE", tables, "", "", "");
joins(builder);
sqlClause(builder, "SET", sets, "", "", ", ");
sqlClause(builder, "WHERE", where, "(", ")", " AND ");
return builder.toString();
Expand Down
72 changes: 31 additions & 41 deletions src/test/java/org/apache/ibatis/jdbc/SQLTest.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
/**
* Copyright 2009-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2009-2016 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.jdbc;

import org.hamcrest.CoreMatchers;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

public class SQLTest {

Expand All @@ -26,24 +28,7 @@ public void shouldDemonstrateProvidedStringBuilder() {
//You can pass in your own StringBuilder
final StringBuilder sb = new StringBuilder();
//From the tutorial
final String sql = new SQL() {{
SELECT("P.ID, P.USERNAME, P.PASSWORD, P.FULL_NAME");
SELECT("P.LAST_NAME, P.CREATED_ON, P.UPDATED_ON");
FROM("PERSON P");
FROM("ACCOUNT A");
INNER_JOIN("DEPARTMENT D on D.ID = P.DEPARTMENT_ID");
INNER_JOIN("COMPANY C on D.COMPANY_ID = C.ID");
WHERE("P.ID = A.ID");
WHERE("P.FIRST_NAME like ?");
OR();
WHERE("P.LAST_NAME like ?");
GROUP_BY("P.ID");
HAVING("P.LAST_NAME like ?");
OR();
HAVING("P.FIRST_NAME like ?");
ORDER_BY("P.ID");
ORDER_BY("P.FULL_NAME");
}}.usingAppender(sb).toString();
final String sql = example1().usingAppender(sb).toString();

assertEquals("SELECT P.ID, P.USERNAME, P.PASSWORD, P.FULL_NAME, P.LAST_NAME, P.CREATED_ON, P.UPDATED_ON\n" +
"FROM PERSON P, ACCOUNT A\n" +
Expand Down Expand Up @@ -138,10 +123,10 @@ public void shouldProduceExpectedComplexSelectStatement() {
"HAVING (P.LAST_NAME like ?) \n" +
"OR (P.FIRST_NAME like ?)\n" +
"ORDER BY P.ID, P.FULL_NAME";
assertEquals(expected, example1());
assertEquals(expected, example1().toString());
}

private static String example1() {
private static SQL example1() {
return new SQL() {{
SELECT("P.ID, P.USERNAME, P.PASSWORD, P.FULL_NAME");
SELECT("P.LAST_NAME, P.CREATED_ON, P.UPDATED_ON");
Expand All @@ -159,7 +144,7 @@ private static String example1() {
HAVING("P.FIRST_NAME like ?");
ORDER_BY("P.ID");
ORDER_BY("P.FULL_NAME");
}}.toString();
}};
}

private static String example2(final String id, final String firstName, final String lastName) {
Expand Down Expand Up @@ -214,7 +199,7 @@ public void variableLengthArgumentOnJoin() {
}}.toString();

assertEquals("JOIN TABLE_A b ON b.id = a.id\n" +
"JOIN TABLE_C c ON c.id = a.id", sql);
"JOIN TABLE_C c ON c.id = a.id", sql);
}

@Test
Expand All @@ -224,7 +209,7 @@ public void variableLengthArgumentOnInnerJoin() {
}}.toString();

assertEquals("INNER JOIN TABLE_A b ON b.id = a.id\n" +
"INNER JOIN TABLE_C c ON c.id = a.id", sql);
"INNER JOIN TABLE_C c ON c.id = a.id", sql);
}

@Test
Expand All @@ -234,7 +219,7 @@ public void variableLengthArgumentOnOuterJoin() {
}}.toString();

assertEquals("OUTER JOIN TABLE_A b ON b.id = a.id\n" +
"OUTER JOIN TABLE_C c ON c.id = a.id", sql);
"OUTER JOIN TABLE_C c ON c.id = a.id", sql);
}

@Test
Expand All @@ -244,7 +229,7 @@ public void variableLengthArgumentOnLeftOuterJoin() {
}}.toString();

assertEquals("LEFT OUTER JOIN TABLE_A b ON b.id = a.id\n" +
"LEFT OUTER JOIN TABLE_C c ON c.id = a.id", sql);
"LEFT OUTER JOIN TABLE_C c ON c.id = a.id", sql);
}

@Test
Expand All @@ -254,7 +239,7 @@ public void variableLengthArgumentOnRightOuterJoin() {
}}.toString();

assertEquals("RIGHT OUTER JOIN TABLE_A b ON b.id = a.id\n" +
"RIGHT OUTER JOIN TABLE_C c ON c.id = a.id", sql);
"RIGHT OUTER JOIN TABLE_C c ON c.id = a.id", sql);
}

@Test
Expand Down Expand Up @@ -300,18 +285,23 @@ public void variableLengthArgumentOnSet() {
}}.toString();

assertEquals("UPDATE TABLE_A\n" +
"SET a = #{a}, b = #{b}", sql);
"SET a = #{a}, b = #{b}", sql);
}

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

System.out.println(sql);

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

@Test
public void fixFor903UpdateJoins() {
final SQL sql = new SQL().UPDATE("table1 a").INNER_JOIN("table2 b USING (ID)").SET("a.value = b.value");
assertThat(sql.toString(), CoreMatchers.equalTo("UPDATE table1 a\nINNER JOIN table2 b USING (ID)\nSET a.value = b.value"));
}
}

0 comments on commit ed2cc17

Please sign in to comment.