From f577b9ae5e1c8211d1379e5dfb7dd49ca743c62e Mon Sep 17 00:00:00 2001 From: duncan_zhou Date: Thu, 19 Sep 2019 13:29:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DPGOutputVisitor=20=E8=BE=93?= =?UTF-8?q?=E5=87=BAPGInsertStatementParser=E7=9A=84=E6=97=B6=E5=80=99on?= =?UTF-8?q?=20Conflict=20Update=E8=AF=AD=E6=B3=95=E7=9A=84=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../postgresql/visitor/PGOutputVisitor.java | 2 +- .../druid/postgresql/PGUpsertTest.java | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/alibaba/druid/postgresql/PGUpsertTest.java diff --git a/src/main/java/com/alibaba/druid/sql/dialect/postgresql/visitor/PGOutputVisitor.java b/src/main/java/com/alibaba/druid/sql/dialect/postgresql/visitor/PGOutputVisitor.java index 7f3ce12afb..c1dc471e6c 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/postgresql/visitor/PGOutputVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/postgresql/visitor/PGOutputVisitor.java @@ -455,7 +455,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor if (onConflictDoNothing) { print0(ucase ? " DO NOTHING" : " do nothing"); } else if ((onConflictUpdateSetItems != null && onConflictUpdateSetItems.size() > 0)) { - print0(ucase ? " UPDATE SET " : " update set "); + print0(ucase ? " DO UPDATE SET " : " do update set "); printAndAccept(onConflictUpdateSetItems, ", "); } } diff --git a/src/test/java/com/alibaba/druid/postgresql/PGUpsertTest.java b/src/test/java/com/alibaba/druid/postgresql/PGUpsertTest.java new file mode 100644 index 0000000000..a09db92d0e --- /dev/null +++ b/src/test/java/com/alibaba/druid/postgresql/PGUpsertTest.java @@ -0,0 +1,20 @@ +package com.alibaba.druid.postgresql; + +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser; +import junit.framework.TestCase; +import org.junit.Assert; + +public class PGUpsertTest extends TestCase { + + public void testUpsert(){ + String sql = "insert into \"test_dup\" values(1,'2',-100) on conflict(id) do update set \"count\" = test_dup.\"count\" + 1;"; + String targetSql = "INSERT INTO \"test_dup\"\n" + + "VALUES (1, '2', -100)\n" + + "ON CONFLICT (id) DO UPDATE SET \"count\" = test_dup.\"count\" + 1"; + PGSQLStatementParser parser=new PGSQLStatementParser(sql); + SQLStatement statement = parser.parseStatement(); + Assert.assertEquals(targetSql, statement.toString()); + } + +}