您的位置:首页 > 博客中心 > 数据库 >

解剖SQLSERVER 第三篇 数据类型的实现(译)

时间:2022-03-14 01:07

 

相关测试

[TestFixture]
public class SqlIntTests
{
    [Test]
    public void GetValue()
    {
        var type = new SqlInt();
        byte[] input;

        input = new byte[] { 0x5e, 0x3b, 0x27, 0x2a };
        Assert.AreEqual(707214174, Convert.ToInt32(type.GetValue(input)));

        input = new byte[] { 0x8d, 0xf9, 0xaa, 0x30 };
        Assert.AreEqual(816511373, Convert.ToInt32(type.GetValue(input)));

        input = new byte[] { 0x7a, 0x4a, 0x72, 0xe2 };
        Assert.AreEqual(-495826310, Convert.ToInt32(type.GetValue(input)));
    }

    [Test]
    public void Length()
    {
        var type = new SqlInt();

        Assert.Throws<ArgumentException>(() => type.GetValue(new byte[3]));
        Assert.Throws<ArgumentException>(() => type.GetValue(new byte[5]));
    }
}

 

 

SqlNVarchar 实现

nvarchar 类型也是非常简单的,注意,如果是可变长度我们返回长度的结果是null

ISqlType 接口实现必须是无状态的

GetValue 简单的将输入的字节的数进行转换,这将转换为相关的.NET 类型,这里是string类型

public class SqlNVarchar : ISqlType
{
    public bool IsVariableLength
    {
        get { return true; }
    }

    public short? FixedLength
    {
        get { return null; }
    }

    public object GetValue(byte[] value)
    {
        return Encoding.Unicode.GetString(value);
    }
}

 

相关测试

[TestFixture]
public class SqlNvarcharTests
{
    [Test]
    public void GetValue()
    {
        var type = new SqlNVarchar();
        byte[] input = new byte[] { 0x47, 0x04, 0x2f, 0x04, 0xe6, 0x00 };

        Assert.AreEqual("u0447u042fu00e6", (string)type.GetValue(input));
    }
}

其他类型的实现

OrcaMDF 软件现在支持12种数据类型,以后将会支持datetime和bit类型,因为这两个类型相比起其他类型有些特殊

其余类型我以后也将会进行实现

 

第三篇完

热门排行

今日推荐

热门手游