1 package jp.sourceforge.ea2ddl.dao.allcommon.s2dao.internal.util;
\r
3 import java.lang.reflect.Method;
\r
4 import java.lang.reflect.InvocationTargetException;
\r
5 import java.sql.Date;
\r
6 import java.sql.Time;
\r
7 import java.sql.Timestamp;
\r
8 import java.text.SimpleDateFormat;
\r
9 import java.util.Calendar;
\r
11 import org.seasar.extension.jdbc.ValueType;
\r
13 import jp.sourceforge.ea2ddl.dao.allcommon.DBFluteConfig;
\r
16 * @author DBFlute(AutoGenerator)
\r
18 public class InternalBindVariableUtil {
\r
20 // ===================================================================================
\r
23 private static final String NULL = "null";
\r
25 // ===================================================================================
\r
28 private InternalBindVariableUtil() {
\r
31 public static String getCompleteSql(String sql, Object[] args) {
\r
32 if (args == null || args.length == 0) {
\r
35 return getCompleteSql(sql, args, new ValueType[args.length]);
\r
38 public static String getCompleteSql(String sql, Object[] args,
\r
39 ValueType[] valueTypes) {
\r
40 if (args == null || args.length == 0) {
\r
43 StringBuffer buf = new StringBuffer(sql.length() + args.length * 15);
\r
52 pos = sql.indexOf('?', pos2);
\r
53 pos3 = sql.indexOf('\'', pos2);
\r
54 pos4 = sql.indexOf('\'', pos3 + 1);
\r
55 pos5 = sql.indexOf("/*", pos2);
\r
56 pos6 = sql.indexOf("*/", pos5 + 1);
\r
58 if (pos3 >= 0 && pos3 < pos && pos < pos4) {
\r
59 buf.append(sql.substring(pos2, pos4 + 1));
\r
61 } else if (pos5 >= 0 && pos5 < pos && pos < pos6) {
\r
62 buf.append(sql.substring(pos2, pos6 + 1));
\r
65 if (args.length <= index) {
\r
66 String msg = "The size of bind arguments is illegal:";
\r
67 msg = msg + " size=" + args.length + " sql=" + sql;
\r
68 throw new IllegalStateException(msg);
\r
70 buf.append(sql.substring(pos2, pos));
\r
71 buf.append(getBindVariableText(args[index],
\r
72 valueTypes[index]));
\r
77 buf.append(sql.substring(pos2));
\r
81 return buf.toString();
\r
84 public static String getBindVariableText(Object bindVariable) {
\r
85 if (bindVariable instanceof String) {
\r
86 return quote(bindVariable.toString());
\r
87 } else if (bindVariable instanceof Number) {
\r
88 return bindVariable.toString();
\r
89 } else if (bindVariable instanceof Time) {
\r
90 SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
\r
91 return quote(sdf.format((java.util.Date) bindVariable));
\r
92 } else if (bindVariable instanceof Timestamp) {
\r
93 SimpleDateFormat sdf = new SimpleDateFormat(getLogTimestampFormat());
\r
94 return quote(sdf.format((java.util.Date) bindVariable));
\r
95 } else if (bindVariable instanceof java.util.Date) {
\r
96 SimpleDateFormat sdf = new SimpleDateFormat(getLogDateFormat());
\r
97 return quote(sdf.format((java.util.Date) bindVariable));
\r
98 } else if (bindVariable instanceof Boolean) {
\r
99 return bindVariable.toString();
\r
100 } else if (bindVariable == null) {
\r
103 return quote(bindVariable.toString());
\r
107 protected static String getLogDateFormat() {
\r
108 String logDateFormat = DBFluteConfig.getInstance().getLogDateFormat();
\r
109 return logDateFormat != null ? logDateFormat : "yyyy-MM-dd";
\r
112 protected static String getLogTimestampFormat() {
\r
113 String logTimestampFormat = DBFluteConfig.getInstance().getLogTimestampFormat();
\r
114 return logTimestampFormat != null ? logTimestampFormat : "yyyy-MM-dd HH:mm:ss";
\r
117 // For various seasar's version.
\r
118 protected static final Class<?>[] TOTEXT_ARGUMENT_TYPES = new Class<?>[]{Object.class};
\r
119 protected static final Method TOTEXT_METHOD;
\r
121 Method method = null;
\r
123 method = ValueType.class.getMethod("toText", TOTEXT_ARGUMENT_TYPES);
\r
124 } catch (SecurityException e) {
\r
125 } catch (NoSuchMethodException e) {
\r
127 TOTEXT_METHOD = method;
\r
129 public static String getBindVariableText(Object bindVariable, ValueType valueType) {
\r
130 if (valueType != null && TOTEXT_METHOD != null ) {
\r
132 return (String)TOTEXT_METHOD.invoke(valueType, new Object[]{bindVariable});
\r
133 } catch (IllegalArgumentException e) {
\r
134 String msg = "ValueType.toText() threw the IllegalArgumentException:";
\r
135 msg = msg + " valueType=" + valueType + " bindVariable=" + bindVariable;
\r
136 throw new IllegalStateException(msg, e);
\r
137 } catch (IllegalAccessException e) {
\r
138 String msg = "ValueType.toText() threw the IllegalAccessException:";
\r
139 msg = msg + " valueType=" + valueType + " bindVariable=" + bindVariable;
\r
140 throw new IllegalStateException(msg, e);
\r
141 } catch (InvocationTargetException e) {
\r
142 if (e.getTargetException() instanceof RuntimeException) {
\r
143 throw (RuntimeException)e.getTargetException();
\r
145 String msg = "ValueType.toText() threw the exception:";
\r
146 msg = msg + " valueType=" + valueType + " bindVariable=" + bindVariable;
\r
147 throw new IllegalStateException(msg, e.getTargetException());
\r
151 return getBindVariableText(bindVariable);
\r
154 public static String nullText() {
\r
158 public static String toText(Number value) {
\r
159 if (value == null) {
\r
162 return value.toString();
\r
165 public static String toText(Boolean value) {
\r
166 if (value == null) {
\r
169 return quote(value.toString());
\r
172 public static String toText(String value) {
\r
173 if (value == null) {
\r
176 return quote(value);
\r
179 public static String toText(Date value) {
\r
180 if (value == null) {
\r
183 Calendar calendar = Calendar.getInstance();
\r
184 calendar.setTime(value);
\r
185 StringBuilder buf = new StringBuilder();
\r
186 addDate(buf, calendar);
\r
187 return quote(buf.toString());
\r
190 public static String toText(Time value) {
\r
191 if (value == null) {
\r
194 Calendar calendar = Calendar.getInstance();
\r
195 calendar.setTime(value);
\r
196 StringBuilder buf = new StringBuilder();
\r
197 addTime(buf, calendar);
\r
198 addTimeDecimalPart(buf, calendar.get(Calendar.MILLISECOND));
\r
199 return quote(buf.toString());
\r
202 public static String toText(Timestamp value) {
\r
203 if (value == null) {
\r
206 Calendar calendar = Calendar.getInstance();
\r
207 calendar.setTime(value);
\r
208 StringBuilder buf = new StringBuilder(30);
\r
209 addDate(buf, calendar);
\r
210 addTime(buf, calendar);
\r
211 addTimeDecimalPart(buf, value.getNanos());
\r
212 return quote(buf.toString());
\r
215 public static String toText(byte[] value) {
\r
216 if (value == null) {
\r
219 return quote(value.toString() + "(byteLength=" + Integer.toString(value.length) + ")");
\r
223 * {@link Object}の文字列表現を返します。
\r
229 public static String toText(Object value) {
\r
230 if (value == null) {
\r
233 return quote(value.toString());
\r
237 protected static void addDate(StringBuilder buf, Calendar calendar) {
\r
238 int year = calendar.get(Calendar.YEAR);
\r
241 int month = calendar.get(Calendar.MONTH) + 1;
\r
247 int date = calendar.get(Calendar.DATE);
\r
255 protected static void addTime(StringBuilder buf, Calendar calendar) {
\r
256 if (buf.length() > 0) {
\r
259 int hour = calendar.get(Calendar.HOUR_OF_DAY);
\r
265 int minute = calendar.get(Calendar.MINUTE);
\r
269 buf.append(minute);
\r
271 int second = calendar.get(Calendar.SECOND);
\r
275 buf.append(second);
\r
279 protected static void addTimeDecimalPart(StringBuilder buf, int decimalPart) {
\r
280 if (decimalPart == 0) {
\r
283 if (buf.length() > 0) {
\r
286 buf.append(decimalPart);
\r
290 protected static String quote(String text) {
\r
291 return "'" + text + "'";
\r