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
public class upgrade_db_to_1_5{
37 2378 sgarg
38
    static String database = "@database@";
39
    static String url = "@jdbc-connect@";
40
    static String user = "@user@";
41
    static String password = "@password@";
42
43
    public static void upgrade_xml_nodes(Connection sqlca) throws SQLException{
44 2355 sgarg
        Statement sqlStatement = null;
45
        PreparedStatement pstmt = null;
46 2354 sgarg
        ResultSet rset = null;
47 2355 sgarg
48 2378 sgarg
        // Delete old nodedatanumerical column from xml_nodes if one exsists
49
        try {
50
            System.out.println(
51
                "Deleting old nodedatanumerical column from xml_nodes...");
52 2354 sgarg
53 2378 sgarg
            pstmt = sqlca.prepareStatement(
54
                "ALTER TABLE xml_nodes DROP COLUMN nodedatanumerical");
55
            pstmt.execute();
56
            pstmt.close();
57
58
            System.out.println("Done.");
59
        } catch (Exception e) {
60
            System.out.println(" column not found.");
61
        }
62
63
        // Create nodedatanumerical column in xml_nodes
64
        System.out.println(
65
            "Creating new nodedatanumerical column in xml_nodes...");
66
67
        if (database.equals("oracle")) {
68
            pstmt = sqlca.prepareStatement(
69
                "ALTER TABLE xml_nodes ADD nodedatanumerical NUMBER");
70
        }
71
        else {
72
            pstmt = sqlca.prepareStatement(
73
                "ALTER TABLE xml_nodes ADD nodedatanumerical FLOAT8");
74
        }
75
        pstmt.execute();
76
        pstmt.close();
77
78
        System.out.println("Done.");
79
80
        // Copy numerical values from nodedata to
81
        // nodedatanumerical column in xml_nodes
82
        System.out.println(
83 2484 jones
            "Please be patient as the next upgrade step can be extremely time consuming.");
84
        System.out.println(
85 2378 sgarg
            "Copy numerical values from nodedata to nodedatanumerical "
86
            + "in xml_nodes...");
87
88
        sqlStatement = sqlca.createStatement();
89
        rset = sqlStatement.executeQuery(
90
            "SELECT DISTINCT NODEID, NODEDATA "
91
            + "FROM xml_nodes WHERE "
92
            + "nodedata IS NOT NULL AND "
93
            + "UPPER(nodedata) = LOWER(nodedata)");
94
95
        int count = 0;
96
        while (rset.next()) {
97
98
            String nodeid = rset.getString(1);
99
            String nodedata = rset.getString(2);
100
101 2354 sgarg
            try {
102 2378 sgarg
                if (!nodedata.trim().equals("")) {
103
                    double s = Double.parseDouble(nodedata);
104 2354 sgarg
105 2378 sgarg
                    pstmt = sqlca.prepareStatement(
106
                        "update xml_nodes set nodedatanumerical = " + s +
107
                        " where nodeid=" + nodeid);
108
                    pstmt.execute();
109
                    pstmt.close();
110 2354 sgarg
111 2378 sgarg
                    count++;
112 2484 jones
                    if (count%5 == 0) {
113 2504 sgarg
                        System.out.println(count + "...");
114 2484 jones
                    }
115 2378 sgarg
                }
116
            } catch (NumberFormatException nfe) {
117
118
            } catch (Exception e) {
119
                System.out.println("Exception:" + e.getMessage());
120 2354 sgarg
            }
121 2378 sgarg
        }
122 2484 jones
        System.out.println("\nDone. " + count + " values copied.");
123 2354 sgarg
124 2378 sgarg
        rset.close();
125
        sqlStatement.close();
126
    }
127
128
129
    public static void main(String [] ags){
130
131
        try {
132
            Connection sqlca = null;
133
134
            // Create a JDBC connection to the database
135
            DriverManager.registerDriver(new @dbDriver@());
136
            sqlca = DriverManager.getConnection(url, user, password);
137
138 2504 sgarg
            // Upgrade the xml_nodes table only if it oracle
139
	    if (database.equals("oracle")) {
140
            	upgrade_xml_nodes(sqlca);
141
	    }
142 2378 sgarg
            // Close the connection...
143 2354 sgarg
            sqlca.close();
144 2378 sgarg
        } catch (SQLException ex) {
145 2354 sgarg
            System.out.println("SQLException:" + ex.getMessage());
146
        }
147
    }
148
}