Project

General

Profile

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: jones $'
9
 *     '$Date: 2006-11-10 10:25:38 -0800 (Fri, 10 Nov 2006) $'
10
 * '$Revision: 3077 $'
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
public class upgrade_db_to_1_5{
37

    
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
        Statement sqlStatement = null;
45
        PreparedStatement pstmt = null;
46
        ResultSet rset = null;
47

    
48
        // 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

    
53
            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
            "Please be patient as the next upgrade step can be extremely time consuming.");
84
        System.out.println(
85
            "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
            try {
102
                if (!nodedata.trim().equals("")) {
103
                    double s = Double.parseDouble(nodedata);
104

    
105
                    pstmt = sqlca.prepareStatement(
106
                        "update xml_nodes set nodedatanumerical = " + s +
107
                        " where nodeid=" + nodeid);
108
                    pstmt.execute();
109
                    pstmt.close();
110

    
111
                    count++;
112
                    if (count%5 == 0) {
113
                        System.out.println(count + "...");
114
                    }
115
                }
116
            } catch (NumberFormatException nfe) {
117

    
118
            } catch (Exception e) {
119
                System.out.println("Exception:" + e.getMessage());
120
            }
121
        }
122
        System.out.println("\nDone. " + count + " values copied.");
123

    
124
        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
            // Upgrade the xml_nodes table only if it oracle
139
	    if (database.equals("oracle")) {
140
            	upgrade_xml_nodes(sqlca);
141
	    }
142
            // Close the connection...
143
            sqlca.close();
144
        } catch (SQLException ex) {
145
            System.out.println("SQLException:" + ex.getMessage());
146
        }
147
    }
148
}
(29-29/33)