Exemplo n.º 1
0
        public static IForeignKey <TDatabase, TTable> ForeignKey <TDatabase, TTable, TReference>(this IForeignKeyBuilder <TDatabase, TTable> @this,
                                                                                                 Expression <Func <TTable, object> > keyColumns,
                                                                                                 Expression <Func <TDatabase, IEnumerable <TReference> > > referenceTable,
                                                                                                 Expression <Func <TReference, object> > referenceColumns,
                                                                                                 ForeignKeyAction updateAction = ForeignKeyAction.Cascade,
                                                                                                 ForeignKeyAction deleteAction = ForeignKeyAction.Cascade,
                                                                                                 string name = null)
        {
            var builder = @this as InternalTableBuilder <TDatabase, TTable>;

            var tempKeyColumns       = ExpressionHelpers.ParseMultiPropertySelector(keyColumns ?? throw ModelBuilderError.ArgumentNull(nameof(keyColumns)).AsException()).MatchColumns(builder.Name, builder.Columns);
            var tempReferenceTable   = ExpressionHelpers.ParseSelector(referenceTable ?? throw ModelBuilderError.ArgumentNull(nameof(referenceTable)).AsException());
            var tempReferenceColumns = ExpressionHelpers.ParseMultiPropertySelector(referenceColumns ?? throw ModelBuilderError.ArgumentNull(nameof(referenceColumns)).AsException());

            if (tempKeyColumns.Length != tempReferenceColumns.Length)
            {
                throw ModelBuilderError.ForeignKeyColumnCountsDontMatch().AsException();
            }

            builder.ForeignKeys.Add(new ForeignKeyModel(name: name,
                                                        keyColumns: tempKeyColumns.ToImmutableArray(),
                                                        referenceTable: tempReferenceTable,
                                                        referenceColumns: tempReferenceColumns.ToImmutableArray(),
                                                        updateAction: updateAction,
                                                        deleteAction: deleteAction));

            return(builder);
        }
Exemplo n.º 2
0
        public static ITable <TDatabase, TTable> Table <TDatabase, TTable>(this ITableBuilder <TDatabase> @this,
                                                                           Expression <Func <TDatabase, IEnumerable <TTable> > > table,
                                                                           string name   = null,
                                                                           string schema = null)
        {
            var builder = new InternalTableBuilder <TDatabase, TTable>((@this as TableBuilder <TDatabase>).DatabaseBuilder)
            {
                Property = ExpressionHelpers.ParsePropertySelector(table ?? throw ModelBuilderError.ArgumentNull(nameof(table)).AsException()),
                Schema   = schema
            };

            builder.Name = name ?? builder.Property.Name;

            {
                var priorTable = builder.DatabaseBuilder.Tables.FirstOrDefault(t => t.Property.Name == builder.Property.Name);
                if (priorTable != null)
                {
                    throw ModelBuilderError.TableRepeatedSelector(database: typeof(TDatabase).Name, property: builder.Property.Name, otherTable: BuilderHelper.GetTableName(priorTable.Schema, priorTable.Name)).AsException();
                }
            }

            if (builder.DatabaseBuilder.Tables.Any(t => t.Schema == builder.Schema && t.Name == builder.Name))
            {
                throw ModelBuilderError.TableRepeatedName(BuilderHelper.GetTableName(builder.Schema, builder.Name)).AsException();
            }

            return(builder);
        }
Exemplo n.º 3
0
        public async void Column__Error_On_Identity_Column_Nullable(string errorLocation, string column)
        {
            var mb = @"mb.Database(nameof(Database))
                         .Table(d => d.Table(t => t.Users)
                                      " + string.Format(column, errorLocation) + @"
                                      .Build())
                         .Build();";

            await VerifyErrorRaised(mb, ModelBuilderError.ColumnIdentityNullable(), errorLocation);
        }
Exemplo n.º 4
0
        public async void Error_On_Enum_Max_Length_Bigger_Than_String_Size(string errorLocation, string column)
        {
            var mb = @"mb.Database(nameof(Database))
                         .Table(d => d.Table(t => t.Users)
                                      " + string.Format(column, errorLocation) + @"
                                      .Build())
                         .Build();";

            await VerifyErrorRaised(mb, ModelBuilderError.ColumnEnumLongerThanMaxStringSize("UserType.Winner", 2), errorLocation);
        }
