|
| 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.foreign; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Objects; |
| 15 | +import java.util.function.Consumer; |
| 16 | +import net.sf.jsqlparser.expression.Expression; |
| 17 | +import net.sf.jsqlparser.schema.TableReference; |
| 18 | +import net.sf.jsqlparser.statement.StatementVisitor; |
| 19 | + |
| 20 | +/** |
| 21 | + * PostgreSQL {@code IMPORT FOREIGN SCHEMA remote_schema [{LIMIT TO | EXCEPT} (table [, ...])] FROM |
| 22 | + * SERVER server INTO local_schema [OPTIONS (option 'value' [, ...])]}, which creates foreign tables |
| 23 | + * in the local schema for the tables of a schema on the foreign server. |
| 24 | + * |
| 25 | + * @see <a href="https://www.postgresql.org/docs/18/sql-importforeignschema.html">IMPORT FOREIGN |
| 26 | + * SCHEMA</a> |
| 27 | + */ |
| 28 | +public class ImportForeignSchema extends ForeignDataStatement { |
| 29 | + private String remoteSchema; |
| 30 | + private Qualification qualification; |
| 31 | + private final List<TableReference> tables = new ArrayList<>(); |
| 32 | + private String server; |
| 33 | + private String localSchema; |
| 34 | + |
| 35 | + /** How the table list restricts the import. */ |
| 36 | + public enum Qualification { |
| 37 | + /** Import only the listed tables. */ |
| 38 | + LIMIT_TO("LIMIT TO"), |
| 39 | + /** Import every table but the listed ones. */ |
| 40 | + EXCEPT("EXCEPT"); |
| 41 | + |
| 42 | + private final String sql; |
| 43 | + |
| 44 | + Qualification(String sql) { |
| 45 | + this.sql = sql; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public String toString() { |
| 50 | + return sql; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public String getRemoteSchema() { |
| 55 | + return remoteSchema; |
| 56 | + } |
| 57 | + |
| 58 | + public void setRemoteSchema(String remoteSchema) { |
| 59 | + this.remoteSchema = remoteSchema; |
| 60 | + } |
| 61 | + |
| 62 | + public ImportForeignSchema withRemoteSchema(String remoteSchema) { |
| 63 | + setRemoteSchema(remoteSchema); |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + /** The kind of the table list, or null when the whole remote schema is imported. */ |
| 68 | + public Qualification getQualification() { |
| 69 | + return qualification; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * The tables of the remote schema that LIMIT TO or EXCEPT lists; empty without a qualification. |
| 74 | + * The list is a read-only snapshot, change it with {@link #setTables(Qualification, List)}. |
| 75 | + */ |
| 76 | + public List<TableReference> getTables() { |
| 77 | + return List.copyOf(tables); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Restricts the import to the listed tables or excludes them. |
| 82 | + * |
| 83 | + * @param qualification the kind of the list, null to import the whole remote schema |
| 84 | + * @param tables the listed tables; at least one with a qualification, none without |
| 85 | + */ |
| 86 | + public void setTables(Qualification qualification, List<TableReference> tables) { |
| 87 | + List<TableReference> copy = tables == null ? List.of() : List.copyOf(tables); |
| 88 | + if (qualification == null != copy.isEmpty()) { |
| 89 | + throw new IllegalArgumentException( |
| 90 | + "LIMIT TO and EXCEPT need a table list, and a table list needs one of them"); |
| 91 | + } |
| 92 | + this.qualification = qualification; |
| 93 | + this.tables.clear(); |
| 94 | + this.tables.addAll(copy); |
| 95 | + } |
| 96 | + |
| 97 | + public ImportForeignSchema withTables(Qualification qualification, |
| 98 | + List<TableReference> tables) { |
| 99 | + setTables(qualification, tables); |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + public String getServer() { |
| 104 | + return server; |
| 105 | + } |
| 106 | + |
| 107 | + public void setServer(String server) { |
| 108 | + this.server = server; |
| 109 | + } |
| 110 | + |
| 111 | + public ImportForeignSchema withServer(String server) { |
| 112 | + setServer(server); |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + public String getLocalSchema() { |
| 117 | + return localSchema; |
| 118 | + } |
| 119 | + |
| 120 | + public void setLocalSchema(String localSchema) { |
| 121 | + this.localSchema = localSchema; |
| 122 | + } |
| 123 | + |
| 124 | + public ImportForeignSchema withLocalSchema(String localSchema) { |
| 125 | + setLocalSchema(localSchema); |
| 126 | + return this; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public <T, S> T accept(StatementVisitor<T> visitor, S context) { |
| 131 | + return visitor.visit(this, context); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public StringBuilder appendTo(StringBuilder sql, Consumer<Expression> printer) { |
| 136 | + sql.append("IMPORT FOREIGN SCHEMA ").append(Objects.requireNonNull(remoteSchema)); |
| 137 | + if (qualification != null) { |
| 138 | + sql.append(' ').append(qualification).append(" ("); |
| 139 | + for (int i = 0; i < tables.size(); i++) { |
| 140 | + if (i > 0) { |
| 141 | + sql.append(", "); |
| 142 | + } |
| 143 | + tables.get(i).appendTo(sql); |
| 144 | + } |
| 145 | + sql.append(')'); |
| 146 | + } |
| 147 | + sql.append(" FROM SERVER ").append(Objects.requireNonNull(server)); |
| 148 | + sql.append(" INTO ").append(Objects.requireNonNull(localSchema)); |
| 149 | + appendOptionsTo(sql, printer); |
| 150 | + return sql; |
| 151 | + } |
| 152 | +} |
0 commit comments