1 33 package smallsql.database; 34 35 import java.sql.*; 36 37 38 abstract class Command { 39 40 int type; 41 String catalog; 42 String name; 43 44 SSResultSet rs; 45 int updateCount = -1; 46 47 48 final Expressions columnExpressions; 49 50 51 Expressions params = new Expressions(); 52 53 final Logger log; 54 55 Command(Logger log){ 56 this.log = log; 57 this.columnExpressions = new Expressions(); 58 } 59 60 Command(Logger log, Expressions columnExpressions){ 61 this.log = log; 62 this.columnExpressions = columnExpressions; 63 } 64 65 66 72 void addColumnExpression( Expression column ){ 73 columnExpressions.add( column ); 74 } 75 76 void addParameter( ExpressionValue param ){ 77 params.add( param ); 78 } 79 80 81 84 void verifyParams() throws SQLException{ 85 for(int p=0; p<params.size(); p++){ 86 if(((ExpressionValue)params.get(p)).isEmpty()) 87 throw Utils.createSQLException("Parameter " + (p+1) + " is empty."); 88 } 89 } 90 91 94 void clearParams(){ 95 for(int p=0; p<params.size(); p++){ 96 ((ExpressionValue)params.get(p)).clear(); 97 } 98 } 99 100 104 private ExpressionValue getParam(int idx) throws SQLException{ 105 if(idx < 1 || idx > params.size()) 106 throw Utils.createSQLException("Parameter index " +idx+ " out of range. The value must be between 1 and " + params.size()); 107 return ((ExpressionValue)params.get(idx-1)); 108 } 109 110 114 void setParamValue(int idx, Object value, int dataType) throws SQLException{ 115 getParam(idx).set( value, dataType ); 116 if(log.isLogging()){ 117 log.println("param"+idx+'='+value+"; type="+dataType); 118 } 119 } 120 121 125 void setParamValue(int idx, Object value, int dataType, int length) throws SQLException{ 126 getParam(idx).set( value, dataType, length ); 127 if(log.isLogging()){ 128 log.println("param"+idx+'='+value+"; type="+dataType+"; length="+length); 129 } 130 } 131 132 final void execute(SSConnection con, SSStatement st) throws SQLException{ 133 int savepoint = con.getSavepoint(); 134 try{ 135 executeImpl( con, st ); 136 }catch(Throwable e){ 137 con.rollback(savepoint); 138 throw Utils.createSQLException(e); 139 }finally{ 140 if(con.getAutoCommit()) con.commit(); 141 } 142 } 143 144 abstract void executeImpl(SSConnection con, SSStatement st) throws Exception ; 145 146 SSResultSet getQueryResult() throws SQLException{ 147 if(rs == null) 148 throw Utils.createSQLException("No ResultSet was produce."); 149 return rs; 150 } 151 152 SSResultSet getResultSet(){ 153 return rs; 154 } 155 156 int getUpdateCount(){ 157 return updateCount; 158 } 159 160 161 165 boolean getMoreResults(){ 166 rs = null; 167 updateCount = -1; 168 return false; 169 } 170 171 172 175 void setMaxRows(int max){} 176 177 178 int getMaxRows(){return 0;} 179 } | Popular Tags |