1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class for upgrading the database to version 1.5
|
4
|
* Copyright: 2000 Regents of the University of California and the
|
5
|
* National Center for Ecological Analysis and Synthesis
|
6
|
* Authors: Saurabh Garg
|
7
|
*
|
8
|
* '$Author: daigle $'
|
9
|
* '$Date: 2009-08-24 14:37:27 -0700 (Mon, 24 Aug 2009) $'
|
10
|
* '$Revision: 5031 $'
|
11
|
*
|
12
|
* This program is free software; you can redistribute it and/or modify
|
13
|
* it under the terms of the GNU General Public License as published by
|
14
|
* the Free Software Foundation; either version 2 of the License, or
|
15
|
* (at your option) any later version.
|
16
|
*
|
17
|
* This program is distributed in the hope that it will be useful,
|
18
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20
|
* GNU General Public License for more details.
|
21
|
*
|
22
|
* You should have received a copy of the GNU General Public License
|
23
|
* along with this program; if not, write to the Free Software
|
24
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25
|
*/
|
26
|
|
27
|
|
28
|
import java.io.*;
|
29
|
import java.sql.Connection;
|
30
|
import java.sql.ResultSet;
|
31
|
import java.sql.PreparedStatement;
|
32
|
import java.sql.Statement;
|
33
|
import java.sql.SQLException;
|
34
|
import java.sql.DriverManager;
|
35
|
|
36
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
37
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
38
|
|
39
|
public class upgrade_db_to_1_5{
|
40
|
|
41
|
static String database = null;
|
42
|
static String url = null;
|
43
|
static String user = null;
|
44
|
static String password = null;
|
45
|
static {
|
46
|
try {
|
47
|
database = PropertyService.getProperty("database.type");
|
48
|
url = PropertyService.getProperty("database.connectionURI");
|
49
|
user = PropertyService.getProperty("database.user");
|
50
|
password = PropertyService.getProperty("database.password");
|
51
|
} catch (PropertyNotFoundException pnfe) {
|
52
|
System.err.println("Error in upgrade_db_to_1_5 static block:"
|
53
|
+ pnfe.getMessage());
|
54
|
pnfe.printStackTrace();
|
55
|
}
|
56
|
}
|
57
|
|
58
|
public static void upgrade_xml_nodes(Connection sqlca) throws SQLException{
|
59
|
Statement sqlStatement = null;
|
60
|
PreparedStatement pstmt = null;
|
61
|
ResultSet rset = null;
|
62
|
|
63
|
// Delete old nodedatanumerical column from xml_nodes if one exsists
|
64
|
try {
|
65
|
System.out.println(
|
66
|
"Deleting old nodedatanumerical column from xml_nodes...");
|
67
|
|
68
|
pstmt = sqlca.prepareStatement(
|
69
|
"ALTER TABLE xml_nodes DROP COLUMN nodedatanumerical");
|
70
|
pstmt.execute();
|
71
|
pstmt.close();
|
72
|
|
73
|
System.out.println("Done.");
|
74
|
} catch (Exception e) {
|
75
|
System.out.println(" column not found.");
|
76
|
}
|
77
|
|
78
|
// Create nodedatanumerical column in xml_nodes
|
79
|
System.out.println(
|
80
|
"Creating new nodedatanumerical column in xml_nodes...");
|
81
|
|
82
|
if (database.equals("oracle")) {
|
83
|
pstmt = sqlca.prepareStatement(
|
84
|
"ALTER TABLE xml_nodes ADD nodedatanumerical NUMBER");
|
85
|
}
|
86
|
else {
|
87
|
pstmt = sqlca.prepareStatement(
|
88
|
"ALTER TABLE xml_nodes ADD nodedatanumerical FLOAT8");
|
89
|
}
|
90
|
pstmt.execute();
|
91
|
pstmt.close();
|
92
|
|
93
|
System.out.println("Done.");
|
94
|
|
95
|
// Copy numerical values from nodedata to
|
96
|
// nodedatanumerical column in xml_nodes
|
97
|
System.out.println(
|
98
|
"Please be patient as the next upgrade step can be extremely time consuming.");
|
99
|
System.out.println(
|
100
|
"Copy numerical values from nodedata to nodedatanumerical "
|
101
|
+ "in xml_nodes...");
|
102
|
|
103
|
sqlStatement = sqlca.createStatement();
|
104
|
rset = sqlStatement.executeQuery(
|
105
|
"SELECT DISTINCT NODEID, NODEDATA "
|
106
|
+ "FROM xml_nodes WHERE "
|
107
|
+ "nodedata IS NOT NULL AND "
|
108
|
+ "UPPER(nodedata) = LOWER(nodedata)");
|
109
|
|
110
|
int count = 0;
|
111
|
while (rset.next()) {
|
112
|
|
113
|
String nodeid = rset.getString(1);
|
114
|
String nodedata = rset.getString(2);
|
115
|
|
116
|
try {
|
117
|
if (!nodedata.trim().equals("")) {
|
118
|
double s = Double.parseDouble(nodedata);
|
119
|
|
120
|
pstmt = sqlca.prepareStatement(
|
121
|
"update xml_nodes set nodedatanumerical = " + s +
|
122
|
" where nodeid=" + nodeid);
|
123
|
pstmt.execute();
|
124
|
pstmt.close();
|
125
|
|
126
|
count++;
|
127
|
if (count%5 == 0) {
|
128
|
System.out.println(count + "...");
|
129
|
}
|
130
|
}
|
131
|
} catch (NumberFormatException nfe) {
|
132
|
|
133
|
} catch (Exception e) {
|
134
|
System.out.println("Exception:" + e.getMessage());
|
135
|
}
|
136
|
}
|
137
|
System.out.println("\nDone. " + count + " values copied.");
|
138
|
|
139
|
rset.close();
|
140
|
sqlStatement.close();
|
141
|
}
|
142
|
|
143
|
|
144
|
public static void main(String [] ags){
|
145
|
|
146
|
try {
|
147
|
Connection sqlca = null;
|
148
|
|
149
|
// Create a JDBC connection to the database
|
150
|
if (database.equals("oracle")) {
|
151
|
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
|
152
|
}
|
153
|
else if (database.equals("postgresql")) {
|
154
|
DriverManager.registerDriver(new org.postgresql.Driver());
|
155
|
}
|
156
|
else if (database.equals("sqlserver")) {
|
157
|
DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
|
158
|
}
|
159
|
|
160
|
sqlca = DriverManager.getConnection(url, user, password);
|
161
|
|
162
|
// Upgrade the xml_nodes table only if it oracle
|
163
|
if (database.equals("oracle")) {
|
164
|
upgrade_xml_nodes(sqlca);
|
165
|
}
|
166
|
// Close the connection...
|
167
|
sqlca.close();
|
168
|
} catch (SQLException ex) {
|
169
|
System.out.println("SQLException:" + ex.getMessage());
|
170
|
}
|
171
|
}
|
172
|
}
|