Package org.springframework.jdbc.core.namedparam

Examples of org.springframework.jdbc.core.namedparam.MapSqlParameterSource


    SimpleJdbcCall adder = new SimpleJdbcCall(mockDataSource).withProcedureName("add_invoice");
    adder.declareParameters(new SqlParameter("amount", Types.INTEGER),
        new SqlParameter("custid", Types.INTEGER),
        new SqlOutParameter("newid", Types.INTEGER));
    Number newId = adder.executeObject(Number.class, new MapSqlParameterSource()
        .addValue("amount", amount)
        .addValue("custid", custid));
    assertEquals(4, newId.intValue());
  }
View Full Code Here


    ctrlResultSet.replay();
    replay();

    SimpleJdbcCall adder = new SimpleJdbcCall(mockDataSource).withProcedureName("add_invoice");
    Number newId = adder.executeObject(Number.class, new MapSqlParameterSource()
        .addValue("amount", amount)
        .addValue("custid", custid));
    assertEquals(4, newId.intValue());

    ctrlResultSet.verify();
View Full Code Here

    SimpleJdbcCall adder = new SimpleJdbcCall(mockDataSource).withFunctionName("add_invoice");
    adder.declareParameters(new SqlOutParameter("return", Types.INTEGER),
        new SqlParameter("amount", Types.INTEGER),
        new SqlParameter("custid", Types.INTEGER));
    Number newId = adder.executeFunction(Number.class, new MapSqlParameterSource()
        .addValue("amount", amount)
        .addValue("custid", custid));
    assertEquals(4, newId.intValue());
  }
View Full Code Here

    ctrlResultSet.replay();
    replay();

    SimpleJdbcCall adder = new SimpleJdbcCall(mockDataSource).withFunctionName("add_invoice");
    Number newId = adder.executeFunction(Number.class, new MapSqlParameterSource()
        .addValue("amount", amount)
        .addValue("custid", custid));
    assertEquals(4, newId.intValue());

    ctrlResultSet.verify();
View Full Code Here

    ctrlMetaDataResultSet.replay();
    ctrlColumnsResultSet.replay();
    replay();
   
    MapSqlParameterSource map = new MapSqlParameterSource();
    map.addValue("id", 1);
    map.addValue("name", "Sven");
    map.addValue("customersince", new Date());
    map.addValue("version", 0);   
    map.registerSqlType("customersince", Types.DATE);
    map.registerSqlType("version", Types.NUMERIC);

    context.setTableName(TABLE);
    context.processMetaData(mockDataSource, new ArrayList<String>(), new String[] {});

    List<Object> values = context.matchInParameterValuesWithInsertColumns(map);
View Full Code Here

    ctrlMetaDataResultSet.replay();
    ctrlColumnsResultSet.replay();
    replay();

    MapSqlParameterSource map = new MapSqlParameterSource();

    String[] keyCols = new String[] {"id"};

    context.setTableName(TABLE);
    context.processMetaData(mockDataSource, new ArrayList<String>(), keyCols);
