|
| 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.create.table; |
| 11 | + |
| 12 | +import java.util.function.Consumer; |
| 13 | +import net.sf.jsqlparser.expression.Expression; |
| 14 | +import net.sf.jsqlparser.schema.Column; |
| 15 | + |
| 16 | +/** A SQL Server table-level DEFAULT expression FOR a column, with an optional constraint name. */ |
| 17 | +public class DefaultConstraint extends NamedConstraint { |
| 18 | + private Expression expression; |
| 19 | + private Column column; |
| 20 | + private boolean withValues; |
| 21 | + |
| 22 | + public DefaultConstraint() { |
| 23 | + setType("DEFAULT"); |
| 24 | + setKind(Kind.DEFAULT); |
| 25 | + } |
| 26 | + |
| 27 | + public Expression getExpression() { |
| 28 | + return expression; |
| 29 | + } |
| 30 | + |
| 31 | + public void setExpression(Expression expression) { |
| 32 | + this.expression = expression; |
| 33 | + } |
| 34 | + |
| 35 | + public Column getColumn() { |
| 36 | + return column; |
| 37 | + } |
| 38 | + |
| 39 | + public void setColumn(Column column) { |
| 40 | + this.column = column; |
| 41 | + } |
| 42 | + |
| 43 | + public boolean isWithValues() { |
| 44 | + return withValues; |
| 45 | + } |
| 46 | + |
| 47 | + public void setWithValues(boolean withValues) { |
| 48 | + this.withValues = withValues; |
| 49 | + } |
| 50 | + |
| 51 | + public DefaultConstraint withExpression(Expression expression) { |
| 52 | + setExpression(expression); |
| 53 | + return this; |
| 54 | + } |
| 55 | + |
| 56 | + public DefaultConstraint withColumn(Column column) { |
| 57 | + setColumn(column); |
| 58 | + return this; |
| 59 | + } |
| 60 | + |
| 61 | + public DefaultConstraint withWithValues(boolean withValues) { |
| 62 | + setWithValues(withValues); |
| 63 | + return this; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public DefaultConstraint withName(String name) { |
| 68 | + setName(name); |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + /** Shares rendering with deparsers while allowing both the value and column to be visited. */ |
| 73 | + public void appendTo(StringBuilder builder, Consumer<Expression> expressionWriter) { |
| 74 | + if (expression == null || column == null) { |
| 75 | + throw new IllegalStateException("DEFAULT requires an expression and a target column"); |
| 76 | + } |
| 77 | + if (getName() != null) { |
| 78 | + builder.append("CONSTRAINT ").append(getName()).append(' '); |
| 79 | + } |
| 80 | + builder.append("DEFAULT "); |
| 81 | + expressionWriter.accept(expression); |
| 82 | + builder.append(" FOR "); |
| 83 | + expressionWriter.accept(column); |
| 84 | + if (withValues) { |
| 85 | + builder.append(" WITH VALUES"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public String toString() { |
| 91 | + StringBuilder builder = new StringBuilder(); |
| 92 | + appendTo(builder, builder::append); |
| 93 | + return builder.toString(); |
| 94 | + } |
| 95 | +} |
0 commit comments