示例#1
0
  public BoundSql getBoundSql(Object parameterObject) {
    BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    if (parameterMappings == null || parameterMappings.size() <= 0) {
      boundSql =
          new BoundSql(
              configuration,
              boundSql.getSql(),
              parameterMap.getParameterMappings(),
              parameterObject);
    }

    // check for nested result maps in parameter mappings (issue #30)
    for (ParameterMapping pm : boundSql.getParameterMappings()) {
      String rmId = pm.getResultMapId();
      if (rmId != null) {
        ResultMap rm = configuration.getResultMap(rmId);
        if (rm != null) {
          hasNestedResultMaps |= rm.hasNestedResultMaps();
        }
      }
    }

    return boundSql;
  }
 private ParameterMapping oldParseMapping(
     String token, Class parameterClass, TypeHandlerFactory typeHandlerFactory) {
   ParameterMapping mapping = new ParameterMapping();
   if (token.indexOf(PARAM_DELIM) > -1) {
     StringTokenizer paramParser = new StringTokenizer(token, PARAM_DELIM, true);
     int n1 = paramParser.countTokens();
     if (n1 == 3) {
       String name = paramParser.nextToken();
       paramParser.nextToken(); // ignore ":"
       String type = paramParser.nextToken();
       mapping.setPropertyName(name);
       mapping.setJdbcTypeName(type);
       TypeHandler handler;
       if (parameterClass == null) {
         handler = typeHandlerFactory.getUnkownTypeHandler();
       } else {
         handler = resolveTypeHandler(typeHandlerFactory, parameterClass, name, null, type);
       }
       mapping.setTypeHandler(handler);
       return mapping;
     } else if (n1 >= 5) {
       String name = paramParser.nextToken();
       paramParser.nextToken(); // ignore ":"
       String type = paramParser.nextToken();
       paramParser.nextToken(); // ignore ":"
       String nullValue = paramParser.nextToken();
       while (paramParser.hasMoreTokens()) {
         nullValue = nullValue + paramParser.nextToken();
       }
       mapping.setPropertyName(name);
       mapping.setJdbcTypeName(type);
       mapping.setNullValue(nullValue);
       TypeHandler handler;
       if (parameterClass == null) {
         handler = typeHandlerFactory.getUnkownTypeHandler();
       } else {
         handler = resolveTypeHandler(typeHandlerFactory, parameterClass, name, null, type);
       }
       mapping.setTypeHandler(handler);
       return mapping;
     } else {
       throw new SqlMapException("Incorrect inline parameter map format: " + token);
     }
   } else {
     mapping.setPropertyName(token);
     TypeHandler handler;
     if (parameterClass == null) {
       handler = typeHandlerFactory.getUnkownTypeHandler();
     } else {
       handler = resolveTypeHandler(typeHandlerFactory, parameterClass, token, null, null);
     }
     mapping.setTypeHandler(handler);
     return mapping;
   }
 }
 public List<Object> getParameters() {
   List<Object> params = new ArrayList<Object>();
   if (parameterMappings == null) {
     return params;
   }
   for (ParameterMapping parameterMapping : parameterMappings) {
     String property = parameterMapping.getProperty();
     Object value = getAdditionalParameter(property);
     params.add(value);
   }
   return params;
 }
 public void traverse(ASTVisitor visitor, ClassScope classScope) {
   if (visitor.visit(this, classScope)) {
     this.roleMethodSpec.traverse(visitor, this.scope);
     for (int idx = 0; idx < this.baseMethodSpecs.length; idx++) {
       this.baseMethodSpecs[idx].traverse(visitor, this.scope);
     }
     if (this.mappings != null) {
       for (int idy = 0; idy < this.mappings.length; idy++) {
         ParameterMapping mapping = this.mappings[idy];
         mapping.traverse(visitor, this.scope);
       }
     }
   }
   visitor.endVisit(this, classScope);
 }
示例#5
0
  /**
   * @param ps
   * @param parameters
   * @throws java.sql.SQLException
   */
  public void setParameters(
      StatementScope statementScope, PreparedStatement ps, Object[] parameters)
      throws SQLException {

    ErrorContext errorContext = statementScope.getErrorContext();
    errorContext.setActivity("applying a parameter map");
    errorContext.setObjectId(this.getId());
    errorContext.setResource(this.getResource());
    errorContext.setMoreInfo("Check the parameter map.");

    if (parameterMappings != null) {
      for (int i = 0; i < parameterMappings.length; i++) {
        ParameterMapping mapping = parameterMappings[i];
        errorContext.setMoreInfo(mapping.getErrorString());
        if (mapping.isInputAllowed()) {
          setParameter(ps, mapping, parameters, i);
        }
      }
    }
  }
