Example usage for java.sql Types NUMERIC

List of usage examples for java.sql Types NUMERIC

Introduction

In this page you can find the example usage for java.sql Types NUMERIC.

Prototype

int NUMERIC

To view the source code for java.sql Types NUMERIC.

Click Source Link

Document

The constant in the Java programming language, sometimes referred to as a type code, that identifies the generic SQL type NUMERIC.

Usage

From source file:axiom.objectmodel.db.NodeManager.java

private void setStatementValues(PreparedStatement stmt, int stmtNumber, Property p, int columnType)
        throws SQLException {
    if (p.getValue() == null) {
        stmt.setNull(stmtNumber, columnType);
    } else {/*w w  w. java2  s .c  o m*/
        switch (columnType) {
        case Types.BIT:
        case Types.TINYINT:
        case Types.BIGINT:
        case Types.SMALLINT:
        case Types.INTEGER:
            stmt.setLong(stmtNumber, p.getIntegerValue());

            break;

        case Types.REAL:
        case Types.FLOAT:
        case Types.DOUBLE:
        case Types.NUMERIC:
        case Types.DECIMAL:
            stmt.setDouble(stmtNumber, p.getFloatValue());

            break;

        case Types.VARBINARY:
        case Types.BINARY:
        case Types.BLOB:
            stmt.setString(stmtNumber, p.getStringValue());

            break;

        case Types.LONGVARBINARY:
        case Types.LONGVARCHAR:
            try {
                stmt.setString(stmtNumber, p.getStringValue());
            } catch (SQLException x) {
                String str = p.getStringValue();
                Reader r = new StringReader(str);

                stmt.setCharacterStream(stmtNumber, r, str.length());
            }

            break;

        case Types.CLOB:
            String val = p.getStringValue();
            Reader isr = new StringReader(val);
            stmt.setCharacterStream(stmtNumber, isr, val.length());

            break;

        case Types.CHAR:
        case Types.VARCHAR:
        case Types.OTHER:
            stmt.setString(stmtNumber, p.getStringValue());

            break;

        case Types.DATE:
        case Types.TIME:
        case Types.TIMESTAMP:
            stmt.setTimestamp(stmtNumber, p.getTimestampValue());

            break;

        case Types.NULL:
            stmt.setNull(stmtNumber, 0);

            break;

        default:
            stmt.setString(stmtNumber, p.getStringValue());

            break;
        }
    }
}

From source file:com.flexive.ejb.beans.structure.AssignmentEngineBean.java

/**
 * Updates a property/* ww  w . ja  v  a2  s. co  m*/
 *
 * @param con  a valid and open connection
 * @param prop the instance of FxPropertyEdit to be changed
 * @return returns true if changes were made to the property
 * @throws FxApplicationException on errors
 */
