4 using System.Data.SqlTypes;
\r
5 using System.Reflection;
\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
12 namespace ${glPackageBaseCommonS2DaoInternalUtil} {
\r
14 public class ${glInternalBindVariableUtil} {
\r
16 // ===============================================================================
\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
23 // ===============================================================================
\r
26 private ${glInternalBindVariableUtil}() {
\r
29 // ===============================================================================
\r
32 public static String GetCompleteSql(String sql, Object[] args) {
\r
33 if (args == null || args.Length == 0) {
\r
37 return ReplaceSql(sql, args);
\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
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
51 return text.ToString();
\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
60 PropertyInfo pi = bindVariable.GetType().GetProperty("Value");
\r
61 return GetBindVariableText(pi.GetValue(bindVariable, null));
\r
63 } else if (bindVariable is string) {
\r
64 return "'" + bindVariable + "'";
\r
65 } else if (bindVariable == 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
75 return "'" + ((DateTime) bindVariable).ToString(sqlLogDateTimeFormat) + "'";
\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
83 return "'" + bindVariable + "'";
\r