1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
//---------------------------------------------------------------------
// <copyright file="Metadata.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Metadata.Edm;
using System.Globalization;
using System.Diagnostics;
namespace System.Data.Query.InternalTrees
{
/// <summary>
/// Describes metadata about a table
/// </summary>
internal class TableMD
{
private List<ColumnMD> m_columns;
private List<ColumnMD> m_keys;
private EntitySetBase m_extent; // null for transient tables
private bool m_flattened;
/// <summary>
/// private initializer
/// </summary>
/// <param name="extent">the entity set corresponding to this table (if any)</param>
private TableMD(EntitySetBase extent)
{
m_columns = new List<ColumnMD>();
m_keys = new List<ColumnMD>();
m_extent = extent;
}
/// <summary>
/// Create a typed-table definition corresponding to an entityset (if specified)
///
/// The table has exactly one column - the type of the column is specified by
/// the "type" parameter. This table is considered to be un-"flattened"
/// </summary>
/// <param name="type">type of each element (row) of the table</param>
/// <param name="extent">entityset corresponding to the table (if any)</param>
internal TableMD(TypeUsage type, EntitySetBase extent)
: this(extent)
{
m_columns.Add(new ColumnMD(this, "element", type));
m_flattened = !PlanCompiler.TypeUtils.IsStructuredType(type);
}
/// <summary>
/// Creates a "flattened" table definition.
///
/// The table has one column for each specified property in the "properties" parameter.
/// The name and datatype of each table column are taken from the corresponding property.
///
/// The keys of the table (if any) are those specified in the "keyProperties" parameter
///
/// The table may correspond to an entity set (if the entityset parameter was non-null)
/// </summary>
/// <param name="properties">prperties corresponding to columns of the table</param>
/// <param name="keyProperties"></param>
/// <param name="extent">entityset corresponding to the table (if any)</param>
internal TableMD(IEnumerable<EdmProperty> properties, IEnumerable<EdmMember> keyProperties,
EntitySetBase extent)
: this(extent)
{
Dictionary<string, ColumnMD> columnMap = new Dictionary<string, ColumnMD>();
m_flattened = true;
foreach (EdmProperty p in properties)
{
ColumnMD newColumn = new ColumnMD(this, p);
m_columns.Add(newColumn);
columnMap[p.Name] = newColumn;
}
foreach (EdmMember p in keyProperties)
{
ColumnMD keyColumn;
if (!columnMap.TryGetValue(p.Name, out keyColumn))
{
Debug.Assert(false, "keyMember not in columns?");
}
else
{
m_keys.Add(keyColumn);
}
}
}
/// <summary>
/// The extent metadata (if any)
/// </summary>
internal EntitySetBase Extent { get { return m_extent; } }
/// <summary>
/// List of columns of this table
/// </summary>
internal List<ColumnMD> Columns { get { return m_columns; } }
/// <summary>
/// Keys for this table
/// </summary>
internal List<ColumnMD> Keys { get { return m_keys; } }
/// <summary>
/// Is this table a "flat" table?
/// </summary>
internal bool Flattened { get { return m_flattened; } }
/// <summary>
/// String form - for debugging
/// </summary>
/// <returns></returns>
public override string ToString()
{
return (m_extent != null ? m_extent.Name : "Transient");
}
}
/// <summary>
/// Describes information about each column
/// </summary>
internal class ColumnMD
{
private string m_name;
private TypeUsage m_type;
private EdmMember m_property;
/// <summary>
/// Default constructor
/// </summary>
/// <param name="table">Table containing this column</param>
/// <param name="name">Column name</param>
/// <param name="type">Datatype of the column</param>
internal ColumnMD(TableMD table, string name, TypeUsage type)
{
m_name = name;
m_type = type;
}
/// <summary>
/// More useful default constructor
/// </summary>
/// <param name="table">table containing this column</param>
/// <param name="property">property describing this column</param>
internal ColumnMD(TableMD table, EdmMember property)
: this(table, property.Name, property.TypeUsage)
{
m_property = property;
}
/// <summary>
/// Column Name
/// </summary>
internal string Name { get { return m_name; } }
/// <summary>
/// Datatype of the column
/// </summary>
internal TypeUsage Type { get { return m_type; } }
/// <summary>
/// Is this column nullable ?
/// </summary>
internal bool IsNullable
{
get
{
return (m_property == null) || TypeSemantics.IsNullable(m_property);
}
}
/// <summary>
/// debugging help
/// </summary>
/// <returns></returns>
public override string ToString()
{
return m_name;
}
}
/// <summary>
/// Represents one instance of a table. Contains the table metadata
/// </summary>
internal class Table
{
private TableMD m_tableMetadata;
private VarList m_columns;
private VarVec m_referencedColumns;
private VarVec m_keys;
private VarVec m_nonnullableColumns;
private int m_tableId;
internal Table(Command command, TableMD tableMetadata, int tableId)
{
m_tableMetadata = tableMetadata;
m_columns = Command.CreateVarList();
m_keys = command.CreateVarVec();
m_nonnullableColumns = command.CreateVarVec();
m_tableId = tableId;
Dictionary<string, ColumnVar> columnVarMap = new Dictionary<string, ColumnVar>();
foreach (ColumnMD c in tableMetadata.Columns)
{
ColumnVar v = command.CreateColumnVar(this, c);
columnVarMap[c.Name] = v;
if (!c.IsNullable)
{
m_nonnullableColumns.Set(v);
}
}
foreach (ColumnMD c in tableMetadata.Keys)
{
ColumnVar v = columnVarMap[c.Name];
m_keys.Set(v);
}
m_referencedColumns = command.CreateVarVec(m_columns);
}
/// <summary>
/// Metadata for the table instance
/// </summary>
internal TableMD TableMetadata { get { return m_tableMetadata; } }
/// <summary>
/// List of column references
/// </summary>
internal VarList Columns { get { return m_columns; } }
/// <summary>
/// Get the list of all referenced columns.
/// </summary>
internal VarVec ReferencedColumns
{
get { return m_referencedColumns; }
}
/// <summary>
///
/// </summary>
internal VarVec NonNullableColumns { get { return m_nonnullableColumns; } }
/// <summary>
/// List of keys
/// </summary>
internal VarVec Keys { get { return m_keys; } }
/// <summary>
/// (internal) id for this table instance
/// </summary>
internal int TableId { get { return m_tableId; } }
/// <summary>
/// String form - for debugging
/// </summary>
/// <returns></returns>
public override string ToString()
{
return String.Format(CultureInfo.InvariantCulture, "{0}::{1}", m_tableMetadata.ToString(), this.TableId); ;
}
}
}
|