1 package in.co.daffodil.db.jdbc; 2 3 import java.math.*; 4 import java.sql.*; 5 import java.sql.PreparedStatement ; 6 import java.util.*; 7 import com.daffodilwoods.daffodildb.client.*; 8 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*; 9 import com.daffodilwoods.daffodildb.server.serversystem.*; 10 import com.daffodilwoods.database.resource.*; 11 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*; 12 import java.lang.reflect.Constructor ; 13 import com.daffodilwoods.database.general._ParameterMetaData; 14 import com.daffodilwoods.daffodildb.server.sql99.dml.DMLResult; 15 import com.daffodilwoods.daffodildb.server.sql99.common.*; 16 17 49 50 51 public class DaffodilDBPreparedStatement 52 extends DaffodilDBStatement 53 implements PreparedStatement { 54 55 String query; 56 Object [] parameters; 57 boolean[] parametersSet; 58 int parametersCount; 59 ArrayList parameterBatch; 60 DaffodilDBResultSetMetaData metaData; 61 _PreparedStatement serverPreparedStatement; 62 int autoGeneratedKeyType ; 63 Object autoGeneratedKeyParams; 64 67 boolean userDefinedFunction ; 68 private Object [] executeObject = null; 69 70 71 72 public DaffodilDBPreparedStatement(DaffodilDBConnection connection, 73 String query) throws SQLException { 74 this(connection, query, ResultSet.TYPE_FORWARD_ONLY, 75 ResultSet.CONCUR_READ_ONLY); 76 } 77 78 public DaffodilDBPreparedStatement(DaffodilDBConnection connection, 79 String sql, int resultSetType, 80 int resultSetConcurrency) throws 81 SQLException { 82 this(connection, sql, resultSetType, resultSetConcurrency, 83 1 ,_Connection.NOAUTOGENERATEDKEYS,null); 84 } 85 86 public DaffodilDBPreparedStatement(DaffodilDBConnection connection, 87 String sql, int autoGeneratedKeys) throws 88 SQLException { 89 this(connection, sql, ResultSet.TYPE_FORWARD_ONLY, 90 ResultSet.CONCUR_READ_ONLY, 91 1, 92 autoGeneratedKeys == NO_GENERATED_KEYS ?_Connection.NOAUTOGENERATEDKEYS 93 : _Connection.AUTOGENERATEDKEYS, null); 94 } 95 96 public DaffodilDBPreparedStatement(DaffodilDBConnection connection, 97 String sql, int[] columnIndexes) throws 98 SQLException { 99 this(connection, sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, 100 1 ,_Connection.COLUMNINDEXES,columnIndexes); 101 } 102 103 public DaffodilDBPreparedStatement(DaffodilDBConnection connection, 104 String sql, String [] columnNames) throws 105 SQLException { 106 this(connection, sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, 107 1 ,_Connection.COLUMNNAMES,columnNames); 108 } 109 110 public DaffodilDBPreparedStatement(DaffodilDBConnection connection, 111 String sql, int resultSetType, 112 int resultSetConcurrency, 113 int resultSetHoldability) throws 114 SQLException { 115 this(connection, sql, resultSetType, resultSetConcurrency, 116 resultSetHoldability,_Connection.NOAUTOGENERATEDKEYS,null); 117 } 118 119 120 public DaffodilDBPreparedStatement(DaffodilDBConnection connection, 121 String sql, int resultSetType, 122 int resultSetConcurrency, 123 int resultSetHoldability,int autoType0, Object autoParam0) throws 124 SQLException { 125 super(connection, resultSetType, resultSetConcurrency, resultSetHoldability); 126 if (sql == null || sql.length() == 0) { 127 DException dex = new DException("DSE828", null); 128 throw dex.getSqlException(connection.getLocale()); 129 } 130 query = sql; 131 autoGeneratedKeyType = autoType0; 132 autoGeneratedKeyParams = autoParam0; 133 setPreparedStatement(); 134 } 135 136 137 138 162 163 public synchronized boolean execute() throws SQLException { 164 checkConnection(); 165 writeToLog(query); 166 createEnvironmentToExecute(true); 167 checkIfAllParametersSet(); 168 printQueryParam(); 169 if (resultSet != null) { 170 resultSet.close(); 171 resultSet = null; 172 } 173 updateCount = -1; 174 currentResultIndex = 0; 175 Object [] reqObject = null; 176 try { 177 synchronized (connection) { 178 executeResult = new Object [] { 179 serverPreparedStatement.execute(parameters.length == 0 ? null : 180 parameters, queryTimeOut)}; 181 reqObject = getRequiredObjectOfExecute( 182 executeResult[0], resultSetConcurrency,maxRows); 183 } 184 } 185 catch (DException dxe) { 186 throw dxe.getSqlException(connection.getLocale()); 187 } 188 189 if (reqObject[0] instanceof _RecordSetBuffer) { 190 executeResult[0] = reqObject[0]; 191 _RecordSetBuffer recordSetBuffer = (_RecordSetBuffer) executeResult[0]; 192 resultSet = new DaffodilDBResultSet(this); 193 resultSet.setRecordSetBuffer(recordSetBuffer); 194 int oldResultSetCon = resultSetConcurrency; 195 resultSetConcurrency = reqObject[1].hashCode(); 196 if(oldResultSetCon != resultSetConcurrency ) 197 addWarning("the resultSet Concurency is set ResultSet.CONCUR_READ_ONLY " ) ; 198 199 setResultSetAttributes(); 200 setRecordSetBufferAttributes(recordSetBuffer); 201 resultSetConcurrency = oldResultSetCon; 202 return true; 203 } 204 if(reqObject[0] instanceof DMLResult){ 205 dmlResult = (DMLResult)reqObject[0]; 206 executeResult[0] = new Integer (dmlResult.rowsEffected); 207 }else{ 208 executeResult[0] = reqObject[0]; 209 } 210 updateCount = executeResult[0].hashCode(); 211 updateCount = updateCount == Integer.MIN_VALUE ? 0 : updateCount; 212 return false; 213 } 214 215 224 225 public synchronized ResultSet executeQuery() throws SQLException { 226 checkConnection(); 227 writeToLog("Query",query); 228 createEnvironmentToExecute(true); 229 checkIfAllParametersSet(); 230 printQueryParam(); 231 if (resultSet != null) { 232 resultSet.close(); 233 resultSet = null; 234 } 235 updateCount = -1; 236 237 Object result[] = null; 238 try { 239 if (serverPreparedStatement.getQueryType() != 240 _PreparedStatement.queryexpression) { 241 DException ex = new DException("DSE86", null); 242 throw ex.getSqlException(connection.getLocale()); 243 } 244 synchronized (connection ) { 245 result = getRequiredObjectOfExecute( 246 serverPreparedStatement.execute(parameters.length == 0 ? null : 247 parameters, queryTimeOut), 248 resultSetConcurrency,maxRows); 249 } 250 } 251 catch (DException dxe) { 252 throw dxe.getSqlException(connection.getLocale()); 253 } 254 _RecordSetBuffer recordSetBuffer = (_RecordSetBuffer)result[0]; 255 resultSet = new DaffodilDBResultSet(this); 256 resultSet.setRecordSetBuffer(recordSetBuffer); 257 int oldResultSetCon = resultSetConcurrency; 258 resultSetConcurrency = result[1].hashCode(); 259 if(oldResultSetCon != resultSetConcurrency ) 260 addWarning("the resultSet Concurency is set ResultSet.CONCUR_READ_ONLY " ) ; 261 setResultSetAttributes(); 262 setRecordSetBufferAttributes(recordSetBuffer); 263 resultSetConcurrency = oldResultSetCon; 264 return resultSet; 265 } 266 267 279 280 public synchronized int executeUpdate() throws SQLException { 281 checkConnection(); 282 writeToLog("Update", query ); 283 createEnvironmentToExecute(true); 284 checkIfAllParametersSet(); 285 printQueryParam(); 286 if (resultSet != null) { 287 resultSet.close(); 288 resultSet = null; 289 } 290 updateCount = -1; 291 292 Object result = null; 293 try { 294 if (serverPreparedStatement.getQueryType() == 295 _PreparedStatement.queryexpression) { 296 DException ex = new DException("DSE533",null); 297 throw ex.getSqlException(connection.getLocale()); 298 } 299 synchronized (connection) { 300 result = serverPreparedStatement.execute(parameters.length == 0 ? null : 301 parameters, queryTimeOut); 302 } 303 } 304 catch (DException dxe) { 305 throw dxe.getSqlException(connection.getLocale()); 306 } 307 if (result instanceof _SelectIterator) { 308 DException dex = new DException("DSE819", null); 309 throw dex.getSqlException(connection.getLocale()); 310 } 311 if(result instanceof DMLResult){ 312 dmlResult = (DMLResult)result; 313 updateCount = dmlResult.rowsEffected; 314 }else{ 315 updateCount = result.hashCode(); 316 updateCount = updateCount == Integer.MIN_VALUE ? 0 : updateCount; 317 } 318 return updateCount; 319 } 320 321 330 331 public synchronized void setNull(int parameterIndex, int sqlType) throws SQLException { 332 setObject(parameterIndex, null, sqlType); 333 } 334 335 344 345 public synchronized void setBoolean(int parameterIndex, boolean x) throws SQLException { 346 setObject(parameterIndex, Utilities.getBooleanValue(x)); 347 } 348 349 358 359 public synchronized void setByte(int parameterIndex, byte x) throws SQLException { 360 setObject(parameterIndex, new Byte (x)); 361 } 362 363 372 373 public synchronized void setShort(int parameterIndex, short x) throws SQLException { 374 setObject(parameterIndex, new Short (x)); 375 } 376 377 386 387 public synchronized void setInt(int parameterIndex, int x) throws SQLException { 388 setObject(parameterIndex, new Integer (x)); 389 } 390 391 400 401 public synchronized void setLong(int parameterIndex, long x) throws SQLException { 402 setObject(parameterIndex, new Long (x)); 403 } 404 405 414 415 public synchronized void setFloat(int parameterIndex, float x) throws SQLException { 416 setObject(parameterIndex, new Float (x)); 417 } 418 419 428 429 public synchronized void setDouble(int parameterIndex, double x) throws SQLException { 430 setObject(parameterIndex, new Double (x)); 431 } 432 433 442 443 public synchronized void setBigDecimal(int parameterIndex, BigDecimal x) throws 444 SQLException { 445 setObject(parameterIndex, x); 446 } 447 448 460 461 public synchronized void setString(int parameterIndex, String x) throws SQLException { 462 setObject(parameterIndex, x); 463 } 464 465 475 476 public synchronized void setBytes(int parameterIndex, byte x[]) throws SQLException { 477 setObject(parameterIndex, x); 478 } 479 480 489 490 public synchronized void setDate(int parameterIndex, java.sql.Date x) throws SQLException { 491 setObject(parameterIndex, x); 492 } 493 494 503 504 public synchronized void setTime(int parameterIndex, java.sql.Time x) throws SQLException { 505 setObject(parameterIndex, x); 506 } 507 508 518 519 public synchronized void setTimestamp(int parameterIndex, java.sql.Timestamp x) throws 520 SQLException { 521 setObject(parameterIndex, x); 522 } 523 524 542 543 public synchronized void setAsciiStream(int parameterIndex, java.io.InputStream x, 544 int length) throws SQLException { 545 byte[] bytes = new byte[length]; 546 try { 547 x.read(bytes, 0, length); 548 } 549 catch (java.io.IOException ex) { 550 DException dxe = new DException("DSE513", null); 551 throw dxe.getSqlException(connection.getLocale()); 552 } 553 setObject(parameterIndex, bytes); 554 } 555 556 579 580 public synchronized void setUnicodeStream(int parameterIndex, java.io.InputStream x, 581 int length) throws SQLException { 582 byte[] bytes = new byte[length]; 583 try { 584 x.read(bytes, 0, length); 585 } 586 catch (java.io.IOException ex) { 587 DException dxe = new DException("DSE513", null); 588 throw dxe.getSqlException(connection.getLocale()); 589 } 590 setObject(parameterIndex, bytes); 591 } 592 593 610 public synchronized void setBinaryStream(int parameterIndex, java.io.InputStream x, 611 int length) throws SQLException { 612 byte[] bytes = new byte[length]; 613 try { 614 x.read(bytes, 0, length); 615 } 616 catch (java.io.IOException ex) { 617 DException dxe = new DException("DSE513", null); 618 throw dxe.getSqlException(connection.getLocale()); 619 } 620 setObject(parameterIndex, bytes); 621 } 622 623 633 634 public synchronized void clearParameters() throws SQLException { 635 checkConnection(); 636 Arrays.fill(parametersSet, false); 637 Arrays.fill(parameters, null); 638 } 639 640 641 671 672 public synchronized void setObject(int parameterIndex, Object x, int targetSqlType, 673 int scale) throws SQLException { 674 Object value = null; 675 try { 676 value = Utilities.convertObject(x, targetSqlType); 677 } 678 catch (DException dxe) { 679 throw dxe.getSqlException(connection.getLocale()); 680 } 681 if (targetSqlType == java.sql.Types.DECIMAL && 682 value instanceof java.math.BigDecimal ) { 683 java.math.BigDecimal numericValue = (java.math.BigDecimal ) value; 684 value = scale == -1 ? numericValue : 685 numericValue.setScale(scale, numericValue.ROUND_DOWN); 686 } 687 setObject(parameterIndex, value); 688 } 689 690 701 702 public synchronized void setObject(int parameterIndex, Object x, int targetSqlType) throws 703 SQLException { 704 try { 705 Object o = Utilities.convertObject(x, targetSqlType); 706 setObject(parameterIndex, o); 707 } 708 catch (DException dxe) { 709 throw dxe.getSqlException(connection.getLocale()); 710 } 711 } 712 713 743 744 public synchronized void setObject(int parameterIndex, Object x) throws SQLException { 745 checkConnection(); 746 checkValidParameterIndex(parameterIndex); 747 parametersSet[parameterIndex - 1] = true; 748 parameters[parameterIndex - 1] = x; 749 } 750 751 755 756 764 765 public synchronized void addBatch() throws SQLException { 766 checkConnection(); 767 checkIfAllParametersSet(); 768 try { 769 parameterBatch.add(parameters); 770 } 771 catch (NullPointerException npe) { 772 parameterBatch = new ArrayList(4); 773 parameterBatch.add(parameters); 774 } 775 parametersSet = new boolean[parametersCount]; 776 parameters = new Object [parametersCount]; 777 Arrays.fill(parametersSet, false); 778 } 779 780 800 801 public synchronized void setCharacterStream(int parameterIndex, java.io.Reader reader, 802 int length) throws SQLException { 803 try { 804 char[] chars = new char[length]; 805 reader.read(chars, 0, length); 806 byte[] bytes = Utilities.convertCharsToBytes(chars); 807 setObject(parameterIndex, bytes); 808 } 809 catch (Exception ex) { 810 DException dxe = new DException("DSE513", null); 811 throw dxe.getSqlException(connection.getLocale()); 812 } 813 } 814 815 826 827 public synchronized void setRef(int parameterIndex, Ref x) throws SQLException { 828 setObject(parameterIndex, x); 829 } 830 831 841 842 public synchronized void setBlob(int parameterIndex, Blob x) throws SQLException { 843 setObject(parameterIndex, x); 844 } 845 846 856 public synchronized void setClob(int parameterIndex, Clob x) throws SQLException { 857 setObject(parameterIndex, x); 858 } 859 860 870 871 public synchronized void setArray(int parameterIndex, Array x) throws SQLException { 872 setObject(parameterIndex, x); 873 } 874 875 899 900 public synchronized ResultSetMetaData getMetaData() throws SQLException { 901 if (metaData == null) { 902 try { 903 _ColumnCharacteristics clmchs = serverPreparedStatement. 904 getColumnCharacteristics(); 905 metaData = new DaffodilDBResultSetMetaData(connection); 906 metaData.setColumnCharacteristics(clmchs); 907 } 908 catch (DException dxe) { 909 throw dxe.getSqlException(connection.getLocale()); 910 } 911 } 912 return metaData; 913 } 914 915 932 933 public synchronized void setDate(int parameterIndex, java.sql.Date x, Calendar cal) throws 934 SQLException { 935 cal.setTime(x); 936 java.sql.Date date = new com.daffodilwoods.daffodildb.utils.DBDate(cal.getTime().getTime()); 937 setObject(parameterIndex, date); 938 } 939 940 957 958 public synchronized void setTime(int parameterIndex, java.sql.Time x, Calendar cal) throws 959 SQLException { 960 cal.setTime(x); 961 Time time = new Time(cal.getTime().getTime()); 962 setObject(parameterIndex, time); 963 } 964 965 982 983 public synchronized void setTimestamp(int parameterIndex, java.sql.Timestamp x, 984 Calendar cal) throws SQLException { 985 cal.setTime(x); 986 Timestamp timeStamp = new Timestamp(cal.getTime().getTime()); 987 setObject(parameterIndex, timeStamp); 988 } 989 990 1018 1019 public synchronized void setNull(int parameterIndex, int sqlType, String typeName) throws 1020 SQLException { 1021 setObject(parameterIndex, null, sqlType); 1022 } 1023 1024 1025 1035 public synchronized void setURL(int parameterIndex, java.net.URL x) throws SQLException { 1036 setObject(parameterIndex, x); 1037 } 1038 1039 1050 public synchronized ParameterMetaData getParameterMetaData() throws SQLException { 1051 try { 1052 Class cl = Class.forName("in.co.daffodil.db.jdbc.DaffodilDBParameterMetaData"); 1053 Constructor cons = cl.getConstructor(new Class []{ 1054 DaffodilDBConnection.class, _ParameterMetaData.class,Boolean .class}); 1055 Object obj = cons.newInstance(new Object []{connection, 1056 serverPreparedStatement.getParameterMetaData(),userDefinedFunction ? Boolean.TRUE : Boolean.FALSE 1057 }); 1058 return (ParameterMetaData)obj; 1059 } 1060 catch (DException ex) { 1061 throw ex.getSqlException(connection.getLocale()); 1062 } 1063 catch (Exception ex) { 1064 return null; 1065 } 1066 } 1067 1068 1069 1070 public synchronized int[] executeBatch() throws SQLException { 1071 checkConnection(); 1072 if (parameterBatch == null) 1073 return new int[0]; 1074 createEnvironmentToExecute(false); 1075 int size = parameterBatch.size(); 1076 int[] abc = new int[size]; 1077 Arrays.fill(abc, -3 ); 1078 Object obj = null; 1079 synchronized (connection) { 1080 for (int i = 0; i < size; i++) { 1081 try { 1082 if (serverPreparedStatement.getQueryType() == 1083 _PreparedStatement.queryexpression) { 1084 DException ex = new DException("DSE533", null); 1085 throw new BatchUpdateException(ex.getMessage(), abc); 1086 } 1087 Object [] tempParam = (Object []) parameterBatch.get(i); 1088 obj = serverPreparedStatement.execute(tempParam.length == 0 ? null 1089 : tempParam, queryTimeOut); 1090 } 1091 catch (DException E) { 1092 clearBatch(); 1093 throw new BatchUpdateException(E.getMessage(), abc); 1094 } 1095 abc[i] = obj.hashCode(); 1096 abc[i] = abc[i] == Integer.MIN_VALUE ? 0 : abc[i]; 1097 } 1098 } 1099 clearBatch(); 1100 return abc; 1101 } 1102 1103 public synchronized void clearBatch() throws SQLException { 1104 checkConnection(); 1105 if (parameterBatch != null) 1106 parameterBatch.clear(); 1107 clearParameters(); 1108 } 1109 1110 public void addBatch(String query) throws SQLException { 1111 DException dex = new DException("DSE525", null); 1112 throw dex.getSqlException(connection.getLocale()); 1113 } 1114 1115 public ResultSet executeQuery(String query) throws SQLException { 1116 DException dex = new DException("DSE567", null); 1117 throw dex.getSqlException(connection.getLocale()); 1118 } 1119 1120 public int executeUpdate(String query) throws SQLException { 1121 DException dex = new DException("DSE567", null); 1122 throw dex.getSqlException(connection.getLocale()); 1123 } 1124 1125 public boolean execute(String query) throws SQLException { 1126 DException dex = new DException("DSE567", null); 1127 throw dex.getSqlException(connection.getLocale()); 1128 } 1129 1130 protected void checkIfAllParametersSet() throws SQLException{ 1131 boolean flag = true; 1132 for (int i = 0; flag && i < parametersCount; i++) 1133 flag = flag && parametersSet[i]; 1134 if(flag==false) { 1135 DException dex = new DException("DSE1157", null); 1136 throw dex.getSqlException(connection.getLocale()); 1137 } 1138 } 1139 1140 protected void checkValidParameterIndex(int i) throws SQLException { 1141 if (i < 1 || i > parametersCount) { 1142 DException dex = new DException("DSE479", new Object [] {new Integer (i), 1143 new Integer (parametersCount)}); 1144 throw dex.getSqlException(connection.getLocale()); 1145 } 1146 } 1147 1148 protected void setPreparedStatement() throws SQLException { 1149 String queryForDB = changeQueryForDB(query); 1150 try { 1151 if(autoGeneratedKeyType == _Connection.NOAUTOGENERATEDKEYS){ 1152 serverPreparedStatement = connection.getServerConnection(). 1153 getPreparedStatement(queryForDB, 1154 resultSetConcurrency == 1155 ResultSet.CONCUR_UPDATABLE ? 1156 IteratorConstants.UPDATABLE : 1157 IteratorConstants.NONSCROLLABLE); 1158 } 1159 else{ 1160 serverPreparedStatement = connection.getServerConnection(). 1161 getPreparedStatement(queryForDB, 1162 resultSetConcurrency == 1163 ResultSet.CONCUR_UPDATABLE ? 1164 IteratorConstants.UPDATABLE : 1165 IteratorConstants.NONSCROLLABLE,autoGeneratedKeyType,autoGeneratedKeyParams); 1166 } 1167 parametersCount = serverPreparedStatement.getParameterCount(); 1168 } 1169 catch (DException dxe) { 1170 throw dxe.getSqlException(connection.getLocale()); 1171 } 1172 parametersSet = new boolean[parametersCount]; 1173 parameters = new Object [parametersCount]; 1174 Arrays.fill(parametersSet, false); 1175 } 1176 1177 protected void printQueryParam() throws SQLException { 1178 } 1179 1180 public synchronized void close() throws SQLException { 1181 if(connection == null ) return; 1184 if(serverPreparedStatement.getQueryType() == _PreparedStatement.queryexpression && connection.isStatementCachedEnabled()){ 1185 this.clearParameters(); 1186 connection.addToCache(Utilities.calculatehashCode(query,resultSetType,resultSetConcurrency,resultSetHoldability), this); 1187 } 1188 else 1189 serverPreparedStatement.close(); 1190 super.close(); 1191 } 1192 1193 protected Object [] getRequiredObjectOfExecute(Object returnObject, 1194 int concurrency,int maxrows) throws 1195 DException, SQLException { 1196 return returnObject instanceof _SelectIterator ? getRequiredObjectOfExecuteQuery(returnObject, concurrency,maxrows) : 1197 new Object [] {returnObject,new Integer (concurrency)}; 1198 } 1199 1200 private Object [] getRequiredObjectOfExecuteQuery(Object returnObject, 1201 int concurrency,int maxRows) throws DException, SQLException { 1202 _SelectIterator retriever = (_SelectIterator) returnObject; 1203 if (concurrency == ResultSet.CONCUR_UPDATABLE && retriever.isUpdatable()) { 1204 RecordSetUpdateable rsb = new RecordSetUpdateable(); 1205 rsb.setSelectIterator(retriever); 1206 return new Object []{rsb,new Integer (ResultSet.CONCUR_UPDATABLE)}; 1207 1208 } 1209 RecordSet rsb = new RecordSet(); 1210 rsb.setSelectIterator(retriever); 1211 return new Object []{rsb,new Integer (ResultSet.CONCUR_READ_ONLY)}; 1212 } 1213 public void setConnection(DaffodilDBConnection con ){ 1214 connection = con; 1215 } 1216 1217} 1218 | Popular Tags |