Apply code review remarks

Adjust style and remove the `SQLServerSample` as it seems redundant with the
unit test present.
上级 65f4fae6
......@@ -1861,27 +1861,23 @@ public class Parser {
}
private void discardWithTableHints() {
if (readIf("WITH")) {
if (readIf(WITH)) {
read(OPEN_PAREN);
if (!readIf(CLOSE_PAREN)) {
do {
discardTableHint();
} while (readIfMore(true));
}
}
}
private void discardTableHint() {
if (readIf("INDEX")) {
if (readIf(OPEN_PAREN)) {
do {
readExpression();
} while (readIf(COMMA));
read(CLOSE_PAREN);
} else if (readIf(EQUAL)){
readExpression();
} while (readIfMore(true));
} else {
throw getSyntaxError();
read(EQUAL);
readExpression();
}
} else {
readExpression();
......@@ -7492,8 +7488,9 @@ public class Parser {
parseAutoIncrement(column);
}
if (database.getMode().useIdentityAsAutoIncrement) {
if (readIf("NOT")) {
read("NULL");
if (readIf(NOT)) {
read(NULL);
column.setNullable(false);
}
if (readIf("IDENTITY")) {
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;
import org.h2.test.TestBase;
......@@ -25,35 +30,45 @@ public class TestCompatibilitySQLServer extends TestDb {
@Override
public void test() throws Exception {
testDiscardTableHints();
testUseIdentityAsAutoIncrementAlias();
}
deleteDb("sqlserver");
private void testDiscardTableHints() throws SQLException {
final Connection conn = getConnection("sqlserver;MODE=MSSQLServer");
try {
testDiscardTableHints(conn);
testUseIdentityAsAutoIncrementAlias(conn);
} finally {
conn.close();
deleteDb("sqlserver");
}
}
Connection conn = getConnection("sqlserver;MODE=MSSQLServer");
Statement stat = conn.createStatement();
private void testDiscardTableHints(Connection conn) throws SQLException {
final Statement stat = conn.createStatement();
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))");
assertSupportedSyntax(stat, "select * from parent");
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, name))");
assertSupportedSyntax(stat, "select * from parent p join child ch on ch.parent_id = p.id");
assertSupportedSyntax(stat, "select * from parent p with(nolock) join child ch with(nolock) on ch.parent_id = p.id");
assertSupportedSyntax(stat, "select * from parent p with(nolock) join child ch with(nolock, index = id) 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");
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("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 {
deleteDb("sqlserver");
Connection conn = getConnection("sqlserver;MODE=MSSQLServer");
Statement stat = conn.createStatement();
private void testUseIdentityAsAutoIncrementAlias(Connection conn) throws SQLException {
final 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)");
......@@ -63,14 +78,7 @@ public class TestCompatibilitySQLServer extends TestDb {
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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论