public void setText(String s) {
   // for some reason the antlr.CommonAST initialization routines force
   // this method to get called twice.  The first time with an empty string
   if (StringHelper.isNotEmpty(s)) {
     constantExpression = s;
     constantValue = ReflectHelper.getConstantValue(s);
     heuristicType = TypeFactory.heuristicType(constantValue.getClass().getName());
     super.setText(s);
   }
 }
예제 #2
0
  @SuppressWarnings({"unchecked", "RedundantCast"})
  private void extractParameterInfo(Map<String, Class> namedParameterTypeRedefinition) {
    if (!AbstractQueryImpl.class.isInstance(query)) {
      throw new IllegalStateException("Unknown query type for parameter extraction");
    }

    HashSet<Parameter<?>> parameters = new HashSet<Parameter<?>>();
    AbstractQueryImpl queryImpl = AbstractQueryImpl.class.cast(query);

    // extract named params
    for (String name : (Set<String>) queryImpl.getParameterMetadata().getNamedParameterNames()) {
      final NamedParameterDescriptor descriptor =
          queryImpl.getParameterMetadata().getNamedParameterDescriptor(name);
      Class javaType = namedParameterTypeRedefinition.get(name);
      if (javaType != null && mightNeedRedefinition(javaType)) {
        descriptor.resetExpectedType(TypeFactory.heuristicType(javaType.getName()));
      } else if (descriptor.getExpectedType() != null) {
        javaType = descriptor.getExpectedType().getReturnedClass();
      }
      final ParameterImpl parameter = new ParameterImpl(name, javaType);
      parameters.add(parameter);
      if (descriptor.isJpaStyle()) {
        if (jpaPositionalIndices == null) {
          jpaPositionalIndices = new HashSet<Integer>();
        }
        jpaPositionalIndices.add(Integer.valueOf(name));
      }
    }

    // extract positional parameters
    for (int i = 0, max = queryImpl.getParameterMetadata().getOrdinalParameterCount();
        i < max;
        i++) {
      final OrdinalParameterDescriptor descriptor =
          queryImpl.getParameterMetadata().getOrdinalParameterDescriptor(i + 1);
      ParameterImpl parameter =
          new ParameterImpl(
              i + 1,
              descriptor.getExpectedType() == null
                  ? null
                  : descriptor.getExpectedType().getReturnedClass());
      parameters.add(parameter);
      Integer position = descriptor.getOrdinalPosition();
      if (jpaPositionalIndices != null && jpaPositionalIndices.contains(position)) {
        log.warn(
            "Parameter position ["
                + position
                + "] occurred as both JPA and Hibernate positional parameter");
      }
    }

    this.parameters = java.util.Collections.unmodifiableSet(parameters);
  }
예제 #3
0
  /**
   * @param column
   * @param generatedIdentifier
   * @return
   */
  private String guessAndAlignType(
      Table table, Column column, Mapping mapping, boolean generatedIdentifier) {
    // TODO: this method mutates the column if the types does not match...not good.
    // maybe we should copy the column instead before calling this method.
    Integer sqlTypeCode = column.getSqlTypeCode();
    String location =
        "Table: "
            + Table.qualify(table.getCatalog(), table.getSchema(), table.getQuotedName())
            + " column: "
            + column.getQuotedName();
    if (sqlTypeCode == null) {
      throw new JDBCBinderException("sqltype is null for " + location);
    }

    String preferredHibernateType =
        revengStrategy.columnToHibernateTypeName(
            TableIdentifier.create(table),
            column.getName(),
            sqlTypeCode.intValue(),
            column.getLength(),
            column.getPrecision(),
            column.getScale(),
            column.isNullable(),
            generatedIdentifier);

    Type wantedType = TypeFactory.heuristicType(preferredHibernateType);

    if (wantedType != null) {
      int[] wantedSqlTypes = wantedType.sqlTypes(mapping);

      if (wantedSqlTypes.length > 1) {
        throw new JDBCBinderException(
            "The type "
                + preferredHibernateType
                + " found on "
                + location
                + " spans multiple columns. Only single column types allowed.");
      }

      int wantedSqlType = wantedSqlTypes[0];
      if (wantedSqlType != sqlTypeCode.intValue()) {
        log.debug(
            "Sql type mismatch for "
                + location
                + " between DB and wanted hibernate type. Sql type set to "
                + typeCodeName(sqlTypeCode.intValue())
                + " instead of "
                + typeCodeName(wantedSqlType));
        column.setSqlTypeCode(new Integer(wantedSqlType));
      }
    } else {
      log.debug(
          "No Hibernate type found for "
              + preferredHibernateType
              + ". Most likely cause is a missing UserType class.");
    }

    if (preferredHibernateType == null) {
      throw new JDBCBinderException(
          "Could not find javatype for " + typeCodeName(sqlTypeCode.intValue()));
    }

    return preferredHibernateType;
  }