|
| 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.create.database; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Collection; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Optional; |
| 17 | + |
| 18 | +import net.sf.jsqlparser.statement.Statement; |
| 19 | +import net.sf.jsqlparser.statement.StatementVisitor; |
| 20 | + |
| 21 | +/** |
| 22 | + * A {@code CREATE DATABASE} statement. Modelled after MySQL, where the statement takes an optional |
| 23 | + * {@code IF NOT EXISTS} and vendor-specific options, which are captured verbatim. |
| 24 | + */ |
| 25 | +public class CreateDatabase implements Statement { |
| 26 | + |
| 27 | + private String databaseName; |
| 28 | + private boolean hasIfNotExists = false; |
| 29 | + private List<String> databaseOptions = null; |
| 30 | + |
| 31 | + @Override |
| 32 | + public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) { |
| 33 | + return statementVisitor.visit(this, context); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * The name of the database. |
| 38 | + * |
| 39 | + * @return the database name |
| 40 | + */ |
| 41 | + public String getDatabaseName() { |
| 42 | + return databaseName; |
| 43 | + } |
| 44 | + |
| 45 | + public CreateDatabase setDatabaseName(String databaseName) { |
| 46 | + this.databaseName = databaseName; |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + public boolean hasIfNotExists() { |
| 51 | + return hasIfNotExists; |
| 52 | + } |
| 53 | + |
| 54 | + public CreateDatabase setIfNotExists(boolean hasIfNotExists) { |
| 55 | + this.hasIfNotExists = hasIfNotExists; |
| 56 | + return this; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * The vendor-specific options of the database, captured as raw tokens. |
| 61 | + * |
| 62 | + * @return the list of option tokens, {@code null} when no options were given |
| 63 | + */ |
| 64 | + public List<String> getDatabaseOptions() { |
| 65 | + return databaseOptions; |
| 66 | + } |
| 67 | + |
| 68 | + public CreateDatabase setDatabaseOptions(List<String> databaseOptions) { |
| 69 | + this.databaseOptions = databaseOptions; |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + public CreateDatabase withDatabaseName(String databaseName) { |
| 74 | + return this.setDatabaseName(databaseName); |
| 75 | + } |
| 76 | + |
| 77 | + public CreateDatabase withIfNotExists(boolean hasIfNotExists) { |
| 78 | + return this.setIfNotExists(hasIfNotExists); |
| 79 | + } |
| 80 | + |
| 81 | + public CreateDatabase withDatabaseOptions(List<String> databaseOptions) { |
| 82 | + return this.setDatabaseOptions(databaseOptions); |
| 83 | + } |
| 84 | + |
| 85 | + public CreateDatabase addDatabaseOptions(String... databaseOptions) { |
| 86 | + List<String> collection = |
| 87 | + Optional.ofNullable(getDatabaseOptions()).orElseGet(ArrayList::new); |
| 88 | + Collections.addAll(collection, databaseOptions); |
| 89 | + return this.withDatabaseOptions(collection); |
| 90 | + } |
| 91 | + |
| 92 | + public CreateDatabase addDatabaseOptions(Collection<String> databaseOptions) { |
| 93 | + List<String> collection = |
| 94 | + Optional.ofNullable(getDatabaseOptions()).orElseGet(ArrayList::new); |
| 95 | + collection.addAll(databaseOptions); |
| 96 | + return this.withDatabaseOptions(collection); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public String toString() { |
| 101 | + String sql = "CREATE DATABASE"; |
| 102 | + if (hasIfNotExists) { |
| 103 | + sql += " IF NOT EXISTS"; |
| 104 | + } |
| 105 | + if (databaseName != null) { |
| 106 | + sql += " " + databaseName; |
| 107 | + } |
| 108 | + if (databaseOptions != null && !databaseOptions.isEmpty()) { |
| 109 | + sql += " " + String.join(" ", databaseOptions); |
| 110 | + } |
| 111 | + return sql; |
| 112 | + } |
| 113 | +} |
0 commit comments