提交 eadac55f authored 作者: lukaseder's avatar lukaseder

[#285] Add support for ALTER SEQUENCE IF EXISTS

上级 77609335
......@@ -210,7 +210,7 @@ ALTER SCHEMA TEST RENAME TO PRODUCTION
"
"Commands (DDL)","ALTER SEQUENCE","
ALTER SEQUENCE sequenceName [ RESTART WITH long ] [ INCREMENT BY long ]
ALTER SEQUENCE [ IF EXISTS ] sequenceName [ RESTART WITH long ] [ INCREMENT BY long ]
[ MINVALUE long | NOMINVALUE | NO MINVALUE ]
[ MAXVALUE long | NOMAXVALUE | NO MAXVALUE ]
[ CYCLE long | NOCYCLE | NO CYCLE ]
......
......@@ -4891,10 +4891,11 @@ public class Parser {
}
private AlterSequence parseAlterSequence() {
boolean ifExists = readIfExists(false);
String sequenceName = readIdentifierWithSchema();
Sequence sequence = getSchema().getSequence(sequenceName);
AlterSequence command = new AlterSequence(session, sequence.getSchema());
command.setSequence(sequence);
AlterSequence command = new AlterSequence(session, getSchema());
command.setSequenceName(sequenceName);
command.setIfExists(ifExists);
while (true) {
if (readIf("RESTART")) {
read("WITH");
......
......@@ -24,7 +24,9 @@ import org.h2.table.Table;
*/
public class AlterSequence extends SchemaCommand {
private boolean ifExists;
private Table table;
private String sequenceName;
private Sequence sequence;
private Expression start;
private Expression increment;
......@@ -37,8 +39,12 @@ public class AlterSequence extends SchemaCommand {
super(session, schema);
}
public void setSequence(Sequence sequence) {
this.sequence = sequence;
public void setIfExists(boolean b) {
ifExists = b;
}
public void setSequenceName(String sequenceName) {
this.sequenceName = sequenceName;
}
@Override
......@@ -49,7 +55,7 @@ public class AlterSequence extends SchemaCommand {
public void setColumn(Column column) {
table = column.getTable();
sequence = column.getSequence();
if (sequence == null) {
if (sequence == null && !ifExists) {
throw DbException.get(ErrorCode.SEQUENCE_NOT_FOUND_1, column.getSQL());
}
}
......@@ -81,6 +87,16 @@ public class AlterSequence extends SchemaCommand {
@Override
public int update() {
Database db = session.getDatabase();
if (sequence == null) {
sequence = getSchema().findSequence(sequenceName);
if (sequence == null) {
if (!ifExists) {
throw DbException.get(ErrorCode.SEQUENCE_NOT_FOUND_1, sequenceName);
} else {
return 0;
}
}
}
if (table != null) {
session.getUser().checkRight(table, Right.ALL);
}
......
......@@ -75,7 +75,7 @@ ALTER SCHEMA schema RENAME TO newSchemaName
","
Renames a schema."
"Commands (DDL)","ALTER SEQUENCE","
ALTER SEQUENCE sequenceName [ RESTART WITH long ] [ INCREMENT BY long ]
ALTER SEQUENCE [ IF EXISTS ] sequenceName [ RESTART WITH long ] [ INCREMENT BY long ]
[ MINVALUE long | NOMINVALUE | NO MINVALUE ]
[ MAXVALUE long | NOMAXVALUE | NO MAXVALUE ]
[ CYCLE long | NOCYCLE | NO CYCLE ]
......@@ -92,6 +92,10 @@ Adds a new column to a table."
ALTER TABLE tableName ADD constraint [ CHECK | NOCHECK ]
","
Adds a constraint to a table."
"Commands (DDL)","ALTER TABLE RENAME CONSTRAINT","
ALTER TABLE tableName RENAME oldConstraintName TO newConstraintName
","
Renames a constraint."
"Commands (DDL)","ALTER TABLE ALTER COLUMN","
ALTER TABLE tableName ALTER COLUMN columnName
{ { dataType [ DEFAULT expression ] [ [ NOT ] NULL ] [ AUTO_INCREMENT | IDENTITY ] }
......
......@@ -4455,6 +4455,24 @@ create index if not exists idx_id on s.test(id);
alter index s.idx_id rename to s.index_id;
> ok
alter sequence if exists s.seq restart with 10;
> ok
create sequence s.seq cache 0;
> ok
alter sequence if exists s.seq restart with 3;
> ok
select s.seq.nextval as x;
> X
> -
> 3
> rows: 1
drop sequence s.seq;
> ok
create sequence s.seq cache 0;
> ok
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论