|
| 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.alter; |
| 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.create.publication.PublicationTarget; |
| 17 | +import net.sf.jsqlparser.statement.create.publication.PublicationOption; |
| 18 | +import net.sf.jsqlparser.statement.Statement; |
| 19 | +import net.sf.jsqlparser.statement.StatementVisitor; |
| 20 | + |
| 21 | +public class AlterPublication implements Statement { |
| 22 | + private String name; |
| 23 | + private Action action; |
| 24 | + private List<PublicationTarget> targets = new ArrayList<>(); |
| 25 | + private List<PublicationOption> options = new ArrayList<>(); |
| 26 | + private String newName; |
| 27 | + |
| 28 | + public String getName() { |
| 29 | + return name; |
| 30 | + } |
| 31 | + |
| 32 | + public void setName(String name) { |
| 33 | + this.name = name; |
| 34 | + } |
| 35 | + |
| 36 | + public Action getAction() { |
| 37 | + return action; |
| 38 | + } |
| 39 | + |
| 40 | + public void setAction(Action action) { |
| 41 | + this.action = action; |
| 42 | + } |
| 43 | + |
| 44 | + public List<PublicationTarget> getTargets() { |
| 45 | + return targets; |
| 46 | + } |
| 47 | + |
| 48 | + public void setTargets(List<PublicationTarget> targets) { |
| 49 | + this.targets = targets; |
| 50 | + } |
| 51 | + |
| 52 | + public List<PublicationOption> getOptions() { |
| 53 | + return options; |
| 54 | + } |
| 55 | + |
| 56 | + public void setOptions(List<PublicationOption> options) { |
| 57 | + this.options = options; |
| 58 | + } |
| 59 | + |
| 60 | + public String getNewName() { |
| 61 | + return newName; |
| 62 | + } |
| 63 | + |
| 64 | + public void setNewName(String newName) { |
| 65 | + this.newName = newName; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public <T, S> T accept(StatementVisitor<T> visitor, S context) { |
| 70 | + return visitor.visit(this, context); |
| 71 | + } |
| 72 | + |
| 73 | + public enum Action { |
| 74 | + ADD, SET, DROP, SET_OPTIONS, OWNER, RENAME |
| 75 | + } |
| 76 | + |
| 77 | + public void appendTo(StringBuilder sql, Consumer<Expression> expressions) { |
| 78 | + sql.append("ALTER PUBLICATION ").append(name).append(' '); |
| 79 | + switch (action) { |
| 80 | + case OWNER: |
| 81 | + sql.append("OWNER TO ").append(newName); |
| 82 | + break; |
| 83 | + case RENAME: |
| 84 | + sql.append("RENAME TO ").append(newName); |
| 85 | + break; |
| 86 | + case SET_OPTIONS: |
| 87 | + sql.append("SET ("); |
| 88 | + for (int i = 0; i < options.size(); i++) { |
| 89 | + if (i > 0) { |
| 90 | + sql.append(", "); |
| 91 | + } |
| 92 | + options.get(i).appendTo(sql, expressions); |
| 93 | + } |
| 94 | + sql.append(')'); |
| 95 | + break; |
| 96 | + case ADD: |
| 97 | + case SET: |
| 98 | + case DROP: |
| 99 | + sql.append(action).append(' '); |
| 100 | + for (int i = 0; i < targets.size(); i++) { |
| 101 | + if (i > 0) { |
| 102 | + sql.append(", "); |
| 103 | + } |
| 104 | + targets.get(i).appendTo(sql, expressions); |
| 105 | + } |
| 106 | + break; |
| 107 | + default: |
| 108 | + throw new IllegalStateException("Unknown publication alteration: " + action); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String toString() { |
| 114 | + StringBuilder sql = new StringBuilder(); |
| 115 | + appendTo(sql, sql::append); |
| 116 | + return sql.toString(); |
| 117 | + } |
| 118 | +} |
0 commit comments