View Full Code Here

        String.format("Failed to execute %s.%s with SQL=%s", this.getClass().getName(), task_, sql), ex_,
        DAOMessageID.DAO_FAILURE.getMessage());
  }

  protected MapSqlParameterSource prepParams(Object... args) {
    MapSqlParameterSource msp = new MapSqlParameterSource();
    for (Object arg : args) {
      if (null == arg) {
        continue;
      }
      if (arg instanceof Map) {
        msp.addValues((Map) arg);
        continue;
      }
      if (arg instanceof MapSqlParameterSource) {
        msp.addValues(((MapSqlParameterSource) arg).getValues());
        continue;
      }

      BeanPropertySqlParameterSource sps = new BeanPropertySqlParameterSource(arg);
      List<Field> fields = new ArrayList<Field>();
      for (Class<?> c = arg.getClass(); c != null; c = c.getSuperclass()) {
        if (c.equals(Object.class))
          continue;
        fields.addAll(Arrays.asList(c.getDeclaredFields()));
      }
      for (Field f : fields) {
        try {
          String fn = f.getName();
          if (msp.hasValue(fn)) {
            if (warnParamOverride) {
              warnParamOverride = false;
              logger.warn(String.format("Field with name=%s has "
                  + "been already mapped by another arg bean. Overriding! Next time will warn if DEBUG is enabled.", fn));
            } else {
              if (logger.isDebugEnabled()) {
                logger.warn(String.format("Field with name=%s has "
                  + "been already mapped by another arg bean. Overriding!", fn));
              }
            }
          }
          if (Enum.class.isAssignableFrom(f.getType())) {
            sps.registerSqlType(f.getName(), Types.VARCHAR);
          }

          msp.addValue(fn, sps.getValue(fn), sps.getSqlType(fn), sps.getTypeName(fn));
          logger.debug(String.format("prepared sql arg: name=%s, value=%s, type=%s", fn, sps.getValue(fn),
              sps.getTypeName(fn)));
        } catch (Exception e) {
          Throwables.propagate(e);
        }
View Full Code Here

  private void checkForHistory(String operation, String procName, Object ... args) {
    if (this instanceof DaoWithHistory) {
      DaoWithHistory dwh = (DaoWithHistory) this;
      if (!Strings.isNullOrEmpty(procName) && !procName.equals(PROC_NAME_HISTORIZE)) {
        try {
          MapSqlParameterSource params = prepParams(args);
          Object original = null;
          original = dwh.getRowForHistory(params);
          if (insertHistorySql == null) {
            String insertSql = dwh.getInsertSql();
            Pattern p1 = Pattern.compile("(?i)insert[^\\(]*\\(");
            Pattern p2 = Pattern.compile("(?i)values[^\\(]*\\(");
            Matcher m = p1.matcher(insertSql);
            if (!m.find()) {
              logger.warn("Could not find pattern \"INSERT INTO ... (\" in insert SQL: {}. Ignoring history.", insertSql);
              return;
            }
            int re = m.end();
            insertSql = insertSql.substring(0, re-1).replace("[\n\r]", "").trim() +"Hist (" +"histId, histOperation, histStamp, histUserId," +insertSql.substring(re);
            m = p2.matcher(insertSql);
            if (!m.find()) {
              logger.warn("Could not find pattern \"VALUES (\" in insert SQL: {}. Ignoring history.", insertSql);
              return;
            }
            re = m.end();
            insertSql = insertSql.substring(0, re) +":histId, :histOperation, CURRENT_TIMESTAMP, :histUserId," +insertSql.substring(re);
   
            insertHistorySql = insertSql;
          }
          params.addValue("histId",0);
          params.addValue("histOperation", operation);
          params.addValue("histStamp","");
          params.addValue("histUserId", (String)params.getValue("userId"));
          insert("__insertHistory", insertHistorySql, params, original);
        } catch (Throwable e) {
          logger.warn("Failed to insert history!", e);
        }
      }
View Full Code Here

        final ArrayList<MapSqlParameterSource> parameters = new ArrayList<MapSqlParameterSource>();
       
        // Copy record contents to sql parameter source
        for (Record r: records) {
            try {
                MapSqlParameterSource p = new MapSqlParameterSource() ;
               
                for (String f: placeholders) {
                    try {
                        if (! r.has(f) || r.get(f).getValue() == null) {
                            // Handle undefined or null-valued fields by simply adding null                           
                            p.addValue(placeholderMap.get(f), null);
                        } else {
                            if (typeMap.get(f).equals(java.sql.Types.NUMERIC)) {
                                try {
                                    p.addValue(placeholderMap.get(f), Long.valueOf(r.get(f).getValue()),
                                            java.sql.Types.NUMERIC);
                                } catch (NumberFormatException ex) {
                                    // Parse to big decimal as a fallback option
                                    p.addValue(placeholderMap.get(f), new java.math.BigDecimal(r.get(f).getValue()),
                                            java.sql.Types.NUMERIC);
                                }
                            }
                            else {
                                p.addValue(placeholderMap.get(f), r.get(f).getValue(), typeMap.get(f)) ;
                            }
                        }
                    } catch (NullPointerException ex) {
                        log.warn("Target SQL placeholder '" + f + "' refers to a nonexistent field.");
                        p.addValue(placeholderMap.get(f), null);
                    }
                }
               
                parameters.add(p);
            } catch (NumberFormatException ex) {
View Full Code Here

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserDetails userDetails = null;
        try {
            userDetails = jdbcTemplate.queryForObject(queriesProperties.getProperty("user.get.by.username"), new MapSqlParameterSource("username", username), new UserMapper());
        } catch (EmptyResultDataAccessException e) {
            throw new UsernameNotFoundException(String.format("user '%s' not found", username));
        }

        return userDetails;
View Full Code Here

TOP

Related Classes of org.springframework.jdbc.core.namedparam.MapSqlParameterSource

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.