01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.tools;
07:
08: import java.sql.SQLException;
09: import java.util.ArrayList;
10:
11: import org.h2.engine.Constants;
12: import org.h2.store.FileLister;
13: import org.h2.util.FileUtils;
14:
15: /**
16: * Delete the database files. The database must be closed before calling this
17: * tool.
18: */
19: public class DeleteDbFiles {
20:
21: private void showUsage() {
22: System.out.println("java " + getClass().getName()
23: + " [-dir <dir>] [-db <database>] [-quiet]");
24: System.out
25: .println("See also http://h2database.com/javadoc/org/h2/tools/DeleteDbFiles.html");
26: }
27:
28: /**
29: * The command line interface for this tool.
30: * The options must be split into strings like this: "-db", "test",...
31: * Options are case sensitive. The following options are supported:
32: * <ul>
33: * <li>-help or -? (print the list of options)
34: * </li><li>-dir database directory (the default is the current directory)
35: * </li><li>-db database name (all databases if no name is specified)
36: * </li><li>-quiet does not print progress information
37: * </li></ul>
38: *
39: * @param args the command line arguments
40: * @throws SQLException
41: */
42: public static void main(String[] args) throws SQLException {
43: new DeleteDbFiles().run(args);
44: }
45:
46: private void run(String[] args) throws SQLException {
47: String dir = ".";
48: String db = null;
49: boolean quiet = false;
50: for (int i = 0; args != null && i < args.length; i++) {
51: if (args[i].equals("-dir")) {
52: dir = args[++i];
53: } else if (args[i].equals("-db")) {
54: db = args[++i];
55: } else if (args[i].equals("-quiet")) {
56: quiet = true;
57: } else {
58: showUsage();
59: return;
60: }
61: }
62: execute(dir, db, quiet);
63: }
64:
65: /**
66: * Deletes the database files.
67: *
68: * @param dir the directory
69: * @param db the database name (null for all databases)
70: * @param quiet don't print progress information
71: * @throws SQLException
72: */
73: public static void execute(String dir, String db, boolean quiet)
74: throws SQLException {
75: DeleteDbFiles delete = new DeleteDbFiles();
76: ArrayList files = FileLister.getDatabaseFiles(dir, db, true);
77: for (int i = 0; i < files.size(); i++) {
78: String fileName = (String) files.get(i);
79: delete.process(fileName, quiet);
80: if (!quiet) {
81: System.out.println("processed: " + fileName);
82: }
83: }
84: if (files.size() == 0 && !quiet) {
85: System.out.println("No database files found");
86: }
87: }
88:
89: private void process(String fileName, boolean quiet)
90: throws SQLException {
91: if (quiet || fileName.endsWith(Constants.SUFFIX_TEMP_FILE)
92: || fileName.endsWith(Constants.SUFFIX_TRACE_FILE)) {
93: FileUtils.tryDelete(fileName);
94: } else {
95: FileUtils.delete(fileName);
96: }
97: }
98:
99: }
|