//-----------------------------------------------------------------------
// 
//     Copyright (c) Microsoft Corporation.
// 
//-----------------------------------------------------------------------
namespace Microsoft.Isam.Esent.Interop
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Runtime.InteropServices;
    /// 
    /// Native (unmanaged) version of the JET_ENUMCOLUMN structure.
    /// 
    [StructLayout(LayoutKind.Sequential)]
    [SuppressMessage(
        "Microsoft.StyleCop.CSharp.NamingRules",
        "SA1300:ElementMustBeginWithUpperCaseLetter",
        Justification = "This should match the unmanaged API, which isn't capitalized.")]
    [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 unsafe struct NATIVE_ENUMCOLUMN
    {
        /// 
        /// The columnid that was enumerated.
        /// 
        public uint columnid;
        /// 
        /// The column status code from the enumeration of the column.
        /// 
        public int err;
        /// 
        /// The size of the value that was enumerated for the column.
        /// This member is only used if  is equal to
        /// .
        /// 
        /// 
        /// The unmanaged JET_ENUMCOLUMN structure is a union so this
        /// is aliased with cEnumColumnValue.
        /// 
        public uint cbData;
        /// 
        /// The the value that was enumerated for the column.
        /// This member is only used if  is equal to
        /// .
        /// 
        /// 
        /// The unmanaged JET_ENUMCOLUMN structure is a union so this
        /// is aliased with rgEnumColumnValue.
        /// 
        public IntPtr pvData;
        /// 
        /// Gets or sets the number of entries in rgEnumColumnValue.
        /// This member is only used if  is not
        /// .
        /// 
        /// 
        /// The unmanaged JET_ENUMCOLUMN structure is a union so this
        /// property uses cbData as its backing storage.
        /// 
        public uint cEnumColumnValue
        {
            get
            {
                return this.cbData;
            }
            set
            {
                this.cbData = value;
            }
        }
        /// 
        /// Gets or sets an array of column values.
        /// This member is only used if  is not
        /// .
        /// 
        /// 
        /// The unmanaged JET_ENUMCOLUMN structure is a union so this
        /// property uses pvData as its backing storage.
        /// 
        public NATIVE_ENUMCOLUMNVALUE* rgEnumColumnValue
        {
            get
            {
                return (NATIVE_ENUMCOLUMNVALUE*)this.pvData;
            }
            set
            {
                this.pvData = new IntPtr(value);
            }
        }
    }
    /// 
    /// Enumerates the column values of a record using the JetEnumerateColumns
    /// function. JetEnumerateColumns returns an array of JET_ENUMCOLUMNVALUE
    /// structures. The array is returned in memory that was allocated using
    /// the callback that was supplied to that function.
    /// 
    [SuppressMessage(
        "Microsoft.StyleCop.CSharp.NamingRules",
        "SA1300:ElementMustBeginWithUpperCaseLetter",
        Justification = "This should match the unmanaged API, which isn't capitalized.")]
    public class JET_ENUMCOLUMN
    {
        /// 
        /// Gets the columnid ID that was enumerated.
        /// 
        public JET_COLUMNID columnid { get; internal set; }
        /// 
        /// Gets the column status code that results from the enumeration.
        /// 
        /// 
        public JET_wrn err { get; internal set; }
        /// 
        /// Gets the number of column values enumerated for the column.
        /// This member is only used if  is not
        /// .
        /// 
        public int cEnumColumnValue { get; internal set; }
        /// 
        /// Gets the enumerated column values for the column.
        /// This member is only used if  is not
        /// .
        /// 
        public JET_ENUMCOLUMNVALUE[] rgEnumColumnValue { get; internal set; }
        /// 
        /// Gets the size of the value that was enumerated for the column.
        /// This member is only used if  is equal to
        /// .
        /// 
        public int cbData { get; internal set; }
        /// 
        /// Gets the the value that was enumerated for the column.
        /// This member is only used if  is equal to
        /// .
        /// This points to memory allocated with the 
        ///  allocator callback passed to
        /// .
        /// Remember to release the memory when finished.
        /// 
        public IntPtr pvData { get; internal set; }
        /// 
        /// Returns a  that represents the current .
        /// 
        /// 
        /// A  that represents the current .
        /// 
        public override string ToString()
        {
            return string.Format(CultureInfo.InvariantCulture, "JET_ENUMCOLUMN(0x{0:x})", this.columnid);
        }
        /// 
        /// Sets the fields of the object from a native JET_ENUMCOLUMN struct.
        /// 
        /// 
        /// The native enumcolumn to set the values from.
        /// 
        internal void SetFromNativeEnumColumn(NATIVE_ENUMCOLUMN value)
        {
            this.columnid = new JET_COLUMNID { Value = value.columnid };
            this.err = (JET_wrn)value.err;
            if (JET_wrn.ColumnSingleValue == this.err)
            {
                this.cbData = checked((int)value.cbData);
                this.pvData = value.pvData;
            }
            else
            {
                this.cEnumColumnValue = checked((int)value.cEnumColumnValue);
                this.rgEnumColumnValue = null;
            }
        }
    }
}