|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2019 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.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import net.sf.jsqlparser.expression.Expression; |
| 15 | +import net.sf.jsqlparser.expression.UserVariable; |
| 16 | +import net.sf.jsqlparser.statement.create.table.ColDataType; |
| 17 | +import net.sf.jsqlparser.statement.create.table.ColumnDefinition; |
| 18 | + |
| 19 | +public final class DeclareStatement implements Statement { |
| 20 | + |
| 21 | + private UserVariable userVariable = null; |
| 22 | + private DeclareType type = DeclareType.TYPE; |
| 23 | + private String typeName; |
| 24 | + private List<TypeDefExpr> typeDefExprList = new ArrayList<>(); |
| 25 | + private List<ColumnDefinition> colDefs = new ArrayList<>(); |
| 26 | + |
| 27 | + public DeclareStatement() { |
| 28 | + } |
| 29 | + |
| 30 | + public void setUserVariable(UserVariable userVariable) { |
| 31 | + this.userVariable = userVariable; |
| 32 | + } |
| 33 | + |
| 34 | + public UserVariable getUserVariable() { |
| 35 | + return userVariable; |
| 36 | + } |
| 37 | + |
| 38 | + public DeclareType getType() { |
| 39 | + return type; |
| 40 | + } |
| 41 | + |
| 42 | + public String getTypeName() { |
| 43 | + return typeName; |
| 44 | + } |
| 45 | + |
| 46 | + public void setDeclareType(DeclareType type) { |
| 47 | + this.type = type; |
| 48 | + } |
| 49 | + |
| 50 | + public void addType(ColDataType colDataType, Expression defaultExpr) { |
| 51 | + typeDefExprList.add(new TypeDefExpr(colDataType, defaultExpr)); |
| 52 | + } |
| 53 | + |
| 54 | + public void addType(UserVariable userVariable, ColDataType colDataType, Expression defaultExpr) { |
| 55 | + typeDefExprList.add(new TypeDefExpr(userVariable, colDataType, defaultExpr)); |
| 56 | + } |
| 57 | + |
| 58 | + public void addColumnDefinition(ColumnDefinition colDef) { |
| 59 | + colDefs.add(colDef); |
| 60 | + } |
| 61 | + |
| 62 | + public List<ColumnDefinition> getColumnDefinitions() { |
| 63 | + return colDefs; |
| 64 | + } |
| 65 | + |
| 66 | + public List<TypeDefExpr> getTypeDefinitions() { |
| 67 | + return typeDefExprList; |
| 68 | + } |
| 69 | + |
| 70 | + public void setTypeName(String typeName) { |
| 71 | + this.typeName = typeName; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public String toString() { |
| 76 | + StringBuilder b = new StringBuilder("DECLARE "); |
| 77 | + if (type == DeclareType.AS) { |
| 78 | + b.append(userVariable.toString()); |
| 79 | + b.append(" AS ").append(typeName); |
| 80 | + } else { |
| 81 | + if (type == DeclareType.TABLE) { |
| 82 | + b.append(userVariable.toString()); |
| 83 | + b.append(" TABLE ("); |
| 84 | + for (int i = 0; i < colDefs.size(); i++) { |
| 85 | + if (i > 0) { |
| 86 | + b.append(", "); |
| 87 | + } |
| 88 | + b.append(colDefs.get(i).toString()); |
| 89 | + } |
| 90 | + b.append(")"); |
| 91 | + } else { |
| 92 | + for (int i = 0; i < typeDefExprList.size(); i++) { |
| 93 | + if (i > 0) { |
| 94 | + b.append(", "); |
| 95 | + } |
| 96 | + final TypeDefExpr type = typeDefExprList.get(i); |
| 97 | + if (type.userVariable != null) { |
| 98 | + b.append(type.userVariable.toString()).append(" "); |
| 99 | + } |
| 100 | + b.append(type.colDataType.toString()); |
| 101 | + if (type.defaultExpr != null) { |
| 102 | + b.append(" = ").append(type.defaultExpr.toString()); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + return b.toString(); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void accept(StatementVisitor statementVisitor |
| 112 | + ) { |
| 113 | + statementVisitor.visit(this); |
| 114 | + } |
| 115 | + |
| 116 | + public static class TypeDefExpr { |
| 117 | + |
| 118 | + public final UserVariable userVariable; |
| 119 | + public final ColDataType colDataType; |
| 120 | + public final Expression defaultExpr; |
| 121 | + |
| 122 | + public TypeDefExpr(ColDataType colDataType, Expression defaultExpr) { |
| 123 | + this(null, colDataType, defaultExpr); |
| 124 | + } |
| 125 | + |
| 126 | + public TypeDefExpr(UserVariable userVariable, ColDataType colDataType, Expression defaultExpr) { |
| 127 | + this.userVariable = userVariable; |
| 128 | + this.colDataType = colDataType; |
| 129 | + this.defaultExpr = defaultExpr; |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments