Source code: com/lilacsoftware/orca/DBManager/TableBuilder.java
1 /*
2 * This file is part of [PROGRAM NAME] - [What it does in brief]
3 * Copyright (c) 2001 [Author]
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16 package com.lilacsoftware.orca.DBManager;
17
18 import java.sql.*;
19 import java.util.ArrayList;
20 /**
21 * DB class for creating and updating and up to date Orca database. If the DB
22 * already exists, TableBuilder determines which tables need to be added or
23 * altered (or later deleted.)
24 *
25 *@author user
26 *@created November 6, 2001
27 */
28 public class TableBuilder
29 {
30 private int softwareDBVersion = 0;
31 private boolean upToDate = false;
32 private ArrayList createTableList = new ArrayList();
33 private ArrayList alterTableList = new ArrayList();
34 private DBConnectionManager m_connMgr;
35
36
37 /**
38 *@param softwareDBVersion The database version number that the software
39 * can work with.
40 *@exception SQLException Description of Exception
41 *@since
42 */
43 public TableBuilder( int softwareDBVersion )
44 throws SQLException
45 {
46 this.softwareDBVersion = softwareDBVersion;
47 init();
48
49 }
50
51
52 /**
53 *@return the upToDate value
54 *@since
55 */
56 public boolean isUpToDate()
57 {
58 return upToDate;
59 }
60
61
62 /**
63 * Gets the currentDBVersion attribute of the TableBuilder object
64 *
65 *@return The currentDBVersion value
66 *@exception SQLException Description of Exception
67 *@since
68 */
69 public int getCurrentDBVersion()
70 throws SQLException
71 {
72 if ( !databaseHasTables() ) {
73 return -1;
74 }
75 if ( !hasTable( "dbversion" ) ) {
76 //Version 0 had no dbversion table
77 return 0;
78 }
79
80 int dbVersion = 0;
81 // get database version from dbversino table
82 try {
83 Connection con = m_connMgr.getConnection();
84 if ( con == null ) {
85 throw new SQLException( "Can't get pool connection...." );
86 }
87
88 ResultSet rs = null;
89 Statement stmt = null;
90 try {
91 stmt = con.createStatement();
92 rs = stmt.executeQuery( "SELECT * FROM dbversion" );
93 rs.next();
94 dbVersion = rs.getInt( 1 );
95 rs.close();
96 stmt.close();
97 }
98 //tbd better sql exception handling
99 finally {
100 m_connMgr.freeConnection( con );
101 }
102
103 }
104 catch ( SQLException e ) {
105 e.printStackTrace();
106 //need way better sqlexc handlinjg....
107 }
108 return dbVersion;
109 }
110
111
112 /**
113 * Description of the Method
114 *
115 *@since
116 */
117 public void createAllTables()
118 {
119 ;
120 }
121
122
123 /**
124 * Description of the Method
125 *
126 *@since
127 */
128 public void updateDatabase()
129 {
130 ;
131 }
132
133
134 /**
135 * Description of the Method
136 *
137 *@return Description of the Returned Value
138 *@exception SQLException Description of Exception
139 *@since
140 */
141 public boolean databaseHasTables()
142 throws SQLException
143 {
144 return hasTable( "tracktable" );
145 }
146
147
148
149 /**
150 * Utility to check if the database contains a named table.
151 *
152 *@param tableName
153 *@return true if table exists
154 *@exception SQLException
155 *@since
156 */
157 public boolean hasTable( String tableName )
158 throws SQLException
159 {
160 ResultSet rs = null;
161 boolean hasTable = false;
162 Connection con = m_connMgr.getConnection();
163 if ( con == null ) {
164 throw new SQLException( "Can't get pool connection...." );
165 }
166 try {
167 DatabaseMetaData md = con.getMetaData();
168
169 rs = md.getTables( null, null, tableName, null );
170 if ( !rs.next() ) {
171 hasTable = false;
172 }
173 else {
174 hasTable = true;
175 }
176 return hasTable;
177 }
178 finally {
179 if ( rs != null ) {
180 try {
181 rs.close();
182 }
183 catch ( SQLException e1 ) {
184 }
185 }
186 rs = null;
187 m_connMgr.freeConnection( con );
188 }
189 }
190
191
192 /**
193 * Initializes, determining the up-to-dateness of the database.
194 *
195 *@exception SQLException Description of Exception
196 *@since
197 */
198 private void init()
199 throws SQLException
200 {
201 m_connMgr = DBConnectionManager.getInstance();
202 int currentDBVersion = getCurrentDBVersion();
203 if ( currentDBVersion == softwareDBVersion ) {
204 upToDate = true;
205 }
206 else if ( currentDBVersion > softwareDBVersion ) {
207 upToDate = false;
208 }
209 else {
210 //Shouldn't run with software dbVersion that is earlier than the current database.
211 System.out.println( "" );
212 System.out.println( "" );
213 System.out.println( "SERIOUS ERROR: This database is designed to work with a later" );
214 System.out.println( " version of Orca. You should obtain a later software version." );
215 System.out.println( " Orca will continue, but consider yourself warned!" );
216 System.out.println( "" );
217 upToDate = true;
218 }
219 }
220
221 }
222