All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.httprpc.sql.Parameters Maven / Gradle / Ivy

There is a newer version: 9.5
Show newest version
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.httprpc.sql;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Set;

/**
 * Class for simplifying execution of prepared statements.
 */
public class Parameters extends AbstractMap {
    private String sql;
    private LinkedList keys;

    private HashMap values = new HashMap<>();

    private static final int EOF = -1;

    private Parameters(String sql, LinkedList keys) {
        this.sql = sql;
        this.keys = keys;
    }

    /**
     * Returns the parsed SQL.
     *
     * @return
     * The parsed SQL.
     */
    public String getSQL() {
        return sql;
    }

    /**
     * Returns a parameter value.
     *
     * @param key
     * The parameter name.
     *
     * @return
     * The parameter value, or null if the parameter has not been set.
     */
    @Override
    public Object get(Object key) {
        return values.get(key);
    }

    /**
     * Sets a parameter value.
     *
     * @param key
     * The parameter name.
     *
     * @param value
     * The parameter value.
     */
    @Override
    public Object put(String key, Object value) {
        return values.put(key, value);
    }

    @Override
    public Set> entrySet() {
        return values.entrySet();
    }

    /**
     * Applies the provided argument values to a prepared statement.
     *
     * @param statement
     * The prepared statement.
     *
     * @throws SQLException
     * If an exception occurs while applying the argument values.
     */
    public void apply(PreparedStatement statement) throws SQLException {
        int i = 1;

        for (String key : keys) {
            statement.setObject(i++, values.get(key));
        }
    }

    /**
     * Parses a parameterized SQL statement.
     *
     * @param sql
     * A string containing the SQL to parse.
     *
     * @return
     * An {@link Parameters} instance containing the parsed SQL.
     */
    public static Parameters parse(String sql) {
        if (sql == null) {
            throw new IllegalArgumentException();
        }

        Parameters parameters;
        try (Reader sqlReader = new StringReader(sql)) {
            parameters = parse(sqlReader);
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }

        return parameters;
    }

    /**
     * Parses a parameterized SQL statement.
     *
     * @param sqlReader
     * A reader containing the SQL to parse.
     *
     * @return
     * An {@link Parameters} instance containing the parsed SQL.
     *
     * @throws IOException
     * If an exception occurs while reading the SQL statement.
     */
    public static Parameters parse(Reader sqlReader) throws IOException {
        if (sqlReader == null) {
            throw new IllegalArgumentException();
        }

        LinkedList keys = new LinkedList<>();

        StringBuilder sqlBuilder = new StringBuilder();

        int c = sqlReader.read();

        while (c != EOF) {
            if (c == ':') {
                c = sqlReader.read();

                StringBuilder keyBuilder = new StringBuilder();

                while (c != EOF && Character.isJavaIdentifierPart(c)) {
                    keyBuilder.append((char)c);

                    c = sqlReader.read();
                }

                keys.add(keyBuilder.toString());

                sqlBuilder.append("?");
            } else {
                sqlBuilder.append((char)c);

                c = sqlReader.read();
            }
        }

        return new Parameters(sqlBuilder.toString(), keys);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy