OSDN Git Service

DBFlute-0.9.3に更新
[ea2ddl/ea2ddl.git] / ea2ddl-gen / dbflute / templates / om / csharp / allcommon / s2dao / internal / util / InternalBindVariableUtil.vmnet
1 \r
2 using System;\r
3 using System.Data;\r
4 using System.Data.SqlTypes;\r
5 using System.Reflection;\r
6 using System.Text;\r
7 using System.Text.RegularExpressions;\r
8 using Seasar.Framework.Util;\r
9 using Seasar.Extension.ADO;\r
10 using Seasar.Extension.ADO.Impl;\r
11 \r
12 namespace ${glPackageBaseCommonS2DaoInternalUtil} {\r
13 \r
14     public class ${glInternalBindVariableUtil} {\r
15     \r
16         // ===============================================================================\r
17         //                                                                      Definition\r
18         //                                                                      ==========\r
19         protected static readonly IDbParameterParser _parser = new BasicDbParameterParser();\r
20         protected static readonly string sqlLogDateFormat = "yyyy-MM-dd";\r
21         protected static readonly string sqlLogDateTimeFormat = "yyyy-MM-dd HH.mm.ss";\r
22                 \r
23         // ===============================================================================\r
24         //                                                                     Constructor\r
25         //                                                                     ===========\r
26         private ${glInternalBindVariableUtil}() {\r
27         }\r
28 \r
29         // ===============================================================================\r
30         //                                                                     CompleteSql\r
31         //                                                                     ===========\r
32         public static String GetCompleteSql(String sql, Object[] args) {\r
33             if (args == null || args.Length == 0) {\r
34                 return sql;\r
35             }\r
36             _parser.Parse(sql);\r
37             return ReplaceSql(sql, args);\r
38         }\r
39 \r
40         protected static String ReplaceSql(string sql, object[] args) {\r
41             StringBuilder text = new StringBuilder(sql);\r
42             for (int startIndex = 0, argsIndex = 0; argsIndex < args.Length; ++argsIndex) {\r
43                 Match match = _parser.Match(text.ToString(), startIndex);\r
44                 if (!match.Success) {\r
45                     break;\r
46                 }\r
47                 string newValue = GetBindVariableText(args[argsIndex]);\r
48                 text.Replace(match.Value, newValue, match.Index, match.Length);\r
49                 startIndex = match.Index + newValue.Length;\r
50             }\r
51             return text.ToString();\r
52         }\r
53 \r
54         public static String GetBindVariableText(Object bindVariable) {\r
55             if (bindVariable is INullable) {\r
56                 INullable nullable = bindVariable as INullable;\r
57                 if (nullable.IsNull) {\r
58                     return GetBindVariableText(null);\r
59                 } else {\r
60                     PropertyInfo pi = bindVariable.GetType().GetProperty("Value");\r
61                     return GetBindVariableText(pi.GetValue(bindVariable, null));\r
62                 }\r
63             } else if (bindVariable is string) {\r
64                 return "'" + bindVariable + "'";\r
65             } else if (bindVariable == null) {\r
66                 return "null";\r
67             } else if (bindVariable.GetType().IsPrimitive) {\r
68                 return bindVariable.ToString();\r
69             } else if (bindVariable is decimal) {\r
70                 return bindVariable.ToString();\r
71             } else if (bindVariable is DateTime) {\r
72                 if ((DateTime) bindVariable == ((DateTime) bindVariable).Date) {\r
73                     return "'" + ((DateTime) bindVariable).ToString(sqlLogDateFormat) + "'";\r
74                 } else {\r
75                     return "'" + ((DateTime) bindVariable).ToString(sqlLogDateTimeFormat) + "'";\r
76                 }\r
77             } else if (bindVariable is bool) {\r
78                 return bindVariable.ToString();\r
79             } else if (bindVariable.GetType().IsEnum) {\r
80                 object o = Convert.ChangeType(bindVariable, Enum.GetUnderlyingType(bindVariable.GetType()));\r
81                 return o.ToString();\r
82             } else {\r
83                 return "'" + bindVariable + "'";\r
84             }\r
85         }\r
86         }\r
87 }\r