Java Code Examples for java.sql.JDBCType#valueOf()

The following examples show how to use java.sql.JDBCType#valueOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PPDaiDasProxyParser.java    From das with Apache License 2.0 6 votes vote down vote up
public PPDaiDasProxyParser(EntityMeta tableMeta) {
    if(tableMeta == null) {
        return;
    }
    this.tableMeta = tableMeta;
    if(tableMeta.isMapType()){
       return;
    } else {
        this.tableMeta = tableMeta;
        types = new JDBCType[tableMeta.getColumnTypes().size()];
        int i = 0;
        for (String t : tableMeta.getColumnTypes()) {
            types[i++] = JDBCType.valueOf(t);//.getVendorTypeNumber();
        }
    }
   /* if(tableMeta != null && !tableMeta.isMapType()*//* && tableMeta.getTableName() !=null*//*) {
        this.tableMeta = tableMeta;
        types = new JDBCType[tableMeta.getColumnTypes().size()];
        int i = 0;
        for (String t : tableMeta.getColumnTypes())
            types[i++] = JDBCType.valueOf(t);//.getVendorTypeNumber();
    }*/
}
 
Example 2
Source File: SchemaUtil.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Creates a {@link ResultSetFieldExtractor} for logical types. */
private static <InputT, BaseT> ResultSetFieldExtractor createLogicalTypeExtractor(
    final Schema.LogicalType<InputT, BaseT> fieldType) {
  String logicalTypeName = fieldType.getIdentifier();
  JDBCType underlyingType = JDBCType.valueOf(logicalTypeName);
  switch (underlyingType) {
    case DATE:
      return DATE_EXTRACTOR;
    case TIME:
      return TIME_EXTRACTOR;
    case TIMESTAMP_WITH_TIMEZONE:
      return TIMESTAMP_EXTRACTOR;
    default:
      ResultSetFieldExtractor extractor = createFieldExtractor(fieldType.getBaseType());
      return (rs, index) -> fieldType.toInputType((BaseT) extractor.extract(rs, index));
  }
}
 
Example 3
Source File: PPDaiDasProxyParser.java    From das with Apache License 2.0 5 votes vote down vote up
public PPDaiDasProxyParser(String appId, String logicDbName, EntityMeta tableMeta) {
    this.appId = appId;
    this.logicDbName = logicDbName;
    this.tableMeta = tableMeta;
    types = new JDBCType[tableMeta.getColumnTypes().size()];
    int i = 0;
    for (String t : tableMeta.getColumnTypes()) {
        types[i++] = JDBCType.valueOf(t);//.getVendorTypeNumber();
    }
}
 
Example 4
Source File: ColumnDefinitionSerializer.java    From das with Apache License 2.0 5 votes vote down vote up
@Override
public Segment deserialize(JsonObject jo) {
    TableDefinition td = getSerializeFactory().deserialize(jo.getAsJsonObject("table"));
    ColumnDefinition cd = new ColumnDefinition(td, jo.get("columnName").getAsString(), JDBCType.valueOf(jo.get("type").getAsInt()));
    writeField(cd, "alias", jo.get("alias").isJsonNull() ? Optional.empty() : Optional.<String>of(jo.get("alias").getAsString()));
    return cd;
}
 
Example 5
Source File: AbsDatabaseDriver.java    From linstor-server with GNU General Public License v3.0 5 votes vote down vote up
public List<String> getAsStringList(Column col) throws DatabaseException
{
    List<String> ret;
    try
    {
        Object value = get(col);
        if (value == null)
        {
            ret = null;
        }
        else
        if (col.getSqlType() == Types.VARCHAR)
        {
            ret = new ArrayList<>(OBJ_MAPPER.readValue((String) value, List.class));
        }
        else
        if (col.getSqlType() == Types.BLOB)
        {
            ret = new ArrayList<>(OBJ_MAPPER.readValue((byte[]) value, List.class));
        }
        else
        {
            throw new DatabaseException(
                "Failed to deserialize json array. No handler found for sql type: " +
                JDBCType.valueOf(col.getSqlType()) +
                " in table " + table.getName() + ", column " + col.getName()
            );
        }
    }
    catch (IOException exc)
    {
        throw new DatabaseException(
            "Failed to deserialize json array. Table: " + table.getName() + ", column: " + col.getName(),
            exc
        );
    }

    return ret;
}
 
