Search in sources :

Example 1 with SqlType

use of jodd.db.type.SqlType in project jodd by oblac.

the class DbQuery method setObject.

/**
	 * Sets object parameter in an advanced way.
	 * <p>
	 * First, it checks if object is <code>null</code> and invokes <code>setNull</code> if so.
	 * If object is not <code>null</code>, it tries to resolve {@link SqlType sql type} (by looking up
	 * or using provided class) and use it for setting data.
	 * If sql type is not found, default <code>setObject</code> is invoked.
	 */
@SuppressWarnings({ "unchecked" })
public void setObject(int index, Object value, Class<? extends SqlType> sqlTypeClass, int dbSqlType) {
    init();
    if (value == null) {
        setNull(index, Types.NULL);
        return;
    }
    SqlType sqlType;
    if (sqlTypeClass != null) {
        sqlType = SqlTypeManager.lookupSqlType(sqlTypeClass);
    } else {
        sqlType = SqlTypeManager.lookup(value.getClass());
    }
    try {
        if ((sqlType != null) && (dbSqlType != SqlType.DB_SQLTYPE_NOT_AVAILABLE)) {
            sqlType.storeValue(preparedStatement, index, value, dbSqlType);
        } else {
            DbUtil.setPreparedStatementObject(preparedStatement, index, value, dbSqlType);
        }
    } catch (SQLException sex) {
        throwSetParamError(index, sex);
    }
}
Also used : SQLException(java.sql.SQLException) SqlType(jodd.db.type.SqlType)

Example 2 with SqlType

use of jodd.db.type.SqlType in project jodd by oblac.

the class DefaultResultSetMapper method readColumnValue.

// ---------------------------------------------------------------- parse object
/**
	 * Reads column value from result set. Since this method may be called more then once for
	 * the same column, it caches column values.
	 */
@SuppressWarnings({ "unchecked" })
protected Object readColumnValue(int colNdx, Class destinationType, Class<? extends SqlType> sqlTypeClass, int columnDbSqlType) {
    if (colNdx != cachedColumnNdx) {
        try {
            SqlType sqlType;
            if (sqlTypeClass != null) {
                sqlType = SqlTypeManager.lookupSqlType(sqlTypeClass);
            } else {
                sqlType = SqlTypeManager.lookup(destinationType);
            }
            if (sqlType != null) {
                cachedColumnValue = sqlType.readValue(resultSet, colNdx + 1, destinationType, columnDbSqlType);
            } else {
                cachedColumnValue = resultSet.getObject(colNdx + 1);
                cachedColumnValue = TypeConverterManager.convertType(cachedColumnValue, destinationType);
            }
        } catch (SQLException sex) {
            throw new DbOomException(dbOomQuery, "Invalid value for column #" + (colNdx + 1), sex);
        }
        cachedColumnNdx = colNdx;
    }
    return cachedColumnValue;
}
Also used : DbOomException(jodd.db.oom.DbOomException) SQLException(java.sql.SQLException) SqlType(jodd.db.type.SqlType)

Aggregations

SQLException (java.sql.SQLException)2 SqlType (jodd.db.type.SqlType)2 DbOomException (jodd.db.oom.DbOomException)1