Debian Codegraph @

src/main/org/h2/command/ddl/TruncateTable.java

raw
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.Sequence;
14 import org.h2.table.Column;
15 import org.h2.table.Table;
16
17 /**
18 * This class represents the statement
19 * TRUNCATE TABLE
20 */
21 public class TruncateTable extends DefineCommand {
22
23 private Table table;
24
25 private boolean restart;
26
27 public TruncateTable(SessionLocal session) {
28 super(session);
29 }
30
31 public void setTable(Table table) {
32 this.table = table;
33 }
34
35 public void setRestart(boolean restart) {
36 this.restart = restart;
37 }
38
39 @Override
40 public long update() {
41 if (!table.canTruncate()) {
42 throw DbException.get(ErrorCode.CANNOT_TRUNCATE_1, table.getTraceSQL());
43 }
44 session.getUser().checkTableRight(table, Right.DELETE);
45 table.lock(session, Table.EXCLUSIVE_LOCK);
46 long result = table.truncate(session);
47 if (restart) {
48 for (Column column : table.getColumns()) {
49 Sequence sequence = column.getSequence();
50 if (sequence != null) {
51 sequence.modify(sequence.getStartValue(), null, null, null, null, null, null);
52 getDatabase().updateMeta(session, sequence);
53 }
54 }
55 }
56 return result;
57 }
58
59 @Override
60 public int getType() {
61 return CommandInterface.TRUNCATE_TABLE;
62 }
63
64 }