|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
NamedParamSQL.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
* Joey and its relative products are published under the terms
|
|
3 |
* of the Apache Software License.
|
|
4 |
*/
|
|
5 |
package org.asyrinx.brownie.core.sql;
|
|
6 |
|
|
7 |
import java.lang.reflect.InvocationTargetException;
|
|
8 |
import java.util.ArrayList;
|
|
9 |
import java.util.HashMap;
|
|
10 |
import java.util.HashSet;
|
|
11 |
import java.util.Iterator;
|
|
12 |
import java.util.List;
|
|
13 |
import java.util.Map;
|
|
14 |
import java.util.Set;
|
|
15 |
|
|
16 |
import org.apache.commons.beanutils.PropertyUtils;
|
|
17 |
import org.asyrinx.brownie.core.lang.StringUtils;
|
|
18 |
|
|
19 |
/**
|
|
20 |
* @author Akima
|
|
21 |
*/
|
|
22 |
public class NamedParamSQL { |
|
23 |
|
|
24 |
/**
|
|
25 |
* Constructor for NamedParamSQL.
|
|
26 |
*/
|
|
27 | 0 |
public NamedParamSQL(String original) {
|
28 | 0 |
super();
|
29 | 0 |
this.original = original;
|
30 | 0 |
initQuotedParamClasses(); |
31 |
} |
|
32 |
|
|
33 |
protected final String original;
|
|
34 |
|
|
35 |
protected final Set quotedParamClasses = new HashSet(); |
|
36 |
|
|
37 |
protected List paramNames = null; |
|
38 |
|
|
39 |
private char quote = '\''; |
|
40 |
|
|
41 |
static public final char PARAM_DELIMETER = '$'; |
|
42 |
|
|
43 | 0 |
protected void initQuotedParamClasses() { |
44 | 0 |
quotedParamClasses.add(String.class);
|
45 | 0 |
quotedParamClasses.add(Character.class);
|
46 | 0 |
quotedParamClasses.add(Character[].class);
|
47 |
} |
|
48 |
|
|
49 | 0 |
protected Iterator getParamNameIterator() {
|
50 | 0 |
if (paramNames != null) |
51 | 0 |
return paramNames.iterator();
|
52 | 0 |
paramNames = new ArrayList();
|
53 | 0 |
StringUtils.extractStrings(original, paramNames, PARAM_DELIMETER); |
54 | 0 |
return paramNames.iterator();
|
55 |
} |
|
56 |
|
|
57 | 0 |
private Map getPreparingProps() {
|
58 | 0 |
Map result = new HashMap();
|
59 | 0 |
Iterator iterator = getParamNameIterator(); |
60 | 0 |
while (iterator.hasNext()) {
|
61 | 0 |
String paramName = (String) iterator.next(); |
62 | 0 |
result.put(paramName, "?");
|
63 |
} |
|
64 | 0 |
return result;
|
65 |
} |
|
66 |
|
|
67 |
/**
|
|
68 |
* Method toPreparable.
|
|
69 |
*
|
|
70 |
* @return String
|
|
71 |
*/
|
|
72 | 0 |
public String toPreparable() {
|
73 | 0 |
return toExecutable(getPreparingProps());
|
74 |
} |
|
75 |
|
|
76 |
/**
|
|
77 |
* Method toExecutable.
|
|
78 |
*
|
|
79 |
* @param value
|
|
80 |
* @return String
|
|
81 |
*/
|
|
82 | 0 |
public String toExecutable(Object value) {
|
83 | 0 |
if (value instanceof Map) { |
84 | 0 |
return toExecutableByMap((Map) value);
|
85 |
} else {
|
|
86 | 0 |
return toExecutableByBean(value);
|
87 |
} |
|
88 |
} |
|
89 |
|
|
90 |
/**
|
|
91 |
* Method toExecutable.
|
|
92 |
*
|
|
93 |
* @param props
|
|
94 |
* @return String
|
|
95 |
*/
|
|
96 | 0 |
protected String toExecutableByBean(Object bean) {
|
97 | 0 |
StringBuffer result = new StringBuffer(this.original); |
98 | 0 |
Iterator iterator = getParamNameIterator(); |
99 | 0 |
while (iterator.hasNext()) {
|
100 | 0 |
String paramName = (String) iterator.next(); |
101 | 0 |
Object param = null;
|
102 | 0 |
try {
|
103 | 0 |
param = PropertyUtils.getNestedProperty(bean, paramName); |
104 |
} catch (IllegalAccessException e) {
|
|
105 |
//ignore intensionally
|
|
106 |
} catch (InvocationTargetException e) {
|
|
107 |
//ignore intensionally
|
|
108 |
} catch (NoSuchMethodException e) {
|
|
109 |
//ignore intensionally
|
|
110 |
} |
|
111 | 0 |
if (param != null) |
112 | 0 |
StringUtils.replace(result, PARAM_DELIMETER + paramName |
113 |
+ PARAM_DELIMETER, getPropParamValue(param)); |
|
114 |
} |
|
115 | 0 |
return result.toString();
|
116 |
} |
|
117 |
|
|
118 |
/**
|
|
119 |
* Method toExecutable.
|
|
120 |
*
|
|
121 |
* @param props
|
|
122 |
* @return String
|
|
123 |
*/
|
|
124 | 0 |
protected String toExecutableByMap(Map props) {
|
125 | 0 |
StringBuffer result = new StringBuffer(this.original); |
126 | 0 |
Iterator iterator = getParamNameIterator(); |
127 | 0 |
while (iterator.hasNext()) {
|
128 | 0 |
String param = (String) iterator.next(); |
129 | 0 |
StringUtils.replace(result, PARAM_DELIMETER + param |
130 |
+ PARAM_DELIMETER, getPropParamValue(props.get(param))); |
|
131 |
} |
|
132 | 0 |
return result.toString();
|
133 |
} |
|
134 |
|
|
135 | 0 |
private String getPropParamValue(Object value) {
|
136 | 0 |
if (value == null) |
137 | 0 |
return StringUtils.NULL_STRING;
|
138 | 0 |
if (quotedParamClasses.contains(value.getClass())) {
|
139 | 0 |
return quote + String.valueOf(value) + quote;
|
140 |
} else {
|
|
141 | 0 |
return String.valueOf(value);
|
142 |
} |
|
143 |
} |
|
144 |
|
|
145 |
/**
|
|
146 |
* Returns the quotedParamClasses.
|
|
147 |
*
|
|
148 |
* @return Set
|
|
149 |
*/
|
|
150 | 0 |
public Set getQuotedParamClasses() {
|
151 | 0 |
return quotedParamClasses;
|
152 |
} |
|
153 |
|
|
154 |
/**
|
|
155 |
* Returns the original.
|
|
156 |
*
|
|
157 |
* @return String
|
|
158 |
*/
|
|
159 | 0 |
public String getOriginal() {
|
160 | 0 |
return original;
|
161 |
} |
|
162 |
|
|
163 |
/**
|
|
164 |
* Returns the quote.
|
|
165 |
*
|
|
166 |
* @return char
|
|
167 |
*/
|
|
168 | 0 |
public char getQuote() { |
169 | 0 |
return quote;
|
170 |
} |
|
171 |
|
|
172 |
/**
|
|
173 |
* Sets the quote.
|
|
174 |
*
|
|
175 |
* @param quote
|
|
176 |
* The quote to set
|
|
177 |
*/
|
|
178 | 0 |
public void setQuote(char quote) { |
179 | 0 |
this.quote = quote;
|
180 |
} |
|
181 |
|
|
182 |
} |
|