将SubSonic SqlQuery对象转换为纯SQL的扩展方法

时间:2009-06-11 09:01:45

标签: c# sql subsonic

我有时需要查看SubSonic生成的SQL语句。

这适用于:

SqlQuery qry = DB.Select().From<Product>()
                 .Where(Products.Columns.Name).IsEqualTo("Productname");

Console.WriteLine(qry.BuildSqlStatement());

但是,它返回带有参数(SELECT ... WHERE productname = ?productname)的查询。因此,我总是必须在我的代码中找到真正的值,并用propper格式化的值替换参数(例如productname ='Productname'),这对于复杂的查询来说真的很烦人。

所以,我决定制作一个我想要分享的扩展方法(查看我的代码答案)。

1 个答案:

答案 0 :(得分:2)

它远非完美,但它现在符合我的需要。

但我可以使用

 Console.WriteLine(qry.BuildSqlStatementDebug());

然后将结果复制到剪贴板并从查询浏览器中再次运行我的数据库。

using System;
using SubSonic;

namespace MyNamespace.ExtensionMethods
{

public static class SubsonicSqlQueryExtensionMethods
{

    public static String BuildSqlStatementDebug(this SqlQuery qry)
    {

        var result = qry.BuildSqlStatement();

        foreach (var c in qry.Constraints)
        {

            if (c.Comparison == Comparison.BetweenAnd)
            {
                result = result.Replace(c.ParameterName + "_start", GetFormattedValue(c.StartValue, c.DbType));
                result = result.Replace(c.ParameterName + "_end", GetFormattedValue(c.EndValue, c.DbType));
            }
            else
            {
                result = result.Replace(c.ParameterName, GetFormattedValue(c.ParameterValue, c.DbType));
            }

        }

        return result;

    }

    // Works for MySQL
    private static readonly String formatter_date = "'{0:yyyy-MM-dd}'";
    private static readonly String formatter_datetime = "'{0:yyyy-MM-dd hh:mm:ss}'";

    private static readonly String formatter_string = "'{0}'";

    private static String GetFormattedValue(Object value, System.Data.DbType type)
    {

        switch (type)
        {
            case System.Data.DbType.AnsiString:
                return String.Format(formatter_string, value);
            case System.Data.DbType.AnsiStringFixedLength:
                return String.Format(formatter_string, value);
            //case System.Data.DbType.Binary:
            //    break;
            case System.Data.DbType.Boolean:
                return (Boolean)value == true ? "true" : "false";
            //case System.Data.DbType.Byte:
            //    break;
            //case System.Data.DbType.Currency:
            //    break;
            case System.Data.DbType.Date:
                return String.Format(formatter_date, value); break;
            case System.Data.DbType.DateTime:
                return String.Format(formatter_datetime, value); break;
            case System.Data.DbType.DateTime2:
                return String.Format(formatter_datetime, value); break;
            //case System.Data.DbType.DateTimeOffset:
            //    break;
            //case System.Data.DbType.Decimal:
            //    break;
            //case System.Data.DbType.Double:
            //    break;
            case System.Data.DbType.Guid:
                return String.Format(formatter_string, value);
            //case System.Data.DbType.Int16:
            //    break;
            //case System.Data.DbType.Int32:
            //    break;
            //case System.Data.DbType.Int64:
            //    break;
            //case System.Data.DbType.Object:
            //    break;
            //case System.Data.DbType.SByte:
            //    break;
            //case System.Data.DbType.Single:
            //    break;
            case System.Data.DbType.String:
                return String.Format(formatter_string, value);
            case System.Data.DbType.StringFixedLength:
                return String.Format(formatter_string, value);
            //case System.Data.DbType.Time:
            //    break;
            //case System.Data.DbType.UInt16:
            //    break;
            //case System.Data.DbType.UInt32:
            //    break;
            //case System.Data.DbType.UInt64:
            //    break;
            //case System.Data.DbType.VarNumeric:
            //    break;
            case System.Data.DbType.Xml:
                return String.Format(formatter_string, value);
            default:
                return value.ToString();
        }

    }

}

}