using System; using System.Collections; using System.Data.SqlTypes; using Microsoft.SqlServer.Server; public class CLR_iter { private class stringiter : IEnumerator { string _str; char delim; int _start_ix; int _end_ix; int _listpos; public string str { get { return this._str; } } public int start_ix { get { return this._start_ix; } } public int end_ix { get { return this._end_ix; } } public int listpos { get { return this._listpos; } } public stringiter(SqlString str, SqlString delimiter) { this._str = str.IsNull ? "" : str.Value; this.delim = delimiter.IsNull ? '\0' : delimiter.Value.ToCharArray(0, 1)[0]; Reset(); } public bool MoveNext() { this._start_ix = this._end_ix + 1; if (delim == ' ') { while (this._start_ix < this._str.Length && this.str[this._start_ix] == ' ') { this._start_ix++; } } if (this._start_ix >= this._str.Length) { return false; } this._end_ix = this.str.IndexOf(this.delim, this._start_ix); this._listpos++; if (this.end_ix == -1) { this._end_ix = this._str.Length; } return true; } public Object Current { get { return this; } } public void Reset() { this._start_ix = -1; this._end_ix = -1; this._listpos = 0; } } [SqlFunction(FillRowMethodName="CharlistFillRow")] public static IEnumerator CLR_charlist_iter(SqlString str, SqlString delimiter) { return new stringiter(str, delimiter); } public static void CharlistFillRow(object obj, out int listpos, out string str) { stringiter iter = (stringiter) obj; listpos = iter.listpos; str = iter.str.Substring(iter.start_ix, iter.end_ix - iter.start_ix); str = str.Trim(); } [SqlFunction(FillRowMethodName="IntlistFillRow")] public static IEnumerator CLR_intlist_iter(SqlString str, SqlString delimiter) { return new stringiter(str, delimiter); } public static void IntlistFillRow(object obj, out int listpos, out int number) { stringiter iter = (stringiter) obj; listpos = iter.listpos; string str = iter.str.Substring(iter.start_ix, iter.end_ix - iter.start_ix); number = System.Convert.ToInt32(str); } }