Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

MySQL can setup indices on a table during the CREATE TABLE statement. #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ private void handleCreateTableConstraints(CreateTableStatement createStatement,
delayedStatements.add(new AlterTableStatement(false, createStatement.getTableName(), Arrays.asList(new AlterTableSpecification("ADD", constraint))));
it.remove();
}
if (constraint.getType().equals("KEY")) {
// TODO Translate index types
delayedStatements.add(new CreateIndexStatement(false, null, false, constraint.getIndexName(), createStatement.getTableName(), constraint.getIndexColumnNames()));
it.remove();
}

constraint.setIndexType(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ public void testScript5() throws Exception {
loadScript("xwiki-sqlyog.sql");
}

@Test
public void testScriptCreateTableWithKey() throws Exception {
loadScript("create-table-with-key.sql");
}

private void loadScript(String s) throws Exception {
long time0 = System.currentTimeMillis();

Expand Down
10 changes: 10 additions & 0 deletions converter/src/test/resources/scripts/create-table-with-key.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

CREATE TABLE `sample` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`VALUE` text,
`TIME` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`version` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
UNIQUE KEY `PK_SAMPLE` (`ID`),
KEY `TIME` (`TIME`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='A sample table.';
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.granveaud.mysql2h2converter.sql;

import com.google.common.base.Joiner;

import java.util.List;

/*
CREATE
[ UNIQUE ] [ index_type ] INDEX [ [ IF NOT EXISTS ] index_name ]
| PRIMARY KEY [ HASH ]
ON tbl_name (index_col_name,...)

index_type:
HASH | SPATIAL

index_col_name:
col_name [ASC | DESC] [ NULLS

*/
public class CreateIndexStatement implements Statement {
private boolean unique, ifNotExists;
private String tableName;
private String indexType;
private String indexName;
private List<ColumnName> columnNames;

public CreateIndexStatement(boolean unique, String indexType, boolean ifNotExists, String indexName, String tableName, List<ColumnName> columnNames) {
this.unique = unique;
this.indexName = indexName;
this.indexType = indexType;
this.ifNotExists = ifNotExists;
this.tableName = tableName;
this.columnNames = columnNames;
}

@Override
public String toString() {
return "CREATE" + (unique ? " UNIQUE": "") + ( indexType != null ? " " + indexType : "" ) + " INDEX" + (ifNotExists ? " IF NOT EXISTS" : "") +
( indexName != null ? " " + indexName : "" ) +
" ON " + tableName + " (" + Joiner.on(',').join(columnNames) + ")"
;
}
}