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

[CBRD-25297] building Java type import list at code generation stage #5133

Merged
merged 5 commits into from
Apr 26, 2024
Merged
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 @@ -66,22 +66,6 @@ public ParseTreeConverter(Map<ParserRuleContext, SqlSemantics> staticSqls) {
this.staticSqls = staticSqls;
}

public void addToImports(String i) {

assert i != null;

if ("com.cubrid.plcsql.predefined.sp.SpLib.Query".equals(i)) {
// no need to import Cursor now
} else if (i.startsWith("java.lang.")
&& i.lastIndexOf('.') == 9) { // 9:the index of the second '.'
// no need to import java.lang.*
} else if (i.startsWith("Null")) {
// NULL type is not a java type but an internal type for convenience in typechecking.
} else {
imports.add(i);
}
}

public void askServerSemanticQuestions() {
if (semanticQuestions.size() == 0) {
return; // nothing to do
Expand Down Expand Up @@ -187,7 +171,6 @@ public void askServerSemanticQuestions() {
Type retType =
DBTypeAdapter.getDeclType(
fs.retType.type, fs.retType.prec, fs.retType.scale);
addToImports(retType.fullJavaType);

gfc.decl = new DeclFunc(null, fs.name, paramList, TypeSpec.getBogus(retType));

Expand All @@ -213,7 +196,6 @@ public void askServerSemanticQuestions() {
Type ty =
DBTypeAdapter.getDeclType(
ct.colType.type, ct.colType.prec, ct.colType.scale);
addToImports(ty.fullJavaType);

assert node instanceof TypeSpecPercent;
((TypeSpecPercent) node).type = ty;
Expand All @@ -226,10 +208,6 @@ public void askServerSemanticQuestions() {
@Override
public AstNode visitSql_script(Sql_scriptContext ctx) {

addToImports("com.cubrid.jsp.Server");
addToImports("com.cubrid.plcsql.predefined.PlcsqlRuntimeError");
addToImports("java.util.List");

AstNode ret = visitCreate_routine(ctx.create_routine());

assert symbolStack.getSize() == 2;
Expand Down Expand Up @@ -358,7 +336,6 @@ public TypeSpec visitNumeric_type(Numeric_typeContext ctx) {
throw new RuntimeException("unreachable");
}

addToImports("java.math.BigDecimal");
return new TypeSpec(ctx, TypeNumeric.getInstance(precision, scale));
}

Expand Down Expand Up @@ -409,7 +386,6 @@ public TypeSpec visitSimple_type(Simple_typeContext ctx) {
String plcType = Misc.getNormalizedText(ctx);
Type ty = nameToType.get(plcType);
assert ty != null;
addToImports(ty.fullJavaType);
return new TypeSpec(ctx, ty);
}

Expand Down Expand Up @@ -637,7 +613,6 @@ public Expr visitBit_or_exp(Bit_or_expContext ctx) {

@Override
public ExprDate visitDate_exp(Date_expContext ctx) {
addToImports("java.sql.Date");

String s = ctx.quoted_string().getText();
s = unquoteStr(s);
Expand All @@ -653,7 +628,6 @@ public ExprDate visitDate_exp(Date_expContext ctx) {

@Override
public ExprTime visitTime_exp(Time_expContext ctx) {
addToImports("java.sql.Time");

String s = ctx.quoted_string().getText();
s = unquoteStr(s);
Expand All @@ -669,15 +643,13 @@ public ExprTime visitTime_exp(Time_expContext ctx) {

@Override
public ExprTimestamp visitTimestamp_exp(Timestamp_expContext ctx) {
addToImports("java.sql.Timestamp");

String s = ctx.quoted_string().getText();
return parseZonedDateTime(ctx, s, false, "TIMESTAMP");
}

@Override
public ExprDatetime visitDatetime_exp(Datetime_expContext ctx) {
addToImports("java.sql.Timestamp");

String s = ctx.quoted_string().getText();
s = unquoteStr(s);
Expand Down Expand Up @@ -733,7 +705,6 @@ public ExprUint visitUint_exp(Uint_expContext ctx) {
Misc.getLineColumnOf(ctx), // s006
"number of digits of an integer literal may not exceed 38");
}
addToImports("java.math.BigDecimal");
ty = Type.NUMERIC_ANY;
} else if (bi.compareTo(INT_MAX) > 0 || bi.compareTo(INT_MIN) < 0) {
ty = Type.BIGINT;
Expand All @@ -751,7 +722,6 @@ public ExprUint visitUint_exp(Uint_expContext ctx) {
@Override
public ExprFloat visitFp_num_exp(Fp_num_expContext ctx) {
try {
addToImports("java.math.BigDecimal");

String text = ctx.FLOATING_POINT_NUM().getText().toLowerCase();

Expand Down Expand Up @@ -830,8 +800,6 @@ public Expr visitField_exp(Field_expContext ctx) {
|| fieldName.equals("NEXTVAL")) {

connectionRequired = true;
addToImports("java.sql.*");
addToImports("java.math.BigDecimal");

String recordText = Misc.getNormalizedText(ctx.record);
// do not push a symbol table: no nested structure
Expand Down Expand Up @@ -864,7 +832,6 @@ public Expr visitFunction_call(Function_callContext ctx) {
if (decl == null) {

connectionRequired = true;
addToImports("java.sql.*");

ExprGlobalFuncCall ret = new ExprGlobalFuncCall(ctx, name, args);
semanticQuestions.put(ret, new ServerAPI.FunctionSignature(name));
Expand All @@ -879,7 +846,6 @@ public Expr visitFunction_call(Function_callContext ctx) {
}

connectionRequired = true;
addToImports("java.sql.*");
return new ExprBuiltinFuncCall(ctx, name, args);
} else {
if (decl.paramList.nodes.size() != args.nodes.size()) {
Expand Down Expand Up @@ -1015,7 +981,6 @@ public Expr visitSqlerrm_exp(Sqlerrm_expContext ctx) {
@Override
public Expr visitList_exp(List_expContext ctx) {
NodeList<Expr> elems = visitExpressions(ctx.expressions());
addToImports("java.util.Arrays");
return new ExprList(ctx, elems);
}
*/
Expand Down Expand Up @@ -1086,9 +1051,14 @@ public AstNode visitPragma_declaration(Pragma_declarationContext ctx) {
"AUTONOMOUS_TRANSACTION can only be declared at the top level");
}

throw new SemanticError(
Misc.getLineColumnOf(ctx), "AUTONOMOUS_TRANSACTION is not supported yet");

/*
// just turn on the flag and return nothing
autonomousTransaction = true;
return null;
*/
}

@Override
Expand Down Expand Up @@ -1140,7 +1110,6 @@ public AstNode visitVariable_declaration(Variable_declarationContext ctx) {
public AstNode visitCursor_definition(Cursor_definitionContext ctx) {

connectionRequired = true;
addToImports("java.sql.*");

String name = Misc.getNormalizedText(ctx.identifier());

Expand Down Expand Up @@ -1320,7 +1289,6 @@ public Expr visitIdentifier(IdentifierContext ctx) {
// this is possibly a global function call

connectionRequired = true;
addToImports("java.sql.*");

Expr ret = new ExprGlobalFuncCall(ctx, name, EMPTY_ARGS);
semanticQuestions.put(ret, new ServerAPI.FunctionSignature(name));
Expand All @@ -1336,7 +1304,6 @@ public Expr visitIdentifier(IdentifierContext ctx) {
} else if (decl instanceof DeclFunc) {
if (decl.scope().level == SymbolStack.LEVEL_PREDEFINED) {
connectionRequired = true;
addToImports("java.sql.*");
return new ExprBuiltinFuncCall(ctx, name, EMPTY_ARGS);
} else {
return new ExprLocalFuncCall(ctx, name, EMPTY_ARGS, scope, (DeclFunc) decl);
Expand Down Expand Up @@ -1553,7 +1520,6 @@ public Expr visitStep(StepContext ctx) {
public AstNode visitStmt_for_cursor_loop(Stmt_for_cursor_loopContext ctx) {

connectionRequired = true;
addToImports("java.sql.*");

symbolStack.pushSymbolTable("for_cursor_loop", null);

Expand Down Expand Up @@ -1605,7 +1571,6 @@ public AstNode visitStmt_for_cursor_loop(Stmt_for_cursor_loopContext ctx) {
public StmtForSqlLoop visitStmt_for_static_sql_loop(Stmt_for_static_sql_loopContext ctx) {

connectionRequired = true;
addToImports("java.sql.*");

symbolStack.pushSymbolTable("for_s_sql_loop", null);

Expand Down Expand Up @@ -1653,7 +1618,6 @@ public StmtForSqlLoop visitStmt_for_static_sql_loop(Stmt_for_static_sql_loopCont
public StmtForSqlLoop visitStmt_for_dynamic_sql_loop(Stmt_for_dynamic_sql_loopContext ctx) {

connectionRequired = true;
addToImports("java.sql.*");

symbolStack.pushSymbolTable("for_d_sql_loop", null);

Expand Down Expand Up @@ -1794,10 +1758,6 @@ public AstNode visitSimple_case_statement(Simple_case_statementContext ctx) {

symbolStack.popSymbolTable();

if (whenParts.nodes.size() > 0) {
addToImports("java.util.Objects");
}

controlFlowBlocked = allFlowsBlocked; // s017-6
return new StmtCase(ctx, level, selector, whenParts, elsePart);
}
Expand Down Expand Up @@ -1843,7 +1803,6 @@ public StmtRaiseAppErr visitRaise_application_error_statement(
public StmtStaticSql visitStatic_sql(Static_sqlContext ctx) {

connectionRequired = true;
addToImports("java.sql.*");
SqlSemantics sws = staticSqls.get(ctx);
assert sws != null;
StaticSql staticSql = checkAndConvertStaticSql(sws, ctx);
Expand All @@ -1870,7 +1829,6 @@ public StmtStaticSql visitStatic_sql(Static_sqlContext ctx) {
public AstNode visitClose_statement(Close_statementContext ctx) {

connectionRequired = true;
addToImports("java.sql.*");

IdentifierContext idCtx = ctx.cursor_exp().identifier();

Expand All @@ -1889,7 +1847,6 @@ public AstNode visitClose_statement(Close_statementContext ctx) {
public AstNode visitOpen_statement(Open_statementContext ctx) {

connectionRequired = true;
addToImports("java.sql.*");

IdentifierContext idCtx = ctx.cursor_exp().identifier();

Expand Down Expand Up @@ -1933,7 +1890,6 @@ public NodeList<Expr> visitExpressions(ExpressionsContext ctx) {
public AstNode visitFetch_statement(Fetch_statementContext ctx) {

connectionRequired = true;
addToImports("java.sql.*");

IdentifierContext idCtx = ctx.cursor_exp().identifier();
ExprId cursor = visitNonFuncIdentifier(idCtx); // s037: undeclared id ...
Expand Down Expand Up @@ -1979,7 +1935,6 @@ public AstNode visitFetch_statement(Fetch_statementContext ctx) {
public AstNode visitOpen_for_statement(Open_for_statementContext ctx) {

connectionRequired = true;
addToImports("java.sql.*");

ExprId refCursor = visitNonFuncIdentifier(ctx.identifier()); // s040: undeclared id ...
if (!isAssignableTo(refCursor)) {
Expand Down Expand Up @@ -2009,14 +1964,12 @@ public AstNode visitOpen_for_statement(Open_for_statementContext ctx) {
@Override
public StmtCommit visitCommit_statement(Commit_statementContext ctx) {
connectionRequired = true;
addToImports("java.sql.*");
return new StmtCommit(ctx);
}

@Override
public StmtRollback visitRollback_statement(Rollback_statementContext ctx) {
connectionRequired = true;
addToImports("java.sql.*");
return new StmtRollback(ctx);
}

Expand All @@ -2041,7 +1994,6 @@ public AstNode visitProcedure_call(Procedure_callContext ctx) {
if (decl == null) {

connectionRequired = true;
addToImports("java.sql.*");

StmtGlobalProcCall ret = new StmtGlobalProcCall(ctx, name, args);
semanticQuestions.put(ret, new ServerAPI.ProcedureSignature(name));
Expand Down Expand Up @@ -2085,7 +2037,6 @@ public AstNode visitProcedure_call(Procedure_callContext ctx) {
public StmtSql visitExecute_immediate(Execute_immediateContext ctx) {

connectionRequired = true;
addToImports("java.sql.*");

Expr dynSql = visitExpression(ctx.dyn_sql().expression());

Expand Down Expand Up @@ -2447,7 +2398,6 @@ private StaticSql checkAndConvertStaticSql(SqlSemantics sws, ParserRuleContext c
}

ExprAutoParam autoParam = new ExprAutoParam(ctx, pi.value, pi.type);
addToImports(DBTypeAdapter.getValueType(pi.type).fullJavaType);
hostExprs.put(
autoParam,
null); // null: type check is not necessary for auto parameters
Expand Down Expand Up @@ -2577,7 +2527,6 @@ private String makeParamList(NodeList<DeclParam> paramList, String name, PlParam

Type paramType =
DBTypeAdapter.getDeclType(params[i].type, params[i].prec, params[i].scale);
addToImports(paramType.fullJavaType);

TypeSpec tySpec = TypeSpec.getBogus(paramType);
if ((params[i].mode & ServerConstants.PARAM_MODE_OUT) != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Set;
import org.antlr.v4.runtime.ParserRuleContext;
import org.apache.commons.text.StringEscapeUtils;

Expand All @@ -65,7 +66,7 @@ public Type getType() {
return DBTypeAdapter.getValueType(ty);
}

public String javaCode() {
public String javaCode(Set<String> javaTypesUsed) {

if (ty == DBType.DB_NULL) {
return "null";
Expand Down Expand Up @@ -97,6 +98,7 @@ public String javaCode() {
return String.format("new Long(%sL)", javaObj);
case DBType.DB_NUMERIC:
assert javaObj instanceof BigDecimal;
javaTypesUsed.add("java.math.BigDecimal");
return String.format("new BigDecimal(\"%s\")", javaObj);
case DBType.DB_FLOAT:
assert javaObj instanceof Float;
Expand All @@ -106,13 +108,16 @@ public String javaCode() {
return String.format("new Double(%s)", javaObj);
case DBType.DB_DATE:
assert javaObj instanceof Date;
javaTypesUsed.add("java.sql.Date");
return String.format("Date.valueOf(\"%s\")", javaObj);
case DBType.DB_TIME:
assert javaObj instanceof Time;
javaTypesUsed.add("java.sql.Time");
return String.format("Time.valueOf(\"%s\")", javaObj);
case DBType.DB_DATETIME:
case DBType.DB_TIMESTAMP:
assert javaObj instanceof Timestamp;
javaTypesUsed.add("java.sql.Timestamp");
return String.format("Timestamp.valueOf(\"%s\")", javaObj);
default:
assert false : "unreachable";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.cubrid.jsp.value.DateTimeParser;
import com.cubrid.plcsql.compiler.visitor.AstVisitor;
import java.time.LocalDate;
import java.util.Set;
import org.antlr.v4.runtime.ParserRuleContext;

public class ExprDate extends Expr {
Expand All @@ -50,7 +51,10 @@ public ExprDate(ParserRuleContext ctx, LocalDate date) {
this.date = date;
}

public String javaCode() {
public String javaCode(Set<String> javaTypesUsed) {

javaTypesUsed.add("java.sql.Date");

if (date.equals(DateTimeParser.nullDate)) {
return "new Date(0 - 1900, 0 - 1, 0)";
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.cubrid.jsp.value.DateTimeParser;
import com.cubrid.plcsql.compiler.visitor.AstVisitor;
import java.time.LocalDateTime;
import java.util.Set;
import org.antlr.v4.runtime.ParserRuleContext;

public class ExprDatetime extends Expr {
Expand All @@ -50,7 +51,10 @@ public ExprDatetime(ParserRuleContext ctx, LocalDateTime time) {
this.time = time;
}

public String javaCode() {
public String javaCode(Set<String> javaTypesUsed) {

javaTypesUsed.add("java.sql.Timestamp");

if (time.equals(DateTimeParser.nullDatetime)) {
return "new Timestamp(0 - 1900, 0 - 1, 0, 0, 0, 0, 0)";
// server
Expand Down
Loading
Loading