use of net.sf.jsqlparser.statement.create.table.ColumnDefinition in project herddb by diennea.
the class SQLPlanner method buildCreateTableStatement.
private Statement buildCreateTableStatement(String defaultTableSpace, CreateTable s) throws StatementExecutionException {
String tableSpace = s.getTable().getSchemaName();
String tableName = s.getTable().getName();
if (tableSpace == null) {
tableSpace = defaultTableSpace;
}
try {
boolean foundPk = false;
Table.Builder tablebuilder = Table.builder().uuid(UUID.randomUUID().toString()).name(tableName).tablespace(tableSpace);
Set<String> primaryKey = new HashSet<>();
if (s.getIndexes() != null) {
for (Index index : s.getIndexes()) {
if (index.getType().equalsIgnoreCase("PRIMARY KEY")) {
for (String n : index.getColumnsNames()) {
n = n.toLowerCase();
tablebuilder.primaryKey(n);
primaryKey.add(n);
foundPk = true;
}
}
}
}
int position = 0;
for (ColumnDefinition cf : s.getColumnDefinitions()) {
String columnName = cf.getColumnName().toLowerCase();
int type;
String dataType = cf.getColDataType().getDataType();
type = sqlDataTypeToColumnType(dataType, cf.getColDataType().getArgumentsStringList());
tablebuilder.column(columnName, type, position++);
if (cf.getColumnSpecStrings() != null) {
List<String> columnSpecs = decodeColumnSpecs(cf.getColumnSpecStrings());
boolean auto_increment = decodeAutoIncrement(columnSpecs);
if (columnSpecs.contains("PRIMARY")) {
foundPk = true;
tablebuilder.primaryKey(columnName, auto_increment);
}
if (auto_increment && primaryKey.contains(cf.getColumnName())) {
tablebuilder.primaryKey(columnName, auto_increment);
}
}
}
if (!foundPk) {
tablebuilder.column("_pk", ColumnTypes.LONG, position++);
tablebuilder.primaryKey("_pk", true);
}
Table table = tablebuilder.build();
List<herddb.model.Index> otherIndexes = new ArrayList<>();
if (s.getIndexes() != null) {
for (Index index : s.getIndexes()) {
if (index.getType().equalsIgnoreCase("PRIMARY KEY")) {
} else if (index.getType().equalsIgnoreCase("INDEX")) {
String indexName = index.getName().toLowerCase();
String indexType = convertIndexType(null);
herddb.model.Index.Builder builder = herddb.model.Index.builder().name(indexName).type(indexType).uuid(UUID.randomUUID().toString()).table(tableName).tablespace(tableSpace);
for (String columnName : index.getColumnsNames()) {
columnName = columnName.toLowerCase();
Column column = table.getColumn(columnName);
if (column == null) {
throw new StatementExecutionException("no such column " + columnName + " on table " + tableName + " in tablespace " + tableSpace);
}
builder.column(column.name, column.type);
}
otherIndexes.add(builder.build());
}
}
}
CreateTableStatement statement = new CreateTableStatement(table, otherIndexes);
return statement;
} catch (IllegalArgumentException err) {
throw new StatementExecutionException("bad table definition: " + err.getMessage(), err);
}
}
use of net.sf.jsqlparser.statement.create.table.ColumnDefinition in project JSqlParser by JSQLParser.
the class CreateTableTest method testRUBiSCreateList.
public void testRUBiSCreateList() throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(CreateTableTest.class.getResourceAsStream("/RUBiS-create-requests.txt")));
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
try {
int numSt = 1;
while (true) {
String line = getLine(in);
if (line == null) {
break;
}
if (!"#begin".equals(line)) {
break;
}
line = getLine(in);
StringBuilder buf = new StringBuilder(line);
while (true) {
line = getLine(in);
if ("#end".equals(line)) {
break;
}
buf.append("\n");
buf.append(line);
}
String query = buf.toString();
if (!getLine(in).equals("true")) {
continue;
}
String tableName = getLine(in);
String cols = getLine(in);
try {
CreateTable createTable = (CreateTable) parserManager.parse(new StringReader(query));
String[] colsList = null;
if ("null".equals(cols)) {
colsList = new String[0];
} else {
StringTokenizer tokenizer = new StringTokenizer(cols, " ");
List colsListList = new ArrayList();
while (tokenizer.hasMoreTokens()) {
colsListList.add(tokenizer.nextToken());
}
colsList = (String[]) colsListList.toArray(new String[colsListList.size()]);
}
List colsFound = new ArrayList();
if (createTable.getColumnDefinitions() != null) {
for (Iterator iter = createTable.getColumnDefinitions().iterator(); iter.hasNext(); ) {
ColumnDefinition columnDefinition = (ColumnDefinition) iter.next();
String colName = columnDefinition.getColumnName();
boolean unique = false;
if (createTable.getIndexes() != null) {
for (Iterator iterator = createTable.getIndexes().iterator(); iterator.hasNext(); ) {
Index index = (Index) iterator.next();
if (index.getType().equals("PRIMARY KEY") && index.getColumnsNames().size() == 1 && index.getColumnsNames().get(0).equals(colName)) {
unique = true;
}
}
}
if (!unique) {
if (columnDefinition.getColumnSpecStrings() != null) {
for (Iterator iterator = columnDefinition.getColumnSpecStrings().iterator(); iterator.hasNext(); ) {
String par = (String) iterator.next();
if (par.equals("UNIQUE")) {
unique = true;
} else if (par.equals("PRIMARY") && iterator.hasNext() && iterator.next().equals("KEY")) {
unique = true;
}
}
}
}
if (unique) {
colName += ".unique";
}
colsFound.add(colName.toLowerCase());
}
}
assertEquals("stm:" + query, colsList.length, colsFound.size());
for (int i = 0; i < colsList.length; i++) {
assertEquals("stm:" + query, colsList[i], colsFound.get(i));
}
} catch (Exception e) {
throw new TestException("error at stm num: " + numSt + " " + query, e);
}
numSt++;
}
} finally {
if (in != null) {
in.close();
}
}
}
use of net.sf.jsqlparser.statement.create.table.ColumnDefinition in project JSqlParser by JSQLParser.
the class CreateTableDeParser method deParse.
public void deParse(CreateTable createTable) {
buffer.append("CREATE ");
if (createTable.isUnlogged()) {
buffer.append("UNLOGGED ");
}
String params = PlainSelect.getStringList(createTable.getCreateOptionsStrings(), false, false);
if (!"".equals(params)) {
buffer.append(params).append(' ');
}
buffer.append("TABLE ");
if (createTable.isIfNotExists()) {
buffer.append("IF NOT EXISTS ");
}
buffer.append(createTable.getTable().getFullyQualifiedName());
if (createTable.getSelect() != null) {
buffer.append(" AS ");
if (createTable.isSelectParenthesis()) {
buffer.append("(");
}
buffer.append(createTable.getSelect().toString());
if (createTable.isSelectParenthesis()) {
buffer.append(")");
}
} else {
if (createTable.getColumnDefinitions() != null) {
buffer.append(" (");
for (Iterator<ColumnDefinition> iter = createTable.getColumnDefinitions().iterator(); iter.hasNext(); ) {
ColumnDefinition columnDefinition = iter.next();
buffer.append(columnDefinition.getColumnName());
buffer.append(" ");
buffer.append(columnDefinition.getColDataType().toString());
if (columnDefinition.getColumnSpecStrings() != null) {
for (String s : columnDefinition.getColumnSpecStrings()) {
buffer.append(" ");
buffer.append(s);
}
}
if (iter.hasNext()) {
buffer.append(", ");
}
}
if (createTable.getIndexes() != null) {
for (Iterator<Index> iter = createTable.getIndexes().iterator(); iter.hasNext(); ) {
buffer.append(", ");
Index index = iter.next();
buffer.append(index.toString());
}
}
buffer.append(")");
}
}
params = PlainSelect.getStringList(createTable.getTableOptionsStrings(), false, false);
if (!"".equals(params)) {
buffer.append(' ').append(params);
}
}
Aggregations