net.sf.jsqlparser.statement.create.table.CreateTable Maven / Gradle / Ivy
Go to download
JSqlParser parses an SQL statement and translate it into a hierarchy of Java classes.
The generated hierarchy can be navigated using the Visitor Pattern.
/*
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2013 JSQLParser
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
package net.sf.jsqlparser.statement.create.table;
import java.util.List;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectBody;
/**
* A "CREATE TABLE" statement
*/
public class CreateTable implements Statement {
private Table table;
private boolean unlogged = false;
private List createOptionsStrings;
private List tableOptionsStrings;
private List columnDefinitions;
private List indexes;
private Select select;
private boolean ifNotExists = false;
@Override
public void accept(StatementVisitor statementVisitor) {
statementVisitor.visit(this);
}
/**
* The name of the table to be created
*/
public Table getTable() {
return table;
}
public void setTable(Table table) {
this.table = table;
}
/**
* Whether the table is unlogged or not (PostgreSQL 9.1+ feature)
* @return
*/
public boolean isUnlogged() { return unlogged; }
public void setUnlogged(boolean unlogged) { this.unlogged = unlogged; }
/**
* A list of {@link ColumnDefinition}s of this table.
*/
public List getColumnDefinitions() {
return columnDefinitions;
}
public void setColumnDefinitions(List list) {
columnDefinitions = list;
}
/**
* A list of options (as simple strings) of this table definition, as
* ("TYPE", "=", "MYISAM")
*/
public List> getTableOptionsStrings() {
return tableOptionsStrings;
}
public void setTableOptionsStrings(List list) {
tableOptionsStrings = list;
}
public List getCreateOptionsStrings() {
return createOptionsStrings;
}
public void setCreateOptionsStrings(List createOptionsStrings) {
this.createOptionsStrings = createOptionsStrings;
}
/**
* A list of {@link Index}es (for example "PRIMARY KEY") of this table.
* Indexes created with column definitions (as in mycol INT PRIMARY KEY) are
* not inserted into this list.
*/
public List getIndexes() {
return indexes;
}
public void setIndexes(List list) {
indexes = list;
}
public Select getSelect() {
return select;
}
public void setSelect(Select select) {
this.select = select;
}
public boolean isIfNotExists() {
return ifNotExists;
}
public void setIfNotExists(boolean ifNotExists) {
this.ifNotExists = ifNotExists;
}
@Override
public String toString() {
String sql;
String createOps = PlainSelect.getStringList(createOptionsStrings, false, false);
sql = "CREATE " + (unlogged ? "UNLOGGED " : "") +
(!"".equals(createOps)?createOps + " ":"") +
"TABLE " + (ifNotExists?"IF NOT EXISTS ":"") + table;
if (select != null) {
sql += " AS " + select.toString();
} else {
sql += " (";
sql += PlainSelect.getStringList(columnDefinitions, true, false);
if (indexes != null && !indexes.isEmpty()) {
sql += ", ";
sql += PlainSelect.getStringList(indexes);
}
sql += ")";
String options = PlainSelect.getStringList(tableOptionsStrings, false, false);
if (options != null && options.length() > 0) {
sql += " " + options;
}
}
return sql;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy