Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
db1612f4
提交
db1612f4
authored
2月 26, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement derived column list syntax on table alias
上级
d4891863
隐藏空白字符变更
内嵌
并排
正在显示
10 个修改的文件
包含
127 行增加
和
4 行删除
+127
-4
help.csv
h2/src/docsrc/help/help.csv
+2
-1
Parser.java
h2/src/main/org/h2/command/Parser.java
+8
-0
Select.java
h2/src/main/org/h2/command/dml/Select.java
+2
-1
SelectListColumnResolver.java
h2/src/main/org/h2/command/dml/SelectListColumnResolver.java
+5
-0
ExpressionColumn.java
h2/src/main/org/h2/expression/ExpressionColumn.java
+11
-2
ColumnResolver.java
h2/src/main/org/h2/table/ColumnResolver.java
+8
-0
SingleColumnResolver.java
h2/src/main/org/h2/table/SingleColumnResolver.java
+5
-0
TableFilter.java
h2/src/main/org/h2/table/TableFilter.java
+33
-0
TestScript.java
h2/src/test/org/h2/test/scripts/TestScript.java
+1
-0
aliases.sql
h2/src/test/org/h2/test/scripts/aliases.sql
+52
-0
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
db1612f4
...
...
@@ -2256,7 +2256,8 @@ ID + 20
"
"Other Grammar","Table Expression","
{ [ schemaName. ] tableName | ( select ) | valuesExpression } [ [ AS ] newTableAlias ]
{ [ schemaName. ] tableName | ( select ) | valuesExpression }
[ [ AS ] newTableAlias [ ( columnName [,...] ) ] ]
[ USE INDEX ([ indexName [,...] ]) ]
[ { { LEFT | RIGHT } [ OUTER ] | [ INNER ] | CROSS | NATURAL }
JOIN tableExpression [ ON expression ] ]
...
...
h2/src/main/org/h2/command/Parser.java
浏览文件 @
db1612f4
...
...
@@ -1374,6 +1374,14 @@ public class Parser {
alias
=
readFromAlias
(
null
);
if
(
alias
!=
null
)
{
top
.
setAlias
(
alias
);
if
(
readIf
(
"("
))
{
ArrayList
<
String
>
derivedColumnNames
=
New
.
arrayList
();
do
{
derivedColumnNames
.
add
(
readAliasIdentifier
());
}
while
(
readIf
(
","
));
read
(
")"
);
top
.
setDerivedColumns
(
derivedColumnNames
);
}
}
return
top
;
}
...
...
h2/src/main/org/h2/command/dml/Select.java
浏览文件 @
db1612f4
...
...
@@ -737,8 +737,9 @@ public class Select extends Query {
if
(
filter
.
isNaturalJoinColumn
(
c
))
{
continue
;
}
String
name
=
filter
.
getDerivedColumnName
(
c
);
ExpressionColumn
ec
=
new
ExpressionColumn
(
session
.
getDatabase
(),
null
,
alias
,
c
.
getName
());
session
.
getDatabase
(),
null
,
alias
,
name
!=
null
?
name
:
c
.
getName
());
expressions
.
add
(
index
++,
ec
);
}
return
index
;
...
...
h2/src/main/org/h2/command/dml/SelectListColumnResolver.java
浏览文件 @
db1612f4
...
...
@@ -52,6 +52,11 @@ public class SelectListColumnResolver implements ColumnResolver {
return
columns
;
}
@Override
public
String
getDerivedColumnName
(
Column
column
)
{
return
null
;
}
@Override
public
String
getSchemaName
()
{
return
null
;
...
...
h2/src/main/org/h2/expression/ExpressionColumn.java
浏览文件 @
db1612f4
...
...
@@ -33,7 +33,7 @@ public class ExpressionColumn extends Expression {
private
final
Database
database
;
private
final
String
schemaName
;
private
final
String
tableAlias
;
private
final
String
columnName
;
private
String
columnName
;
private
ColumnResolver
columnResolver
;
private
int
queryLevel
;
private
Column
column
;
...
...
@@ -89,7 +89,10 @@ public class ExpressionColumn extends Expression {
return
;
}
for
(
Column
col
:
resolver
.
getColumns
())
{
String
n
=
col
.
getName
();
String
n
=
resolver
.
getDerivedColumnName
(
col
);
if
(
n
==
null
)
{
n
=
col
.
getName
();
}
if
(
database
.
equalsIdentifiers
(
columnName
,
n
))
{
mapColumn
(
resolver
,
col
,
level
);
return
;
...
...
@@ -253,6 +256,12 @@ public class ExpressionColumn extends Expression {
@Override
public
String
getAlias
()
{
if
(
column
!=
null
)
{
if
(
columnResolver
!=
null
)
{
String
name
=
columnResolver
.
getDerivedColumnName
(
column
);
if
(
name
!=
null
)
{
return
name
;
}
}
return
column
.
getName
();
}
if
(
tableAlias
!=
null
)
{
...
...
h2/src/main/org/h2/table/ColumnResolver.java
浏览文件 @
db1612f4
...
...
@@ -30,6 +30,14 @@ public interface ColumnResolver {
*/
Column
[]
getColumns
();
/**
* Get derived column name, or {@code null}.
*
* @param column column
* @return derived column name, or {@code null}
*/
String
getDerivedColumnName
(
Column
column
);
/**
* Get the list of system columns, if any.
*
...
...
h2/src/main/org/h2/table/SingleColumnResolver.java
浏览文件 @
db1612f4
...
...
@@ -42,6 +42,11 @@ public class SingleColumnResolver implements ColumnResolver {
return
new
Column
[]
{
column
};
}
@Override
public
String
getDerivedColumnName
(
Column
column
)
{
return
null
;
}
@Override
public
String
getSchemaName
()
{
return
null
;
...
...
h2/src/main/org/h2/table/TableFilter.java
浏览文件 @
db1612f4
...
...
@@ -6,6 +6,7 @@
package
org
.
h2
.
table
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
org.h2.api.ErrorCode
;
import
org.h2.command.Parser
;
...
...
@@ -117,6 +118,8 @@ public class TableFilter implements ColumnResolver {
private
final
int
hashCode
;
private
final
int
orderInFrom
;
private
HashMap
<
Column
,
String
>
derivedColumnMap
;
/**
* Create a new table filter object.
*
...
...
@@ -992,6 +995,12 @@ public class TableFilter implements ColumnResolver {
return
table
.
getColumns
();
}
@Override
public
String
getDerivedColumnName
(
Column
column
)
{
HashMap
<
Column
,
String
>
map
=
derivedColumnMap
;
return
map
!=
null
?
map
.
get
(
column
)
:
null
;
}
/**
* Get the system columns that this table understands. This is used for
* compatibility with other databases. The columns are only returned if the
...
...
@@ -1056,6 +1065,30 @@ public class TableFilter implements ColumnResolver {
this
.
alias
=
alias
;
}
/**
* Set derived column list.
*
* @param derivedColumnNames names of derived columns
*/
public
void
setDerivedColumns
(
ArrayList
<
String
>
derivedColumnNames
)
{
Column
[]
columns
=
getColumns
();
int
count
=
columns
.
length
;
if
(
count
!=
derivedColumnNames
.
size
())
{
throw
DbException
.
get
(
ErrorCode
.
COLUMN_COUNT_DOES_NOT_MATCH
);
}
HashMap
<
Column
,
String
>
map
=
new
HashMap
<>(
count
);
for
(
int
i
=
0
;
i
<
count
;
i
++)
{
String
alias
=
derivedColumnNames
.
get
(
i
);
for
(
int
j
=
0
;
j
<
i
;
j
++)
{
if
(
alias
.
equals
(
derivedColumnNames
.
get
(
j
)))
{
throw
DbException
.
get
(
ErrorCode
.
DUPLICATE_COLUMN_NAME_1
,
alias
);
}
}
map
.
put
(
columns
[
i
],
alias
);
}
this
.
derivedColumnMap
=
map
;
}
@Override
public
Expression
optimize
(
ExpressionColumn
expressionColumn
,
Column
column
)
{
return
expressionColumn
;
...
...
h2/src/test/org/h2/test/scripts/TestScript.java
浏览文件 @
db1612f4
...
...
@@ -82,6 +82,7 @@ public class TestScript extends TestBase {
reconnectOften
=
!
config
.
memory
&&
config
.
big
;
testScript
(
"testScript.sql"
);
testScript
(
"aliases.sql"
);
testScript
(
"joins.sql"
);
testScript
(
"altertable-index-reuse.sql"
);
testScript
(
"query-optimisations.sql"
);
...
...
h2/src/test/org/h2/test/scripts/aliases.sql
0 → 100644
浏览文件 @
db1612f4
-- 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
(
VALUES
(
1
,
2
));
>
C1
C2
>
-- --
>
1
2
>
rows
:
1
SELECT
*
FROM
(
VALUES
(
1
,
2
))
AS
T
;
>
C1
C2
>
-- --
>
1
2
>
rows
:
1
SELECT
*
FROM
(
VALUES
(
1
,
2
))
AS
T
(
A
,
B
);
>
A
B
>
-
-
>
1
2
>
rows
:
1
SELECT
A
AS
A1
,
B
AS
B1
FROM
(
VALUES
(
1
,
2
))
AS
T
(
A
,
B
);
>
A1
B1
>
-- --
>
1
2
>
rows
:
1
SELECT
A
AS
A1
,
B
AS
B1
FROM
(
VALUES
(
1
,
2
))
AS
T
(
A
,
B
)
WHERE
A
<>
B
;
>
A1
B1
>
-- --
>
1
2
>
rows
:
1
SELECT
A
AS
A1
,
B
AS
B1
FROM
(
VALUES
(
1
,
2
))
AS
T
(
A
,
B
)
WHERE
A1
<>
B1
;
>
exception
SELECT
*
FROM
(
VALUES
(
1
,
2
))
AS
T
(
A
);
>
exception
SELECT
*
FROM
(
VALUES
(
1
,
2
))
AS
T
(
A
,
a
);
>
exception
SELECT
*
FROM
(
VALUES
(
1
,
2
))
AS
T
(
A
,
B
,
C
);
>
exception
SELECT
V
AS
V1
,
A
AS
A1
,
B
AS
B1
FROM
(
VALUES
(
1
))
T1
(
V
)
INNER
JOIN
(
VALUES
(
1
,
2
))
T2
(
A
,
B
)
ON
V
=
A
;
>
V1
A1
B1
>
-- -- --
>
1
1
2
>
rows
:
1
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论