/// <summary> /// Initializes a new instance of the <see cref="Procedure"/> class. /// </summary> /// <param name="prefix">The prefix used on stored procedure names.</param> public Procedure(dac.TSqlObject tSqlObject, string prefix, IEnumerable<dac.TSqlObject> primaryKeys, IDictionary<dac.TSqlObject, IEnumerable<ForeignKeyConstraintDefinition>> foreignKeys) { this.Prefix = prefix ?? ""; this.RawName = tSqlObject.Name.Parts.Last(); this.Name = this.RawName.Substring(this.Prefix.Length); this.Parameters = tSqlObject.GetReferenced(dac.Procedure.Parameters).Select(x => new Parameter(x, primaryKeys, foreignKeys)); TSqlFragment fragment; TSqlModelUtils.TryGetFragmentForAnalysis(tSqlObject, out fragment); var selectVisitor = new SelectVisitor(); fragment.Accept(selectVisitor); var bodyColumnTypes = tSqlObject.GetReferenced(dac.Procedure.BodyDependencies) .Where(x => x.ObjectType.Name == "Column") .GroupBy(bd => string.Join(".", bd.Name.Parts)) .Select(grp => grp.First()) .ToDictionary( key => string.Join(".", key.Name.Parts), val => new DataType { Map = DataTypeHelper.Instance.GetMap(TypeFormat.SqlServerDbType, val.GetReferenced(dac.Column.DataType).First().Name.Parts.Last()), Nullable = dac.Column.Nullable.GetValue<bool>(val) }, StringComparer.InvariantCultureIgnoreCase); var unions = selectVisitor.Nodes.OfType<BinaryQueryExpression>().Select(bq => GetQueryFromUnion(bq)).Where(x => x != null); var selects = selectVisitor.Nodes.OfType<QuerySpecification>().Concat(unions); this.Selects = selects.Select(s => new Select(s, bodyColumnTypes)).ToList(); }
/// <summary> /// Initializes a new instance of the <see cref="Table" /> class. /// </summary> /// <param name="tSqlObject">The TSqlObject representing the table.</param> /// <param name="primaryKeys">The primary keys.</param> /// <param name="foreignKeys">The foreign keys.</param> public Table(dac.TSqlObject tSqlObject, IEnumerable<dac.TSqlObject> primaryKeys, IDictionary<dac.TSqlObject, IEnumerable<ForeignKeyConstraintDefinition>> foreignKeys) { // Get the name. this.Name = tSqlObject.Name.Parts.Last(); // Get the columns var columns = new List<Column>(); var sqlColumns = tSqlObject.ObjectType.Name == "TableType" ? tSqlObject.GetReferenced(dac.TableType.Columns) : tSqlObject.GetReferenced(dac.Table.Columns); foreach (var sqlColumn in sqlColumns) { var column = new Column(sqlColumn, tSqlObject, primaryKeys, foreignKeys); columns.Add(column); } this.Columns = columns; }
/// <summary> /// Parses the specified TSqlModel /// </summary> /// <param name="model">The model.</param> public void LoadModel(dac.TSqlModel model) { var sqlTables = model.GetObjects(dac.DacQueryScopes.UserDefined, dac.Table.TypeClass); var sqlViews = model.GetObjects(dac.DacQueryScopes.UserDefined, dac.View.TypeClass); var primaryKeys = model.GetObjects(dac.DacQueryScopes.UserDefined, dac.PrimaryKeyConstraint.TypeClass).Select(o => o.GetReferenced().Where(r => r.ObjectType.Name == "Column")).SelectMany(c=> c); var foreignKeyDictionaries = new[] { GetForeignKeys(sqlTables), GetForeignKeys(sqlViews) }; var foreignKeys = foreignKeyDictionaries .SelectMany(x => x) .ToDictionary(pair => pair.Key, pair => pair.Value); _tables = sqlTables.Select(sqlTable => new Table(sqlTable, primaryKeys, foreignKeys)).ToList(); _views = sqlViews.Select(sqlView => new View(sqlView, primaryKeys, foreignKeys)).ToList(); _procedures = model.GetObjects(dac.DacQueryScopes.UserDefined, dac.Procedure.TypeClass).Select(sqlProc => new Procedure(sqlProc, this.ProcedurePrefix, primaryKeys, foreignKeys)).ToList(); _modelLoaded = true; }