//-----------------------------------------------------------------------
//
// 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_COLUMNCREATE 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_COLUMNCREATE
{
///
/// Size of the structure.
///
public uint cbStruct;
///
/// Name of the column.
///
public IntPtr szColumnName;
///
/// Type of the columnn.
///
public uint coltyp;
///
/// The maximum length of this column (only relevant for binary and text columns).
///
public uint cbMax;
///
/// Column options.
///
public uint grbit;
///
/// Default value (NULL if none).
///
public IntPtr pvDefault;
///
/// Size of the default value.
///
public uint cbDefault;
///
/// Code page (for text columns only).
///
public uint cp;
///
/// The returned column id.
///
public uint columnid;
///
/// The returned error code.
///
public int err;
}
///
/// 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_COLUMNCREATE : IContentEquatable, IDeepCloneable
{
///
/// Name of the column.
///
private string name;
///
/// The type of the column.
///
private JET_coltyp columnType;
///
/// Maximum size of the column.
///
private int maxSize;
///
/// Column options.
///
private ColumndefGrbit options;
///
/// Default value (NULL if none).
///
private byte[] defaultValue;
///
/// Size of the default value.
///
private int defaultValueSize;
///
/// The code page. Only valid for text columns.
///
private JET_CP codePage;
///
/// Id of the column. Not serialized because it is an internal
/// value and shouldn't be persisted.
///
[NonSerialized]
private JET_COLUMNID id;
///
/// The returned error code.
///
private JET_err errorCode;
///
/// Gets or sets the name of the column to create.
///
public string szColumnName
{
[DebuggerStepThrough]
get { return this.name; }
set { this.name = value; }
}
///
/// Gets or sets type of the column.
///
public JET_coltyp coltyp
{
[DebuggerStepThrough]
get { return this.columnType; }
set { this.columnType = 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 or sets the default value (NULL if none).
///
public byte[] pvDefault
{
get { return this.defaultValue; }
set { this.defaultValue = value; }
}
///
/// Gets or sets the size of the default value.
///
public int cbDefault
{
get { return this.defaultValueSize; }
set { this.defaultValueSize = 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 the columnid of the column.
///
public JET_COLUMNID columnid
{
[DebuggerStepThrough]
get { return this.id; }
internal set { this.id = value; }
}
///
/// Gets or sets the error code from creating this column.
///
public JET_err err
{
[DebuggerStepThrough]
get { return this.errorCode; }
set { this.errorCode = value; }
}
///
/// 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_COLUMNCREATE other)
{
if (null == other)
{
return false;
}
this.CheckMembersAreValid();
other.CheckMembersAreValid();
return this.err == other.err
&& this.szColumnName == other.szColumnName
&& this.coltyp == other.coltyp
&& this.cbMax == other.cbMax
&& this.grbit == other.grbit
&& this.cbDefault == other.cbDefault
&& this.cp == other.cp
&& this.columnid == other.columnid
&& Util.ArrayEqual(this.pvDefault, other.pvDefault, 0, other.cbDefault);
}
#region IDeepCloneable
///
/// Returns a deep copy of the object.
///
/// A deep copy of the object.
public JET_COLUMNCREATE DeepClone()
{
JET_COLUMNCREATE result = (JET_COLUMNCREATE)this.MemberwiseClone();
if (this.pvDefault != null)
{
result.pvDefault = new byte[this.pvDefault.Length];
Array.Copy(this.pvDefault, result.pvDefault, this.pvDefault.Length);
}
return result;
}
#endregion
///
/// Generate a string representation of the instance.
///
/// The structure as a string.
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "JET_COLUMNCREATE({0},{1},{2})", this.szColumnName, this.coltyp, this.grbit);
}
///
/// Check this object to make sure its parameters are valid.
///
internal void CheckMembersAreValid()
{
if (null == this.szColumnName)
{
throw new ArgumentNullException("szColumnName");
}
if (this.cbDefault < 0)
{
throw new ArgumentOutOfRangeException("cbDefault", this.cbDefault, "cannot be negative");
}
if (null == this.pvDefault && 0 != this.cbDefault)
{
throw new ArgumentOutOfRangeException("cbDefault", this.cbDefault, "must be 0");
}
if (null != this.pvDefault && (this.cbDefault > this.pvDefault.Length))
{
throw new ArgumentOutOfRangeException("cbDefault", this.cbDefault, "can't be greater than pvDefault.Length");
}
}
///
/// Returns the unmanaged columncreate that represents this managed class.
/// , , ,
/// and are not converted.
///
/// A native (interop) version of the JET_COLUMNCREATE.
internal NATIVE_COLUMNCREATE GetNativeColumnCreate()
{
var native = new NATIVE_COLUMNCREATE();
native.cbStruct = checked((uint)Marshal.SizeOf(typeof(NATIVE_COLUMNCREATE)));
// columncreate.szColumnName is converted at pinvoke time.
native.szColumnName = IntPtr.Zero;
native.coltyp = (uint)this.coltyp;
native.cbMax = (uint)this.cbMax;
native.grbit = (uint)this.grbit;
// columncreate.pvDefault is converted at pinvoke time.
native.pvDefault = IntPtr.Zero;
native.cbDefault = checked((uint)this.cbDefault);
native.cp = (uint)this.cp;
return native;
}
///
/// Sets only the output fields of the object from a native JET_COLUMNCREATE struct,
/// specifically and .
///
///
/// The native columncreate to set the values from.
///
internal void SetFromNativeColumnCreate(NATIVE_COLUMNCREATE value)
{
this.columnid = new JET_COLUMNID { Value = value.columnid };
this.err = (JET_err)value.err;
}
}
}