alibaba / innodb-java-reader

A library and command-line tool to access MySQL InnoDB data file directly in Java
Apache License 2.0
462 stars 115 forks source link

Support UNIQUE KEY without a name in create table syntax #36

Closed s-sathish closed 3 years ago

s-sathish commented 3 years ago

Hi, I have the following create table syntax - CREATE TABLE innodb_java_reader_test1 ( number VARCHAR(16) NOT NULL, testnumber VARCHAR(16) NOT NULL, UNIQUE KEY (number, testnumber) ); The above syntax is stored in "~/sathishFiles/innodb_java_reader_test1.sql" file as well for use in the cli command below.

And when it is created in MySQL, it looks like this - CREATE TABLE innodb_java_reader_test1 ( number varchar(16) NOT NULL, testnumber varchar(16) NOT NULL, UNIQUE KEY number (number,testnumber) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

As we can see, the unique key name will default to the first column name.

Now when I do the java -jar innodb-java-reader-cli/target/innodb-java-reader-cli.jar -ibd-file-path /usr/local/var/mysql/innodb_java_reader/innodb_java_reader_test1.ibd -create-table-sql-file-path ~/sathishFiles/innodb_java_reader_test1.sql -c query-all -o ~/sathishFiles/innodb_java_reader_test1

It fails with the following exception - Caused by: java.lang.IllegalArgumentException: Key name should not be empty It's failing because of the missing key name for the unique key in the create table syntax

As MySQL does create a name for the key if we don't specify it explicitly, can't we do the same at our end ? So if indexName is null, then we just use the firstColumnName as indexName. With this change, the command was able to dump the records from the table successfully.

Let me know if this makes sense.