| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.command.ddl; |
| 7 |
|
| 8 |
import org.h2.api.ErrorCode; |
| 9 |
import org.h2.command.CommandInterface; |
| 10 |
import org.h2.engine.Right; |
| 11 |
import org.h2.engine.SessionLocal; |
| 12 |
import org.h2.message.DbException; |
| 13 |
import org.h2.schema.Schema; |
| 14 |
import org.h2.schema.Sequence; |
| 15 |
import org.h2.table.Column; |
| 16 |
|
| 17 |
/** |
| 18 |
* This class represents the statement ALTER SEQUENCE. |
| 19 |
*/ |
| 20 |
public class AlterSequence extends SchemaOwnerCommand { |
| 21 |
|
| 22 |
private boolean ifExists; |
| 23 |
|
| 24 |
private Column column; |
| 25 |
|
| 26 |
private Boolean always; |
| 27 |
|
| 28 |
private String sequenceName; |
| 29 |
|
| 30 |
private Sequence sequence; |
| 31 |
|
| 32 |
private SequenceOptions options; |
| 33 |
|
| 34 |
public AlterSequence(SessionLocal session, Schema schema) { |
| 35 |
super(session, schema); |
| 36 |
transactional = true; |
| 37 |
} |
| 38 |
|
| 39 |
public void setIfExists(boolean b) { |
| 40 |
ifExists = b; |
| 41 |
} |
| 42 |
|
| 43 |
public void setSequenceName(String sequenceName) { |
| 44 |
this.sequenceName = sequenceName; |
| 45 |
} |
| 46 |
|
| 47 |
public void setOptions(SequenceOptions options) { |
| 48 |
this.options = options; |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public boolean isTransactional() { |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Set the column |
| 58 |
* |
| 59 |
* @param column the column |
| 60 |
* @param always whether value should be always generated, or null if "set |
| 61 |
* generated is not specified |
| 62 |
*/ |
| 63 |
public void setColumn(Column column, Boolean always) { |
| 64 |
this.column = column; |
| 65 |
this.always = always; |
| 66 |
sequence = column.getSequence(); |
| 67 |
if (sequence == null && !ifExists) { |
| 68 |
throw DbException.get(ErrorCode.SEQUENCE_NOT_FOUND_1, column.getTraceSQL()); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
long update(Schema schema) { |
| 74 |
if (sequence == null) { |
| 75 |
sequence = schema.findSequence(sequenceName); |
| 76 |
if (sequence == null) { |
| 77 |
if (!ifExists) { |
| 78 |
throw DbException.get(ErrorCode.SEQUENCE_NOT_FOUND_1, sequenceName); |
| 79 |
} |
| 80 |
return 0; |
| 81 |
} |
| 82 |
} |
| 83 |
if (column != null) { |
| 84 |
session.getUser().checkTableRight(column.getTable(), Right.SCHEMA_OWNER); |
| 85 |
} |
| 86 |
options.setDataType(sequence.getDataType()); |
| 87 |
Long startValue = options.getStartValue(session); |
| 88 |
sequence.modify( |
| 89 |
options.getRestartValue(session, startValue != null ? startValue : sequence.getStartValue()), |
| 90 |
startValue, |
| 91 |
options.getMinValue(sequence, session), options.getMaxValue(sequence, session), |
| 92 |
options.getIncrement(session), options.getCycle(), options.getCacheSize(session)); |
| 93 |
sequence.flush(session); |
| 94 |
if (column != null && always != null) { |
| 95 |
column.setSequence(sequence, always); |
| 96 |
getDatabase().updateMeta(session, column.getTable()); |
| 97 |
} |
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public int getType() { |
| 103 |
return CommandInterface.ALTER_SEQUENCE; |
| 104 |
} |
| 105 |
|
| 106 |
} |