|
| 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.load; |
| 11 | + |
| 12 | +import net.sf.jsqlparser.expression.operators.relational.ExpressionList; |
| 13 | +import net.sf.jsqlparser.schema.Table; |
| 14 | +import net.sf.jsqlparser.statement.Statement; |
| 15 | +import net.sf.jsqlparser.statement.StatementVisitor; |
| 16 | +import net.sf.jsqlparser.statement.create.table.ColumnDefinition; |
| 17 | +import net.sf.jsqlparser.statement.select.PlainSelect; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +/** |
| 22 | + * BigQuery's {@code LOAD DATA {INTO | OVERWRITE} table [(column_definitions)] [OPTIONS |
| 23 | + * (option_list)] FROM FILES (file_options) [WITH PARTITION COLUMNS] [WITH CONNECTION connection]}. |
| 24 | + * |
| 25 | + * @see <a href= |
| 26 | + * "https://cloud.google.com/bigquery/docs/reference/standard-sql/load-statements">Load |
| 27 | + * statements</a> |
| 28 | + */ |
| 29 | +public class LoadDataStatement implements Statement { |
| 30 | + private boolean overwrite; |
| 31 | + private Table table; |
| 32 | + private List<ColumnDefinition> columnDefinitions; |
| 33 | + private ExpressionList<?> options; |
| 34 | + private ExpressionList<?> fileOptions; |
| 35 | + private boolean withPartitionColumns; |
| 36 | + private String connectionName; |
| 37 | + |
| 38 | + public boolean isOverwrite() { |
| 39 | + return overwrite; |
| 40 | + } |
| 41 | + |
| 42 | + public void setOverwrite(boolean overwrite) { |
| 43 | + this.overwrite = overwrite; |
| 44 | + } |
| 45 | + |
| 46 | + public LoadDataStatement withOverwrite(boolean overwrite) { |
| 47 | + setOverwrite(overwrite); |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + public Table getTable() { |
| 52 | + return table; |
| 53 | + } |
| 54 | + |
| 55 | + public void setTable(Table table) { |
| 56 | + this.table = table; |
| 57 | + } |
| 58 | + |
| 59 | + public LoadDataStatement withTable(Table table) { |
| 60 | + setTable(table); |
| 61 | + return this; |
| 62 | + } |
| 63 | + |
| 64 | + public List<ColumnDefinition> getColumnDefinitions() { |
| 65 | + return columnDefinitions; |
| 66 | + } |
| 67 | + |
| 68 | + public void setColumnDefinitions(List<ColumnDefinition> columnDefinitions) { |
| 69 | + this.columnDefinitions = columnDefinitions; |
| 70 | + } |
| 71 | + |
| 72 | + public LoadDataStatement withColumnDefinitions(List<ColumnDefinition> columnDefinitions) { |
| 73 | + setColumnDefinitions(columnDefinitions); |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + public ExpressionList<?> getOptions() { |
| 78 | + return options; |
| 79 | + } |
| 80 | + |
| 81 | + public void setOptions(ExpressionList<?> options) { |
| 82 | + this.options = options; |
| 83 | + } |
| 84 | + |
| 85 | + public LoadDataStatement withOptions(ExpressionList<?> options) { |
| 86 | + setOptions(options); |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + public ExpressionList<?> getFileOptions() { |
| 91 | + return fileOptions; |
| 92 | + } |
| 93 | + |
| 94 | + public void setFileOptions(ExpressionList<?> fileOptions) { |
| 95 | + this.fileOptions = fileOptions; |
| 96 | + } |
| 97 | + |
| 98 | + public LoadDataStatement withFileOptions(ExpressionList<?> fileOptions) { |
| 99 | + setFileOptions(fileOptions); |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + public boolean isWithPartitionColumns() { |
| 104 | + return withPartitionColumns; |
| 105 | + } |
| 106 | + |
| 107 | + public void setWithPartitionColumns(boolean withPartitionColumns) { |
| 108 | + this.withPartitionColumns = withPartitionColumns; |
| 109 | + } |
| 110 | + |
| 111 | + public LoadDataStatement withWithPartitionColumns(boolean withPartitionColumns) { |
| 112 | + setWithPartitionColumns(withPartitionColumns); |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + public String getConnectionName() { |
| 117 | + return connectionName; |
| 118 | + } |
| 119 | + |
| 120 | + public void setConnectionName(String connectionName) { |
| 121 | + this.connectionName = connectionName; |
| 122 | + } |
| 123 | + |
| 124 | + public LoadDataStatement withConnectionName(String connectionName) { |
| 125 | + setConnectionName(connectionName); |
| 126 | + return this; |
| 127 | + } |
| 128 | + |
| 129 | + public StringBuilder appendTo(StringBuilder builder) { |
| 130 | + builder.append("LOAD DATA ").append(overwrite ? "OVERWRITE " : "INTO ").append(table); |
| 131 | + if (columnDefinitions != null && !columnDefinitions.isEmpty()) { |
| 132 | + builder.append(" ").append(PlainSelect.getStringList(columnDefinitions, true, true)); |
| 133 | + } |
| 134 | + if (options != null) { |
| 135 | + builder.append(" OPTIONS (").append(options).append(")"); |
| 136 | + } |
| 137 | + builder.append(" FROM FILES (").append(fileOptions).append(")"); |
| 138 | + if (withPartitionColumns) { |
| 139 | + builder.append(" WITH PARTITION COLUMNS"); |
| 140 | + } |
| 141 | + if (connectionName != null) { |
| 142 | + builder.append(" WITH CONNECTION ").append(connectionName); |
| 143 | + } |
| 144 | + return builder; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public String toString() { |
| 149 | + return appendTo(new StringBuilder()).toString(); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) { |
| 154 | + return statementVisitor.visit(this, context); |
| 155 | + } |
| 156 | +} |
0 commit comments