1 23 24 30 31 package com.sun.jdo.spi.persistence.support.sqlstore.sql.generator; 32 33 import org.netbeans.modules.dbschema.ColumnElement; 34 import com.sun.jdo.spi.persistence.support.sqlstore.LogHelperSQLStore; 35 import com.sun.jdo.spi.persistence.support.sqlstore.database.DBVendorType; 36 import com.sun.jdo.spi.persistence.support.sqlstore.model.LocalFieldDesc; 37 import com.sun.jdo.spi.persistence.utility.logging.Logger; 38 39 import java.io.ByteArrayInputStream ; 40 import java.io.StringReader ; 41 import java.math.BigDecimal ; 42 import java.math.BigInteger ; 43 import java.sql.*; 44 45 47 public class DBStatement extends Object { 48 49 50 public static final String BATCH_THRESHOLD_PROPERTY = 51 "com.sun.jdo.spi.persistence.support.sqlstore.BATCH_THRESHOLD"; 52 53 57 private static final int BATCH_THRESHOLD = 58 Integer.getInteger(BATCH_THRESHOLD_PROPERTY, 100).intValue(); 59 60 61 private PreparedStatement preparedStmt; 62 63 64 private int batchCounter = 0; 65 66 67 private String statementText; 68 69 70 private static Logger logger = LogHelperSQLStore.getLogger(); 71 72 78 public DBStatement(Connection conn, String statementText, int timeout) 79 throws SQLException 80 { 81 this.statementText = statementText; 82 preparedStmt = conn.prepareStatement(statementText); 83 preparedStmt.setQueryTimeout(timeout); 85 } 86 87 88 public String getStatementText() 89 { 90 return statementText; 91 } 92 93 94 public PreparedStatement getPreparedStatement() 95 { 96 return preparedStmt; 97 } 98 99 103 public boolean exceedsBatchThreshold() 104 { 105 return batchCounter >= BATCH_THRESHOLD; 106 } 107 108 112 public void addBatch() 113 throws SQLException 114 { 115 batchCounter++; 116 if (logger.isLoggable(Logger.FINER)) { 117 logger.finer("sqlstore.sql.generator.dbstatement.addbatch", new Integer (batchCounter)); 119 } 120 preparedStmt.addBatch(); 121 } 122 123 127 public int[] executeBatch() 128 throws SQLException 129 { 130 if (logger.isLoggable(Logger.FINER)) { 131 logger.finer("sqlstore.sql.generator.dbstatement.executebatch", new Integer (batchCounter)); 133 } 134 batchCounter = 0; 135 return preparedStmt.executeBatch(); 136 } 137 138 142 public int executeUpdate() 143 throws SQLException 144 { 145 return preparedStmt.executeUpdate(); 146 } 147 148 152 public ResultSet executeQuery() 153 throws SQLException 154 { 155 return preparedStmt.executeQuery(); 156 } 157 158 162 public void close() 163 throws SQLException 164 { 165 if (preparedStmt != null) { 166 preparedStmt.close(); 167 } 168 } 169 170 186 public void bindInputColumn(int index, Object val, ColumnElement columnElement, DBVendorType vendorType) 187 throws SQLException 188 { 189 int sqlType = getSqlType(columnElement); 190 if (logger.isLoggable(Logger.FINER)) { 191 Object [] items = {new Integer (index),val,new Integer (sqlType)}; 192 logger.finer("sqlstore.sql.generator.dbstatement.bindinputcolumn", items); } 194 195 if (val == null) { 196 preparedStmt.setNull(index, sqlType); 199 } else { 200 if (val instanceof Number ) { 201 Number number = (Number ) val; 202 if (number instanceof Integer ) { 203 preparedStmt.setInt(index, number.intValue()); 204 } else if (number instanceof Long ) { 205 preparedStmt.setLong(index, number.longValue()); 206 } else if (number instanceof Short ) { 207 preparedStmt.setShort(index, number.shortValue()); 208 } else if (number instanceof Byte ) { 209 preparedStmt.setByte(index, number.byteValue()); 210 } else if (number instanceof Double ) { 211 preparedStmt.setDouble(index, number.doubleValue()); 212 } else if (number instanceof Float ) { 213 preparedStmt.setFloat(index, number.floatValue()); 214 } else if (number instanceof BigDecimal ) { 215 preparedStmt.setBigDecimal(index, (BigDecimal ) number); 216 } else if (number instanceof BigInteger ) { 217 preparedStmt.setBigDecimal(index, new BigDecimal ((BigInteger ) number)); 218 } 219 } else if (val instanceof String ) { 220 bindStringValue(index, (String )val, columnElement, vendorType); 221 } else if (val instanceof Boolean ) { 222 preparedStmt.setBoolean(index, ((Boolean ) val).booleanValue()); 223 } else if (val instanceof java.util.Date ) { 224 if (val instanceof java.sql.Date ) { 225 preparedStmt.setDate(index, (java.sql.Date ) val); 226 } else if (val instanceof Time) { 227 preparedStmt.setTime(index, (Time) val); 228 } else if (val instanceof Timestamp) { 229 preparedStmt.setTimestamp(index, (Timestamp) val); 230 } else { 231 Timestamp timestamp = new Timestamp(((java.util.Date ) val).getTime()); 232 preparedStmt.setTimestamp(index, timestamp); 233 } 234 } else if (val instanceof Character ) { 235 bindStringValue(index, val.toString(), columnElement, vendorType); 236 } else if (val instanceof byte[]) { 237 byte[] ba = (byte[]) val; 244 preparedStmt.setBinaryStream(index, new ByteArrayInputStream (ba), ba.length); 245 } else if (val instanceof Blob) { 246 preparedStmt.setBlob(index, (Blob) val); 247 } else if (val instanceof Clob) { 248 preparedStmt.setClob(index, (Clob) val); 249 } else { 250 preparedStmt.setObject(index, val); 251 } 252 } 253 } 254 255 264 private void bindStringValue(int index, String strVal, ColumnElement columnElement, DBVendorType vendorType) 265 throws SQLException { 266 267 int sqlType = getSqlType(columnElement); 268 if(LocalFieldDesc.isCharLobType(sqlType) ) { 269 preparedStmt.setCharacterStream(index, new StringReader (strVal), strVal.length()); 277 } else if(LocalFieldDesc.isFixedCharType(sqlType) ) { 278 vendorType.getSpecialDBOperation().bindFixedCharColumn(preparedStmt, index, 279 strVal, getLength(columnElement) ); 280 } else { 281 preparedStmt.setString(index, strVal); 282 } 283 } 284 285 private static int getSqlType(ColumnElement columnElement) { 286 return (columnElement != null) ? columnElement.getType() : Types.OTHER; 287 } 288 289 private static int getLength(ColumnElement columnElement) { 290 int length = -1; 291 if(columnElement != null) { 292 Integer l = columnElement.getLength(); 293 if(l != null) { 294 length = l.intValue(); 295 } 296 } 297 return length; 298 } 299 } 300 | Popular Tags |