当前位置: 首页 > news >正文

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

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

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

 

http://improve.dk/implementing-data-types-in-orcamdf/

实现对SQLSERVER数据类型的解析在OrcaMDF 软件里面是一件比较简单的事,只需要实现ISqlType 接口

public interface ISqlType
{
    bool IsVariableLength { get; }
    short? FixedLength { get; }
    object GetValue(byte[] value);
}

IsVariableLength 返回数据类型是否是定长的还是变长的。

FixedLength 返回定长数据类型的长度,否则他返回null

数据类型解释器不关心变长字段的长度,输入的字节大小会决定长度

最后,GetValue 将输入字节参数进行解释并将字节解释为相关的.NET对象

 

 

SqlInt实现

int类型作为定长类型是非常简单的,能直接使用BitConverter进行转换

public class SqlInt : ISqlType
{
    public bool IsVariableLength
    {
        get { return false; }
    }

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

    public object GetValue(byte[] value)
    {
        if (value.Length != 4)
            throw new ArgumentException("Invalid value length: " + value.Length);

        return BitConverter.ToInt32(value, 0);
    }
}

 

相关测试

[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类型,因为这两个类型相比起其他类型有些特殊

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

 

第三篇完

相关文章:

  • DB2数据库用 With语句分隔字符
  • 处理和引发事件的规范
  • 图像的边缘提取
  • Linux之shell编程基础
  • 測试之路2——对照XML文件1
  • 魅族 连接 mac 调试
  • PhotoSwipe - 移动开发必备的 iOS 风格相册
  • https://github.com/cykl/infoqscraper/
  • 数据结构实验之栈四:括号匹配
  • django 安装/部署过程
  • 使用expdp的心得
  • 安装Ubuntu开发工具中心
  • Linux学习之CentOS(九)--Linux系统的网络环境配置
  • 关于Close和FormClosed FormClosing
  • oracle rac 日志体系结构!
  • 【comparator, comparable】小总结
  • 【干货分享】SpringCloud微服务架构分布式组件如何共享session对象
  • 08.Android之View事件问题
  • axios 和 cookie 的那些事
  • Fundebug计费标准解释:事件数是如何定义的?
  • java概述
  • Joomla 2.x, 3.x useful code cheatsheet
  • Octave 入门
  • Promise初体验
  • quasar-framework cnodejs社区
  • vue-loader 源码解析系列之 selector
  • 复习Javascript专题(四):js中的深浅拷贝
  • 看到一个关于网页设计的文章分享过来!大家看看!
  • 你对linux中grep命令知道多少?
  • [地铁译]使用SSD缓存应用数据——Moneta项目: 低成本优化的下一代EVCache ...
  • gunicorn工作原理
  • ​总结MySQL 的一些知识点:MySQL 选择数据库​
  • #if和#ifdef区别
  • #Lua:Lua调用C++生成的DLL库
  • #pragma预处理命令
  • $GOPATH/go.mod exists but should not goland
  • (13)Latex:基于ΤΕΧ的自动排版系统——写论文必备
  • (读书笔记)Javascript高级程序设计---ECMAScript基础
  • (附源码)计算机毕业设计ssm本地美食推荐平台
  • (含react-draggable库以及相关BUG如何解决)固定在左上方某盒子内(如按钮)添加可拖动功能,使用react hook语法实现
  • (一)WLAN定义和基本架构转
  • (一)基于IDEA的JAVA基础10
  • .NET Remoting Basic(10)-创建不同宿主的客户端与服务器端
  • .NET 事件模型教程(二)
  • .NET/C# 判断某个类是否是泛型类型或泛型接口的子类型
  • .netcore如何运行环境安装到Linux服务器
  • .net中的Queue和Stack
  • .vue文件怎么使用_我在项目中是这样配置Vue的
  • @property @synthesize @dynamic 及相关属性作用探究
  • [ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname
  • [ 数据结构 - C++]红黑树RBTree
  • [20181219]script使用小技巧.txt
  • [Android]使用Git将项目提交到GitHub
  • [Angularjs]asp.net mvc+angularjs+web api单页应用
  • [c++] 单例模式 + cyberrt TimingWheel 单例分析