001/* 002 * Copyright 2019 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.odf.ose.db; 017 018import java.util.ArrayList; 019import java.util.List; 020 021import org.ametys.odf.ose.db.parameter.ValuedQueryParameter; 022 023/** 024 * Object representing a query and its parameters. 025 */ 026public class ParameterizableQuery 027{ 028 private String _query; 029 private List<ValuedQueryParameter> _parameters; 030 031 /** 032 * Constructor. 033 * @param query The query 034 * @param parameters The query parameters 035 */ 036 public ParameterizableQuery(String query, List<ValuedQueryParameter> parameters) 037 { 038 this._query = query; 039 this._parameters = parameters; 040 } 041 042 /** 043 * Constructor. 044 * @param query The query 045 */ 046 public ParameterizableQuery(String query) 047 { 048 this(query, new ArrayList<>()); 049 } 050 051 /** 052 * Get the query 053 * @return the query 054 */ 055 public String getQuery() 056 { 057 return _query; 058 } 059 060 /** 061 * Get the query parameters 062 * @return the parameters 063 */ 064 public List<ValuedQueryParameter> getParameters() 065 { 066 return _parameters; 067 } 068 069 /** 070 * Gets the representation of this parameterizable query as a readable string for logging purposes. 071 * @return the representation of this parameterizable query as a readable string for logging purposes. 072 */ 073 public String toReadableString() 074 { 075 StringBuilder sb = new StringBuilder(); 076 // Set the query 077 sb.append("SQL query:"); 078 sb.append(System.lineSeparator()); 079 sb.append(getQuery()); 080 sb.append(System.lineSeparator()); 081 // Set the parameters 082 sb.append("Parameters:"); 083 for (ValuedQueryParameter param : getParameters()) 084 { 085 sb.append(System.lineSeparator()); 086 sb.append(" - "); 087 sb.append(param.getName()); 088 sb.append(" = "); 089 sb.append(param.getValue()); 090 } 091 return sb.toString(); 092 } 093}