Project

General

Profile

1 2354 sgarg
/**
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$'
9
 *     '$Date$'
10
 * '$Revision$'
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 2378 sgarg
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 2354 sgarg
36 5031 daigle
import edu.ucsb.nceas.metacat.properties.PropertyService;
37 4080 daigle
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
38 3780 daigle
39 2354 sgarg
public class upgrade_db_to_1_5{
40 2378 sgarg
41 4080 daigle
    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 2378 sgarg
58
    public static void upgrade_xml_nodes(Connection sqlca) throws SQLException{
59 2355 sgarg
        Statement sqlStatement = null;
60
        PreparedStatement pstmt = null;
61 2354 sgarg
        ResultSet rset = null;
62 2355 sgarg
63 2378 sgarg
        // 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 2354 sgarg
68 2378 sgarg
            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 2484 jones
            "Please be patient as the next upgrade step can be extremely time consuming.");
99
        System.out.println(
100 2378 sgarg
            "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 2354 sgarg
            try {
117 2378 sgarg
                if (!nodedata.trim().equals("")) {
118
                    double s = Double.parseDouble(nodedata);
119 2354 sgarg
120 2378 sgarg
                    pstmt = sqlca.prepareStatement(
121
                        "update xml_nodes set nodedatanumerical = " + s +
122
                        " where nodeid=" + nodeid);
123
                    pstmt.execute();
124
                    pstmt.close();
125 2354 sgarg
126 2378 sgarg
                    count++;
127 2484 jones
                    if (count%5 == 0) {
128 2504 sgarg
                        System.out.println(count + "...");
129 2484 jones
                    }
130 2378 sgarg
                }
131
            } catch (NumberFormatException nfe) {
132
133
            } catch (Exception e) {
134
                System.out.println("Exception:" + e.getMessage());
135 2354 sgarg
            }
136 2378 sgarg
        }
137 2484 jones
        System.out.println("\nDone. " + count + " values copied.");
138 2354 sgarg
139 2378 sgarg
        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 4080 daigle
            if (database.equals("oracle")) {
151 3780 daigle
            	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 2378 sgarg
            sqlca = DriverManager.getConnection(url, user, password);
161
162 2504 sgarg
            // Upgrade the xml_nodes table only if it oracle
163
	    if (database.equals("oracle")) {
164
            	upgrade_xml_nodes(sqlca);
165
	    }
166 2378 sgarg
            // Close the connection...
167 2354 sgarg
            sqlca.close();
168 2378 sgarg
        } catch (SQLException ex) {
169 2354 sgarg
            System.out.println("SQLException:" + ex.getMessage());
170
        }
171
    }
172
}