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
 *    Release: @release@
8
 *
9
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27
28
29
import java.io.*;
30 2378 sgarg
import java.sql.Connection;
31
import java.sql.ResultSet;
32
import java.sql.PreparedStatement;
33
import java.sql.Statement;
34
import java.sql.SQLException;
35
import java.sql.DriverManager;
36 2354 sgarg
37
public class upgrade_db_to_1_5{
38 2378 sgarg
39
    static String database = "@database@";
40
    static String url = "@jdbc-connect@";
41
    static String user = "@user@";
42
    static String password = "@password@";
43
44
    public static void upgrade_xml_nodes(Connection sqlca) throws SQLException{
45 2355 sgarg
        Statement sqlStatement = null;
46
        PreparedStatement pstmt = null;
47 2354 sgarg
        ResultSet rset = null;
48 2355 sgarg
49 2378 sgarg
        // Delete old nodedatanumerical column from xml_nodes if one exsists
50
        try {
51
            System.out.println(
52
                "Deleting old nodedatanumerical column from xml_nodes...");
53 2354 sgarg
54 2378 sgarg
            pstmt = sqlca.prepareStatement(
55
                "ALTER TABLE xml_nodes DROP COLUMN nodedatanumerical");
56
            pstmt.execute();
57
            pstmt.close();
58
59
            System.out.println("Done.");
60
        } catch (Exception e) {
61
            System.out.println(" column not found.");
62
        }
63
64
        // Create nodedatanumerical column in xml_nodes
65
        System.out.println(
66
            "Creating new nodedatanumerical column in xml_nodes...");
67
68
        if (database.equals("oracle")) {
69
            pstmt = sqlca.prepareStatement(
70
                "ALTER TABLE xml_nodes ADD nodedatanumerical NUMBER");
71
        }
72
        else {
73
            pstmt = sqlca.prepareStatement(
74
                "ALTER TABLE xml_nodes ADD nodedatanumerical FLOAT8");
75
        }
76
        pstmt.execute();
77
        pstmt.close();
78
79
        System.out.println("Done.");
80
81
        // Copy numerical values from nodedata to
82
        // nodedatanumerical column in xml_nodes
83
        System.out.println(
84 2484 jones
            "Please be patient as the next upgrade step can be extremely time consuming.");
85
        System.out.println(
86 2378 sgarg
            "Copy numerical values from nodedata to nodedatanumerical "
87
            + "in xml_nodes...");
88
89
        sqlStatement = sqlca.createStatement();
90
        rset = sqlStatement.executeQuery(
91
            "SELECT DISTINCT NODEID, NODEDATA "
92
            + "FROM xml_nodes WHERE "
93
            + "nodedata IS NOT NULL AND "
94
            + "UPPER(nodedata) = LOWER(nodedata)");
95
96
        int count = 0;
97
        while (rset.next()) {
98
99
            String nodeid = rset.getString(1);
100
            String nodedata = rset.getString(2);
101
102 2354 sgarg
            try {
103 2378 sgarg
                if (!nodedata.trim().equals("")) {
104
                    double s = Double.parseDouble(nodedata);
105 2354 sgarg
106 2378 sgarg
                    pstmt = sqlca.prepareStatement(
107
                        "update xml_nodes set nodedatanumerical = " + s +
108
                        " where nodeid=" + nodeid);
109
                    pstmt.execute();
110
                    pstmt.close();
111 2354 sgarg
112 2378 sgarg
                    count++;
113 2484 jones
                    if (count%5 == 0) {
114 2504 sgarg
                        System.out.println(count + "...");
115 2484 jones
                    }
116 2378 sgarg
                }
117
            } catch (NumberFormatException nfe) {
118
119
            } catch (Exception e) {
120
                System.out.println("Exception:" + e.getMessage());
121 2354 sgarg
            }
122 2378 sgarg
        }
123 2484 jones
        System.out.println("\nDone. " + count + " values copied.");
124 2354 sgarg
125 2378 sgarg
        rset.close();
126
        sqlStatement.close();
127
    }
128
129
130
    public static void main(String [] ags){
131
132
        try {
133
            Connection sqlca = null;
134
135
            // Create a JDBC connection to the database
136
            DriverManager.registerDriver(new @dbDriver@());
137
            sqlca = DriverManager.getConnection(url, user, password);
138
139 2504 sgarg
            // Upgrade the xml_nodes table only if it oracle
140
	    if (database.equals("oracle")) {
141
            	upgrade_xml_nodes(sqlca);
142
	    }
143 2378 sgarg
            // Close the connection...
144 2354 sgarg
            sqlca.close();
145 2378 sgarg
        } catch (SQLException ex) {
146 2354 sgarg
            System.out.println("SQLException:" + ex.getMessage());
147
        }
148
    }
149
}