Repository navigation
Expand file tree
/
Copy pathConfigurationParameterChange.java
More file actions
174 lines (150 loc) · 4.71 KB
/
Copy pathConfigurationParameterChange.java
File metadata and controls
174 lines (150 loc) · 4.71 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
/*-
* #%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;
import java.io.Serializable;
import java.util.function.Consumer;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
/**
* A PostgreSQL {@code SET} or {@code RESET} of a configuration parameter, shared by statements that
* store parameter defaults, such as {@code ALTER ROLE} and {@code ALTER DATABASE}:
*
* <pre>
* SET parameter { TO | = } { value [, ...] | DEFAULT }
* SET parameter FROM CURRENT
* RESET { parameter | ALL }
* </pre>
*
* Parameter names keep their SQL spelling, including qualifiers and identifier quotes.
* {@code RESET ALL} uses the parameter name {@code ALL}.
*/
public class ConfigurationParameterChange implements Serializable {
public enum Action {
SET, RESET
}
/** What a {@code SET} assigns. Only the selected kind is rendered. */
public enum ValueKind {
/** The explicit values of {@link #getValues()}. */
VALUES,
/** {@code TO DEFAULT} or {@code = DEFAULT}. */
DEFAULT,
/** {@code FROM CURRENT}. */
FROM_CURRENT
}
private Action action = Action.SET;
private String parameter;
private ValueKind valueKind = ValueKind.VALUES;
private boolean useEquals;
private ExpressionList<Expression> values;
public ConfigurationParameterChange() {}
public ConfigurationParameterChange(Action action, String parameter) {
this.action = action;
this.parameter = parameter;
}
public Action getAction() {
return action;
}
public void setAction(Action action) {
this.action = action;
}
public String getParameter() {
return parameter;
}
public void setParameter(String parameter) {
this.parameter = parameter;
}
public ValueKind getValueKind() {
return valueKind;
}
/**
* Selects what a {@code SET} assigns. The values are retained when another kind is selected.
*
* @param valueKind the assigned value kind
*/
public void setValueKind(ValueKind valueKind) {
this.valueKind = valueKind;
}
/**
* Whether {@code =} rather than {@code TO} precedes the value.
*
* @return {@code true} for {@code =}
*/
public boolean isUseEquals() {
return useEquals;
}
public void setUseEquals(boolean useEquals) {
this.useEquals = useEquals;
}
/**
* The values assigned by {@link ValueKind#VALUES}, as a mutable list.
*
* @return the values, or {@code null} if none were set
*/
public ExpressionList<Expression> getValues() {
return values;
}
public void setValues(ExpressionList<Expression> values) {
this.values = values;
}
public ConfigurationParameterChange withAction(Action action) {
setAction(action);
return this;
}
public ConfigurationParameterChange withParameter(String parameter) {
setParameter(parameter);
return this;
}
public ConfigurationParameterChange withValueKind(ValueKind valueKind) {
setValueKind(valueKind);
return this;
}
public ConfigurationParameterChange withUseEquals(boolean useEquals) {
setUseEquals(useEquals);
return this;
}
public ConfigurationParameterChange withValues(ExpressionList<Expression> values) {
setValues(values);
return this;
}
/**
* Appends this clause to a builder.
*
* @param builder the target
* @param valueRenderer appends each value, for example through an expression deparser
* @return the builder
*/
public StringBuilder appendTo(StringBuilder builder, Consumer<Expression> valueRenderer) {
if (action == Action.RESET) {
return builder.append("RESET ").append(parameter);
}
builder.append("SET ").append(parameter);
if (valueKind == ValueKind.FROM_CURRENT) {
return builder.append(" FROM CURRENT");
}
builder.append(useEquals ? " = " : " TO ");
if (valueKind == ValueKind.DEFAULT) {
return builder.append("DEFAULT");
}
if (values != null) {
for (int i = 0; i < values.size(); i++) {
if (i > 0) {
builder.append(", ");
}
valueRenderer.accept(values.get(i));
}
}
return builder;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
return appendTo(builder, builder::append).toString();
}
}