示例#1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Column" /> class.
        /// </summary>
        /// <param name="tSqlObject">The TSqlObject representing the column.</param>
        /// <param name="tSqlTable">The table or view this column belongs to.</param>
        /// <param name="primaryKeys">The primary keys.</param>
        /// <param name="foreignKeys">The foreign keys.</param>
        public Column(dac.TSqlObject tSqlObject)
        {
            this.Name = tSqlObject.Name.Parts.Last();

            if (tSqlObject.ObjectType.Name == "TableTypeColumn")
            {
                var sqlDataTypeName = tSqlObject.GetReferenced(dac.TableTypeColumn.DataType).ToList().First().Name.Parts.Last();
                this.DataTypes  = DataTypeHelper.Instance.GetMap(TypeFormat.SqlServerDbType, sqlDataTypeName);
                this.IsIdentity = dac.TableTypeColumn.IsIdentity.GetValue <bool>(tSqlObject);
                this.IsNullable = dac.TableTypeColumn.Nullable.GetValue <bool>(tSqlObject);
                this.Precision  = dac.TableTypeColumn.Precision.GetValue <int>(tSqlObject);
                this.Scale      = dac.TableTypeColumn.Scale.GetValue <int>(tSqlObject);
                this.Length     = dac.TableTypeColumn.Length.GetValue <int>(tSqlObject);
            }
            else
            {
                dac.ColumnType metaType = tSqlObject.GetMetadata <dac.ColumnType>(dac.Column.ColumnType);

                switch (metaType)
                {
                case dac.ColumnType.Column:
                case dac.ColumnType.ColumnSet:
                    SetProperties(tSqlObject);
                    break;

                case dac.ColumnType.ComputedColumn:
                    // use the referenced column - this works for simple view referenced
                    // column but not for a computed expression like [Name] = [FirstName] + ' ' + [LastName]
                    var referenced = tSqlObject.GetReferenced().ToArray();
                    if (referenced.Length == 1)
                    {
                        var tSqlObjectReferenced = referenced[0];
                        SetProperties(tSqlObjectReferenced);
                    }
                    else
                    {
                        // TODO: how to get and evaluate the expression?
                    }
                    break;
                }
            }
        }
示例#2
0
        private static void ReadTheModel(TSqlModel model)
        {
            // This will get all tables.
            var tables = model.GetObjects(DacQueryScopes.Default, Table.TypeClass).ToList();

            // Look up a specific table by ID. Note that if no schema is defined when creating an element, the default "dbo" schema is used
            var t1 = model.GetObjects(Table.TypeClass, new ObjectIdentifier("dbo", "t1"), DacQueryScopes.UserDefined).FirstOrDefault();

            // Get a the column referenced by this table, and query its length
            TSqlObject column       = t1.GetReferenced(Table.Columns).First(col => col.Name.Parts[2].Equals("c1"));
            int        columnLength = column.GetProperty <int>(Column.Length);

            Console.WriteLine("Column c1 has length {0}", columnLength);

            // Verify the ColumnType of this column. This can help indicate which properties will return meaningful values.
            // For instance  since Column.Collation is only available on a simple column, and Column.Persisted is only on computed columns
            ColumnType columnType = column.GetMetadata <ColumnType>(Column.ColumnType);

            Console.WriteLine("Column c1 is of type '{0}'", columnType);
        }
