Package com.ibatis.sqlmap.engine.type

Examples of com.ibatis.sqlmap.engine.type.TypeHandler


    Document doc = newDocument("parameter");
    Probe probe = ProbeFactory.getProbe(doc);

    String complexName = mapping.getColumnName();

    TypeHandler stringTypeHandler = typeHandlerFactory.getTypeHandler(String.class);
    if (complexName.indexOf('=') > -1) {
      // old 1.x style multiple params
      StringTokenizer parser = new StringTokenizer(complexName, "{}=, ", false);
      while (parser.hasMoreTokens()) {
        String propName = parser.nextToken();
        String colName = parser.nextToken();
        Object propValue = stringTypeHandler.getResult(rs, colName);
        probe.setObject(doc, propName, propValue.toString());
      }
    } else {
      // single param
      Object propValue = stringTypeHandler.getResult(rs, complexName);
      probe.setObject(doc, "value", propValue.toString());
    }

    return doc;
  }
View Full Code Here


      StringTokenizer parser = new StringTokenizer(complexName, "{}=, ", false);
      while (parser.hasMoreTokens()) {
        String propName = parser.nextToken();
        String colName = parser.nextToken();
        Class propType = PROBE.getPropertyTypeForSetter(parameterObject, propName);
        TypeHandler propTypeHandler = typeHandlerFactory.getTypeHandler(propType);
        Object propValue = propTypeHandler.getResult(rs, colName);
        PROBE.setObject(parameterObject, propName, propValue);
      }
    } else {
      // single param
      TypeHandler propTypeHandler = typeHandlerFactory.getTypeHandler(parameterType);
      if (propTypeHandler == null) {
        propTypeHandler = typeHandlerFactory.getUnkownTypeHandler();
      }
      parameterObject = propTypeHandler.getResult(rs, complexName);
    }

    return parameterObject;
  }
