Ejemplo n.º 1
0
        public async void Table__Error_On_Null_Table_Builder(string tableBuilder)
        {
            var mb = @"var _ = mb.Database(nameof(Database))
                                 .Table(" + tableBuilder + @")
                                 .Build();";

            await VerifyErrorRaised(mb, BuilderError.ArgumentNull("table"), tableBuilder);
        }
 public static IDatabase <TDatabase> Database <TDatabase>(this IDatabaseBuilder <TDatabase> @this,
                                                          string name)
 {
     return(new InternalDatabaseBuilder <TDatabase>()
     {
         Name = name ?? throw BuilderError.ArgumentNull(nameof(name)).AsException(),
         Tables = new List <TableModel>()
     });
Ejemplo n.º 3
0
        public async void Table__Error_On_Null_Table_Selector(string selector)
        {
            var mb = @"var _ = mb.Database(nameof(Database))
                                 .Table(d => d.Table(" + selector + @")
                                              .Column(t => t.UserId, SqlType.Int)
                                              .Build())
                                 .Build();";

            await VerifyErrorRaised(mb, BuilderError.ArgumentNull("table"), selector);
        }
Ejemplo n.º 4
0
        //[InlineData("null as string")] GetConstantValue is not (currently) parsing "null as string" as a constant
        public async void Database__Error_On_Name_Null(string databaseName)
        {
            var mb = @"var _ = mb.Database(" + databaseName + @")
                                 .Table(d => d.Table(t => t.Users)
                                              .Column(t => t.UserId, SqlType.Int)
                                              .Build())
                                 .Build();";

            await VerifyErrorRaised(mb, BuilderError.ArgumentNull("name"), databaseName);
        }
Ejemplo n.º 5
0
        public async void Column__Error_On_Null_Column_Selector(string selector)
        {
            var mb = @"mb.Database(nameof(Database))
                         .Table(d => d.Table(t => t.Users)
                                      .Column(" + selector + @", SqlType.Int)
                                      .Build())
                         .Build();";

            await VerifyErrorRaised(mb, BuilderError.ArgumentNull("column"), selector);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        public SetQueryBase(QueryBase innerBuilder, Expression column, Expression value)
        {
            if (column == null)
            {
                throw BuilderError.ArgumentNull(nameof(column)).AsException();
            }

            var columnSelector = ExpressionHelpers.ParseSelector(column as LambdaExpression);

            if (value == null)
            {
                throw BuilderError.ArgumentNull(nameof(value)).AsException();
            }

            InnerQuery = innerBuilder;
            //Column = column;
            Value = value;
        }
Ejemplo n.º 8
0
        static FuzzyDatabaseModel ParseDatabase(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax expression)
        {
            var arguments = AH.ParseArguments(context, expression);
            var nameArg   = arguments["name"];

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

            var databaseModel = new FuzzyDatabaseModel()
            {
                Name   = AH.ParseConstantArgument <string>(context, nameArg, null),
                Tables = new List <FuzzyTableModel>()
            };

            return(databaseModel);
        }
Ejemplo n.º 9
0
        public GroupByQueryBase(QueryBase innerQuery, LambdaExpression keys)
        {
            InnerQuery = innerQuery;

            if (keys == null)
            {
                throw BuilderError.ArgumentNull(nameof(keys)).AsException();
            }

            // Keys must be named
            if (keys.Body is NewExpression newExpression)
            {
                KeyColumns = newExpression.Members.Zip(newExpression.Arguments, (l, r) => (Left: l, Right: r))
                             .Select(p => (p.Left as PropertyInfo, p.Right))
                             .ToImmutableArray();
            }
            else
            {
                throw QueryBuilderError.GroupByNotNewExpression().AsException();
            }
        }
Ejemplo n.º 10
0
        static FuzzyTableModel ParseTablePrimaryKey(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax expression, FuzzyDatabaseModel partialDatabase)
        {
            var innerModel = ParseTableChain(context, expression, partialDatabase);

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

            var keyColumnsArg = arguments["keyColumns"];
            var nameArg       = arguments["name"];
            var clusteredArg  = arguments["clustered"];

            var columns = 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)
                {
                    columns = AH.MatchColumns(context, props.Value, innerModel.Columns, ToString(innerModel.Name));
                }
            }

            var fuzzyPrimaryKey = new FuzzyPrimaryKeyModel()
            {
                Columns     = columns,
                IsClustered = AH.ParseConstantArgument(context, clusteredArg, () => AH.Just(true)),
                Name        = AH.ParseConstantArgument(context, nameArg, () => (!innerModel.Name.HasValue || !innerModel.Schema.HasValue || !columns.HasValue || columns.Value.Any(c => !c.Item2.Name.HasValue)) ?
                                                       new Optional <string>() :
                                                       AH.Just(BuildDefaultKeyName("PK", innerModel.Schema.Value, innerModel.Name.Value, columns.Value.Select(c => c.Item2.Name.Value))))
            };

            innerModel.PrimaryKey = fuzzyPrimaryKey;

            return(innerModel);
        }
Ejemplo n.º 11
0
        static FuzzyDatabaseModel ParseTable(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax expression)
        {
            var innerModel = ParseDatabaseChain(context, expression);

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

            var tableArg = arguments["table"];

            if (AH.IsNull(context, tableArg))
            {
                context.ReportDiagnostic(BuilderError.ArgumentNull("table").MakeDiagnostic(tableArg.GetLocation()));
            }
            else if (tableArg.Expression is MemberAccessExpressionSyntax)
            {
                var _ = context.SemanticModel.GetSymbolInfo(tableArg.Expression);

                // Static class function reference (e.g. User.ProvideModel)
                var staticFunctionMember = tableArg.Expression as MemberAccessExpressionSyntax;
                var className            = (staticFunctionMember.Expression as IdentifierNameSyntax).Identifier.Text;
                var functionName         = (staticFunctionMember?.Name?.Identifier)?.Text;

                throw new NotImplementedException();
            }
            else if (tableArg.Expression is SimpleLambdaExpressionSyntax)
            {
                // Defined inline
                //ParseTableChain(context, firstArgument.Expression);
                var lambdaExpression = tableArg.Expression as SimpleLambdaExpressionSyntax;

                var table = ParseTableBuild(context, lambdaExpression.Body as InvocationExpressionSyntax, innerModel);

                innerModel.Tables.Add(table);
            }

            return(innerModel);
        }
Ejemplo n.º 12
0
 public async void Error_On_Null_Keys_Expression(string groupBy, string location)
 {
     await VerifyGroupByErrorRaised(BuilderError.ArgumentNull("keys"), groupBy, location);
 }
Ejemplo n.º 13
0
 public async void Error_On_KeyColumns_Null(string location, string index)
 {
     await VerifyIndexErrorRaised(BuilderError.ArgumentNull("keyColumns"), location, index);
 }
Ejemplo n.º 14
0
 public async void Error_On_ReferenceColumns_Null(string location, string foreignKey)
 {
     await VerifyForeignKeyErrorRaised(BuilderError.ArgumentNull("referenceColumns"), foreignKey, location);
 }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
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);
        }
Ejemplo n.º 17
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);
        }
Ejemplo n.º 18
0
 public async void Error_On_Null_PrimaryKey_Selector(string location, string primaryKey)
 {
     await VerifyPrimaryKeyErrorRaised(BuilderError.ArgumentNull("keyColumns"), location, primaryKey);
 }