Apply code review remarks

Adjust style and remove the `SQLServerSample` as it seems redundant with the
unit test present.
上级 65f4fae6
...@@ -1861,13 +1861,11 @@ public class Parser { ...@@ -1861,13 +1861,11 @@ public class Parser {
} }
private void discardWithTableHints() { private void discardWithTableHints() {
if (readIf("WITH")) { if (readIf(WITH)) {
read(OPEN_PAREN); read(OPEN_PAREN);
if (!readIf(CLOSE_PAREN)) { do {
do { discardTableHint();
discardTableHint(); } while (readIfMore(true));
} while (readIfMore(true));
}
} }
} }
...@@ -1876,12 +1874,10 @@ public class Parser { ...@@ -1876,12 +1874,10 @@ public class Parser {
if (readIf(OPEN_PAREN)) { if (readIf(OPEN_PAREN)) {
do { do {
readExpression(); readExpression();
} while (readIf(COMMA)); } while (readIfMore(true));
read(CLOSE_PAREN);
} else if (readIf(EQUAL)){
readExpression();
} else { } else {
throw getSyntaxError(); read(EQUAL);
readExpression();
} }
} else { } else {
readExpression(); readExpression();
...@@ -7492,8 +7488,9 @@ public class Parser { ...@@ -7492,8 +7488,9 @@ public class Parser {
parseAutoIncrement(column); parseAutoIncrement(column);
} }
if (database.getMode().useIdentityAsAutoIncrement) { if (database.getMode().useIdentityAsAutoIncrement) {
if (readIf("NOT")) { if (readIf(NOT)) {
read("NULL"); read(NULL);
column.setNullable(false);
} }
if (readIf("IDENTITY")) { if (readIf("IDENTITY")) {
parseAutoIncrement(column); parseAutoIncrement(column);
......
package org.h2.samples;
import org.h2.tools.DeleteDbFiles;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class SQLServerSample {
/**
* Called when ran from the command line.
*
* @param args ignored
*/
public static void main(String... args) throws Exception {
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:mem:;MODE=MSSQLServer");
Statement stat = conn.createStatement();
stat.execute("create table parent(id int primary key identity, name varchar(255))");
stat.execute("create table child(id int primary key identity, name varchar(255), parent_id int, foreign key (parent_id) references public.parent(id) )");
stat.execute("insert into parent values(1, 'Thomas')");
stat.execute("insert into child values(1, 'John', 1)");
ResultSet rs;
rs = stat.executeQuery("select * from parent with(nolock)");
while (rs.next()) {
System.out.println(rs.getString("name"));
}
rs = stat.executeQuery("select * from parent with(nolock, index = id)");
while (rs.next()) {
System.out.println(rs.getString("name"));
}
rs = stat.executeQuery("select * from parent with(nolock, index(id, name))");
while (rs.next()) {
System.out.println(rs.getString("name"));
}
rs = stat.executeQuery("select * from parent p with(nolock) join child ch with(nolock) on ch.parent_id = p.id");
while (rs.next()) {
System.out.println(rs.getString("parent.name") + " -> " + rs.getString("child.name"));
}
stat.close();
conn.close();
}
}
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db; package org.h2.test.db;
import org.h2.test.TestBase; import org.h2.test.TestBase;
...@@ -25,35 +30,45 @@ public class TestCompatibilitySQLServer extends TestDb { ...@@ -25,35 +30,45 @@ public class TestCompatibilitySQLServer extends TestDb {
@Override @Override
public void test() throws Exception { public void test() throws Exception {
testDiscardTableHints();
testUseIdentityAsAutoIncrementAlias();
}
private void testDiscardTableHints() throws SQLException {
deleteDb("sqlserver"); deleteDb("sqlserver");
Connection conn = getConnection("sqlserver;MODE=MSSQLServer"); final Connection conn = getConnection("sqlserver;MODE=MSSQLServer");
Statement stat = conn.createStatement(); try {
testDiscardTableHints(conn);
stat.execute("create table parent(id int primary key, name varchar(255))"); testUseIdentityAsAutoIncrementAlias(conn);
stat.execute("create table child(id int primary key, parent_id int, name varchar(255), foreign key (parent_id) references public.parent(id))"); } finally {
conn.close();
deleteDb("sqlserver");
}
}
assertSupportedSyntax(stat, "select * from parent"); private void testDiscardTableHints(Connection conn) throws SQLException {
assertSupportedSyntax(stat, "select * from parent with(nolock)"); final Statement stat = conn.createStatement();
assertSupportedSyntax(stat, "select * from parent with(nolock, index = id)");
assertSupportedSyntax(stat, "select * from parent with(nolock, index(id, name))");
assertSupportedSyntax(stat, "select * from parent p join child ch on ch.parent_id = p.id"); stat.execute("create table parent(id int primary key, name varchar(255))");
assertSupportedSyntax(stat, "select * from parent p with(nolock) join child ch with(nolock) on ch.parent_id = p.id"); stat.execute("create table child(" +
assertSupportedSyntax(stat, "select * from parent p with(nolock) join child ch with(nolock, index = id) on ch.parent_id = p.id"); "id int primary key, " +
assertSupportedSyntax(stat, "select * from parent p with(nolock) join child ch with(nolock, index(id, name)) on ch.parent_id = p.id"); "parent_id int, " +
"name varchar(255), " +
"foreign key (parent_id) references public.parent(id))");
stat.execute("select * from parent");
stat.execute("select * from parent with(nolock)");
stat.execute("select * from parent with(nolock, index = id)");
stat.execute("select * from parent with(nolock, index(id, name))");
stat.execute("select * from parent p " +
"join child ch on ch.parent_id = p.id");
stat.execute("select * from parent p with(nolock) " +
"join child ch with(nolock) on ch.parent_id = p.id");
stat.execute("select * from parent p with(nolock) " +
"join child ch with(nolock, index = id) on ch.parent_id = p.id");
stat.execute("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 { private void testUseIdentityAsAutoIncrementAlias(Connection conn) throws SQLException {
deleteDb("sqlserver"); final Statement stat = conn.createStatement();
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("create table test(id int primary key identity, expected_id int)");
stat.execute("insert into test (expected_id) VALUES (1), (2), (3)"); stat.execute("insert into test (expected_id) VALUES (1), (2), (3)");
...@@ -63,14 +78,7 @@ public class TestCompatibilitySQLServer extends TestDb { ...@@ -63,14 +78,7 @@ public class TestCompatibilitySQLServer extends TestDb {
assertEquals(results.getInt("expected_id"), results.getInt("id")); assertEquals(results.getInt("expected_id"), results.getInt("id"));
} }
assertSupportedSyntax(stat, "create table test2 (id int primary key not null identity)"); stat.execute("create table test2 (id int primary key not null identity)");
} }
private void assertSupportedSyntax(Statement stat, String sql) {
try {
stat.execute(sql);
} catch (SQLException ex) {
fail("Failed to execute SQL statement: " + sql);
}
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论