001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.tools.dbloader;
007:
008: import java.io.BufferedWriter;
009: import java.io.File;
010: import java.io.FileWriter;
011: import java.io.PrintWriter;
012: import java.sql.Connection;
013: import java.sql.ResultSet;
014: import java.sql.ResultSetMetaData;
015: import java.sql.SQLException;
016: import java.sql.Statement;
017:
018: import org.jasig.portal.RDBMServices;
019: import org.jasig.portal.utils.XMLEscaper;
020: import org.springframework.dao.DataAccessException;
021:
022: /**
023: * Title: DbUnload
024: * Description: Dump database table(s) into a xml format
025: * @author George Lindholm
026: * @version $Revision: 36814 $
027: */
028:
029: public class DbUnload {
030: static void dumpTable(PrintWriter xmlOut, Statement stmt,
031: String tableName) throws Exception {
032: String sql = "SELECT * FROM " + tableName;
033: ResultSet rs;
034: try {
035: rs = stmt.executeQuery(sql);
036: } catch (SQLException e) {
037: System.err.println("Problem accessing table " + tableName
038: + ": " + e);
039: return;
040: }
041:
042: xmlOut.println(" <table>");
043: xmlOut.println(" <name>" + tableName.toUpperCase()
044: + "</name>");
045: xmlOut.println(" <rows>");
046: try {
047: ResultSetMetaData rsmd = rs.getMetaData();
048: int columnCount = rsmd.getColumnCount();
049: int[] columnType = new int[columnCount];
050: String[] columnName = new String[columnCount];
051: for (int i = 0; i < columnCount; i++) {
052: columnType[i] = rsmd.getColumnType(i + 1);
053: columnName[i] = rsmd.getColumnName(i + 1);
054: }
055: while (rs.next()) {
056: xmlOut.println(" <row>");
057: for (int i = 0; i < rsmd.getColumnCount(); i++) {
058: String value = "";
059: if (columnType[i] == java.sql.Types.VARCHAR
060: || columnType[i] == java.sql.Types.LONGVARCHAR
061: || columnType[i] == java.sql.Types.CHAR) {
062: value = rs.getString(i + 1);
063: value = XMLEscaper.escape(value);
064: if (value != null && value.startsWith("<?xml ")) {
065: value = "<![CDATA[\n" + value + "\n]]>";
066: }
067: } else if (columnType[i] == java.sql.Types.NUMERIC
068: || columnType[i] == java.sql.Types.INTEGER) {
069: value = rs.getInt(i + 1) + "";
070: } else if (columnType[i] == java.sql.Types.BIGINT) {
071: value = rs.getLong(i + 1) + "";
072: } else if (columnType[i] == java.sql.Types.TIMESTAMP) {
073: java.sql.Timestamp ts = rs.getTimestamp(i + 1);
074: if (!rs.wasNull()) {
075: value = ts.toString();
076: }
077: } else if (columnType[i] == java.sql.Types.DATE) {
078: java.sql.Date dt = rs.getDate(i + 1);
079: if (!rs.wasNull()) {
080: java.sql.Timestamp ts = new java.sql.Timestamp(
081: dt.getTime());
082: value = ts.toString();
083: }
084: } else {
085: throw new Exception("Unrecognized column type "
086: + columnType[i] + " for column "
087: + (i + 1) + " in table " + tableName);
088: }
089: if (rs.wasNull())
090: xmlOut.println(" <column><name>"
091: + columnName[i].toUpperCase()
092: + "</name></column>");
093: else
094: xmlOut.println(" <column><name>"
095: + columnName[i].toUpperCase()
096: + "</name><value>" + value
097: + "</value></column>");
098: }
099: xmlOut.println(" </row>");
100: }
101: } finally {
102: rs.close();
103: }
104: xmlOut.println(" </rows>");
105: xmlOut.println(" </table>");
106: xmlOut.println();
107: }
108:
109: public static void main(String[] args) {
110: if (args.length < 2) {
111: System.err
112: .println("Usage \"dbunload <table>... <out xmlfile>\"");
113: return;
114: }
115: Connection con = null;
116: try {
117: PrintWriter xmlOut;
118:
119: if (!args[args.length - 1].equals("-")) {
120: File xmlFile = new File(args[args.length - 1]);
121: xmlFile.createNewFile();
122:
123: xmlOut = new PrintWriter(new BufferedWriter(
124: new FileWriter(args[args.length - 1], true)));
125: } else { // stdout
126: xmlOut = new PrintWriter(System.out);
127: }
128:
129: RDBMServices.setGetDatasourceFromJndi(false); /*don't try jndi when not in web app */
130: con = RDBMServices.getConnection();
131:
132: Statement stmt = con.createStatement();
133: xmlOut.println("<?xml version=\"1.0\"?>");
134: xmlOut.println();
135: xmlOut.println("<data>");
136: try {
137: for (int i = 0; i < args.length - 1; i++) {
138: dumpTable(xmlOut, stmt, args[i].toUpperCase());
139: }
140: } finally {
141: stmt.close();
142: }
143: xmlOut.println("</data>");
144: xmlOut.close();
145: } catch (DataAccessException dae) {
146: // we know this was thrown by RDBMServices.getConnection()
147: if (con == null) {
148: System.err
149: .println("Unable to get a database connection");
150: return;
151: }
152: } catch (Exception e) {
153: e.printStackTrace();
154: System.exit(1);
155: } finally {
156: try {
157: RDBMServices.releaseConnection(con);
158: } catch (Exception e) {
159: }
160: }
161: }
162: }
|