Skip to content

Commit

Permalink
fixes #661
Browse files Browse the repository at this point in the history
  • Loading branch information
wumpz committed Aug 17, 2018
1 parent 78ff2ad commit 44ea8bd
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 12 deletions.
7 changes: 6 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -3358,7 +3358,12 @@ List<String> CreateParameter():
tk=<K_TEMP> { param.add(tk.image); }
|
tk=<K_TEMPORARY> { param.add(tk.image); }

|
tk=<K_PARTITION> { param.add(tk.image); }
|
tk=<K_BY> { param.add(tk.image); }
|
tk=<K_IN> { param.add(tk.image); }
)
{return param;}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,47 @@
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import junit.framework.TestCase;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import net.sf.jsqlparser.statement.create.table.Index;
import net.sf.jsqlparser.test.TestException;
import net.sf.jsqlparser.util.TablesNamesFinder;
import static net.sf.jsqlparser.test.TestUtils.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

public class CreateTableTest extends TestCase {

private CCJSqlParserManager parserManager = new CCJSqlParserManager();
public class CreateTableTest {

public CreateTableTest(String arg0) {
super(arg0);
}
private final CCJSqlParserManager parserManager = new CCJSqlParserManager();

@Test
public void testCreateTable2() throws JSQLParserException {
String statement = "CREATE TABLE testtab (\"test\" varchar (255))";
assertSqlCanBeParsedAndDeparsed(statement);
}

@Test
public void testCreateTable3() throws JSQLParserException {
String statement = "CREATE TABLE testtab (\"test\" varchar (255), \"test2\" varchar (255))";
assertSqlCanBeParsedAndDeparsed(statement);
}

@Test
public void testCreateTableAsSelect() throws JSQLParserException, JSQLParserException, JSQLParserException, JSQLParserException {
String statement = "CREATE TABLE a AS SELECT col1, col2 FROM b";
assertSqlCanBeParsedAndDeparsed(statement);
}

@Test
public void testCreateTableAsSelect2() throws JSQLParserException {
String statement = "CREATE TABLE newtable AS WITH a AS (SELECT col1, col3 FROM testtable) SELECT col1, col2, col3 FROM b INNER JOIN a ON b.col1 = a.col1";
assertSqlCanBeParsedAndDeparsed(statement);
}

@Test
public void testCreateTable() throws JSQLParserException {
String statement = "CREATE TABLE mytab (mycol a (10, 20) c nm g, mycol2 mypar1 mypar2 (23,323,3) asdf ('23','123') dasd, "
+ "PRIMARY KEY (mycol2, mycol)) type = myisam";
Expand All @@ -61,6 +63,7 @@ public void testCreateTable() throws JSQLParserException {
assertEquals(statement, "" + createTable);
}

@Test
public void testCreateTableUnlogged() throws JSQLParserException {
String statement = "CREATE UNLOGGED TABLE mytab (mycol a (10, 20) c nm g, mycol2 mypar1 mypar2 (23,323,3) asdf ('23','123') dasd, "
+ "PRIMARY KEY (mycol2, mycol)) type = myisam";
Expand All @@ -76,72 +79,88 @@ public void testCreateTableUnlogged() throws JSQLParserException {
assertEquals(statement, "" + createTable);
}

@Test
public void testCreateTableUnlogged2() throws JSQLParserException {
String statement = "CREATE UNLOGGED TABLE mytab (mycol a (10, 20) c nm g, mycol2 mypar1 mypar2 (23,323,3) asdf ('23','123') dasd, PRIMARY KEY (mycol2, mycol))";
assertSqlCanBeParsedAndDeparsed(statement);
}

@Test
public void testCreateTableForeignKey() throws JSQLParserException {
String statement = "CREATE TABLE test (id INT UNSIGNED NOT NULL AUTO_INCREMENT, string VARCHAR (20), user_id INT UNSIGNED, PRIMARY KEY (id), FOREIGN KEY (user_id) REFERENCES ra_user(id))";
assertSqlCanBeParsedAndDeparsed(statement);
}

@Test
public void testCreateTableForeignKey2() throws JSQLParserException {
String statement = "CREATE TABLE test (id INT UNSIGNED NOT NULL AUTO_INCREMENT, string VARCHAR (20), user_id INT UNSIGNED, PRIMARY KEY (id), CONSTRAINT fkIdx FOREIGN KEY (user_id) REFERENCES ra_user(id))";
assertSqlCanBeParsedAndDeparsed(statement);
}

@Test
public void testCreateTableForeignKey3() throws JSQLParserException {
String statement = "CREATE TABLE test (id INT UNSIGNED NOT NULL AUTO_INCREMENT, string VARCHAR (20), user_id INT UNSIGNED REFERENCES ra_user(id), PRIMARY KEY (id))";
assertSqlCanBeParsedAndDeparsed(statement, true);
}

@Test
public void testCreateTableForeignKey4() throws JSQLParserException {
String statement = "CREATE TABLE test (id INT UNSIGNED NOT NULL AUTO_INCREMENT, string VARCHAR (20), user_id INT UNSIGNED FOREIGN KEY REFERENCES ra_user(id), PRIMARY KEY (id))";
assertSqlCanBeParsedAndDeparsed(statement, true);
}

@Test
public void testCreateTablePrimaryKey() throws JSQLParserException {
String statement = "CREATE TABLE test (id INT UNSIGNED NOT NULL AUTO_INCREMENT, string VARCHAR (20), user_id INT UNSIGNED, CONSTRAINT pk_name PRIMARY KEY (id))";
assertSqlCanBeParsedAndDeparsed(statement);
}

@Test
public void testCreateTableParams() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TEMPORARY TABLE T1 (PROCESSID VARCHAR (32)) ON COMMIT PRESERVE ROWS");
}

@Test
public void testCreateTableUniqueConstraint() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE Activities (_id INTEGER PRIMARY KEY AUTOINCREMENT,uuid VARCHAR(255),user_id INTEGER,sound_id INTEGER,sound_type INTEGER,comment_id INTEGER,type String,tags VARCHAR(255),created_at INTEGER,content_id INTEGER,sharing_note_text VARCHAR(255),sharing_note_created_at INTEGER,UNIQUE (created_at, type, content_id, sound_id, user_id))", true);
}

@Test
public void testCreateTableDefault() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE T1 (id integer default -1)");
}

@Test
public void testCreateTableDefault2() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE T1 (id integer default 1)");
}

