Anatomy of SQL Server third data type implementation (translation)

Source: Internet
Author: User
Tags assert

Anatomy of SQL Server third data type implementation (translation)

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

The implementation of SQL Server data type parsing in the Orcamdf software is a relatively simple thing, only need to implement the Isqltype interface

 Public Interface isqltype{    boolget;}      Short Get ; }     Object GetValue (byte[] value);

isvariablelength Returns whether the data type is fixed-length or variable-length.

fixedlength Returns the length of the fixed-length data type, otherwise he returns null

The data type interpreter does not care about the length of the variable length field, and the size of the input byte determines the length

Finally,GetValue interprets the input byte parameters and interprets the bytes as related. NET Object

Sqlint implementation

The int type is very simple as a fixed-length type and can be converted directly using Bitconverter

 Public classsqlint:isqltype{ Public BOOLIsvariablelength {Get{return false; } }     Public  Short?FixedLength {Get{return 4; } }     Public ObjectGetValue (byte[] value) {        if(value.) Length! =4)            Throw NewArgumentException ("Invalid value Length:"+value.        Length); returnBitconverter.toint32 (Value,0); }}

Related tests

[Testfixture] Public classsqlinttests{[Test] Public voidGetValue () {varType =NewSqlint (); 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 voidLength () {varType =NewSqlint (); Assert.throws<ArgumentException> (() = type. GetValue (New byte[3])); Assert.throws<ArgumentException> (() = type. GetValue (New byte[5])); }}

Sqlnvarchar implementation

The nvarchar type is also very simple, note that if it is variable length The result of our return length is null

The Isqltype interface implementation must be stateless

GetValue simply converts the number of bytes entered, which will be converted to the relevant. NET type, here is the string type

 Public classsqlnvarchar:isqltype{ Public BOOLIsvariablelength {Get{return true; } }     Public  Short?FixedLength {Get{return NULL; } }     Public ObjectGetValue (byte[] value) {        returnEncoding.Unicode.GetString (value); }}

Related tests

[Testfixture] Public classsqlnvarchartests{[Test] Public voidGetValue () {varType =NewSqlnvarchar (); byte[] input =New byte[] {0x47,0x04,0x2f,0x04,0xe6,0x00 }; Assert.AreEqual ("U0447u042fu00e6", (string) type.    GetValue (input)); }}

Other types of implementations

The Orcamdf software now supports 12 data types, and later will support both datetime and bit types, since these two types are somewhat special compared to other types

The rest of the types I'll be implementing later

End of the third article

Anatomy of SQL Server third data type implementation (translation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.