Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Building an embedded database engine in C#

, 10 Jun 2009
DbfDotNet is a very fast and compact fully managed standalone database/entity framework, for the .Net Framework.
Prize winner in Competition "Best C# article of June 2009"
using System;
using System.Collections.Generic;
using System.Text;

namespace DbfDotNet.Core
{
    [DbfDotNet.Record(FieldMapping= FieldMapping.ExplicitColumnsOnly)]
    internal abstract class SortFields
    {
        internal void Read(NdxFile ndxFile, object dbfRecord)
        {
            throw new NotImplementedException();
        }

        internal void Read(NdxFile ndxFile, Byte[] recordBuffer)
        {
            throw new NotImplementedException();
        }

        protected abstract bool ChainCompareIsEqual(object other, int[] result);
        
        #region IChainCompare Members

        public bool ChainCompare(SortFields other, int[] result)
        {
            return ChainCompareIsEqual(other, result);
        }

        #endregion

        internal SortFields Clone()
        {
            SortFields result = (SortFields)base.MemberwiseClone();
            return result;
        }
    }

    // in .Net 3.5 you can't create types dynamically.
    // Using those SortField below however we can create dynamic class with fixed field count but arbitrary types.
    // This is useful for indexes

#pragma warning disable 649

    internal class SortFields<T1> : SortFields
        where T1 : IComparable<T1>
    {
        public T1 f1;

        protected override bool ChainCompareIsEqual(object other, int[] result)
        {
            int cmp = f1.CompareTo(((SortFields<T1>)other).f1);
            result[0] = cmp;
            return cmp == 0;
        }

        public override string ToString()
        {
            return f1.ToString();
        }
    }

    internal class SortFields<T1, T2> : SortFields<T1>
        where T1 : IComparable<T1>
        where T2 : IComparable<T2>
    {
        public T2 f2;

        protected override bool ChainCompareIsEqual(object other, int[] result)
        {
            if (base.ChainCompareIsEqual(other, result))
            {
                int cmp = f2.CompareTo(((SortFields<T1, T2>)other).f2);
                result[1] = cmp;
                return cmp == 0;
            }
            else return false;
        }
    }

    // that's enough for now (but we should extend to at least 4 or 5)
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Pascal Ganaye
Software Developer (Senior)
United Kingdom United Kingdom
I am a French programmer.
These days I spend most of my time with the .NET framework, JavaScript and html.

| Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 10 Jun 2009
Article Copyright 2009 by Pascal Ganaye
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid