Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2016 the original author or authors.
*
* 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 com.github.drinkjava2.jdbpro.template;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.github.drinkjava2.jdbpro.PreparedSQL;
/**
* BasicSqlTemplate is a simple implementation of SqlTemplateEngine. It allow
* use #{xxxx} format parameters in template, and replace ${xxxx} pieces
* directly. This is a thread safe class.
*
* @author Yong Zhu
* @since 1.7.0
*/
public class BasicSqlTemplate implements SqlTemplateEngine {
/**
* If set true, for ${placeHolder} in template, should use
* put("$placeHolder",value) instead of use put("placeHolder",value)
* Default is false
*/
protected Boolean dollarKeyForDollarPlaceHolder = false;
/**
* If set true, ${placeHolder} can write as :placeHolder
* Default is true
*/
protected Boolean allowColonAsDelimiter = true;
private String startDelimiter = "#{";
private String endDelimiter = "}";
private static final String DIRECT_REPLACE_START_DELIMITER = "${";
private static final String DIRECT_REPLACE_END_DELIMITER = "}";
/** A lazy initialization singleton pattern */
private static class InnerBasicSqlTemplate {
private InnerBasicSqlTemplate() {
}
private static final BasicSqlTemplate INSTANCE = new BasicSqlTemplate();
}
/** @return A singleton instance of BasicSqlTemplate */
public static BasicSqlTemplate instance() {
return InnerBasicSqlTemplate.INSTANCE;
}
/**
* Build a BasicSqlTemplate instance, default use #{} as delimiter,
* dollarKeyForDollarPlaceHolder is false, allow
*/
public BasicSqlTemplate() {
}
/**
* Build a BasicSqlTemplate instance by given startDelimiter and endDelimiter,
* startDelimiter should be 1 or 2 characters and endDelimiter should be 1
* character
*
* @param startDelimiter
* The start delimiter
* @param endDelimiter
* The end delimiter
*/
/**
* @param startDelimiter
* The start delimiter
* @param endDelimiter
* The end delimiter
* @param allowColonAsDelimiter
* If set true, write :placeHolder is equal to #{placeHolder}
* @param dollarKeyForDollarPlaceHolder
* If set true, ${placeHolder} should use put("$placeHolder",value)
* instead of use put("placeHolder",value)
*/
public BasicSqlTemplate(String startDelimiter, String endDelimiter, Boolean allowColonAsDelimiter,
Boolean dollarKeyForDollarPlaceHolder) {
if (isEmpty(startDelimiter) || isEmpty(endDelimiter) || startDelimiter.length() > 2
|| endDelimiter.length() != 1)
throw new BasicSqlTemplateException(
"BasicSqlTemplate only support startDelimiter has 1 or 2 characters and endDelimiter has 1 character");
this.startDelimiter = startDelimiter;
this.endDelimiter = endDelimiter;
this.allowColonAsDelimiter = allowColonAsDelimiter;
this.dollarKeyForDollarPlaceHolder = dollarKeyForDollarPlaceHolder;
}
private static boolean isParamChars(char c) {
return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || c == '_' || c == '.';
}
@Override
public PreparedSQL render(String sqlTemplate, Map paramMap, Object[] unbindParams) {
if (paramMap == null)
throw new BasicSqlTemplateException("In BasicSqlTemplate, paramMap can not be null");
if (!paramMap.isEmpty() && unbindParams != null && unbindParams.length > 0)
throw new BasicSqlTemplateException(
"Can not use paramMap or unbindParams at same time in BasicSqlTemplate.");
String newSql = sqlTemplate;
if (allowColonAsDelimiter)
newSql = translateColonToDelimiter(sqlTemplate);
return doRender(newSql, paramMap, unbindParams);
}
/**
* Render a template with
*
* @param template
* A SQL Template String
* @param paramMap
* A Map stored SQL parameters
* @param unbindedParams
* Optional, unbinded params,
* @return A PreparedSQL instance which filled SQL and Params
*/
private PreparedSQL doRender(String template, Map paramMap, Object[] unbindParams) {
if (template == null)
throw new NullPointerException("Template can not be null");
int unbindParamPos = 0;
List