View Full Code Here

    return parameterObject;
  }

  protected Object getPrimitiveResultMappingValue(ResultSet rs, ResultMapping mapping) throws SQLException {
    Object value = null;
    TypeHandler typeHandler = mapping.getTypeHandler();
    if (typeHandler != null) {
      String columnName = mapping.getColumnName();
      int columnIndex = mapping.getColumnIndex();
      if (columnName == null) {
        value = typeHandler.getResult(rs, columnIndex);
      } else {
        value = typeHandler.getResult(rs, columnName);
      }
    } else {
      throw new SqlMapException("No type handler could be found to map the property '" + mapping.getPropertyName() + "' to the column '" + mapping.getColumnName() + "'.  One or both of the types, or the combination of types is not supported.");
    }
    return value;
View Full Code Here

    return value;
  }

  protected Object doNullMapping(Object value, ResultMapping mapping) throws SqlMapException {
      if ( value == null )  {
        TypeHandler typeHandler = mapping.getTypeHandler();
        if (typeHandler != null) {
            String nullValue = mapping.getNullValue();
            if ( nullValue != null )
              value = typeHandler.valueOf(nullValue);
            return value;
          } else {
            throw new SqlMapException("No type handler could be found to map the property '" + mapping.getPropertyName() + "' to the column '" + mapping.getColumnName() + "'.  One or both of the types, or the combination of types is not supported.");
          }
      }
View Full Code Here

  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 {
View Full Code Here

  public TypeHandler resolveTypeHandler(TypeHandlerFactory typeHandlerFactory, Class clazz, String propertyName, String javaType, String jdbcType) {
    return resolveTypeHandler(typeHandlerFactory, clazz, propertyName, javaType, jdbcType, false);
  }

  public TypeHandler resolveTypeHandler(TypeHandlerFactory typeHandlerFactory, Class clazz, String propertyName, String javaType, String jdbcType, boolean useSetterToResolve) {
    TypeHandler handler = null;
    if (clazz == null) {
      // Unknown
      handler = typeHandlerFactory.getUnkownTypeHandler();
    } else if (DomTypeMarker.class.isAssignableFrom(clazz)) {
      // DOM
View Full Code Here

        javaType = vars.typeHandlerFactory.resolveAlias(javaType);
        resultMap = applyNamespace( resultMap );

        vars.errorCtx.setObjectId(propertyName + " mapping of the " + vars.currentParameterMap.getId() + " parameter map");

        TypeHandler handler = null;
        if (callback != null) {
          vars.errorCtx.setMoreInfo("Check the parameter mapping typeHandler attribute '" + callback + "' (must be a TypeHandler or TypeHandlerCallback implementation).");
          try {
            Object impl = Resources.classForName(callback).newInstance();
            if (impl instanceof TypeHandlerCallback) {
View Full Code Here

        callback = vars.typeHandlerFactory.resolveAlias(callback);
        javaType = vars.typeHandlerFactory.resolveAlias(javaType);

        vars.errorCtx.setObjectId(propertyName + " mapping of the " + vars.currentResultMap.getId() + " result map");

        TypeHandler handler = null;
        if (callback != null) {
          vars.errorCtx.setMoreInfo("Check the result mapping typeHandler attribute '" + callback + "' (must be a TypeHandler or TypeHandlerCallback implementation).");
          try {
            Object impl = Resources.classForName(callback).newInstance();
            if (impl instanceof TypeHandlerCallback) {
              handler = new CustomTypeHandler((TypeHandlerCallback) impl);
            } else if (impl instanceof TypeHandler) {
              handler = (TypeHandler) impl;
            } else {
              throw new RuntimeException ("The class '"+callback+"' is not a valid implementation of TypeHandler or TypeHandlerCallback");
            }
          } catch (Exception e) {
            throw new RuntimeException("Error occurred during custom type handler configuration.  Cause: " + e, e);
          }
        } else {
          vars.errorCtx.setMoreInfo("Check the result mapping property type or name.");
          handler = resolveTypeHandler(vars.client.getDelegate().getTypeHandlerFactory(), vars.currentResultMap.getResultClass(), propertyName, javaType, jdbcType, true);
        }


        BasicResultMapping mapping = new BasicResultMapping();
        mapping.setPropertyName(propertyName);
        mapping.setColumnName(columnName);
        mapping.setJdbcTypeName(jdbcType);
        mapping.setTypeHandler(handler);
        mapping.setNullValue(nullValue);
        mapping.setStatementName(statementName);
        mapping.setNestedResultMapName(resultMapName);

        if (resultMapName != null && resultMapName.length() > 0) {
          vars.currentResultMap.addNestedResultMappings(mapping);
        }

        try {
          if (javaType != null && javaType.length() > 0) {
            mapping.setJavaType(Resources.classForName(javaType));
          }
        } catch (ClassNotFoundException e) {
          throw new RuntimeException("Error setting javaType on result mapping.  Cause: " + e);
        }

        if (columnIndex != null && columnIndex.length() > 0) {
          mapping.setColumnIndex(Integer.parseInt(columnIndex));
        } else {
          vars.resultMappingIndex++;
          mapping.setColumnIndex(vars.resultMappingIndex);
        }

        vars.resultMappingList.add(mapping);
      }
    });

    parser.addNodelet("/sqlMap/resultMap/discriminator/subMap", new Nodelet() {
      public void process(Node node) throws Exception {
        if (vars.discriminator == null) {
          throw new RuntimeException ("The discriminator is null, but somehow a subMap was reached.  This is a bug.");
        }
        Properties childAttributes = NodeletUtils.parseAttributes(node, vars.properties);
        String value = childAttributes.getProperty("value");
        String resultMap = childAttributes.getProperty("resultMap");
        vars.discriminator.addSubMap(value, applyNamespace(resultMap));
      }
    });

    parser.addNodelet("/sqlMap/resultMap/discriminator", new Nodelet() {
      public void process(Node node) throws Exception {
        Properties childAttributes = NodeletUtils.parseAttributes(node, vars.properties);
        String nullValue = childAttributes.getProperty("nullValue");
        String jdbcType = childAttributes.getProperty("jdbcType");
        String javaType = childAttributes.getProperty("javaType");
        String columnName = childAttributes.getProperty("column");
        String columnIndex = childAttributes.getProperty("columnIndex");
        String callback = childAttributes.getProperty("typeHandler");

        callback = vars.typeHandlerFactory.resolveAlias(callback);
        javaType = vars.typeHandlerFactory.resolveAlias(javaType);

        TypeHandler handler = null;
        if (callback != null) {
          vars.errorCtx.setMoreInfo("Check the result mapping typeHandler attribute '" + callback + "' (must be a TypeHandlerCallback implementation).");
          try {
            Object impl = Resources.classForName(callback).newInstance();
            if (impl instanceof TypeHandlerCallback) {
View Full Code Here

        result = ResultLoader.loadResult(client, statementName, parameterObject, targetType);

        String nullValue = mapping.getNullValue();
        if (result == null && nullValue != null) {
          TypeHandler typeHandler = typeHandlerFactory.getTypeHandler(targetType);
          if (typeHandler != null) {
            result = typeHandler.valueOf(nullValue);
          }
        }
      }
      return result;
    } catch (InstantiationException e) {
View Full Code Here

  }

  private Object preparePrimitiveParameterObject(ResultSet rs, BasicResultMapping mapping, Class parameterType) throws SQLException {
    Object parameterObject;
    TypeHandlerFactory typeHandlerFactory = getDelegate().getTypeHandlerFactory();
    TypeHandler th = typeHandlerFactory.getTypeHandler(parameterType);
    parameterObject = th.getResult(rs, mapping.getColumnName());
    return parameterObject;
  }
View Full Code Here

TOP

Related Classes of com.ibatis.sqlmap.engine.type.TypeHandler

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.