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: daigle $'
9
 *     '$Date: 2008-04-02 16:28:31 -0700 (Wed, 02 Apr 2008) $'
10
 * '$Revision: 3780 $'
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
import edu.ucsb.nceas.metacat.MetaCatUtil;
37

    
38
public class upgrade_db_to_1_5{
39

    
40
    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

    
45
    public static void upgrade_xml_nodes(Connection sqlca) throws SQLException{
46
        Statement sqlStatement = null;
47
        PreparedStatement pstmt = null;
48
        ResultSet rset = null;
49

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

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

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

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

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

    
126
        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
            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
            sqlca = DriverManager.getConnection(url, user, password);
148

    
149
            // Upgrade the xml_nodes table only if it oracle
150
	    if (database.equals("oracle")) {
151
            	upgrade_xml_nodes(sqlca);
152
	    }
153
            // Close the connection...
154
            sqlca.close();
155
        } catch (SQLException ex) {
156
            System.out.println("SQLException:" + ex.getMessage());
157
        }
158
    }
159
}
(33-33/37)