User: mulder  
  Date: 00/09/07 06:54:18

  Modified:    src/main/org/jboss/ejb/plugins/jaws/jdbc JDBCCommand.java
                        JDBCInitCommand.java
  Log:
  Removed a hardcoded reference to Hypersonic
  
  Gracefully handle when a table already exists and you go to create it
  (identified by SELECT COUNT(*) FROM TABLENAME WHERE 0=1)
  
  Revision  Changes    Path
  1.10      +3 -7      jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCCommand.java
  
  Index: JDBCCommand.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCCommand.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- JDBCCommand.java  2000/09/07 12:40:08     1.9
  +++ JDBCCommand.java  2000/09/07 13:54:17     1.10
  @@ -52,7 +52,7 @@
    * utility methods that database commands may need to call.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Justin Forder</a>
  - * @version $Revision: 1.9 $
  + * @version $Revision: 1.10 $
    */
   public abstract class JDBCCommand
   {
  @@ -609,17 +609,13 @@
      // Private -------------------------------------------------------
   
      /** Get a database connection */
  -   private Connection getConnection() throws SQLException
  +   protected Connection getConnection() throws SQLException
      {
         DataSource ds = metaInfo.getDataSource();
         if (ds != null)
         {
            return ds.getConnection();
  -      } else
  -      {
  -         String url = metaInfo.getDbURL();
  -         return DriverManager.getConnection(url,"sa","");
  -      }
  +      } else throw new RuntimeException("Unable to locate data source!");
      }
   
      private final void setUpJDBCTypeNames()
  
  
  
  1.4       +52 -21    
jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCInitCommand.java
  
  Index: JDBCInitCommand.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCInitCommand.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- JDBCInitCommand.java      2000/08/24 10:56:36     1.3
  +++ JDBCInitCommand.java      2000/09/07 13:54:17     1.4
  @@ -9,8 +9,11 @@
   
   import java.util.Iterator;
   
  +import java.sql.Connection;
   import java.sql.PreparedStatement;
  +import java.sql.ResultSet;
   import java.sql.SQLException;
  +import java.sql.Statement;
   
   import org.jboss.ejb.plugins.jaws.JPMInitCommand;
   import org.jboss.ejb.plugins.jaws.CMPFieldInfo;
  @@ -25,31 +28,31 @@
    * @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Joe Shevland</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Justin Forder</a>
  - * @version $Revision: 1.3 $
  + * @version $Revision: 1.4 $
    */
   public class JDBCInitCommand
      extends JDBCUpdateCommand
      implements JPMInitCommand
   {
      // Constructors --------------------------------------------------
  -   
  +
      public JDBCInitCommand(JDBCCommandFactory factory)
      {
         super(factory, "Init");
  -      
  +
         // Create table SQL
         String sql = "CREATE TABLE " + metaInfo.getTableName() + " (";
  -      
  +
         Iterator it = metaInfo.getCMPFieldInfos();
         boolean first = true;
         while (it.hasNext())
         {
            CMPFieldInfo fieldInfo = (CMPFieldInfo)it.next();
  -         
  +
            if (fieldInfo.isEJBReference())
            {
               JawsCMPField[] pkFields = fieldInfo.getForeignKeyCMPFields();
  -            
  +
               for (int i = 0; i < pkFields.length; i++)
               {
                  sql += (first ? "" : ",") +
  @@ -66,38 +69,66 @@
               first = false;
            }
         }
  -      
  +
         sql += ")";
  -      
  +
         setSQL(sql);
      }
  -   
  +
      // JPMInitCommand implementation ---------------------------------
  -   
  +
      public void execute() throws Exception
      {
         // Create table if necessary
         if (metaInfo.getCreateTable())
         {
  +          boolean created = false;
  +          Connection con = null;
  +          Statement st = null;
  +          ResultSet rs = null;
  +          try {
  +              con = getConnection();
  +              st = con.createStatement();
  +              rs = st.executeQuery("SELECT COUNT(*) FROM 
"+metaInfo.getTableName()+" WHERE 0=1");
  +              if(rs.next())
  +                created = true;
  +              rs.close();
  +              rs = null;
  +              st.close();
  +              st = null;
  +              con.close();
  +              con = null;
  +          } catch(SQLException e) {
  +              created = false;
  +          } finally {
  +              if(rs != null) try {rs.close();}catch(SQLException e) {}
  +              if(st != null) try {st.close();}catch(SQLException e) {}
  +              if(con != null) try {con.close();}catch(SQLException e) {}
  +          }
  +
            // Try to create it
  -         try
  -         {
  -            jdbcExecute(null);
  -         } catch (Exception e)
  -         {
  -            log.debug("Could not create table " + 
  -                      metaInfo.getTableName() + ": " + e.getMessage());
  +         if(created) {
  +             System.out.println("Table '"+metaInfo.getTableName()+"' already 
exists!");
  +         } else {
  +             try
  +             {
  +                jdbcExecute(null);
  +             } catch (Exception e)
  +             {
  +                log.debug("Could not create table " +
  +                          metaInfo.getTableName() + ": " + e.getMessage());
  +             }
            }
         }
      }
  -   
  +
      // JDBCUpdateCommand overrides -----------------------------------
  -   
  -   protected Object handleResult(int rowsAffected, Object argOrArgs) 
  +
  +   protected Object handleResult(int rowsAffected, Object argOrArgs)
         throws Exception
      {
         log.debug("Table " + metaInfo.getTableName() + " created");
  -      
  +
         return null;
      }
   }
  
  
  

Reply via email to