@Test
public void testCreateTableIfNotExists() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE IF NOT EXISTS animals (id INT NOT NULL)");
}

@Test
public void testCreateTableInlinePrimaryKey() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE animals (id INT PRIMARY KEY NOT NULL)");
}

@Test
public void testCreateTableWithRange() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE foo (name character varying (255), range character varying (255), start_range integer, end_range integer)");
}

@Test
public void testCreateTableWithKey() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE bar (key character varying (255) NOT NULL)");
}

@Test
public void testCreateTableWithUniqueKey() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE animals (id INT NOT NULL, name VARCHAR (100) UNIQUE KEY (id))");
}

@Test
public void testCreateTableVeryComplex() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE `wp_commentmeta` ( `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `comment_id` bigint(20) unsigned NOT NULL DEFAULT '0', `meta_key` varchar(255) DEFAULT NULL, `meta_value` longtext, PRIMARY KEY (`meta_id`), KEY `comment_id` (`comment_id`), KEY `meta_key` (`meta_key`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8", true);
assertSqlCanBeParsedAndDeparsed("CREATE TABLE `wp_comments` ( `comment_ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `comment_post_ID` bigint(20) unsigned NOT NULL DEFAULT '0', `comment_author` tinytext NOT NULL, `comment_author_email` varchar(100) NOT NULL DEFAULT '', `comment_author_url` varchar(200) NOT NULL DEFAULT '', `comment_author_IP` varchar(100) NOT NULL DEFAULT '', `comment_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `comment_date_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `comment_content` text NOT NULL, `comment_karma` int(11) NOT NULL DEFAULT '0', `comment_approved` varchar(20) NOT NULL DEFAULT '1', `comment_agent` varchar(255) NOT NULL DEFAULT '', `comment_type` varchar(20) NOT NULL DEFAULT '', `comment_parent` bigint(20) unsigned NOT NULL DEFAULT '0', `user_id` bigint(20) unsigned NOT NULL DEFAULT '0', PRIMARY KEY (`comment_ID`), KEY `comment_post_ID` (`comment_post_ID`), KEY `comment_approved_date_gmt` (`comment_approved`,`comment_date_gmt`), KEY `comment_date_gmt` (`comment_date_gmt`), KEY `comment_parent` (`comment_parent`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8", true);
Expand All @@ -156,106 +175,132 @@ public void testCreateTableVeryComplex() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE `wp_users` ( `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `user_login` varchar(60) NOT NULL DEFAULT '', `user_pass` varchar(64) NOT NULL DEFAULT '', `user_nicename` varchar(50) NOT NULL DEFAULT '', `user_email` varchar(100) NOT NULL DEFAULT '', `user_url` varchar(100) NOT NULL DEFAULT '', `user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `user_activation_key` varchar(60) NOT NULL DEFAULT '', `user_status` int(11) NOT NULL DEFAULT '0', `display_name` varchar(250) NOT NULL DEFAULT '', PRIMARY KEY (`ID`), KEY `user_login_key` (`user_login`), KEY `user_nicename` (`user_nicename`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8", true);
}

@Test
public void testCreateTableArrays() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE sal_emp (name text, pay_by_quarter integer[], schedule text[][])");
}

@Test
public void testCreateTableArrays2() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE sal_emp (name text, pay_by_quarter integer[5], schedule text[3][2])");
}

@Test
public void testCreateTableColumnValues() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE mytable1 (values INTEGER)");
}

