DBStatementpublic class DBStatement extends Object
Fields Summary |
---|
public static final String | BATCH_THRESHOLD_PROPERTYName of the batch threshold property. | private static final int | BATCH_THRESHOLDBatch threshold. Set the value from the system property named by
this class followed by "BATCH_THRESHOLD". Default is 100. | private PreparedStatement | preparedStmtThe wrapped PreparedStatement. | private int | batchCounterCurrent number of batched commands. | private String | statementTextThe SQL text. | private static com.sun.jdo.spi.persistence.utility.logging.Logger | loggerThe logger |
Constructors Summary |
---|
public DBStatement(Connection conn, String statementText, int timeout)This constructor is used for batched updates.
this.statementText = statementText;
preparedStmt = conn.prepareStatement(statementText);
// Set SELECT/INSERT/UPDATE/DELETE Statement timeout
if(timeout != -1) {
// Avoid calling setQueryTimeOut when user has specified it as -1
// This can be used as a mechanism to prevent calling setQueryTimeOut
// for drivers that do not support this call
// for example, look at bug 6561160
preparedStmt.setQueryTimeout(timeout);
}
|
Methods Summary |
---|
public void | addBatch()Increases the batch counter and delegates the addBatch call to the
PreparedStatement wrapped by this DBStatement.
batchCounter++;
if (logger.isLoggable(Logger.FINER)) {
logger.finer("sqlstore.sql.generator.dbstatement.addbatch", // NOI18N
new Integer(batchCounter));
}
preparedStmt.addBatch();
| public void | bindInputColumn(int index, java.lang.Object val, org.netbeans.modules.dbschema.ColumnElement columnElement, com.sun.jdo.spi.persistence.support.sqlstore.database.DBVendorType vendorType)Binds the specified value to the column corresponding with
the specified index reference.
int sqlType = getSqlType(columnElement);
if (logger.isLoggable(Logger.FINER)) {
Object[] items = {new Integer(index),val,new Integer(sqlType)};
logger.finer("sqlstore.sql.generator.dbstatement.bindinputcolumn", items); // NOI18N
}
if (val == null) {
//setNull is called only for insert and update statement to set a column
//to null value. We will always have valid sqlType in this case
preparedStmt.setNull(index, sqlType);
} else {
if (val instanceof Number) {
Number number = (Number) val;
if (number instanceof Integer) {
preparedStmt.setInt(index, number.intValue());
} else if (number instanceof Long) {
preparedStmt.setLong(index, number.longValue());
} else if (number instanceof Short) {
preparedStmt.setShort(index, number.shortValue());
} else if (number instanceof Byte) {
preparedStmt.setByte(index, number.byteValue());
} else if (number instanceof Double) {
preparedStmt.setDouble(index, number.doubleValue());
} else if (number instanceof Float) {
preparedStmt.setFloat(index, number.floatValue());
} else if (number instanceof BigDecimal) {
preparedStmt.setBigDecimal(index, (BigDecimal) number);
} else if (number instanceof BigInteger) {
preparedStmt.setBigDecimal(index, new BigDecimal((BigInteger) number));
}
} else if (val instanceof String) {
bindStringValue(index, (String)val, columnElement, vendorType);
} else if (val instanceof Boolean) {
preparedStmt.setBoolean(index, ((Boolean) val).booleanValue());
} else if (val instanceof java.util.Date) {
if (val instanceof java.sql.Date) {
preparedStmt.setDate(index, (java.sql.Date) val);
} else if (val instanceof Time) {
preparedStmt.setTime(index, (Time) val);
} else if (val instanceof Timestamp) {
preparedStmt.setTimestamp(index, (Timestamp) val);
} else {
Timestamp timestamp = new Timestamp(((java.util.Date) val).getTime());
preparedStmt.setTimestamp(index, timestamp);
}
} else if (val instanceof Character) {
bindStringValue(index, val.toString(), columnElement, vendorType);
} else if (val instanceof byte[]) {
//
// We use setBinaryStream() because of a limit on the maximum
// array size that can be bound using the
// PreparedStatement class setBytes() method on Oracle.
//
//preparedStmt.setBytes(index, (byte[]) val);
byte[] ba = (byte[]) val;
preparedStmt.setBinaryStream(index, new ByteArrayInputStream(ba), ba.length);
} else if (val instanceof Blob) {
preparedStmt.setBlob(index, (Blob) val);
} else if (val instanceof Clob) {
preparedStmt.setClob(index, (Clob) val);
} else {
preparedStmt.setObject(index, val);
}
}
| private void | bindStringValue(int index, java.lang.String strVal, org.netbeans.modules.dbschema.ColumnElement columnElement, com.sun.jdo.spi.persistence.support.sqlstore.database.DBVendorType vendorType)Binds the specified value to the column corresponding with
the specified index reference.
int sqlType = getSqlType(columnElement);
if(LocalFieldDesc.isCharLobType(sqlType) ) {
//Correct sqlType is passed for parameter markers which do not belong to where clause
//So for insert and update statement we can safely detect binding to character LOB here.
//
//For parameter markers belonging to where clause, we do not always receive correct sqlType.
//It is not allowed by any db to have a Character LOB type in where clause except
//for null comparison. Let the db report an error if user puts a field mapped
//to character LOB column in where clause.
preparedStmt.setCharacterStream(index, new StringReader(strVal), strVal.length());
} else if(LocalFieldDesc.isFixedCharType(sqlType) ) {
vendorType.getSpecialDBOperation().bindFixedCharColumn(preparedStmt, index,
strVal, getLength(columnElement) );
} else {
preparedStmt.setString(index, strVal);
}
| public void | close()Delegates the close call to the PreparedStatement wrapped by
this DBStatement.
if (preparedStmt != null) {
preparedStmt.close();
}
| public boolean | exceedsBatchThreshold()Checks whether the current number of batched commands exceeds the
batch threshold defined by {@link #BATCH_THRESHOLD}.
return batchCounter >= BATCH_THRESHOLD;
| public int[] | executeBatch()Delegates the executeBatch call to the PreparedStatement wrapped by
this DBStatement and resets the batch counter.
if (logger.isLoggable(Logger.FINER)) {
logger.finer("sqlstore.sql.generator.dbstatement.executebatch", // NOI18N
new Integer(batchCounter));
}
batchCounter = 0;
return preparedStmt.executeBatch();
| public java.sql.ResultSet | executeQuery()Delegates the executeQuery call to the PreparedStatement wrapped by
this DBStatement.
return preparedStmt.executeQuery();
| public int | executeUpdate()Delegates the executeUpdate call to the PreparedStatement wrapped by
this DBStatement.
return preparedStmt.executeUpdate();
| private static int | getLength(org.netbeans.modules.dbschema.ColumnElement columnElement)
int length = -1;
if(columnElement != null) {
Integer l = columnElement.getLength();
if(l != null) {
length = l.intValue();
}
}
return length;
| public java.sql.PreparedStatement | getPreparedStatement()Returns the wrapped PreparedStatement.
return preparedStmt;
| private static int | getSqlType(org.netbeans.modules.dbschema.ColumnElement columnElement)
return (columnElement != null) ? columnElement.getType() : Types.OTHER;
| public java.lang.String | getStatementText()Returns the SQL text.
return statementText;
|
|