Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:修复Oracle输出SQL时游标参数缺失的BUG #6016

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2955,4 +2955,117 @@ public boolean visit(OracleCreateTableSpaceStatement x) {
print0(ucase ? x.getSql().toUpperCase() : x.getSql().toLowerCase());
return false;
}

public void visitCursorParameters(List<SQLParameter> cursorParameters) {
for (SQLParameter cursorParameter : cursorParameters) {
print('(');
cursorParameter.getName().accept(this);
print(' ');
cursorParameter.getDataType().accept(this);
print(')');
}
}

@Override
public boolean visit(SQLParameter x) {
SQLName name = x.getName();
if (x.getDataType().getName().equalsIgnoreCase("CURSOR")) {
print0(ucase ? "CURSOR " : "cursor ");
x.getName().accept(this);
// Handling cursor parameters
visitCursorParameters(x.getCursorParameters());
print0(ucase ? " IS" : " is");
this.indentCount++;
println();
SQLSelect select = ((SQLQueryExpr) x.getDefaultValue()).getSubQuery();
select.accept(this);
this.indentCount--;

} else {
if (x.isMap()) {
print0(ucase ? "MAP MEMBER " : "map member ");
} else if (x.isOrder()) {
print0(ucase ? "ORDER MEMBER " : "order member ");
} else if (x.isMember()) {
print0(ucase ? "MEMBER " : "member ");
}
SQLDataType dataType = x.getDataType();

if (DbType.oracle == dbType
|| dataType instanceof OracleFunctionDataType
|| dataType instanceof OracleProcedureDataType) {
if (dataType instanceof OracleFunctionDataType) {
OracleFunctionDataType functionDataType = (OracleFunctionDataType) dataType;
visit(functionDataType);
return false;
}

if (dataType instanceof OracleProcedureDataType) {
OracleProcedureDataType procedureDataType = (OracleProcedureDataType) dataType;
visit(procedureDataType);
return false;
}

String dataTypeName = dataType.getName();
boolean printType = (dataTypeName.startsWith("TABLE OF") && x.getDefaultValue() == null)
|| dataTypeName.equalsIgnoreCase("REF CURSOR")
|| dataTypeName.startsWith("VARRAY(");
if (printType) {
print0(ucase ? "TYPE " : "type ");
}

//枚举类型特殊处理
if ("ENUM".equals(dataTypeName)) {
dataType.accept(this);
return false;
} else {
name.accept(this);
}
if (x.getParamType() == SQLParameter.ParameterType.IN) {
print0(ucase ? " IN " : " in ");
} else if (x.getParamType() == SQLParameter.ParameterType.OUT) {
print0(ucase ? " OUT " : " out ");
} else if (x.getParamType() == SQLParameter.ParameterType.INOUT) {
print0(ucase ? " IN OUT " : " in out ");
} else {
print(' ');
}

if (x.isNoCopy()) {
print0(ucase ? "NOCOPY " : "nocopy ");
}

if (x.isConstant()) {
print0(ucase ? "CONSTANT " : "constant ");
}

if (printType) {
print0(ucase ? "IS " : "is ");
}
} else {
if (x.getParamType() == SQLParameter.ParameterType.IN) {
boolean skip = DbType.mysql == dbType
&& x.getParent() instanceof SQLCreateFunctionStatement;

if (!skip) {
print0(ucase ? "IN " : "in ");
}
} else if (x.getParamType() == SQLParameter.ParameterType.OUT) {
print0(ucase ? "OUT " : "out ");
} else if (x.getParamType() == SQLParameter.ParameterType.INOUT) {
print0(ucase ? "INOUT " : "inout ");
}
x.getName().accept(this);
print(' ');
}

dataType.accept(this);
if (x.isNotNull()) {
print0(ucase ? " NOT NULL" : " not null");
}
printParamDefaultValue(x);
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.alibaba.druid.demo.sql;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import junit.framework.TestCase;

/**
* @author LENOVO
* @date 2024/7/17 10:56
*/
public class OracleCursorParamTest extends TestCase {

public void test_cursor_parameters() {
String sql = "DECLARE\n" +
"CURSOR cur_param(name_str VARCHAR2) IS SELECT u.USERNAME, u.PASSWORD FROM MG_USER u WHERE ID = 1;\n" +
"op_name VARCHAR2(100);\n" +
"BEGIN\n" +
"SELECT NVL(EMP_NAME, USERNAME) INTO op_name FROM MG_USER WHERE ID = 1;\n" +
"END;";

SQLStatement stmt = SQLUtils.parseSingleStatement(sql, DbType.oracle);

System.out.println(stmt);
}
}
Loading