示例#6
0
  protected void setParameter(
      PreparedStatement ps, ParameterMapping mapping, Object[] parameters, int i)
      throws SQLException {
    Object value = parameters[i];
    // Apply Null Value
    String nullValueString = mapping.getNullValue();
    if (nullValueString != null) {
      TypeHandler handler = mapping.getTypeHandler();
      if (handler.equals(value, nullValueString)) {
        value = null;
      }
    }

    // Set Parameter
    TypeHandler typeHandler = mapping.getTypeHandler();
    if (value != null) {
      typeHandler.setParameter(ps, i + 1, value, mapping.getJdbcTypeName());
    } else if (typeHandler instanceof CustomTypeHandler) {
      typeHandler.setParameter(ps, i + 1, value, mapping.getJdbcTypeName());
    } else {
      int jdbcType = mapping.getJdbcType();
      if (jdbcType != JdbcTypeRegistry.UNKNOWN_TYPE) {
        ps.setNull(i + 1, jdbcType);
      } else {
        ps.setNull(i + 1, Types.OTHER);
      }
    }
  }
  private ParameterMapping newParseMapping(
      String token, Class parameterClass, TypeHandlerFactory typeHandlerFactory) {
    ParameterMapping mapping = new ParameterMapping();

    // #propertyName,javaType=string,jdbcType=VARCHAR,mode=IN,nullValue=N/A,handler=string,numericScale=2#

    StringTokenizer paramParser = new StringTokenizer(token, "=,", false);
    mapping.setPropertyName(paramParser.nextToken());

    while (paramParser.hasMoreTokens()) {
      String field = paramParser.nextToken();
      if (paramParser.hasMoreTokens()) {
        String value = paramParser.nextToken();
        if ("javaType".equals(field)) {
          value = typeHandlerFactory.resolveAlias(value);
          mapping.setJavaTypeName(value);
        } else if ("jdbcType".equals(field)) {
          mapping.setJdbcTypeName(value);
        } else if ("mode".equals(field)) {
          mapping.setMode(value);
        } else if ("nullValue".equals(field)) {
          mapping.setNullValue(value);
        } else if ("handler".equals(field)) {
          try {
            value = typeHandlerFactory.resolveAlias(value);
            Object impl = Resources.instantiate(value);
            if (impl instanceof TypeHandlerCallback) {
              mapping.setTypeHandler(new CustomTypeHandler((TypeHandlerCallback) impl));
            } else if (impl instanceof TypeHandler) {
              mapping.setTypeHandler((TypeHandler) impl);
            } else {
              throw new SqlMapException(
                  "The class "
                      + value
                      + " is not a valid implementation of TypeHandler or TypeHandlerCallback");
            }
          } catch (Exception e) {
            throw new SqlMapException(
                "Error loading class specified by handler field in " + token + ".  Cause: " + e, e);
          }
        } else if ("numericScale".equals(field)) {
          try {
            Integer numericScale = Integer.valueOf(value);
            if (numericScale.intValue() < 0) {
              throw new SqlMapException(
                  "Value specified for numericScale must be greater than or equal to zero");
            }
            mapping.setNumericScale(numericScale);
          } catch (NumberFormatException e) {
            throw new SqlMapException("Value specified for numericScale is not a valid Integer");
          }
        } else {
          throw new SqlMapException(
              "Unrecognized parameter mapping field: '" + field + "' in " + token);
        }
      } else {
        throw new SqlMapException(
            "Incorrect inline parameter map format (missmatched name=value pairs): " + token);
      }
    }

    if (mapping.getTypeHandler() == null) {
      TypeHandler handler;
      if (parameterClass == null) {
        handler = typeHandlerFactory.getUnkownTypeHandler();
      } else {
        handler =
            resolveTypeHandler(
                typeHandlerFactory,
                parameterClass,
                mapping.getPropertyName(),
                mapping.getJavaTypeName(),
                mapping.getJdbcTypeName());
      }
      mapping.setTypeHandler(handler);
    }

    return mapping;
  }