|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2026 JSQLParser |
| 6 | + * %% |
| 7 | + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 |
| 8 | + * #L% |
| 9 | + */ |
| 10 | +package net.sf.jsqlparser.statement.oracle; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import java.util.function.Consumer; |
| 15 | +import net.sf.jsqlparser.expression.Expression; |
| 16 | +import net.sf.jsqlparser.statement.Block; |
| 17 | +import net.sf.jsqlparser.statement.Statement; |
| 18 | +import net.sf.jsqlparser.statement.Statements; |
| 19 | +import net.sf.jsqlparser.statement.StatementVisitor; |
| 20 | +import net.sf.jsqlparser.statement.create.table.ColDataType; |
| 21 | + |
| 22 | +/** An Oracle anonymous block with variable declarations and exception handlers. */ |
| 23 | +public class OracleBlock extends Block { |
| 24 | + private final List<VariableDeclaration> declarations = new ArrayList<>(); |
| 25 | + private final List<ExceptionHandler> exceptionHandlers = new ArrayList<>(); |
| 26 | + |
| 27 | + public List<VariableDeclaration> getDeclarations() { |
| 28 | + return declarations; |
| 29 | + } |
| 30 | + |
| 31 | + public List<ExceptionHandler> getExceptionHandlers() { |
| 32 | + return exceptionHandlers; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public <T, S> T accept(StatementVisitor<T> visitor, S context) { |
| 37 | + return visitor.visit(this, context); |
| 38 | + } |
| 39 | + |
| 40 | + public void visitChildren(Consumer<Expression> expressions, Consumer<Statement> statements) { |
| 41 | + for (VariableDeclaration declaration : declarations) { |
| 42 | + if (declaration.getInitializer() != null) { |
| 43 | + expressions.accept(declaration.getInitializer()); |
| 44 | + } |
| 45 | + } |
| 46 | + if (getStatements() != null) { |
| 47 | + getStatements().forEach(statements); |
| 48 | + } |
| 49 | + exceptionHandlers.forEach(handler -> handler.getStatements().forEach(statements)); |
| 50 | + } |
| 51 | + |
| 52 | + public StringBuilder appendTo(StringBuilder builder, Consumer<Expression> expressions, |
| 53 | + Consumer<Statement> statements) { |
| 54 | + if (!declarations.isEmpty()) { |
| 55 | + builder.append("DECLARE\n"); |
| 56 | + declarations.forEach(declaration -> { |
| 57 | + declaration.appendTo(builder, expressions); |
| 58 | + builder.append(";\n"); |
| 59 | + }); |
| 60 | + } |
| 61 | + builder.append("BEGIN\n"); |
| 62 | + appendStatements(builder, getStatements(), statements); |
| 63 | + if (!exceptionHandlers.isEmpty()) { |
| 64 | + builder.append("EXCEPTION\n"); |
| 65 | + for (ExceptionHandler handler : exceptionHandlers) { |
| 66 | + builder.append("WHEN ").append(String.join(" OR ", handler.getExceptions())) |
| 67 | + .append(" THEN\n"); |
| 68 | + appendStatements(builder, handler.getStatements(), statements); |
| 69 | + } |
| 70 | + } |
| 71 | + builder.append("END"); |
| 72 | + if (hasSemicolonAfterEnd()) { |
| 73 | + builder.append(';'); |
| 74 | + } |
| 75 | + return builder; |
| 76 | + } |
| 77 | + |
| 78 | + private static void appendStatements(StringBuilder builder, Statements body, |
| 79 | + Consumer<Statement> printer) { |
| 80 | + if (body == null) { |
| 81 | + return; |
| 82 | + } |
| 83 | + for (Statement statement : body) { |
| 84 | + printer.accept(statement); |
| 85 | + if (!(statement instanceof Block) || !((Block) statement).hasSemicolonAfterEnd()) { |
| 86 | + builder.append(';'); |
| 87 | + } |
| 88 | + builder.append('\n'); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public StringBuilder appendTo(StringBuilder builder) { |
| 94 | + return appendTo(builder, builder::append, builder::append); |
| 95 | + } |
| 96 | + |
| 97 | + public static class VariableDeclaration implements java.io.Serializable { |
| 98 | + private String name; |
| 99 | + private ColDataType dataType; |
| 100 | + private boolean constant; |
| 101 | + private boolean notNull; |
| 102 | + private String initializerOperator = ":="; |
| 103 | + private Expression initializer; |
| 104 | + |
| 105 | + public String getName() { |
| 106 | + return name; |
| 107 | + } |
| 108 | + |
| 109 | + public void setName(String name) { |
| 110 | + this.name = name; |
| 111 | + } |
| 112 | + |
| 113 | + public ColDataType getDataType() { |
| 114 | + return dataType; |
| 115 | + } |
| 116 | + |
| 117 | + public void setDataType(ColDataType type) { |
| 118 | + dataType = type; |
| 119 | + } |
| 120 | + |
| 121 | + public boolean isConstant() { |
| 122 | + return constant; |
| 123 | + } |
| 124 | + |
| 125 | + public void setConstant(boolean value) { |
| 126 | + constant = value; |
| 127 | + } |
| 128 | + |
| 129 | + public boolean isNotNull() { |
| 130 | + return notNull; |
| 131 | + } |
| 132 | + |
| 133 | + public void setNotNull(boolean value) { |
| 134 | + notNull = value; |
| 135 | + } |
| 136 | + |
| 137 | + public String getInitializerOperator() { |
| 138 | + return initializerOperator; |
| 139 | + } |
| 140 | + |
| 141 | + public void setInitializerOperator(String operator) { |
| 142 | + initializerOperator = operator; |
| 143 | + } |
| 144 | + |
| 145 | + public Expression getInitializer() { |
| 146 | + return initializer; |
| 147 | + } |
| 148 | + |
| 149 | + public void setInitializer(Expression expression) { |
| 150 | + initializer = expression; |
| 151 | + } |
| 152 | + |
| 153 | + public StringBuilder appendTo(StringBuilder builder, Consumer<Expression> printer) { |
| 154 | + builder.append(name).append(' '); |
| 155 | + if (constant) { |
| 156 | + builder.append("CONSTANT "); |
| 157 | + } |
| 158 | + builder.append(dataType); |
| 159 | + if (notNull) { |
| 160 | + builder.append(" NOT NULL"); |
| 161 | + } |
| 162 | + if (initializer != null) { |
| 163 | + builder.append(' ').append(initializerOperator).append(' '); |
| 164 | + printer.accept(initializer); |
| 165 | + } |
| 166 | + return builder; |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public String toString() { |
| 171 | + StringBuilder builder = new StringBuilder(); |
| 172 | + return appendTo(builder, builder::append).toString(); |
| 173 | + } |
| 174 | + } |
| 175 | + public static class ExceptionHandler implements java.io.Serializable { |
| 176 | + private final List<String> exceptions = new ArrayList<>(); |
| 177 | + private Statements statements; |
| 178 | + |
| 179 | + public List<String> getExceptions() { |
| 180 | + return exceptions; |
| 181 | + } |
| 182 | + |
| 183 | + public Statements getStatements() { |
| 184 | + return statements; |
| 185 | + } |
| 186 | + |
| 187 | + public void setStatements(Statements statements) { |
| 188 | + this.statements = statements; |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments