mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -23,6 +23,7 @@ import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLObjectImpl;
|
||||
import com.alibaba.druid.sql.ast.SQLStatementImpl;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLLiteralExpr;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
@@ -77,6 +78,10 @@ public class SQLCreateViewStatement extends SQLStatementImpl implements SQLDDLSt
|
||||
this.setTableSource(new SQLExprTableSource(name));
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.setName(new SQLIdentifierExpr(name));
|
||||
}
|
||||
|
||||
public SQLExprTableSource getTableSource() {
|
||||
return tableSource;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLReplaceable;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplaceable {
|
||||
@@ -78,6 +79,26 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
|
||||
this.left = left;
|
||||
}
|
||||
|
||||
public void setLeft(String tableName) {
|
||||
SQLExprTableSource tableSource;
|
||||
if (tableName == null || tableName.length() == 0) {
|
||||
tableSource = null;
|
||||
} else {
|
||||
tableSource = new SQLExprTableSource(new SQLIdentifierExpr(tableName));
|
||||
}
|
||||
this.setLeft(tableSource);
|
||||
}
|
||||
|
||||
public void setRight(String tableName) {
|
||||
SQLExprTableSource tableSource;
|
||||
if (tableName == null || tableName.length() == 0) {
|
||||
tableSource = null;
|
||||
} else {
|
||||
tableSource = new SQLExprTableSource(new SQLIdentifierExpr(tableName));
|
||||
}
|
||||
this.setRight(tableSource);
|
||||
}
|
||||
|
||||
public SQLTableSource getRight() {
|
||||
return this.right;
|
||||
}
|
||||
|
||||
@@ -162,6 +162,16 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
public void setFrom(String tableName) {
|
||||
SQLExprTableSource from;
|
||||
if (tableName == null || tableName.length() == 0) {
|
||||
from = null;
|
||||
} else {
|
||||
from = new SQLExprTableSource(new SQLIdentifierExpr(tableName));
|
||||
}
|
||||
this.setFrom(from);
|
||||
}
|
||||
|
||||
public boolean isParenthesized() {
|
||||
return parenthesized;
|
||||
}
|
||||
|
||||
+1006
File diff suppressed because it is too large
Load Diff
+101
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.mysql.visitor.transform;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectJoin;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectSubqueryTableSource;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitorAdapter;
|
||||
import oracle.sql.SQLName;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author wenshao[szujobs@hotmail.com]
|
||||
*/
|
||||
public class FromSubqueryResolver extends OracleASTVisitorAdapter {
|
||||
private final List<SQLStatement> targetList;
|
||||
private final String viewName;
|
||||
|
||||
public FromSubqueryResolver(List<SQLStatement> targetList, String viewName) {
|
||||
this.targetList = targetList;
|
||||
this.viewName = viewName;
|
||||
}
|
||||
|
||||
public boolean visit(OracleSelectSubqueryTableSource x) {
|
||||
return visit((SQLSubqueryTableSource) x);
|
||||
}
|
||||
|
||||
public boolean visit(SQLSubqueryTableSource x) {
|
||||
String subViewName = generateSubViewName();
|
||||
|
||||
SQLObject parent = x.getParent();
|
||||
if(parent instanceof SQLSelectQueryBlock) {
|
||||
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) parent;
|
||||
queryBlock.setFrom(subViewName);
|
||||
} else if(parent instanceof SQLJoinTableSource) {
|
||||
SQLJoinTableSource join = (SQLJoinTableSource) parent;
|
||||
if (join.getLeft() == x) {
|
||||
join.setLeft(subViewName);
|
||||
} else if (join.getRight() == x) {
|
||||
join.setRight(subViewName);
|
||||
}
|
||||
}
|
||||
|
||||
SQLCreateViewStatement stmt = new SQLCreateViewStatement();
|
||||
|
||||
stmt.setName(generateSubViewName());
|
||||
|
||||
|
||||
SQLSelect select = x.getSelect();
|
||||
stmt.setSubQuery(select);
|
||||
|
||||
targetList.add(0, stmt);
|
||||
|
||||
stmt.accept(new FromSubqueryResolver(targetList, viewName));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private String generateSubViewName() {
|
||||
return this.viewName + "_" + targetList.size();
|
||||
}
|
||||
|
||||
public static List<SQLStatement> resolve(SQLCreateViewStatement stmt) {
|
||||
List<SQLStatement> targetList = new ArrayList<SQLStatement>();
|
||||
targetList.add(stmt);
|
||||
|
||||
String viewName = SQLUtils.normalize(stmt.getName().getSimpleName());
|
||||
|
||||
FromSubqueryResolver visitor = new FromSubqueryResolver(targetList, viewName);
|
||||
stmt.accept(visitor);
|
||||
|
||||
String dbType = stmt.getDbType();
|
||||
for (int i = 0; i < targetList.size() - 1; ++i) {
|
||||
SQLCreateViewStatement targetStmt = (SQLCreateViewStatement) targetList.get(i);
|
||||
targetStmt.setDbType(dbType);
|
||||
targetStmt.setAfterSemi(true);
|
||||
}
|
||||
|
||||
|
||||
return targetList;
|
||||
}
|
||||
}
|
||||
@@ -156,6 +156,19 @@ public class OdpsExprParser extends SQLExprParser {
|
||||
return super.equalityRest(expr);
|
||||
}
|
||||
|
||||
public SQLExpr relationalRest(SQLExpr expr) {
|
||||
if (identifierEquals("REGEXP")) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
return new SQLBinaryOpExpr(expr, SQLBinaryOperator.RegExp, rightExp, JdbcConstants.MYSQL);
|
||||
}
|
||||
|
||||
return super.relationalRest(expr);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OdpsSelectParser createSelectParser() {
|
||||
return new OdpsSelectParser(this);
|
||||
|
||||
+9
@@ -76,6 +76,7 @@ public class OracleCreateTableStatement extends SQLCreateTableStatement implemen
|
||||
|
||||
private SQLName of;
|
||||
private OIDIndex oidIndex;
|
||||
private boolean monitoring;
|
||||
|
||||
public OracleCreateTableStatement() {
|
||||
super (JdbcConstants.ORACLE);
|
||||
@@ -262,6 +263,14 @@ public class OracleCreateTableStatement extends SQLCreateTableStatement implemen
|
||||
this.oidIndex = oidIndex;
|
||||
}
|
||||
|
||||
public boolean isMonitoring() {
|
||||
return monitoring;
|
||||
}
|
||||
|
||||
public void setMonitoring(boolean monitoring) {
|
||||
this.monitoring = monitoring;
|
||||
}
|
||||
|
||||
public boolean isCompressForOltp() {
|
||||
return compressForOltp;
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ package com.alibaba.druid.sql.dialect.oracle.ast.stmt;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
@@ -119,4 +121,24 @@ public class OracleSelectJoin extends SQLJoinTableSource implements OracleSelect
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setLeft(String tableName) {
|
||||
SQLExprTableSource tableSource;
|
||||
if (tableName == null || tableName.length() == 0) {
|
||||
tableSource = null;
|
||||
} else {
|
||||
tableSource = new OracleSelectTableReference(new SQLIdentifierExpr(tableName));
|
||||
}
|
||||
this.setLeft(tableSource);
|
||||
}
|
||||
|
||||
public void setRight(String tableName) {
|
||||
SQLExprTableSource tableSource;
|
||||
if (tableName == null || tableName.length() == 0) {
|
||||
tableSource = null;
|
||||
} else {
|
||||
tableSource = new OracleSelectTableReference(new SQLIdentifierExpr(tableName));
|
||||
}
|
||||
this.setRight(tableSource);
|
||||
}
|
||||
}
|
||||
|
||||
+12
@@ -23,7 +23,9 @@ import com.alibaba.druid.sql.ast.SQLCommentHint;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLLimit;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;
|
||||
@@ -161,4 +163,14 @@ public class OracleSelectQueryBlock extends SQLSelectQueryBlock {
|
||||
throw new UnsupportedOperationException("not support offset");
|
||||
}
|
||||
}
|
||||
|
||||
public void setFrom(String tableName) {
|
||||
SQLExprTableSource from;
|
||||
if (tableName == null || tableName.length() == 0) {
|
||||
from = null;
|
||||
} else {
|
||||
from = new OracleSelectTableReference(new SQLIdentifierExpr(tableName));
|
||||
}
|
||||
this.setFrom(from);
|
||||
}
|
||||
}
|
||||
|
||||
+5
@@ -16,6 +16,7 @@
|
||||
package com.alibaba.druid.sql.dialect.oracle.ast.stmt;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.clause.PartitionExtensionClause;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SampleClause;
|
||||
@@ -34,6 +35,10 @@ public class OracleSelectTableReference extends SQLExprTableSource implements Or
|
||||
|
||||
}
|
||||
|
||||
public OracleSelectTableReference(SQLExpr expr) {
|
||||
this.setExpr(expr);
|
||||
}
|
||||
|
||||
public PartitionExtensionClause getPartition() {
|
||||
return partition;
|
||||
}
|
||||
|
||||
@@ -150,6 +150,10 @@ public class OracleCreateTableParser extends SQLCreateTableParser {
|
||||
// OracleStorageClause storage = ((OracleExprParser) this.exprParser).parseStorage();
|
||||
// stmt.setStorage(storage);
|
||||
// continue;
|
||||
} else if (identifierEquals("MONITORING")) {
|
||||
lexer.nextToken();
|
||||
stmt.setMonitoring(true);
|
||||
continue;
|
||||
} else if (lexer.token() == Token.LOB) {
|
||||
OracleLobStorageClause lobStorage = ((OracleExprParser) this.exprParser).parseLobStorage();
|
||||
stmt.setLobStorage(lobStorage);
|
||||
|
||||
@@ -2137,6 +2137,11 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
|
||||
print0(ucase ? "PRESERVE ROWS" : "preserve rows");
|
||||
}
|
||||
|
||||
if (x.isMonitoring()) {
|
||||
println();
|
||||
print0(ucase ? "MONITORING" : "monitoring");
|
||||
}
|
||||
|
||||
if (x.getPartitioning() != null) {
|
||||
println();
|
||||
x.getPartitioning().accept(this);
|
||||
|
||||
@@ -2009,12 +2009,15 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
}
|
||||
|
||||
protected void printCreateTable(SQLCreateTableStatement x) {
|
||||
print0(ucase ? "CREATE TABLE " : "create table ");
|
||||
if (SQLCreateTableStatement.Type.GLOBAL_TEMPORARY.equals(x.getType())) {
|
||||
print0(ucase ? "CREATE " : "create ");
|
||||
|
||||
final SQLCreateTableStatement.Type tableType = x.getType();
|
||||
if (SQLCreateTableStatement.Type.GLOBAL_TEMPORARY.equals(tableType)) {
|
||||
print0(ucase ? "GLOBAL TEMPORARY " : "global temporary ");
|
||||
} else if (SQLCreateTableStatement.Type.LOCAL_TEMPORARY.equals(x.getType())) {
|
||||
} else if (SQLCreateTableStatement.Type.LOCAL_TEMPORARY.equals(tableType)) {
|
||||
print0(ucase ? "LOCAL TEMPORARY " : "local temporary ");
|
||||
}
|
||||
print0(ucase ? "TABLE " : "table ");
|
||||
|
||||
printTableSourceExpr(x.getName());
|
||||
|
||||
|
||||
+23
-26
@@ -1,26 +1,23 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.mysql.visitor;
|
||||
|
||||
|
||||
public class MySql2OracleOutputVisitor extends MySqlOutputVisitor {
|
||||
|
||||
public MySql2OracleOutputVisitor(Appendable appender){
|
||||
super(appender);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package com.alibaba.druid.sql.visitor;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 16/07/2017.
|
||||
*/
|
||||
public interface SQLTransformVisitor extends SQLASTVisitor {
|
||||
String getSrcDbType();
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlSelectParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySql2OracleOutputVisitor;
|
||||
import com.alibaba.druid.sql.parser.SQLSelectParser;
|
||||
|
||||
public class SQLParserTest extends TestCase {
|
||||
|
||||
public void test_select() throws Exception {
|
||||
String sql = " SELECT COUNT(*) FROM close_plan WHERE 1=1 AND close_type = ? AND target_type = ? AND target_id = ? AND( mi_name=? ) AND end_time >= ? ";
|
||||
SQLSelectParser parser = new MySqlSelectParser(sql);
|
||||
SQLSelect select = parser.select();
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
MySql2OracleOutputVisitor visitor = new MySql2OracleOutputVisitor(out);
|
||||
|
||||
select.accept(visitor);
|
||||
|
||||
System.out.println(out);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.alibaba.druid.bvt.sql.mysql.transform;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.transform.FromSubqueryResolver;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 16/07/2017.
|
||||
*/
|
||||
public class Trans_CreateView_0 extends TestCase {
|
||||
public void test_createView() throws Exception {
|
||||
String sql = "CREATE OR REPLACE FORCE VIEW \"CMIS\".\"WFI_AGENT_LOGEND\" (\"INSTANCEID\", \"WFID\", \"WFSIGN\", \"NODEID\", \"NODESTARTTIME\", \"ORIGINALUSER\", \"REPLACER\", \"SYSID\", \"PK_VALUE\", \"APPL_TYPE\", \"WFI_END_ORG\", \"WFI_STATUS\") AS \n" +
|
||||
" SELECT INSTANCEID,WFID,WFSIGN,NODEID,NODESTARTTIME,ORIGINALUSER,REPLACER,SYSID,PK_VALUE,APPL_TYPE,WFI_END_ORG,WFI_STATUS FROM (SELECT ROWID ROWIDTMP, AA.* FROM (SELECT A.INSTANCEID INSTANCEID,A.WFID WFID,A.WFSIGN WFSIGN,A.NODEID NODEID,A.NODESTARTTIME NODESTARTTIME,A.ORIGINALUSER ORIGINALUSER,A.REPLACER REPLACER,A.SYSID SYSID,B.PK_VALUE PK_VALUE,B.APPL_TYPE APPL_TYPE,B.WFI_END_ORG WFI_END_ORG,B.WFI_STATUS WFI_STATUS FROM WF_AGENT_LOG A, WFI_JOIN_HIS B WHERE A.INSTANCEID=B.INSTANCEID) AA) WHERE ROWIDTMP IN (SELECT ROWIDMAX FROM ( SELECT MAX(ROWID) ROWIDMAX,INSTANCEID FROM(SELECT A.INSTANCEID INSTANCEID FROM WF_AGENT_LOG A, WFI_JOIN_HIS B WHERE A.INSTANCEID=B.INSTANCEID) AA GROUP BY INSTANCEID ))\n" +
|
||||
" WITH READ ONLY";
|
||||
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
|
||||
assertEquals(1, stmtList.size());
|
||||
|
||||
SQLStatement stmt = stmtList.get(0);
|
||||
List<SQLStatement> targetList = FromSubqueryResolver.resolve((SQLCreateViewStatement) stmt);
|
||||
String targetSql = SQLUtils.toSQLString(targetList, JdbcConstants.ORACLE);
|
||||
assertEquals(5, targetList.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.alibaba.druid.bvt.sql.mysql.transform;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.transform.FromSubqueryResolver;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 16/07/2017.
|
||||
*/
|
||||
public class Trans_CreateView_1 extends TestCase {
|
||||
public void test_createView() throws Exception {
|
||||
String sql = " CREATE OR REPLACE FORCE VIEW \"CMIS\".\"LM_LOAN_MSGINFO\" (\"LOAN_NO\", \"OD_PRCP\", \"OD_DAYS\", \"OD_TNR\", \"ENTRYCS_DAYS\") AS \n" +
|
||||
" select loan.loan_no as loan_no,coln.od_prcp,\n" +
|
||||
"to_date((select cur_prcs_dt from s_ctrl),'YYYY-MM-DD')-to_date(loan.next_due_dt,'YYYY-MM-DD') as od_days,\n" +
|
||||
"shd.od_tnr as od_tnr,\n" +
|
||||
"to_date((select cur_prcs_dt from s_ctrl),'YYYY-MM-DD')-to_date(coln.entry_dt,'YYYY-MM-DD') as entrycs_days from lm_loan loan\n" +
|
||||
"left join (select count(*) as od_tnr,loan_no\n" +
|
||||
" from lm_pm_shd\n" +
|
||||
" where ps_due_dt < (select cur_prcs_dt from s_ctrl)\n" +
|
||||
" and setl_ind = 'N'\n" +
|
||||
" and ps_od_ind = 'Y'\n" +
|
||||
" and ps_perd_no <> 0 group by loan_no) shd on shd.loan_no=loan.loan_no\n" +
|
||||
"left join cs_coln coln on loan.loan_no = coln.loan_no\n" +
|
||||
"where loan.LOAN_OD_IND='Y' and loan.LOAN_STS='ACTV'";
|
||||
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
|
||||
assertEquals(1, stmtList.size());
|
||||
|
||||
SQLStatement stmt = stmtList.get(0);
|
||||
List<SQLStatement> targetList = FromSubqueryResolver.resolve((SQLCreateViewStatement) stmt);
|
||||
String targetSql = SQLUtils.toSQLString(targetList, JdbcConstants.ORACLE);
|
||||
assertEquals(2, targetList.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.odps;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OdpsSelectTest26 extends TestCase {
|
||||
|
||||
public void test_select() throws Exception {
|
||||
// 1095288847322
|
||||
String sql = "select count(distinct(trackid)) as total , process ,value5 as result\n" +
|
||||
" from \n" +
|
||||
" havanaapp.s_register_process_log where \n" +
|
||||
" ds='20170706' and value6='MOBILE_TB' and value1 REGEXP '^00'\n" +
|
||||
" and process in('preCheckMobile','VerifyScrollCheckcode','SendMobileCheckcode','VerifyMobile',\n" +
|
||||
" 'VerifyMobileCheckCode','VerifyNick','VerifyPasswordFormat','register','CheckMobileConflict','VerifyEmail') \n" +
|
||||
" group by process,value5";//
|
||||
assertEquals("SELECT COUNT(DISTINCT trackid) AS total, process, value5 AS result\n" +
|
||||
"FROM havanaapp.s_register_process_log\n" +
|
||||
"WHERE ds = '20170706'\n" +
|
||||
"\tAND value6 = 'MOBILE_TB'\n" +
|
||||
"\tAND value1 REGEXP '^00'\n" +
|
||||
"\tAND process IN (\n" +
|
||||
"\t\t'preCheckMobile', \n" +
|
||||
"\t\t'VerifyScrollCheckcode', \n" +
|
||||
"\t\t'SendMobileCheckcode', \n" +
|
||||
"\t\t'VerifyMobile', \n" +
|
||||
"\t\t'VerifyMobileCheckCode', \n" +
|
||||
"\t\t'VerifyNick', \n" +
|
||||
"\t\t'VerifyPasswordFormat', \n" +
|
||||
"\t\t'register', \n" +
|
||||
"\t\t'CheckMobileConflict', \n" +
|
||||
"\t\t'VerifyEmail'\n" +
|
||||
"\t)\n" +
|
||||
"GROUP BY process, \n" +
|
||||
"\tvalue5", SQLUtils.formatOdps(sql));
|
||||
|
||||
assertEquals("select count(DISTINCT trackid) as total, process, value5 as result\n" +
|
||||
"from havanaapp.s_register_process_log\n" +
|
||||
"where ds = '20170706'\n" +
|
||||
"\tand value6 = 'MOBILE_TB'\n" +
|
||||
"\tand value1 regexp '^00'\n" +
|
||||
"\tand process in (\n" +
|
||||
"\t\t'preCheckMobile', \n" +
|
||||
"\t\t'VerifyScrollCheckcode', \n" +
|
||||
"\t\t'SendMobileCheckcode', \n" +
|
||||
"\t\t'VerifyMobile', \n" +
|
||||
"\t\t'VerifyMobileCheckCode', \n" +
|
||||
"\t\t'VerifyNick', \n" +
|
||||
"\t\t'VerifyPasswordFormat', \n" +
|
||||
"\t\t'register', \n" +
|
||||
"\t\t'CheckMobileConflict', \n" +
|
||||
"\t\t'VerifyEmail'\n" +
|
||||
"\t)\n" +
|
||||
"group by process, \n" +
|
||||
"\tvalue5", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ODPS);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.ODPS);
|
||||
stmt.accept(visitor);
|
||||
|
||||
// System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
// System.out.println("coditions : " + visitor.getConditions());
|
||||
// System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
assertEquals(1, visitor.getTables().size());
|
||||
assertEquals(6, visitor.getColumns().size());
|
||||
assertEquals(3, visitor.getConditions().size());
|
||||
|
||||
// System.out.println(SQLUtils.formatOdps(sql));
|
||||
|
||||
// assertTrue(visitor.getColumns().contains(new Column("abc", "name")));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.oracle.create;
|
||||
|
||||
import com.alibaba.druid.sql.OracleTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OracleCreateTableTest71 extends OracleTest {
|
||||
|
||||
public void test_types() throws Exception {
|
||||
String sql = //
|
||||
" CREATE TABLE \"CMIS\".\"DR$IDX_FULL_BIZ_TABLE_NAME$N\" \n" +
|
||||
" (\t\"NLT_DOCID\" NUMBER(38,0), \n" +
|
||||
"\t\"NLT_MARK\" CHAR(1) NOT NULL ENABLE, \n" +
|
||||
"\t PRIMARY KEY (\"NLT_DOCID\") ENABLE\n" +
|
||||
" ) ORGANIZATION INDEX NOCOMPRESS PCTFREE 10 INITRANS 2 MAXTRANS 255 LOGGING\n" +
|
||||
" STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645\n" +
|
||||
" PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1\n" +
|
||||
" BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)\n" +
|
||||
" TABLESPACE \"CMISTBS\" \n" +
|
||||
" PCTTHRESHOLD 50\n" +
|
||||
" MONITORING ";
|
||||
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
//
|
||||
assertEquals("CREATE TABLE \"CMIS\".\"DR$IDX_FULL_BIZ_TABLE_NAME$N\" (\n" +
|
||||
"\t\"NLT_DOCID\" NUMBER(38, 0),\n" +
|
||||
"\t\"NLT_MARK\" CHAR(1) NOT NULL ENABLE,\n" +
|
||||
"\tPRIMARY KEY (\"NLT_DOCID\") ENABLE\n" +
|
||||
")\n" +
|
||||
"ORGANIZATION INDEX\n" +
|
||||
"\tPCTFREE 10\n" +
|
||||
"\tINITRANS 2\n" +
|
||||
"\tMAXTRANS 255\n" +
|
||||
"\tNOCOMPRESS\n" +
|
||||
"\tLOGGING\n" +
|
||||
"\tTABLESPACE \"CMISTBS\"\n" +
|
||||
"\tSTORAGE (\n" +
|
||||
"\t\tINITIAL 65536\n" +
|
||||
"\t\tNEXT 1048576\n" +
|
||||
"\t\tMINEXTENTS 1\n" +
|
||||
"\t\tMAXEXTENTS 2147483645\n" +
|
||||
"\t\tPCTINCREASE 0\n" +
|
||||
"\t\tFREELISTS 1\n" +
|
||||
"\t\tFREELIST GROUPS 1\n" +
|
||||
"\t\tBUFFER_POOL DEFAULT\n" +
|
||||
"\t\tFLASH_CACHE DEFAULT\n" +
|
||||
"\t\tCELL_FLASH_CACHE DEFAULT\n" +
|
||||
"\t)\n" +
|
||||
"\tPCTTHRESHOLD 10\n" +
|
||||
"MONITORING",//
|
||||
SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
|
||||
//
|
||||
// SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.ORACLE);
|
||||
// stmt.accept(visitor);
|
||||
//
|
||||
// System.out.println("Tables : " + visitor.getTables());
|
||||
// System.out.println("fields : " + visitor.getColumns());
|
||||
// System.out.println("coditions : " + visitor.getConditions());
|
||||
// System.out.println("relationships : " + visitor.getRelationships());
|
||||
// System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
//
|
||||
// assertEquals(1, visitor.getTables().size());
|
||||
//
|
||||
// assertEquals(3, visitor.getColumns().size());
|
||||
//
|
||||
// assertTrue(visitor.getColumns().contains(new TableStat.Column("JWGZPT.A", "XM")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.alibaba.druid.util;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 16/07/2017.
|
||||
*/
|
||||
public class DropTables extends TestCase {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user