Clover coverage report - Replica - 1.0-Alpha
Coverage timestamp: Dom Fev 1 2004 17:00:58 WET
file stats: LOC: 335   Methods: 17
NCLOC: 269   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
DBConnection.java 0% 0% 0% 0%
coverage
 1   
 package nmp.dbms.JDBC;
 2   
 
 3   
 import java.io.*;
 4   
 import java.sql.*;
 5   
 
 6   
 /**
 7   
  * REpresents database connection.<BR>
 8   
  * The following nativeSQL modifications are supported:
 9   
  * <UL>
 10   
  *   <LI>Oracle
 11   
  *     <OL>
 12   
  *       <LI>; removed from statements (if present)
 13   
  *       <LI>COUNTER datatype changed to INTEGER and trigger inserted in create table statements (in executeUpdate)
 14   
  *       <LI>converts LONGBINARY in LONG RAW in create table statements (in executeUpdate)
 15   
  *     </OL>
 16   
  *   <LI>MSAccess
 17   
  *     <OL>
 18   
  *       <LI>; inserted in statements (if not present)
 19   
  *     </OL>
 20   
  *   <LI>HypersonicSQL
 21   
  *     <OL>
 22   
  *       <LI>; removed from statements (if present)
 23   
  *       <LI>removes constraint in create table statements (in executeUpdate)
 24   
  *       <LI>COUNTER datatype changed to INTEGER IDENTITY  in create table statements (in executeUpdate)
 25   
  *     </OL>
 26   
  * </UL>
 27   
  */
 28   
 public class DBConnection
 29   
 {
 30   
     Connection con;
 31   
     Statement stmt;
 32   
     boolean debug;
 33   
     boolean toNative;
 34   
     PrintWriter logWriter;
 35   
     String dbproduct;        // lower case database product name
 36   
 
 37  0
     public DBConnection( String driver, String url, String user, String passwd,
 38   
                         boolean debug, PrintWriter logWriter)
 39   
                     throws SQLException {
 40  0
         this( driver, url, user, passwd, debug, logWriter, true);
 41   
     }
 42   
 
 43  0
     public DBConnection( String driver, String url, String user, String passwd,
 44   
                         boolean debug, PrintWriter logWriter, boolean toNative)
 45   
                     throws SQLException {
 46  0
         this.toNative = toNative;
 47  0
         dbproduct = "unknown";
 48  0
         this.debug = debug;
 49  0
         try {
 50  0
             con = null;
 51  0
             this.logWriter = logWriter;
 52  0
             Class.forName( driver);
 53  0
             if( logWriter != null)
 54  0
                 try {
 55  0
                     DriverManager.setLogWriter( logWriter);
 56   
                 }catch( java.lang.NoSuchMethodError e) {
 57   
                     // do nothing - using personal java
 58   
                 }
 59  0
             openConnection( url, user, passwd);
 60   
         } catch( ClassNotFoundException th) {
 61  0
             throw new SQLException( "Driver not found");
 62   
         }
 63   
     }
 64   
     
 65  0
     protected void finalize() {
 66  0
         try {
 67  0
             closeConnection();
 68   
         } catch( Exception ex) {
 69   
             /* do nothing */
 70   
         }
 71   
     }
 72   
     
 73  0
     public void reset()
 74   
             throws SQLException {
 75  0
         if( stmt != null)
 76  0
             stmt.close();
 77  0
         stmt = con.createStatement();
 78   
     }
 79   
 
 80  0
     public void openConnection( String url, String user, String passwd)
 81   
             throws SQLException {
 82  0
         if( con != null)
 83  0
             closeConnection();
 84  0
         con = DriverManager.getConnection( url, user, passwd);
 85  0
         if( debug)
 86  0
             checkForWarning( con.getWarnings());
 87  0
         con.setAutoCommit( true);
 88   
 
 89  0
         stmt = con.createStatement();
 90   
 
 91  0
         DatabaseMetaData dma = con.getMetaData ();
 92  0
         dbproduct = dma.getDatabaseProductName().toLowerCase();
 93  0
         if( dbproduct == null)
 94  0
             dbproduct = "unknown";
 95   
 
 96  0
         if( debug) {
 97  0
             PrintWriter writer = DriverManager.getLogWriter();
 98   
     
 99  0
             writer.println("\nConnected to :" + dma.getURL());
 100  0
             writer.println("Driver       :" + dma.getDriverName());
 101  0
             writer.println("Version      :" + dma.getDriverVersion());
 102  0
             writer.println("Database     :" + dma.getDatabaseProductName());
 103  0
             writer.println("Version db   :" + dma.getDatabaseProductVersion());
 104  0
             writer.println("Max con      :" + Integer.toString( dma.getMaxConnections()));
 105  0
             writer.print("Trans level  :"); writer.println( dma.supportsTransactions());
 106  0
             writer.print("SQL1 level   :"); writer.println( dma.supportsANSI92EntryLevelSQL());
 107  0
             writer.print("SQL2 level   :"); writer.println( dma.supportsANSI92IntermediateSQL());
 108  0
             writer.print("SQL3 level   :"); writer.println( dma.supportsANSI92FullSQL());
 109  0
             writer.println("");
 110   
         }
 111   
     }
 112   
 
 113  0
     public void closeConnection()
 114   
             throws SQLException {
 115  0
         if( stmt != null)
 116  0
             stmt.close();
 117  0
         if( con != null)        
 118  0
             con.close();
 119   
     }
 120   
 
 121   
     /**
 122   
      * Returns nativeSQL statement
 123   
      */
 124  0
     public String nativeSQL( String command) throws SQLException {
 125  0
         return nativeSQL0( command)[0];
 126   
     }
 127   
 
 128   
     /**
 129   
      * Returns a sequence of equivalente nativeSQL statements.
 130   
      */
 131  0
     protected String []nativeSQL0( String command)
 132   
             throws SQLException {
 133  0
         String []arr = null;
 134  0
         command = command.trim();
 135  0
         if( toNative) {
 136  0
         String cmdlower = command.toLowerCase();
 137  0
         if( dbproduct.startsWith( "oracle")) {
 138  0
             if( command.endsWith( ";")) {
 139  0
                 command = command.substring( 0, command.length() - 1);
 140  0
                 cmdlower = command.toLowerCase();
 141   
             }
 142  0
             if( cmdlower.startsWith( "create table")) {
 143  0
                 int pos = cmdlower.indexOf( "longbinary");
 144  0
                 while( pos != -1) {
 145  0
                     command = command.substring( 0, pos) + "LONG RAW" + command.substring( pos + 10, command.length());
 146  0
                     cmdlower = command.toLowerCase();
 147  0
                     pos = cmdlower.indexOf( "longbinary");
 148   
                 }
 149  0
                 pos = cmdlower.indexOf( "counter");
 150  0
                 if( pos != -1) {
 151  0
                     int nxtpos = cmdlower.lastIndexOf( "counter");
 152  0
                     if( nxtpos == pos) {
 153  0
                         int i = pos - 1;
 154  0
                         while( i >= 0 && Character.isWhitespace( command.charAt( i)))
 155  0
                             i--;
 156  0
                         int endpos = i;
 157  0
                         while( i >= 0 && Character.isJavaIdentifierPart( command.charAt( i)))
 158  0
                             i--;
 159  0
                         int stpos = i + 1;
 160  0
                         String fldname = command.substring( stpos, endpos + 1);
 161  0
                         i = cmdlower.indexOf( "table");
 162  0
                         i = i + 5;
 163  0
                         while( i < command.length() && Character.isWhitespace( command.charAt( i)))
 164  0
                             i++;
 165  0
                         stpos = i;
 166  0
                         while( i < command.length() && Character.isJavaIdentifierPart( command.charAt( i)))
 167  0
                             i++;
 168  0
                         endpos = i - 1;
 169  0
                         String tblname = command.substring( stpos, endpos + 1);
 170  0
                         arr = new String[3];
 171  0
                         arr[1] = "create sequence seq_" + tblname + "_" + fldname;
 172  0
                         arr[2] = "create or replace trigger trg_" + tblname + "_" + fldname +
 173   
                                  " BEFORE insert ON " + tblname + " FOR EACH ROW " +
 174   
                                  " when (new." + fldname + " IS NULL)" +
 175   
                                  " declare val integer;" +
 176   
                                  " begin " +
 177   
                                  " select seq_" + tblname + "_" + fldname + ".nextval into val from dual; "+
 178   
                                  " :new." + fldname + " := val; " + 
 179   
                                  " end; ";
 180  0
                         command = command.substring( 0, pos) + "integer" + command.substring( pos + 7);
 181   
                     }
 182   
                 }
 183   
             }
 184  0
         } else if( dbproduct.startsWith( "access")) {
 185  0
             if( ! command.endsWith( ";")) {
 186  0
                 command = command + ";";
 187  0
                 cmdlower = command.toLowerCase();
 188   
             }
 189  0
         } else if( dbproduct.startsWith( "hypersonicsql") || dbproduct.startsWith( "hsql")) {
 190  0
             if( command.endsWith( ";")) {
 191  0
                 command = command.substring( 0, command.length() - 1);
 192  0
                 cmdlower = command.toLowerCase();
 193   
             }
 194  0
             if( cmdlower.startsWith( "create table")) {
 195  0
                 int pos = cmdlower.indexOf( "constraint");
 196  0
                 while( pos >= 0) {
 197  0
                     int i = pos + 10;
 198  0
                     while( i < command.length() && Character.isWhitespace( command.charAt( i)))
 199  0
                         i++;
 200  0
                     while( i < command.length() && Character.isJavaIdentifierPart( command.charAt( i)))
 201  0
                         i++;
 202  0
                     command = command.substring( 0, pos) + command.substring( i);
 203  0
                     cmdlower = command.toLowerCase();
 204  0
                     pos = cmdlower.indexOf( "constraint");
 205   
                 }
 206  0
                 pos = cmdlower.indexOf( "counter");
 207  0
                 while( pos != -1) {
 208  0
                     command = command.substring( 0, pos) + "INTEGER IDENTITY" + command.substring( pos + 7, command.length());
 209  0
                     cmdlower = command.toLowerCase();
 210  0
                     pos = cmdlower.indexOf( "counter");
 211   
                 }
 212  0
                 pos = cmdlower.indexOf( "longbinary");
 213  0
                 while( pos != -1) {
 214  0
                     command = command.substring( 0, pos) + "LONGVARBINARY" + command.substring( pos + 10, command.length());
 215  0
                     cmdlower = command.toLowerCase();
 216  0
                     pos = cmdlower.indexOf( "longbinary");
 217   
                 }
 218   
             }
 219   
         }
 220   
         }
 221  0
         if( arr == null)
 222  0
             arr = new String[1];
 223  0
         arr[0] = con.nativeSQL( command);
 224  0
         return arr;
 225   
     }
 226   
 
 227  0
     public int executeUpdate( String command, java.util.Vector values) throws SQLException{
 228  0
         PreparedStatement prepStmt = null;
 229  0
         try {
 230  0
             command = nativeSQL( command);
 231  0
             prepStmt = con.prepareStatement(command);
 232  0
             if( debug)
 233  0
                 DriverManager.getLogWriter().println( command);
 234  0
             for ( int i = 0; i < values.size(); i++)
 235  0
                 prepStmt.setObject( i+1, nmp.io.Utilities.serialize( values.elementAt( i)), java.sql.Types.LONGVARBINARY);
 236   
 //                prepStmt.setBytes( i+1, nmp.io.Utilities.serialize( values.elementAt( i)));
 237  0
             return prepStmt.executeUpdate();
 238   
         } catch( IOException e) {
 239  0
             throw new SQLException( "Error serializing object in executeUpdate");
 240   
         } catch( SQLException ex) {
 241  0
             if( debug)
 242  0
                 ex.printStackTrace( logWriter);
 243  0
             throw ex;
 244   
         } finally {
 245  0
             if( prepStmt != null)
 246  0
                 prepStmt.close();
 247   
         }
 248   
     }
 249   
 
 250  0
     public int executeUpdate( String command)
 251   
             throws SQLException{
 252  0
         String []cmds = nativeSQL0( command);
 253  0
         int res = executeUpdate0( cmds[0]);
 254  0
         for( int i = 1 ; i < cmds.length; i++)
 255  0
             executeUpdate0( cmds[i]);
 256  0
         return res;
 257   
     }
 258   
 
 259  0
     protected int executeUpdate0( String command)
 260   
             throws SQLException{
 261  0
         try {
 262  0
             if( debug)
 263  0
                 DriverManager.getLogWriter().println( command);
 264  0
             return stmt.executeUpdate( command);
 265   
         } catch( SQLException ex) {
 266  0
             if( debug)
 267  0
                 ex.printStackTrace( logWriter);
 268  0
             throw ex;
 269   
         }
 270   
     }
 271   
 
 272  0
     public ResultSet executeQuery( String command)
 273   
             throws SQLException{
 274  0
         try {
 275  0
             command = nativeSQL( command);
 276  0
             if( debug)
 277  0
                 DriverManager.getLogWriter().println( command);
 278  0
             return stmt.executeQuery( command);
 279   
         } catch( SQLException ex) {
 280  0
             if( debug)
 281  0
                 ex.printStackTrace( logWriter);
 282  0
             throw ex;
 283   
         }
 284   
     }
 285   
     
 286   
     /**
 287   
      * Sets the autocommit mode
 288   
      */
 289  0
     public void setAutoCommit( boolean flag) throws SQLException {
 290  0
         if( con != null)
 291  0
             con.setAutoCommit( flag);
 292   
     }
 293   
 
 294   
     /**
 295   
      * Commit transaction
 296   
      */
 297  0
     public void commit() throws SQLException {
 298  0
         if( con != null)
 299  0
             con.commit();
 300   
     }
 301   
 
 302   
     /**
 303   
      * Rollback transaction
 304   
      */
 305  0
     public void rollback() throws SQLException {
 306  0
         if( con != null)
 307  0
             con.rollback();
 308   
     }
 309   
 
 310   
 
 311  0
     private void checkForWarning( SQLWarning warn)     
 312   
             throws SQLException  {
 313  0
         PrintWriter writer = DriverManager.getLogWriter();
 314   
         
 315  0
         if( warn != null) {
 316  0
             writer.println( "\n *** Warning ***\n");
 317  0
             while (warn != null) {
 318  0
                 writer.println( "SQLState: " + warn.getSQLState ());
 319  0
                 writer.println( "Message:  " + warn.getMessage ());
 320  0
                 writer.println( "Vendor:   " + warn.getErrorCode ());
 321  0
                 writer.println( "");
 322  0
                 warn = warn.getNextWarning();
 323   
             }
 324   
         }
 325   
     }
 326   
     
 327  0
     public boolean isValid() {
 328  0
         try {
 329  0
             return con != null && ! con.isClosed();
 330   
         } catch( SQLException ex) {
 331  0
             return false;
 332   
         }
 333   
     }
 334   
 }
 335