Example usage for org.springframework.jdbc.core.namedparam NamedParameterUtils parseSqlStatementIntoString

List of usage examples for org.springframework.jdbc.core.namedparam NamedParameterUtils parseSqlStatementIntoString

Introduction

In this page you can find the example usage for org.springframework.jdbc.core.namedparam NamedParameterUtils parseSqlStatementIntoString.

Prototype

public static String parseSqlStatementIntoString(String sql) 

Source Link

Document

Parse the SQL statement and locate any placeholders or named parameters.

Usage

From source file:com.weir.schedule.dao.SimpleJob.java

/**
 * <p> Called by the// w  w w .  j  a  v a2 s . c om
 * <code>{@link org.quartz.Scheduler}</code> when a
 * <code>{@link org.quartz.Trigger}</code> fires that is associated with the
 * <code>Job</code>. </p>
 *
 * @throws JobExecutionException if there is an exception while executing
 * the job.
 */
@Override
public void execute(JobExecutionContext jec) throws JobExecutionException {
    JobDataMap jdm = jec.getJobDetail().getJobDataMap();

    //        for (Entry<String, Object> et : jdm.entrySet()) {
    //            _log.debug("key:" + et.getKey() + "\tvalue:" + et.getValue());
    //        }

    if (!jdm.containsKey(ParamKey._conditionSQL)) {
        return;
    }
    if (!jdm.containsKey(ParamKey._sourceSQL)) {
        return;
    }
    if (!jdm.containsKey(ParamKey._targetSQL)) {
        return;
    }
    if (jdm.containsKey(ParamKey._isReverse)) {
        this.isReverse = Boolean.valueOf(jdm.getString(ParamKey._isReverse));
    }

    sourceSQL = jdm.getString(ParamKey._sourceSQL);
    Map<String, String> p = new HashMap<String, String>();
    //        p.put("name", "my name");
    p.put("name", "my name");
    //        ????????
    String sql = NamedParameterUtils.parseSqlStatementIntoString(sourceSQL);
    //        Map??????
    Object[] args = NamedParameterUtils.buildValueArray(sourceSQL, p);

    log.debug("++++++++++++++++++++++++++++++++++++++++++++++++");
    log.debug(sql);
    for (int i = 0; i < args.length; i++) {
        log.debug(args[i].toString());
    }
    log.debug("================================================");
}

From source file:org.tradex.jdbc.JDBCHelper.java

/**
 * Generates a SqlParameterSource for the passed SQL text and supplied binds
 * @param sql The SQL to bind to//from w  w w . j a  v  a 2 s.co  m
 * @param binds The supplied variables to bind
 * @return a SqlParameterSource
 */
public SqlParameterSource getBinds(String sql, final Object... binds) {
    final MapSqlParameterSource sqlParamSource = new MapSqlParameterSource();
    int[] parameterTypes = TYPE_CACHE.get(sql);
    if (parameterTypes == null) {
        synchronized (TYPE_CACHE) {
            parameterTypes = TYPE_CACHE.get(sql);
            if (parameterTypes == null) {
                Connection conn = null;
                PreparedStatement ps = null;
                try {
                    conn = ds.getConnection();
                    ps = conn.prepareStatement(NamedParameterUtils.parseSqlStatementIntoString(sql).toString());
                    ParameterMetaData pmd = ps.getParameterMetaData();
                    int paramCount = pmd.getParameterCount();
                    if (paramCount > 0 && (binds == null || binds.length != paramCount)) {
                        throw new RuntimeException("Bind Count [" + (binds == null ? 0 : binds.length)
                                + "] was not equal to parameter count [" + paramCount + "]");
                    }
                    parameterTypes = new int[paramCount];
                    for (int i = 0; i < paramCount; i++) {
                        parameterTypes[i] = pmd.getParameterType(i + 1);
                    }
                } catch (RuntimeException re) {
                    throw re;
                } catch (Exception e) {
                    throw new RuntimeException("Failed to get binds for [" + sql + "]", e);
                } finally {
                    try {
                        ps.close();
                    } catch (Exception e) {
                    }
                    try {
                        conn.close();
                    } catch (Exception e) {
                    }
                }

            }
            TYPE_CACHE.put(sql, parameterTypes);
        }
    }
    for (int i = 0; i < parameterTypes.length; i++) {
        sqlParamSource.addValue("" + i, binds[i], parameterTypes[i]);
    }
    return sqlParamSource;
}