// ---------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------
//
//
// ---------------------------------------------------------------------
namespace Microsoft.Database.Isam
{
using System;
using Microsoft.Isam.Esent.Interop;
using Microsoft.Isam.Esent.Interop.Vista;
///
/// Identifies a column in a table
///
///
/// A Columnid contains the name of a column and its internal identifier.
/// A Columnid also encodes the type of the column which is used for conversions to and from CLR objects.
/// Retrieving an column by columnid is more efficient than retrieving a column by name, as the name to
/// columnid and type lookup can be expensive
///
public class Columnid
{
///
/// The columnid
///
private readonly JET_COLUMNID columnid;
///
/// The column type of this column.
///
private readonly JET_coltyp coltyp;
///
/// The name
///
private readonly string name;
///
/// The CLR type that closest represents the data stored in the database.
///
private readonly Type type;
///
/// Whether the column contains ASCII data (text columns only).
///
private readonly bool isAscii;
///
/// Initializes a new instance of the class.
///
/// The column identifier.
internal Columnid(JET_COLUMNBASE columnbase)
: this(columnbase.szBaseColumnName, columnbase.columnid, columnbase.coltyp, columnbase.cp == JET_CP.ASCII)
{
}
///
/// Initializes a new instance of the class.
///
/// The column name.
/// The column identifier.
/// The column type.
/// If it's a text column, whether the data is ASCII or Unicode.
internal Columnid(string name, JET_COLUMNID columnid, JET_coltyp coltyp, bool isAscii)
{
this.name = name;
this.columnid = columnid;
this.coltyp = coltyp;
this.isAscii = isAscii;
switch (coltyp)
{
case JET_coltyp.Nil:
throw new ArgumentOutOfRangeException("columnid.Type", "Nil is not valid");
case JET_coltyp.Bit:
this.type = typeof(bool);
break;
case JET_coltyp.UnsignedByte:
this.type = typeof(byte);
break;
case JET_coltyp.Short:
this.type = typeof(short);
break;
case JET_coltyp.Long:
this.type = typeof(int);
break;
case JET_coltyp.Currency:
this.type = typeof(long);
break;
case JET_coltyp.IEEESingle:
this.type = typeof(float);
break;
case JET_coltyp.IEEEDouble:
this.type = typeof(double);
break;
case JET_coltyp.DateTime:
this.type = typeof(DateTime);
break;
case JET_coltyp.Binary:
this.type = typeof(byte[]);
break;
case JET_coltyp.Text:
this.type = typeof(string);
break;
case JET_coltyp.LongBinary:
this.type = typeof(byte[]);
break;
case JET_coltyp.LongText:
this.type = typeof(string);
break;
case VistaColtyp.UnsignedLong:
this.type = typeof(uint);
break;
case VistaColtyp.LongLong:
this.type = typeof(long);
break;
case VistaColtyp.GUID:
this.type = typeof(Guid);
break;
case VistaColtyp.UnsignedShort:
this.type = typeof(ushort);
break;
default:
throw new ArgumentOutOfRangeException("columnid.Coltyp", "unknown type");
}
}
///
/// Gets a value indicating whether the column contains ASCII data (text columns only).
///
public bool IsAscii
{
get
{
return this.isAscii;
}
}
///
/// Gets the name of the column
///
///
/// A column name is only unique in the context of a specific table.
///
public string Name
{
get
{
return this.name;
}
}
///
/// Gets the type of the column.
///
///
/// The type.
///
public Type Type
{
get
{
return this.type;
}
}
///
/// Gets the interop columnid.
///
///
/// The interop columnid.
///
internal JET_COLUMNID InteropColumnid
{
get
{
return this.columnid;
}
}
///
/// Gets the underlying ESE of the column.
///
internal JET_coltyp Coltyp
{
get
{
return this.coltyp;
}
}
///
/// Returns a that represents this instance.
///
///
/// A that represents this instance.
///
public override string ToString()
{
return string.Format("Columnid({0}, {1}, {2}, IsAscii={3})", this.Name, this.type.Name, this.coltyp, this.isAscii);
}
}
}