Search in sources :

Example 6 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.

the class TimestampRDBMSMapping method getTimestamp.

/**
 * Method to access a Timestamp from the ResultSet.
 * @param rs The ResultSet
 * @param param The parameter position in the ResultSet row.
 * @return The Timestamp object
 */
protected Timestamp getTimestamp(ResultSet rs, int param) {
    Timestamp value;
    Calendar cal = storeMgr.getCalendarForDateTimezone();
    try {
        // TODO pass the calendar to Oracle or HSQLDB makes it loses milliseconds
        if (cal != null) {
            value = rs.getTimestamp(param, cal);
        } else {
            value = rs.getTimestamp(param);
        }
    } catch (SQLException e) {
        try {
            String s = rs.getString(param);
            if (rs.wasNull()) {
                value = null;
            } else {
                // JDBC driver has returned something other than a java.sql.Timestamp so we convert it to a Timestamp using its string form.
                value = s == null ? null : TypeConversionHelper.stringToTimestamp(s, cal);
            }
        } catch (SQLException nestedEx) {
            throw new NucleusDataStoreException(Localiser.msg("055002", "Timestamp", "" + param, column, e.getMessage()), nestedEx);
        }
    }
    return value;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) SQLException(java.sql.SQLException) Calendar(java.util.Calendar) Timestamp(java.sql.Timestamp)

Example 7 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.

the class CharRDBMSMapping method getChar.

/**
 * Method to extract a character from the ResultSet at the specified position
 * @param rs The Result Set
 * @param param The parameter position
 * @return the character
 */
public char getChar(ResultSet rs, int param) {
    char value;
    try {
        String str = rs.getString(param);
        if (str == null) {
            return 0;
        }
        value = str.charAt(0);
    } catch (SQLException e) {
        throw new NucleusDataStoreException(Localiser.msg("055002", "char", "" + param, column, e.getMessage()), e);
    }
    return value;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) SQLException(java.sql.SQLException)

Example 8 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.

the class ClobRDBMSMapping method getObject.

public Object getObject(ResultSet rs, int param) {
    if (getDatastoreAdapter().supportsOption(DatastoreAdapter.CLOB_SET_USING_SETSTRING)) {
        return super.getObject(rs, param);
    }
    Object value;
    try {
        Clob clob = rs.getClob(param);
        if (!rs.wasNull()) {
            BufferedReader br = new BufferedReader(clob.getCharacterStream());
            try {
                int c;
                StringBuilder sb = new StringBuilder();
                while ((c = br.read()) != -1) {
                    sb.append((char) c);
                }
                value = sb.toString();
            } finally {
                br.close();
            }
        } else {
            value = null;
        }
    } catch (SQLException e) {
        throw new NucleusDataStoreException(Localiser.msg("055002", "Object", "" + param, column, e.getMessage()), e);
    } catch (IOException e) {
        throw new NucleusDataStoreException(Localiser.msg("055002", "Object", "" + param, column, e.getMessage()), e);
    }
    return value;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) SQLException(java.sql.SQLException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Clob(java.sql.Clob)

Example 9 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.

the class DecimalRDBMSMapping method getObject.

public Object getObject(ResultSet rs, int param) {
    Object value;
    try {
        if (getJavaTypeMapping().getJavaType().getName().equals(ClassNameConstants.JAVA_LANG_INTEGER)) {
            value = rs.getBigDecimal(param);
            value = value == null ? null : Integer.valueOf(((BigDecimal) value).toBigInteger().intValue());
        } else if (getJavaTypeMapping().getJavaType().getName().equals(ClassNameConstants.JAVA_LANG_LONG)) {
            value = rs.getBigDecimal(param);
            value = value == null ? null : Long.valueOf(((BigDecimal) value).toBigInteger().longValue());
        } else if (getJavaTypeMapping().getJavaType().getName().equals(ClassNameConstants.JAVA_MATH_BIGINTEGER)) {
            value = rs.getBigDecimal(param);
            value = value == null ? null : ((BigDecimal) value).toBigInteger();
        } else if (getJavaTypeMapping().getJavaType().getName().equals(ClassNameConstants.JAVA_MATH_BIGDECIMAL)) {
            value = rs.getBigDecimal(param);
        } else if (getJavaTypeMapping().getJavaType().getName().equals(ClassNameConstants.JAVA_LANG_FLOAT)) {
            double d = rs.getDouble(param);
            value = rs.wasNull() ? null : (float) d;
        } else if (getJavaTypeMapping().getJavaType().getName().equals(ClassNameConstants.JAVA_LANG_DOUBLE)) {
            double d = rs.getDouble(param);
            value = rs.wasNull() ? null : d;
        } else {
            int i = rs.getInt(param);
            value = rs.wasNull() ? null : Integer.valueOf(i);
        }
    } catch (SQLException e) {
        throw new NucleusDataStoreException(Localiser.msg("055002", "Object", "" + param, column, e.getMessage()), e);
    }
    return value;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) SQLException(java.sql.SQLException) BigDecimal(java.math.BigDecimal)

Example 10 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.

the class LongVarcharRDBMSMapping method getObject.

public Object getObject(ResultSet rs, int param) {
    Object value;
    try {
        String s = rs.getString(param);
        value = rs.wasNull() ? null : s;
    } catch (SQLException e) {
        throw new NucleusDataStoreException(Localiser.msg("055002", "Object", "" + param, column, e.getMessage()), e);
    }
    return value;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) SQLException(java.sql.SQLException)

Aggregations

NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)288 SQLException (java.sql.SQLException)185 ExecutionContext (org.datanucleus.ExecutionContext)139 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)131 PreparedStatement (java.sql.PreparedStatement)128 SQLController (org.datanucleus.store.rdbms.SQLController)126 ResultSet (java.sql.ResultSet)73 Iterator (java.util.Iterator)43 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)38 CollectionAddOperation (org.datanucleus.flush.CollectionAddOperation)34 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)33 ArrayList (java.util.ArrayList)32 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)30 Collection (java.util.Collection)27 CollectionRemoveOperation (org.datanucleus.flush.CollectionRemoveOperation)26 StatementClassMapping (org.datanucleus.store.rdbms.query.StatementClassMapping)24 List (java.util.List)23 ObjectProvider (org.datanucleus.state.ObjectProvider)22 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)22 MappedDatastoreException (org.datanucleus.store.rdbms.exceptions.MappedDatastoreException)21