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