private boolean updateProperty(Connection con, FxPropertyEdit prop) throws FxApplicationException {
    if (prop.isNew())
        throw new FxInvalidParameterException("ex.structure.property.update.new", prop.getName());
    boolean changes = false;
    boolean success = false;
    StringBuilder changesDesc = new StringBuilder(200);
    final FxEnvironment env = CacheAdmin.getEnvironment();
    FxProperty org = env.getProperty(prop.getId());
    PreparedStatement ps = null;

    try {
        if (!org.isSystemInternal() || FxContext.getUserTicket().isGlobalSupervisor()) {
            // change the multiplicity override prop
            if (org.mayOverrideBaseMultiplicity() != prop.mayOverrideBaseMultiplicity()) {
                if (!prop.mayOverrideBaseMultiplicity() && getPropertyInstanceCount(prop.getId()) > 0) {
                    final long minMult = getPropertyInstanceMultiplicity(con, org.getId(), true);
                    if (minMult > 0 && minMult < prop.getMultiplicity().getMin())
                        throw new FxUpdateException("ex.structure.modification.contentExists",
                                "minimumMultiplicity");
                    if (getPropertyInstanceMultiplicity(con, org.getId(), false) > prop.getMultiplicity()
                            .getMax())
                        throw new FxUpdateException("ex.structure.modification.contentExists",
                                "maximumMultiplicity");
                }
                ps = con.prepareStatement(
                        "UPDATE " + TBL_STRUCT_PROPERTIES + " SET MAYOVERRIDEMULT=? WHERE ID=?");
                ps.setBoolean(1, prop.mayOverrideBaseMultiplicity());
                ps.setLong(2, prop.getId());
                ps.executeUpdate();
                ps.close();
                if (changes)
                    changesDesc.append(',');
                changesDesc.append("mayOverrideMultiplicity=").append(prop.mayOverrideBaseMultiplicity());
                changes = true;
            }
            // change the props multiplicity
            if (org.getMultiplicity().getMin() != prop.getMultiplicity().getMin()
                    || org.getMultiplicity().getMax() != prop.getMultiplicity().getMax()) {
                if (!prop.mayOverrideBaseMultiplicity()) {
                    if (org.getMultiplicity().getMin() < prop.getMultiplicity().getMin()) {
                        for (FxPropertyAssignment pa : CacheAdmin.getEnvironment()
                                .getPropertyAssignments(prop.getId(), true))
                            checkChangePropertyAssignmentMinMultiplicity(con, pa, prop.getMultiplicity());
                    }
                    if (org.getMultiplicity().getMax() > prop.getMultiplicity().getMax()) {
                        if (getPropertyInstanceMultiplicity(con, org.getId(), false) > prop.getMultiplicity()
                                .getMax())
                            throw new FxUpdateException("ex.structure.modification.contentExists",
                                    "maximumMultiplicity");
                    }
                }
                ps = con.prepareStatement(
                        "UPDATE " + TBL_STRUCT_PROPERTIES + " SET DEFMINMULT=? ,DEFMAXMULT=? WHERE ID=?");
                ps.setInt(1, prop.getMultiplicity().getMin());
                ps.setInt(2, prop.getMultiplicity().getMax());
                ps.setLong(3, prop.getId());
                ps.executeUpdate();
                ps.close();
                if (changes)
                    changesDesc.append(',');
                changesDesc.append("multiplicity=").append(prop.getMultiplicity());
                changes = true;
            }
            //not supported yet
            if (!org.getName().equals(prop.getName())) {
                throw new FxUpdateException("ex.structure.modification.notSupported", "name");
                /*
                if (ps != null) ps.close();
                ps = con.prepareStatement("UPDATE " + TBL_STRUCT_PROPERTIES + " SET NAME=? WHERE ID=?");
                ps.setString(1, prop.getName());
                ps.setLong(2, prop.getId());
                ps.executeUpdate();
                if (changes)
                changesDesc.append(',');
                changesDesc.append("name=").append(prop.getName());
                changes = true;
                */
            }
            //may only change if there are no existing content instances that use this property already
            if (org.getDataType().getId() != prop.getDataType().getId()) {
                if (getPropertyInstanceCount(org.getId()) == 0) {
                    ps = con.prepareStatement("UPDATE " + TBL_STRUCT_PROPERTIES + " SET DATATYPE=? WHERE ID=?");
                    ps.setLong(1, prop.getDataType().getId());
                    ps.setLong(2, prop.getId());
                    ps.executeUpdate();
                    ps.close();
                    //FX-858: get all assignments for this property and re-flatten if possible to reflect data type changes
                    final List<FxPropertyAssignment> refAssignments = env
                            .getReferencingPropertyAssignments(prop.getId());
                    final FxFlatStorage fs = FxFlatStorageManager.getInstance();
                    List<FxPropertyAssignment> flattened = new ArrayList<FxPropertyAssignment>(
                            refAssignments.size());
                    for (FxPropertyAssignment refAssignment : refAssignments) {
                        if (refAssignment.isFlatStorageEntry()) {
                            fs.unflatten(con, refAssignment);
                            flattened.add(refAssignment);
                        }
                    }
                    if (flattened.size() > 0) {
                        try {
                            StructureLoader.reload(con);
                            final FxEnvironment envNew = CacheAdmin.getEnvironment();
                            boolean needReload = false;
                            for (FxPropertyAssignment ref : flattened) {
                                final FxPropertyAssignment paNew = (FxPropertyAssignment) envNew
                                        .getAssignment(ref.getId());
                                if (fs.isFlattenable(paNew)) {
                                    fs.flatten(con, fs.getDefaultStorage(), paNew);
                                    needReload = true;
                                }
                            }
                            if (needReload)
                                StructureLoader.reload(con);
                        } catch (FxCacheException e) {
                            EJBUtils.rollback(ctx);
                            throw new FxCreateException(e, "ex.cache", e.getMessage());
                        }
                    }
                    if (changes)
                        changesDesc.append(',');
                    changesDesc.append("dataType=").append(prop.getDataType().getName());
                    changes = true;
                } else
                    throw new FxUpdateException("ex.structure.modification.contentExists", "dataType");
            }
            //may only change if there are no existing content instances that use this property already
            if (org.getReferencedType() != null && prop.getReferencedType() != null
                    && org.getReferencedType().getId() != prop.getReferencedType().getId()
                    || org.hasReferencedType() != prop.hasReferencedType()) {
                if (getPropertyInstanceCount(org.getId()) == 0) {
                    if (prop.isDefaultValueSet() && (prop.getDefaultValue() instanceof FxReference)) {
                        //check if the type matches the instance
                        checkReferencedType(con, (FxReference) prop.getDefaultValue(),
                                prop.getReferencedType());
                        //check for referencing assignments
                        final List<FxPropertyAssignment> refAssignments = env
                                .getReferencingPropertyAssignments(prop.getId());
                        for (FxPropertyAssignment refAssignment : refAssignments) {
                            if (refAssignment.hasAssignmentDefaultValue()
                                    && refAssignment.getDefaultValue() instanceof FxReference)
                                checkReferencedType(con, (FxReference) refAssignment.getDefaultValue(),
                                        prop.getReferencedType());
                        }
                    }
                    ps = con.prepareStatement("UPDATE " + TBL_STRUCT_PROPERTIES + " SET REFTYPE=? WHERE ID=?");
                    ps.setLong(2, prop.getId());
                    if (prop.hasReferencedType()) {
                        ps.setLong(1, prop.getReferencedType().getId());
                    } else
                        ps.setNull(1, java.sql.Types.NUMERIC);
                    ps.executeUpdate();
                    ps.close();
                    if (changes)
                        changesDesc.append(',');
                    changesDesc.append("referencedType=").append(prop.getReferencedType());
                    changes = true;
                } else
                    throw new FxUpdateException("ex.structure.modification.contentExists", "referencedType");
            }
            // set fulltext indexing for the property
            if (org.isFulltextIndexed() != prop.isFulltextIndexed()) {
                ps = con.prepareStatement(
                        "UPDATE " + TBL_STRUCT_PROPERTIES + " SET ISFULLTEXTINDEXED=? WHERE ID=?");
                ps.setBoolean(1, prop.isFulltextIndexed());
                ps.setLong(2, prop.getId());
                ps.executeUpdate();
                ps.close();
                if (changes)
                    changesDesc.append(',');
                changesDesc.append("isFulltextIndexed=").append(prop.isFulltextIndexed());
                FulltextIndexer ft = StorageManager.getFulltextIndexer(con);
                if (prop.isFulltextIndexed())
                    ft.rebuildIndexForProperty(prop.getId());
                else
                    ft.removeIndexForProperty(prop.getId());
                changes = true;
            }
            // set ACL override flag
            if (org.mayOverrideACL() != prop.mayOverrideACL()) {
                ps = con.prepareStatement(
                        "UPDATE " + TBL_STRUCT_PROPERTIES + " SET MAYOVERRIDEACL=? WHERE ID=?");
                ps.setBoolean(1, prop.mayOverrideACL());
                ps.setLong(2, prop.getId());
                ps.executeUpdate();
                ps.close();
                if (changes)
                    changesDesc.append(',');
                changesDesc.append("mayOverrideACL=").append(prop.mayOverrideACL());
                changes = true;
            }
            //may only change if there are no existing content instances that use this property already
            if (org.getReferencedList() != null && prop.getReferencedList() != null
                    && org.getReferencedList().getId() != prop.getReferencedList().getId()
                    || org.hasReferencedList() != prop.hasReferencedList()) {
                if (getPropertyInstanceCount(org.getId()) == 0) {
                    ps = con.prepareStatement("UPDATE " + TBL_STRUCT_PROPERTIES + " SET REFLIST=? WHERE ID=?");
                    ps.setLong(2, prop.getId());
                    if (prop.hasReferencedList()) {
                        ps.setLong(1, prop.getReferencedList().getId());
                    } else
                        ps.setNull(1, java.sql.Types.NUMERIC);
                    ps.executeUpdate();
                    ps.close();
                    if (changes)
                        changesDesc.append(',');
                    changesDesc.append("referencedList=").append(prop.getReferencedList());
                    changes = true;
                } else
                    throw new FxUpdateException("ex.structure.modification.contentExists", "referencedList");
            }
            // set the unique mode
            if (org.getUniqueMode() != prop.getUniqueMode()) {
                boolean allowChange = getPropertyInstanceCount(org.getId()) == 0
                        || prop.getUniqueMode().equals(UniqueMode.None);
                if (!allowChange) {
                    boolean hasFlat = false;
                    for (FxPropertyAssignment pa : env.getPropertyAssignments(prop.getId(), true)) {
                        if (pa.isFlatStorageEntry()) {
                            hasFlat = true;
                            break;
                        }
                    }
                    if (!hasFlat) {
                        boolean check = true;
                        for (FxType type : env.getTypesForProperty(prop.getId())) {
                            check = StorageManager.getContentStorage(TypeStorageMode.Hierarchical)
                                    .uniqueConditionValid(con, prop.getUniqueMode(), prop, type.getId(), null);
                            if (!check)
                                break;
                        }
                        allowChange = check;
                    }
                }
                if (allowChange) {
                    ps = con.prepareStatement(
                            "UPDATE " + TBL_STRUCT_PROPERTIES + " SET UNIQUEMODE=? WHERE ID=?");
                    ps.setLong(1, prop.getUniqueMode().getId());
                    ps.setLong(2, prop.getId());
                    ps.executeUpdate();
                    ps.close();
                    if (changes)
                        changesDesc.append(',');
                    changesDesc.append("uniqueMode=").append(prop.getUniqueMode().getId());
                    changes = true;
                } else
                    throw new FxUpdateException("ex.structure.modification.contentExists", "uniqueMode");
            }
            // change the property's ACL
            if (org.getACL().getId() != prop.getACL().getId()) {
                ps = con.prepareStatement("UPDATE " + TBL_STRUCT_PROPERTIES + " SET ACL=? WHERE ID=?");
                ps.setLong(1, prop.getACL().getId());
                ps.setLong(2, prop.getId());
                ps.executeUpdate();
                ps.close();
                if (changes)
                    changesDesc.append(',');
                changesDesc.append("acl=").append(prop.getACL().getId());
                changes = true;
            }
            // change the prop's label
            if (org.getLabel() != null && !org.getLabel().equals(prop.getLabel())
                    || org.getLabel() == null && prop.getLabel() != null
                    || org.getHint() != null && !org.getHint().equals(prop.getHint())
                    || org.getHint() == null && prop.getHint() != null) {
                Database.storeFxString(new FxString[] { prop.getLabel(), prop.getHint() }, con,
                        TBL_STRUCT_PROPERTIES, new String[] { "DESCRIPTION", "HINT" }, "ID", prop.getId());
                if (changes)
                    changesDesc.append(',');
                changesDesc.append("label=").append(prop.getLabel()).append(',');
                changesDesc.append("hint=").append(prop.getHint()).append(',');
                changes = true;
            }
            // change the default value
            if (org.getDefaultValue() != null && !org.getDefaultValue().equals(prop.getDefaultValue())
                    || org.getDefaultValue() == null && prop.getDefaultValue() != null) {
                if (changes)
                    changesDesc.append(',');
                ps = con.prepareStatement(
                        "UPDATE " + TBL_STRUCT_PROPERTIES + " SET DEFAULT_VALUE=? WHERE ID=?");
                FxValue defValue = prop.getDefaultValue();
                if (defValue instanceof FxBinary) {
                    ContentStorage storage = StorageManager.getContentStorage(TypeStorageMode.Hierarchical);
                    storage.prepareBinary(con, (FxBinary) defValue);
                }
                if (prop.isDefaultValueSet() && (defValue instanceof FxReference)) {
                    //check if the type matches the instance
                    checkReferencedType(con, (FxReference) defValue, prop.getReferencedType());
                    //check for referencing assignments
                    final List<FxPropertyAssignment> refAssignments = env
                            .getReferencingPropertyAssignments(prop.getId());
                    for (FxPropertyAssignment refAssignment : refAssignments) {
                        if (refAssignment.hasAssignmentDefaultValue()
                                && refAssignment.getDefaultValue() instanceof FxReference)
                            checkReferencedType(con, (FxReference) refAssignment.getDefaultValue(),
                                    prop.getReferencedType());
                    }
                }
                final String _def = defValue == null || defValue.isEmpty() ? null
                        : ConversionEngine.getXStream().toXML(defValue);
                if (_def == null)
                    ps.setNull(1, java.sql.Types.VARCHAR);
                else
                    ps.setString(1, _def);
                ps.setLong(2, prop.getId());
                ps.executeUpdate();
                ps.close();
                changesDesc.append("defaultValue=").append(prop.getDefaultValue());
                changes = true;
            }
            //update SystemInternal flag, this is a one way function, so it can only be set, but not reset!!
            if (!org.isSystemInternal() && prop.isSystemInternal()) {
                if (FxContext.getUserTicket().isGlobalSupervisor()) {
                    ps = con.prepareStatement(
                            "UPDATE " + TBL_STRUCT_PROPERTIES + " SET SYSINTERNAL=? WHERE ID=?");
                    ps.setBoolean(1, prop.isSystemInternal());
                    ps.setLong(2, prop.getId());
                    ps.executeUpdate();
                    ps.close();
                    if (changes)
                        changesDesc.append(',');
                    changesDesc.append("systemInternal=").append(prop.isSystemInternal());
                    changes = true;
                } else
                    throw new FxUpdateException("ex.structure.modification.systemInternal.notGlobalSupervisor",
                            prop.getName());
            }
            if (org.isMultiLang() != prop.isMultiLang()) {
                if (getPropertyInstanceCount(org.getId()) > 0)
                    throw new FxUpdateException("ex.structure.modification.contentExists", "multiLang");
            }
        }
        if (updatePropertyOptions(con, prop)) {
            changesDesc.append(",options:");
            List<FxStructureOption> options = prop.getOptions();
            for (FxStructureOption option : options) {
                changesDesc.append(option.getKey()).append("=").append(option.getValue())
                        .append(" overridable=").append(option.isOverridable()).append(" isSet=")
                        .append(option.isSet());
            }
            changes = true;
        }

        if (changes)
            htracker.track("history.assignment.updateProperty", prop.getName(), prop.getId(),
                    changesDesc.toString());
        success = true;
    } catch (SQLException e) {
        EJBUtils.rollback(ctx);
        /*TODO: Determine if this must be checked
        if (Database.isUniqueConstraintViolation(e))
        throw new FxEntryExistsException("ex.structure.assignment.property.exists", prop.getAlias(), prop.getXPath());
        */
        throw new FxCreateException(LOG, e, "ex.db.sqlError", e.getMessage());
    } finally {
        Database.closeObjects(AssignmentEngineBean.class, null, ps);
        if (!success) {
            EJBUtils.rollback(ctx);
        }
    }
    return changes;
}

