java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 24 Dec 2013 11:54:58 +0100
branchv_0
changeset 51 6730214fab41
parent 50 074b81e5fa7c
child 54 53020d0bd2e4
permissions -rw-r--r--
log warning: Possibly missing parameters
     1 /**
     2  * SQL-DK
     3  * Copyright © 2013 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package info.globalcode.sql.dk;
    19 
    20 import static info.globalcode.sql.dk.Functions.notNull;
    21 import static info.globalcode.sql.dk.Functions.findByName;
    22 import java.sql.Connection;
    23 import java.sql.PreparedStatement;
    24 import java.sql.SQLException;
    25 import java.util.ArrayList;
    26 import java.util.List;
    27 import java.util.logging.Level;
    28 import java.util.logging.Logger;
    29 import java.util.regex.Matcher;
    30 import java.util.regex.Pattern;
    31 
    32 /**
    33  *
    34  * @author Ing. František Kučera (frantovo.cz)
    35  */
    36 public class SQLCommandNamed extends SQLCommand {
    37 
    38 	private static final Logger log = Logger.getLogger(SQLCommandNamed.class.getName());
    39 	private String namePrefix;
    40 	private String nameSuffix;
    41 	private List<NamedParameter> parameters;
    42 	private List<NamedParameter> parametersUsed = new ArrayList<>();
    43 	private StringBuilder updatedQuery;
    44 	private Pattern pattern;
    45 
    46 	public SQLCommandNamed(String query, List<NamedParameter> parameters, String namePrefix, String nameSuffix) {
    47 		super(query);
    48 		this.updatedQuery = new StringBuilder(query.length());
    49 		this.parameters = parameters;
    50 		this.namePrefix = namePrefix;
    51 		this.nameSuffix = nameSuffix;
    52 	}
    53 
    54 	@Override
    55 	public PreparedStatement prepareStatement(Connection c) throws SQLException {
    56 		buildPattern();
    57 		placeParametersAndUpdateQuery();
    58 		logPossiblyMissingParameters();
    59 		return c.prepareStatement(updatedQuery.toString());
    60 	}
    61 
    62 	@Override
    63 	public void parametrize(PreparedStatement ps) throws SQLException {
    64 		int i = 1;
    65 		for (Parameter p : notNull(parametersUsed)) {
    66 			ps.setObject(i++, p.getValue(), p.getType());
    67 		}
    68 	}
    69 
    70 	/**
    71 	 * Builds a regexp pattern that matches all parameter names (with prefix/suffix) and which has
    72 	 * one group: parameter name (without prefix/suffix)
    73 	 */
    74 	private void buildPattern() {
    75 		StringBuilder patternString = new StringBuilder();
    76 
    77 		patternString.append(Pattern.quote(namePrefix));
    78 		patternString.append("(");
    79 		for (int i = 0; i < parameters.size(); i++) {
    80 			patternString.append(Pattern.quote(parameters.get(i).getName()));
    81 			if (i < parameters.size() - 1) {
    82 				patternString.append("|");
    83 			}
    84 		}
    85 		patternString.append(")");
    86 		patternString.append(Pattern.quote(nameSuffix));
    87 
    88 		pattern = Pattern.compile(patternString.toString());
    89 	}
    90 
    91 	private void placeParametersAndUpdateQuery() throws SQLException {
    92 		final String originalQuery = getQuery();
    93 		Matcher m = pattern.matcher(originalQuery);
    94 
    95 		int lastPosition = 0;
    96 		while (m.find(lastPosition)) {
    97 			String name = m.group(1);
    98 
    99 			updatedQuery.append(originalQuery.substring(lastPosition, m.start()));
   100 			updatedQuery.append("?");
   101 
   102 			parametersUsed.add(findByName(parameters, name));
   103 
   104 			lastPosition = m.end();
   105 		}
   106 		updatedQuery.append(originalQuery.substring(lastPosition, originalQuery.length()));
   107 
   108 		for (NamedParameter definedParameter : parameters) {
   109 			if (findByName(parametersUsed, definedParameter.getName()) == null) {
   110 				throw new SQLException("Parameter „" + definedParameter.getName() + "“ is defined but not used in the query: „" + originalQuery + "“");
   111 			}
   112 		}
   113 	}
   114 
   115 	private void logPossiblyMissingParameters() {
   116 		Pattern p = Pattern.compile(Pattern.quote(namePrefix) + ".*?" + Pattern.quote(nameSuffix));
   117 		Matcher m = p.matcher(updatedQuery);
   118 		int lastPosition = 0;
   119 		while (m.find(lastPosition)) {
   120 
   121 			log.log(Level.WARNING, "Possibly missing parameter: {0}", m.group());
   122 			lastPosition = m.end();
   123 		}
   124 	}
   125 
   126 	@Override
   127 	public List<NamedParameter> getParameters() {
   128 		return parameters;
   129 	}
   130 }