Treat identity as an auto_increment alias in MSSQLServer mode

Add support for `create table test(id int primary key identity)` statements,
which are common in the SQLServer world.
上级 d31f44c2
...@@ -7491,6 +7491,14 @@ public class Parser { ...@@ -7491,6 +7491,14 @@ public class Parser {
if (readIf("AUTO_INCREMENT")) { if (readIf("AUTO_INCREMENT")) {
parseAutoIncrement(column); parseAutoIncrement(column);
} }
if (database.getMode().useIdentityAsAutoIncrement) {
if (readIf("NOT")) {
read("NULL");
}
if (readIf("IDENTITY")) {
parseAutoIncrement(column);
}
}
if (affinity) { if (affinity) {
CreateIndex idx = createAffinityIndex(schema, tableName, cols); CreateIndex idx = createAffinityIndex(schema, tableName, cols);
command.addConstraintCommand(idx); command.addConstraintCommand(idx);
......
...@@ -192,6 +192,11 @@ public class Mode { ...@@ -192,6 +192,11 @@ public class Mode {
*/ */
public boolean discardWithTableHints; public boolean discardWithTableHints;
/**
* Use "IDENTITY" as an alias for "auto_increment" (SQLServer style)
*/
public boolean useIdentityAsAutoIncrement;
/** /**
* Convert (VAR)CHAR to VAR(BINARY) and vice versa with UTF-8 encoding instead of HEX. * Convert (VAR)CHAR to VAR(BINARY) and vice versa with UTF-8 encoding instead of HEX.
*/ */
...@@ -262,6 +267,7 @@ public class Mode { ...@@ -262,6 +267,7 @@ public class Mode {
mode.swapConvertFunctionParameters = true; mode.swapConvertFunctionParameters = true;
mode.supportPoundSymbolForColumnNames = true; mode.supportPoundSymbolForColumnNames = true;
mode.discardWithTableHints = true; mode.discardWithTableHints = true;
mode.useIdentityAsAutoIncrement = true;
// MS SQL Server does not support client info properties. See // MS SQL Server does not support client info properties. See
// https://msdn.microsoft.com/en-Us/library/dd571296%28v=sql.110%29.aspx // https://msdn.microsoft.com/en-Us/library/dd571296%28v=sql.110%29.aspx
mode.supportedClientInfoPropertiesRegEx = null; mode.supportedClientInfoPropertiesRegEx = null;
......
...@@ -4,6 +4,7 @@ import org.h2.test.TestBase; ...@@ -4,6 +4,7 @@ import org.h2.test.TestBase;
import org.h2.test.TestDb; import org.h2.test.TestDb;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
...@@ -25,6 +26,7 @@ public class TestCompatibilitySQLServer extends TestDb { ...@@ -25,6 +26,7 @@ public class TestCompatibilitySQLServer extends TestDb {
@Override @Override
public void test() throws Exception { public void test() throws Exception {
testDiscardTableHints(); testDiscardTableHints();
testUseIdentityAsAutoIncrementAlias();
} }
private void testDiscardTableHints() throws SQLException { private void testDiscardTableHints() throws SQLException {
...@@ -35,7 +37,7 @@ public class TestCompatibilitySQLServer extends TestDb { ...@@ -35,7 +37,7 @@ public class TestCompatibilitySQLServer extends TestDb {
stat.execute("create table parent(id int primary key, name varchar(255))"); stat.execute("create table parent(id int primary key, name varchar(255))");
stat.execute("create table child(id int primary key, parent_id int, name varchar(255), foreign key (parent_id) references public.parent(id))"); stat.execute("create table child(id int primary key, parent_id int, name varchar(255), foreign key (parent_id) references public.parent(id))");
assertSupportedSyntax(stat, "select * from parent"); assertSupportedSyntax(stat, "select * from parent");
assertSupportedSyntax(stat, "select * from parent with(nolock)"); assertSupportedSyntax(stat, "select * from parent with(nolock)");
assertSupportedSyntax(stat, "select * from parent with(nolock, index = id)"); assertSupportedSyntax(stat, "select * from parent with(nolock, index = id)");
...@@ -47,6 +49,23 @@ public class TestCompatibilitySQLServer extends TestDb { ...@@ -47,6 +49,23 @@ public class TestCompatibilitySQLServer extends TestDb {
assertSupportedSyntax(stat, "select * from parent p with(nolock) join child ch with(nolock, index(id, name)) on ch.parent_id = p.id"); assertSupportedSyntax(stat, "select * from parent p with(nolock) join child ch with(nolock, index(id, name)) on ch.parent_id = p.id");
} }
private void testUseIdentityAsAutoIncrementAlias() throws SQLException {
deleteDb("sqlserver");
Connection conn = getConnection("sqlserver;MODE=MSSQLServer");
Statement stat = conn.createStatement();
stat.execute("create table test(id int primary key identity, expected_id int)");
stat.execute("insert into test (expected_id) VALUES (1), (2), (3)");
final ResultSet results = stat.executeQuery("select * from test");
while (results.next()) {
assertEquals(results.getInt("expected_id"), results.getInt("id"));
}
assertSupportedSyntax(stat, "create table test2 (id int primary key not null identity)");
}
private void assertSupportedSyntax(Statement stat, String sql) { private void assertSupportedSyntax(Statement stat, String sql) {
try { try {
stat.execute(sql); stat.execute(sql);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论