Example 6
Source File: ColumnInfo.java    From generator with Apache License 2.0 5 votes vote down vote up
public ColumnInfo(String columnName, int type, String remarks, String tableRemarks, boolean isPrimaryKey) {
    this.columnName = columnName;
    this.type = JDBCType.valueOf(type);
    this.remarks = remarks;
    this.tableRemarks = tableRemarks;
    this.propertyName = StringUtil.columnName2PropertyName(columnName);
    this.isPrimaryKey = isPrimaryKey;
}
 
Example 7
Source File: DbMetaDataHelper.java    From syndesis with Apache License 2.0 5 votes vote down vote up
private static List<ColumnMetaData> convert(ResultSet resultSet) throws SQLException {
    List<ColumnMetaData> list = new ArrayList<>();
    Integer position = 0;
    while (resultSet.next()) {
        // the order in which the columns are read is significant for some databases
        // for certain combinations of Oracle Database / Oracle JDBC driver if we
        // try to fetch COLUMN_DEF column after IS_AUTOINCREMENT we get:
        //     java.sql.SQLException: Stream has already been closed
        // reason for this could like in the fact that the IS_AUTOINCREMENT column
        // is the last column the table metadata ResultSet has, and once we try to
        // read it we moved beyond the previous columns in the ResultSet coupled
        // with the fact that reading could be unbuffered or that on reaching the
        // end of the row data that bit of stream is closed automatically
        // this issue was reported in https://issues.jboss.org/browse/ENTESB-12159
        // against Oracle 12.1
        String name = resultSet.getString("COLUMN_NAME");
        JDBCType type = JDBCType.valueOf(resultSet.getInt("DATA_TYPE"));
        String columnDefString = resultSet.getString("COLUMN_DEF");
        String autoIncString = resultSet.getString("IS_AUTOINCREMENT");

        boolean autoIncrement = false;
        if ("YES".equalsIgnoreCase(autoIncString) ||
                (columnDefString != null && columnDefString.contains("nextval"))) {
            autoIncrement = true;
        }

        ColumnMetaData columnMetaData = new ColumnMetaData(name, type, position++, autoIncrement);
        list.add(columnMetaData);
    }
    return list;
}
 
Example 8
Source File: Column.java    From stategen with GNU Affero General Public License v3.0 4 votes vote down vote up
public JDBCType getJDBCType (){
    return JDBCType.valueOf(_sqlType);
}
 
Example 9
Source File: SQLEngine.java    From linstor-server with GNU General Public License v3.0 4 votes vote down vote up
<DATA> int setValues(
    Map<Column, ExceptionThrowingFunction<DATA, Object, AccessDeniedException>> setters,
    PreparedStatement stmt,
    int startIdxRef,
    DatabaseTable table,
    Predicate<Column> predicate,
    DATA data
)
    throws SQLException, DatabaseException, AccessDeniedException
{
    int idx = startIdxRef;
    for (Column col : table.values())
    {
        if (predicate.test(col))
        {
            Object obj = setters.get(col).accept(data);
            if (obj == null)
            {
                if (col.isNullable())
                {
                    switch (col.getSqlType())
                    {
                        case Types.BLOB:
                            stmt.setBytes(idx, (byte[]) obj);
                            break;
                        default:
                            stmt.setNull(idx, col.getSqlType());
                            break;
                    }
                }
                else
                {
                    throw new DatabaseException(
                        "Cannot persist null object to not null database column.",
                        null,
                        null,
                        null,
                        "Table: " + table.getName() + ", Column: " + col.getName()
                    );
                }
            }
            else
            {
                try
                {
                    switch (col.getSqlType())
                    {
                        case Types.BLOB:
                            stmt.setBytes(idx, (byte[]) obj);
                            break;
                        default:
                            stmt.setObject(idx, obj, col.getSqlType());
                            break;
                    }
                }
                catch (Exception exc)
                {
                    throw new LinStorDBRuntimeException(
                        "Could not set object '" + obj.toString() + "' of type " + obj.getClass().getSimpleName() +
                            " as SQL type: " + col.getSqlType() + " (" + JDBCType.valueOf(col.getSqlType()) +
                            ") for column " + table.getName() + "." + col.getName(),
                        exc
                    );
                }
            }
            ++idx;
        }
    }
    return idx;
}
 
