Clover coverage report - Replica - 1.0-Alpha
Coverage timestamp: Dom Fev 1 2004 17:00:58 WET
file stats: LOC: 332   Methods: 24
NCLOC: 209   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
MobisnapInterpreter.java 0% 0% 0% 0%
coverage
 1   
 package mobisnap.mobile_trx;
 2   
 
 3   
 import java.io.*;
 4   
 import java.sql.*;
 5   
 import java.util.*;
 6   
 import mobisnap.*;
 7   
 import nmp.dbms.*;
 8   
 import nmp.dbms.JDBC.*;
 9   
 import mobisnap.common.reservation.*;
 10   
 
 11   
 /**
 12   
  * Interprets MobisnapSQL
 13   
  */
 14   
 public class MobisnapInterpreter
 15   
 {
 16   
     protected Jdbc_Sql    database;
 17   
     protected TransactionHandle    transaction;
 18   
     protected MobisnapSQL    interpreter;
 19   
     /**
 20   
      * Used to store the reservation used to process a mobile transaction during
 21   
      * the course of its execution
 22   
      */
 23   
     protected Vector refRsrvs;
 24   
     protected Vector usedRsrvs;
 25   
 
 26  0
     public MobisnapInterpreter( String driverName, String url, String name, String pwd ) throws SQLException {
 27  0
         database = new Jdbc_Sql( driverName, url, name, pwd);
 28  0
         transaction = database.getTransactionHandle();
 29  0
         refRsrvs = new Vector();
 30  0
         usedRsrvs = new Vector();
 31   
     }
 32   
     
 33  0
     public MobisnapInterpreter( Jdbc_Sql database) throws SQLException {
 34  0
         this.database = database;
 35  0
         transaction = database.getTransactionHandle();
 36  0
         refRsrvs = new Vector();
 37  0
         usedRsrvs = new Vector();
 38   
     }
 39   
     
 40   
     /**
 41   
      * Releases interpreter
 42   
      */
 43  0
     public void release() throws SQLException {
 44  0
         if( transaction != null) {
 45  0
             transaction.release();
 46  0
             transaction = null;
 47   
         }
 48  0
         if( database != null) {
 49  0
             database.closeAll();
 50  0
             database = null;
 51   
         }
 52   
     }
 53   
 
 54   
     /**
 55   
      * Initializes internal structures related with transaction processing
 56   
      */
 57  0
     protected void initTransaction( Reader reader) throws Exception {
 58  0
         if( transaction == null)
 59  0
             throw new SQLException( "Interpreter not ready");
 60  0
         if( interpreter == null) {
 61  0
             MobisnapSQL.transaction = transaction;
 62  0
             transaction.commit();
 63  0
             MobisnapSQL.names = new MSQLNames();
 64  0
             interpreter = new MobisnapSQL( reader);
 65   
         } else {
 66  0
             transaction.commit();
 67  0
             MobisnapSQL.names.reset();
 68  0
             MobisnapSQL.ReInit( reader);
 69   
         }
 70   
     }
 71   
     
 72   
     /**
 73   
      * Initializes internal structures related with query processing
 74   
      */
 75  0
     protected void initQuery( Reader reader) throws Exception {
 76  0
         if( database == null)
 77  0
             throw new SQLException( "Interpreter not ready");
 78  0
         if( interpreter == null) {
 79  0
             MobisnapSQL.transaction = transaction;
 80  0
             transaction.commit();
 81  0
             MobisnapSQL.names = new MSQLNames();
 82  0
             interpreter = new MobisnapSQL( reader);
 83   
         } else {
 84  0
             transaction.commit();
 85  0
             MobisnapSQL.names.reset();
 86  0
             MobisnapSQL.ReInit( reader);
 87   
         }
 88   
     }
 89   
 
 90   
     /**
 91   
      * interpreter should be properly initialized - defined only to 
 92   
      * make the definition visible to outside extensions
 93   
      */
 94  0
     protected ASTDatabaseDefinition databaseDefinition() throws Exception {
 95  0
         return MobisnapSQL.DatabaseDefinition();
 96   
     }
 97   
     
 98   
     /**
 99   
      * interpreter should be properly initialized - defined only to 
 100   
      * make the definition visible to outside extensions
 101   
      */
 102  0
     protected ASTCreateSnapshot createSnapshot() throws Exception  {
 103  0
         return MobisnapSQL.CreateSnapshot();
 104   
     }
 105   
     
 106   
     /**
 107   
      * interpreter should be properly initialized - defined only to 
 108   
      * make the definition visible to outside extensions
 109   
      */
 110  0
     protected ASTUpdateSnapshot updateSnapshot() throws Exception  {
 111  0
         return MobisnapSQL.UpdateSnapshot();
 112   
     }
 113   
     
 114   
     /**
 115   
      * interpreter should be properly initialized - defined only to 
 116   
      * make the definition visible to outside extensions
 117   
      */
 118  0
     protected ASTGetReservation getReservation() throws Exception  {
 119  0
         return MobisnapSQL.GetReservation();
 120   
     }
 121   
     
 122   
     /**
 123   
      * interpreter should be properly initialized - defined only to 
 124   
      * make the definition visible to outside extensions
 125   
      */
 126  0
     protected ASTCreateTable createTable( java.util.HashMap map) throws Exception  {
 127  0
         return MobisnapSQL.CreateTable( map);
 128   
     }
 129   
     
 130   
     /**
 131   
      * interpreter should be properly initialized - defined only to 
 132   
      * make the definition visible to outside extensions
 133   
      */
 134  0
     protected ASTDropTable dropTable( java.util.HashMap map) throws Exception  {
 135  0
         return MobisnapSQL.DropTable(map);
 136   
     }
 137   
     
 138   
     /**
 139   
      * interpreter should be properly initialized - defined only to 
 140   
      * make the definition visible to outside extensions
 141   
      */
 142  0
     protected ASTSQLStatement sqlStatement() throws Exception  {
 143  0
         return MobisnapSQL.SQLStatement();
 144   
     }
 145   
     
 146   
     /**
 147   
      * Process a query statement
 148   
      */
 149  0
     public synchronized SQLVectorResult processQuery( int msql_type, Reader reader) throws Exception {
 150  0
         initQuery( reader);
 151  0
         ASTQueryStatement node = MobisnapSQL.QueryStatement();
 152  0
         return node.getResult( msql_type);
 153   
     }
 154   
     
 155   
     /**
 156   
      * Initializes reservations
 157   
      */
 158  0
     private void initReservations() {
 159  0
         refRsrvs.removeAllElements();
 160  0
         usedRsrvs.removeAllElements();
 161   
     }
 162   
     
 163   
     /**
 164   
      * Should be called when some reservation has been associated with any variable
 165   
      */
 166  0
     public void refReservation( ReservationUseBase rsrv) {
 167  0
         refRsrvs.addElement( rsrv);
 168   
     }
 169   
     
 170   
     /**
 171   
      * Should be called when some reservation has effectively been used in the execution
 172   
      * of the mobile transactions, i.e., when some test has been made that must be
 173   
      * backed up by the transaction
 174   
      */
 175  0
     public void useReservation( ReservationUseBase rsrv) {
 176  0
         usedRsrvs.addElement( rsrv);
 177   
     }
 178   
     
 179   
     /**
 180   
      * The associated transaction has been commit - used reservations must be updated
 181   
      */
 182  0
     protected void commitReservations() {
 183   
         // do nothing
 184   
     }
 185   
     
 186   
     /**
 187   
      * The associated reservations has been rollbacked - used reservations must be updated
 188   
      * If called after "commitReservations" it has no effect
 189   
      */
 190  0
     protected void rollbackReservations() {
 191   
         // do nothing
 192   
     }
 193   
     
 194   
     /**
 195   
      * Returns the used reservation that backs up the given conditions
 196   
      * 
 197   
      * @param table Table name of possible reservation
 198   
      * @param column Column name of possible reservation
 199   
      * @param where Table row specification
 200   
      * @return Returns null if no used reservation backs up the given conbditions
 201   
      */
 202  0
     public ReservationUseBase getUsedReservation( String table, String column, SimpleNode where) {
 203  0
         where = where.simplify( MobisnapConstants.MSQL_SERVER, false);
 204  0
         Enumeration enum = usedRsrvs.elements();
 205  0
         while( enum.hasMoreElements()) {
 206  0
             ReservationUseBase rsrv = (ReservationUseBase)enum.nextElement();
 207  0
             if( rsrv.getRsrv().checkReservation( table, column, where))
 208  0
                 return rsrv;
 209   
         }
 210  0
         return null;
 211   
     }
 212   
     
 213   
     /**
 214   
      * Returns the used reservation that backs up the given conditions
 215   
      * 
 216   
      * @param table Table name of possible reservation
 217   
      * @param column Column name of possible reservation
 218   
      * @param conditions Table row specification
 219   
      * @return Returns null if no used reservation backs up the given conbditions
 220   
      */
 221  0
     public ReservationUseBase getUsedReservation( String table, String column, Vector conditions) {
 222  0
         Enumeration enum = usedRsrvs.elements();
 223  0
         while( enum.hasMoreElements()) {
 224  0
             ReservationUseBase rsrv = (ReservationUseBase)enum.nextElement();
 225  0
             if( rsrv.getRsrv().checkReservation( table, column, conditions))
 226  0
                 return rsrv;
 227   
         }
 228  0
         return null;
 229   
     }
 230   
     
 231   
 
 232   
     /**
 233   
      * Process a mobile transaction
 234   
      * 
 235   
      * @param msql_type Processing type (server, client tentative, client reservation)
 236   
      * @param reader Reader to read mobile transaction
 237   
      */
 238  0
     public int processMSQLTrx( Reader reader) throws Exception {
 239  0
         return processMSQLTrx( MobisnapConstants.MSQL_SERVER, reader);
 240   
     }
 241  0
     public synchronized int processMSQLTrx( int msql_type, Reader reader) throws Exception {
 242  0
         initTransaction( reader);
 243  0
         SimpleNode mainNode = MobisnapSQL.CompilationUnit() ;
 244  0
         if( msql_type == MobisnapConstants.MSQL_RESERVATION_CLIENT) {
 245  0
             try {
 246  0
                 initReservations();
 247  0
                 mainNode.process( msql_type);
 248  0
                 throw new mobisnap.MobisnapException( "Internal error - compilation unit ended without a result");
 249   
             } catch( CommitException e) {
 250  0
                 commitReservations();
 251  0
                 return MobisnapConstants.MSQLCR_RESERVATION_COMMIT;
 252   
             } catch( UnknownException e) {
 253  0
                 transaction.rollback();
 254  0
                 return MobisnapConstants.MSQLCR_UNKNOWN;
 255   
             } catch( RollbackException e) {
 256   
                   // do nothing
 257   
             } catch( NoReservationException e) {
 258   
                   // do nothing
 259   
             } finally {
 260  0
                 rollbackReservations();
 261   
             }
 262  0
             msql_type = MobisnapConstants.MSQL_TENTATIVE_CLIENT;
 263   
         }
 264  0
         if( msql_type == MobisnapConstants.MSQL_SERVER) {
 265  0
             try {
 266  0
                 mainNode.process( msql_type);
 267  0
                 throw new mobisnap.MobisnapException( "Internal error - compilation unit ended without a result");
 268   
             } catch( CommitException e) {
 269  0
                 return MobisnapConstants.MSQLSR_COMMIT;
 270   
             } catch( RollbackException e) {
 271  0
                 return MobisnapConstants.MSQLSR_ABORT;
 272   
             } catch( UnknownException e) {
 273  0
                 transaction.rollback();
 274  0
                 return MobisnapConstants.MSQLSR_ABORT;
 275   
             }
 276   
         } else {
 277  0
             try {
 278  0
                 mainNode.process( msql_type);
 279  0
                 throw new mobisnap.MobisnapException( "Internal error - compilation unit ended without a result");
 280   
             } catch( CommitException e) {
 281  0
                 return MobisnapConstants.MSQLCR_TENTATIVE_COMMIT;
 282   
             } catch( RollbackException e) {
 283  0
                 return MobisnapConstants.MSQLCR_TENTATIVE_ABORT;
 284   
             } catch( UnknownException e) {
 285  0
                 transaction.rollback();
 286  0
                 return MobisnapConstants.MSQLCR_UNKNOWN;
 287   
             }
 288   
         }
 289   
     }
 290   
 
 291   
     /**
 292   
      * Process a database definition : syntax : begin (sql_ddl_statements)* end ;
 293   
      */
 294  0
     public synchronized void processDatabaseDef( Reader reader, Hashtable table) throws Exception {
 295  0
         initQuery( reader);
 296  0
         ASTDatabaseDefinition node = databaseDefinition();
 297  0
         Iterator it = node.definitions().iterator();
 298  0
         while( it.hasNext()) {
 299  0
             SimpleNode aux = (SimpleNode)it.next();
 300  0
             if( aux instanceof ASTCreateTable) {
 301  0
                 ASTCreateTable ct = (ASTCreateTable)aux;
 302  0
                 StringBuffer buffer = new StringBuffer();
 303  0
                 ct.sourceCode( MobisnapConstants.MSQL_SERVER, buffer);
 304  0
                 table.put( ct.tableName( MobisnapConstants.MSQL_SERVER), buffer.toString().trim());
 305   
             } else 
 306  0
                 System.err.println( "instruction not processed");
 307   
         }
 308   
     }
 309   
 
 310  0
     public static void main( String[] args) {
 311  0
         try {
 312  0
             MobisnapInterpreter interpreter = null;
 313  0
             try { 
 314  0
                 System.out.println( "Usage:\nMobisnapInterpreter filename [url user pwd [driver]]");
 315  0
                 if( args.length == 5)
 316  0
                     interpreter = new MobisnapInterpreter( args[4], args[1], args[2], args[3]);
 317  0
                 else if( args.length == 4)
 318  0
                     interpreter = new MobisnapInterpreter( "sun.jdbc.odbc.JdbcOdbcDriver", args[1], args[2], args[3]);
 319   
                 else
 320  0
                     interpreter = new MobisnapInterpreter( "sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:wine", "nmp", "");
 321   
 
 322  0
                 interpreter.processMSQLTrx( MobisnapConstants.MSQL_SERVER, new FileReader( args[0]));
 323   
 
 324   
             } finally {
 325  0
                 interpreter.release();
 326   
             }
 327   
         } catch( Exception e) {
 328  0
             e.printStackTrace();
 329   
         }
 330   
     }
 331   
 }
 332