|
| 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; |
| 11 | + |
| 12 | +import net.sf.jsqlparser.statement.select.PlainSelect; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +/** |
| 17 | + * DuckDB's {@code ATTACH [DATABASE] [IF NOT EXISTS] path [AS alias] [(options)]}, which adds another |
| 18 | + * database file or remote database to the session. |
| 19 | + * |
| 20 | + * @see <a href="https://duckdb.org/docs/stable/sql/statements/attach">ATTACH</a> |
| 21 | + */ |
| 22 | +public class AttachStatement implements Statement { |
| 23 | + private String databasePath; |
| 24 | + private String alias; |
| 25 | + private boolean usingDatabaseKeyword; |
| 26 | + private boolean ifNotExists; |
| 27 | + private List<String> options; |
| 28 | + |
| 29 | + public String getDatabasePath() { |
| 30 | + return databasePath; |
| 31 | + } |
| 32 | + |
| 33 | + public void setDatabasePath(String databasePath) { |
| 34 | + this.databasePath = databasePath; |
| 35 | + } |
| 36 | + |
| 37 | + public AttachStatement withDatabasePath(String databasePath) { |
| 38 | + setDatabasePath(databasePath); |
| 39 | + return this; |
| 40 | + } |
| 41 | + |
| 42 | + public String getAlias() { |
| 43 | + return alias; |
| 44 | + } |
| 45 | + |
| 46 | + public void setAlias(String alias) { |
| 47 | + this.alias = alias; |
| 48 | + } |
| 49 | + |
| 50 | + public AttachStatement withAlias(String alias) { |
| 51 | + setAlias(alias); |
| 52 | + return this; |
| 53 | + } |
| 54 | + |
| 55 | + public boolean isUsingDatabaseKeyword() { |
| 56 | + return usingDatabaseKeyword; |
| 57 | + } |
| 58 | + |
| 59 | + public void setUsingDatabaseKeyword(boolean usingDatabaseKeyword) { |
| 60 | + this.usingDatabaseKeyword = usingDatabaseKeyword; |
| 61 | + } |
| 62 | + |
| 63 | + public AttachStatement withUsingDatabaseKeyword(boolean usingDatabaseKeyword) { |
| 64 | + setUsingDatabaseKeyword(usingDatabaseKeyword); |
| 65 | + return this; |
| 66 | + } |
| 67 | + |
| 68 | + public boolean isIfNotExists() { |
| 69 | + return ifNotExists; |
| 70 | + } |
| 71 | + |
| 72 | + public void setIfNotExists(boolean ifNotExists) { |
| 73 | + this.ifNotExists = ifNotExists; |
| 74 | + } |
| 75 | + |
| 76 | + public AttachStatement withIfNotExists(boolean ifNotExists) { |
| 77 | + setIfNotExists(ifNotExists); |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + public List<String> getOptions() { |
| 82 | + return options; |
| 83 | + } |
| 84 | + |
| 85 | + public void setOptions(List<String> options) { |
| 86 | + this.options = options; |
| 87 | + } |
| 88 | + |
| 89 | + public AttachStatement withOptions(List<String> options) { |
| 90 | + setOptions(options); |
| 91 | + return this; |
| 92 | + } |
| 93 | + |
| 94 | + public StringBuilder appendTo(StringBuilder builder) { |
| 95 | + builder.append("ATTACH "); |
| 96 | + if (usingDatabaseKeyword) { |
| 97 | + builder.append("DATABASE "); |
| 98 | + } |
| 99 | + if (ifNotExists) { |
| 100 | + builder.append("IF NOT EXISTS "); |
| 101 | + } |
| 102 | + builder.append(databasePath); |
| 103 | + if (alias != null) { |
| 104 | + builder.append(" AS ").append(alias); |
| 105 | + } |
| 106 | + if (options != null && !options.isEmpty()) { |
| 107 | + builder.append(" ").append(PlainSelect.getStringList(options, true, true)); |
| 108 | + } |
| 109 | + return builder; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String toString() { |
| 114 | + return appendTo(new StringBuilder()).toString(); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) { |
| 119 | + return statementVisitor.visit(this, context); |
| 120 | + } |
| 121 | +} |
0 commit comments