static string ToString(FuzzyTableModel table) { var schema = table.Schema.HasValue ? table.Schema.Value : "?"; var name = table.Name.HasValue ? table.Name.Value : "?"; return(schema == null ? name : $"{schema}.{name}"); }
static FuzzyTableModel ParseTableTable(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax expression, FuzzyDatabaseModel partialDatabase) { var arguments = AH.ParseArguments(context, expression); var tableArg = arguments["table"]; var nameArg = arguments["name"]; var schemaArg = arguments["schema"]; if (AH.IsNull(context, tableArg)) { context.ReportDiagnostic(BuilderError.ArgumentNull("table").MakeDiagnostic(tableArg.Expression.GetLocation())); } var property = AH.ParseSelector(context, tableArg); var fuzzyTableModel = new FuzzyTableModel() { Property = property, Columns = new List <FuzzyColumnModel>(), Indexes = new List <FuzzyIndexModel>(), ForeignKeys = new List <FuzzyForeignKeyModel>(), Schema = AH.ParseConstantArgument(context, schemaArg, () => AH.Just(null as string)), Name = AH.ParseConstantArgument(context, nameArg, () => property.HasValue ? AH.Just(property.Value?.Name) : new Optional <string>()) }; if (fuzzyTableModel.Property.HasValue) { var priorTable = partialDatabase.Tables.FirstOrDefault(t => t.Property.HasValue && t.Property.Value.Name == fuzzyTableModel.Property.Value.Name); if (priorTable != null) { context.ReportDiagnostic(ModelBuilderError.TableRepeatedSelector(ToString(partialDatabase.Name), ToString(fuzzyTableModel.Property), ToString(priorTable)).MakeDiagnostic(tableArg.GetLocation())); } } if (fuzzyTableModel.Name.HasValue && fuzzyTableModel.Schema.HasValue) { var priorTable = partialDatabase.Tables.FirstOrDefault(t => t.Name.HasValue && t.Schema.HasValue && t.Name.Value == fuzzyTableModel.Name.Value && t.Schema.Value == fuzzyTableModel.Schema.Value); if (priorTable != null) { context.ReportDiagnostic(ModelBuilderError.TableRepeatedName(ToString(priorTable)).MakeDiagnostic(nameArg != null ? nameArg.GetLocation() : tableArg.GetLocation(), schemaArg != null ? new List <Location>() { schemaArg.GetLocation() } : null)); } } return(fuzzyTableModel); }