19 using System.Collections.Generic;
20 using System.Collections.ObjectModel;
30 namespace Deveel.Data.Sql.Tables {
42 private readonly IList<ColumnInfo>
columns;
55 : this(tableName, -1, false, new List<
ColumnInfo>(), false) {
58 private TableInfo(
ObjectName tableName,
int id,
bool perm, IList<ColumnInfo> columns,
bool isReadOnly) {
59 if (tableName == null)
60 throw new ArgumentNullException(
"tableName");
62 TableName = tableName;
65 this.columns = columns;
66 IsReadOnly = isReadOnly;
68 columnsCache =
new Dictionary<ObjectName, int>();
77 columns =
new List<ColumnInfo>();
79 if (columnInfo != null) {
80 foreach (var info
in columnInfo) {
86 columnsCache =
new Dictionary<ObjectName, int>();
100 get {
return TableName; }
107 public int Id {
get;
private set; }
113 public bool IsPermanent {
get;
private set; }
121 get {
return TableName.
Name; }
130 get {
return TableName.
Parent; }
136 public string CatalogName {
137 get {
return SchemaName != null && SchemaName.
Parent != null ? SchemaName.
Parent.
Name : null; }
153 public bool IsReadOnly {
get;
private set; }
159 public int ColumnCount {
160 get {
return columns.Count; }
178 if (offset < 0 || offset >= columns.Count)
179 throw new ArgumentOutOfRangeException(
"offset");
181 return columns[offset];
187 throw new InvalidOperationException();
196 data.
SetValue(
"TableName", TableName);
198 data.
SetValue(
"ReadOnly", IsReadOnly);
199 data.
SetValue(
"Permanent", IsPermanent);
200 data.
SetValue(
"Columns", columns.ToArray());
205 throw new ArgumentNullException(
"column");
209 columnsCache.Clear();
232 throw new ArgumentNullException(
"column");
238 throw new ArgumentException(
String.Format(
"The column {0} belongs to another table already ({1})", column.
ColumnName,
241 if (columns.Any(x => x.ColumnName == column.
ColumnName))
242 throw new ArgumentException(
String.Format(
"Column {0} is already defined in table {1}", column.
ColumnName, TableName));
244 columnsCache.Clear();
260 return AddColumn(columnName, columnType,
false);
280 if (
String.IsNullOrEmpty(columnName))
281 throw new ArgumentNullException(
"columnName");
283 if (columnType == null)
284 throw new ArgumentNullException(
"columnType");
286 var column =
new ColumnInfo(columnName, columnType);
287 column.IsNotNull = notNull;
294 return columns.GetEnumerator();
297 IEnumerator IEnumerable.GetEnumerator() {
298 return GetEnumerator();
311 return IndexOfColumn(
new ObjectName(TableName, columnName));
316 if (!columnsCache.TryGetValue(columnName, out index)) {
318 for (
int i = 0; i < columns.Count; i++) {
319 var column = columns[i];
320 if (column.ColumnName.Equals(columnName.
Name, StringComparison.Ordinal)) {
322 columnsCache[columnName] = index;
343 return new TableInfo(TableName, Id, IsPermanent,
new ReadOnlyCollection<ColumnInfo>(columns),
true);
347 return new TableInfo(alias, Id, IsPermanent,
new ReadOnlyCollection<ColumnInfo>(columns),
true);
352 return visitor.Visit(expression);
360 this.tableInfo = tableInfo;
361 this.ignoreCase = ignoreCase;
368 var comparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
370 foreach (var columnInfo
in tableInfo) {
371 if (
String.Equals(colName, columnInfo.ColumnName, comparison)) {
381 if (columnNames == null)
384 return columnNames.Select(IndexOfColumn).ToArray();
388 var writer =
new BinaryWriter(stream, Encoding.Unicode);
389 Serialize(tableInfo, writer);
399 writer.Write(catName);
401 writer.Write(tableInfo.
Name);
403 var colCount = tableInfo.
columns.Count;
404 writer.Write(colCount);
405 for (
int i = 0; i < colCount; i++) {
406 var column = tableInfo.
columns[i];
412 var reader =
new BinaryReader(stream, Encoding.Unicode);
413 return Deserialize(reader, typeResolver);
417 var version = reader.ReadInt32();
419 throw new FormatException(
"Invalid version of the table info.");
421 var catName = reader.ReadString();
422 var schemName = reader.ReadString();
423 var tableName = reader.ReadString();
425 var objSchemaName = !
String.IsNullOrEmpty(catName)
429 var objTableName =
new ObjectName(objSchemaName, tableName);
431 var tableInfo =
new TableInfo(objTableName);
433 var colCount = reader.ReadInt32();
434 for (
int i = 0; i < colCount; i++) {
437 if (columnInfo != null)
438 tableInfo.AddColumn(columnInfo);
Defines the metadata properties of a column within a table of a database.
ColumnInfo AddColumn(string columnName, SqlType columnType)
Adds a new column to the table having the given name and type.
static TableInfo Deserialize(Stream stream, ITypeResolver typeResolver)
string Name
Gets the name part of the table name.
ObjectName ReferenceName
Gets the name of the object referenced by the expression.
void GetData(SerializeData data)
readonly Dictionary< ObjectName, int > columnsCache
An expression that references an object within a context.
A long string in the system.
TableInfo(ObjectName tableName)
Constructs the object with the given table name.
void AddColumnSafe(ColumnInfo column)
readonly TableInfo tableInfo
string CatalogName
Gets the name of the catalog containing the table, if defined.
int IndexOfColumn(ObjectName columnName)
ColumnsResolver(TableInfo tableInfo, bool ignoreCase)
TableInfo(ObjectData data)
void SetValue(string key, Type type, object value)
Describes the name of an object within a database.
TableInfo TableInfo
Gets the table where the column is attached to.
TableInfo Alias(ObjectName alias)
void AddColumn(ColumnInfo column)
Adds a new column to the table at the last position of the columns list in the table metadata...
static void Serialize(TableInfo tableInfo, BinaryWriter writer)
TableInfo AsReadOnly()
Creates a new instance of TableInfo as an immutable copy of this table metadata.
static ColumnInfo Deserialize(Stream stream, ITypeResolver typeResolver)
static TableInfo Deserialize(BinaryReader reader, ITypeResolver typeResolver)
static void Serialize(ColumnInfo columnInfo, BinaryWriter writer)
Defines the properties of a specific SQL Type and handles the values compatible.
string ColumnName
Gets the name of the column.
TableInfo(ObjectName tableName, int id, bool perm, IList< ColumnInfo > columns, bool isReadOnly)
int IndexOfColumn(string columnName)
Gets the offset of the column with the given name.
ColumnInfo AddColumn(string columnName, SqlType columnType, bool notNull)
Adds a new column to the table having the given name and type.
ObjectName Parent
Gets the parent reference of the current one, if any or null if none.
string Name
Gets the name of the object being referenced.
static SqlReferenceExpression Reference(ObjectName objectName)
ObjectName SchemaName
Gets the schema name part of the table name.
bool GetBoolean(string key)
object GetValue(string key)
override SqlExpression VisitReference(SqlReferenceExpression reference)
readonly IList< ColumnInfo > columns
Defines the base class for instances that represent SQL expression tree nodes.
DbObjectType
The kind of objects that can be handled by a database system and its managers
IEnumerator< ColumnInfo > GetEnumerator()
Defines the metadata properties of a table existing within a database.
A visitor for SqlExpression objects.
IEnumerable< int > IndexOfColumns(IEnumerable< string > columnNames)
SqlExpression ResolveColumns(bool ignoreCase, SqlExpression expression)
static void Serialize(TableInfo tableInfo, Stream stream)