From source file:org.pentaho.jfreereport.wizard.utility.report.ReportGenerationUtility.java

/**
 * Get the String representing the format-type associated with the value in <param>fieldType</param>.
 * This method will return the appropriate type String based in the input <param>fieldType</param>.
 * See the documentation for getGroupMessageFieldFormatText() for more information.
 * //from  www  .  j a  v  a 2s  .  com
 * @param fieldType int one of the values in java.sql.Types. Currently the only values
 * that return non-null are Types.DATE and Types.NUMERIC
 * 
 * @return String null if fieldType is a value that does not support formatting,
 * otherwise a String specifying the type of the field.
 * 
 * @throws RuntimeException if fieldType is not recognized. Currently the only
 * recognized types are Types.DATE, Types.NUMBER, Types.VARCHAR.
 */
private static String getFormatTypeAsString(int fieldType) {
    switch (fieldType) {
    case Types.DATE:
        return "date"; //$NON-NLS-1$
    case Types.NUMERIC:
        return "number"; //$NON-NLS-1$
    case Types.VARCHAR:
        return null;
    default:
        // TODO sbarkdull, should be internationalized
        throw new RuntimeException("Invalid field type: " + "(" + String.valueOf(fieldType) + ")");
    }
}

From source file:org.apache.ddlutils.platform.PlatformImplBase.java

