use of net.sf.jsqlparser.statement.create.table.Index 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.Index in project herddb by diennea.
the class JdbcQueryRewriter method visit.
@Override
public void visit(CreateIndex createIndex) {
visit(createIndex.getTable());
Index index = createIndex.getIndex();
visit(index);
}
use of net.sf.jsqlparser.statement.create.table.Index in project herddb by diennea.
the class JSQLParserPlanner method buildCreateTableStatement.
private Statement buildCreateTableStatement(String defaultTableSpace, CreateTable s) throws StatementExecutionException {
String tableSpace = fixMySqlBackTicks(s.getTable().getSchemaName());
String tableName = fixMySqlBackTicks(s.getTable().getName());
if (tableSpace == null) {
tableSpace = defaultTableSpace;
}
if (s.getColumnDefinitions() == null) {
throw new StatementExecutionException("A table must have at least 1 column");
}
final boolean isNotExsists = s.isIfNotExists();
try {
boolean foundPk = false;
Table.Builder tablebuilder = Table.builder().uuid(UUID.randomUUID().toString()).name(tableName).tablespace(tableSpace);
Set<String> primaryKey = new HashSet<>();
Set<String> simpleUniqueFields = new HashSet<>();
if (s.getIndexes() != null) {
for (Index index : s.getIndexes()) {
if (index.getType().equalsIgnoreCase("PRIMARY KEY")) {
for (String n : index.getColumnsNames()) {
n = fixMySqlBackTicks(n.toLowerCase());
tablebuilder.primaryKey(n);
primaryKey.add(n);
foundPk = true;
}
}
}
}
int position = 0;
for (ColumnDefinition cf : s.getColumnDefinitions()) {
String columnName = fixMySqlBackTicks(cf.getColumnName().toLowerCase());
int type;
String dataType = cf.getColDataType().getDataType();
List<String> columnSpecs = decodeColumnSpecs(cf.getColumnSpecs());
type = sqlDataTypeToColumnType(dataType, cf.getColDataType().getArgumentsStringList(), columnSpecs);
Bytes defaultValue = decodeDefaultValue(cf, type);
if (!columnSpecs.isEmpty()) {
boolean auto_increment = decodeAutoIncrement(columnSpecs);
if (columnSpecs.contains("PRIMARY")) {
foundPk = true;
tablebuilder.primaryKey(columnName, auto_increment);
}
if (auto_increment && primaryKey.contains(columnName)) {
tablebuilder.primaryKey(columnName, auto_increment);
}
boolean isUnique = columnSpecs.contains("UNIQUE");
if (isUnique) {
simpleUniqueFields.add(columnName);
}
}
tablebuilder.column(columnName, type, position++, defaultValue);
}
if (!foundPk) {
tablebuilder.column("_pk", ColumnTypes.LONG, position++, null);
tablebuilder.primaryKey("_pk", true);
}
Table table = tablebuilder.build();
List<herddb.model.Index> otherIndexes = new ArrayList<>();
List<herddb.model.ForeignKeyDef> foreignKeys = new ArrayList<>();
if (s.getIndexes() != null) {
for (Index index : s.getIndexes()) {
if (index.getType().equalsIgnoreCase("PRIMARY KEY")) {
} else if (index.getType().equalsIgnoreCase("INDEX") || index.getType().equalsIgnoreCase("KEY") || index.getType().equalsIgnoreCase("UNIQUE KEY")) {
String indexName = fixMySqlBackTicks(index.getName().toLowerCase());
String indexType = convertIndexType(null);
boolean unique = index.getType().equalsIgnoreCase("UNIQUE KEY");
herddb.model.Index.Builder builder = herddb.model.Index.builder().onTable(table).name(indexName).unique(unique).type(indexType).uuid(UUID.randomUUID().toString());
for (String columnName : index.getColumnsNames()) {
columnName = fixMySqlBackTicks(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());
} else if (index.getType().equals("FOREIGN KEY")) {
ForeignKeyIndex fk = (ForeignKeyIndex) index;
ForeignKeyDef fkDef = parseForeignKeyIndex(fk, table, tableName, tableSpace);
foreignKeys.add(fkDef);
} else {
throw new StatementExecutionException("Unsupported index type " + index.getType());
}
}
}
for (String col : simpleUniqueFields) {
herddb.model.Index.Builder builder = herddb.model.Index.builder().onTable(table).name(table.name + "_unique_" + col).unique(true).type(herddb.model.Index.TYPE_BRIN).uuid(UUID.randomUUID().toString()).column(col, table.getColumn(col).type);
otherIndexes.add(builder.build());
}
if (!foreignKeys.isEmpty()) {
table = table.withForeignKeys(foreignKeys.toArray(new ForeignKeyDef[0]));
}
CreateTableStatement statement = new CreateTableStatement(table, otherIndexes, isNotExsists);
return statement;
} catch (IllegalArgumentException err) {
throw new StatementExecutionException("bad table definition: " + err.getMessage(), err);
}
}
use of net.sf.jsqlparser.statement.create.table.Index in project herddb by diennea.
the class JSQLParserPlanner method buildCreateIndexStatement.
private Statement buildCreateIndexStatement(String defaultTableSpace, CreateIndex s) throws StatementExecutionException {
try {
String tableSpace = s.getTable().getSchemaName();
if (tableSpace == null) {
tableSpace = defaultTableSpace;
}
tableSpace = fixMySqlBackTicks(tableSpace);
String tableName = fixMySqlBackTicks(s.getTable().getName().toLowerCase());
String indexName = fixMySqlBackTicks(s.getIndex().getName().toLowerCase());
boolean unique = isUnique(s.getIndex().getType());
String indexType = convertIndexType(s.getIndex().getType());
herddb.model.Index.Builder builder = herddb.model.Index.builder().name(indexName).uuid(UUID.randomUUID().toString()).type(indexType).unique(unique).table(tableName).tablespace(tableSpace);
AbstractTableManager tableDefinition = manager.getTableSpaceManager(tableSpace).getTableManager(tableName);
if (tableDefinition == null) {
throw new TableDoesNotExistException("no such table " + tableName + " in tablespace " + tableSpace);
}
for (String columnName : s.getIndex().getColumnsNames()) {
columnName = fixMySqlBackTicks(columnName.toLowerCase());
Column column = tableDefinition.getTable().getColumn(columnName);
if (column == null) {
throw new StatementExecutionException("no such column " + columnName + " on table " + tableName + " in tablespace " + tableSpace);
}
builder.column(column.name, column.type);
}
CreateIndexStatement statement = new CreateIndexStatement(builder.build());
return statement;
} catch (IllegalArgumentException err) {
throw new StatementExecutionException("bad index definition: " + err.getMessage(), err);
}
}
use of net.sf.jsqlparser.statement.create.table.Index 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();
}
}
}
Aggregations