|
| 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; |
| 11 | + |
| 12 | +import java.io.Serializable; |
| 13 | +import java.util.Objects; |
| 14 | +import net.sf.jsqlparser.expression.Expression; |
| 15 | +import net.sf.jsqlparser.expression.StringValue; |
| 16 | +import net.sf.jsqlparser.statement.create.table.ColDataType; |
| 17 | + |
| 18 | +/** |
| 19 | + * The value of a {@link DefinitionElement}: PostgreSQL's {@code def_arg}. The grammar reads a name, |
| 20 | + * such as a function or a storage strategy, as a type name, so it is a {@link Kind#TYPE} argument. |
| 21 | + */ |
| 22 | +public final class DefinitionArgument implements Serializable { |
| 23 | + public enum Kind { |
| 24 | + /** A type or a name, see {@link #getDataType()}. */ |
| 25 | + TYPE, |
| 26 | + /** A reserved keyword such as {@code TRUE} or {@code NULL}, see {@link #getKeyword()}. */ |
| 27 | + KEYWORD, |
| 28 | + /** An operator, see {@link #getOperator()}. */ |
| 29 | + OPERATOR, |
| 30 | + /** A number with an optional sign, see {@link #getNumber()}. */ |
| 31 | + NUMBER, |
| 32 | + /** A string constant, see {@link #getString()}. */ |
| 33 | + STRING, |
| 34 | + /** The keyword {@code NONE}. */ |
| 35 | + NONE |
| 36 | + } |
| 37 | + |
| 38 | + private final Kind kind; |
| 39 | + private ColDataType dataType; |
| 40 | + private boolean setOf; |
| 41 | + private boolean typeReference; |
| 42 | + private String keyword; |
| 43 | + private String operator; |
| 44 | + private boolean operatorKeyword; |
| 45 | + private Expression number; |
| 46 | + private StringValue string; |
| 47 | + |
| 48 | + private DefinitionArgument(Kind kind) { |
| 49 | + this.kind = kind; |
| 50 | + } |
| 51 | + |
| 52 | + public static DefinitionArgument type(ColDataType dataType) { |
| 53 | + return type(dataType, false, false); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * A type argument. |
| 58 | + * |
| 59 | + * @param dataType the type or name |
| 60 | + * @param setOf whether {@code SETOF} precedes the type |
| 61 | + * @param typeReference whether {@code %TYPE} follows the name of a column |
| 62 | + * @return the argument |
| 63 | + */ |
| 64 | + public static DefinitionArgument type(ColDataType dataType, boolean setOf, |
| 65 | + boolean typeReference) { |
| 66 | + DefinitionArgument argument = new DefinitionArgument(Kind.TYPE); |
| 67 | + argument.dataType = Objects.requireNonNull(dataType); |
| 68 | + argument.setOf = setOf; |
| 69 | + argument.typeReference = typeReference; |
| 70 | + return argument; |
| 71 | + } |
| 72 | + |
| 73 | + public static DefinitionArgument keyword(String keyword) { |
| 74 | + DefinitionArgument argument = new DefinitionArgument(Kind.KEYWORD); |
| 75 | + argument.keyword = Objects.requireNonNull(keyword); |
| 76 | + return argument; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * An operator argument. |
| 81 | + * |
| 82 | + * @param operator the operator, with its schema when written as {@code OPERATOR(schema.op)} |
| 83 | + * @param operatorKeyword whether the operator is written as {@code OPERATOR(...)} |
| 84 | + * @return the argument |
| 85 | + */ |
| 86 | + public static DefinitionArgument operator(String operator, boolean operatorKeyword) { |
| 87 | + DefinitionArgument argument = new DefinitionArgument(Kind.OPERATOR); |
| 88 | + argument.operator = Objects.requireNonNull(operator); |
| 89 | + argument.operatorKeyword = operatorKeyword; |
| 90 | + return argument; |
| 91 | + } |
| 92 | + |
| 93 | + public static DefinitionArgument number(Expression number) { |
| 94 | + DefinitionArgument argument = new DefinitionArgument(Kind.NUMBER); |
| 95 | + argument.number = Objects.requireNonNull(number); |
| 96 | + return argument; |
| 97 | + } |
| 98 | + |
| 99 | + public static DefinitionArgument string(StringValue string) { |
| 100 | + DefinitionArgument argument = new DefinitionArgument(Kind.STRING); |
| 101 | + argument.string = Objects.requireNonNull(string); |
| 102 | + return argument; |
| 103 | + } |
| 104 | + |
| 105 | + public static DefinitionArgument none() { |
| 106 | + return new DefinitionArgument(Kind.NONE); |
| 107 | + } |
| 108 | + |
| 109 | + public Kind getKind() { |
| 110 | + return kind; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * The type of a {@link Kind#TYPE} argument. A plain name, such as the function of |
| 115 | + * {@code INPUT = box3_in}, is the type's {@link ColDataType#getDataType() name}. |
| 116 | + * |
| 117 | + * @return the type, or {@code null} for another kind |
| 118 | + */ |
| 119 | + public ColDataType getDataType() { |
| 120 | + return dataType; |
| 121 | + } |
| 122 | + |
| 123 | + public boolean isSetOf() { |
| 124 | + return setOf; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Whether {@code %TYPE} follows the name, which then names a column. |
| 129 | + * |
| 130 | + * @return {@code true} for {@code table.column%TYPE} |
| 131 | + */ |
| 132 | + public boolean isTypeReference() { |
| 133 | + return typeReference; |
| 134 | + } |
| 135 | + |
| 136 | + public String getKeyword() { |
| 137 | + return keyword; |
| 138 | + } |
| 139 | + |
| 140 | + public String getOperator() { |
| 141 | + return operator; |
| 142 | + } |
| 143 | + |
| 144 | + public boolean isOperatorKeyword() { |
| 145 | + return operatorKeyword; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * The number of a {@link Kind#NUMBER} argument: a long or double value, inside a signed |
| 150 | + * expression when a sign is written. |
| 151 | + * |
| 152 | + * @return the number, or {@code null} for another kind |
| 153 | + */ |
| 154 | + public Expression getNumber() { |
| 155 | + return number; |
| 156 | + } |
| 157 | + |
| 158 | + public StringValue getString() { |
| 159 | + return string; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public String toString() { |
| 164 | + switch (kind) { |
| 165 | + case TYPE: |
| 166 | + return (setOf ? "SETOF " : "") + dataType + (typeReference ? "%TYPE" : ""); |
| 167 | + case KEYWORD: |
| 168 | + return keyword; |
| 169 | + case OPERATOR: |
| 170 | + return operatorKeyword ? "OPERATOR(" + operator + ")" : operator; |
| 171 | + case NUMBER: |
| 172 | + return number.toString(); |
| 173 | + case STRING: |
| 174 | + return string.toString(); |
| 175 | + default: |
| 176 | + return "NONE"; |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments