com.github.drinkjava2.jdbpro.template
Class BasicSqlTemplate

java.lang.Object
  extended by com.github.drinkjava2.jdbpro.template.BasicSqlTemplate
All Implemented Interfaces:
SqlTemplateEngine

public class BasicSqlTemplate
extends Object
implements SqlTemplateEngine

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.

Since:
1.7.0
Author:
Yong Zhu

Nested Class Summary
static class BasicSqlTemplate.BasicSqlTemplateException
           
 
Field Summary
protected  Boolean allowColonAsDelimiter
          If set true, ${placeHolder} can write as :placeHolder
Default is true
protected  Boolean dollarKeyForDollarPlaceHolder
          If set true, for ${placeHolder} in template, should use put("$placeHolder",value) instead of use put("placeHolder",value)
Default is false
 
Constructor Summary
BasicSqlTemplate()
          Build a BasicSqlTemplate instance, default use #{} as delimiter, dollarKeyForDollarPlaceHolder is false, allow
BasicSqlTemplate(String startDelimiter, String endDelimiter, Boolean allowColonAsDelimiter, Boolean dollarKeyForDollarPlaceHolder)
           
 
Method Summary
static BasicSqlTemplate instance()
           
static boolean isEmpty(CharSequence cs)
           Checks if a CharSequence is empty ("") or null.
 PreparedSQL render(String sqlTemplate, Map<String,Object> paramMap, Object[] unbindParams)
          Render a SQL Template String and a Map instance into a PreparedSQL instance
static String substringAfter(String str, String separator)
           Gets the substring after the first occurrence of a separator.
static String substringBefore(String str, String separator)
           Gets the substring before the first occurrence of a separator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dollarKeyForDollarPlaceHolder

protected Boolean dollarKeyForDollarPlaceHolder
If set true, for ${placeHolder} in template, should use put("$placeHolder",value) instead of use put("placeHolder",value)
Default is false


allowColonAsDelimiter

protected Boolean allowColonAsDelimiter
If set true, ${placeHolder} can write as :placeHolder
Default is true

Constructor Detail

BasicSqlTemplate

public BasicSqlTemplate()
Build a BasicSqlTemplate instance, default use #{} as delimiter, dollarKeyForDollarPlaceHolder is false, allow


BasicSqlTemplate

public BasicSqlTemplate(String startDelimiter,
                        String endDelimiter,
                        Boolean allowColonAsDelimiter,
                        Boolean dollarKeyForDollarPlaceHolder)
Parameters:
startDelimiter - The start delimiter
endDelimiter - The end delimiter
allowColonAsDelimiter - If set true, write :placeHolder is equal to #{placeHolder}
dollarKeyForDollarPlaceHolder - If set true, ${placeHolder} should use put("$placeHolder",value) instead of use put("placeHolder",value)
Method Detail

instance

public static BasicSqlTemplate instance()
Returns:
A singleton instance of BasicSqlTemplate

render

public PreparedSQL render(String sqlTemplate,
                          Map<String,Object> paramMap,
                          Object[] unbindParams)
Description copied from interface: SqlTemplateEngine
Render a SQL Template String and a Map instance into a PreparedSQL instance

Specified by:
render in interface SqlTemplateEngine
Parameters:
sqlTemplate - A SQL Template String, or a SqlId used to locate the real SQL template String
paramMap - A Map instance, key is String type, value is Object type
unbindParams - Optional, some time un-binded parameter can make template SQL executed like normal SQL, i.e., use normal params to replace placeholder according apperance order
            For example: 
            Template "delete from #{tb}", use put("tb","users") method,      will get "delete from ?"
            Template "delete from ${tb}", use replace("tb","users") method,   will get "delete from users"
            Template "delete from #{tb}", use replace("tb","users") method,   will cause an Exception
            Template "delete from ${tb}", use put("tb","users") method,      will cause an Exception
 
            
This design is to avoid typing mistake cause a SQL injection security leak, when programmer use replace() or replace0() method, he will aware this is a String direct replace method, not a SQL parameter, SQL parameter always use "put" method.
Returns:
PreparedSQL instance

isEmpty

public static boolean isEmpty(CharSequence cs)

Checks if a CharSequence is empty ("") or null.

 StringUtils.isEmpty(null)      = true
 StringUtils.isEmpty("")        = true
 StringUtils.isEmpty(" ")       = false
 StringUtils.isEmpty("bob")     = false
 StringUtils.isEmpty("  bob  ") = false
 

NOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is available in isBlank().

Parameters:
cs - the CharSequence to check, may be null
Returns:
true if the CharSequence is empty or null

substringBefore

public static String substringBefore(String str,
                                     String separator)

Gets the substring before the first occurrence of a separator. The separator is not returned.

A null string input will return null. An empty ("") string input will return the empty string. A null separator will return the input string.

If nothing is found, the string input is returned.

 StringUtils.substringBefore(null, *)      = null
 StringUtils.substringBefore("", *)        = ""
 StringUtils.substringBefore("abc", "a")   = ""
 StringUtils.substringBefore("abcba", "b") = "a"
 StringUtils.substringBefore("abc", "c")   = "ab"
 StringUtils.substringBefore("abc", "d")   = "abc"
 StringUtils.substringBefore("abc", "")    = ""
 StringUtils.substringBefore("abc", null)  = "abc"
 

Parameters:
str - the String to get a substring from, may be null
separator - the String to search for, may be null
Returns:
the substring before the first occurrence of the separator, null if null String input

substringAfter

public static String substringAfter(String str,
                                    String separator)

Gets the substring after the first occurrence of a separator. The separator is not returned.

A null string input will return null. An empty ("") string input will return the empty string. A null separator will return the empty string if the input string is not null.

If nothing is found, the empty string is returned.

 StringUtils.substringAfter(null, *)      = null
 StringUtils.substringAfter("", *)        = ""
 StringUtils.substringAfter(*, null)      = ""
 StringUtils.substringAfter("abc", "a")   = "bc"
 StringUtils.substringAfter("abcba", "b") = "cba"
 StringUtils.substringAfter("abc", "c")   = ""
 StringUtils.substringAfter("abc", "d")   = ""
 StringUtils.substringAfter("abc", "")    = "abc"
 

Parameters:
str - the String to get a substring from, may be null
separator - the String to search for, may be null
Returns:
the substring after the first occurrence of the separator, null if null String input


Copyright © 2018. All rights reserved.