|
|||||||||||||||||||
| 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 | |||||||||||||||
| SqlReplacer.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 |
/*
|
|
| 6 |
* Created on 2004/02/15
|
|
| 7 |
*/
|
|
| 8 |
package org.asyrinx.brownie.core.sql;
|
|
| 9 |
|
|
| 10 |
import java.sql.Time;
|
|
| 11 |
import java.sql.Timestamp;
|
|
| 12 |
import java.text.DateFormat;
|
|
| 13 |
import java.text.SimpleDateFormat;
|
|
| 14 |
import java.util.ArrayList;
|
|
| 15 |
import java.util.Collections;
|
|
| 16 |
import java.util.Date;
|
|
| 17 |
import java.util.List;
|
|
| 18 |
import java.util.Set;
|
|
| 19 |
|
|
| 20 |
import org.asyrinx.brownie.core.collection.IntegerKeyMap;
|
|
| 21 |
import org.asyrinx.brownie.core.collection.StringKeyMap;
|
|
| 22 |
|
|
| 23 |
/**
|
|
| 24 |
* PreparedStatementで使用されるSQLの?を置換し、実行可能なSQLに変換するクラスです。 <br>
|
|
| 25 |
* 基本的に?を該当する値に変換するだけですが、値のクラスによって以下の処理を行います。 <br>
|
|
| 26 |
* 文字列(Stringオブジェクト)はquoteプロパティで指定された文字で囲まれます。 <br>
|
|
| 27 |
* 日付(Dateオブジェクト)はformatForDateプロパティで指定されたフォーマットで文字列に変換され、quoteプロパティで指定された文字で囲まれます。
|
|
| 28 |
* <br>
|
|
| 29 |
* 時刻(Timeオブジェクト)はformatForTimeプロパティで指定されたフォーマットで文字列に変換され、quoteプロパティで指定された文字で囲まれます。
|
|
| 30 |
* <br>
|
|
| 31 |
* TimestampオブジェクトはformatForTimestampプロパティで指定されたフォーマットで文字列に変換され、quoteプロパティで指定された文字で囲まれます。
|
|
| 32 |
* <br>
|
|
| 33 |
*
|
|
| 34 |
* @author akima
|
|
| 35 |
*/
|
|
| 36 |
public class SqlReplacer { |
|
| 37 |
|
|
| 38 |
/**
|
|
| 39 |
*
|
|
| 40 |
*/
|
|
| 41 | 0 |
public SqlReplacer() {
|
| 42 | 0 |
super();
|
| 43 |
} |
|
| 44 |
|
|
| 45 |
private DateFormat formatForDate = new SimpleDateFormat("yyyy/MM/dd"); |
|
| 46 |
|
|
| 47 |
private DateFormat formatForTime = new SimpleDateFormat("HH:mm:ss"); |
|
| 48 |
|
|
| 49 |
private DateFormat formatForTimestamp = new SimpleDateFormat( |
|
| 50 |
"yyyy/MM/dd HH:mm:ss");
|
|
| 51 |
|
|
| 52 |
private char quote = DEFAULT_QUOTE; |
|
| 53 |
|
|
| 54 |
private static final char DEFAULT_QUOTE = '\''; |
|
| 55 |
|
|
| 56 |
/**
|
|
| 57 |
* 変換を実行します。
|
|
| 58 |
*
|
|
| 59 |
* @param sql
|
|
| 60 |
* ?を含むSQL文
|
|
| 61 |
* @param parameters
|
|
| 62 |
* 番号と関連付けられたパラメータ群
|
|
| 63 |
* @return 変換されたSQL文
|
|
| 64 |
*/
|
|
| 65 | 0 |
public String execute(String sql, IntegerKeyMap parameters) {
|
| 66 | 0 |
final StringBuffer result = new StringBuffer(100);
|
| 67 | 0 |
final int lastParameterIndex = getLastParameterIndex(parameters);
|
| 68 | 0 |
int paramIndex = 0;
|
| 69 | 0 |
boolean inSingleQuote = false; |
| 70 | 0 |
boolean inDoubleQuote = false; |
| 71 | 0 |
for (int i = 0; i < sql.length(); i++) { |
| 72 | 0 |
final char currChar = sql.charAt(i);
|
| 73 | 0 |
switch (currChar) {
|
| 74 |
case '?':
|
|
| 75 | 0 |
if (inSingleQuote || inDoubleQuote) {
|
| 76 | 0 |
result.append(currChar); |
| 77 | 0 |
break;
|
| 78 |
} |
|
| 79 | 0 |
paramIndex++; |
| 80 | 0 |
if (paramIndex > lastParameterIndex)
|
| 81 | 0 |
paramIndex = 1; |
| 82 | 0 |
final Object value = parameters.get(paramIndex); |
| 83 | 0 |
appendValue(result, value); |
| 84 | 0 |
break;
|
| 85 |
case '\'':
|
|
| 86 | 0 |
if (!inDoubleQuote)
|
| 87 | 0 |
inSingleQuote = !inSingleQuote; |
| 88 | 0 |
result.append(currChar); |
| 89 | 0 |
break;
|
| 90 |
case '"': |
|
| 91 | 0 |
if (!inSingleQuote)
|
| 92 | 0 |
inDoubleQuote = !inDoubleQuote; |
| 93 | 0 |
result.append(currChar); |
| 94 | 0 |
break;
|
| 95 |
default:
|
|
| 96 | 0 |
result.append(currChar); |
| 97 | 0 |
break;
|
| 98 |
} |
|
| 99 |
} |
|
| 100 | 0 |
return result.toString();
|
| 101 |
} |
|
| 102 |
|
|
| 103 |
/**
|
|
| 104 |
* 変換を実行します。
|
|
| 105 |
*
|
|
| 106 |
* @param sql
|
|
| 107 |
* @param parameters
|
|
| 108 |
* 番号と関連付けられたパラメータ群
|
|
| 109 |
* @return 変換されたSQL文
|
|
| 110 |
*/
|
|
| 111 | 0 |
public String execute(String sql, StringKeyMap parameters) {
|
| 112 | 0 |
return sql + " parameters=" + parameters; |
| 113 |
} |
|
| 114 |
|
|
| 115 |
/**
|
|
| 116 |
* @param parameters
|
|
| 117 |
* @return
|
|
| 118 |
*/
|
|
| 119 | 0 |
private int getLastParameterIndex(IntegerKeyMap parameters) { |
| 120 | 0 |
final Set keySet = parameters.keySet(); |
| 121 | 0 |
final List keyList = new ArrayList();
|
| 122 | 0 |
keyList.addAll(keySet); |
| 123 | 0 |
Collections.sort(keyList); |
| 124 | 0 |
if (keyList.isEmpty())
|
| 125 | 0 |
return 0;
|
| 126 | 0 |
final Object value = keyList.get(keyList.size() - 1); |
| 127 | 0 |
if (value instanceof Number) |
| 128 | 0 |
return ((Number) value).intValue();
|
| 129 | 0 |
return 0;
|
| 130 |
} |
|
| 131 |
|
|
| 132 | 0 |
private void appendValue(StringBuffer dest, Object value) { |
| 133 | 0 |
if (value == null) { |
| 134 | 0 |
dest.append("null");
|
| 135 | 0 |
return;
|
| 136 |
} |
|
| 137 | 0 |
if (needQuote(value))
|
| 138 | 0 |
dest.append(getQuote()); |
| 139 |
|
|
| 140 | 0 |
if (value instanceof String) |
| 141 | 0 |
dest.append(toString((String) value)); |
| 142 | 0 |
else if (value instanceof Timestamp) |
| 143 | 0 |
dest.append(toString((Timestamp) value)); |
| 144 | 0 |
else if (value instanceof Time) |
| 145 | 0 |
dest.append(toString((Time) value)); |
| 146 | 0 |
else if (value instanceof Date) |
| 147 | 0 |
dest.append(toString((Date) value)); |
| 148 |
else
|
|
| 149 | 0 |
dest.append(toString(value)); |
| 150 |
|
|
| 151 | 0 |
if (needQuote(value))
|
| 152 | 0 |
dest.append(getQuote()); |
| 153 |
} |
|
| 154 |
|
|
| 155 | 0 |
private boolean needQuote(Object value) { |
| 156 | 0 |
return (value instanceof String) || (value instanceof Date) |
| 157 |
|| (value instanceof Time) || (value instanceof Timestamp); |
|
| 158 |
} |
|
| 159 |
|
|
| 160 | 0 |
private String toString(Object value) {
|
| 161 | 0 |
return String.valueOf(value);
|
| 162 |
} |
|
| 163 |
|
|
| 164 | 0 |
private String toString(String value) {
|
| 165 | 0 |
return value;
|
| 166 |
} |
|
| 167 |
|
|
| 168 | 0 |
private String toString(Time value) {
|
| 169 | 0 |
return String.valueOf(formatForTime.format(value));
|
| 170 |
} |
|
| 171 |
|
|
| 172 | 0 |
private String toString(Date value) {
|
| 173 | 0 |
return String.valueOf(formatForDate.format(value));
|
| 174 |
} |
|
| 175 |
|
|
| 176 | 0 |
private String toString(Timestamp value) {
|
| 177 | 0 |
return formatForTimestamp.format(new Date(value.getTime())); |
| 178 |
} |
|
| 179 |
|
|
| 180 |
/**
|
|
| 181 |
* 変換を実行します。 Date、Time、Timestamp用のフォーマット文字列はデフォルトのものを使用します。
|
|
| 182 |
*
|
|
| 183 |
* @param sql
|
|
| 184 |
* ?を含むSQL文
|
|
| 185 |
* @param parameters
|
|
| 186 |
* 番号と関連付けられたパラメータ群
|
|
| 187 |
* @return 変換されたSQL文
|
|
| 188 |
*/
|
|
| 189 | 0 |
public static String replacePreparedParmaeters(String sql, |
| 190 |
IntegerKeyMap parameters) {
|
|
| 191 | 0 |
final SqlReplacer replacer = new SqlReplacer();
|
| 192 | 0 |
return replacer.execute(sql, parameters);
|
| 193 |
} |
|
| 194 |
|
|
| 195 |
/**
|
|
| 196 |
* 変換を実行します。 Date、Time、Timestamp用のフォーマット文字列はデフォルトのものを使用します。
|
|
| 197 |
*
|
|
| 198 |
* @param sql
|
|
| 199 |
* ?を含むSQL文
|
|
| 200 |
* @param parameters
|
|
| 201 |
* 番号と関連付けられたパラメータ群
|
|
| 202 |
* @param formatForDate
|
|
| 203 |
* Date用のフォーマット
|
|
| 204 |
* @param formatForTime
|
|
| 205 |
* Time用のフォーマット
|
|
| 206 |
* @param formatForTimestamp
|
|
| 207 |
* Timestamp用のフォーマット
|
|
| 208 |
* @return 変換されたSQL文
|
|
| 209 |
*/
|
|
| 210 | 0 |
public static String replacePreparedParmaeters(String sql, |
| 211 |
IntegerKeyMap parameters, String formatForDate, |
|
| 212 |
String formatForTime, String formatForTimestamp) {
|
|
| 213 | 0 |
final SqlReplacer replacer = new SqlReplacer();
|
| 214 | 0 |
replacer.setFormatForDate(new SimpleDateFormat(formatForDate));
|
| 215 | 0 |
replacer.setFormatForTime(new SimpleDateFormat(formatForTime));
|
| 216 | 0 |
replacer |
| 217 |
.setFormatForTimestamp(new SimpleDateFormat(formatForTimestamp));
|
|
| 218 | 0 |
return replacer.execute(sql, parameters);
|
| 219 |
} |
|
| 220 |
|
|
| 221 |
/**
|
|
| 222 |
* Date用のフォーマット
|
|
| 223 |
*
|
|
| 224 |
* @return
|
|
| 225 |
*/
|
|
| 226 | 0 |
public DateFormat getFormatForDate() {
|
| 227 | 0 |
return formatForDate;
|
| 228 |
} |
|
| 229 |
|
|
| 230 |
/**
|
|
| 231 |
* Time用のフォーマット
|
|
| 232 |
*
|
|
| 233 |
* @return
|
|
| 234 |
*/
|
|
| 235 | 0 |
public DateFormat getFormatForTime() {
|
| 236 | 0 |
return formatForTime;
|
| 237 |
} |
|
| 238 |
|
|
| 239 |
/**
|
|
| 240 |
* Timestamp用のフォーマット
|
|
| 241 |
*
|
|
| 242 |
* @return
|
|
| 243 |
*/
|
|
| 244 | 0 |
public DateFormat getFormatForTimestamp() {
|
| 245 | 0 |
return formatForTimestamp;
|
| 246 |
} |
|
| 247 |
|
|
| 248 |
/**
|
|
| 249 |
* Date用のフォーマット
|
|
| 250 |
*
|
|
| 251 |
* @param format
|
|
| 252 |
*/
|
|
| 253 | 0 |
public void setFormatForDate(DateFormat format) { |
| 254 | 0 |
formatForDate = format; |
| 255 |
} |
|
| 256 |
|
|
| 257 |
/**
|
|
| 258 |
* Time用のフォーマット
|
|
| 259 |
*
|
|
| 260 |
* @param format
|
|
| 261 |
*/
|
|
| 262 | 0 |
public void setFormatForTime(DateFormat format) { |
| 263 | 0 |
formatForTime = format; |
| 264 |
} |
|
| 265 |
|
|
| 266 |
/**
|
|
| 267 |
* Timestamp用のフォーマット
|
|
| 268 |
*
|
|
| 269 |
* @param format
|
|
| 270 |
*/
|
|
| 271 | 0 |
public void setFormatForTimestamp(DateFormat format) { |
| 272 | 0 |
formatForTimestamp = format; |
| 273 |
} |
|
| 274 |
|
|
| 275 |
/**
|
|
| 276 |
* 文字列、日付、時刻等を変換の際に使用するクォーテーション
|
|
| 277 |
*
|
|
| 278 |
* @return
|
|
| 279 |
*/
|
|
| 280 | 0 |
public char getQuote() { |
| 281 | 0 |
return quote;
|
| 282 |
} |
|
| 283 |
|
|
| 284 |
/**
|
|
| 285 |
* 文字列、日付、時刻等を変換の際に使用するクォーテーション
|
|
| 286 |
*
|
|
| 287 |
* @param c
|
|
| 288 |
*/
|
|
| 289 | 0 |
public void setQuote(char c) { |
| 290 | 0 |
quote = c; |
| 291 |
} |
|
| 292 |
|
|
| 293 |
} |
|
||||||||||