@Test
public void testCreateTableColumnValue() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE mytable1 (value INTEGER)");
}

@Test
public void testCreateTableForeignKey5() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE IF NOT EXISTS table1 (id INTEGER PRIMARY KEY AUTO_INCREMENT, aid INTEGER REFERENCES accounts ON aid ON DELETE CASCADE, name STRING, lastname STRING)");
}

@Test
public void testCreateTableForeignKey6() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE test (id long, fkey long references another_table (id))");
}

@Test
public void testMySqlCreateTableOnUpdateCurrentTimestamp() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE test (applied timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)");
}

@Test
public void testMySqlCreateTableWithConstraintWithCascade() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE table1 (id INT (10) UNSIGNED NOT NULL AUTO_INCREMENT, t2_id INT (10) UNSIGNED DEFAULT NULL, t3_id INT (10) UNSIGNED DEFAULT NULL, t4_id INT (10) UNSIGNED NOT NULL, PRIMARY KEY (id), KEY fkc_table1_t4 (t4_id), KEY fkc_table1_t2 (t2_id), KEY fkc_table1_t3 (t3_id), CONSTRAINT fkc_table1_t2 FOREIGN KEY (t2_id) REFERENCES table_two(t2o_id) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT fkc_table1_t3 FOREIGN KEY (t3_id) REFERENCES table_three(t3o_id) ON UPDATE CASCADE, CONSTRAINT fkc_table1_t4 FOREIGN KEY (t4_id) REFERENCES table_four(t4o_id) ON DELETE CASCADE) ENGINE = InnoDB AUTO_INCREMENT = 8761 DEFAULT CHARSET = utf8");
}

@Test
public void testMySqlCreateTableWithConstraintWithNoAction() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE table1 (id INT (10) UNSIGNED NOT NULL AUTO_INCREMENT, t2_id INT (10) UNSIGNED DEFAULT NULL, t3_id INT (10) UNSIGNED DEFAULT NULL, t4_id INT (10) UNSIGNED NOT NULL, PRIMARY KEY (id), KEY fkc_table1_t4 (t4_id), KEY fkc_table1_t2 (t2_id), KEY fkc_table1_t3 (t3_id), CONSTRAINT fkc_table1_t2 FOREIGN KEY (t2_id) REFERENCES table_two(t2o_id) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fkc_table1_t3 FOREIGN KEY (t3_id) REFERENCES table_three(t3o_id) ON UPDATE NO ACTION, CONSTRAINT fkc_table1_t4 FOREIGN KEY (t4_id) REFERENCES table_four(t4o_id) ON DELETE NO ACTION) ENGINE = InnoDB AUTO_INCREMENT = 8761 DEFAULT CHARSET = utf8");
}

@Test
public void testMySqlCreateTableWithTextIndexes() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE table2 (id INT (10) UNSIGNED NOT NULL AUTO_INCREMENT, name TEXT, url TEXT, created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (id), FULLTEXT KEY idx_table2_name (name)) ENGINE = InnoDB AUTO_INCREMENT = 7334 DEFAULT CHARSET = utf8");
}

@Test
public void testCreateTableWithCheck() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE table2 (id INT (10) NOT NULL, name TEXT, url TEXT, CONSTRAINT name_not_empty CHECK (name <> ''))");
}

@Test
public void testCreateTableIssue270() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE item (i_item_sk integer NOT NULL, i_item_id character (16) NOT NULL, i_rec_start_date date, i_rec_end_date date, i_item_desc character varying(200), i_current_price numeric(7,2), i_wholesale_cost numeric(7,2), i_brand_id integer, i_brand character(50), i_class_id integer, i_class character(50), i_category_id integer, i_category character(50), i_manufact_id integer, i_manufact character(50), i_size character(20), i_formulation character(20), i_color character(20), i_units character(10), i_container character(10), i_manager_id integer, i_product_name character(50) )", true);
}

