|
| 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.insert; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import java.util.function.Consumer; |
| 15 | +import net.sf.jsqlparser.expression.Expression; |
| 16 | +import net.sf.jsqlparser.schema.Table; |
| 17 | +import net.sf.jsqlparser.statement.Statement; |
| 18 | +import net.sf.jsqlparser.statement.StatementVisitor; |
| 19 | +import net.sf.jsqlparser.statement.create.table.ColumnDefinition; |
| 20 | +import net.sf.jsqlparser.statement.select.OrderByElement; |
| 21 | + |
| 22 | +/** SQL Server's SQL declaration preceding a separate bulk-load data stream. */ |
| 23 | +public class InsertBulk implements Statement { |
| 24 | + private Table table; |
| 25 | + private List<ColumnDefinition> columns = new ArrayList<>(); |
| 26 | + private List<Option> options = new ArrayList<>(); |
| 27 | + |
| 28 | + public Table getTable() { |
| 29 | + return table; |
| 30 | + } |
| 31 | + |
| 32 | + public void setTable(Table table) { |
| 33 | + this.table = table; |
| 34 | + } |
| 35 | + |
| 36 | + public List<ColumnDefinition> getColumns() { |
| 37 | + return columns; |
| 38 | + } |
| 39 | + |
| 40 | + public void setColumns(List<ColumnDefinition> columns) { |
| 41 | + this.columns = columns; |
| 42 | + } |
| 43 | + |
| 44 | + public List<Option> getOptions() { |
| 45 | + return options; |
| 46 | + } |
| 47 | + |
| 48 | + public void setOptions(List<Option> options) { |
| 49 | + this.options = options; |
| 50 | + } |
| 51 | + |
| 52 | + public void visitExpressions(Consumer<Expression> visitor) { |
| 53 | + for (Option option : options) { |
| 54 | + if (option.getValue() != null) { |
| 55 | + visitor.accept(option.getValue()); |
| 56 | + } |
| 57 | + for (OrderByElement order : option.getOrderByElements()) { |
| 58 | + visitor.accept(order.getExpression()); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public StringBuilder appendTo(StringBuilder builder, Consumer<Expression> expressions, |
| 64 | + Consumer<OrderByElement> ordering) { |
| 65 | + builder.append("INSERT BULK ").append(table).append(" ("); |
| 66 | + for (int i = 0; i < columns.size(); i++) { |
| 67 | + if (i > 0) { |
| 68 | + builder.append(", "); |
| 69 | + } |
| 70 | + builder.append(columns.get(i)); |
| 71 | + } |
| 72 | + builder.append(')'); |
| 73 | + if (!options.isEmpty()) { |
| 74 | + builder.append(" WITH ("); |
| 75 | + for (int i = 0; i < options.size(); i++) { |
| 76 | + if (i > 0) { |
| 77 | + builder.append(", "); |
| 78 | + } |
| 79 | + options.get(i).appendTo(builder, expressions, ordering); |
| 80 | + } |
| 81 | + builder.append(')'); |
| 82 | + } |
| 83 | + return builder; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public <T, S> T accept(StatementVisitor<T> visitor, S context) { |
| 88 | + return visitor.visit(this, context); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public String toString() { |
| 93 | + StringBuilder builder = new StringBuilder(); |
| 94 | + return appendTo(builder, value -> builder.append(value), order -> builder.append(order)) |
| 95 | + .toString(); |
| 96 | + } |
| 97 | + |
| 98 | + public static class Option { |
| 99 | + public enum Kind { |
| 100 | + CHECK_CONSTRAINTS, FIRE_TRIGGERS, KEEP_NULLS, TABLOCK, ALLOW_ENCRYPTED_VALUE_MODIFICATIONS, ROWS_PER_BATCH, ORDER |
| 101 | + } |
| 102 | + |
| 103 | + private Kind kind; |
| 104 | + private Expression value; |
| 105 | + private List<OrderByElement> orderByElements = new ArrayList<>(); |
| 106 | + |
| 107 | + public Option(Kind kind) { |
| 108 | + this.kind = kind; |
| 109 | + } |
| 110 | + |
| 111 | + public Kind getKind() { |
| 112 | + return kind; |
| 113 | + } |
| 114 | + |
| 115 | + public void setKind(Kind kind) { |
| 116 | + this.kind = kind; |
| 117 | + } |
| 118 | + |
| 119 | + public Expression getValue() { |
| 120 | + return value; |
| 121 | + } |
| 122 | + |
| 123 | + public void setValue(Expression value) { |
| 124 | + this.value = value; |
| 125 | + } |
| 126 | + |
| 127 | + public List<OrderByElement> getOrderByElements() { |
| 128 | + return orderByElements; |
| 129 | + } |
| 130 | + |
| 131 | + public void setOrderByElements(List<OrderByElement> orderByElements) { |
| 132 | + this.orderByElements = orderByElements; |
| 133 | + } |
| 134 | + |
| 135 | + public StringBuilder appendTo(StringBuilder builder, Consumer<Expression> expressions, |
| 136 | + Consumer<OrderByElement> ordering) { |
| 137 | + builder.append(kind); |
| 138 | + if (value != null) { |
| 139 | + builder.append(" = "); |
| 140 | + expressions.accept(value); |
| 141 | + } |
| 142 | + if (!orderByElements.isEmpty()) { |
| 143 | + builder.append(" ("); |
| 144 | + for (int i = 0; i < orderByElements.size(); i++) { |
| 145 | + if (i > 0) { |
| 146 | + builder.append(", "); |
| 147 | + } |
| 148 | + ordering.accept(orderByElements.get(i)); |
| 149 | + } |
| 150 | + builder.append(')'); |
| 151 | + } |
| 152 | + return builder; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public String toString() { |
| 157 | + StringBuilder builder = new StringBuilder(); |
| 158 | + return appendTo(builder, value -> builder.append(value), order -> builder.append(order)) |
| 159 | + .toString(); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments