View Javadoc
1 /* 2 * PROJECT : DAR Runtime and Tools 3 * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org 4 * LICENSE : GNU LESSER GENERAL PUBLIC LICENSE 5 * Version 2.1, February 1999 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 package org.enableit.db.darrt; 22 23 import java.sql.Connection; 24 import java.sql.DatabaseMetaData; 25 import java.util.ArrayList; 26 import java.util.List; 27 import java.util.TreeMap; 28 29 import org.apache.log4j.Logger; 30 import org.enableit.db.ConnFactory; 31 import org.enableit.db.DBUtils; 32 import org.enableit.db.DatabaseProxy; 33 import org.enableit.db.beans.Provider; 34 35 36 /*** 37 * Class to extract a VIEW's definition from a database. 38 */ 39 public class ViewExporter { 40 /*** 41 * Query to return a VIEW definition from an Oracle database. 42 */ 43 public static final String ORACLE_QUERY = "SELECT text definition FROM all_views WHERE view_name = ?"; 44 45 /*** 46 * Query to return a VIEW definition from an Sybase database. 47 */ 48 public static final String SYBASE_QUERY = "SELECT viewtext definition FROM sysviews WHERE viewname = ?"; 49 50 /*** 51 * The Log4J <code>Logger</code> doing the logging. 52 */ 53 protected static Logger logger = Logger.getLogger(ViewExporter.class); 54 55 /*** 56 * CVS info ABOUT this class and its current version 57 */ 58 public static final String ABOUT = "$Id: ViewExporter.java,v 1.10 2004/03/19 19:30:10 tim Exp $"; 59 60 /* 61 * Properties 62 */ 63 64 /*** 65 * Defines the database connection properties. 66 */ 67 private Provider provider; 68 69 /*** 70 * The SQL query to obtain a view's definition from the database 71 * proprietary metadata. 72 */ 73 private String query; 74 75 /* 76 * Constructors 77 */ 78 79 /*** 80 * Default Constructor 81 */ 82 public ViewExporter() { 83 } 84 85 /* 86 * Methods 87 */ 88 89 /*** 90 * Sets the details of the target database. 91 */ 92 public void setProvider(Provider provider) { 93 this.provider = provider; 94 } 95 96 /*** 97 * Export the definition of the named view. 98 * 99 * @return defn The VIEW definition or an empty string if it could not be found. 100 */ 101 public String export(String viewName) { 102 String defn = null; 103 104 try { 105 if (provider == null) { 106 logger.debug("No connection to export view."); 107 } else { 108 logger.debug( 109 "Attempting connection from the provider configuration"); 110 111 Connection conn = ConnFactory.getConnection(provider); 112 113 defn = export(conn, viewName); 114 } 115 } catch (Exception e) { 116 logger.error(e.getClass().getName() + ":" + e.getMessage()); 117 } 118 119 defn = ((defn == null) ? new String() : defn); 120 121 return defn; 122 } 123 124 /*** 125 * Export the definition of the named view. 126 * 127 * @return defn The VIEW definition or an empty string if it could not be found. 128 */ 129 public String export(Connection conn, String viewName) { 130 logger.info("METHOD_ENTRY: export, viewName=" + viewName); 131 132 String defn = null; 133 134 try { 135 ArrayList parms = new ArrayList(); 136 137 parms.add(viewName); 138 139 // set the query 140 DatabaseMetaData dmd = conn.getMetaData(); 141 142 switch (DBUtils.getDatabaseProduct(dmd)) { 143 case DBUtils.DB_SYBASE_ASA: 144 query = SYBASE_QUERY; 145 146 break; 147 case DBUtils.DB_SYBASE_ASE: 148 query = SYBASE_QUERY; 149 150 break; 151 case DBUtils.DB_ORACLE: 152 query = ORACLE_QUERY; 153 154 break; 155 default: 156 157 //throw new DBException("Unknown database type, cannot extract View definition.") ; 158 logger.warn( 159 "Unknown database type, cannot extract View definition."); 160 } 161 162 if (query != null) { 163 // Give it a whirl ... 164 List results = DatabaseProxy.executeQuery(conn, query, parms); 165 166 if (results.size() > 0) { 167 if (results.size() > 1) { 168 logger.warn( 169 "More than one view definition found!, view name=" 170 + viewName); 171 } 172 173 TreeMap row = (TreeMap) results.get(0); 174 175 defn = (String) row.get("definition"); 176 177 if (defn == null) { 178 defn = (String) row.get("DEFINITION"); 179 } 180 } 181 } 182 } catch (Exception e) { 183 logger.error(e.getClass().getName() + ":" + e.getMessage()); 184 } 185 186 defn = ((defn == null) ? new String() : defn); 187 188 logger.info("METHOD_EXIT: export"); 189 190 return defn; 191 } 192 }

This page was automatically generated by Maven