//-----------------------------------------------------------------------
// 
//     Copyright (c) Microsoft Corporation.
// 
//-----------------------------------------------------------------------
namespace Microsoft.Isam.Esent.Interop
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Runtime.InteropServices;
    /// 
    /// The native ANSI version of the JET_COLUMNBASE structure.
    /// 
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    [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_COLUMNBASE
    {
        /// 
        /// 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 .
        /// 
        [Obsolete("Use cp")]
        public ushort langid;
        /// 
        /// Code page for text columns.
        /// 
        public ushort cp;
        /// 
        /// Reserved. Should be 0.
        /// 
        [Obsolete("Reserved")]
        public ushort wFiller;
        /// 
        /// Maximum length of the column.
        /// 
        public uint cbMax;
        /// 
        /// Column options.
        /// 
        public uint grbit;
        /// 
        /// The table from which the current table inherits its DDL.
        /// 
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NameSize)]
        public string szBaseTableName;
        /// 
        /// The name of the column in the template table.
        /// 
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NameSize)]
        public string szBaseColumnName;
        /// 
        /// Max size of the table/column name string.
        /// 
        private const int NameSize = 256;
    }
    /// 
    /// The native Unicode version of the JET_COLUMNBASE structure.
    /// 
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    [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_COLUMNBASE_WIDE
    {
        /// 
        /// 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 .
        /// 
        [Obsolete("Use cp")]
        public ushort langid;
        /// 
        /// Code page for text columns.
        /// 
        public ushort cp;
        /// 
        /// Reserved. Should be 0.
        /// 
        [Obsolete("Reserved")]
        public ushort wFiller;
        /// 
        /// Maximum length of the column.
        /// 
        public uint cbMax;
        /// 
        /// Column options.
        /// 
        public uint grbit;
        /// 
        /// The table from which the current table inherits its DDL.
        /// 
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NameSize)]
        public string szBaseTableName;
        /// 
        /// The name of the column in the template table.
        /// 
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NameSize)]
        public string szBaseColumnName;
        /// 
        /// Max size of the table/column name string.
        /// 
        private const int NameSize = 256;
    }
    /// 
    /// 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.")]
    public sealed class JET_COLUMNBASE : IEquatable
    {
        /// 
        /// Initializes a new instance of the  class.
        /// 
        internal JET_COLUMNBASE()
        {            
        }
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// 
        /// The value.
        /// 
        internal JET_COLUMNBASE(NATIVE_COLUMNBASE 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 };
            this.szBaseTableName = StringCache.TryToIntern(value.szBaseTableName);
            this.szBaseColumnName = StringCache.TryToIntern(value.szBaseColumnName);
        }
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// 
        /// The value.
        /// 
        internal JET_COLUMNBASE(NATIVE_COLUMNBASE_WIDE 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
            };
            this.szBaseTableName = StringCache.TryToIntern(value.szBaseTableName);
            this.szBaseColumnName = StringCache.TryToIntern(value.szBaseColumnName);
        }
        /// 
        /// Gets type of the column.
        /// 
        public JET_coltyp coltyp { get; internal set; }
        /// 
        /// Gets code page of the column. This is only meaningful for columns of type
        ///  and .
        /// 
        public JET_CP cp { get; internal set; }
        /// 
        /// Gets the maximum length of the column. This is only meaningful for columns of
        /// type , ,  and
        /// .
        /// 
        public int cbMax { get; internal set; }
        /// 
        /// Gets the column options.
        /// 
        public ColumndefGrbit grbit { get; internal set; }
        /// 
        /// Gets the columnid of the column.
        /// 
        public JET_COLUMNID columnid { get; internal set; }
        /// 
        /// Gets the table from which the current table inherits its DDL.
        /// 
        public string szBaseTableName { get; internal set; }
        /// 
        /// Gets the name of the column in the template table.
        /// 
        public string szBaseColumnName { get; internal set; }
        /// 
        /// Returns a  that represents the current .
        /// 
        /// 
        /// A  that represents the current .
        /// 
        public override string ToString()
        {
            return string.Format(CultureInfo.InvariantCulture, "JET_COLUMNBASE({0},{1})", this.coltyp, this.grbit);
        }
        /// 
        /// Returns the hash code for this instance.
        /// 
        /// The hash code for this instance.
        public override int GetHashCode()
        {
            var hashes = new[]
            {
                this.coltyp.GetHashCode(),
                this.cp.GetHashCode(),
                this.cbMax,
                this.grbit.GetHashCode(),
                this.columnid.GetHashCode(),
                this.szBaseTableName.GetHashCode(),
                this.szBaseColumnName.GetHashCode(),
            };
            return Util.CalculateHashCode(hashes);
        }
        /// 
        /// Returns a value indicating whether this instance is equal
        /// to another instance.
        /// 
        /// An object to compare with this instance.
        /// True if the two instances are equal.
        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return false;
            }
            return this.Equals((JET_COLUMNBASE)obj);
        }
        /// 
        /// 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 Equals(JET_COLUMNBASE other)
        {
            if (null == other)
            {
                return false;
            }
            return this.coltyp == other.coltyp
                   && this.cp == other.cp
                   && this.cbMax == other.cbMax
                   && this.columnid == other.columnid
                   && this.grbit == other.grbit
                   && string.Equals(this.szBaseTableName, other.szBaseTableName, StringComparison.Ordinal)
                   && string.Equals(this.szBaseColumnName, other.szBaseColumnName, StringComparison.Ordinal);
        }
    }
}