具有多个 sql_variant 参数的 SQLCLR 自定义聚合 [英] SQLCLR custom aggregate with multiple sql_variant parameters

查看:33
本文介绍了具有多个 sql_variant 参数的 SQLCLR 自定义聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月前我在这个帖子上发布了一个关于 CLR 用户定义聚合的问题.

这就像一个魅力.但现在我想用 sql_variant 类型的两个参数实现完全相同的功能.

This works like a charm. But now I would like to quite the same functions with the two parameters in sql_variant type.

就像在我之前的帖子中一样,这两个函数是 sMax 和 sMin,并将根据第二个值返回第一个值.

Like in my previous post, the two function would be sMax and sMin and will return the first value depending on the second.

我发现sql_variant类型是C#中的对象类型.但我很难积累和比较对象.

I found that the sql_variant type is a object type in C#. But I having difficulties to accumulate and compare the object.

在不知道类型的情况下比较这两个对象的最佳选择是什么?

What is the best option to compare this two object without knowing the type?

推荐答案

感谢您的回复.我用这个哲学来完成我的聚合函数.

Thanks for your response. I use this philosophy to complete my aggregate function.

到目前为止,它正在工作,但并非所有人都认为很好......

So far, it's working but not everythinks fine...

  • isNull 不起作用
  • 没有isNull,使用Convert.ToString不好,它用空字符串替换空值.但没有它会因空值而崩溃
  • 在 Read 函数中:使用 ReadString 函数.ReadBytes 更好?
  • 使用 Convert 和 toString 执行 CompareTo,这是一个好方法吗?

代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Diagnostics.Eventing.Reader;
using System.Globalization;
using Microsoft.SqlServer.Server;
using System.Text;
using System.Collections;
using System.IO;

[Serializable]
[SqlUserDefinedAggregate(
    Format.UserDefined,
    IsInvariantToOrder = true,
    IsInvariantToNulls = true,
    IsInvariantToDuplicates = true,
    MaxByteSize = -1)]
public struct sMax : IBinarySerialize, INullable
{
    #region Helpers

    private struct MyData
    {
        public object Data { get; set; }
        public InType DataType { get; set; }

        public object Group { get; set; }
        public InType GroupType { get; set; }

        public int CompareTo(MyData other)
        {
            if (Group == null)
                return other.Group == null ? 0 : -1;

            if (other.Group == null)
                return 1;

            if (GroupType == InType.Int)
                return Convert.ToInt32(Group).CompareTo(Convert.ToInt32(other.Group));
            if (GroupType == InType.BigInt)
                return Convert.ToInt64(Group).CompareTo(Convert.ToInt64(other.Group));
            if (GroupType == InType.Double)
                return Convert.ToDouble(Group).CompareTo(Convert.ToDouble(other.Group));
            if (GroupType == InType.Date)
                return Convert.ToDateTime(Group.ToString()).CompareTo(Convert.ToDateTime(other.Group.ToString()));
            if (GroupType == InType.String)
                return Convert.ToString(Group).CompareTo(Convert.ToString(other.Group));
            else
                return 0;
        }

        public static bool operator < (MyData left, MyData right)
        {
            return left.CompareTo(right) == -1;
        }

        public static bool operator > (MyData left, MyData right)
        {
            return left.CompareTo(right) == 1;
        }
    }

    private enum InType
    {
        String,
        Int,
        BigInt,
        Date,
        Double,
        Unknow
    }

    private InType GetType(object value)
    {
        if (value.GetType() == typeof(SqlInt32))
            return InType.Int;
        else if (value.GetType() == typeof(SqlInt64))
            return InType.BigInt;
        else if (value.GetType() == typeof(SqlString))
            return InType.String;
        else if (value.GetType() == typeof(SqlDateTime))
            return InType.Date;
        else if (value.GetType() == typeof(SqlDouble))
            return InType.Double;
        else
            return InType.Unknow;
    }

    #endregion

    private MyData _maxItem;

    public void Init()
    {
        _maxItem = default(MyData);

        this.IsNull = true;
    }

    public void Accumulate(object data, object group)
    {
        if (data != null && group != null)
        {
            var current = new MyData
            {
                Data = data,
                Group = group,
                DataType = GetType(data),
                GroupType = GetType(group)
            };

            if (current > _maxItem)
            {
                _maxItem = current;
            }
        }
    }

    public void Merge(sMax other)
    {
        if (other._maxItem > _maxItem)
        {
            _maxItem = other._maxItem;
        }
    }

    public SqlString Terminate()
    {
        return this.IsNull ? SqlString.Null : new SqlString(_maxItem.Data.ToString());
    }

    public void Read(BinaryReader reader)
    {
        IsNull = reader.ReadBoolean();
        _maxItem.Group = reader.ReadString();
        _maxItem.Data = reader.ReadString();

        if (_maxItem.Data != null)
            this.IsNull = false;
    }

    public void Write(BinaryWriter writer)
    {
        writer.Write(this.IsNull);
        writer.Write(_maxItem.Group.ToString());
        writer.Write(_maxItem.Data.ToString());
    }

    public Boolean IsNull { get; private set; }
}

这篇关于具有多个 sql_variant 参数的 SQLCLR 自定义聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