KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > tools > DeleteDbFiles


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.tools;
6
7 import java.sql.SQLException JavaDoc;
8
9 import org.h2.engine.Constants;
10 import org.h2.util.FileUtils;
11
12 /**
13  * Delete the database files. The database must be closed before calling this tool.
14  *
15  * @author Thomas
16  */

17
18 public class DeleteDbFiles extends FileBase {
19     
20     private boolean quiet;
21
22     private void showUsage() {
23         System.out.println("java "+getClass().getName()+" [-dir <dir>] [-db <database>] [-quiet]");
24     }
25     
26     /**
27      * The command line interface for this tool.
28      * The options must be split into strings like this: "-db", "test",...
29      * The following options are supported:
30      * <ul>
31      * <li>-help or -? (print the list of options)
32      * <li>-dir directory (the default is the current directory)
33      * <li>-db databaseName (all databases if no name is specified)
34      * <li>-quiet does not print progress information
35      * </ul>
36      *
37      * @param args the command line arguments
38      * @throws SQLException
39      */

40     public static void main(String JavaDoc[] args) throws SQLException JavaDoc {
41         new DeleteDbFiles().run(args);
42     }
43
44     private void run(String JavaDoc[] args) throws SQLException JavaDoc {
45         String JavaDoc dir = ".";
46         String JavaDoc db = null;
47         boolean quiet = false;
48         for(int i=0; args != null && i<args.length; i++) {
49             if(args[i].equals("-dir")) {
50                 dir = args[++i];
51             } else if(args[i].equals("-db")) {
52                 db = args[++i];
53             } else if(args[i].equals("-quiet")) {
54                 quiet = true;
55             } else {
56                 showUsage();
57                 return;
58             }
59         }
60         execute(dir, db, quiet);
61     }
62     
63     /**
64      * Deletes the database files.
65      *
66      * @param dir the directory
67      * @param db the database name (null for all databases)
68      * @param quiet don't print progress information
69      * @throws SQLException
70      */

71     public static void execute(String JavaDoc dir, String JavaDoc db, boolean quiet) throws SQLException JavaDoc {
72         DeleteDbFiles delete = new DeleteDbFiles();
73         delete.quiet = quiet;
74         delete.processFiles(dir, db, !quiet);
75     }
76
77     protected void process(String JavaDoc fileName) throws SQLException JavaDoc {
78         if(quiet || fileName.endsWith(Constants.SUFFIX_TEMP_FILE) || fileName.endsWith(Constants.SUFFIX_TRACE_FILE)) {
79             FileUtils.tryDelete(fileName);
80         } else {
81             FileUtils.delete(fileName);
82         }
83     }
84     
85     protected boolean allFiles() {
86         return true;
87     }
88
89 }
90
Popular Tags