File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\tools\DeleteDbFiles.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.tools;
8
9 import java.sql.SQLException;
10 import java.util.ArrayList;
11
12 import org.h2.engine.Constants;
13 import org.h2.store.FileLister;
14 import org.h2.store.fs.FileUtils;
15 import org.h2.util.Tool;
16
17 /**
18 * Deletes all files belonging to a database.
19 * <br />
20 * The database must be closed before calling this tool.
21 * @h2.resource
22 */
23 public class DeleteDbFiles extends Tool {
24
25 /**
26 * Options are case sensitive. Supported options are:
27 * <table>
28 * <tr><td>[-help] or [-?]</td>
29 * <td>Print the list of options</td></tr>
30 * <tr><td>[-dir &lt;dir&gt;]</td>
31 * <td>The directory (default: .)</td></tr>
32 * <tr><td>[-db &lt;database&gt;]</td>
33 * <td>The database name</td></tr>
34 * <tr><td>[-quiet]</td>
35 * <td>Do not print progress information</td></tr>
36 * </table>
37 * @h2.resource
38 *
39 * @param args the command line arguments
40 */
41 public static void main(String... args) throws SQLException {
42 new DeleteDbFiles().runTool(args);
43 }
44
45 public void runTool(String... args) throws SQLException {
46 String dir = ".";
47 String db = null;
48 boolean quiet = false;
49 for (int i = 0; args != null && i < args.length; i++) {
50 String arg = args[i];
51 if (arg.equals("-dir")) {
52 dir = args[++i];
53 } else if (arg.equals("-db")) {
54 db = args[++i];
55 } else if (arg.equals("-quiet")) {
56 quiet = true;
57 } else if (arg.equals("-help") || arg.equals("-?")) {
58 showUsage();
59 return;
60 } else {
61 showUsageAndThrowUnsupportedOption(arg);
62 }
63 }
64 process(dir, db, quiet);
65 }
66
67 /**
68 * Deletes the database files.
69 *
70 * @param dir the directory
71 * @param db the database name (null for all databases)
72 * @param quiet don't print progress information
73 */
74 public static void execute(String dir, String db, boolean quiet) {
75 new DeleteDbFiles().process(dir, db, quiet);
76 }
77
78 /**
79 * Deletes the database files.
80 *
81 * @param dir the directory
82 * @param db the database name (null for all databases)
83 * @param quiet don't print progress information
84 */
85 private void process(String dir, String db, boolean quiet) {
86 ArrayList<String> files = FileLister.getDatabaseFiles(dir, db, true);
87 if (files.size() == 0 && !quiet) {
88 printNoDatabaseFilesFound(dir, db);
89 }
90 for (String fileName : files) {
91 process(fileName, quiet);
92 if (!quiet) {
93 out.println("Processed: " + fileName);
94 }
95 }
96 }
97
98 private static void process(String fileName, boolean quiet) {
99 if (FileUtils.isDirectory(fileName)) {
100 // only delete empty directories
101 FileUtils.tryDelete(fileName);
102 } else if (quiet || fileName.endsWith(Constants.SUFFIX_TEMP_FILE) || fileName.endsWith(Constants.SUFFIX_TRACE_FILE)) {
103 FileUtils.tryDelete(fileName);
104 } else {
105 FileUtils.delete(fileName);
106 }
107 }
108
109 }
110