@Test
public void testCreateTableIssue270_1() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE item (i_item_sk integer NOT NULL, i_item_id character (16))");
}

@Test
public void testCreateTempTableIssue293() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE GLOBAL TEMPORARY TABLE T1 (PROCESSID VARCHAR (32))");
}

@Test
public void testCreateTableWithTablespaceIssue247() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE TABLE1 (COLUMN1 VARCHAR2 (15), COLUMN2 VARCHAR2 (15), CONSTRAINT P_PK PRIMARY KEY (COLUMN1) USING INDEX TABLESPACE \"T_INDEX\") TABLESPACE \"T_SPACE\"");
}

@Test
public void testCreateTableWithTablespaceIssue247_1() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE TABLE1 (COLUMN1 VARCHAR2 (15), COLUMN2 VARCHAR2 (15), CONSTRAINT P_PK PRIMARY KEY (COLUMN1) USING INDEX TABLESPACE \"T_INDEX\")");
}

@Test
public void testOnDeleteSetNull() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE inventory (inventory_id INT PRIMARY KEY, product_id INT, CONSTRAINT fk_inv_product_id FOREIGN KEY (product_id) REFERENCES products(product_id) ON DELETE SET NULL)");
}

@Test
public void testColumnCheck() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE table1 (col1 INTEGER CHECK (col1 > 100))");
}

@Test
public void testTableReferenceWithSchema() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE table1 (col1 INTEGER REFERENCES schema1.table1)");
}

@Test
public void testNamedColumnConstraint() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE foo (col1 integer CONSTRAINT no_null NOT NULL)");
}

@Test
public void testColumnConstraintWith() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE foo (col1 integer) WITH (fillfactor=70)");
}

@Test
public void testExcludeWhereConstraint() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE foo (col1 integer, EXCLUDE WHERE (col1 > 100))");
}

@Test
public void testTimestampWithoutTimezone() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE abc.tabc (transaction_date TIMESTAMP WITHOUT TIME ZONE)");
}

@Test
public void testCreateUnitonIssue402() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE temp.abc AS SELECT sku FROM temp.a UNION SELECT sku FROM temp.b");
}

@Test
public void testCreateUnitonIssue402_2() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE temp.abc AS (SELECT sku FROM temp.a UNION SELECT sku FROM temp.b)");
}

@Test
public void testTimestampWithTimezone() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE country_region ("
+ "regionid BIGINT NOT NULL CONSTRAINT pk_auth_region PRIMARY KEY, "
Expand All @@ -265,34 +310,45 @@ public void testTimestampWithTimezone() throws JSQLParserException {
+ "CONSTRAINT region_name_unique UNIQUE (region_name))");
}

@Test
public void testCreateTableAsSelect3() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE public.sales1 AS (SELECT * FROM public.sales)");
}

@Test
public void testQuotedPKColumnsIssue491() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE `FOO` (`ID` INT64, `NAME` STRING (100)) PRIMARY KEY (`ID`)");
}

@Test
public void testQuotedPKColumnsIssue491_2() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE `FOO` (`ID` INT64, `NAME` STRING (100), PRIMARY KEY (`ID`))");
}

@Test
public void testKeySyntaxWithLengthColumnParameter() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE basic (BASIC_TITLE varchar (255) NOT NULL, KEY BASIC_TITLE (BASIC_TITLE(255)))");
}

@Test
public void testIssue273Varchar2Byte() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE IF NOT EXISTS \"TABLE_OK\" (\"SOME_FIELD\" VARCHAR2 (256 BYTE))");
}


@Test
public void testIssue273Varchar2Char() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE IF NOT EXISTS \"TABLE_OK\" (\"SOME_FIELD\" VARCHAR2 (256 CHAR))");
}

@Test
public void testIssue661Partition() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE T_TEST_PARTITION (PART_COLUMN VARCHAR2 (32) NOT NULL, OTHER_COLS VARCHAR2 (10) NOT NULL) TABLESPACE TBS_DATA_01 PARTITION BY HASH (PART_COLUMN) PARTITIONS 4 STORE IN (TBS_DATA_01) COMPRESS");
}

@Test
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;
Expand Down

0 comments on commit 44ea8bd

Please sign in to comment.