mirror of https://github.com/alibaba/druid.git
Compare commits
3 Commits
b87f14c30f
...
da466fcd33
Author | SHA1 | Date |
---|---|---|
|
da466fcd33 | |
|
b1fda6d046 | |
|
801c32ae58 |
|
@ -99,6 +99,7 @@ public enum SQLBinaryOperator {
|
|||
BooleanXor("XOR", 150),
|
||||
BooleanOr("OR", 160),
|
||||
Assignment(":=", 169),
|
||||
Blank("", 170),
|
||||
|
||||
PG_And("&&", 140),
|
||||
PG_ST_DISTANCE("<->", 20);
|
||||
|
|
|
@ -820,6 +820,45 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLCreateFunctionStatement x) {
|
||||
boolean create = x.isCreate();
|
||||
if (create) {
|
||||
print0(ucase ? "CREATE " : "create ");
|
||||
|
||||
if (x.isOrReplace()) {
|
||||
print0(ucase ? "OR REPLACE " : "or replace ");
|
||||
}
|
||||
}
|
||||
|
||||
if (x.isTemporary()) {
|
||||
print0(ucase ? "TEMPORARY " : "temporary ");
|
||||
}
|
||||
print0(ucase ? "FUNCTION " : "function ");
|
||||
|
||||
x.getName().accept(this);
|
||||
|
||||
int paramSize = x.getParameters().size();
|
||||
|
||||
print0(" (");
|
||||
this.indentCount++;
|
||||
|
||||
for (int i = 0; i < paramSize; ++i) {
|
||||
if (i != 0) {
|
||||
print0(","); // change: remove extra space
|
||||
println();
|
||||
}
|
||||
SQLParameter param = x.getParameters().get(i);
|
||||
param.accept(this);
|
||||
}
|
||||
|
||||
this.indentCount--;
|
||||
print(')');
|
||||
|
||||
printCreateFunctionBody(x);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLCreateUserStatement x) {
|
||||
print0(ucase ? "CREATE USER " : "create user ");
|
||||
|
|
|
@ -3532,6 +3532,9 @@ public class SQLExprParser extends SQLParser {
|
|||
SQLBinaryOperator operator = andRestGetAndOperator();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, operator, rightExp, dbType);
|
||||
} else if (token == VARIANT) {
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Blank, new SQLVariantRefExpr(lexer.stringVal()), dbType);
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
@ -3977,6 +3980,11 @@ public class SQLExprParser extends SQLParser {
|
|||
return expr;
|
||||
}
|
||||
break;
|
||||
case VARIANT:
|
||||
rightExp = new SQLVariantRefExpr(lexer.stringVal);
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Blank, rightExp, dbType);
|
||||
lexer.nextToken();
|
||||
return expr;
|
||||
default:
|
||||
return expr;
|
||||
}
|
||||
|
|
|
@ -756,6 +756,7 @@ public class SQLSelectParser extends SQLParser {
|
|||
case BANGEQ:
|
||||
case LIKE:
|
||||
case NOT:
|
||||
case VARIANT:
|
||||
where = this.exprParser.relationalRest(where);
|
||||
break;
|
||||
default:
|
||||
|
|
|
@ -1048,7 +1048,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
|||
&& ((SQLIdentifierExpr) right).getName().equalsIgnoreCase("NOTFOUND")) {
|
||||
printOpSpace = false;
|
||||
}
|
||||
if (printOpSpace) {
|
||||
if (printOpSpace && operator != SQLBinaryOperator.Blank) {
|
||||
print(' ');
|
||||
}
|
||||
}
|
||||
|
@ -12510,7 +12510,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
|||
return;
|
||||
}
|
||||
println();
|
||||
println(ucase ? "AS" : "as");
|
||||
print(ucase ? "AS " : "as ");
|
||||
block.accept(this);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
package com.alibaba.druid.bvt.sql.postgresql.create_function;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.DbType;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNullExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLBlockStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateFunctionStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprStatement;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class PGCreateFunctionTest extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
SQLCreateFunctionStatement sqlCreateFunctionStatement = new SQLCreateFunctionStatement();
|
||||
sqlCreateFunctionStatement.setDbType(DbType.postgresql);
|
||||
sqlCreateFunctionStatement.setCreate(true);
|
||||
sqlCreateFunctionStatement.setName(new SQLIdentifierExpr("func1"));
|
||||
SQLBlockStatement sqlBlockStatement = new SQLBlockStatement();
|
||||
sqlCreateFunctionStatement.setBlock(sqlBlockStatement);
|
||||
|
||||
// Set sql block statement
|
||||
SQLExprStatement sqlExprStatement = new SQLExprStatement();
|
||||
sqlExprStatement.setExpr(new SQLNullExpr());
|
||||
sqlExprStatement.setAfterSemi(true);
|
||||
sqlBlockStatement.setHaveBeginEnd(true);
|
||||
sqlBlockStatement.setIsDollarQuoted(true);
|
||||
sqlBlockStatement.setLanguage("plpgsql");
|
||||
List<SQLStatement> sqlExprStatementList = new ArrayList<>();
|
||||
sqlExprStatementList.add(sqlExprStatement);
|
||||
sqlBlockStatement.setStatementList(sqlExprStatementList);
|
||||
|
||||
String sql = sqlCreateFunctionStatement.toString();
|
||||
assertEquals("CREATE FUNCTION func1 ()\n" +
|
||||
"AS $$\n" +
|
||||
"BEGIN\n" +
|
||||
"\tNULL;\n" +
|
||||
"END;\n" +
|
||||
"$$ LANGUAGE plpgsql;", sql);
|
||||
}
|
||||
}
|
|
@ -1,3 +1,14 @@
|
|||
select c from b where c=1 ${if(len(a)=0,'and 1=1',"and a>0")} ${if(len(a)=0,'and 1=1',"and a>0")}
|
||||
--------------------
|
||||
SELECT c
|
||||
FROM b
|
||||
WHERE c = 1 ${if(len(a)=0,'and 1=1',"and a>0")} ${if(len(a)=0,'and 1=1',"and a>0")}
|
||||
------------------------------------------------------------------------------------------------------------------------
|
||||
select ${if(len(a)=0,' 1=1 ',"c")}, ${if(len(a)=0,' 1=1 ',"c")} from b
|
||||
--------------------
|
||||
SELECT ${if(len(a)=0,' 1=1 ',"c")}, ${if(len(a)=0,' 1=1 ',"c")}
|
||||
FROM b
|
||||
------------------------------------------------------------------------------------------------------------------------
|
||||
--test
|
||||
select a from b
|
||||
--------------------
|
||||
|
|
Loading…
Reference in New Issue