Rumah >pangkalan data >tutorial mysql >解剖SQLSERVER 第三篇 数据类型的实现(译)
解剖SQLSERVER 第三篇 数据类型的实现(译) http://improve.dk/implementing-data-types-in-orcamdf/ 实现对SQLSERVER数据类型的解析在OrcaMDF 软件里面是一件比较简单的事,只需要实现ISqlType 接口 public interface ISqlType{ bool IsVariableLength { g
http://improve.dk/implementing-data-types-in-orcamdf/
实现对SQLSERVER数据类型的解析在OrcaMDF 软件里面是一件比较简单的事,只需要实现ISqlType 接口
<span>public</span> <span>interface</span><span> ISqlType { </span><span>bool</span> IsVariableLength { <span>get</span><span>; } </span><span>short</span>? FixedLength { <span>get</span><span>; } </span><span>object</span> GetValue(<span>byte</span><span>[] value); }</span>
IsVariableLength 返回数据类型是否是定长的还是变长的。
FixedLength 返回定长数据类型的长度,否则他返回null
数据类型解释器不关心变长字段的长度,输入的字节大小会决定长度
最后,GetValue 将输入字节参数进行解释并将字节解释为相关的.NET对象
SqlInt实现
int类型作为定长类型是非常简单的,能直接使用BitConverter进行转换
<span>public</span> <span>class</span><span> SqlInt : ISqlType { </span><span>public</span> <span>bool</span><span> IsVariableLength { </span><span>get</span> { <span>return</span> <span>false</span><span>; } } </span><span>public</span> <span>short</span>?<span> FixedLength { </span><span>get</span> { <span>return</span> <span>4</span><span>; } } </span><span>public</span> <span>object</span> GetValue(<span>byte</span><span>[] value) { </span><span>if</span> (value.Length != <span>4</span><span>) </span><span>throw</span> <span>new</span> ArgumentException(<span>"</span><span>Invalid value length: </span><span>"</span> +<span> value.Length); </span><span>return</span> BitConverter.ToInt32(value, <span>0</span><span>); } }</span>
相关测试
<span>[TestFixture] </span><span>public</span> <span>class</span><span> SqlIntTests { [Test] </span><span>public</span> <span>void</span><span> GetValue() { </span><span>var</span> type = <span>new</span><span> SqlInt(); </span><span>byte</span><span>[] input; input </span>= <span>new</span> <span>byte</span>[] { <span>0x5e</span>, <span>0x3b</span>, <span>0x27</span>, <span>0x2a</span><span> }; Assert.AreEqual(</span><span>707214174</span><span>, Convert.ToInt32(type.GetValue(input))); input </span>= <span>new</span> <span>byte</span>[] { <span>0x8d</span>, <span>0xf9</span>, <span>0xaa</span>, <span>0x30</span><span> }; Assert.AreEqual(</span><span>816511373</span><span>, Convert.ToInt32(type.GetValue(input))); input </span>= <span>new</span> <span>byte</span>[] { <span>0x7a</span>, <span>0x4a</span>, <span>0x72</span>, <span>0xe2</span><span> }; Assert.AreEqual(</span>-<span>495826310</span><span>, Convert.ToInt32(type.GetValue(input))); } [Test] </span><span>public</span> <span>void</span><span> Length() { </span><span>var</span> type = <span>new</span><span> SqlInt(); Assert.Throws</span><argumentexception>(() => type.GetValue(<span>new</span> <span>byte</span>[<span>3</span><span>])); Assert.Throws</span><argumentexception>(() => type.GetValue(<span>new</span> <span>byte</span>[<span>5</span><span>])); } }</span></argumentexception></argumentexception>
SqlNVarchar 实现
nvarchar 类型也是非常简单的,注意,如果是可变长度我们返回长度的结果是null
ISqlType 接口实现必须是无状态的
GetValue 简单的将输入的字节的数进行转换,这将转换为相关的.NET 类型,这里是string类型
<span>public</span> <span>class</span><span> SqlNVarchar : ISqlType { </span><span>public</span> <span>bool</span><span> IsVariableLength { </span><span>get</span> { <span>return</span> <span>true</span><span>; } } </span><span>public</span> <span>short</span>?<span> FixedLength { </span><span>get</span> { <span>return</span> <span>null</span><span>; } } </span><span>public</span> <span>object</span> GetValue(<span>byte</span><span>[] value) { </span><span>return</span><span> Encoding.Unicode.GetString(value); } }</span>
相关测试
<span>[TestFixture] </span><span>public</span> <span>class</span><span> SqlNvarcharTests { [Test] </span><span>public</span> <span>void</span><span> GetValue() { </span><span>var</span> type = <span>new</span><span> SqlNVarchar(); </span><span>byte</span>[] input = <span>new</span> <span>byte</span>[] { <span>0x47</span>, <span>0x04</span>, <span>0x2f</span>, <span>0x04</span>, <span>0xe6</span>, <span>0x00</span><span> }; Assert.AreEqual(</span><span>"</span><span>u0447u042fu00e6</span><span>"</span>, (<span>string</span><span>)type.GetValue(input)); } }</span>
其他类型的实现
OrcaMDF 软件现在支持12种数据类型,以后将会支持datetime和bit类型,因为这两个类型相比起其他类型有些特殊
其余类型我以后也将会进行实现
第三篇完