1 /*
2 Bloof - visualize the evolution of your software project
3 Copyright ( C ) 2003 Lukasz Pekacki <lukasz@pekacki.de>
4 http://bloof.sf.net/
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program; if not, write to the Free Software Foundation, Inc.,
16 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 $RCSfile: PostgresControl.java,v $
19 Created on $Date: 2003/09/06 08:35:09 $
20 */
21 package net.sf.bloof.db;
22
23
24 import java.io.IOException;
25 import java.sql.Connection;
26 import java.sql.DriverManager;
27 import java.sql.SQLException;
28 import java.util.logging.Logger;
29
30 /***
31 * This class controls the access to a Postgres database
32 * @author Lukasz Pekacki <pekacki@users.sourceforge.net>
33 * @version $Id: PostgresControl.java,v 1.8 2003/09/06 08:35:09 pekacki Exp $
34 */
35 public class PostgresControl {
36
37 /***
38 * Closes the database, freeing resources
39 * @throws SQLException on error
40 * @throws IOException on error
41 */
42 public static void close() throws SQLException, IOException {
43 if (sConnection != null) {
44 sConnection.close();
45 sConnection = null;
46 }
47 }
48 /***
49 * Creates a new McKoi SQL Database
50 * @param aDbAccess access info for database
51 * @return Connection to the database
52 * @throws SQLException if error on database connect occurs
53 * @throws IOException if error on file access
54 */
55 public static Connection create(DbAccess aDbAccess) throws SQLException, IOException {
56 if (sConnection != null) {
57 /* drop and recrate tables */
58 dropTables();
59 createTables();
60 return sConnection;
61 } else {
62 open(aDbAccess);
63 dropTables();
64 createTables();
65 return sConnection;
66 }
67 }
68 /***
69 * Create indices on the databaes for faster processing
70 * @throws IOException on access error to ddl file
71 */
72 public static void createIndices() throws IOException {
73 Database.proccessDBScript(Database.CREATE_INDEX_FILE, sConnection);
74 }
75
76 /***
77 * Method createTables.
78 */
79 private static void createTables() throws IOException {
80 Database.proccessDBScript(Database.CREATE_TABLE_FILE, sConnection);
81 }
82
83 /***
84 * Method dropTables.
85 */
86 private static void dropTables() throws IOException {
87 Database.proccessDBScript(Database.DROP_TABLE_FILE, sConnection);
88 }
89
90 /***
91 * Returns true if the given String has the prefix of this Database JDBC URL
92 * @param aDbUrl URL to check
93 * @return true if the given String has the prefix of this Database JDBC URL
94 */
95 public static boolean isMyUrl(String aDbUrl) {
96 return aDbUrl.startsWith(MY_URL_PREFIX);
97 }
98 /***
99 * Starts a local internal McKoi SQL Database;
100 * if the database does not exist yet, it will be created
101 * @param aDbAccess access info for database
102 * @return Connection to the database
103 * @throws SQLException if error on database connect occurs
104 */
105 public static Connection open(DbAccess aDbAccess) throws SQLException {
106 if (sConnection != null) {
107 return sConnection;
108 } else {
109 try {
110 Class.forName("org.postgresql.Driver");
111 } catch (ClassNotFoundException e) {
112 sLogger.warning("Could not load postgres JDBC Driver. " + e.toString());
113 throw new SQLException("Could not load postgres JDBC Driver. " + e.toString());
114 }
115 sConnection =
116 DriverManager.getConnection(
117 aDbAccess.getDatabaseUrl(),
118 aDbAccess.getLogin(),
119 aDbAccess.getPassword());
120 return sConnection;
121 }
122 }
123 /***
124 * Name and prefix of the Database
125 * */
126 public static final String MY_URL_PREFIX = "jdbc:postgresql:", NAME = "Postgres";
127 private static Connection sConnection;
128 private static Logger sLogger = Logger.getLogger(PostgresControl.class.getPackage().getName());
129 }
This page was automatically generated by Maven