//-----------------------------------------------------------------------
// 
//     Copyright (c) Microsoft Corporation.
// 
//-----------------------------------------------------------------------
namespace Microsoft.Isam.Esent.Interop
{
    using System;
    using System.Diagnostics;
    /// 
    /// A Unicode string column value.
    /// 
    public class StringColumnValue : ColumnValue
    {
        /// 
        /// Internal value.
        /// 
        private string internalValue;
        /// 
        /// Gets the last set or retrieved value of the column. The
        /// value is returned as a generic object.
        /// 
        public override object ValueAsObject
        {
            [DebuggerStepThrough]
            get { return this.Value; }
        }
        /// 
        /// Gets or sets the value of the column. Use  to update a
        /// record with the column value.
        /// 
        public string Value
        {
            get
            {
                return this.internalValue;
            }
            set
            {
                this.internalValue = value;
                this.Error = value == null ? JET_wrn.ColumnNull : JET_wrn.Success;
            }
        }
        /// 
        /// Gets the byte length of a column value, which is zero if column is null, otherwise
        /// it matches the byte length of the string value. The byte length is determined in
        /// assumption of two bytes per character.
        /// 
        public override int Length
        {
            get { return this.Value != null ? this.Value.Length * sizeof(char) : 0; }
        }
        /// 
        /// Gets the size of the value in the column. This returns 0 for
        /// variable sized columns (i.e. binary and string).
        /// 
        protected override int Size
        {
            [DebuggerStepThrough]
            get { return 0; }
        }
        /// 
        /// Gets a string representation of this object.
        /// 
        /// A string representation of this object.
        public override string ToString()
        {
            return this.Value;
        }
        /// 
        /// Recursive SetColumns method for data pinning. This populates the buffer and
        /// calls the inherited SetColumns method.
        /// 
        /// The session to use.
        /// 
        /// The table to set the columns in. An update should be prepared.
        /// 
        /// 
        /// Column values to set.
        /// 
        /// 
        /// Structures to put the pinned data in.
        /// 
        /// Offset of this object in the array.
        /// An error code.
        internal override unsafe int SetColumns(JET_SESID sesid, JET_TABLEID tableid, ColumnValue[] columnValues, NATIVE_SETCOLUMN* nativeColumns, int i)
        {
            if (null != this.Value)
            {
                fixed (void* buffer = this.Value)
                {
                    return this.SetColumns(
                        sesid, tableid, columnValues, nativeColumns, i, buffer, checked(this.Value.Length * sizeof(char)), true);
                }
            }
            return this.SetColumns(sesid, tableid, columnValues, nativeColumns, i, null, 0, false);
        }
        /// 
        /// Given data retrieved from ESENT, decode the data and set the value in the ColumnValue object.
        /// 
        /// An array of bytes.
        /// The starting position within the bytes.
        /// The number of bytes to decode.
        /// The error returned from ESENT.
        protected override void GetValueFromBytes(byte[] value, int startIndex, int count, int err)
        {
            if (JET_wrn.ColumnNull == (JET_wrn)err)
            {
                this.Value = null;
            }
            else
            {
                this.Value = StringCache.GetString(value, startIndex, count);
            }
        }
    }
}