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