/**
 * This is the core method to retrieve a value for a column from a result set. Its  primary
 * purpose is to call the appropriate method on the result set, and to provide an extension
 * point where database-specific implementations can change this behavior.
 * /*w w  w  .  j a  v a 2 s.  c  o m*/
 * @param resultSet  The result set to extract the value from
 * @param columnName The name of the column; can be <code>null</code> in which case the
  *                   <code>columnIdx</code> will be used instead
  * @param columnIdx  The index of the column's value in the result set; is only used if
  *                   <code>columnName</code> is <code>null</code>
 * @param jdbcType   The jdbc type to extract
 * @return The value
 * @throws SQLException If an error occurred while accessing the result set
 */
protected Object extractColumnValue(ResultSet resultSet, String columnName, int columnIdx, int jdbcType)
        throws SQLException {
    boolean useIdx = (columnName == null);
    Object value;

    switch (jdbcType) {
    case Types.CHAR:
    case Types.VARCHAR:
    case Types.LONGVARCHAR:
        value = useIdx ? resultSet.getString(columnIdx) : resultSet.getString(columnName);
        break;
    case Types.NUMERIC:
    case Types.DECIMAL:
        value = useIdx ? resultSet.getBigDecimal(columnIdx) : resultSet.getBigDecimal(columnName);
        break;
    case Types.BIT:
    case Types.BOOLEAN:
        value = new Boolean(useIdx ? resultSet.getBoolean(columnIdx) : resultSet.getBoolean(columnName));
        break;
    case Types.TINYINT:
    case Types.SMALLINT:
    case Types.INTEGER:
        value = new Integer(useIdx ? resultSet.getInt(columnIdx) : resultSet.getInt(columnName));
        break;
    case Types.BIGINT:
        value = new Long(useIdx ? resultSet.getLong(columnIdx) : resultSet.getLong(columnName));
        break;
    case Types.REAL:
        value = new Float(useIdx ? resultSet.getFloat(columnIdx) : resultSet.getFloat(columnName));
        break;
    case Types.FLOAT:
    case Types.DOUBLE:
        value = new Double(useIdx ? resultSet.getDouble(columnIdx) : resultSet.getDouble(columnName));
        break;
    case Types.BINARY:
    case Types.VARBINARY:
    case Types.LONGVARBINARY:
        value = useIdx ? resultSet.getBytes(columnIdx) : resultSet.getBytes(columnName);
        break;
    case Types.DATE:
        value = useIdx ? resultSet.getDate(columnIdx) : resultSet.getDate(columnName);
        break;
    case Types.TIME:
        value = useIdx ? resultSet.getTime(columnIdx) : resultSet.getTime(columnName);
        break;
    case Types.TIMESTAMP:
        value = useIdx ? resultSet.getTimestamp(columnIdx) : resultSet.getTimestamp(columnName);
        break;
    case Types.CLOB:
        Clob clob = useIdx ? resultSet.getClob(columnIdx) : resultSet.getClob(columnName);

        if (clob == null) {
            value = null;
        } else {
            long length = clob.length();

            if (length > Integer.MAX_VALUE) {
                value = clob;
            } else if (length == 0) {
                // the javadoc is not clear about whether Clob.getSubString
                // can be used with a substring length of 0
                // thus we do the safe thing and handle it ourselves
                value = "";
            } else {
                value = clob.getSubString(1l, (int) length);
            }
        }
        break;
    case Types.BLOB:
        Blob blob = useIdx ? resultSet.getBlob(columnIdx) : resultSet.getBlob(columnName);

        if (blob == null) {
            value = null;
        } else {
            long length = blob.length();

            if (length > Integer.MAX_VALUE) {
                value = blob;
            } else if (length == 0) {
                // the javadoc is not clear about whether Blob.getBytes
                // can be used with for 0 bytes to be copied
                // thus we do the safe thing and handle it ourselves
                value = new byte[0];
            } else {
                value = blob.getBytes(1l, (int) length);
            }
        }
        break;
    case Types.ARRAY:
        value = useIdx ? resultSet.getArray(columnIdx) : resultSet.getArray(columnName);
        break;
    case Types.REF:
        value = useIdx ? resultSet.getRef(columnIdx) : resultSet.getRef(columnName);
        break;
    default:
        value = useIdx ? resultSet.getObject(columnIdx) : resultSet.getObject(columnName);
        break;
    }
    return resultSet.wasNull() ? null : value;
}