示例#3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Column" /> class.
        /// </summary>
        /// <param name="tSqlObject">The TSqlObject representing the column.</param>
        /// <param name="tSqlTable">The table or view this column belongs to.</param>
        /// <param name="primaryKeys">The primary keys.</param>
        /// <param name="foreignKeys">The foreign keys.</param>
        public Column(dac.TSqlObject tSqlObject, dac.TSqlObject tSqlTable, IEnumerable <dac.TSqlObject> primaryKeys, IDictionary <dac.TSqlObject, IEnumerable <ForeignKeyConstraintDefinition> > foreignKeys)
        {
            this.Name = tSqlObject.Name.Parts.Last();
            var fullName = string.Join(".", tSqlObject.Name.Parts);

            this.IsPrimaryKey = primaryKeys.Any(p => string.Join(".", p.Name.Parts) == fullName);

            // Get relationships where this column is the child.
            IEnumerable <ForeignKeyConstraintDefinition> myForeignKeys;

            foreignKeys.TryGetValue(tSqlTable, out myForeignKeys);
            myForeignKeys            = myForeignKeys ?? Enumerable.Empty <ForeignKeyConstraintDefinition>();
            this.ParentRelationships = from f in myForeignKeys
                                       where f.Columns.Any(c => c.Value == this.Name)
                                       select new RelationshipIdentifier
            {
                TableOrView = f.ReferenceTableName.BaseIdentifier != null ? f.ReferenceTableName.BaseIdentifier.Value : null,
                Schema      = f.ReferenceTableName.SchemaIdentifier != null ? f.ReferenceTableName.SchemaIdentifier.Value : null,
                Database    = f.ReferenceTableName.DatabaseIdentifier != null ? f.ReferenceTableName.DatabaseIdentifier.Value : null,
                Columns     = f.ReferencedTableColumns.Select(c => c.Value)
            };
            this.IsForeignKey = this.ParentRelationships.Any();

            // Get relationships where this column is the parent.
            var childTables = foreignKeys.Where(f => f.Value.Any(v =>
                                                                 v.ReferenceTableName.BaseIdentifier.Value == tSqlTable.Name.Parts.Last() &&
                                                                 v.ReferencedTableColumns.Any(c => c.Value == this.Name)));

            this.ChildRelationships = from t in childTables
                                      from r in t.Value
                                      let tableParts = t.Key.Name.Parts.Count()
                                                       select new RelationshipIdentifier
            {
                TableOrView = t.Key.Name.Parts.Last(),
                Schema      = tableParts > 1 ? t.Key.Name.Parts.ElementAt(tableParts - 2) : null,
                Database    = tableParts > 2 ? t.Key.Name.Parts.ElementAt(tableParts - 3) : null,
                Columns     = r.Columns.Select(c => c.Value)
            };

            if (tSqlObject.ObjectType.Name == "TableTypeColumn")
            {
                var sqlDataTypeName = tSqlObject.GetReferenced(dac.TableTypeColumn.DataType).ToList().First().Name.Parts.Last();
                this.DataTypes  = DataTypeHelper.Instance.GetMap(TypeFormat.SqlServerDbType, sqlDataTypeName);
                this.IsIdentity = dac.TableTypeColumn.IsIdentity.GetValue <bool>(tSqlObject);
                this.IsNullable = dac.TableTypeColumn.Nullable.GetValue <bool>(tSqlObject);
                this.Precision  = dac.TableTypeColumn.Precision.GetValue <int>(tSqlObject);
                this.Scale      = dac.TableTypeColumn.Scale.GetValue <int>(tSqlObject);
                this.Length     = dac.TableTypeColumn.Length.GetValue <int>(tSqlObject);
            }
            else
            {
                dac.ColumnType metaType = tSqlObject.GetMetadata <dac.ColumnType>(dac.Column.ColumnType);

                switch (metaType)
                {
                case dac.ColumnType.Column:
                case dac.ColumnType.ColumnSet:
                    SetProperties(tSqlObject);
                    break;

                case dac.ColumnType.ComputedColumn:
                    // use the referenced column - this works for simple view referenced
                    // column but not for a computed expression like [Name] = [FirstName] + ' ' + [LastName]
                    var referenced = tSqlObject.GetReferenced().ToArray();
                    if (referenced.Length == 1)
                    {
                        var tSqlObjectReferenced = referenced[0];
                        SetProperties(tSqlObjectReferenced);
                    }
                    else
                    {
                        // TODO: how to get and evaluate the expression?
                    }
                    break;
                }
            }
        }