Repository navigation
Expand file tree
/
Copy pathAlterDatabase.java
More file actions
195 lines (169 loc) · 5.78 KB
/
Copy pathAlterDatabase.java
File metadata and controls
195 lines (169 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2026 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.alter.database;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.statement.ConfigurationParameterChange;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;
import net.sf.jsqlparser.statement.create.database.DatabaseOption;
/**
* MySQL or PostgreSQL {@code ALTER DATABASE}. MySQL's {@code ALTER SCHEMA} synonym only changes
* options, and a missing name selects the current database. The options use the same
* {@link DatabaseOption} model as {@code CREATE DATABASE}. Names retain SQL identifier quoting.
*/
public class AlterDatabase implements Statement {
public enum Action {
/** MySQL options or PostgreSQL {@code [WITH] option ...}, see {@link #getOptions()}. */
OPTIONS,
/** {@code RENAME TO}, see {@link #getNewName()}. */
RENAME,
/** {@code OWNER TO}, see {@link #getOwner()}. */
OWNER,
/** {@code SET TABLESPACE}, see {@link #getTablespace()}. */
SET_TABLESPACE,
/** {@code REFRESH COLLATION VERSION}. */
REFRESH_COLLATION_VERSION,
/** {@code SET} or {@code RESET}, see {@link #getParameterChange()}. */
CONFIGURATION_PARAMETER
}
private String databaseName;
private boolean useSchemaKeyword;
private Action action = Action.OPTIONS;
private boolean useWith;
private final List<DatabaseOption> options = new ArrayList<>();
private String newName;
private String owner;
private String tablespace;
private ConfigurationParameterChange parameterChange;
public String getDatabaseName() {
return databaseName;
}
public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}
public boolean isUseSchemaKeyword() {
return useSchemaKeyword;
}
public void setUseSchemaKeyword(boolean useSchemaKeyword) {
this.useSchemaKeyword = useSchemaKeyword;
}
/**
* The rendered alteration. Payloads of other actions are retained for a later switch back.
*
* @return the action, {@link Action#OPTIONS} unless changed
*/
public Action getAction() {
return action;
}
public void setAction(Action action) {
this.action = action;
}
/**
* Whether PostgreSQL's optional {@code WITH} precedes the options.
*
* @return {@code true} if {@code WITH} is rendered
*/
public boolean isUseWith() {
return useWith;
}
public void setUseWith(boolean useWith) {
this.useWith = useWith;
}
/**
* The options of {@link Action#OPTIONS}, as a mutable list.
*
* @return the options in source order
*/
public List<DatabaseOption> getOptions() {
return options;
}
public Optional<DatabaseOption> getOption(DatabaseOption.Kind kind) {
return options.stream().filter(option -> option.getKind() == kind).findFirst();
}
public String getNewName() {
return newName;
}
public void setNewName(String newName) {
this.newName = newName;
}
/**
* The new owner, a role name or {@code CURRENT_USER}, {@code CURRENT_ROLE} or
* {@code SESSION_USER}.
*
* @return the owner
*/
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getTablespace() {
return tablespace;
}
public void setTablespace(String tablespace) {
this.tablespace = tablespace;
}
public ConfigurationParameterChange getParameterChange() {
return parameterChange;
}
public void setParameterChange(ConfigurationParameterChange parameterChange) {
this.parameterChange = parameterChange;
}
public StringBuilder appendTo(StringBuilder builder) {
return appendTo(builder, builder::append);
}
/**
* Appends this statement to a builder.
*
* @param builder the target
* @param valueRenderer appends each configuration parameter value
* @return the builder
*/
public StringBuilder appendTo(StringBuilder builder, Consumer<Expression> valueRenderer) {
builder.append(useSchemaKeyword ? "ALTER SCHEMA" : "ALTER DATABASE");
if (databaseName != null) {
builder.append(' ').append(databaseName);
}
switch (action) {
case RENAME:
return builder.append(" RENAME TO ").append(newName);
case OWNER:
return builder.append(" OWNER TO ").append(owner);
case SET_TABLESPACE:
return builder.append(" SET TABLESPACE ").append(tablespace);
case REFRESH_COLLATION_VERSION:
return builder.append(" REFRESH COLLATION VERSION");
case CONFIGURATION_PARAMETER:
if (parameterChange == null) {
throw new IllegalStateException("Expected a configuration parameter change");
}
return parameterChange.appendTo(builder.append(' '), valueRenderer);
default:
if (useWith) {
builder.append(" WITH");
}
options.forEach(option -> builder.append(' ').append(option));
return builder;
}
}
@Override
public String toString() {
return appendTo(new StringBuilder()).toString();
}
@Override
public <T, S> T accept(StatementVisitor<T> visitor, S context) {
return visitor.visit(this, context);
}
}