Subsonic 3 和 Activerecord 不生成 MySQL 存储过程

发布于 2024-07-28 23:49:34 字数 286 浏览 6 评论 0原文

似乎包含的 T4 模板(或 SVN 主干中的模板)只是跳过为 MySQL 生成 SP... 当 StoredProcedures.ttinclude 与 MySQL.ttinclude 一起运行时,我收到错误“编译转换:名称“GetSPs”在当前上下文中不存在”。

GetSPs 是为 SQLServer 定义的,我看到有人为 Oracle 编写了自己的方法,但是有人知道正确的 GetSPs() 方法对于 MySQL 应该是什么样子吗?

就我个人而言,我认为除非我可以运行自己的 SP,否则它没有真正的功能:/

It seems that the included T4 templates (or the one in the SVN trunk for that matter) just skips generating SPs for MySQL...
When running StoredProcedures.ttinclude together with MySQL.ttinclude, I get the error "Compiling transformation: The name 'GetSPs' does not exist in the current context".

GetSPs is defined for SQLServer and I saw that someone wrote his own for Oracle, but does anyone have a clue how the proper GetSPs()-method should look like for MySQL?

Personally I don't think it's really functional unless I can run my own SPs :/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪我入戏太深 2024-08-04 23:49:34

我对版本 2 中的代码进行了一些修改,并想出了一些对我有用的代码(可能不是在所有情况下 100%)。 将其放入 MySQL.ttinclude 中以生成存储过程。 享受!


string[] GetSPList()
{
    var result=new List();
    const string sql = "SELECT routine_name FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = ?databaseName";
    StringBuilder sList = new StringBuilder();

    using(conn=new MySqlConnection(ConnectionString))
    {
        MySqlCommand cmd = new MySqlCommand(sql, conn);

        cmd.Parameters.AddWithValue("?databaseName", DatabaseName);
        conn.Open();
        using(IDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
        {
            bool isFirst = true;
            while(rdr.Read())
            {
                if(!isFirst)
                    sList.Append('|');
                isFirst = false;
                sList.Append(rdr[0]);
            }
            rdr.Close();
        }
    }
    return sList.ToString().Split(new char[] {'|'}, StringSplitOptions.RemoveEmptyEntries);
}

List GetSPParams(string spName)
{
    var result=new List();
    MySqlCommand cmd = new MySqlCommand();
    using(conn=new MySqlConnection(ConnectionString))
    {
        conn.Open();
        cmd.Connection = conn;
        cmd.CommandText = spName;
        cmd.CommandType = CommandType.StoredProcedure;

        try
        {
            MySqlCommandBuilder.DeriveParameters(cmd);
        }
        catch
        {
        }

        if(cmd.Parameters.Count > 0)
        {
            foreach(MySqlParameter param in cmd.Parameters)
            {
                SPParam p = new SPParam();
                p.SysType = GetSysType(param.MySqlDbType.ToString());
                p.DbType = param.DbType.ToString();
                p.Name = param.ParameterName;
                p.CleanName=CleanUp(p.Name);
                result.Add(p);
            }
        }
        conn.Close();
    }

    return result;
}

List GetSPs(){
    var result=new List();
    string[] spNames = GetSPList();

    foreach(string spName in spNames){
        var sp=new SP();
        sp.Name=spName;
        sp.CleanName=CleanUp(sp.Name);
        sp.Parameters=GetSPParams(sp.Name);
        result.Add(sp);        
    }

    return result;
}

I tinkered around a bit with the code from version 2 and came up with a little code (probably not 100% in all situations) that did the trick for me. Put this in MySQL.ttinclude to get Stored Procedures generated. Enjoy!


string[] GetSPList()
{
    var result=new List();
    const string sql = "SELECT routine_name FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = ?databaseName";
    StringBuilder sList = new StringBuilder();

    using(conn=new MySqlConnection(ConnectionString))
    {
        MySqlCommand cmd = new MySqlCommand(sql, conn);

        cmd.Parameters.AddWithValue("?databaseName", DatabaseName);
        conn.Open();
        using(IDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
        {
            bool isFirst = true;
            while(rdr.Read())
            {
                if(!isFirst)
                    sList.Append('|');
                isFirst = false;
                sList.Append(rdr[0]);
            }
            rdr.Close();
        }
    }
    return sList.ToString().Split(new char[] {'|'}, StringSplitOptions.RemoveEmptyEntries);
}

List GetSPParams(string spName)
{
    var result=new List();
    MySqlCommand cmd = new MySqlCommand();
    using(conn=new MySqlConnection(ConnectionString))
    {
        conn.Open();
        cmd.Connection = conn;
        cmd.CommandText = spName;
        cmd.CommandType = CommandType.StoredProcedure;

        try
        {
            MySqlCommandBuilder.DeriveParameters(cmd);
        }
        catch
        {
        }

        if(cmd.Parameters.Count > 0)
        {
            foreach(MySqlParameter param in cmd.Parameters)
            {
                SPParam p = new SPParam();
                p.SysType = GetSysType(param.MySqlDbType.ToString());
                p.DbType = param.DbType.ToString();
                p.Name = param.ParameterName;
                p.CleanName=CleanUp(p.Name);
                result.Add(p);
            }
        }
        conn.Close();
    }

    return result;
}

List GetSPs(){
    var result=new List();
    string[] spNames = GetSPList();

    foreach(string spName in spNames){
        var sp=new SP();
        sp.Name=spName;
        sp.CleanName=CleanUp(sp.Name);
        sp.Parameters=GetSPParams(sp.Name);
        result.Add(sp);        
    }

    return result;
}
尤怨 2024-08-04 23:49:34

我通过将这段代码添加到我的 MySQL.ttinclude 来运行它,但是我必须将通用列表更改为类型化列表。 例如:ListList。 不过,代码在那之后就可以工作了;-)

I got this code to run by adding it to my MySQL.ttinclude however I had to change the generic List's to typed lists. eg: List<SPParam> and List<SP>. The code worked after that though ;-)

扮仙女 2024-08-04 23:49:34

根据 craig 的回答,我将列表更改为类型化列表,并且还需要从 GetSPList() 代码块中删除 var result=new List(); 。 干杯

As per craig's answer, I changed the Lists to typed lists and I also needed to remove var result=new List(); from the GetSPList() code block. Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文