From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java

/**
 * Append SQL for the given numeric value to the buffer, casting as needed.
 *//*from   w  w w  .  j  a  va 2s.  c  om*/
protected void appendNumericCast(SQLBuffer buf, FilterValue val) {
    if (val.isConstant())
        appendCast(buf, val, Types.NUMERIC);
    else
        val.appendTo(buf);
}

From source file:com.mirth.connect.donkey.server.data.jdbc.JdbcDao.java

private static String sqlTypeToString(int sqlType) {
    switch (sqlType) {
    case Types.ARRAY:
        return "ARRAY";
    case Types.BIGINT:
        return "BIGINT";
    case Types.BINARY:
        return "BINARY";
    case Types.BIT:
        return "BIT";
    case Types.BLOB:
        return "BLOB";
    case Types.BOOLEAN:
        return "BOOLEAN";
    case Types.CHAR:
        return "CHAR";
    case Types.CLOB:
        return "CLOB";
    case Types.DATALINK:
        return "DATALINK";
    case Types.DATE:
        return "DATE";
    case Types.DECIMAL:
        return "DECIMAL";
    case Types.DISTINCT:
        return "DISTINCT";
    case Types.DOUBLE:
        return "DOUBLE";
    case Types.FLOAT:
        return "FLOAT";
    case Types.INTEGER:
        return "INTEGER";
    case Types.JAVA_OBJECT:
        return "JAVA_OBJECT";
    case Types.LONGNVARCHAR:
        return "LONGNVARCHAR";
    case Types.LONGVARBINARY:
        return "LONGVARBINARY";
    case Types.LONGVARCHAR:
        return "LONGVARCHAR";
    case Types.NCHAR:
        return "NCHAR";
    case Types.NCLOB:
        return "NCLOB";
    case Types.NULL:
        return "NULL";
    case Types.NUMERIC:
        return "NUMERIC";
    case Types.NVARCHAR:
        return "NVARCHAR";
    case Types.OTHER:
        return "OTHER";
    case Types.REAL:
        return "REAL";
    case Types.REF:
        return "REF";
    case Types.ROWID:
        return "ROWID";
    case Types.SMALLINT:
        return "SMALLINT";
    case Types.SQLXML:
        return "SQLXML";
    case Types.STRUCT:
        return "STRUCT";
    case Types.TIME:
        return "TIME";
    case Types.TIMESTAMP:
        return "TIMESTAMP";
    case Types.TINYINT:
        return "TINYINT";
    case Types.VARBINARY:
        return "VARBINARY";
    case Types.VARCHAR:
        return "VARCHAR";
    default:// ww w .  j  a v a 2s . com
        return "UNKNOWN";
    }
}

From source file:ProcessRequest.java

