Skip to content Skip to sidebar Skip to footer

Vs 2012 Ssdt Build Clr With Ienumerable Failing On Generated Syntax

I'm attempting to reuse some code found here. After adding a new using the c# (shown below) seems to be fine. When I go to publish the database, however, the code generator does n

Solution 1:

After some help from a co-worker I discovered that two changes were needed in the CLR:

  1. the SQLFunction[] declaration needed to include a TableDefinition argument as shown in the example on here. (code is shown below)

    [SqlFunction(FillRowMethodName = "FillRegExRow",
    TableDefinition = "[rowId] int,[matchId] int,[groupId] int, [value] nvarchar(4000)")]
    publicstatic IEnumerable RegExMatches(string sourceString, string pattern)
    
  2. int data types in RegExRow.RegExRow were changed to SqlInt32. (This might not have been necessary to resolve the issue in my original question).

So the overall code changed to:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Text.RegularExpressions;
using System.Collections;

publicclassSQLRegEx
{
    privateclassRegExRow
    {
    ///<summary>/// Private class for passing matches of the RegExMatches to the FillRow method///</summary>///<param name=”rowId”>ID of the Row</param>///<param name=”matchId”>ID of the Match</param>///<param name=”groupID”>ID of the Group within the Match</param>///<param name=”value”>Value of the particular group</param>publicRegExRow(SqlInt32 rowId, SqlInt32 matchId, SqlInt32 groupID, stringvalue)
    {

        RowId = rowId;
        MatchId = matchId;
        GroupID = groupID;
        Value = value;
    }

    public SqlInt32 RowId;
    public SqlInt32 MatchId;
    public SqlInt32 GroupID;
    publicstring Value;
}

///<summary>/// Applies Regular Expression on the Source string and returns value of particular group from withing a specified match///</summary>///<param name=”sourceString”>Source string on which the regular expression should be applied</param>///<param name=”pattern”>Regular Expression pattern</param>///<param name=”matchId”>ID of the Match to be returned 1 inex-based</param>///<param name=”groupId”>ID of the group from within a match to return. GroupID 0 returns complete match</param>///<returns>Value of the Group from within a Match</returns>
[SqlFunction(IsDeterministic=true)]
publicstatic SqlChars RegExMatch(string sourceString, string pattern, int matchId, int groupId)
{
    Match m = null;
    Regex r = new Regex(pattern, RegexOptions.Compiled);

    if (matchId == 1)
    {
        m = r.Match(sourceString);
    }
    elseif (matchId > 1)
    {
        MatchCollection mc = r.Matches(sourceString);

        if (mc!=null && mc.Count > matchId-1)
        {
            m = mc[matchId-1];
        }
        else
        {
            m= null;
        }

        ///m = mc != null && mc.Count > matchId – 1 ? mc[matchId - 1] : null;
    }

    return m != null && m.Groups.Count > groupId ? new SqlChars(m.Groups[groupId].Value) : SqlChars.Null;
}

///<summary>/// Applies Regular Expression o the Source strings and return all matches and groups///</summary>///<param name=”sourceString”>Source string on which the regular expression should be applied</param>///<param name=”pattern”>Regular Expression pattern</param>///<returns>Returns list of RegExRows representing the group value</returns>///

[SqlFunction(FillRowMethodName = "FillRegExRow",
            TableDefinition = "rowId int,[matchId] int,[groupId] int, [value] nvarchar(4000)")]
publicstatic IEnumerable RegExMatches(string sourceString, string pattern)
{
    Regex r = new Regex(pattern, RegexOptions.Compiled);
    int rowId = 0;
    int matchId = 0;
    foreach (Match m in r.Matches(sourceString))
    {
        matchId++;
        for (int i = 0; i < m.Groups.Count; i++)
        {
            ++rowId;
            yieldreturnnewRegExRow(rowId, matchId, i, m.Groups[i].Value);
        }
    }
}

///<summary>/// FillRow method to populate the output table///</summary>///<param name=”obj”>RegExRow passed as object</param>///<param name=”rowId”>ID or the returned row</param>///<param name=”matchId”>ID of returned Match</param>///<param name=”groupID”>ID of group in the Match</param>///<param name=”value”>Value of the Group</param>publicstaticvoidFillRegExRow(Object obj, out SqlInt32 rowId, out SqlInt32 matchId, out SqlInt32 groupID, out SqlChars value)
{
    RegExRow r = (RegExRow)obj;
    rowId = r.RowId;
    matchId = r.MatchId;
    groupID = r.GroupID;
    value = new SqlChars(r.Value);
}

}

Post a Comment for "Vs 2012 Ssdt Build Clr With Ienumerable Failing On Generated Syntax"