OSDN Git Service

"https://svn.sourceforge.jp/svnroot/ea2ddl" にプロジェクト "ea2ddl-dao" を共用
[ea2ddl/ea2ddl.git] / ea2ddl-dao / src / main / java / jp / sourceforge / ea2ddl / dao / allcommon / s2dao / internal / util / InternalBindVariableUtil.java
1 package jp.sourceforge.ea2ddl.dao.allcommon.s2dao.internal.util;\r
2 \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
10 \r
11 import org.seasar.extension.jdbc.ValueType;\r
12 \r
13 import jp.sourceforge.ea2ddl.dao.allcommon.DBFluteConfig;\r
14 \r
15 /**\r
16  * @author DBFlute(AutoGenerator)\r
17  */\r
18 public class InternalBindVariableUtil {\r
19 \r
20     // ===================================================================================\r
21     //                                                                          Definition\r
22     //                                                                          ==========\r
23     private static final String NULL = "null";\r
24 \r
25     // ===================================================================================\r
26     //                                                                         Constructor\r
27     //                                                                         ===========\r
28     private InternalBindVariableUtil() {\r
29     }\r
30 \r
31     public static String getCompleteSql(String sql, Object[] args) {\r
32         if (args == null || args.length == 0) {\r
33             return sql;\r
34         }\r
35         return getCompleteSql(sql, args, new ValueType[args.length]);\r
36     }\r
37 \r
38     public static String getCompleteSql(String sql, Object[] args,\r
39             ValueType[] valueTypes) {\r
40         if (args == null || args.length == 0) {\r
41             return sql;\r
42         }\r
43         StringBuffer buf = new StringBuffer(sql.length() + args.length * 15);\r
44         int pos = 0;\r
45         int pos2 = 0;\r
46         int pos3 = 0;\r
47         int pos4 = 0;\r
48         int pos5 = 0;\r
49         int pos6 = 0;\r
50         int index = 0;\r
51         while (true) {\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
57             if (pos > 0) {\r
58                 if (pos3 >= 0 && pos3 < pos && pos < pos4) {\r
59                     buf.append(sql.substring(pos2, pos4 + 1));\r
60                     pos2 = pos4 + 1;\r
61                 } else if (pos5 >= 0 && pos5 < pos && pos < pos6) {\r
62                     buf.append(sql.substring(pos2, pos6 + 1));\r
63                     pos2 = pos6 + 1;\r
64                 } else {\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
69                     }\r
70                     buf.append(sql.substring(pos2, pos));\r
71                     buf.append(getBindVariableText(args[index],\r
72                             valueTypes[index]));\r
73                     pos2 = pos + 1;\r
74                     index++;\r
75                 }\r
76             } else {\r
77                 buf.append(sql.substring(pos2));\r
78                 break;\r
79             }\r
80         }\r
81         return buf.toString();\r
82     }\r
83 \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
101             return NULL;\r
102         } else {\r
103             return quote(bindVariable.toString());\r
104         }\r
105     }\r
106 \r
107     protected static String getLogDateFormat() {\r
108         String logDateFormat = DBFluteConfig.getInstance().getLogDateFormat();\r
109         return logDateFormat != null ? logDateFormat : "yyyy-MM-dd";\r
110     }\r
111 \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
115     }\r
116         \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
120         static {\r
121             Method method = null;\r
122             try {\r
123                 method = ValueType.class.getMethod("toText", TOTEXT_ARGUMENT_TYPES);\r
124         } catch (SecurityException e) {\r
125         } catch (NoSuchMethodException e) {\r
126         }\r
127                 TOTEXT_METHOD = method;\r
128         }\r
129     public static String getBindVariableText(Object bindVariable, ValueType valueType) {\r
130         if (valueType != null && TOTEXT_METHOD != null ) {\r
131             try {\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
144                 } else {\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
148                 }\r
149             }\r
150         }\r
151         return getBindVariableText(bindVariable);\r
152     }\r
153         \r
154     public static String nullText() {\r
155         return NULL;\r
156     }\r
157 \r
158     public static String toText(Number value) {\r
159         if (value == null) {\r
160             return NULL;\r
161         }\r
162         return value.toString();\r
163     }\r
164 \r
165     public static String toText(Boolean value) {\r
166         if (value == null) {\r
167             return NULL;\r
168         }\r
169         return quote(value.toString());\r
170     }\r
171 \r
172     public static String toText(String value) {\r
173         if (value == null) {\r
174             return NULL;\r
175         }\r
176         return quote(value);\r
177     }\r
178 \r
179     public static String toText(Date value) {\r
180         if (value == null) {\r
181             return NULL;\r
182         }\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
188     }\r
189 \r
190     public static String toText(Time value) {\r
191         if (value == null) {\r
192             return NULL;\r
193         }\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
200     }\r
201 \r
202     public static String toText(Timestamp value) {\r
203         if (value == null) {\r
204             return NULL;\r
205         }\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
213     }\r
214 \r
215     public static String toText(byte[] value) {\r
216         if (value == null) {\r
217             return NULL;\r
218         }\r
219         return quote(value.toString() + "(byteLength=" + Integer.toString(value.length) + ")");\r
220     }\r
221 \r
222     /**\r
223      * {@link Object}の文字列表現を返します。\r
224      * \r
225      * @param value\r
226      *            値\r
227      * @return 文字列表現\r
228      */\r
229     public static String toText(Object value) {\r
230         if (value == null) {\r
231             return NULL;\r
232         }\r
233         return quote(value.toString());\r
234     }\r
235 \r
236         // yyyy-mm-dd\r
237     protected static void addDate(StringBuilder buf, Calendar calendar) {\r
238         int year = calendar.get(Calendar.YEAR);\r
239         buf.append(year);\r
240         buf.append('-');\r
241         int month = calendar.get(Calendar.MONTH) + 1;\r
242         if (month < 10) {\r
243             buf.append('0');\r
244         }\r
245         buf.append(month);\r
246         buf.append('-');\r
247         int date = calendar.get(Calendar.DATE);\r
248         if (date < 10) {\r
249             buf.append('0');\r
250         }\r
251         buf.append(date);\r
252     }\r
253 \r
254         // hh:mm:ss\r
255     protected static void addTime(StringBuilder buf, Calendar calendar) {\r
256         if (buf.length() > 0) {\r
257             buf.append(' ');\r
258         }\r
259         int hour = calendar.get(Calendar.HOUR_OF_DAY);\r
260         if (hour < 10) {\r
261             buf.append('0');\r
262         }\r
263         buf.append(hour);\r
264         buf.append(':');\r
265         int minute = calendar.get(Calendar.MINUTE);\r
266         if (minute < 10) {\r
267             buf.append('0');\r
268         }\r
269         buf.append(minute);\r
270         buf.append(':');\r
271         int second = calendar.get(Calendar.SECOND);\r
272         if (second < 10) {\r
273             buf.append('0');\r
274         }\r
275         buf.append(second);\r
276     }\r
277 \r
278         // .000\r
279     protected static void addTimeDecimalPart(StringBuilder buf, int decimalPart) {\r
280         if (decimalPart == 0) {\r
281             return;\r
282         }\r
283         if (buf.length() > 0) {\r
284             buf.append('.');\r
285         }\r
286         buf.append(decimalPart);\r
287     }\r
288 \r
289         // 'text'\r
290     protected static String quote(String text) {\r
291         return "'" + text + "'";\r
292     }\r
293 }\r