Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
86462170
提交
86462170
authored
9月 18, 2015
作者:
Noel Grandin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add support for dropping multiple columns in ALTER TABLE DROP COLUMN...
上级
7014d46a
显示空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
85 行增加
和
46 行删除
+85
-46
help.csv
h2/src/docsrc/help/help.csv
+3
-2
changelog.html
h2/src/docsrc/html/changelog.html
+2
-0
Parser.java
h2/src/main/org/h2/command/Parser.java
+10
-5
AlterTableAlterColumn.java
h2/src/main/org/h2/command/ddl/AlterTableAlterColumn.java
+24
-7
Table.java
h2/src/main/org/h2/table/Table.java
+34
-31
TestAlter.java
h2/src/test/org/h2/test/db/TestAlter.java
+12
-1
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
86462170
...
...
@@ -289,12 +289,13 @@ ALTER TABLE TEST ALTER COLUMN NAME SET NULL;
"
"Commands (DDL)","ALTER TABLE DROP COLUMN","
ALTER TABLE tableName DROP COLUMN [ IF EXISTS ]
columnName
ALTER TABLE tableName DROP COLUMN [ IF EXISTS ]
( columnName [,...] )
","
Removes
a column
from a table.
Removes
column(s)
from a table.
This command commits an open transaction in this connection.
","
ALTER TABLE TEST DROP COLUMN NAME
ALTER TABLE TEST DROP COLUMN NAME1, NAME2
"
"Commands (DDL)","ALTER TABLE DROP CONSTRAINT","
...
...
h2/src/docsrc/html/changelog.html
浏览文件 @
86462170
...
...
@@ -27,6 +27,8 @@ Change Log
<h2>
Version 1.4.189 Beta (2015-09-13)
</h2>
<ul>
<li>
Add support for dropping multiple columns in ALTER TABLE DROP COLUMN...
</li>
<li>
Fix bug in XA management when doing rollback after prepare. Patch by Stephane Lacoin.
</li>
<li>
MVStore CLOB and BLOB: An exception with the message "Block not found" could be thrown
...
...
h2/src/main/org/h2/command/Parser.java
浏览文件 @
86462170
...
...
@@ -5415,12 +5415,17 @@ public class Parser {
AlterTableAlterColumn
command
=
new
AlterTableAlterColumn
(
session
,
table
.
getSchema
());
command
.
setType
(
CommandInterface
.
ALTER_TABLE_DROP_COLUMN
);
ArrayList
<
Column
>
columnsToRemove
=
New
.
arrayList
();
do
{
String
columnName
=
readColumnIdentifier
();
command
.
setTable
(
table
);
if
(
ifExists
&&
!
table
.
doesColumnExist
(
columnName
))
{
return
new
NoOperation
(
session
);
}
command
.
setOldColumn
(
table
.
getColumn
(
columnName
));
Column
column
=
table
.
getColumn
(
columnName
);
columnsToRemove
.
add
(
column
);
}
while
(
readIf
(
","
));
command
.
setTable
(
table
);
command
.
setColumnsToRemove
(
columnsToRemove
);
return
command
;
}
}
else
if
(
readIf
(
"CHANGE"
))
{
...
...
h2/src/main/org/h2/command/ddl/AlterTableAlterColumn.java
浏览文件 @
86462170
...
...
@@ -7,7 +7,7 @@ package org.h2.command.ddl;
import
java.util.ArrayList
;
import
java.util.HashSet
;
import
java.util.Iterator
;
import
org.h2.api.ErrorCode
;
import
org.h2.command.CommandInterface
;
import
org.h2.command.Parser
;
...
...
@@ -57,6 +57,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
private
String
addAfter
;
private
boolean
ifNotExists
;
private
ArrayList
<
Column
>
columnsToAdd
;
private
ArrayList
<
Column
>
columnsToRemove
;
public
AlterTableAlterColumn
(
Session
session
,
Schema
schema
)
{
super
(
session
,
schema
);
...
...
@@ -85,7 +86,6 @@ public class AlterTableAlterColumn extends SchemaCommand {
session
.
getUser
().
checkRight
(
table
,
Right
.
ALL
);
table
.
checkSupportAlter
();
table
.
lock
(
session
,
true
,
true
);
Sequence
sequence
=
oldColumn
==
null
?
null
:
oldColumn
.
getSequence
();
if
(
newColumn
!=
null
)
{
checkDefaultReferencesTable
(
newColumn
.
getDefaultExpression
());
}
...
...
@@ -116,6 +116,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
break
;
}
case
CommandInterface
.
ALTER_TABLE_ALTER_COLUMN_DEFAULT
:
{
Sequence
sequence
=
oldColumn
==
null
?
null
:
oldColumn
.
getSequence
();
checkDefaultReferencesTable
(
defaultExpression
);
oldColumn
.
setSequence
(
null
);
oldColumn
.
setDefaultExpression
(
session
,
defaultExpression
);
...
...
@@ -162,11 +163,11 @@ public class AlterTableAlterColumn extends SchemaCommand {
break
;
}
case
CommandInterface
.
ALTER_TABLE_DROP_COLUMN
:
{
if
(
table
.
getColumns
().
length
==
1
)
{
if
(
table
.
getColumns
().
length
-
columnsToRemove
.
size
()
<
1
)
{
throw
DbException
.
get
(
ErrorCode
.
CANNOT_DROP_LAST_COLUMN
,
oldColumn
.
getSQL
());
columnsToRemove
.
get
(
0
)
.
getSQL
());
}
table
.
drop
SingleColumnConstraintsAndIndexes
(
session
,
oldColumn
);
table
.
drop
MultipleColumnsConstraintsAndIndexes
(
session
,
columnsToRemove
);
copyData
();
break
;
}
...
...
@@ -282,8 +283,20 @@ public class AlterTableAlterColumn extends SchemaCommand {
newColumns
.
add
(
col
.
getClone
());
}
if
(
type
==
CommandInterface
.
ALTER_TABLE_DROP_COLUMN
)
{
int
position
=
oldColumn
.
getColumnId
();
newColumns
.
remove
(
position
);
for
(
Column
removeCol
:
columnsToRemove
)
{
Column
foundCol
=
null
;
for
(
Iterator
<
Column
>
iter
=
newColumns
.
iterator
();
iter
.
hasNext
();
)
{
Column
newCol
=
iter
.
next
();
if
(
newCol
.
getName
()
==
removeCol
.
getName
())
{
foundCol
=
newCol
;
break
;
}
}
if
(
foundCol
==
null
)
{
throw
DbException
.
throwInternalError
(
removeCol
.
getCreateSQL
());
}
newColumns
.
remove
(
foundCol
);
}
}
else
if
(
type
==
CommandInterface
.
ALTER_TABLE_ADD_COLUMN
)
{
int
position
;
if
(
addBefore
!=
null
)
{
...
...
@@ -509,4 +522,8 @@ public class AlterTableAlterColumn extends SchemaCommand {
public
void
setNewColumns
(
ArrayList
<
Column
>
columnsToAdd
)
{
this
.
columnsToAdd
=
columnsToAdd
;
}
public
void
setColumnsToRemove
(
ArrayList
<
Column
>
columnsToRemove
)
{
this
.
columnsToRemove
=
columnsToRemove
;
}
}
h2/src/main/org/h2/table/Table.java
浏览文件 @
86462170
...
...
@@ -9,7 +9,6 @@ import java.util.ArrayList;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.Set
;
import
org.h2.api.ErrorCode
;
import
org.h2.command.Prepared
;
import
org.h2.constraint.Constraint
;
...
...
@@ -531,19 +530,20 @@ public abstract class Table extends SchemaObjectBase {
}
/**
* Check that th
is column is
not referenced by a multi-column constraint or
* Check that th
ese columns are
not referenced by a multi-column constraint or
* multi-column index. If it is, an exception is thrown. Single-column
* references and indexes are dropped.
*
* @param session the session
* @param col
the column
* @param col
umsToDrop the columns to drop
* @throws DbException if the column is referenced by multi-column
* constraints or indexes
*/
public
void
drop
SingleColumn
ConstraintsAndIndexes
(
Session
session
,
Column
col
)
{
ArrayList
<
Constraint
>
constraintsToDrop
=
New
.
arrayLis
t
();
public
void
drop
MultipleColumns
ConstraintsAndIndexes
(
Session
session
,
ArrayList
<
Column
>
columsToDrop
)
{
HashSet
<
Constraint
>
constraintsToDrop
=
New
.
hashSe
t
();
if
(
constraints
!=
null
)
{
for
(
Column
col
:
columsToDrop
)
{
for
(
int
i
=
0
,
size
=
constraints
.
size
();
i
<
size
;
i
++)
{
Constraint
constraint
=
constraints
.
get
(
i
);
HashSet
<
Column
>
columns
=
constraint
.
getReferencedColumns
(
this
);
...
...
@@ -558,9 +558,11 @@ public abstract class Table extends SchemaObjectBase {
}
}
}
ArrayList
<
Index
>
indexesToDrop
=
New
.
arrayList
();
}
HashSet
<
Index
>
indexesToDrop
=
New
.
hashSet
();
ArrayList
<
Index
>
indexes
=
getIndexes
();
if
(
indexes
!=
null
)
{
for
(
Column
col
:
columsToDrop
)
{
for
(
int
i
=
0
,
size
=
indexes
.
size
();
i
<
size
;
i
++)
{
Index
index
=
indexes
.
get
(
i
);
if
(
index
.
getCreateSQL
()
==
null
)
{
...
...
@@ -577,6 +579,7 @@ public abstract class Table extends SchemaObjectBase {
}
}
}
}
for
(
Constraint
c
:
constraintsToDrop
)
{
session
.
getDatabase
().
removeSchemaObject
(
session
,
c
);
}
...
...
h2/src/test/org/h2/test/db/TestAlter.java
浏览文件 @
86462170
...
...
@@ -10,7 +10,6 @@ import java.sql.DatabaseMetaData;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
org.h2.api.ErrorCode
;
import
org.h2.test.TestBase
;
...
...
@@ -38,6 +37,7 @@ public class TestAlter extends TestBase {
stat
=
conn
.
createStatement
();
testAlterTableAlterColumnAsSelfColumn
();
testAlterTableDropColumnWithReferences
();
testAlterTableDropMultipleColumns
();
testAlterTableAlterColumnWithConstraint
();
testAlterTableAlterColumn
();
testAlterTableAddColumnIdentity
();
...
...
@@ -108,6 +108,17 @@ public class TestAlter extends TestBase {
}
private
void
testAlterTableDropMultipleColumns
()
throws
SQLException
{
stat
.
execute
(
"create table test(id int, name varchar, name2 varchar)"
);
stat
.
execute
(
"alter table test drop column name, name2"
);
stat
.
execute
(
"drop table test"
);
stat
.
execute
(
"create table test(id int, name varchar, name2 varchar)"
);
assertThrows
(
ErrorCode
.
CANNOT_DROP_LAST_COLUMN
,
stat
).
execute
(
"alter table test drop column id, name, name2"
);
stat
.
execute
(
"drop table test"
);
}
/**
* Tests a bug we used to have where altering the name of a column that had
* a check constraint that referenced itself would result in not being able
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论