public JSONArray parseQueryResults(ResultSet rs, String table) throws SQLException, JSONException {
    JSONArray resultJSONArray = new JSONArray();
    ResultSetMetaData rsmd = rs.getMetaData();
    int columns = rsmd.getColumnCount();
    while (rs.next()) {
        JSONObject result = new JSONObject();
        JSONObject resultMeta = new JSONObject();
        resultMeta.put("table", table);
        result.put("metadata", resultMeta);
        for (int i = 1; i <= columns; i++) {
            //out.println("<td>"+rs.getString(i)+"</td>");
            int type = rsmd.getColumnType(i);
            //result.put(rsmd.getColumnName(i), rs.get)
            switch (type) {
            case Types.BIT:
                result.put(rsmd.getColumnName(i), rs.getBoolean(i));
                break;
            case Types.TINYINT:
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.SMALLINT:
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.INTEGER:
                //System.out.println(rsmd.getColumnName(i) + "  type: "+type);
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.BIGINT:
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.FLOAT:
                result.put(rsmd.getColumnName(i), rs.getFloat(i));
                break;
            case Types.REAL:
                result.put(rsmd.getColumnName(i), rs.getDouble(i));
                break;
            case Types.DOUBLE:
                result.put(rsmd.getColumnName(i), rs.getDouble(i));
                break;
            case Types.NUMERIC:
                result.put(rsmd.getColumnName(i), rs.getDouble(i));
                break;
            case Types.DECIMAL:
                result.put(rsmd.getColumnName(i), rs.getDouble(i));
                break;
            case Types.CHAR:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            case Types.VARCHAR:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            case Types.LONGVARCHAR:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            case Types.DATE: {
                java.util.Date date = rs.getDate(i);
                result.put(rsmd.getColumnName(i), date.getTime());
                break;
            }/*  www .  ja  v  a  2  s  . c o  m*/
            case Types.TIME: {
                java.util.Date date = rs.getDate(i);
                result.put(rsmd.getColumnName(i), date.getTime());
                break;
            }
            case Types.TIMESTAMP: {
                java.util.Date date = rs.getDate(i);
                result.put(rsmd.getColumnName(i), date.getTime());
                break;
            }
            case Types.BINARY:
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.VARBINARY:
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.LONGVARBINARY:
                result.put(rsmd.getColumnName(i), rs.getLong(i));
                break;
            case Types.NULL:
                result.put(rsmd.getColumnName(i), "");
                break;
            case Types.BOOLEAN:
                result.put(rsmd.getColumnName(i), rs.getBoolean(i));
                break;
            case Types.ROWID:
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.NCHAR:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            case Types.NVARCHAR:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            case Types.LONGNVARCHAR:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            case Types.SQLXML:
            case Types.NCLOB:
            case Types.DATALINK:
            case Types.REF:
            case Types.OTHER:
            case Types.JAVA_OBJECT:
            case Types.DISTINCT:
            case Types.STRUCT:
            case Types.ARRAY:
            case Types.BLOB:
            case Types.CLOB:
            default:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            }
        }
        //if(table.equals("Ticket"))
        //System.out.println(result.toString(5));
        resultJSONArray.put(result);
    }
    return resultJSONArray;
}

From source file:com.flexive.ejb.beans.structure.AssignmentEngineBean.java

/**
 * Creates a property assignment/* w ww .ja  v  a  2  s .com*/
 *
 * @param con a valid and open connection
 * @param sql an instance of StringBuilder
 * @param pa  an instance of FxPropertyAssignmentEdit to be persisted
 * @return the property assignmentId
 * @throws FxApplicationException on errors
 */
