mirror of https://github.com/alibaba/druid.git
Compare commits
4 Commits
f6e9c16197
...
434349d77d
| Author | SHA1 | Date |
|---|---|---|
|
|
434349d77d | |
|
|
ac484261c0 | |
|
|
2f11505ce7 | |
|
|
924cd088b7 |
|
|
@ -39,6 +39,7 @@ public class CKLexer extends Lexer {
|
|||
map.put("FINAL", Token.FINAL);
|
||||
map.put("TTL", Token.TTL);
|
||||
map.put("CODEC", Token.CODEC);
|
||||
map.remove("ANY");
|
||||
|
||||
return new Keywords(map);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import com.alibaba.druid.DbType;
|
|||
import com.alibaba.druid.sql.ast.SQLPartitionBy;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGOutputVisitor;
|
||||
import com.alibaba.druid.sql.parser.CharTypes;
|
||||
import com.alibaba.druid.sql.visitor.VisitorFeature;
|
||||
|
||||
public class HologresOutputVisitor extends PGOutputVisitor {
|
||||
public HologresOutputVisitor(StringBuilder appender, boolean parameterized) {
|
||||
|
|
@ -27,4 +29,53 @@ public class HologresOutputVisitor extends PGOutputVisitor {
|
|||
print0(ucase ? "PARTITION BY " : "partition by ");
|
||||
partitionBy.accept(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printComment(String comment) {
|
||||
if (comment == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isEnabled(VisitorFeature.OutputSkipMultilineComment) && comment.startsWith("/*")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isEnabled(VisitorFeature.OutputSkipSingleLineComment)
|
||||
&& (comment.startsWith("-") || comment.startsWith("#"))) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (comment.startsWith("--")
|
||||
&& comment.length() > 2
|
||||
&& comment.charAt(2) != ' '
|
||||
&& comment.charAt(2) != '-') {
|
||||
print0("-- ");
|
||||
print0(comment.substring(2));
|
||||
} else if (comment.startsWith("#")
|
||||
&& comment.length() > 1
|
||||
&& comment.charAt(1) != ' '
|
||||
&& comment.charAt(1) != '#') {
|
||||
print0("# ");
|
||||
print0(comment.substring(1));
|
||||
} else if (comment.startsWith("/*")) {
|
||||
println();
|
||||
print0(comment);
|
||||
} else if (comment.startsWith("--")) {
|
||||
print0(comment);
|
||||
}
|
||||
|
||||
char first = '\0';
|
||||
for (int i = 0; i < comment.length(); i++) {
|
||||
char c = comment.charAt(i);
|
||||
if (CharTypes.isWhitespace(c)) {
|
||||
continue;
|
||||
}
|
||||
first = c;
|
||||
break;
|
||||
}
|
||||
|
||||
if (first == '-' || first == '#') {
|
||||
endLineComment = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -787,6 +787,12 @@ public class PGSQLStatementParser extends SQLStatementParser {
|
|||
lexer.nextToken();
|
||||
accept(Token.NULL);
|
||||
alterColumn.setSetNotNull(true);
|
||||
} else if (lexer.identifierEquals("DATA")) {
|
||||
// alter column ... set data type ...
|
||||
lexer.nextToken();
|
||||
accept(Token.TYPE);
|
||||
SQLDataType dataType = this.exprParser.parseDataType();
|
||||
alterColumn.setDataType(dataType);
|
||||
} else {
|
||||
accept(Token.DEFAULT);
|
||||
SQLExpr defaultValue = this.exprParser.expr();
|
||||
|
|
|
|||
|
|
@ -2527,10 +2527,21 @@ public class Lexer {
|
|||
}
|
||||
|
||||
if (ch == '*' && charAt(pos + 1) == '/') {
|
||||
scanChar();
|
||||
scanChar();
|
||||
if (0 == --depth) {
|
||||
break;
|
||||
int curPos = pos;
|
||||
boolean terminated = true;
|
||||
// If '*/' has leading '--' in the same line, just skip it. For example '-- xxxx */'.
|
||||
while (curPos > 0 && charAt(curPos) != '\n') {
|
||||
if (charAt(curPos) == '-' && (curPos + 1) < text.length() && charAt(curPos + 1) == '-') {
|
||||
terminated = false;
|
||||
}
|
||||
curPos--;
|
||||
}
|
||||
if (terminated) {
|
||||
scanChar();
|
||||
scanChar();
|
||||
if (0 == --depth) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -105,7 +105,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
|||
|
||||
protected transient int lines;
|
||||
private TimeZone timeZone;
|
||||
private boolean endLineComment;
|
||||
protected boolean endLineComment;
|
||||
|
||||
protected SQLDialect dialect;
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,108 @@
|
|||
package com.alibaba.druid.sql.dialect.postgresql.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author gaattc
|
||||
* @since 1.2.24
|
||||
*/
|
||||
public class PGSQLStatementParserTest {
|
||||
|
||||
/**
|
||||
* ALTER COLUMN ... SET DATA TYPE ...
|
||||
*/
|
||||
@Test
|
||||
public void testAlterColumnSetDataType() {
|
||||
String sql = "alter table if exists products alter column price set data type decimal(12,2)";
|
||||
|
||||
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
SQLAlterTableStatement stmt = (SQLAlterTableStatement) statementList.get(0);
|
||||
assertEquals(SQLAlterTableStatement.class, stmt.getClass());
|
||||
|
||||
assertEquals("products", stmt.getTableName());
|
||||
assertTrue(stmt.isIfExists());
|
||||
|
||||
SQLAlterTableAlterColumn alterColumnItem = (SQLAlterTableAlterColumn) stmt.getItems().get(0);
|
||||
assertNotNull(alterColumnItem);
|
||||
|
||||
assertEquals("price", alterColumnItem.getColumn().getName().getSimpleName());
|
||||
assertNotNull(alterColumnItem.getDataType());
|
||||
assertEquals("decimal", alterColumnItem.getDataType().getName().toLowerCase());
|
||||
assertEquals(2, alterColumnItem.getDataType().getArguments().size());
|
||||
assertEquals("12", alterColumnItem.getDataType().getArguments().get(0).toString());
|
||||
assertEquals("2", alterColumnItem.getDataType().getArguments().get(1).toString());
|
||||
|
||||
String outputSql = stmt.toString();
|
||||
assertEquals("ALTER TABLE IF EXISTS products\n\tALTER COLUMN price SET DATA TYPE decimal(12, 2)", outputSql);
|
||||
}
|
||||
|
||||
/**
|
||||
* ALTER COLUMN ... SET DEFAULT ...
|
||||
*/
|
||||
@Test
|
||||
public void testAlterColumnSetDefault() {
|
||||
String sql = "ALTER TABLE products ALTER COLUMN price SET DEFAULT 7.77";
|
||||
|
||||
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLAlterTableStatement stmt = (SQLAlterTableStatement) statementList.get(0);
|
||||
|
||||
SQLAlterTableAlterColumn alterColumnItem = (SQLAlterTableAlterColumn) stmt.getItems().get(0);
|
||||
|
||||
assertEquals("price", alterColumnItem.getColumn().getName().getSimpleName());
|
||||
assertNotNull(alterColumnItem.getSetDefault());
|
||||
assertEquals("7.77", alterColumnItem.getSetDefault().toString());
|
||||
assertNull(alterColumnItem.getDataType());
|
||||
}
|
||||
|
||||
/**
|
||||
* ALTER COLUMN ... SET NOT NULL
|
||||
*/
|
||||
@Test
|
||||
public void testAlterColumnSetNotNull() {
|
||||
String sql = "ALTER TABLE products ALTER COLUMN price SET NOT NULL";
|
||||
|
||||
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLAlterTableStatement stmt = (SQLAlterTableStatement) statementList.get(0);
|
||||
|
||||
SQLAlterTableAlterColumn alterColumnItem = (SQLAlterTableAlterColumn) stmt.getItems().get(0);
|
||||
|
||||
assertEquals("price", alterColumnItem.getColumn().getName().getSimpleName());
|
||||
assertTrue(alterColumnItem.isSetNotNull());
|
||||
assertNull(alterColumnItem.getDataType());
|
||||
}
|
||||
|
||||
/**
|
||||
* DROP
|
||||
*/
|
||||
@Test
|
||||
public void testAlterColumnDropDefault() {
|
||||
String sql = "ALTER TABLE products ALTER COLUMN price DROP DEFAULT";
|
||||
|
||||
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLAlterTableStatement stmt = (SQLAlterTableStatement) statementList.get(0);
|
||||
|
||||
SQLAlterTableAlterColumn alterColumnItem = (SQLAlterTableAlterColumn) stmt.getItems().get(0);
|
||||
|
||||
assertEquals("price", alterColumnItem.getColumn().getName().getSimpleName());
|
||||
assertTrue(alterColumnItem.isDropDefault());
|
||||
assertNull(alterColumnItem.getDataType());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,3 +1,33 @@
|
|||
select a from test
|
||||
-- origin
|
||||
-- ${if(gFinPeriod="季度","/*","")}
|
||||
-- origin end
|
||||
-- new
|
||||
/*
|
||||
-- new end
|
||||
where 1=1
|
||||
-- origin
|
||||
-- ${if(gFinPeriod="季度","","*/")}
|
||||
-- origin end
|
||||
-- new
|
||||
*/
|
||||
--new end
|
||||
--------------------
|
||||
SELECT a
|
||||
FROM test -- origin
|
||||
-- ${if(gFinPeriod="季度","/*","")}
|
||||
-- origin end
|
||||
-- new
|
||||
/*
|
||||
-- new end
|
||||
where 1=1
|
||||
-- origin
|
||||
-- ${if(gFinPeriod="季度","","*/")}
|
||||
-- origin end
|
||||
-- new
|
||||
*/
|
||||
-- new end
|
||||
------------------------------------------------------------------------------------------------------------------------
|
||||
SELECT sum(ret_amount) AS '应收金额'
|
||||
FROM test.test
|
||||
GROUP BY a ORDER BY '应收金额' DESC;
|
||||
|
|
|
|||
Loading…
Reference in New Issue