1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package edu.caltech.nanodb.commands;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import edu.caltech.nanodb.server.NanoDBServer;
import edu.caltech.nanodb.storage.TableManager;
import edu.caltech.nanodb.storage.StorageManager;
/** This Command class represents the <tt>DROP TABLE</tt> SQL command. */
public class DropTableCommand extends Command {
/** A logging object for reporting anything interesting that happens. **/
private static Logger logger = LogManager.getLogger(DropTableCommand.class);
/** The name of the table to drop from the database. */
private String tableName;
/**
* This flag controls whether the drop-table command will fail if the
* table already doesn't exist when the removal is attempted.
*/
private boolean ifExists;
/**
* Construct a drop-table command for the named table.
*
* @param tableName the name of the table to drop.
* @param ifExists a flag controlling whether the command should complain if
* the table already doesn't exist when the removal is attempted.
*/
public DropTableCommand(String tableName, boolean ifExists) {
super(Command.Type.DDL);
this.tableName = tableName;
this.ifExists = ifExists;
}
/**
* Get the name of the table to be dropped.
*
* @return the name of the table to drop
*/
public String getTableName() {
return tableName;
}
/**
* Returns the value of the "if exists" flag; true indicates that it is
* not an error if the table doesn't exist when this command is issued.
*
* @return the value of the "if exists" flag
*/
public boolean getIfExists() {
return ifExists;
}
/**
* This method executes the <tt>DROP TABLE</tt> command by calling the
* {@link TableManager#dropTable} method with the specified table name.
*
* @throws ExecutionException if the table doesn't actually exist, or if
* the table cannot be deleted for some reason.
*/
@Override
public void execute(NanoDBServer server) throws ExecutionException {
StorageManager storageManager = server.getStorageManager();
TableManager tableManager = storageManager.getTableManager();
// See if the table already doesn't exist.
if (ifExists && !tableManager.tableExists(tableName)) {
// Table already doesn't exist! Skip the operation.
out.printf("Table %s already doesn't exist; skipping drop-table.%n",
tableName);
return;
}
tableManager.dropTable(tableName);
}
@Override
public String toString() {
return "DropTable[" + tableName + "]";
}
}