private long createPropertyAssignment(Connection con, StringBuilder sql, FxPropertyAssignmentEdit pa)
        throws FxApplicationException {
    if (!pa.isNew())
        throw new FxInvalidParameterException("ex.structure.assignment.create.existing", pa.getXPath());
    if (sql == null) {
        sql = new StringBuilder(1000);
    }
    PreparedStatement ps = null;
    long newAssignmentId;
    try {
        sql.setLength(0);
        sql.append("INSERT INTO ").append(TBL_STRUCT_ASSIGNMENTS).
        //               1  2     3       4       5       6       7       8   9     10     11   12          13
                append("(ID,ATYPE,ENABLED,TYPEDEF,MINMULT,MAXMULT,DEFMULT,POS,XPATH,XALIAS,BASE,PARENTGROUP,APROPERTY,"
                        +
                        //14 15      16          17
                        "ACL,DEFLANG,SYSINTERNAL,DEFAULT_VALUE)" + "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
        ps = con.prepareStatement(sql.toString());
        newAssignmentId = seq.getId(FxSystemSequencer.ASSIGNMENT);
        ps.setLong(1, newAssignmentId);
        ps.setInt(2, FxAssignment.TYPE_PROPERTY);
        ps.setBoolean(3, pa.isEnabled());
        ps.setLong(4, pa.getAssignedType().getId());
        ps.setInt(5, pa.getMultiplicity().getMin());
        ps.setInt(6, pa.getMultiplicity().getMax());
        ps.setInt(7, pa.getDefaultMultiplicity());
        int position = getValidPosition(con, sql, pa.getPosition(), pa.getAssignedType().getId(),
                pa.getParentGroupAssignment());
        ps.setInt(8, position);
        String XPath;
        if (!pa.getXPath().startsWith(pa.getAssignedType().getName()))
            XPath = XPathElement.buildXPath(false, pa.getAssignedType().getName(), pa.getXPath());
        else
            XPath = pa.getXPath();
        ps.setString(9, XPath);
        ps.setString(10, pa.getAlias());
        if (pa.getBaseAssignmentId() == FxAssignment.NO_BASE)
            ps.setNull(11, Types.NUMERIC);
        else
            ps.setLong(11, pa.getBaseAssignmentId());
        ps.setLong(12, pa.getParentGroupAssignment() == null ? FxAssignment.NO_PARENT
                : pa.getParentGroupAssignment().getId());
        ps.setLong(13, pa.getProperty().getId());
        ps.setLong(14, pa.getACL().getId());
        ps.setInt(15, pa.hasDefaultLanguage() ? (int) pa.getDefaultLanguage() : (int) FxLanguage.SYSTEM_ID);
        ps.setBoolean(16, pa.isSystemInternal());
        FxValue defValue = pa.getDefaultValue();
        if (defValue instanceof FxBinary) {
            ContentStorage storage = StorageManager.getContentStorage(pa.getAssignedType().getStorageMode());
            storage.prepareBinary(con, (FxBinary) defValue);
        }
        final String _def = defValue == null || defValue.isEmpty() ? null
                : ConversionEngine.getXStream().toXML(defValue);
        if (_def == null)
            ps.setNull(17, java.sql.Types.VARCHAR);
        else
            ps.setString(17, _def);
        ps.executeUpdate();
        ps.close();
        Database.storeFxString(new FxString[] { pa.getLabel(), pa.getHint() }, con, TBL_STRUCT_ASSIGNMENTS,
                new String[] { "DESCRIPTION", "HINT" }, "ID", newAssignmentId);
        htracker.track(pa.getAssignedType(), "history.assignment.createPropertyAssignment", XPath,
                pa.getAssignedType().getId(), pa.getAssignedType().getName(), pa.getProperty().getId(),
                pa.getProperty().getName());

        // FxStructureOption inheritance
        boolean isInheritedAssignment = FxSharedUtils.checkAssignmentInherited(pa);
        if (isInheritedAssignment) {
            // FxStructureOptions - retrieve only those with an activated "isInherited" flag
            final List<FxStructureOption> inheritedOpts = FxStructureOption.cloneOptions(pa.getOptions(), true);
            if (inheritedOpts.size() > 0) {
                storeOptions(con, TBL_STRUCT_PROPERTY_OPTIONS, "ID", pa.getProperty().getId(), newAssignmentId,
                        inheritedOpts);
            }
        } else {
            storeOptions(con, TBL_STRUCT_PROPERTY_OPTIONS, "ID", pa.getProperty().getId(), newAssignmentId,
                    pa.getOptions());
        }

        setAssignmentPosition(con, newAssignmentId, pa.getPosition());

        if (!pa.isSystemInternal()) {
            if (divisionConfig.isFlatStorageEnabled()
                    && divisionConfig.get(SystemParameters.FLATSTORAGE_AUTO)) {
                final FxFlatStorage fs = FxFlatStorageManager.getInstance();
                if (fs.isFlattenable(pa)) {
                    try {
                        StructureLoader.reload(con);
                    } catch (FxCacheException e) {
                        EJBUtils.rollback(ctx);
                        throw new FxCreateException(e, "ex.cache", e.getMessage());
                    }
                    fs.flatten(con, fs.getDefaultStorage(),
                            (FxPropertyAssignment) CacheAdmin.getEnvironment().getAssignment(newAssignmentId));
                }
            }

            //only need a reload and inheritance handling if the property is not system internal
            //since system internal properties are only created from the type engine we don't have to care
            try {
                StructureLoader.reloadAssignments(FxContext.get().getDivisionId());
            } catch (FxApplicationException e) {
                EJBUtils.rollback(ctx);
                throw new FxCreateException(e, "ex.cache", e.getMessage());
            }
            if (pa.getAssignedType().getId() != FxType.ROOT_ID)
                createInheritedAssignments(CacheAdmin.getEnvironment().getAssignment(newAssignmentId), con, sql,
                        pa.getAssignedType().getDerivedTypes());
        }
    } catch (SQLException e) {
        final boolean uniqueConstraintViolation = StorageManager.isUniqueConstraintViolation(e);
        if (!ctx.getRollbackOnly())
            EJBUtils.rollback(ctx);
        if (uniqueConstraintViolation)
            throw new FxEntryExistsException("ex.structure.assignment.property.exists", pa.getAlias(),
                    pa.getAssignedType().getName() + pa.getXPath());
        throw new FxCreateException(LOG, e, "ex.db.sqlError", e.getMessage());
    } finally {
        Database.closeObjects(AssignmentEngineBean.class, null, ps);
    }
    return newAssignmentId;
}

From source file:ProcessRequest.java

public void parseQueryResults(ResultSet rs, String table, OutputStream os, boolean append)
        throws SQLException, JSONException, IOException {
    //JSONArray resultJSONArray = new JSONArray();
    ResultSetMetaData rsmd = rs.getMetaData();
    int columns = rsmd.getColumnCount();
    rs.last();//from w  w w .java 2 s. co m
    int rows = rs.getRow();
    os.write(new String("total rows: " + rows).getBytes());
    rs.first();
    int rowCount = 0;
    while (rs.next()) {
        if (!rs.isFirst() || append) {
            os.write(new String(",\n").getBytes());
            os.write(new String("" + rowCount).getBytes());
        }
        if (rowCount >= 69)
            System.out.println("break point");
        rowCount++;
        JSONObject result = new JSONObject();
        JSONObject resultMeta = new JSONObject();
        resultMeta.put("table", table);
        result.put("metadata", resultMeta);
        for (int i = 1; i <= columns; i++) {
            //out.println("<td>"+rs.getString(i)+"</td>");
            int type = rsmd.getColumnType(i);
            //result.put(rsmd.getColumnName(i), rs.get)
            switch (type) {
            case Types.BIT:
                result.put(rsmd.getColumnName(i), rs.getBoolean(i));
                break;
            case Types.TINYINT:
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.SMALLINT:
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.INTEGER:
                //System.out.println(rsmd.getColumnName(i) + "  type: "+type);
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.BIGINT:
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.FLOAT:
                result.put(rsmd.getColumnName(i), rs.getFloat(i));
                break;
            case Types.REAL:
                result.put(rsmd.getColumnName(i), rs.getDouble(i));
                break;
            case Types.DOUBLE:
                result.put(rsmd.getColumnName(i), rs.getDouble(i));
                break;
            case Types.NUMERIC:
                result.put(rsmd.getColumnName(i), rs.getDouble(i));
                break;
            case Types.DECIMAL:
                result.put(rsmd.getColumnName(i), rs.getDouble(i));
                break;
            case Types.CHAR:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            case Types.VARCHAR:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            case Types.LONGVARCHAR:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            case Types.DATE: {
                java.util.Date date = rs.getDate(i);
                result.put(rsmd.getColumnName(i), date.getTime());
                break;
            }
            case Types.TIME: {
                java.util.Date date = rs.getDate(i);
                result.put(rsmd.getColumnName(i), date.getTime());
                break;
            }
            case Types.TIMESTAMP: {
                java.util.Date date = rs.getDate(i);
                result.put(rsmd.getColumnName(i), date.getTime());
                break;
            }
            case Types.BINARY:
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.VARBINARY:
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.LONGVARBINARY:
                result.put(rsmd.getColumnName(i), rs.getLong(i));
                break;
            case Types.NULL:
                result.put(rsmd.getColumnName(i), "");
                break;
            case Types.BOOLEAN:
                result.put(rsmd.getColumnName(i), rs.getBoolean(i));
                break;
            case Types.ROWID:
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.NCHAR:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            case Types.NVARCHAR:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            case Types.LONGNVARCHAR:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            case Types.SQLXML:
            case Types.NCLOB:
            case Types.DATALINK:
            case Types.REF:
            case Types.OTHER:
            case Types.JAVA_OBJECT:
            case Types.DISTINCT:
            case Types.STRUCT:
            case Types.ARRAY:
            case Types.BLOB:
            case Types.CLOB:
            default:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            }
        }
        //if(table.equals("Ticket"))
        //System.out.println(result.toString(5));
        //if(result.getInt("TicketNumber")==126868)
        //   System.out.println("break point");
        //resultJSONArray.put(result);
        os.write(result.toString(5).getBytes());
    }
    //return resultJSONArray;
}

From source file:com.flexive.core.storage.genericSQL.GenericHierarchicalStorage.java

/**
 * Update the main entry/*w  w  w  . j  av a  2 s .  c  om*/
 *
 * @param con     an open and valid connection
 * @param content content to create
 * @throws FxUpdateException on errors
 */
protected void updateMainEntry(Connection con, FxContent content) throws FxUpdateException {
    PreparedStatement ps = null;
    try {
        ps = con.prepareStatement(CONTENT_MAIN_UPDATE);
        ps.setLong(19, content.getPk().getId());
        ps.setInt(20, content.getPk().getVersion());
        ps.setLong(1, content.getTypeId());
        ps.setLong(2, content.getAclIds().size() > 1 ? ACL.NULL_ACL_ID : content.getAclIds().get(0));
        ps.setLong(3, content.getStepId());
        ps.setInt(4, content.getMaxVersion());
        ps.setInt(5, content.getLiveVersion());
        ps.setBoolean(6, content.isMaxVersion());
        ps.setBoolean(7, content.isLiveVersion());
        ps.setBoolean(8, content.isActive());
        ps.setInt(9, (int) content.getMainLanguage());
        if (content.isRelation()) {
            ps.setLong(10, content.getRelatedSource().getId());
            ps.setInt(11, content.getRelatedSource().getVersion());
            ps.setLong(12, content.getRelatedDestination().getId());
            ps.setInt(13, content.getRelatedDestination().getVersion());
            ps.setLong(14, content.getRelatedSourcePosition());
            ps.setLong(15, content.getRelatedDestinationPosition());
        } else {
            ps.setNull(10, java.sql.Types.NUMERIC);
            ps.setNull(11, java.sql.Types.NUMERIC);
            ps.setNull(12, java.sql.Types.NUMERIC);
            ps.setNull(13, java.sql.Types.NUMERIC);
            ps.setNull(14, java.sql.Types.NUMERIC);
            ps.setNull(15, java.sql.Types.NUMERIC);
        }

        if (content.isForceLifeCycle()) {
            ps.setLong(16, content.getValue(FxLargeNumber.class, "/MODIFIED_BY").getBestTranslation());
            ps.setLong(17, content.getValue(FxDateTime.class, "/MODIFIED_AT").getBestTranslation().getTime());
        } else {
            long userId = FxContext.getUserTicket().getUserId();

            ps.setLong(16, userId);
            ps.setLong(17, System.currentTimeMillis());
        }
        setGroupPositions(ps, content, 18);
        ps.executeUpdate();

        if (content.isForceLifeCycle()) {
            ps.close();
            // update created_at/created_by
            ps = con.prepareStatement(CONTENT_MAIN_UPDATE_CREATED_AT);
            ps.setLong(1, content.getValue(FxDateTime.class, "/CREATED_AT").getBestTranslation().getTime());
            ps.setLong(2, content.getValue(FxLargeNumber.class, "/CREATED_BY").getBestTranslation());
            ps.setLong(3, content.getPk().getId());
            ps.setInt(4, content.getPk().getVersion());
            ps.executeUpdate();
        }
        updateACLEntries(con, content, content.getPk(), false);

    } catch (SQLException e) {
        throw new FxUpdateException(LOG, e, "ex.db.sqlError", e.getMessage());
    } catch (FxCreateException e) {
        throw new FxUpdateException(e);
    } finally {
        Database.closeObjects(GenericHierarchicalStorage.class, ps);
    }
}