Exemplo n.º 5
0
        public async void Error_On_Enum_Backed_Column_Not_String_Or_Integral_Type(string errorLocation, string column)
        {
            var mb = @"mb.Database(nameof(Database))
                         .Table(d => d.Table(t => t.Users)
                                      " + string.Format(column, errorLocation) + @"
                                      .Build())
                         .Build();";

            await VerifyErrorRaised(mb, ModelBuilderError.ColumnEnumNotStringOrIntegralType(), errorLocation);
        }
Exemplo n.º 6
0
        public async void Column__Error_On_Repeated_Column_Name(string errorLocation, string column)
        {
            var mb = @"mb.Database(nameof(Database))
                         .Table(d => d.Table(t => t.Users)
                                      .Column(t => t.UserId, SqlType.Int, name: ""FirstName"")
                                      " + string.Format(column, errorLocation) + @"
                                      .Build())
                         .Build();";

            await VerifyErrorRaised(mb, ModelBuilderError.ColumnRepeatedName("Users", "FirstName"), errorLocation);
        }
Exemplo n.º 7
0
        public async void Column__Error_On_Repeated_Column_Selector()
        {
            var selector = "t => t.UserId";
            var mb       = @"mb.Database(nameof(Database))
                         .Table(d => d.Table(t => t.Users)
                                      .Column(" + selector + @", SqlType.Int, name: ""A"")
                                      .Column(" + selector + @", SqlType.Int)
                                      .Build())
                         .Build();";

            await VerifyErrorRaised(mb, ModelBuilderError.ColumnRepeatedSelector("User", "UserId", "A"), selector);
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        public static IIndex <TDatabase, TTable> Index <TDatabase, TTable>(this IIndexBuilder <TDatabase, TTable> @this,
                                                                           Expression <Func <IOrderFilter <TTable>, object> > keyColumns,
                                                                           bool unique    = false,
                                                                           string name    = null,
                                                                           bool clustered = false,
                                                                           Expression <Func <TTable, object> > includedColumns = null)
        {
            var builder = @this as InternalTableBuilder <TDatabase, TTable>;

            var indexKeyColumns      = ExpressionHelpers.ParseOrderedMultiPropertySelector(keyColumns ?? throw ModelBuilderError.ArgumentNull(nameof(keyColumns)).AsException()).MatchColumns(builder.Name, builder.Columns);
            var indexIncludedColumns = includedColumns == null?ImmutableArray.Create <ColumnModel>() : ExpressionHelpers.ParseMultiPropertySelector(includedColumns).MatchColumns(builder.Name, builder.Columns);

            var indexName = name ?? BuilderHelper.GenerateKeyName("IX", builder.Schema, builder.Name, indexKeyColumns.Select(c => c.Name));

            if (clustered)
            {
                var priorClustered = builder.PrimaryKey?.IsClustered == true ? builder.PrimaryKey.Name
                                                                             : builder.Indexes.FirstOrDefault(i => i.IsClustered)?.Name;
                if (priorClustered != null)
                {
                    throw ModelBuilderError.IndexClusteredAlreadySpecified(priorClustered).AsException();
                }
            }

            {
                var column = indexIncludedColumns.FirstOrDefault(i => indexKeyColumns.Any(k => k.Property.Name == i.Property.Name));
                if (column != null)
                {
                    throw ModelBuilderError.IndexIncludedColumnAlreadyInKeyColumns(column.Name).AsException();
                }
            }

            builder.Indexes.Add(new IndexModel(name: indexName,
                                               keyColumns: indexKeyColumns.ToImmutableArray(),
                                               isUnique: unique,
                                               isClustered: clustered,
                                               includedColumns: indexIncludedColumns.ToImmutableArray()));

            return(builder);
        }
Exemplo n.º 10
0
        static void ParseDatabaseBuild(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax expression)
        {
            var innerModel = ParseDatabaseChain(context, expression);

            foreach (var table in innerModel.Tables)
            {
                foreach (var foreignKey in table.ForeignKeys)
                {
                    if (foreignKey.ReferenceTableSelector.HasValue)
                    {
                        var referenceTable = innerModel.Tables.FirstOrDefault(t => t.Property.HasValue && t.Property.Value.Name == foreignKey.ReferenceTableSelector.Value.Item1.Name);

                        if (referenceTable == null)
                        {
                            context.ReportDiagnostic(BuilderError.SelectorNotMappedToTable(foreignKey.ReferenceTableSelector.Value.Item1.Name, ToString(innerModel.Name)).MakeDiagnostic(foreignKey.ReferenceTableSelector.Value.Item2));
                            foreignKey.ReferenceTable = new Optional <FuzzyTableModel>();
                            break;
                        }
                        else
                        {
                            foreignKey.ReferenceTable = new Optional <FuzzyTableModel>(referenceTable);
                        }

                        foreignKey.ReferenceColumns = AH.MatchColumns(context, foreignKey.ReferenceColumnSelectors.Value, referenceTable.Columns, ToString(referenceTable.Name));

                        if (!foreignKey.Name.HasValue &&
                            table.Schema.HasValue &&
                            table.Name.HasValue &&
                            foreignKey.KeyColumns.HasValue &&
                            foreignKey.KeyColumns.Value.All(k => k.Model.Name.HasValue) &&
                            foreignKey.ReferenceTable.HasValue &&
                            foreignKey.ReferenceTable.Value.Schema.HasValue &&
                            foreignKey.ReferenceTable.Value.Name.HasValue)
                        {
                            foreignKey.Name = BuilderHelper.GenerateForeignKeyName(table.Schema.Value,
                                                                                   table.Name.Value,
                                                                                   foreignKey.KeyColumns.Value.Select(k => k.Model.Name.Value),
                                                                                   foreignKey.ReferenceTable.Value.Schema.Value,
                                                                                   foreignKey.ReferenceTable.Value.Name.Value);
                        }

                        if (foreignKey.KeyColumns.HasValue)
                        {
                            foreach (var columnPair in foreignKey.KeyColumns.Value.Zip(foreignKey.ReferenceColumns.Value, (l, r) => (l, r)))
                            {
                                if (columnPair.Item1.Model.Type.HasValue && columnPair.Item2.Model.Type.HasValue && columnPair.Item1.Model.Type.Value != columnPair.Item2.Model.Type.Value)
                                {
                                    context.ReportDiagnostic(ModelBuilderError.ForeignKeyColumnTypesDontMatch(foreignKeyName: ToString(foreignKey.Name),
                                                                                                              keyColumnName: ToString(columnPair.Item1.Model.Name),
                                                                                                              keyColumnType: columnPair.Item1.Model.Type.Value.ToString(),
                                                                                                              referenceColumnName: ToString(columnPair.Item2.Model.Name),
                                                                                                              referenceColumnType: columnPair.Item2.Model.Type.Value.ToString()).MakeDiagnostic(columnPair.Item1.Location, new List <Location>()
                                    {
                                        columnPair.Item2.Location
                                    }));
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 11
0
        static FuzzyTableModel ParseTableIndex(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax expression, FuzzyDatabaseModel partialDatabase)
        {
            var innerModel = ParseTableChain(context, expression, partialDatabase);

            var arguments = AH.ParseArguments(context, expression);

            var keyColumnsArg      = arguments["keyColumns"];
            var uniqueArg          = arguments["unique"];
            var nameArg            = arguments["name"];
            var clusteredArg       = arguments["clustered"];
            var includedColumnsArg = arguments["includedColumns"];

            var keyColumns = new Optional <ImmutableArray <(SortOrder, FuzzyColumnModel)> >();

            if (AH.IsNull(context, keyColumnsArg))
            {
                context.ReportDiagnostic(BuilderError.ArgumentNull("keyColumns").MakeDiagnostic(keyColumnsArg.GetLocation()));
            }
            else
            {
                var props = AH.ParseOrderedMultiProperty(context, keyColumnsArg);
                if (props.HasValue)
                {
                    keyColumns = AH.MatchColumns(context, props.Value, innerModel.Columns, ToString(innerModel.Name));
                }
            }

            var includedColumns = new Optional <ImmutableArray <FuzzyColumnModel> >();

            if (!AH.IsNull(context, includedColumnsArg))
            {
                var props = AH.ParseMultiProperty(context, includedColumnsArg);
                if (props.HasValue)
                {
                    var match = AH.MatchColumns(context, props.Value, innerModel.Columns, ToString(innerModel.Name));
                    includedColumns = match.HasValue ? AH.Just(match.Value.Select(m => m.Item1).ToImmutableArray()) : new Optional <ImmutableArray <FuzzyColumnModel> >();

                    if (keyColumns.HasValue)
                    {
                        var column = props.Value.Select(v => ((FuzzyProperty, Location)?)v)
                                     .FirstOrDefault(i => keyColumns.Value.Any(k => k.Item2.Property.HasValue && k.Item2.Property.Value.Name == i.Value.Item1.Name));
                        if (column != null)
                        {
                            context.ReportDiagnostic(ModelBuilderError.IndexIncludedColumnAlreadyInKeyColumns(column.Value.Item1.Name).MakeDiagnostic(column.Value.Item2));
                        }
                    }
                }
            }

            var fuzzyIndex = new FuzzyIndexModel()
            {
                KeyColumns      = keyColumns,
                IsUnique        = AH.ParseConstantArgument(context, uniqueArg, () => AH.Just(false)),
                IsClustered     = AH.ParseConstantArgument(context, clusteredArg, () => AH.Just(false)),
                IncludedColumns = includedColumns,
                Name            = AH.ParseConstantArgument(context, nameArg, () => (!innerModel.Name.HasValue || !innerModel.Schema.HasValue || !keyColumns.HasValue || keyColumns.Value.Any(c => !c.Item2.Name.HasValue)) ?
                                                           new Optional <string>() :
                                                           AH.Just(BuildDefaultKeyName("IX", innerModel.Schema.Value, innerModel.Name.Value, keyColumns.Value.Select(c => c.Item2.Name.Value))))
            };

            if (fuzzyIndex.IsClustered.HasValue && fuzzyIndex.IsClustered.Value == true)
            {
                var priorClustered = (innerModel.PrimaryKey?.IsClustered)?.HasValue == true && innerModel.PrimaryKey.IsClustered.Value ?
                                     innerModel.PrimaryKey.Name.Value :
                                     innerModel.Indexes.FirstOrDefault(i => i.IsClustered.HasValue && i.IsClustered.Value)?.Name.Value;
                if (priorClustered != null)
                {
                    context.ReportDiagnostic(ModelBuilderError.IndexClusteredAlreadySpecified(priorClustered).MakeDiagnostic(clusteredArg.Expression.GetLocation()));
                }
            }

            innerModel.Indexes.Add(fuzzyIndex);

            return(innerModel);
        }
Exemplo n.º 12
0
 public async void Error_On_Column_Counts_Not_Matching(string location1, string location2, string foreignKey)
 {
     await VerifyForeignKeyErrorRaised(ModelBuilderError.ForeignKeyColumnCountsDontMatch(), foreignKey, location1, location2);
 }
Exemplo n.º 13
0
 public async void Error_On_Clustered_When_Previous_Index_Or_Primary_Key_Clustered(string location, string indexName, string index)
 {
     await VerifyIndexErrorRaised(ModelBuilderError.IndexClusteredAlreadySpecified(indexName), location, index);
 }
Exemplo n.º 14
0
 public async void Error_On_IncludedColumn_Already_In_KeyColumns(string location, string index)
 {
     await VerifyIndexErrorRaised(ModelBuilderError.IndexIncludedColumnAlreadyInKeyColumns("UserId"), location, index);
 }
Exemplo n.º 15
0
        public static IColumn <TDatabase, TTable> Column <TDatabase, TTable, TColumn>(this IColumnBuilder <TDatabase, TTable> @this,
                                                                                      Expression <Func <TTable, TColumn> > column,
                                                                                      SqlType type,
                                                                                      bool nullable        = false,
                                                                                      int?maxLength        = null,
                                                                                      string name          = null,
                                                                                      TColumn defaultValue = default(TColumn),
                                                                                      bool identity        = false,
                                                                                      IDatabaseTypeConverter <TColumn> converter = null)
        {
            var builder = @this as InternalTableBuilder <TDatabase, TTable>;

            var property = ExpressionHelpers.ParsePropertySelector(column ?? throw ModelBuilderError.ArgumentNull(nameof(column)).AsException());

            {
                var priorColumn = builder.Columns.FirstOrDefault(c => c.Property.Name == property.Name);
                if (priorColumn != null)
                {
                    throw ModelBuilderError.ColumnRepeatedSelector(typeof(TTable).Name, property.Name, priorColumn.Name).AsException();
                }
            }

            var columnName = name ?? property.Name;

            if (builder.Columns.Any(t => t.Name == columnName))
            {
                throw ModelBuilderError.ColumnRepeatedName(builder.Name, columnName).AsException();
            }

            if (nullable && identity)
            {
                throw ModelBuilderError.ColumnIdentityNullable().AsException();
            }

            if (property.Type.GetTypeInfo().IsEnum)
            {
                if (!(type == SqlType.String || SqlTypeHelpers.IsIntegral(type)))
                {
                    throw ModelBuilderError.ColumnEnumNotStringOrIntegralType().AsException();
                }

                var values = Enum.GetValues(property.Type);

                if (type == SqlType.String)
                {
                    var maxValue = values.Cast <object>()
                                   .Select(e => e.ToString())
                                   .OrderByDescending(e => e.Length)
                                   .First();

                    if (maxLength != null && maxValue.Length > maxLength)
                    {
                        throw ModelBuilderError.ColumnEnumLongerThanMaxStringSize($"{property.Type.ToString()}.{maxValue}", (int)maxLength).AsException();
                    }
                }
                else
                {
                }
            }

            builder.Columns.Add(new ColumnModel(name: columnName,
                                                property: property,
                                                sqlType: type,
                                                isNullable: nullable,
                                                isIdentity: identity,
                                                defaultValue: defaultValue));

            return(builder);
        }
Exemplo n.º 16
0
        static FuzzyTableModel ParseTableColumn(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax expression, FuzzyDatabaseModel partialDatabase)
        {
            var innerModel = ParseTableChain(context, expression, partialDatabase);

            var arguments       = AH.ParseArguments(context, expression);
            var columnArg       = arguments["column"];
            var typeArg         = arguments["type"];
            var nullableArg     = arguments["nullable"];
            var maxLengthArg    = arguments["maxLength"];
            var nameArg         = arguments["name"];
            var defaultValueArg = arguments["defaultValue"];
            var identityArg     = arguments["identity"];
            var converterArg    = arguments["converter"];

            if (AH.IsNull(context, columnArg))
            {
                context.ReportDiagnostic(BuilderError.ArgumentNull("column").MakeDiagnostic(columnArg.GetLocation()));
            }

            var property = AH.ParseSelector(context, columnArg);

            var fuzzyColumnModel = new FuzzyColumnModel()
            {
                Property   = property,
                MaxLength  = AH.ParseConstantArgument(context, maxLengthArg, () => AH.Just(null as int?)),
                IsIdentity = AH.ParseConstantArgument(context, identityArg, () => AH.Just(false)),
                IsNullable = AH.ParseConstantArgument(context, nullableArg, () => AH.Just(false)),
                Type       = AH.ParseConstantArgument(context, typeArg, () => new Optional <SqlType>()),
                Name       = AH.ParseConstantArgument(context, nameArg, () => !property.HasValue ?
                                                      new Optional <string>() :
                                                      AH.Just(property.Value?.Name))
            };



            if (fuzzyColumnModel.Property.HasValue)
            {
                var otherColumn = innerModel.Columns.FirstOrDefault(t => t.Property.HasValue && t.Property.Value.Name == fuzzyColumnModel.Property.Value.Name);
                if (otherColumn != null)
                {
                    context.ReportDiagnostic(ModelBuilderError.ColumnRepeatedSelector(ToTypeString(innerModel.Property), ToString(fuzzyColumnModel.Property), ToString(otherColumn.Name)).MakeDiagnostic(columnArg.GetLocation()));
                }
            }

            if (fuzzyColumnModel.Name.HasValue)
            {
                if (innerModel.Columns.Any(c => c.Name.HasValue &&
                                           c.Name.Value == fuzzyColumnModel.Name.Value))
                {
                    context.ReportDiagnostic(ModelBuilderError.ColumnRepeatedName(ToString(innerModel.Name), ToString(fuzzyColumnModel.Name)).MakeDiagnostic(nameArg == null ? columnArg.GetLocation() : nameArg.GetLocation()));
                }
            }

            if (fuzzyColumnModel.Property.HasValue && fuzzyColumnModel.Type.HasValue)
            {
                if (fuzzyColumnModel.Property.Value.Type.TypeKind == TypeKind.Enum)
                {
                    if (!(fuzzyColumnModel.Type.Value == SqlType.String || SqlTypeHelpers.IsIntegral(fuzzyColumnModel.Type.Value)))
                    {
                        context.ReportDiagnostic(ModelBuilderError.ColumnEnumNotStringOrIntegralType().MakeDiagnostic(typeArg.GetLocation()));
                    }

                    if (fuzzyColumnModel.Type.Value == SqlType.String && fuzzyColumnModel.MaxLength.HasValue)
                    {
                        var namedType   = fuzzyColumnModel.Property.Value.Type as INamedTypeSymbol;
                        var longestEnum = namedType.MemberNames.OrderByDescending(n => n.Length).First();

                        if (fuzzyColumnModel.MaxLength.Value != null &&
                            longestEnum.Length > fuzzyColumnModel.MaxLength.Value)
                        {
                            context.ReportDiagnostic(ModelBuilderError.ColumnEnumLongerThanMaxStringSize($"{namedType.Name}.{longestEnum}", (int)fuzzyColumnModel.MaxLength.Value).MakeDiagnostic(maxLengthArg.GetLocation()));
                        }
                    }
                }
                else if (fuzzyColumnModel.Property.Value.Type.Name == "DateTimeOffset" ||
                         fuzzyColumnModel.Property.Value.Type.Name == "Guid")
                {
                    //if ((fuzzyColumnModel.Property.Value.Type.Name == "DateTimeOffset" && fuzzyColumnModel.Type.Value != SqlType.DateTimeOffset) ||
                    //    (fuzzyColumnModel.Property.Value.Type.Name == "Guid" && fuzzyColumnModel.Type.Value != SqlType.Guid))
                    //    context.ReportDiagnostic(Diagnostic.Create(_descriptors[InvalidSqlType], typeArg.GetLocation()));
                }
                else
                {
                    var propertyType = fuzzyColumnModel.Property.Value.Type.SpecialType;

                    //if (!_defaultTypeMappings[propertyType].Contains(fuzzyColumnModel.Type.Value))
                    //    context.ReportDiagnostic(Diagnostic.Create(_descriptors[InvalidSqlType], typeArg.GetLocation()));
                }
            }

            if (fuzzyColumnModel.Type.HasValue &&
                fuzzyColumnModel.IsIdentity.HasValue &&
                fuzzyColumnModel.IsIdentity.Value == true)
            {
                var nonIdentityTypes = new List <SqlType>()
                {
                    SqlType.String,
                    SqlType.Bit,
                    SqlType.Date,
                    SqlType.Time,
                    SqlType.DateTime,
                    SqlType.DateTimeOffset,
                    SqlType.Single,
                    SqlType.Double,
                    SqlType.Guid
                };

                //if (nonIdentityTypes.Contains(fuzzyColumnModel.Type.Value))
                //    context.ReportDiagnostic(Diagnostic.Create(_descriptors[InvalidSqlTypeForIdentity], identityArg.GetLocation()));
            }

            if (fuzzyColumnModel.IsIdentity.HasValue && fuzzyColumnModel.IsIdentity.Value == true &&
                fuzzyColumnModel.IsNullable.HasValue && fuzzyColumnModel.IsNullable.Value == true)
            {
                context.ReportDiagnostic(ModelBuilderError.ColumnIdentityNullable().MakeDiagnostic(nullableArg.GetLocation()));
            }

            innerModel.Columns.Add(fuzzyColumnModel);

            return(innerModel);
        }
Exemplo n.º 17
0
 public async void Error_On_Column_Types_Not_Matching(string location1, string location2, string foreignKey)
 {
     await VerifyForeignKeyErrorRaised(ModelBuilderError.ForeignKeyColumnTypesDontMatch("FK_Users__UserType__Addresses", "UserType", "String", "AddressId", "Int"), foreignKey, location1, location2);
 }
Exemplo n.º 18
0
        static FuzzyTableModel ParseTableForeignKey(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax expression, FuzzyDatabaseModel partialDatabase)
        {
            var innerModel = ParseTableChain(context, expression, partialDatabase);

            var arguments = AH.ParseArguments(context, expression);

            var keyColumnsArg       = arguments["keyColumns"];
            var referenceTableArg   = arguments["referenceTable"];
            var referenceColumnsArg = arguments["referenceColumns"];
            var updateActionArg     = arguments["updateAction"];
            var deleteActionArg     = arguments["deleteAction"];
            var nameArg             = arguments["name"];

            var fuzzyForeignKey = new FuzzyForeignKeyModel();

            if (AH.IsNull(context, keyColumnsArg))
            {
                context.ReportDiagnostic(BuilderError.ArgumentNull("keyColumns").MakeDiagnostic(keyColumnsArg.Expression.GetLocation()));
                fuzzyForeignKey.KeyColumns = new Optional <ImmutableArray <(FuzzyColumnModel, Location)> >();
            }
            else
            {
                var keyColumns = AH.ParseMultiProperty(context, keyColumnsArg);

                if (keyColumns.HasValue)
                {
                    fuzzyForeignKey.KeyColumns = AH.MatchColumns(context, keyColumns.Value, innerModel.Columns, ToString(innerModel.Name));
                }
            }

            if (AH.IsNull(context, referenceTableArg))
            {
                context.ReportDiagnostic(BuilderError.ArgumentNull("referenceTable").MakeDiagnostic(referenceTableArg.Expression.GetLocation()));
                fuzzyForeignKey.ReferenceTableSelector = new Optional <(FuzzyProperty, Location)>();
            }
            else
            {
                var selector = AH.ParseSelector(context, referenceTableArg);
                fuzzyForeignKey.ReferenceTableSelector = selector.HasValue ? AH.Just((selector.Value, (referenceTableArg.Expression as LambdaExpressionSyntax).Body.GetLocation())) :
                                                         new Optional <(FuzzyProperty, Location)>();
            }

            if (AH.IsNull(context, referenceColumnsArg))
            {
                context.ReportDiagnostic(BuilderError.ArgumentNull("referenceColumns").MakeDiagnostic(referenceColumnsArg.Expression.GetLocation()));
                fuzzyForeignKey.ReferenceColumnSelectors = new Optional <ImmutableArray <(FuzzyProperty, Location)> >();
            }
            else
            {
                fuzzyForeignKey.ReferenceColumnSelectors = AH.ParseMultiProperty(context, referenceColumnsArg);
            }

            if (fuzzyForeignKey.KeyColumns.HasValue &&
                fuzzyForeignKey.ReferenceColumnSelectors.HasValue &&
                fuzzyForeignKey.KeyColumns.Value.Length != fuzzyForeignKey.ReferenceColumnSelectors.Value.Length)
            {
                context.ReportDiagnostic(ModelBuilderError.ForeignKeyColumnCountsDontMatch().MakeDiagnostic(AH.DeconstructLambda(keyColumnsArg).Body.GetLocation(), new List <Location>()
                {
                    AH.DeconstructLambda(referenceColumnsArg).Body.GetLocation()
                }));
            }

            fuzzyForeignKey.UpdateAction = AH.ParseConstantArgument(context, updateActionArg, () => AH.Just(ForeignKeyAction.Cascade));
            fuzzyForeignKey.DeleteAction = AH.ParseConstantArgument(context, deleteActionArg, () => AH.Just(ForeignKeyAction.Cascade));

            innerModel.ForeignKeys.Add(fuzzyForeignKey);

            return(innerModel);
        }
Exemplo n.º 19
0
 public async void Table__Error_On_Repeated_Table_Name(string error, string additionalLocation, string name, string mb)
 {
     await VerifyErrorRaised(mb, ModelBuilderError.TableRepeatedName(name), error, additionalLocation);
 }
Exemplo n.º 20
0
 public async void Table__Error_On_Repeated_Table_Selector(string error, string mb)
 {
     await VerifyErrorRaised(mb, ModelBuilderError.TableRepeatedSelector("Database", "Users", "Users"), error);
 }
Exemplo n.º 21
0
        public static IPrimaryKey <TDatabase, TTable> PrimaryKey <TDatabase, TTable>(this IPrimaryKeyBuilder <TDatabase, TTable> @this,
                                                                                     Expression <Func <IOrderFilter <TTable>, object> > keyColumns,
                                                                                     string name    = null,
                                                                                     bool clustered = true)
        {
            var builder = @this as InternalTableBuilder <TDatabase, TTable>;

            var columns = ExpressionHelpers.ParseOrderedMultiPropertySelector(keyColumns ?? throw ModelBuilderError.ArgumentNull(nameof(keyColumns)).AsException())
                          .MatchColumns(builder.Name, builder.Columns);

            var primaryKeyName = name ?? BuilderHelper.GenerateKeyName("PK", builder.Schema, builder.Name, columns.Select(c => c.Name));

            builder.PrimaryKey = new PrimaryKeyModel(primaryKeyName,
                                                     columns.ToImmutableArray(),
                                                     clustered);

            return(builder);
        }