提交 6395ede0 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Parse DUAL, SYS.DUAL, SYSDUMMY1, and SYSIBM.SYSDUMMY1 in better way

上级 ffd978cb
...@@ -684,10 +684,6 @@ public class Parser { ...@@ -684,10 +684,6 @@ public class Parser {
if (equalsToken("SESSION", schemaName)) { if (equalsToken("SESSION", schemaName)) {
// for local temporary tables // for local temporary tables
schema = database.getSchema(session.getCurrentSchemaName()); schema = database.getSchema(session.getCurrentSchemaName());
} else if (database.getMode().sysDummy1 &&
"SYSIBM".equals(schemaName)) {
// IBM DB2 and Apache Derby compatibility: SYSIBM.SYSDUMMY1
schema = database.getSchema(session.getCurrentSchemaName());
} }
} }
return schema; return schema;
...@@ -1372,7 +1368,7 @@ public class Parser { ...@@ -1372,7 +1368,7 @@ public class Parser {
private TableFilter readTableFilter() { private TableFilter readTableFilter() {
Table table; Table table;
String alias = null; String alias = null;
if (readIf("(")) { label: if (readIf("(")) {
if (isSelect()) { if (isSelect()) {
Query query = parseSelectUnion(); Query query = parseSelectUnion();
read(")"); read(")");
...@@ -1406,7 +1402,19 @@ public class Parser { ...@@ -1406,7 +1402,19 @@ public class Parser {
table = parseValuesTable(0).getTable(); table = parseValuesTable(0).getTable();
} else { } else {
String tableName = readIdentifierWithSchema(null); String tableName = readIdentifierWithSchema(null);
Schema schema = getSchema(); Schema schema;
if (schemaName == null) {
schema = null;
} else {
schema = findSchema(schemaName);
if (schema == null) {
if (isDualTable(tableName)) {
table = getDualTable(false);
break label;
}
throw DbException.get(ErrorCode.SCHEMA_NOT_FOUND_1, schemaName);
}
}
boolean foundLeftBracket = readIf("("); boolean foundLeftBracket = readIf("(");
if (foundLeftBracket && readIf("INDEX")) { if (foundLeftBracket && readIf("INDEX")) {
// Sybase compatibility with // Sybase compatibility with
...@@ -1442,13 +1450,8 @@ public class Parser { ...@@ -1442,13 +1450,8 @@ public class Parser {
} }
table = new FunctionTable(mainSchema, session, expr, call); table = new FunctionTable(mainSchema, session, expr, call);
} }
} else if (equalsToken("DUAL", tableName)) {
table = getDualTable(false);
} else if (database.getMode().sysDummy1 &&
equalsToken("SYSDUMMY1", tableName)) {
table = getDualTable(false);
} else { } else {
table = readTableOrView(tableName); table = readTableOrView(tableName, true);
} }
} }
ArrayList<String> derivedColumnNames = null; ArrayList<String> derivedColumnNames = null;
...@@ -5933,30 +5936,42 @@ public class Parser { ...@@ -5933,30 +5936,42 @@ public class Parser {
return command; return command;
} }
boolean isDualTable(String tableName) {
return ((schemaName == null || equalsToken(schemaName, "SYS")) && equalsToken("DUAL", tableName))
|| (database.getMode().sysDummy1 && (schemaName == null || equalsToken(schemaName, "SYSIBM")))
&& equalsToken("SYSDUMMY1", tableName);
}
private Table readTableOrView() { private Table readTableOrView() {
return readTableOrView(readIdentifierWithSchema(null)); return readTableOrView(readIdentifierWithSchema(null), false);
} }
private Table readTableOrView(String tableName) { private Table readTableOrView(String tableName, boolean allowDual) {
// same algorithm than readSequence
if (schemaName != null) { if (schemaName != null) {
return getSchema().getTableOrView(session, tableName); Table table = getSchema().resolveTableOrView(session, tableName);
} if (table != null) {
Table table = database.getSchema(session.getCurrentSchemaName()) return table;
.resolveTableOrView(session, tableName); }
if (table != null) { } else {
return table; Table table = database.getSchema(session.getCurrentSchemaName())
} .resolveTableOrView(session, tableName);
String[] schemaNames = session.getSchemaSearchPath(); if (table != null) {
if (schemaNames != null) { return table;
for (String name : schemaNames) { }
Schema s = database.getSchema(name); String[] schemaNames = session.getSchemaSearchPath();
table = s.resolveTableOrView(session, tableName); if (schemaNames != null) {
if (table != null) { for (String name : schemaNames) {
return table; Schema s = database.getSchema(name);
table = s.resolveTableOrView(session, tableName);
if (table != null) {
return table;
}
} }
} }
} }
if (allowDual && isDualTable(tableName)) {
return getDualTable(false);
}
throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName); throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName);
} }
......
...@@ -87,6 +87,7 @@ public class TestScript extends TestBase { ...@@ -87,6 +87,7 @@ public class TestScript extends TestBase {
testScript("testScript.sql"); testScript("testScript.sql");
testScript("derived-column-names.sql"); testScript("derived-column-names.sql");
testScript("dual.sql");
testScript("indexes.sql"); testScript("indexes.sql");
testScript("information_schema.sql"); testScript("information_schema.sql");
testScript("joins.sql"); testScript("joins.sql");
......
-- 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
--
SELECT * FROM DUAL;
>> 1
CREATE TABLE DUAL(A INT);
> ok
INSERT INTO DUAL VALUES (2);
> update count: 1
SELECT A FROM DUAL;
>> 2
SELECT * FROM SYS.DUAL;
>> 1
DROP TABLE DUAL;
> ok
SET MODE DB2;
> ok
SELECT * FROM SYSDUMMY1;
>> 1
CREATE TABLE SYSDUMMY1(A INT);
> ok
INSERT INTO SYSDUMMY1 VALUES (2);
> update count: 1
SELECT A FROM SYSDUMMY1;
>> 2
SELECT * FROM SYSIBM.SYSDUMMY1;
>> 1
DROP TABLE SYSDUMMY1;
> ok
SET MODE Regular;
> ok
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论