1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://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.Session; |
12 | import org.h2.message.DbException; |
13 | import org.h2.table.Table; |
14 | |
15 | /** |
16 | * This class represents the statement |
17 | * TRUNCATE TABLE |
18 | */ |
19 | public class TruncateTable extends DefineCommand { |
20 | |
21 | private Table table; |
22 | |
23 | public TruncateTable(Session session) { |
24 | super(session); |
25 | } |
26 | |
27 | public void setTable(Table table) { |
28 | this.table = table; |
29 | } |
30 | |
31 | @Override |
32 | public int update() { |
33 | session.commit(true); |
34 | if (!table.canTruncate()) { |
35 | throw DbException.get(ErrorCode.CANNOT_TRUNCATE_1, table.getSQL()); |
36 | } |
37 | session.getUser().checkRight(table, Right.DELETE); |
38 | table.lock(session, true, true); |
39 | table.truncate(session); |
40 | return 0; |
41 | } |
42 | |
43 | @Override |
44 | public int getType() { |
45 | return CommandInterface.TRUNCATE_TABLE; |
46 | } |
47 | |
48 | } |