Example 10
Source File: EasyOrmSqlBuilder.java    From hsweb-framework with Apache License 2.0 4 votes vote down vote up
private List<RDBColumnMetaData> createColumn(String prefix, String columnName, ResultMapping resultMapping) {
    List<RDBColumnMetaData> metaData = new ArrayList<>();
    if (resultMapping.getNestedQueryId() == null) {

        if (resultMapping.getNestedResultMapId() != null) {
            ResultMap nests = MybatisUtils.getResultMap(resultMapping.getNestedResultMapId());
            Set<ResultMapping> resultMappings = new HashSet<>(nests.getResultMappings());
            resultMappings.addAll(nests.getIdResultMappings());
            for (ResultMapping mapping : resultMappings) {
                metaData.addAll(createColumn(resultMapping.getProperty(),
                        org.springframework.util.StringUtils.hasText(resultMapping.getColumn())
                                ? resultMapping.getColumn()
                                : resultMapping.getProperty(),
                        mapping));
            }
            return metaData;
        }

        JDBCType jdbcType = JDBCType.VARCHAR;
        try {
            jdbcType = JDBCType.valueOf(resultMapping.getJdbcType().name());
        } catch (Exception e) {
            log.warn("can not parse jdbcType:{}", resultMapping.getJdbcType());
        }
        RDBColumnMetaData column = new RDBColumnMetaData();
        column.setJdbcType(jdbcType);
        column.setName(org.springframework.util.StringUtils.hasText(columnName)
                ? columnName.concat(".").concat(resultMapping.getColumn()) : resultMapping.getColumn());

        if (resultMapping.getTypeHandler() != null) {
            column.setProperty("typeHandler", resultMapping.getTypeHandler().getClass().getName());
        }
        if (!StringUtils.isNullOrEmpty(resultMapping.getProperty())) {
            column.setAlias(org.springframework.util.StringUtils.hasText(prefix)
                    ? prefix.concat(".").concat(resultMapping.getProperty()) : resultMapping.getProperty());

        }
        column.setJavaType(resultMapping.getJavaType());
        column.setProperty("resultMapping", resultMapping);
        metaData.add(column);
    }
    return metaData;
}
 
Example 11
Source File: JdbcUtil.java    From beam with Apache License 2.0 4 votes vote down vote up
/** PreparedStatementSetCaller for Schema Field Logical types. * */
static JdbcIO.PreparedStatementSetCaller getPreparedStatementSetCaller(
    Schema.FieldType fieldType) {
  switch (fieldType.getTypeName()) {
    case ARRAY:
    case ITERABLE:
      return (element, ps, i, fieldWithIndex) ->
          ps.setArray(
              i + 1,
              ps.getConnection()
                  .createArrayOf(
                      fieldType.getCollectionElementType().getTypeName().name(),
                      element.getArray(fieldWithIndex.getIndex()).toArray()));
    case LOGICAL_TYPE:
      {
        String logicalTypeName = fieldType.getLogicalType().getIdentifier();
        JDBCType jdbcType = JDBCType.valueOf(logicalTypeName);
        switch (jdbcType) {
          case DATE:
            return (element, ps, i, fieldWithIndex) ->
                ps.setDate(
                    i + 1,
                    new Date(
                        getDateOrTimeOnly(
                                element.getDateTime(fieldWithIndex.getIndex()).toDateTime(), true)
                            .getTime()
                            .getTime()));
          case TIME:
            return (element, ps, i, fieldWithIndex) ->
                ps.setTime(
                    i + 1,
                    new Time(
                        getDateOrTimeOnly(
                                element.getDateTime(fieldWithIndex.getIndex()).toDateTime(),
                                false)
                            .getTime()
                            .getTime()));
          case TIMESTAMP_WITH_TIMEZONE:
            return (element, ps, i, fieldWithIndex) -> {
              Calendar calendar =
                  withTimestampAndTimezone(
                      element.getDateTime(fieldWithIndex.getIndex()).toDateTime());
              ps.setTimestamp(i + 1, new Timestamp(calendar.getTime().getTime()), calendar);
            };
          default:
            return getPreparedStatementSetCaller(fieldType.getLogicalType().getBaseType());
        }
      }
    default:
      {
        if (typeNamePsSetCallerMap.containsKey(fieldType.getTypeName())) {
          return typeNamePsSetCallerMap.get(fieldType.getTypeName());
        } else {
          throw new RuntimeException(
              fieldType.getTypeName().name()
                  + " in schema is not supported while writing. Please provide statement and preparedStatementSetter");
        }
      }
  }
}