FileDocCategorySizeDatePackage
DBStatement.javaAPI DocGlassfish v2 API12871Mon Jun 04 15:11:40 BST 2007com.sun.jdo.spi.persistence.support.sqlstore.sql.generator

DBStatement

public class DBStatement extends Object

Fields Summary
public static final String
BATCH_THRESHOLD_PROPERTY
Name of the batch threshold property.
private static final int
BATCH_THRESHOLD
Batch threshold. Set the value from the system property named by this class followed by "BATCH_THRESHOLD". Default is 100.
private PreparedStatement
preparedStmt
The wrapped PreparedStatement.
private int
batchCounter
Current number of batched commands.
private String
statementText
The SQL text.
private static com.sun.jdo.spi.persistence.utility.logging.Logger
logger
The logger
Constructors Summary
public DBStatement(Connection conn, String statementText, int timeout)
This constructor is used for batched updates.

param
conn the connection
param
statementText the statement text
param
timeout the query timeout


                              
          
         
    
        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 voidaddBatch()
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 voidbindInputColumn(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.

param
index the index
param
val the value
param
columnElement the columnElement corresponding to the parameter marker at specified index. This parameter will always contain correct value when called for sql statements corresponding to insert and update For select statements this parameter can be null if query compiler is not able to detect java field for a parameter or value passed to the query. Please see RetrieveDescImpl#addValueConstraint for more information
param
vendorType the vendor type
throws
SQLException thrown by setter methods on java.sql.PreparedStatement
see
com.sun.jdo.spi.persistence.support.sqlstore.sql.RetrieveDescImpl#addValueConstraint

        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 voidbindStringValue(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.

param
index the index
param
strVal the value
param
columnElement Descripion of the database column.
param
vendorType the vendor type
throws
SQLException thrown by setter methods on java.sql.PreparedStatement


        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 voidclose()
Delegates the close call to the PreparedStatement wrapped by this DBStatement.

        if (preparedStmt != null) {
            preparedStmt.close();
    }
    
public booleanexceedsBatchThreshold()
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.ResultSetexecuteQuery()
Delegates the executeQuery call to the PreparedStatement wrapped by this DBStatement.

        return preparedStmt.executeQuery();
    
public intexecuteUpdate()
Delegates the executeUpdate call to the PreparedStatement wrapped by this DBStatement.

        return preparedStmt.executeUpdate();
    
private static intgetLength(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.PreparedStatementgetPreparedStatement()
Returns the wrapped PreparedStatement.

        return preparedStmt;
    
private static intgetSqlType(org.netbeans.modules.dbschema.ColumnElement columnElement)

        return (columnElement != null) ? columnElement.getType() : Types.OTHER;
    
public java.lang.StringgetStatementText()
Returns the SQL text.

        return statementText;