// ---------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------
//
//
// ---------------------------------------------------------------------
namespace Microsoft.Database.Isam
{
using System;
using System.Collections;
using System.Globalization;
///
/// A class used to store the table handles in this database.
///
internal class TempTableHandleCollection : DictionaryBase, IEnumerable
{
///
/// The is key unique identifier
///
private readonly bool isKeyGuid = false;
///
/// Initializes a new instance of the class.
///
/// if set to true [use unique identifier].
internal TempTableHandleCollection(bool useGuid)
{
this.isKeyGuid = useGuid;
}
///
/// Gets a value indicating whether [is key unique identifier].
///
///
/// true if [is key unique identifier]; otherwise, false.
///
public bool IsKeyGuid
{
get
{
return this.isKeyGuid;
}
}
///
/// Gets the names.
///
///
/// The names.
///
public ICollection Names
{
get
{
return this.Dictionary.Keys;
}
}
///
/// Gets or sets the for the specified table name
///
///
/// The .
///
/// Name of the table to get or set.
/// A for the specified table name.
public TempTableHandle this[string tableName]
{
get
{
return (TempTableHandle)this.Dictionary[tableName.ToLower(CultureInfo.InvariantCulture)];
}
set
{
this.Dictionary[tableName.ToLower(CultureInfo.InvariantCulture)] = value;
}
}
///
/// Fetches an enumerator containing all the table handles in this database.
///
/// An enumerator containing all the table handles in this database.
///
/// This is the type safe version that may not work in other CLR
/// languages.
///
public new TempTableHandleEnumerator GetEnumerator()
{
return new TempTableHandleEnumerator(this.Dictionary.GetEnumerator());
}
///
/// Fetches an enumerator containing all the tables in this database
///
///
/// An object that can be used to iterate through the collection.
///
///
/// This is the standard version that will work with other CLR
/// languages.
///
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)this.GetEnumerator();
}
///
/// Adds the specified temporary table handle.
///
/// The temporary table handle.
public void Add(TempTableHandle tempTableHandle)
{
if (this.isKeyGuid)
{
this.Dictionary.Add(tempTableHandle.Guid.ToString().ToLower(CultureInfo.InvariantCulture), tempTableHandle);
}
else
{
this.Dictionary.Add(tempTableHandle.Name.ToLower(CultureInfo.InvariantCulture), tempTableHandle);
}
}
///
/// Determines if the database contains a table with the given name.
///
/// Name of the table.
/// Whether the database contains a table with the given name.
public bool Contains(string tableName)
{
return this.Dictionary.Contains(tableName.ToLower(CultureInfo.InvariantCulture));
}
///
/// Removes the specified table name.
///
/// Name of the table.
public void Remove(string tableName)
{
this.Dictionary.Remove(tableName.ToLower(CultureInfo.InvariantCulture));
}
///
/// Performs additional custom processes when validating the element with the specified key and value.
///
/// The key of the element to validate.
/// The value of the element to validate.
///
/// key must be of type System.String;key
/// or
/// value must be of type TempTableHandle;value
/// or
/// key must match value.Guid.ToString();key
/// or
/// key must match value.Name;key
///
protected override void OnValidate(object key, object value)
{
if (!(key is string))
{
throw new ArgumentException("key must be of type System.String", "key");
}
if (!(value is TempTableHandle))
{
throw new ArgumentException("value must be of type TempTableHandle", "value");
}
if (this.isKeyGuid)
{
if (((string)key).ToLower(CultureInfo.InvariantCulture) != ((TempTableHandle)value).Guid.ToString().ToLower(CultureInfo.InvariantCulture))
{
throw new ArgumentException("key must match value.Guid.ToString()", "key");
}
}
else
{
if (((string)key).ToLower(CultureInfo.InvariantCulture) != ((TempTableHandle)value).Name.ToLower(CultureInfo.InvariantCulture))
{
throw new ArgumentException("key must match value.Name", "key");
}
}
}
}
}