//-----------------------------------------------------------------------
// 
//     Copyright (c) Microsoft Corporation.
// 
//-----------------------------------------------------------------------
namespace Microsoft.Isam.Esent.Interop
{
    using System;
    using System.Diagnostics;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Runtime.InteropServices;
    /// 
    /// The native version of the JET_COLUMNDEF structure.
    /// 
    [StructLayout(LayoutKind.Sequential)]
    [SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules",
        "SA1305:FieldNamesMustNotUseHungarianNotation",
        Justification = "This should match the unmanaged API, which isn't capitalized.")]
    [SuppressMessage(
        "Microsoft.StyleCop.CSharp.NamingRules",
        "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
        Justification = "This should match the unmanaged API, which isn't capitalized.")]
    internal struct NATIVE_COLUMNDEF
    {
        /// 
        /// Size of the structure.
        /// 
        public uint cbStruct;
        /// 
        /// Column ID.
        /// 
        public uint columnid;
        /// 
        /// Type of the column.
        /// 
        public uint coltyp;
        /// 
        /// Reserved. Should be 0.
        /// 
        [Obsolete("Reserved")]
        public ushort wCountry;
        /// 
        /// Obsolete. Should be 0.
        /// 
        [Obsolete("Use cp")]
        public ushort langid;
        /// 
        /// Code page for text columns.
        /// 
        public ushort cp;
        /// 
        /// Reserved. Should be 0.
        /// 
        [Obsolete("Reserved")]
        public ushort wCollate;
        /// 
        /// Maximum length of the column.
        /// 
        public uint cbMax;
        /// 
        /// Column options.
        /// 
        public uint grbit;
    }
    /// 
    /// Describes a column in a table of an ESENT database.
    /// 
    [SuppressMessage(
        "Microsoft.StyleCop.CSharp.NamingRules",
        "SA1300:ElementMustBeginWithUpperCaseLetter",
        Justification = "This should match the unmanaged API, which isn't capitalized.")]
    [Serializable]
    public sealed class JET_COLUMNDEF : IContentEquatable, IDeepCloneable
    {
        /// 
        /// The type of the column.
        /// 
        private JET_coltyp columnType;
        /// 
        /// The code page. Only valid for text columns.
        /// 
        private JET_CP codePage;
        /// 
        /// Maximum size of the column.
        /// 
        private int maxSize;
        /// 
        /// Id of the column. Not serialized because it is an internal
        /// value and shouldn't be persisted.
        /// 
        [NonSerialized]
        private JET_COLUMNID id;
        /// 
        /// Column options.
        /// 
        private ColumndefGrbit options;
        /// 
        /// Gets or sets type of the column.
        /// 
        public JET_coltyp coltyp
        {
            [DebuggerStepThrough]
            get { return this.columnType; }
            set { this.columnType = value; }
        }
        /// 
        /// Gets or sets code page of the column. This is only meaningful for columns of type
        ///  and .
        /// 
        public JET_CP cp
        {
            [DebuggerStepThrough]
            get { return this.codePage; }
            set { this.codePage = value; }
        }
        /// 
        /// Gets or sets the maximum length of the column. This is only meaningful for columns of
        /// type , ,  and
        /// .
        /// 
        public int cbMax
        {
            [DebuggerStepThrough]
            get { return this.maxSize; }
            set { this.maxSize = value; }
        }
        /// 
        /// Gets or sets the column options.
        /// 
        public ColumndefGrbit grbit
        {
            [DebuggerStepThrough]
            get { return this.options; }
            set { this.options = value; }
        }
        /// 
        /// Gets the columnid of the column.
        /// 
        public JET_COLUMNID columnid
        {
            [DebuggerStepThrough]
            get { return this.id; }
            internal set { this.id = value; }
        }
        /// 
        /// Returns a  that represents the current .
        /// 
        /// 
        /// A  that represents the current .
        /// 
        public override string ToString()
        {
            return string.Format(CultureInfo.InvariantCulture, "JET_COLUMNDEF({0},{1})", this.columnType, this.options);
        }
        /// 
        /// Returns a value indicating whether this instance is equal
        /// to another instance.
        /// 
        /// An instance to compare with this instance.
        /// True if the two instances are equal.
        public bool ContentEquals(JET_COLUMNDEF other)
        {
            if (null == other)
            {
                return false;
            }
            return this.columnType == other.columnType
                   && this.codePage == other.codePage
                   && this.maxSize == other.maxSize
                   && this.id == other.id
                   && this.options == other.options;
        }
        /// 
        /// Returns a deep copy of the object.
        /// 
        /// A deep copy of the object.
        public JET_COLUMNDEF DeepClone()
        {
            return (JET_COLUMNDEF)this.MemberwiseClone();
        }
        /// 
        /// Returns the unmanaged columndef that represents this managed class.
        /// 
        /// A native (interop) version of the JET_COLUMNDEF.
        internal NATIVE_COLUMNDEF GetNativeColumndef()
        {
            var columndef = new NATIVE_COLUMNDEF();
            columndef.cbStruct = checked((uint)Marshal.SizeOf(typeof(NATIVE_COLUMNDEF)));
            columndef.cp = (ushort)this.cp;
            columndef.cbMax = checked((uint)this.cbMax);
            columndef.grbit = (uint)this.grbit;
            columndef.coltyp = checked((uint)this.coltyp);
            return columndef;
        }
        /// 
        /// Sets the fields of the object from a native JET_COLUMNDEF struct.
        /// 
        /// 
        /// The native columndef to set the values from.
        /// 
        internal void SetFromNativeColumndef(NATIVE_COLUMNDEF value)
        {
            this.coltyp = (JET_coltyp)value.coltyp;
            this.cp = (JET_CP)value.cp;
            this.cbMax = checked((int)value.cbMax);
            this.grbit = (ColumndefGrbit)value.grbit;
            this.columnid = new JET_COLUMNID { Value = value.columnid };
        }
    }
}