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: McKoiControl.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.sql.Statement;
29
30 import com.mckoi.database.control.DBController;
31 import com.mckoi.database.control.DBSystem;
32 import com.mckoi.database.control.DefaultDBConfig;
33
34 /***
35 * This class is responsible for controlling the internal database McKoi.
36 * @see <a href = "http://www.mckoi.com">McKoi SQL Database</a>
37 * @author Lukasz Pekacki <pekacki@users.sourceforge.net>
38 * @version $Id: McKoiControl.java,v 1.9 2003/09/06 08:35:09 pekacki Exp $
39 */
40 public class McKoiControl {
41
42 /***
43 * Closes the database, freeing resources
44 * @throws SQLException on error
45 * @throws IOException on error
46 */
47 public static void close() throws SQLException, IOException {
48 if (sConnection != null){
49 sConnection.close();
50 sSession.close();
51 sConnection = null;
52 sSession = null;
53 }
54 }
55
56 private static DefaultDBConfig configDatabase(DbAccess aDbAccess) {
57 DefaultDBConfig config = new DefaultDBConfig();
58 config.setDatabasePath(fetchLocalDatabasePath(aDbAccess));
59 config.setValue("dont_synch_filesystem", "enabled");
60 config.setValue("table_lock_check", "off");
61 config.setValue("debug_level", "40");
62 config.setLogPath(fetchLocalDatabasePath(aDbAccess) + java.io.File.separator + "log");
63 return config;
64
65 }
66 /***
67 * Creates a new McKoi SQL Database
68 * @param aDbAccess access info for database
69 * @return Connection to the database
70 * @throws SQLException if error on database connect occurs
71 * @throws IOException if the dll file could not be read
72 */
73 public static Connection create(DbAccess aDbAccess) throws SQLException, IOException {
74 if (sSession != null) {
75 /* drop and recrate tables */
76 dropTables();
77 dropSchema();
78 createSchema();
79 selectSchema();
80 createTables();
81 return sConnection;
82 } else {
83 // create a database configuration
84 DefaultDBConfig config = configDatabase(aDbAccess);
85 DBController control = DBController.getDefault();
86 // Does the database exist already?
87 if (control.databaseExists(config)) {
88 sSession = control.startDatabase(config);
89 sConnection = sSession.getConnection(aDbAccess.getLogin(), aDbAccess.getPassword());
90 selectSchema();
91 /* drop and recreate tables */
92 dropTables();
93 dropSchema();
94 createSchema();
95 selectSchema();
96 createTables();
97 } else {
98 sSession =
99 control.createDatabase(config, aDbAccess.getLogin(), aDbAccess.getPassword());
100 sConnection = sSession.getConnection(aDbAccess.getLogin(), aDbAccess.getPassword());
101 /* create tables */
102 createSchema();
103 selectSchema();
104 createTables();
105 }
106 return sConnection;
107 }
108 }
109 /***
110 * Create indices on the databaes for faster processing
111 * @throws IOException on access error to ddl file
112 */
113 public static void createIndices() throws IOException {
114 Database.proccessDBScript(Database.CREATE_INDEX_FILE, sConnection);
115 }
116 /***
117 * Creates a connection to remote McKoi SQL Database
118 * @param aDbAccess access info for database
119 * @return Connection to the database
120 * @throws SQLException if error on database connect occurs
121 * @throws IOException if the dll file could not be read
122 */
123 public static Connection createRemoteConnection(DbAccess aDbAccess)
124 throws SQLException, IOException {
125 if (sConnection != null) {
126 /* drop and recrate tables */
127 dropTables();
128 createTables();
129 return sConnection;
130 } else {
131 openRemote(aDbAccess);
132 dropTables();
133 createTables();
134 return sConnection;
135 }
136 }
137 private static void createSchema() throws SQLException {
138 Statement statement = sConnection.createStatement();
139 statement.execute("CREATE SCHEMA bloof");
140 }
141
142 private static void createTables() throws IOException {
143 Database.proccessDBScript(Database.CREATE_TABLE_FILE, sConnection);
144 }
145 private static void dropSchema() throws SQLException {
146 Statement statement = sConnection.createStatement();
147 statement.execute("DROP SCHEMA bloof");
148 }
149 private static void dropTables() throws IOException {
150 Database.proccessDBScript(Database.DROP_TABLE_FILE, sConnection);
151 }
152
153 private static String fetchLocalDatabasePath(DbAccess aDbAccess) {
154 return aDbAccess.getDatabaseUrl();
155 }
156
157 /***
158 * Returns true if the given String has the prefix of this Database JDBC URL
159 * @param aDbUrl URL to check
160 * @return true if the given String has the prefix of this Database JDBC URL
161 */
162 public static boolean isMyUrl(String aDbUrl) {
163 return aDbUrl.startsWith(MY_URL_PREFIX);
164 }
165
166 /***
167 * Starts a local internal McKoi SQL Database;
168 * if the database does not exist yet, it will be created
169 * @param aDbAccess access info for database
170 * @return Connection to the database
171 * @throws SQLException if error on database connect occurs
172 * @throws IOException if the dll file could not be read
173 */
174 public static Connection open(DbAccess aDbAccess) throws SQLException, IOException {
175 if (sSession != null && sConnection != null) {
176 return sConnection;
177 } else {
178 // create a database configuration
179 DefaultDBConfig config = configDatabase(aDbAccess);
180 DBController control = DBController.getDefault();
181 // Does the database exist already?
182 if (control.databaseExists(config)) {
183 sSession = control.startDatabase(config);
184 sConnection = sSession.getConnection(aDbAccess.getLogin(), aDbAccess.getPassword());
185 selectSchema();
186 return sConnection;
187 } else {
188 throw new SQLException("An internal database does not exist yet. Create a new project first.");
189 }
190 }
191 }
192 /***
193 * Opens a connection to remote McKoi SQL Database;
194 * @param aDbAccess access info for database
195 * @return Connection to the database
196 * @throws SQLException if error on database connect occurs
197 */
198 public static Connection openRemote(DbAccess aDbAccess) throws SQLException {
199 if (sConnection != null ) {
200 return sConnection;
201 } else {
202 try {
203 Class.forName("com.mckoi.JDBCDriver");
204 } catch (ClassNotFoundException e) {
205 throw new SQLException("Could not load mckoi JDBC Driver. " + e.toString());
206 }
207 sConnection =
208 DriverManager.getConnection(
209 aDbAccess.getDatabaseUrl(),
210 aDbAccess.getLogin(),
211 aDbAccess.getPassword());
212 return sConnection;
213 }
214 }
215
216 private static void selectSchema() throws SQLException {
217 Statement statement = sConnection.createStatement();
218 statement.execute("SET SCHEMA bloof");
219 }
220
221 /***
222 * Name and prefix of the Database
223 * */
224 public static final String MY_URL_PREFIX = "jdbc:mckoi:", NAME = "McKoi";
225 private static Connection sConnection = null;
226 private static DBSystem sSession = null;
227 }
This page was automatically generated by Maven